Skip to content

Commit 3fae70a

Browse files
committed
fix!: fix type aware rules, merge typescriptWithTypes to typescript
1 parent f485047 commit 3fae70a

File tree

4 files changed

+53
-65
lines changed

4 files changed

+53
-65
lines changed

eslint.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export default antfu(
77
'fixtures',
88
'_fixtures',
99
],
10+
// typescript: {
11+
// tsconfigPath: 'tsconfig.json',
12+
// },
1013
},
1114
{
1215
files: ['src/**/*.ts'],

src/configs/typescript.ts

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,41 @@ import type { FlatESLintConfigItem } from 'eslint-define-config'
33
import { GLOB_TS, GLOB_TSX } from '../globs'
44
import { parserTs, pluginAntfu, pluginImport, pluginTs } from '../plugins'
55
import { OFF } from '../flags'
6-
import type { OptionsComponentExts, OptionsOverrides, OptionsTypeScriptWithTypes } from '../types'
6+
import type { OptionsComponentExts, OptionsOverrides, OptionsTypeScriptParserOptions, OptionsTypeScriptWithTypes } from '../types'
77
import { renameRules } from '../utils'
88

99
export function typescript(
10-
options?: OptionsComponentExts & OptionsOverrides,
10+
options?: OptionsComponentExts & OptionsOverrides & OptionsTypeScriptWithTypes & OptionsTypeScriptParserOptions,
1111
): FlatESLintConfigItem[] {
1212
const {
1313
componentExts = [],
1414
overrides = {},
15+
parserOptions = {},
16+
tsconfigPath,
1517
} = options ?? {}
1618

19+
const typeAwareRules: FlatESLintConfigItem['rules'] = {
20+
'dot-notation': OFF,
21+
'no-implied-eval': OFF,
22+
'no-throw-literal': OFF,
23+
'ts/await-thenable': 'error',
24+
'ts/dot-notation': ['error', { allowKeywords: true }],
25+
'ts/no-floating-promises': 'error',
26+
'ts/no-for-in-array': 'error',
27+
'ts/no-implied-eval': 'error',
28+
'ts/no-misused-promises': 'error',
29+
'ts/no-throw-literal': 'error',
30+
'ts/no-unnecessary-type-assertion': 'error',
31+
'ts/no-unsafe-argument': 'error',
32+
'ts/no-unsafe-assignment': 'error',
33+
'ts/no-unsafe-call': 'error',
34+
'ts/no-unsafe-member-access': 'error',
35+
'ts/no-unsafe-return': 'error',
36+
'ts/restrict-plus-operands': 'error',
37+
'ts/restrict-template-expressions': 'error',
38+
'ts/unbound-method': 'error',
39+
}
40+
1741
return [
1842
{
1943
// Install the plugins without globs, so they can be configured separately.
@@ -33,6 +57,13 @@ export function typescript(
3357
parser: parserTs,
3458
parserOptions: {
3559
sourceType: 'module',
60+
...tsconfigPath
61+
? {
62+
project: [tsconfigPath],
63+
tsconfigRootDir: process.cwd(),
64+
}
65+
: {},
66+
...parserOptions as any,
3667
},
3768
},
3869
rules: {
@@ -78,6 +109,7 @@ export function typescript(
78109
'ts/prefer-ts-expect-error': 'error',
79110
'ts/triple-slash-reference': OFF,
80111

112+
...tsconfigPath ? typeAwareRules : {},
81113
...overrides,
82114
},
83115
},
@@ -104,54 +136,3 @@ export function typescript(
104136
},
105137
]
106138
}
107-
108-
export function typescriptWithTypes(
109-
options: OptionsTypeScriptWithTypes & OptionsComponentExts & OptionsOverrides,
110-
): FlatESLintConfigItem[] {
111-
const {
112-
componentExts = [],
113-
tsconfigPath,
114-
tsconfigRootDir = process.cwd(),
115-
overrides = {},
116-
} = options
117-
118-
return [
119-
{
120-
files: [
121-
GLOB_TS,
122-
GLOB_TSX,
123-
...componentExts.map(ext => `**/*.${ext}`),
124-
'!**/*.md/*.*',
125-
],
126-
languageOptions: {
127-
parser: parserTs,
128-
parserOptions: {
129-
project: [tsconfigPath],
130-
tsconfigRootDir,
131-
},
132-
},
133-
rules: {
134-
'dot-notation': OFF,
135-
'no-implied-eval': OFF,
136-
'no-throw-literal': OFF,
137-
'ts/await-thenable': 'error',
138-
'ts/dot-notation': ['error', { allowKeywords: true }],
139-
'ts/no-floating-promises': 'error',
140-
'ts/no-for-in-array': 'error',
141-
'ts/no-implied-eval': 'error',
142-
'ts/no-misused-promises': 'error',
143-
'ts/no-throw-literal': 'error',
144-
'ts/no-unnecessary-type-assertion': 'error',
145-
'ts/no-unsafe-argument': 'error',
146-
'ts/no-unsafe-assignment': 'error',
147-
'ts/no-unsafe-call': 'error',
148-
'ts/no-unsafe-member-access': 'error',
149-
'ts/no-unsafe-return': 'error',
150-
'ts/restrict-plus-operands': 'error',
151-
'ts/restrict-template-expressions': 'error',
152-
'ts/unbound-method': 'error',
153-
...overrides,
154-
},
155-
},
156-
]
157-
}

src/factory.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
stylistic,
1818
test,
1919
typescript,
20-
typescriptWithTypes,
2120
unicorn,
2221
vue,
2322
yaml,
@@ -94,17 +93,12 @@ export function antfu(options: OptionsConfig & FlatESLintConfigItem = {}, ...use
9493

9594
if (enableTypeScript) {
9695
configs.push(typescript({
96+
...typeof enableTypeScript !== 'boolean'
97+
? enableTypeScript
98+
: {},
9799
componentExts,
98100
overrides: overrides.typescript,
99101
}))
100-
101-
if (typeof enableTypeScript !== 'boolean') {
102-
configs.push(typescriptWithTypes({
103-
...enableTypeScript,
104-
componentExts,
105-
overrides: overrides.typescriptWithTypes,
106-
}))
107-
}
108102
}
109103

110104
if (enableStylistic)

src/types.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { FlatGitignoreOptions } from 'eslint-config-flat-gitignore'
22
import type { FlatESLintConfigItem } from 'eslint-define-config'
3+
import type { ParserOptions } from '@typescript-eslint/parser'
34

45
export interface OptionsComponentExts {
56
/**
@@ -11,9 +12,19 @@ export interface OptionsComponentExts {
1112
componentExts?: string[]
1213
}
1314

15+
export interface OptionsTypeScriptParserOptions {
16+
/**
17+
* Additional parser options for TypeScript.
18+
*/
19+
parserOptions?: Partial<ParserOptions>
20+
}
21+
1422
export interface OptionsTypeScriptWithTypes {
15-
tsconfigPath: string
16-
tsconfigRootDir?: string
23+
/**
24+
* When this options is provided, type aware rules will be enabled.
25+
* @see https://typescript-eslint.io/linting/typed-linting/
26+
*/
27+
tsconfigPath?: string
1728
}
1829

1930
export interface OptionsHasTypeScript {
@@ -106,7 +117,6 @@ export interface OptionsConfig {
106117
overrides?: {
107118
javascript?: FlatESLintConfigItem['rules']
108119
typescript?: FlatESLintConfigItem['rules']
109-
typescriptWithTypes?: FlatESLintConfigItem['rules']
110120
test?: FlatESLintConfigItem['rules']
111121
vue?: FlatESLintConfigItem['rules']
112122
jsonc?: FlatESLintConfigItem['rules']

0 commit comments

Comments
 (0)