Skip to content

Commit

Permalink
fix(ivy): add missing directoryExists() method to shim CompilerHost (a…
Browse files Browse the repository at this point in the history
…ngular#27470)

The method `ts.CompilerHost.directoryExists` is optional, and was not
previously handled by our ts.CompilerHost wrapper for factory and
summary shims (GeneratedShimsHostWrapper).

TypeScript checks for the existence of this method and silently ignores
things like typeRoots if it's not found. This commit adds proper handling
of directoryExists() to the shim.

A test is also added which verifies typeRoots behavior works when shims
are enabled.

PR Close angular#27470
  • Loading branch information
alxhub authored and ngfelixl committed Jan 27, 2019
1 parent 6edfa76 commit 9b9153a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/compiler-cli/src/ngtsc/shims/src/host.ts
Expand Up @@ -39,11 +39,16 @@ export class GeneratedShimsHostWrapper implements ts.CompilerHost {
(delegate.resolveTypeReferenceDirectives as ts3ResolveTypeReferenceDirectives) !(
names, containingFile);
}
if (delegate.directoryExists !== undefined) {
this.directoryExists = (directoryName: string) => delegate.directoryExists !(directoryName);
}
}

resolveTypeReferenceDirectives?:
(names: string[], containingFile: string) => ts.ResolvedTypeReferenceDirective[];

directoryExists?: (directoryName: string) => boolean;

getSourceFile(
fileName: string, languageVersion: ts.ScriptTarget,
onError?: ((message: string) => void)|undefined,
Expand Down
1 change: 0 additions & 1 deletion packages/compiler-cli/test/ngtsc/env.ts
Expand Up @@ -57,7 +57,6 @@ export class NgtscTestEnvironment {
"skipLibCheck": true,
"noImplicitAny": true,
"strictNullChecks": true,
"types": [],
"outDir": "built",
"rootDir": ".",
"baseUrl": ".",
Expand Down
32 changes: 32 additions & 0 deletions packages/compiler-cli/test/ngtsc/ngtsc_spec.ts
Expand Up @@ -1046,4 +1046,36 @@ describe('ngtsc behavioral tests', () => {
});
});
});

it('should compile programs with typeRoots', () => {
// Write out a custom tsconfig.json that includes 'typeRoots' and 'files'. 'files' is necessary
// because otherwise TS picks up the testTypeRoot/test/index.d.ts file into the program
// automatically. Shims are also turned on (via allowEmptyCodegenFiles) because the shim
// ts.CompilerHost wrapper can break typeRoot functionality (which this test is meant to
// detect).
env.write('tsconfig.json', `{
"extends": "./tsconfig-base.json",
"angularCompilerOptions": {
"allowEmptyCodegenFiles": true
},
"compilerOptions": {
"typeRoots": ["./testTypeRoot"],
},
"files": ["./test.ts"]
}`);
env.write('test.ts', `
import {Test} from 'ambient';
console.log(Test);
`);
env.write('testTypeRoot/.exists', '');
env.write('testTypeRoot/test/index.d.ts', `
declare module 'ambient' {
export const Test = 'This is a test';
}
`);

env.driveMain();

// Success is enough to indicate that this passes.
});
});

0 comments on commit 9b9153a

Please sign in to comment.