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. If a parseForESLint method is exposed on the parser, this method will be used to parse the code. Otherwise, the parse method will be used. Both methods should take in the source code as the first argument, and an optional configuration object as the second argument (provided as parserOptions in a config file). The parse method should simply return the AST. The parseForESLint method should return an object that contains the required property ast and optional properties services, scopeManager, and visitorKeys.

You can find an ESLint parser project here.

{
    "parser": "./path/to/awesome-custom-parser.js"
}
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");
            }
        },
        scopeManager: null,
        visitorKeys: null
    };
};

The AST specification

The AST that custom parsers should create is based on ESTree. The AST requires some additional properties about detail information of the source code.

All nodes:

All nodes must have range property.

The parent property of all nodes must be rewriteable. ESLint sets each node's parent property to its parent node while traversing, before any rules have access to the AST.

The Program node:

The Program node must have tokens and comments properties. Both properties are an array of the below Token interface.

interface Token {
    type: string;
    loc: SourceLocation;
    range: [number, number]; // See "All nodes:" section for details of `range` property.
    value: string;
}

The range indexes of all tokens and comments must not overlap with the range of other tokens and comments.

The Literal node:

The Literal node must have raw property.