Skip to content

Commit f8565f4

Browse files
committed
fix(utils): espree parser isn't working with flat config
This change addresses an issue with using the espree parser with flat config. `keysFromParser` is responsible for finding the `visitorKeys` from a combination of the parser instance, parserPath, and the results of a call to `parseForESLint`. When using flat config, the `parserPath` will not be on the context object., and there are no results from `parseForESLint`. The espree parser _does_ provide visitor keys as a prop on the parser itself. However, this logic was only returning it when it found that "espree" was somewhere in the `parserPath` (which doesn't exist on flat config). I changed it so that it first does the check for the old-school babel parser using parser path, and then just checks the parser instance for the presence of `VisitorKeys`, rather than using parserPath at all. The reason for the re-order is that if the old babel-eslint parser, for some reason has `VisitorKeys`, having the new condition first, would change the behavior.
1 parent f72f207 commit f8565f4

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

utils/parse.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@ function keysFromParser(parserPath, parserInstance, parsedResult) {
2929
if (parsedResult && parsedResult.visitorKeys) {
3030
return parsedResult.visitorKeys;
3131
}
32-
if (typeof parserPath === 'string' && (/.*espree.*/).test(parserPath)) {
33-
return parserInstance.VisitorKeys;
34-
}
32+
// The old babel parser doesn't have a `parseForESLint` eslint function, so we don't end
33+
// up with a `parsedResult` here. It also doesn't expose the visitor keys on the parser itself,
34+
// so we have to try and infer the visitor-keys module from the parserPath.
35+
// This is NOT supported in flat config!
3536
if (typeof parserPath === 'string' && (/.*babel-eslint.*/).test(parserPath)) {
3637
return getBabelEslintVisitorKeys(parserPath);
3738
}
39+
// The espree parser doesn't have the `parseForESLint` function, so we don't ended up with a
40+
// `parsedResult` here, but it does expose the visitor keys on the parser instance that we can use.
41+
if (parserInstance && parserInstance.VisitorKeys) {
42+
return parserInstance.VisitorKeys;
43+
}
3844
return null;
3945
}
4046

0 commit comments

Comments
 (0)