Skip to content

Commit

Permalink
Docs: Add doc on parser services (fixes #8390) (#8795)
Browse files Browse the repository at this point in the history
* Docs: Add doc on parser services (fixes #8390)

* fix doc

* add one update to doc

* fix sentence

* correct custom from customer
  • Loading branch information
VictorHom authored and ilyavolodin committed Jun 26, 2017
1 parent 0d041e7 commit 8b48ae8
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/developer-guide/working-with-plugins.md
Expand Up @@ -218,3 +218,32 @@ Add these keywords into your `package.json` file to make it easy for others to f
## Further Reading

* [npm Developer Guide](https://docs.npmjs.com/misc/developers)

### Working with Custom Parsers

If you want to use your own parser and provide additional capabilities for your rules, you can specify your own custom parser. By default, the ESLint parser will use its parse method that takes in the source code as a first parameter and additional optional parameters as a second parameter to create an AST. You can specify a `parse` configuration to use your own custom parser. If a `parseForESLint` method is exposed, this method will be used to parse. Otherwise, the parser will use the parse method. `parseForESLint` behaves like `parse` and takes in the the source code and optional ESLint configurations. When `parseForESLint` is called, the method should return an object that contains the required property `ast` and an optional `services` property. `ast` should contain the AST. The `services` property contains the parser-dependent services. The value of the service property is available to rules as `context.parserServices`

If no parseForESLint function is found, the parser will use the default parse method with the source code and the parser options. You can find a ESLint parser project [here](https://github.com/eslint/typescript-eslint-parser).

{

"parser": './path/to/awesome-custom-parser.js'
}

```javascript
var espree = require("espree");
// awesome-custom-parser.js
exports.parseForESLint = function(code, options) {
return {
ast: espree.parse(code, options),
services: {
foo: function() {
console.log("foo");
}
}
};
};

```


0 comments on commit 8b48ae8

Please sign in to comment.