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 fileNamesConfig = require('./eslint/file-names');
const generalConfig = require('./eslint/general');
const testConfig = require('./eslint/test');
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
ignores: [ 'test/assets/*', '**/*.md/**/*.ts' ],
typescript: {
tsconfigPath: [ './tsconfig.json', './scripts/tsconfig.json', './test/tsconfig.json' ],
},
generalConfig,
unicornConfig,
typedConfig({
project: [ './tsconfig.json', './scripts/tsconfig.json', './test/tsconfig.json' ],
tsconfigRootDir: __dirname,
}),
testConfig,
{
// JSON rules
files: [ '**/*.json' ],
},
)
.append(generalConfig)
.append(unicornConfig)
.append(fileNamesConfig)
// Using an override here so all the type settings are also applied correctly
.override('antfu:typescript:rules-type-aware', typedConfig)
.append({
...testConfig,
files: [ 'test/**/*.ts' ],
})
.override('antfu:jsonc:rules', {
rules: {
// Consistent with how we do it in code
'jsonc/array-bracket-spacing': [ 'error', 'always', {
singleValue: true,
objectsInArrays: false,
arraysInArrays: false,
}],
},
},
{
})
.append({
// This is necessary to prevent filename checks caused by JSON being present in a README.
files: [ '**/README.md/**' ],
rules: {
'unicorn/filename-case': 'off',
},
},
{
files: [ '**/*.md' ],
})
.override('antfu:markdown:parser', {
rules: {
// We want to be able to use these in Markdown text
'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 = {
name: 'opinionated:general',
rules: {
'antfu/consistent-list-newline': 'error',

View File

@ -2,11 +2,11 @@ const jest = require('eslint-plugin-jest');
// Specifically for tests
module.exports = {
name: 'opinionated:test',
// See https://github.com/jest-community/eslint-plugin-jest/issues/1408
plugins: {
jest,
},
files: [ 'test/**/*.ts' ],
rules: {
...jest.configs.all.rules,
// 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
// 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,
},
},
module.exports = {
rules: {
...typeAwareRules,
'ts/consistent-type-assertions': [ 'error', {
assertionStyle: 'as',
}],
@ -93,14 +50,5 @@ module.exports = function(options) {
// These are not type specific, but we only care about these in TS files
'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 = {
name: 'opinionated:unicorn',
rules: {
'unicorn/better-regex': 'error',
'unicorn/empty-brace-spaces': 'error',
@ -9,18 +10,6 @@ module.exports = {
allowWarningComments: false,
}],
'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/no-array-for-each': 'error',
'unicorn/no-array-reduce': 'error',