Skip to content

Commit cd52e86

Browse files
michaelfaithSukkaW
andauthored
feat: add flat config presets (#122)
Co-authored-by: Sukka <isukkaw@gmail.com>
1 parent f37dc29 commit cd52e86

File tree

14 files changed

+253
-23
lines changed

14 files changed

+253
-23
lines changed

.changeset/great-dodos-dream.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"eslint-plugin-import-x": minor
3+
---
4+
5+
Add ESLint flat configuration presets. You can access them with:
6+
7+
```ts
8+
import eslintPluginImportX from 'eslint-plugin-import-x';
9+
10+
eslintPluginImportX.flatConfigs.recommended;
11+
eslintPluginImportX.flatConfigs.react;
12+
eslintPluginImportX.flatConfigs.typescript;
13+
eslintPluginImportX.flatConfigs.electron;
14+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"codesandbox:install": "yarn --ignore-engines",
3636
"lint": "run-p lint:*",
3737
"lint:docs": "yarn update:eslint-docs --check",
38-
"lint:es": "ESLINT_USE_FLAT_CONFIG=false eslint . --cache",
38+
"lint:es": "cross-env ESLINT_USE_FLAT_CONFIG=false eslint . --cache",
3939
"lint:tsc": "tsc -p tsconfig.base.json --noEmit",
4040
"prepare": "patch-package",
4141
"release": "changeset publish",

src/config/flat/electron.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { PluginFlatConfig } from '../../types'
2+
3+
/**
4+
* Default settings for Electron applications.
5+
*/
6+
export default {
7+
settings: {
8+
'import-x/core-modules': ['electron'],
9+
},
10+
} satisfies PluginFlatConfig

src/config/flat/errors.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { PluginFlatConfig } from '../../types'
2+
3+
/**
4+
* unopinionated config. just the things that are necessarily runtime errors
5+
* waiting to happen.
6+
*/
7+
export default {
8+
rules: {
9+
'import-x/no-unresolved': 2,
10+
'import-x/named': 2,
11+
'import-x/namespace': 2,
12+
'import-x/default': 2,
13+
'import-x/export': 2,
14+
},
15+
} satisfies PluginFlatConfig

src/config/flat/react-native.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { PluginFlatBaseConfig } from '../../types'
2+
3+
/**
4+
* adds platform extensions to Node resolver
5+
*/
6+
export default {
7+
settings: {
8+
'import-x/resolver': {
9+
node: {
10+
// Note: will not complain if only _one_ of these files exists.
11+
extensions: ['.js', '.web.js', '.ios.js', '.android.js'],
12+
},
13+
},
14+
},
15+
} satisfies PluginFlatBaseConfig

src/config/flat/react.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { PluginFlatBaseConfig } from '../../types'
2+
3+
/**
4+
* Adds `.jsx` as an extension, and enables JSX parsing.
5+
*
6+
* Even if _you_ aren't using JSX (or .jsx) directly, if your dependencies
7+
* define jsnext:main and have JSX internally, you may run into problems
8+
* if you don't enable these settings at the top level.
9+
*/
10+
export default {
11+
settings: {
12+
'import-x/extensions': ['.js', '.jsx', '.mjs', '.cjs'],
13+
},
14+
languageOptions: {
15+
parserOptions: {
16+
ecmaFeatures: {
17+
jsx: true,
18+
},
19+
},
20+
},
21+
} satisfies PluginFlatBaseConfig

src/config/flat/recommended.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { PluginFlatBaseConfig } from '../../types'
2+
3+
/**
4+
* The basics.
5+
*/
6+
export default {
7+
rules: {
8+
// analysis/correctness
9+
'import-x/no-unresolved': 'error',
10+
'import-x/named': 'error',
11+
'import-x/namespace': 'error',
12+
'import-x/default': 'error',
13+
'import-x/export': 'error',
14+
15+
// red flags (thus, warnings)
16+
'import-x/no-named-as-default': 'warn',
17+
'import-x/no-named-as-default-member': 'warn',
18+
'import-x/no-duplicates': 'warn',
19+
},
20+
21+
// need all these for parsing dependencies (even if _your_ code doesn't need
22+
// all of them)
23+
languageOptions: {
24+
ecmaVersion: 2018,
25+
sourceType: 'module',
26+
},
27+
} satisfies PluginFlatBaseConfig

src/config/flat/stage-0.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { PluginFlatBaseConfig } from '../../types'
2+
3+
/**
4+
* Rules in progress.
5+
*
6+
* Do not expect these to adhere to semver across releases.
7+
*/
8+
export default {
9+
rules: {
10+
'import-x/no-deprecated': 1,
11+
},
12+
} satisfies PluginFlatBaseConfig

src/config/flat/typescript.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { PluginFlatBaseConfig } from '../../types'
2+
3+
/**
4+
* This config:
5+
* 1) adds `.jsx`, `.ts`, `.cts`, `.mts`, and `.tsx` as an extension
6+
* 2) enables JSX/TSX parsing
7+
*/
8+
9+
// Omit `.d.ts` because 1) TypeScript compilation already confirms that
10+
// types are resolved, and 2) it would mask an unresolved
11+
// `.ts`/`.tsx`/`.js`/`.jsx` implementation.
12+
const typeScriptExtensions = ['.ts', '.tsx', '.cts', '.mts'] as const
13+
14+
const allExtensions = [
15+
...typeScriptExtensions,
16+
'.js',
17+
'.jsx',
18+
'.cjs',
19+
'.mjs',
20+
] as const
21+
22+
export default {
23+
settings: {
24+
'import-x/extensions': allExtensions,
25+
'import-x/external-module-folders': ['node_modules', 'node_modules/@types'],
26+
'import-x/parsers': {
27+
'@typescript-eslint/parser': [...typeScriptExtensions],
28+
},
29+
'import-x/resolver': {
30+
node: {
31+
extensions: allExtensions,
32+
},
33+
},
34+
},
35+
rules: {
36+
// analysis/correctness
37+
38+
// TypeScript compilation already ensures that named imports exist in the referenced module
39+
'import-x/named': 'off',
40+
},
41+
} satisfies PluginFlatBaseConfig

src/config/flat/warnings.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { PluginFlatBaseConfig } from '../../types'
2+
3+
/**
4+
* more opinionated config.
5+
*/
6+
export default {
7+
rules: {
8+
'import-x/no-named-as-default': 1,
9+
'import-x/no-named-as-default-member': 1,
10+
'import-x/no-duplicates': 1,
11+
},
12+
} satisfies PluginFlatBaseConfig

0 commit comments

Comments
 (0)