Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript-estree): fix persisted parse for relative paths #1424

Merged
merged 10 commits into from Jan 12, 2020
Expand Up @@ -395,7 +395,9 @@ function maybeInvalidateProgram(
const folderWatchCallbacks = folderWatchCallbackTrackingMap.get(current);
if (folderWatchCallbacks) {
folderWatchCallbacks.forEach(cb => {
cb(currentDir, ts.FileWatcherEventKind.Changed);
if (currentDir !== current) {
cb(currentDir, ts.FileWatcherEventKind.Changed);
}
cb(current!, ts.FileWatcherEventKind.Changed);
});
hasCallback = true;
Expand Down
40 changes: 38 additions & 2 deletions packages/typescript-estree/tests/lib/persistentParse.ts
Expand Up @@ -10,6 +10,7 @@ const CONTENTS = {
'bat/baz/bar': 'console.log("bat/baz/bar")',
};

const cwdCopy = process.cwd();
const tmpDirs = new Set<tmp.DirResult>();
afterEach(() => {
// stop watching the files and folders
Expand All @@ -18,6 +19,9 @@ afterEach(() => {
// clean up the temporary files and folders
tmpDirs.forEach(t => t.removeCallback());
tmpDirs.clear();

// restore original cwd
process.chdir(cwdCopy);
});

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

function parseFile(filename: keyof typeof CONTENTS, tmpDir: string): void {
function parseFile(
filename: keyof typeof CONTENTS,
tmpDir: string,
relative?: boolean,
): void {
parseAndGenerateServices(CONTENTS.foo, {
project: './tsconfig.json',
tsconfigRootDir: tmpDir,
filePath: path.join(tmpDir, 'src', `${filename}.ts`),
filePath: relative
? path.join('src', `${filename}.ts`)
: path.join(tmpDir, 'src', `${filename}.ts`),
});
}

function existsSync(filename: keyof typeof CONTENTS, tmpDir: string = '') {
return fs.existsSync(path.join(tmpDir, 'src', `${filename}.ts`));
}

function baseTests(
tsConfigExcludeBar: Record<string, unknown>,
tsConfigIncludeAll: Record<string, unknown>,
Expand Down Expand Up @@ -161,6 +175,28 @@ function baseTests(
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
expect(() => parseFile('bar', PROJECT_DIR)).not.toThrow();
});

it('should work with relative paths', () => {
const PROJECT_DIR = setup(tsConfigIncludeAll, false);
console.log(process.cwd());
armano2 marked this conversation as resolved.
Show resolved Hide resolved
process.chdir(PROJECT_DIR);

// parse once to: assert the config as correct, and to make sure the program is setup
expect(() => parseFile('foo', PROJECT_DIR, true)).not.toThrow();
// bar should throw because it doesn't exist yet
expect(() => parseFile('bar', PROJECT_DIR, true)).toThrow();

// write a new file and attempt to parse it
writeFile(PROJECT_DIR, 'bar');

// make sure that file is correctly created
expect(existsSync('bar')).toEqual(true);
expect(existsSync('bar', PROJECT_DIR)).toEqual(true);

// both files should parse fine now
expect(() => parseFile('foo', PROJECT_DIR, true)).not.toThrow();
expect(() => parseFile('bar', PROJECT_DIR, true)).not.toThrow();
});
}

describe('persistent parse', () => {
Expand Down