diff --git a/.changeset/neat-houses-deliver.md b/.changeset/neat-houses-deliver.md new file mode 100644 index 000000000..9b0809186 --- /dev/null +++ b/.changeset/neat-houses-deliver.md @@ -0,0 +1,28 @@ +--- +"eslint-plugin-import-x": patch +--- + +Add a default `languageOptions.parserOptions` to the `recommended` flat config presets. This prevents the `sourceType 'module' is not supported when ecmaVersion < 2015` error when no custom parser options are provided. + +For existing users who are already using the `recommended` flat config presets while providing custom parser options, make sure your custom parser options come after the presets to prevent yours from being overridden: + +```js +// eslint.config.js +import eslintPluginImportX from 'eslint-plugin-import-x' + +export default [ + js.configs.recommended, + eslintPluginImportX.flatConfigs.recommended, + // Always put your custom language options and parser options after the presets + { + languageOptions: { + ecmaVersion: 'latest', + sourceType: 'module', + parserOptions: { + ecmaVersion: 'latest', + sourceType: 'module', + }, + }, + }, +] +``` diff --git a/.changeset/violet-ligers-fail.md b/.changeset/violet-ligers-fail.md new file mode 100644 index 000000000..371ddf642 --- /dev/null +++ b/.changeset/violet-ligers-fail.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-import-x": patch +--- + +Change how the `sourceType` is read from the ESLint rule context. diff --git a/.eslintrc.js b/.eslintrc.js index 54a81f13f..e36ec7553 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -41,6 +41,7 @@ module.exports = { 'n/no-missing-import': 'off', 'n/no-missing-require': 'off', 'n/no-unsupported-features/es-syntax': 'off', + 'unicorn/expiring-todo-comments': 'off', 'unicorn/no-array-callback-reference': 'off', 'unicorn/no-array-reduce': 'off', 'unicorn/no-null': 'off', diff --git a/src/config/flat/recommended.ts b/src/config/flat/recommended.ts index 69056e74a..54b8f7e85 100644 --- a/src/config/flat/recommended.ts +++ b/src/config/flat/recommended.ts @@ -23,5 +23,9 @@ export default { languageOptions: { ecmaVersion: 2018, sourceType: 'module', + parserOptions: { + ecmaVersion: 2018, + sourceType: 'module', + }, }, } satisfies PluginFlatBaseConfig diff --git a/src/utils/source-type.ts b/src/utils/source-type.ts index 89f732e4b..20d776e49 100644 --- a/src/utils/source-type.ts +++ b/src/utils/source-type.ts @@ -1,10 +1,19 @@ import type { RuleContext } from '../types' export default function sourceType(context: RuleContext) { - if ('sourceType' in context.parserOptions) { - return context.parserOptions.sourceType - } if ('languageOptions' in context && context.languageOptions) { - return context.languageOptions.sourceType + if ( + 'parserOptions' in context.languageOptions && + context.languageOptions.parserOptions && + context.languageOptions.parserOptions.sourceType + ) { + return context.languageOptions.parserOptions.sourceType + } + if (context.languageOptions.sourceType) { + return context.languageOptions.sourceType + } + } + if (context.parserOptions.sourceType) { + return context.parserOptions.sourceType } }