Skip to content

Commit

Permalink
Improve --fix integration for import rules
Browse files Browse the repository at this point in the history
This changes the rules and plugins used for managing imports and their
ordering to optimize for `eslint --fix` automatically handling as much
of the work as possible.
  • Loading branch information
rileytomasek committed Mar 3, 2024
1 parent 4a21437 commit 80624f4
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 26 deletions.
File renamed without changes.
1 change: 0 additions & 1 deletion config/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module.exports = {
'no-duplicate-case': 'error',
'no-empty-character-class': 'error',
'no-empty-pattern': 'error',
'no-duplicate-imports': 'error',
'no-empty': ['error', { allowEmptyCatch: true }],
'no-eval': 'error',
'no-ex-assign': 'error',
Expand Down
35 changes: 35 additions & 0 deletions config/imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/** @type {import('eslint').Linter.Config} */
module.exports = {
files: ['**/*.{ts,tsx,mts,cts}'],

plugins: ['simple-import-sort', 'import'],

settings: {
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx', '.d.ts'],
},
'import/resolver': {
typescript: {
alwaysTryTypes: true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`
},
},
},

rules: {
'simple-import-sort/imports': 'error',
'simple-import-sort/exports': 'error',
'@typescript-eslint/consistent-type-imports': [
'error',
{
prefer: 'type-imports',
disallowTypeAnnotations: true,
fixStyle: 'inline-type-imports',
},
],
'import/first': 'error',
'import/newline-after-import': 'error',
'import/no-duplicates': ['error', { 'prefer-inline': true }],
'import/consistent-type-specifier-style': ['error', 'prefer-inline'],
'no-duplicate-imports': 'error',
},
};
2 changes: 1 addition & 1 deletion config/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
'jest/no-identical-title': 'error',
'jest/no-interpolation-in-snapshots': 'error',
'jest/no-jasmine-globals': 'error',
'jest/no-jest-import': 'error',
// 'jest/no-jest-import': 'error',
'jest/no-mocks-import': 'error',
'jest/valid-describe-callback': 'error',
'jest/valid-expect': 'error',
Expand Down
24 changes: 8 additions & 16 deletions config/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
module.exports = {
files: ['**/*.{ts,tsx,mts,cts}'],

plugins: ['@typescript-eslint', 'simple-import-sort'],
plugins: ['@typescript-eslint'],

extends: [
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/stylistic',
],

rules: {
'simple-import-sort/imports': 'error',
'simple-import-sort/exports': 'error',

'@typescript-eslint/naming-convention': [
'error',
{
Expand Down Expand Up @@ -76,22 +73,17 @@ module.exports = {
'error',
{ allowTernary: true },
],
'@typescript-eslint/consistent-type-imports': [
'@typescript-eslint/ban-ts-comment': [
'error',
{
prefer: 'type-imports',
disallowTypeAnnotations: true,
fixStyle: 'inline-type-imports',
'ts-expect-error': 'allow-with-description',
'ts-ignore': true,
'ts-nocheck': true,
'ts-check': false,
// Require a description for @ts-expect-error to be 10 characters or more.
minimumDescriptionLength: 10,
},
],
'@typescript-eslint/ban-ts-comment': {
'ts-expect-error': 'allow-with-description',
'ts-ignore': 'error',
'ts-nocheck': 'error',
'ts-check': 'error',
// Require a description for @ts-expect-error to be 10 characters or more.
minimumDescriptionLength: 10,
},

// Rules enabled in typescript-eslint configs that are not applicable here
'@typescript-eslint/class-literal-property-style': 'off',
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = {

overrides: [
'./config/typescript',
'./config/imports',
'./config/react',
'./config/tests',
'./config/prettier',
Expand Down
9 changes: 6 additions & 3 deletions node.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ module.exports = {

extends: ['./config/core'].map(require.resolve),

overrides: ['./config/typescript', './config/tests', './config/prettier'].map(
require,
),
overrides: [
'./config/typescript',
'./config/imports',
'./config/tests',
'./config/prettier',
].map(require),

ignorePatterns: ['**/node_modules/**', '**/build/**', '**/dist/**'],

Expand Down
Loading

0 comments on commit 80624f4

Please sign in to comment.