From 8b48ae87716554c232c2c6e965225182a86551ef Mon Sep 17 00:00:00 2001 From: Victor Hom Date: Sun, 25 Jun 2017 20:12:46 -0400 Subject: [PATCH] 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 --- docs/developer-guide/working-with-plugins.md | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/docs/developer-guide/working-with-plugins.md b/docs/developer-guide/working-with-plugins.md index b8667d33afa..c3bbccc480e 100644 --- a/docs/developer-guide/working-with-plugins.md +++ b/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"); + } + } + }; +}; + +``` + +