Skip to content

Commit 3578899

Browse files
authored
feat: add ESLint rule for deterministic import order (#98)
1 parent e4d5ab7 commit 3578899

File tree

6 files changed

+1477
-1397
lines changed

6 files changed

+1477
-1397
lines changed

config/eslintrc.template.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = ({ tsconfigRootDir }) => ({
1+
module.exports = ({ tsconfigRootDir, optimizeImports }) => ({
22
root: true,
33
extends: [
44
'airbnb',
@@ -11,7 +11,7 @@ module.exports = ({ tsconfigRootDir }) => ({
1111
'plugin:prettier/recommended',
1212
// 'plugin:lodash/recommended',
1313
],
14-
plugins: ['react', '@typescript-eslint', 'react-compiler'],
14+
plugins: ['react', '@typescript-eslint', 'react-compiler', ...(optimizeImports ? ['unused-imports'] : [])],
1515
ignorePatterns: ['*.js'],
1616
env: {
1717
browser: true,
@@ -68,7 +68,47 @@ module.exports = ({ tsconfigRootDir }) => ({
6868
'import/no-webpack-loader-syntax': 'off', // Disable to allow webpack file-loaders syntax
6969
'import/no-unresolved': 'off', // Disable to allow webpack file-loaders syntax
7070
'import/prefer-default-export': 'off',
71-
'import/order': 'error',
71+
...(optimizeImports ? {
72+
'import/order': [
73+
1,
74+
{
75+
groups: [['builtin', 'external'], 'internal', ['sibling', 'parent']],
76+
pathGroups: [
77+
{
78+
pattern: 'react*',
79+
group: 'external',
80+
position: 'before',
81+
},
82+
{ pattern: 'src/**', group: 'internal', position: 'after' },
83+
],
84+
pathGroupsExcludedImportTypes: ['internal', 'react'],
85+
'newlines-between': 'always',
86+
alphabetize: { order: 'asc' },
87+
},
88+
],
89+
'sort-imports': [
90+
1,
91+
{
92+
ignoreCase: false,
93+
ignoreDeclarationSort: true, // don't want to sort import lines, use eslint-plugin-import instead
94+
ignoreMemberSort: false,
95+
memberSyntaxSortOrder: ['none', 'all', 'multiple', 'single'],
96+
allowSeparatedGroups: true,
97+
},
98+
],
99+
'unused-imports/no-unused-imports': 'error',
100+
'unused-imports/no-unused-vars': [
101+
'warn',
102+
{
103+
vars: 'all',
104+
varsIgnorePattern: '^_',
105+
args: 'after-used',
106+
argsIgnorePattern: '^_',
107+
},
108+
],
109+
} : {
110+
'import/order': 'error',
111+
}),
72112
'prefer-destructuring': ['warn', { object: true, array: false }],
73113
'prefer-promise-reject-errors': 'warn',
74114
'prefer-spread': 'warn',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"eslint-plugin-react": "^7.35.2",
6363
"eslint-plugin-react-compiler": "0.0.0-experimental-b8a7b48-20240830",
6464
"eslint-plugin-react-hooks": "^4.6.2",
65+
"eslint-plugin-unused-imports": "^4.1.4",
6566
"fork-ts-checker-webpack-plugin": "^9.0.2",
6667
"fs-extra": "^11.2.0",
6768
"glob": "^11.0.0",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('visyn_scripts/config/eslintrc.template')({ tsconfigRootDir: __dirname });
1+
module.exports = require('visyn_scripts/config/eslintrc.template')({ tsconfigRootDir: __dirname, optimizeImports: true });

tests_fixtures/standalone_template/src/app/MainApp.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
23
import { MainApp } from './MainApp';
34

45
export default {

tests_fixtures/standalone_template/src/index.initialize.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as React from 'react';
2+
23
import { createRoot } from 'react-dom/client';
4+
35
import { MainApp } from './app';
46

57
// create a new instance of the app

0 commit comments

Comments
 (0)