Skip to content

Commit ba4cd6a

Browse files
committed
refactor: make all configs function for consistency and immutability
1 parent b68f7e1 commit ba4cd6a

23 files changed

+848
-659
lines changed

src/configs/astro.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { type FlatESLintConfigItem } from 'eslint-define-config'
22
import { GLOB_ASTRO } from 'src/globs'
33
import { parserAstro, parserTs, pluginAstro } from '../plugins'
4-
import type { OptionsHasTypeScript } from '../types'
4+
import type { OptionsHasTypeScript, OptionsOverrides } from '../types'
55

6-
export function astro(options: OptionsHasTypeScript = {}): FlatESLintConfigItem[] {
6+
export function astro(options: OptionsHasTypeScript & OptionsOverrides = {}): FlatESLintConfigItem[] {
7+
const { overrides = {} } = options
78
return [
9+
{
10+
plugins: {
11+
astro: pluginAstro,
12+
},
13+
},
814
{
915
files: [GLOB_ASTRO],
1016
languageOptions: {
@@ -14,11 +20,10 @@ export function astro(options: OptionsHasTypeScript = {}): FlatESLintConfigItem[
1420
parser: options.typescript ? (parserTs as any) : null,
1521
},
1622
},
17-
plugins: {
18-
astro: pluginAstro,
19-
},
2023
rules: {
2124
...(pluginAstro.configs.recommended.rules as any),
25+
26+
...overrides,
2227
},
2328
},
2429
]

src/configs/comments.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import type { FlatESLintConfigItem } from 'eslint-define-config'
22
import { pluginComments } from '../plugins'
33

4-
export const comments: FlatESLintConfigItem[] = [
5-
{
6-
plugins: {
7-
'eslint-comments': pluginComments,
4+
export function comments(): FlatESLintConfigItem[] {
5+
return [
6+
{
7+
plugins: {
8+
'eslint-comments': pluginComments,
9+
},
10+
rules: {
11+
'eslint-comments/no-aggregating-enable': 'error',
12+
'eslint-comments/no-duplicate-disable': 'error',
13+
'eslint-comments/no-unlimited-disable': 'error',
14+
'eslint-comments/no-unused-enable': 'error',
15+
},
816
},
9-
rules: {
10-
'eslint-comments/no-aggregating-enable': 'error',
11-
'eslint-comments/no-duplicate-disable': 'error',
12-
'eslint-comments/no-unlimited-disable': 'error',
13-
'eslint-comments/no-unused-enable': 'error',
14-
},
15-
},
16-
]
17+
]
18+
}

src/configs/ignores.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { FlatESLintConfigItem } from 'eslint-define-config'
22
import { GLOB_EXCLUDE } from '../globs'
33

4-
export const ignores: FlatESLintConfigItem[] = [
5-
{ ignores: GLOB_EXCLUDE },
6-
]
4+
export function ignores(): FlatESLintConfigItem[] {
5+
return [
6+
{ ignores: GLOB_EXCLUDE },
7+
]
8+
}

src/configs/imports.ts

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
import type { FlatESLintConfigItem } from 'eslint-define-config'
2-
import { pluginImport } from '../plugins'
2+
import { pluginAntfu, pluginImport } from '../plugins'
3+
import type { OptionsStylistic } from '../types'
34

4-
export const imports: FlatESLintConfigItem[] = [
5-
{
6-
plugins: {
7-
import: pluginImport,
8-
},
9-
rules: {
10-
'import/export': 'error',
11-
'import/first': 'error',
12-
'import/newline-after-import': ['error', { considerComments: true, count: 1 }],
13-
'import/no-duplicates': 'error',
14-
'import/no-mutable-exports': 'error',
15-
'import/no-named-default': 'error',
16-
'import/no-self-import': 'error',
17-
'import/no-webpack-loader-syntax': 'error',
18-
'import/order': 'error',
5+
export function imports(options: OptionsStylistic = {}): FlatESLintConfigItem[] {
6+
const {
7+
stylistic = true,
8+
} = options
9+
10+
return [
11+
{
12+
plugins: {
13+
antfu: pluginAntfu,
14+
import: pluginImport,
15+
},
16+
rules: {
17+
'antfu/import-dedupe': 'error',
18+
'antfu/no-import-node-modules-by-path': 'error',
19+
20+
'import/export': 'error',
21+
'import/first': 'error',
22+
'import/no-duplicates': 'error',
23+
'import/no-mutable-exports': 'error',
24+
'import/no-named-default': 'error',
25+
'import/no-self-import': 'error',
26+
'import/no-webpack-loader-syntax': 'error',
27+
'import/order': 'error',
28+
29+
...stylistic
30+
? {
31+
'import/newline-after-import': ['error', { considerComments: true, count: 1 }],
32+
}
33+
: {},
34+
},
1935
},
20-
},
21-
]
36+
]
37+
}

src/configs/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export * from './stylistic'
1111
export * from './typescript'
1212
export * from './unicorn'
1313
export * from './vue'
14-
export * from './yml'
14+
export * from './yaml'
1515
export * from './test'
1616
export * from './react'
1717
export * from './astro'

src/configs/javascript.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@ import type { FlatESLintConfigItem } from 'eslint-define-config'
22
import globals from 'globals'
33
import { pluginAntfu, pluginUnusedImports } from '../plugins'
44
import { OFF } from '../flags'
5-
import type { OptionsIsInEditor } from '../types'
5+
import type { OptionsIsInEditor, OptionsOverrides } from '../types'
66
import { GLOB_SRC, GLOB_SRC_EXT } from '../globs'
77

8-
export function javascript(options: OptionsIsInEditor = {}): FlatESLintConfigItem[] {
8+
export function javascript(options: OptionsIsInEditor & OptionsOverrides = {}): FlatESLintConfigItem[] {
9+
const {
10+
isInEditor = false,
11+
overrides = {},
12+
} = options
13+
914
return [
15+
{
16+
plugins: {
17+
'antfu': pluginAntfu,
18+
'unused-imports': pluginUnusedImports,
19+
},
20+
},
1021
{
1122
languageOptions: {
1223
ecmaVersion: 2022,
@@ -27,15 +38,10 @@ export function javascript(options: OptionsIsInEditor = {}): FlatESLintConfigIte
2738
},
2839
sourceType: 'module',
2940
},
30-
plugins: {
31-
'antfu': pluginAntfu,
32-
'unused-imports': pluginUnusedImports,
33-
},
41+
3442
rules: {
3543
'accessor-pairs': ['error', { enforceForClassMembers: true, setWithoutGet: true }],
3644

37-
'antfu/import-dedupe': 'error',
38-
'antfu/no-import-node-modules-by-path': 'error',
3945
'antfu/top-level-function': 'error',
4046

4147
'array-callback-return': 'error',
@@ -212,7 +218,7 @@ export function javascript(options: OptionsIsInEditor = {}): FlatESLintConfigIte
212218
'symbol-description': 'error',
213219
'unicode-bom': ['error', 'never'],
214220

215-
'unused-imports/no-unused-imports': options.isInEditor ? OFF : 'error',
221+
'unused-imports/no-unused-imports': isInEditor ? OFF : 'error',
216222
'unused-imports/no-unused-vars': [
217223
'error',
218224
{ args: 'after-used', argsIgnorePattern: '^_', vars: 'all', varsIgnorePattern: '^_' },
@@ -223,6 +229,8 @@ export function javascript(options: OptionsIsInEditor = {}): FlatESLintConfigIte
223229
'vars-on-top': 'error',
224230
'wrap-iife': ['error', 'any', { functionPrototypeMethods: true }],
225231
'yoda': ['error', 'never'],
232+
233+
...overrides,
226234
},
227235
},
228236
{

src/configs/jsdoc.ts

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
11
import type { FlatESLintConfigItem } from 'eslint-define-config'
22
import { pluginJsdoc } from '../plugins'
3-
import { OFF } from '../flags'
3+
import type { OptionsStylistic } from '../types'
44

5-
export const jsdoc: FlatESLintConfigItem[] = [
6-
{
7-
plugins: {
8-
jsdoc: pluginJsdoc,
5+
export function jsdoc(options: OptionsStylistic = {}): FlatESLintConfigItem[] {
6+
const {
7+
stylistic = true,
8+
} = options
9+
10+
return [
11+
{
12+
plugins: {
13+
jsdoc: pluginJsdoc,
14+
},
915
},
10-
rules: {
11-
'jsdoc/check-access': 'warn',
12-
'jsdoc/check-alignment': 'warn',
13-
'jsdoc/check-param-names': 'warn',
14-
'jsdoc/check-property-names': 'warn',
15-
'jsdoc/check-types': 'warn',
16-
'jsdoc/empty-tags': 'warn',
17-
'jsdoc/implements-on-classes': 'warn',
18-
'jsdoc/multiline-blocks': 'warn',
19-
'jsdoc/no-defaults': 'warn',
20-
'jsdoc/no-multi-asterisks': 'warn',
21-
'jsdoc/no-types': 'warn',
22-
'jsdoc/require-param-name': 'warn',
23-
'jsdoc/require-property': 'warn',
24-
'jsdoc/require-property-description': 'warn',
25-
'jsdoc/require-property-name': 'warn',
26-
'jsdoc/require-returns-check': 'warn',
27-
'jsdoc/require-returns-description': 'warn',
28-
'jsdoc/require-yields-check': 'warn',
29-
'jsdoc/valid-types': 'warn',
16+
{
17+
rules: {
18+
'jsdoc/check-access': 'warn',
19+
'jsdoc/check-param-names': 'warn',
20+
'jsdoc/check-property-names': 'warn',
21+
'jsdoc/check-types': 'warn',
22+
'jsdoc/empty-tags': 'warn',
23+
'jsdoc/implements-on-classes': 'warn',
24+
'jsdoc/no-defaults': 'warn',
25+
'jsdoc/no-multi-asterisks': 'warn',
26+
'jsdoc/require-param-name': 'warn',
27+
'jsdoc/require-property': 'warn',
28+
'jsdoc/require-property-description': 'warn',
29+
'jsdoc/require-property-name': 'warn',
30+
'jsdoc/require-returns-check': 'warn',
31+
'jsdoc/require-returns-description': 'warn',
32+
'jsdoc/require-yields-check': 'warn',
33+
'jsdoc/valid-types': 'warn',
34+
35+
...stylistic
36+
? {
37+
'jsdoc/check-alignment': 'warn',
38+
'jsdoc/multiline-blocks': 'warn',
39+
}
40+
: {},
41+
},
3042
},
31-
},
32-
]
43+
]
44+
}

src/configs/jsonc.ts

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,69 @@
11
import type { FlatESLintConfigItem } from 'eslint-define-config'
22
import { GLOB_JSON, GLOB_JSON5, GLOB_JSONC } from '../globs'
33
import { parserJsonc, pluginJsonc } from '../plugins'
4+
import type { OptionsOverrides, OptionsStylistic } from '../types'
45

5-
export const jsonc: FlatESLintConfigItem[] = [
6-
{
7-
plugins: {
8-
jsonc: pluginJsonc as any,
6+
export function jsonc(options: OptionsStylistic & OptionsOverrides = {}): FlatESLintConfigItem[] {
7+
const {
8+
stylistic = true,
9+
overrides = {},
10+
} = options
11+
return [
12+
{
13+
plugins: {
14+
jsonc: pluginJsonc as any,
15+
},
916
},
10-
},
11-
{
12-
files: [GLOB_JSON, GLOB_JSON5, GLOB_JSONC],
13-
languageOptions: {
14-
parser: parserJsonc,
15-
},
16-
rules: {
17-
'jsonc/array-bracket-spacing': ['error', 'never'],
18-
'jsonc/comma-dangle': ['error', 'never'],
19-
'jsonc/comma-style': ['error', 'last'],
20-
'jsonc/indent': ['error', 2],
21-
'jsonc/key-spacing': ['error', { afterColon: true, beforeColon: false }],
22-
'jsonc/no-bigint-literals': 'error',
23-
'jsonc/no-binary-expression': 'error',
24-
'jsonc/no-binary-numeric-literals': 'error',
25-
'jsonc/no-dupe-keys': 'error',
26-
'jsonc/no-escape-sequence-in-identifier': 'error',
27-
'jsonc/no-floating-decimal': 'error',
28-
'jsonc/no-hexadecimal-numeric-literals': 'error',
29-
'jsonc/no-infinity': 'error',
30-
'jsonc/no-multi-str': 'error',
31-
'jsonc/no-nan': 'error',
32-
'jsonc/no-number-props': 'error',
33-
'jsonc/no-numeric-separators': 'error',
34-
'jsonc/no-octal': 'error',
35-
'jsonc/no-octal-escape': 'error',
36-
'jsonc/no-octal-numeric-literals': 'error',
37-
'jsonc/no-parenthesized': 'error',
38-
'jsonc/no-plus-sign': 'error',
39-
'jsonc/no-regexp-literals': 'error',
40-
'jsonc/no-sparse-arrays': 'error',
41-
'jsonc/no-template-literals': 'error',
42-
'jsonc/no-undefined-value': 'error',
43-
'jsonc/no-unicode-codepoint-escapes': 'error',
44-
'jsonc/no-useless-escape': 'error',
45-
'jsonc/object-curly-newline': ['error', { consistent: true, multiline: true }],
46-
'jsonc/object-curly-spacing': ['error', 'always'],
47-
'jsonc/object-property-newline': ['error', { allowMultiplePropertiesPerLine: true }],
48-
'jsonc/quote-props': 'error',
49-
'jsonc/quotes': 'error',
50-
'jsonc/space-unary-ops': 'error',
51-
'jsonc/valid-json-number': 'error',
52-
'jsonc/vue-custom-block/no-parsing-error': 'error',
17+
{
18+
files: [GLOB_JSON, GLOB_JSON5, GLOB_JSONC],
19+
languageOptions: {
20+
parser: parserJsonc,
21+
},
22+
rules: {
23+
'jsonc/no-bigint-literals': 'error',
24+
'jsonc/no-binary-expression': 'error',
25+
'jsonc/no-binary-numeric-literals': 'error',
26+
'jsonc/no-dupe-keys': 'error',
27+
'jsonc/no-escape-sequence-in-identifier': 'error',
28+
'jsonc/no-floating-decimal': 'error',
29+
'jsonc/no-hexadecimal-numeric-literals': 'error',
30+
'jsonc/no-infinity': 'error',
31+
'jsonc/no-multi-str': 'error',
32+
'jsonc/no-nan': 'error',
33+
'jsonc/no-number-props': 'error',
34+
'jsonc/no-numeric-separators': 'error',
35+
'jsonc/no-octal': 'error',
36+
'jsonc/no-octal-escape': 'error',
37+
'jsonc/no-octal-numeric-literals': 'error',
38+
'jsonc/no-parenthesized': 'error',
39+
'jsonc/no-plus-sign': 'error',
40+
'jsonc/no-regexp-literals': 'error',
41+
'jsonc/no-sparse-arrays': 'error',
42+
'jsonc/no-template-literals': 'error',
43+
'jsonc/no-undefined-value': 'error',
44+
'jsonc/no-unicode-codepoint-escapes': 'error',
45+
'jsonc/no-useless-escape': 'error',
46+
'jsonc/space-unary-ops': 'error',
47+
'jsonc/valid-json-number': 'error',
48+
'jsonc/vue-custom-block/no-parsing-error': 'error',
49+
50+
...stylistic
51+
? {
52+
'jsonc/array-bracket-spacing': ['error', 'never'],
53+
'jsonc/comma-dangle': ['error', 'never'],
54+
'jsonc/comma-style': ['error', 'last'],
55+
'jsonc/indent': ['error', 2],
56+
'jsonc/key-spacing': ['error', { afterColon: true, beforeColon: false }],
57+
'jsonc/object-curly-newline': ['error', { consistent: true, multiline: true }],
58+
'jsonc/object-curly-spacing': ['error', 'always'],
59+
'jsonc/object-property-newline': ['error', { allowMultiplePropertiesPerLine: true }],
60+
'jsonc/quote-props': 'error',
61+
'jsonc/quotes': 'error',
62+
}
63+
: {},
64+
65+
...overrides,
66+
},
5367
},
54-
},
55-
]
68+
]
69+
}

0 commit comments

Comments
 (0)