fix(compiler-cli): Return original sourceFile instead of redirected sourceFile from getSourceFile (#26036)

Closes #22524

PR Close #26036
This commit is contained in:
Jon Wallsten
2019-07-08 16:50:19 +02:00
committed by Matias Niemelä
parent 40705f3366
commit 3166cffd28
4 changed files with 85 additions and 0 deletions

View File

@ -107,6 +107,47 @@ describe('perform watch', () => {
expect(getSourceFileSpy !).toHaveBeenCalledWith(utilTsPath, ts.ScriptTarget.ES5);
});
// https://github.com/angular/angular/pull/26036
it('should handle redirected source files', () => {
const config = createConfig();
const host = new MockWatchHost(config);
host.createCompilerHost = (options: ng.CompilerOptions) => {
const ngHost = ng.createCompilerHost({options});
return ngHost;
};
// This file structure has an identical version of "a" under the root node_modules and inside
// of "b". Because their package.json file indicates it is the exact same version of "a",
// TypeScript will transform the source file of "node_modules/b/node_modules/a/index.d.ts"
// into a redirect to "node_modules/a/index.d.ts". During watch compilations, we must assure
// not to reintroduce "node_modules/b/node_modules/a/index.d.ts" as its redirected source file,
// but instead using its original file.
testSupport.writeFiles({
'node_modules/a/index.js': `export class ServiceA {}`,
'node_modules/a/index.d.ts': `export declare class ServiceA {}`,
'node_modules/a/package.json': `{"name": "a", "version": "1.0"}`,
'node_modules/b/node_modules/a/index.js': `export class ServiceA {}`,
'node_modules/b/node_modules/a/index.d.ts': `export declare class ServiceA {}`,
'node_modules/b/node_modules/a/package.json': `{"name": "a", "version": "1.0"}`,
'node_modules/b/index.js': `export {ServiceA as ServiceB} from 'a';`,
'node_modules/b/index.d.ts': `export {ServiceA as ServiceB} from 'a';`,
'src/index.ts': `
import {ServiceA} from 'a';
import {ServiceB} from 'b';
`,
});
const indexTsPath = path.posix.join(testSupport.basePath, 'src', 'index.ts');
performWatchCompilation(host);
// Trigger a file change. This recreates the program from the old program. If redirect sources
// were introduced into the new program, this would fail due to an assertion failure in TS.
host.triggerFileChange(FileChangeEvent.Change, indexTsPath);
expectNoDiagnostics(config.options, host.diagnostics);
});
it('should recover from static analysis errors', () => {
const config = createConfig();
const host = new MockWatchHost(config);