Skip to content

Commit 9e2bf16

Browse files
authoredJan 17, 2024
feat: use typescript project only for type-aware files, closes #380 (#384)
1 parent 20a1d6d commit 9e2bf16

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed
 

‎src/configs/typescript.ts

+30-15
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export async function typescript(
1919
]
2020

2121
const filesTypeAware = options.filesTypeAware ?? [GLOB_TS, GLOB_TSX]
22+
const tsconfigPath = options?.tsconfigPath
23+
? toArray(options.tsconfigPath)
24+
: undefined
25+
const isTypeAware = !!tsconfigPath
2226

2327
const typeAwareRules: FlatConfigItem['rules'] = {
2428
'dot-notation': 'off',
@@ -42,10 +46,6 @@ export async function typescript(
4246
'ts/unbound-method': 'error',
4347
}
4448

45-
const tsconfigPath = options?.tsconfigPath
46-
? toArray(options.tsconfigPath)
47-
: undefined
48-
4949
const [
5050
pluginTs,
5151
parserTs,
@@ -54,23 +54,16 @@ export async function typescript(
5454
interopDefault(import('@typescript-eslint/parser')),
5555
] as const)
5656

57-
return [
58-
{
59-
// Install the plugins without globs, so they can be configured separately.
60-
name: 'antfu:typescript:setup',
61-
plugins: {
62-
antfu: pluginAntfu,
63-
ts: pluginTs as any,
64-
},
65-
},
66-
{
57+
function makeParser(typeAware: boolean, files: string[], ignores?: string[]): FlatConfigItem {
58+
return {
6759
files,
60+
...ignores ? { ignores } : {},
6861
languageOptions: {
6962
parser: parserTs,
7063
parserOptions: {
7164
extraFileExtensions: componentExts.map(ext => `.${ext}`),
7265
sourceType: 'module',
73-
...tsconfigPath
66+
...typeAware
7467
? {
7568
project: tsconfigPath,
7669
tsconfigRootDir: process.cwd(),
@@ -79,6 +72,28 @@ export async function typescript(
7972
...parserOptions as any,
8073
},
8174
},
75+
name: `antfu:typescript:${typeAware ? 'type-aware-parser' : 'parser'}`,
76+
}
77+
}
78+
79+
return [
80+
{
81+
// Install the plugins without globs, so they can be configured separately.
82+
name: 'antfu:typescript:setup',
83+
plugins: {
84+
antfu: pluginAntfu,
85+
ts: pluginTs as any,
86+
},
87+
},
88+
// assign type-aware parser for type-aware files and type-unaware parser for the rest
89+
...isTypeAware
90+
? [
91+
makeParser(true, filesTypeAware),
92+
makeParser(false, files, filesTypeAware),
93+
]
94+
: [makeParser(false, files)],
95+
{
96+
files,
8297
name: 'antfu:typescript:rules',
8398
rules: {
8499
...renameRules(

0 commit comments

Comments
 (0)
Please sign in to comment.