eslint with class property
How `babel-eslint` work with `eslint`, and the general workflow for linting and ast basics
Last updated
Was this helpful?
How `babel-eslint` work with `eslint`, and the general workflow for linting and ast basics
Last updated
Was this helpful?
On github:
If class property is used in class, eslint
requires babel-eslint
as parser (defaulted as espree
) to parse the code before actual linting. For some reason, an error of eslint(no-undef)
is always shown on the class property.
Firstly, I thought reason is @babel/plugin-proposal-class-properties
is not provided for babel-eslint
, while then I found actually babel-eslint
always have class property as default plugin.
This should be solved by
with fix from eslint/eslint
, and fix from babel/babel-eslint
. Thanks to for the hard work.
--------
Explanation: This is a issue caused by the 'scope analyze' in eslint. In the older version (<4.14.0), eslint analyze the scope by its own without passing in the correct childVisitorKeys
attribute.
After those two PRs, babel-eslint
will generate the scopeManager
with its own modification. eslint
will consume the scopeManager
(const scopeManager = parseResult.scopeManager;
), and apply rules over it.
It costs time to locate the root cause for this kind of issue if you are not familiar with the massive project like eslint (or ast concept), not to mention it normally comes with several more issues together.
If you really want to do the research, start from the rules
folder and analyze specific rule to get two cents about what goes wrong, in this case, it's the scope. It would be much easier then to narrow down your research in scope-related logic in parse()
.
The easy way to 'just make things work' maybe is always upgrade version.
eslint.js
(entry) -> CLIEngine(options)
-> processWithText(text, options)
-> linter.verify(text, options)
Inside verify(), sourceCode = parse(text, parser, parseOptions)
-> analyze sourceCode.ast
load and analyze an ast for a 200 line file is not too practical
sharedTraversalContext
contains scope info
scope analyzer
is important for rules to check scope and variables
A correct scope after parser.parse()
requires corresponding childVisitorKeys
defined by different parser, as explains