Skip to content

Commit 537f012

Browse files
committed
feat: use typescript project only for type-aware files
1 parent 98f9174 commit 537f012

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

src/configs/typescript.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export async function typescript(
2727

2828
const filesTypeAware = options.filesTypeAware ?? [GLOB_TS, GLOB_TSX]
2929

30+
const tsconfigPath = options?.tsconfigPath
31+
? toArray(options.tsconfigPath)
32+
: undefined
33+
const isTypeAware = !!tsconfigPath
34+
3035
const typeAwareRules: FlatConfigItem['rules'] = {
3136
'dot-notation': 'off',
3237
'no-implied-eval': 'off',
@@ -49,32 +54,25 @@ export async function typescript(
4954
'ts/unbound-method': 'error',
5055
}
5156

52-
const tsconfigPath = options?.tsconfigPath
53-
? toArray(options.tsconfigPath)
54-
: undefined
55-
5657
const [pluginTs, parserTs] = await Promise.all([
5758
interopDefault(import('@typescript-eslint/eslint-plugin')),
5859
interopDefault(import('@typescript-eslint/parser')),
5960
] as const)
6061

61-
return [
62-
{
63-
// Install the plugins without globs, so they can be configured separately.
64-
name: 'coderwyd:typescript:setup',
65-
plugins: {
66-
antfu: pluginAntfu,
67-
ts: pluginTs as any,
68-
},
69-
},
70-
{
62+
function makeParser(
63+
typeAware: boolean,
64+
files: string[],
65+
ignores?: string[],
66+
): FlatConfigItem {
67+
return {
7168
files,
69+
...(ignores ? { ignores } : {}),
7270
languageOptions: {
7371
parser: parserTs,
7472
parserOptions: {
7573
extraFileExtensions: componentExts.map(ext => `.${ext}`),
7674
sourceType: 'module',
77-
...(tsconfigPath
75+
...(typeAware
7876
? {
7977
project: tsconfigPath,
8078
tsconfigRootDir: process.cwd(),
@@ -83,6 +81,28 @@ export async function typescript(
8381
...(parserOptions as any),
8482
},
8583
},
84+
name: `coderwyd:typescript:${typeAware ? 'type-aware-parser' : 'parser'}`,
85+
}
86+
}
87+
88+
return [
89+
{
90+
// Install the plugins without globs, so they can be configured separately.
91+
name: 'coderwyd:typescript:setup',
92+
plugins: {
93+
antfu: pluginAntfu,
94+
ts: pluginTs as any,
95+
},
96+
},
97+
// assign type-aware parser for type-aware files and type-unaware parser for the rest
98+
...(isTypeAware
99+
? [
100+
makeParser(true, filesTypeAware),
101+
makeParser(false, files, filesTypeAware),
102+
]
103+
: [makeParser(false, files)]),
104+
{
105+
files,
86106
name: 'coderwyd:typescript:rules',
87107
rules: {
88108
...renameRules(

0 commit comments

Comments
 (0)