Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9720d2c

Browse files
armano2bradzacher
authored andcommitted
fix(typescript-estree): fix persisted parse for relative paths (typescript-eslint#1424)
1 parent a37ff9f commit 9720d2c

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,12 @@ function maybeInvalidateProgram(
395395
const folderWatchCallbacks = folderWatchCallbackTrackingMap.get(current);
396396
if (folderWatchCallbacks) {
397397
folderWatchCallbacks.forEach(cb => {
398-
cb(currentDir, ts.FileWatcherEventKind.Changed);
398+
if (currentDir !== current) {
399+
cb(currentDir, ts.FileWatcherEventKind.Changed);
400+
}
399401
cb(current!, ts.FileWatcherEventKind.Changed);
400402
});
401403
hasCallback = true;
402-
break;
403404
}
404405

405406
next = canonicalDirname(current);

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ function getCanonicalFileName(filePath: string): CanonicalPath {
3838
return correctPathCasing(normalized) as CanonicalPath;
3939
}
4040

41+
function ensureAbsolutePath(p: string, extra: Extra): string {
42+
return path.isAbsolute(p)
43+
? p
44+
: path.join(extra.tsconfigRootDir || process.cwd(), p);
45+
}
46+
4147
function getTsconfigPath(tsconfigPath: string, extra: Extra): CanonicalPath {
42-
return getCanonicalFileName(
43-
path.isAbsolute(tsconfigPath)
44-
? tsconfigPath
45-
: path.join(extra.tsconfigRootDir || process.cwd(), tsconfigPath),
46-
);
48+
return getCanonicalFileName(ensureAbsolutePath(tsconfigPath, extra));
4749
}
4850

4951
function canonicalDirname(p: CanonicalPath): CanonicalPath {
@@ -84,6 +86,7 @@ export {
8486
canonicalDirname,
8587
CanonicalPath,
8688
DEFAULT_COMPILER_OPTIONS,
89+
ensureAbsolutePath,
8790
getCanonicalFileName,
8891
getScriptKind,
8992
getTsconfigPath,

packages/typescript-estree/src/parser.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { createSourceFile } from './create-program/createSourceFile';
1111
import { Extra, TSESTreeOptions, ParserServices } from './parser-options';
1212
import { getFirstSemanticOrSyntacticError } from './semantic-or-syntactic-errors';
1313
import { TSESTree } from './ts-estree';
14+
import { ensureAbsolutePath } from './create-program/shared';
1415

1516
/**
1617
* This needs to be kept in sync with the top-level README.md in the
@@ -145,6 +146,7 @@ function applyParserOptionsToExtra(options: TSESTreeOptions): void {
145146
} else {
146147
extra.filePath = getFileName(extra);
147148
}
149+
extra.filePath = ensureAbsolutePath(extra.filePath, extra);
148150

149151
/**
150152
* The JSX AST changed the node type for string literals

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const CONTENTS = {
1010
'bat/baz/bar': 'console.log("bat/baz/bar")',
1111
};
1212

13+
const cwdCopy = process.cwd();
1314
const tmpDirs = new Set<tmp.DirResult>();
1415
afterEach(() => {
1516
// stop watching the files and folders
@@ -18,6 +19,9 @@ afterEach(() => {
1819
// clean up the temporary files and folders
1920
tmpDirs.forEach(t => t.removeCallback());
2021
tmpDirs.clear();
22+
23+
// restore original cwd
24+
process.chdir(cwdCopy);
2125
});
2226

2327
function writeTSConfig(dirName: string, config: Record<string, unknown>): void {
@@ -54,14 +58,24 @@ function setup(tsconfig: Record<string, unknown>, writeBar = true): string {
5458
return tmpDir.name;
5559
}
5660

57-
function parseFile(filename: keyof typeof CONTENTS, tmpDir: string): void {
58-
parseAndGenerateServices(CONTENTS.foo, {
61+
function parseFile(
62+
filename: keyof typeof CONTENTS,
63+
tmpDir: string,
64+
relative?: boolean,
65+
): void {
66+
parseAndGenerateServices(CONTENTS[filename], {
5967
project: './tsconfig.json',
6068
tsconfigRootDir: tmpDir,
61-
filePath: path.join(tmpDir, 'src', `${filename}.ts`),
69+
filePath: relative
70+
? path.join('src', `${filename}.ts`)
71+
: path.join(tmpDir, 'src', `${filename}.ts`),
6272
});
6373
}
6474

75+
function existsSync(filename: keyof typeof CONTENTS, tmpDir = ''): boolean {
76+
return fs.existsSync(path.join(tmpDir, 'src', `${filename}.ts`));
77+
}
78+
6579
function baseTests(
6680
tsConfigExcludeBar: Record<string, unknown>,
6781
tsConfigIncludeAll: Record<string, unknown>,
@@ -161,6 +175,27 @@ function baseTests(
161175
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
162176
expect(() => parseFile('bar', PROJECT_DIR)).not.toThrow();
163177
});
178+
179+
it('should work with relative paths', () => {
180+
const PROJECT_DIR = setup(tsConfigIncludeAll, false);
181+
process.chdir(PROJECT_DIR);
182+
183+
// parse once to: assert the config as correct, and to make sure the program is setup
184+
expect(() => parseFile('foo', PROJECT_DIR, true)).not.toThrow();
185+
// bar should throw because it doesn't exist yet
186+
expect(() => parseFile('bar', PROJECT_DIR, true)).toThrow();
187+
188+
// write a new file and attempt to parse it
189+
writeFile(PROJECT_DIR, 'bar');
190+
191+
// make sure that file is correctly created
192+
expect(existsSync('bar')).toEqual(true);
193+
expect(existsSync('bar', PROJECT_DIR)).toEqual(true);
194+
195+
// both files should parse fine now
196+
expect(() => parseFile('foo', PROJECT_DIR, true)).not.toThrow();
197+
expect(() => parseFile('bar', PROJECT_DIR, true)).not.toThrow();
198+
});
164199
}
165200

166201
describe('persistent parse', () => {

0 commit comments

Comments
 (0)