Skip to content

Commit 6075b9c

Browse files
michaelfaithljharb
authored andcommitted
[utils] [fix] parse: 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 d66933c commit 6075b9c

File tree

9 files changed

+27
-4
lines changed

9 files changed

+27
-4
lines changed

.nycrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"resolvers/*/test",
1515
"scripts",
1616
"memo-parser",
17-
"lib"
17+
"lib",
18+
"examples"
1819
]
1920
}

examples/flat/eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default [
2020
'import/no-dynamic-require': 'warn',
2121
'import/no-nodejs-modules': 'warn',
2222
'import/no-unused-modules': ['warn', { unusedExports: true }],
23+
'import/no-cycle': 'warn',
2324
},
2425
},
2526
];

examples/flat/src/depth-zero.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { foo } from "./es6/depth-one-dynamic";
2+
3+
foo();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function foo() {}
2+
3+
export const bar = () => import("../depth-zero").then(({foo}) => foo);

examples/legacy/.eslintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ module.exports = {
2020
'import/no-dynamic-require': 'warn',
2121
'import/no-nodejs-modules': 'warn',
2222
'import/no-unused-modules': ['warn', { unusedExports: true }],
23+
'import/no-cycle': 'warn',
2324
},
2425
};

examples/legacy/src/depth-zero.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { foo } from "./es6/depth-one-dynamic";
2+
3+
foo();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function foo() {}
2+
3+
export const bar = () => import("../depth-zero").then(({ foo }) => foo);

utils/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
77

88
### Fixed
99
- `parse`: remove unneeded extra backticks ([#3057], thanks [@G-Rath])
10+
- `parse`: espree parser isn't working with flat config ([#3061], thanks [@michaelfaith])
1011

1112
## v2.11.0 - 2024-09-05
1213

@@ -168,6 +169,7 @@ Yanked due to critical issue with cache key resulting from #839.
168169
### Fixed
169170
- `unambiguous.test()` regex is now properly in multiline mode
170171

172+
[#3061]: https://github.com/import-js/eslint-plugin-import/pull/3061
171173
[#3057]: https://github.com/import-js/eslint-plugin-import/pull/3057
172174
[#3049]: https://github.com/import-js/eslint-plugin-import/pull/3049
173175
[#3039]: https://github.com/import-js/eslint-plugin-import/pull/3039

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)