10000 fix(typescript-estree): add default value for `parserOptions.projectF… · dopecodez/typescript-eslint@bf904ec · GitHub
[go: up one dir, main page]

Skip to content

Commit bf904ec

Browse files
authored
fix(typescript-estree): add default value for parserOptions.projectFolderIgnoreList and deduplicate resolved projects (typescript-eslint#2819)
In typescript-eslint#2418 I introduced a regression - I forgot to add in the default value for `projectFolderIgnoreList`. This means that globs have been matching `node_modules` since the v4.0 release! Oops :( This PR fixes that. It also hoists the tsconfig path canonicalisation up so that we can deduplicate early. Previously if you provided a config like `projects: ['./tsconfig.json', './**/tsconfig.json']`, then we would resolve this to `./tsconfig.json` and `tsconfig.json`, then later canonicalise them. This meant we'd check that same tsconfig twice! By hoisting the canonicalisation, we can deduplicate this early. Fixes typescript-eslint#2814
1 parent 14758d2 commit bf904ec

File tree

5 files changed

+39
-32
lines changed

5 files changed

+39
-32
lines changed

packages/typescript-estree/src/create-program/createDefaultProgram.ts

Lines changed: 2 additions & 2 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as ts from 'typescript';
44
import { Extra } from '../parser-options';
55
import {
66
ASTAndProgram,
7-
getTsconfigPath,
7+
CanonicalPath,
88
createDefaultCompilerOptionsFromExtra,
99
} from './shared';
1010

@@ -27,7 +27,7 @@ function createDefaultProgram(
2727
return undefined;
2828
}
2929

30-
const tsconfigPath = getTsconfigPath(extra.projects[0], extra);
30+
const tsconfigPath: CanonicalPath = extra.projects[0];
3131

3232
const commandLine = ts.getParsedCommandLineOfConfigFile(
3333
tsconfigPath,

packages/typescript-estree/src/create-program/createWatchProgram.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
CanonicalPath,
1010
createDefaultCompilerOptionsFromExtra,
1111
getCanonicalFileName,
12-
getTsconfigPath,
1312
} from './shared';
1413

1514
const log = debug('typescript-eslint:typescript-estree:createWatchProgram');
@@ -197,9 +196,7 @@ function getProgramsForProjects(
197196
* - the required program hasn't been created yet, or
198197
* - the file is new/renamed, and the program hasn't been updated.
199198
*/
200-
for (const rawTsconfigPath of extra.projects) {
201-
const tsconfigPath = getTsconfigPath(rawTsconfigPath, extra);
202-
199+
for (const tsconfigPath of extra.projects) {
203200
const existingWatch = knownWatchProgramMap.get(tsconfigPath);
204201

205202
if (existingWatch) {

packages/typescript-estree/src/create-program/shared.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ function ensureAbsolutePath(p: string, extra: Extra): string {
6060
: path.join(extra.tsconfigRootDir || process.cwd(), p);
6161
}
6262

63-
function getTsconfigPath(tsconfigPath: string, extra: Extra): CanonicalPath {
64-
return getCanonicalFileName(ensureAbsolutePath(tsconfigPath, extra));
65-
}
66-
6763
function canonicalDirname(p: CanonicalPath): CanonicalPath {
6864 D7AE
return path.dirname(p) as CanonicalPath;
6965
}
@@ -105,5 +101,4 @@ export {
105101
ensureAbsolutePath,
106102
getCanonicalFileName,
107103
getScriptKind,
108-
getTsconfigPath,
109104
};

packages/typescript-estree/src/parser-options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { DebugLevel } from '@typescript-eslint/types';
22
import { Program } from 'typescript';
33
import { TSESTree, TSNode, TSESTreeToTSNode, TSToken } from './ts-estree';
4+
import { CanonicalPath } from './create-program/shared';
45

56
type DebugModule = 'typescript-eslint' | 'eslint' | 'typescript';
67

@@ -19,7 +20,7 @@ export interface Extra {
1920
loc: boolean;
2021
log: (message: string) => void;
2122
preserveNodeMaps?: boolean;
22-
projects: string[];
23+
projects: CanonicalPath[];
2324
range: boolean;
2425
strict: boolean;
2526
tokens: null | TSESTree.Token[];

packages/typescript-estree/src/parser.ts

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import { createSourceFile } from './create-program/createSourceFile';
1212
import { Extra, TSESTreeOptions, ParserServices } from './parser-options';
1313
import { getFirstSemanticOrSyntacticError } from './semantic-or-syntactic-errors';
1414
import { TSESTree } from './ts-estree';
15-
import { ASTAndProgram, ensureAbsolutePath } from './create-program/shared';
15+
import {
16+
ASTAndProgram,
17+
CanonicalPath,
18+
ensureAbsolutePath,
19+
getCanonicalFileName,
20+
} from './create-program/shared';
1621

1722
const log = debug('typescript-eslint:typescript-estree:parser');
1823

@@ -109,46 +114,53 @@ function resetExtra(): void {
109114
};
110115
}
111116

117+
function getTsconfigPath(tsconfigPath: string, extra: Extra): CanonicalPath {
118+
return getCanonicalFileName(ensureAbsolutePath(tsconfigPath, extra));
119+
}
120+
112121
/**
113-
* Normalizes, sanitizes, resolves and filters the provided
122+
* Normalizes, sanitizes, resolves and filters the provided project paths
114123
*/
115124
function prepareAndTransformProjects(
116125
projectsInput: string | string[] | undefined,
117126
ignoreListInput: string[],
118-
): string[] {
119-
let projects: string[] = [];
127+
): CanonicalPath[] {
128+
const sanitizedProjects: string[] = [];
120129

121130
// Normalize and sanitize the project paths
122131
if (typeof projectsInput === 'string') {
123-
projects.push(projectsInput);
132+
sanitizedProjects.push(projectsInput);
124133
} else if (Array.isArray(projectsInput)) {
125134
for (const project of projectsInput) {
126135
if (typeof project === 'string') {
127-
projects.push(project);
136+
sanitizedProjects.push(project);
128137
}
129138
}
130139
}
131140

132-
if (projects.length === 0) {
133-
return projects;
141+
if (sanitizedProjects.length === 0) {
142+
return [];
134143
}
135144

136145
// Transform glob patterns into paths
137-
const globbedProjects = projects.filter(project => isGlob(project));
138-
projects = projects
139-
.filter(project => !isGlob(project))
140-
.concat(
141-
globSync([...globbedProjects, ...ignoreListInput], {
142-
cwd: extra.tsconfigRootDir,
143-
}),
144-
);
146+
const nonGlobProjects = sanitizedProjects.filter(project => !isGlob(project));
147+
const globProjects = sanitizedProjects.filter(project => isGlob(project));
148+
const uniqueCanonicalProjectPaths = new Set(
149+
nonGlobProjects
150+
.concat(
151+
globSync([...globProjects, ...ignoreListInput], {
152+
cwd: extra.tsconfigRootDir,
153+
}),
154+
)
155+
.map(project => getTsconfigPath(project, extra)),
156+
);
145157

146158
log(
147159
'parserOptions.project (excluding ignored) matched projects: %s',
148-
projects,
160+
uniqueCanonicalProjectPaths,
149161
);
150162

151-
return projects;
163+
return Array.from(uniqueCanonicalProjectPaths);
152164
}
153165

154166
function applyParserOptionsToExtra(options: TSESTreeOptions): void {
@@ -252,8 +264,9 @@ function applyParserOptionsToExtra(options: TSESTreeOptions): void {
252264
// NOTE - ensureAbsolutePath relies upon having the correct tsconfigRootDir in extra
253265
extra.filePath = ensureAbsolutePath(extra.filePath, extra);
254266

255-
// NOTE - prepareAndTransformProjects relies upon having the correct tsconfigRootDir in extra
256-
const projectFolderIgnoreList = (options.projectFolderIgnoreList ?? [])
267+
const projectFolderIgnoreList = (
268+
options.projectFolderIgnoreList ?? ['**/node_modules/**']
269+
)
257270
.reduce<string[]>((acc, folder) => {
258271
if (typeof folder === 'string') {
259272
acc.push(folder);
@@ -262,6 +275,7 @@ function applyParserOptionsToExtra(options: TSESTreeOptions): void {
262275
}, [])
263276
// prefix with a ! for not match glob
264277
.map(folder => (folder.startsWith('!') ? folder : `!${folder}`));
278+
// NOTE - prepareAndTransformProjects relies upon having the correct tsconfigRootDir in extra
265279
extra.projects = prepareAndTransformProjects(
266280
options.project,
267281
projectFolderIgnoreList,

0 commit comments

Comments
 (0)
0