Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#185): add langaugeOptions.parserOptions to flat preset / read sourceType w/ fallback #186

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .changeset/neat-houses-deliver.md
Original file line number Diff line number Diff line change
@@ -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',
},
},
},
]
```
5 changes: 5 additions & 0 deletions .changeset/violet-ligers-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": patch
---

Change how the `sourceType` is read from the ESLint rule context.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions src/config/flat/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ export default {
languageOptions: {
ecmaVersion: 2018,
sourceType: 'module',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
},
},
} satisfies PluginFlatBaseConfig
17 changes: 13 additions & 4 deletions src/utils/source-type.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading