Skip to content

Commit 5c91996

Browse files
committed
fix(utils): add ecmaVersion and sourceType to parserOptions
This change adds `ecmaVersion` and `sourceType` to the options we're passing to the parsers, if they're present on `languageOptions` (which would only be the case for flat config).
1 parent 7c53e51 commit 5c91996

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

utils/parse.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function keysFromParser(parserPath, parserInstance, parsedResult) {
3636
if (typeof parserPath === 'string' && (/.*babel-eslint.*/).test(parserPath)) {
3737
return getBabelEslintVisitorKeys(parserPath);
3838
}
39-
// The espree parser doesn't have the `parseForESLint` function, so we don't ended up with a
39+
// The espree parser doesn't have the `parseForESLint` function, so we don't end up with a
4040
// `parsedResult` here, but it does expose the visitor keys on the parser instance that we can use.
4141
if (parserInstance && parserInstance.VisitorKeys) {
4242
return parserInstance.VisitorKeys;
@@ -113,7 +113,8 @@ exports.default = function parse(path, content, context) {
113113
if (context == null) { throw new Error('need context to parse properly'); }
114114

115115
// ESLint in "flat" mode only sets context.languageOptions.parserOptions
116-
let parserOptions = context.languageOptions && context.languageOptions.parserOptions || context.parserOptions;
116+
const languageOptions = context.languageOptions;
117+
let parserOptions = languageOptions && languageOptions.parserOptions || context.parserOptions;
117118
const parserOrPath = getParser(path, context);
118119

119120
if (!parserOrPath) { throw new Error('parserPath or languageOptions.parser is required!'); }
@@ -144,6 +145,17 @@ exports.default = function parse(path, content, context) {
144145
delete parserOptions.project;
145146
delete parserOptions.projects;
146147

148+
// If this is a flat config, we need to add ecmaVersion and sourceType (if present) from languageOptions
149+
if (languageOptions && languageOptions.ecmaVersion) {
150+
parserOptions.ecmaVersion = languageOptions.ecmaVersion;
151+
}
152+
if (languageOptions && languageOptions.sourceType) {
153+
// @ts-expect-error languageOptions is from the flatConfig Linter type in 8.57 while parserOptions is not.
154+
// Non-flat config parserOptions.sourceType doesn't have "commonjs" in the type. Once upgraded to v9 types,
155+
// they'll be the same and this expect-error should be removed.
156+
parserOptions.sourceType = languageOptions.sourceType;
157+
}
158+
147159
// require the parser relative to the main module (i.e., ESLint)
148160
const parser = typeof parserOrPath === 'string' ? moduleRequire(parserOrPath) : parserOrPath;
149161

0 commit comments

Comments
 (0)