refactor: Simplify eslint configs

This commit is contained in:
Joachim Van Herwegen 2024-03-29 13:27:26 +01:00
parent 7abca33b67
commit cac70b1f88
6 changed files with 109 additions and 133 deletions

View File

@ -1,4 +1,5 @@
const antfu = require('@antfu/eslint-config'); const antfu = require('@antfu/eslint-config');
const fileNamesConfig = require('./eslint/file-names');
const generalConfig = require('./eslint/general'); const generalConfig = require('./eslint/general');
const testConfig = require('./eslint/test'); const testConfig = require('./eslint/test');
const typedConfig = require('./eslint/typed'); const typedConfig = require('./eslint/typed');
@ -16,36 +17,40 @@ module.exports = antfu.default(
{ {
// Don't want to lint test assets, or TS snippets in markdown files // Don't want to lint test assets, or TS snippets in markdown files
ignores: [ 'test/assets/*', '**/*.md/**/*.ts' ], ignores: [ 'test/assets/*', '**/*.md/**/*.ts' ],
typescript: {
tsconfigPath: [ './tsconfig.json', './scripts/tsconfig.json', './test/tsconfig.json' ],
}, },
generalConfig, },
unicornConfig, )
typedConfig({ .append(generalConfig)
project: [ './tsconfig.json', './scripts/tsconfig.json', './test/tsconfig.json' ], .append(unicornConfig)
tsconfigRootDir: __dirname, .append(fileNamesConfig)
}), // Using an override here so all the type settings are also applied correctly
testConfig, .override('antfu:typescript:rules-type-aware', typedConfig)
{ .append({
// JSON rules ...testConfig,
files: [ '**/*.json' ], files: [ 'test/**/*.ts' ],
})
.override('antfu:jsonc:rules', {
rules: { rules: {
// Consistent with how we do it in code
'jsonc/array-bracket-spacing': [ 'error', 'always', { 'jsonc/array-bracket-spacing': [ 'error', 'always', {
singleValue: true, singleValue: true,
objectsInArrays: false, objectsInArrays: false,
arraysInArrays: false, arraysInArrays: false,
}], }],
}, },
}, })
{ .append({
// This is necessary to prevent filename checks caused by JSON being present in a README. // This is necessary to prevent filename checks caused by JSON being present in a README.
files: [ '**/README.md/**' ], files: [ '**/README.md/**' ],
rules: { rules: {
'unicorn/filename-case': 'off', 'unicorn/filename-case': 'off',
}, },
}, })
{ .override('antfu:markdown:parser', {
files: [ '**/*.md' ],
rules: { rules: {
// We want to be able to use these in Markdown text
'no-irregular-whitespace': 'off', 'no-irregular-whitespace': 'off',
}, },
}, });
);

33
eslint/file-names.js Normal file
View File

@ -0,0 +1,33 @@
module.exports = [
{
name: 'opinionated:file-names:all',
rules: {
'unicorn/filename-case': [ 'error', {
cases: {
camelCase: false,
pascalCase: false,
kebabCase: true,
snakeCase: false,
},
ignore: [
// CODE_OF_CONDUCT.md, etc.
/[A-Z_]+\.md$/u,
],
}],
},
},
{
name: 'opinionated:file-names:ts',
files: [ '**/*.ts' ],
rules: {
'unicorn/filename-case': [ 'error', {
cases: {
camelCase: true,
pascalCase: true,
kebabCase: false,
snakeCase: false,
},
}],
},
},
];

View File

@ -1,4 +1,5 @@
module.exports = { module.exports = {
name: 'opinionated:general',
rules: { rules: {
'antfu/consistent-list-newline': 'error', 'antfu/consistent-list-newline': 'error',

View File

@ -2,11 +2,11 @@ const jest = require('eslint-plugin-jest');
// Specifically for tests // Specifically for tests
module.exports = { module.exports = {
name: 'opinionated:test',
// See https://github.com/jest-community/eslint-plugin-jest/issues/1408 // See https://github.com/jest-community/eslint-plugin-jest/issues/1408
plugins: { plugins: {
jest, jest,
}, },
files: [ 'test/**/*.ts' ],
rules: { rules: {
...jest.configs.all.rules, ...jest.configs.all.rules,
// Rule is not smart enough to check called function in the test // Rule is not smart enough to check called function in the test

View File

@ -1,48 +1,5 @@
// Copied from https://github.com/antfu/eslint-config/blob/main/src/configs/typescript.ts module.exports = {
// Doing it like this, so we can make sure these only try to trigger on *.ts files,
// Preventing issues with the *.js files.
const typeAwareRules = {
'dot-notation': 'off',
'no-implied-eval': 'off',
'no-throw-literal': 'off',
'ts/await-thenable': 'error',
'ts/dot-notation': [ 'error', { allowKeywords: true }],
'ts/no-floating-promises': 'error',
'ts/no-for-in-array': 'error',
'ts/no-implied-eval': 'error',
'ts/no-misused-promises': 'error',
'ts/no-throw-literal': 'error',
'ts/no-unnecessary-type-arguments': 'error',
'ts/no-unnecessary-type-assertion': 'error',
'ts/no-unsafe-argument': 'error',
'ts/no-unsafe-assignment': 'error',
'ts/no-unsafe-call': 'error',
'ts/no-unsafe-member-access': 'error',
'ts/no-unsafe-return': 'error',
'ts/restrict-plus-operands': 'error',
'ts/restrict-template-expressions': 'error',
'ts/unbound-method': 'error',
};
const defaults = {
project: [ './tsconfig.json' ],
files: [ '**/*.ts' ],
tsconfigRootDir: process.cwd(),
};
module.exports = function(options) {
options = { ...defaults, ...options };
return {
// By default, antfu also triggers type rules on *.js files which causes all kinds of issues for us
files: options.files,
languageOptions: {
parserOptions: {
tsconfigRootDir: options.tsconfigRootDir,
project: options.project,
},
},
rules: { rules: {
...typeAwareRules,
'ts/consistent-type-assertions': [ 'error', { 'ts/consistent-type-assertions': [ 'error', {
assertionStyle: 'as', assertionStyle: 'as',
}], }],
@ -93,14 +50,5 @@ module.exports = function(options) {
// These are not type specific, but we only care about these in TS files // These are not type specific, but we only care about these in TS files
'max-len': [ 'error', { code: 120, ignoreUrls: true }], 'max-len': [ 'error', { code: 120, ignoreUrls: true }],
'unicorn/filename-case': [ 'error', {
cases: {
camelCase: true,
pascalCase: true,
kebabCase: false,
snakeCase: false,
},
}],
}, },
}; };
};

View File

@ -1,4 +1,5 @@
module.exports = { module.exports = {
name: 'opinionated:unicorn',
rules: { rules: {
'unicorn/better-regex': 'error', 'unicorn/better-regex': 'error',
'unicorn/empty-brace-spaces': 'error', 'unicorn/empty-brace-spaces': 'error',
@ -9,18 +10,6 @@ module.exports = {
allowWarningComments: false, allowWarningComments: false,
}], }],
'unicorn/explicit-length-check': 'error', 'unicorn/explicit-length-check': 'error',
'unicorn/filename-case': [ 'error', {
cases: {
camelCase: false,
pascalCase: false,
kebabCase: true,
snakeCase: false,
},
ignore: [
// CODE_OF_CONDUCT.md, etc.
/[A-Z_]+\.md$/u,
],
}],
'unicorn/new-for-builtins': 'error', 'unicorn/new-for-builtins': 'error',
'unicorn/no-array-for-each': 'error', 'unicorn/no-array-for-each': 'error',
'unicorn/no-array-reduce': 'error', 'unicorn/no-array-reduce': 'error',