diff --git a/.changeset/thirty-houses-rest.md b/.changeset/thirty-houses-rest.md new file mode 100644 index 000000000..a2b688898 --- /dev/null +++ b/.changeset/thirty-houses-rest.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-import-x": patch +--- + +Attach `ecmaVersion` and `sourceType` to `parserOptions` during parse diff --git a/src/utils/parse.ts b/src/utils/parse.ts index 61b94383c..ad7973a68 100644 --- a/src/utils/parse.ts +++ b/src/utils/parse.ts @@ -79,8 +79,7 @@ export function parse( // ESLint in "flat" mode only sets context.languageOptions.parserOptions let parserOptions = - ('languageOptions' in context && context.languageOptions?.parserOptions) || - context.parserOptions + context.languageOptions?.parserOptions || context.parserOptions const parserOrPath = getParser(path, context) @@ -111,6 +110,17 @@ export function parse( // https://github.com/import-js/eslint-plugin-import/issues/1408#issuecomment-509298962 parserOptions = withoutProjectParserOptions(parserOptions) + // If this is a flat config, we need to add ecmaVersion and sourceType (if present) from languageOptions + if ( + parserOptions.ecmaVersion == null && + context.languageOptions?.ecmaVersion + ) { + parserOptions.ecmaVersion = context.languageOptions.ecmaVersion + } + if (parserOptions.sourceType == null && context.languageOptions?.sourceType) { + parserOptions.sourceType = context.languageOptions.sourceType + } + // require the parser relative to the main module (i.e., ESLint) const parser = typeof parserOrPath === 'string'