Permalink
Browse files

Docs: Add doc on parser services (fixes #8390) (#8795)

* 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 8b48ae87716554c232c2c6e965225182a86551ef
Showing with 29 additions and 0 deletions.
  1. +29 −0 docs/developer-guide/working-with-plugins.md
@@ -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.