Skip to content

Commit 206c94b

Browse files
armano2bradzacher
andcommitted
fix(typescript-estree): parsing of deeply nested new files in new folder (typescript-eslint#1412)
Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
1 parent 6aa6bc7 commit 206c94b

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,10 @@ function maybeInvalidateProgram(
394394
current = next;
395395
const folderWatchCallbacks = folderWatchCallbackTrackingMap.get(current);
396396
if (folderWatchCallbacks) {
397-
folderWatchCallbacks.forEach(cb =>
398-
cb(currentDir, ts.FileWatcherEventKind.Changed),
399-
);
397+
folderWatchCallbacks.forEach(cb => {
398+
cb(currentDir, ts.FileWatcherEventKind.Changed);
399+
cb(current!, ts.FileWatcherEventKind.Changed);
400+
});
400401
hasCallback = true;
401402
break;
402403
}

packages/typescript-estree/tests/lib/persistentParse.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const CONTENTS = {
77
foo: 'console.log("foo")',
88
bar: 'console.log("bar")',
99
'baz/bar': 'console.log("baz bar")',
10+
'bat/baz/bar': 'console.log("bat/baz/bar")',
1011
};
1112

1213
const tmpDirs = new Set<tmp.DirResult>();
@@ -22,7 +23,7 @@ afterEach(() => {
2223
function writeTSConfig(dirName: string, config: Record<string, unknown>): void {
2324
fs.writeFileSync(path.join(dirName, 'tsconfig.json'), JSON.stringify(config));
2425
}
25-
function writeFile(dirName: string, file: 'foo' | 'bar' | 'baz/bar'): void {
26+
function writeFile(dirName: string, file: keyof typeof CONTENTS): void {
2627
fs.writeFileSync(path.join(dirName, 'src', `${file}.ts`), CONTENTS[file]);
2728
}
2829
function renameFile(dirName: string, src: 'bar', dest: 'baz/bar'): void {
@@ -53,7 +54,7 @@ function setup(tsconfig: Record<string, unknown>, writeBar = true): string {
5354
return tmpDir.name;
5455
}
5556

56-
function parseFile(filename: 'foo' | 'bar' | 'baz/bar', tmpDir: string): void {
57+
function parseFile(filename: keyof typeof CONTENTS, tmpDir: string): void {
5758
parseAndGenerateServices(CONTENTS.foo, {
5859
project: './tsconfig.json',
5960
tsconfigRootDir: tmpDir,
@@ -112,6 +113,24 @@ function baseTests(
112113
expect(() => parseFile(bazSlashBar, PROJECT_DIR)).not.toThrow();
113114
});
114115

116+
it('allows parsing of deeply nested new files in new folder', () => {
117+
const PROJECT_DIR = setup(tsConfigIncludeAll);
118+
119+
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
120+
121+
// Create deep folder structure after first parse (this is important step)
122+
// context: https://github.com/typescript-eslint/typescript-eslint/issues/1394
123+
fs.mkdirSync(path.join(PROJECT_DIR, 'src', 'bat'));
124+
fs.mkdirSync(path.join(PROJECT_DIR, 'src', 'bat', 'baz'));
125+
126+
const bazSlashBar = path.join('bat', 'baz', 'bar') as 'bat/baz/bar';
127+
128+
// write a new file and attempt to parse it
129+
writeFile(PROJECT_DIR, bazSlashBar);
130+
131+
expect(() => parseFile(bazSlashBar, PROJECT_DIR)).not.toThrow();
132+
});
133+
115134
it('allows renaming of files', () => {
116135
const PROJECT_DIR = setup(tsConfigIncludeAll, true);
117136
const bazSlashBar = path.join('baz', 'bar') as 'baz/bar';

0 commit comments

Comments
 (0)