Skip to content

Commit 7703d75

Browse files
committed
add eslint-plugin-import
Fork because of a required fix that hasn't been shipped yet.
1 parent 3ae875b commit 7703d75

File tree

5 files changed

+362
-4
lines changed

5 files changed

+362
-4
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/package.json b/package.json
2+
index 85c863485b0c37df556132beddf7f6959e6b5961..c1bdec0cb02cbabda11a8917d37d3792134c81aa 100644
3+
--- a/package.json
4+
+++ b/package.json
5+
@@ -21,6 +21,7 @@
6+
"scripts": {
7+
"prebuild": "rimraf lib",
8+
"build": "babel --quiet --out-dir lib src",
9+
+ "prepack": "yarn run build",
10+
"postbuild": "npm run copy-metafiles",
11+
"copy-metafiles": "node --require babel-register ./scripts/copyMetafiles",
12+
"watch": "npm run tests-only -- -- --watch",

eslint-plugin-react.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ declare module 'eslint-plugin-react-hooks' {
2525
}
2626
export default plugin
2727
}
28+
29+
// Non-exhaustive.
30+
declare module 'eslint-plugin-import' {
31+
import type { Linter } from 'eslint'
32+
33+
export const flatConfigs: {
34+
typescript: Linter.Config
35+
recommended: Linter.Config
36+
}
37+
}

lib/basic.mjs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,54 @@
11
// @ts-check
22

3+
import { fileURLToPath } from 'node:url'
4+
import path from 'node:path'
5+
6+
import { fixupPluginRules } from '@eslint/compat'
7+
import { FlatCompat } from '@eslint/eslintrc'
38
import js from '@eslint/js'
9+
import eslintPluginImport from 'eslint-plugin-import'
10+
11+
const __filename = fileURLToPath(import.meta.url)
12+
const __dirname = path.dirname(__filename)
13+
const compat = new FlatCompat({
14+
baseDirectory: __dirname,
15+
recommendedConfig: js.configs.recommended,
16+
})
17+
18+
// https://github.com/import-js/eslint-plugin-import/issues/2948#issuecomment-2148832701
19+
/**
20+
* @param {string} name
21+
* @param {string} alias
22+
* @returns {import('eslint').ESLint.Plugin}
23+
*/
24+
function legacyPlugin(name, alias = name) {
25+
const plugin = compat.plugins(name)[0]?.plugins?.[alias]
26+
if (!plugin) throw new Error(`Unable to resolve plugin ${name} and/or alias ${alias}`)
27+
return fixupPluginRules(plugin)
28+
}
29+
30+
/** @type {import('eslint').Linter.Config[]} */
31+
export const importPlugin = [
32+
{ ...eslintPluginImport.flatConfigs.recommended, plugins: {} },
33+
// TypeScript one doesn't have a name, but recommended does.
34+
{ ...eslintPluginImport.flatConfigs.typescript, name: 'import/typescript', plugins: {} },
35+
36+
{
37+
name: '@textshq/eslint-config.basic.import',
38+
plugins: { import: legacyPlugin('eslint-plugin-import', 'import') },
39+
settings: {
40+
'import/parsers': {
41+
'@typescript-eslint/parser': ['.ts', '.tsx'],
42+
},
43+
'import/resolver': {
44+
typescript: {
45+
alwaysTryTypes: true,
46+
},
47+
node: true,
48+
},
49+
},
50+
},
51+
]
452

553
/** @satisfies {import('@typescript-eslint/utils/ts-eslint').FlatConfig.ConfigFile} */
654
const config = [
@@ -35,6 +83,8 @@ const config = [
3583
'prefer-template': 'off',
3684
},
3785
},
86+
87+
...importPlugin,
3888
]
3989

4090
export default config

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"@stylistic/eslint-plugin": "^2.8.0",
1414
"@typescript-eslint/eslint-plugin": "^8.5.0",
1515
"@typescript-eslint/parser": "^8.5.0",
16+
"eslint-import-resolver-typescript": "^3.6.3",
17+
"eslint-plugin-import": "TextsHQ/eslint-plugin-import#commit=b271f426d6834ce802df68c1eea33b8cae895b26",
1618
"eslint-plugin-jsx-a11y": "^6.10.0",
1719
"eslint-plugin-react": "^7.36.1",
1820
"eslint-plugin-react-hooks": "5.1.0-rc-206df66e-20240912",
@@ -24,7 +26,9 @@
2426
},
2527
"devDependencies": {
2628
"@types/eslint-plugin-jsx-a11y": "^6",
29+
"@types/eslint__eslintrc": "^2.1.2",
2730
"@types/eslint__js": "^8.42.3",
31+
"@types/node": "^18",
2832
"eslint": "^9.10.0",
2933
"typescript": "<5.6.0"
3034
},

0 commit comments

Comments
 (0)