fix(compiler-cli): don't try to tag non-ts files as shims (#36987)

Some projects include .js source files (via the TypeScript allowJs option).
Previously, the compiler would attempt to tag these files for shims, which
caused errors as the regex used to create shim filenames assumes a .ts file.
This commit fixes the bug by filtering out non-ts files during tagging.

PR Close #36987
This commit is contained in:
Alex Rickabaugh 2020-05-07 12:57:41 -07:00
parent d0280a0335
commit 42d1091d6a
4 changed files with 42 additions and 1 deletions

View File

@ -9,6 +9,7 @@
import * as ts from 'typescript'; import * as ts from 'typescript';
import {absoluteFrom, absoluteFromSourceFile} from '../../file_system'; import {absoluteFrom, absoluteFromSourceFile} from '../../file_system';
import {isNonDeclarationTsPath} from '../../util/src/typescript';
import {isExtended as isExtendedSf, isShim, NgExtension, sfExtensionData} from './expando'; import {isExtended as isExtendedSf, isShim, NgExtension, sfExtensionData} from './expando';
import {makeShimFileName} from './util'; import {makeShimFileName} from './util';
@ -42,7 +43,8 @@ export class ShimReferenceTagger {
* Tag `sf` with any needed references if it's not a shim itself. * Tag `sf` with any needed references if it's not a shim itself.
*/ */
tag(sf: ts.SourceFile): void { tag(sf: ts.SourceFile): void {
if (!this.enabled || sf.isDeclarationFile || isShim(sf) || this.tagged.has(sf)) { if (!this.enabled || sf.isDeclarationFile || isShim(sf) || this.tagged.has(sf) ||
!isNonDeclarationTsPath(sf.fileName)) {
return; return;
} }

View File

@ -40,6 +40,17 @@ runInEachFileSystem(() => {
expectReferencedFiles(sf, []); expectReferencedFiles(sf, []);
}); });
it('should not tag .js files', () => {
const tagger = new ShimReferenceTagger(['test1', 'test2']);
const fileName = _('/file.js');
const sf = makeArbitrarySf(fileName);
expectReferencedFiles(sf, []);
tagger.tag(sf);
expectReferencedFiles(sf, []);
});
it('should not tag shim files', () => { it('should not tag shim files', () => {
const tagger = new ShimReferenceTagger(['test1', 'test2']); const tagger = new ShimReferenceTagger(['test1', 'test2']);
const fileName = _('/file.ts'); const fileName = _('/file.ts');

View File

@ -59,6 +59,7 @@ export class NgtscTestEnvironment {
"outDir": "built", "outDir": "built",
"rootDir": ".", "rootDir": ".",
"baseUrl": ".", "baseUrl": ".",
"allowJs": true,
"declaration": true, "declaration": true,
"target": "es5", "target": "es5",
"newLine": "lf", "newLine": "lf",

View File

@ -3540,6 +3540,33 @@ runInEachFileSystem(os => {
env.tsconfig({'generateNgFactoryShims': true}); env.tsconfig({'generateNgFactoryShims': true});
}); });
it('should not be generated for .js files', () => {
// This test verifies that the compiler does not attempt to generate shim files for non-TS
// input files (in this case, other.js).
env.write('test.ts', `
import {Component, NgModule} from '@angular/core';
@Component({
selector: 'test-cmp',
template: 'This is a template',
})
export class TestCmp {}
@NgModule({
declarations: [TestCmp],
exports: [TestCmp],
})
export class TestModule {}
`);
env.write('other.js', `
export class TestJs {}
`);
expect(env.driveDiagnostics()).toEqual([]);
env.assertExists('test.ngfactory.js');
env.assertDoesNotExist('other.ngfactory.js');
});
it('should be able to depend on an existing factory shim', () => { it('should be able to depend on an existing factory shim', () => {
// This test verifies that ngfactory files from the compilations of dependencies are // This test verifies that ngfactory files from the compilations of dependencies are
// available to import in a fresh compilation. It is derived from a bug observed in g3 where // available to import in a fresh compilation. It is derived from a bug observed in g3 where