Skip to content

Commit

Permalink
fix(utils): add ecmaVersion and sourceType to parserOptions
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
michaelfaith committed Sep 18, 2024
1 parent 7c53e51 commit add0fdc
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion utils/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ exports.default = function parse(path, content, context) {
if (context == null) { throw new Error('need context to parse properly'); }

// ESLint in "flat" mode only sets context.languageOptions.parserOptions
let parserOptions = context.languageOptions && context.languageOptions.parserOptions || context.parserOptions;
const languageOptions = context.languageOptions;
let parserOptions = languageOptions && languageOptions.parserOptions || context.parserOptions;
const parserOrPath = getParser(path, context);

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

// If this is a flat config, we need to add ecmaVersion and sourceType (if present) from languageOptions
if (languageOptions && languageOptions.ecmaVersion) {
parserOptions.ecmaVersion = languageOptions.ecmaVersion;
}
if (languageOptions && languageOptions.sourceType) {
// @ts-expect-error languageOptions is from the flatConfig Linter type in 8.57 while parserOptions is not.
// Non-flat config parserOptions.sourceType doesn't have "commonjs" in the type. Once upgrading to v9 types
// they'll be the same and this expect-error should be removed.
parserOptions.sourceType = languageOptions.sourceType;
}

// require the parser relative to the main module (i.e., ESLint)
const parser = typeof parserOrPath === 'string' ? moduleRequire(parserOrPath) : parserOrPath;

Expand Down

0 comments on commit add0fdc

Please sign in to comment.