Skip to content

Commit

Permalink
fix(utils): espree parser isn't working with flat config
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
michaelfaith committed Sep 16, 2024
1 parent f72f207 commit 3829bd7
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions examples/flat/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default [
'import/no-dynamic-require': 'warn',
'import/no-nodejs-modules': 'warn',
'import/no-unused-modules': ['warn', { unusedExports: true }],
'import/no-cycle': 'warn',
},
},
];
3 changes: 3 additions & 0 deletions examples/flat/src/depth-zero.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { foo } from "./es6/depth-one-dynamic";

foo();
3 changes: 3 additions & 0 deletions examples/flat/src/es6/depth-one-dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo() {}

export const bar = () => import("../depth-zero").then(({foo}) => foo);
1 change: 1 addition & 0 deletions examples/legacy/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ module.exports = {
'import/no-dynamic-require': 'warn',
'import/no-nodejs-modules': 'warn',
'import/no-unused-modules': ['warn', { unusedExports: true }],
'import/no-cycle': 'warn',
},
};
3 changes: 3 additions & 0 deletions examples/legacy/src/depth-zero.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { foo } from "./es6/depth-one-dynamic";

foo();
3 changes: 3 additions & 0 deletions examples/legacy/src/es6/depth-one-dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo() {}

export const bar = () => import("../depth-zero").then(({foo}) => foo);
12 changes: 9 additions & 3 deletions utils/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ function keysFromParser(parserPath, parserInstance, parsedResult) {
if (parsedResult && parsedResult.visitorKeys) {
return parsedResult.visitorKeys;
}
if (typeof parserPath === 'string' && (/.*espree.*/).test(parserPath)) {
return parserInstance.VisitorKeys;
}
// The old babel parser doesn't have a `parseForESLint` eslint function, so we don't end
// up with a `parsedResult` here. It also doesn't expose the visitor keys on the parser itself,
// so we have to try and infer the visitor-keys module from the parserPath.
// This is NOT supported in flat config!
if (typeof parserPath === 'string' && (/.*babel-eslint.*/).test(parserPath)) {
return getBabelEslintVisitorKeys(parserPath);
}
// The espree parser doesn't have the `parseForESLint` function, so we don't ended up with a
// `parsedResult` here, but it does expose the visitor keys on the parser instance that we can use.
if (parserInstance && parserInstance.VisitorKeys) {
return parserInstance.VisitorKeys;
}
return null;
}

Expand Down

0 comments on commit 3829bd7

Please sign in to comment.