2019-03-08 14:32:49 -05:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2019-03-08 14:32:49 -05:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
2019-04-27 18:26:13 -04:00
|
|
|
|
|
|
|
import {Diagnostic} from '@angular/compiler-cli';
|
2019-03-08 14:32:49 -05:00
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
|
|
|
import {ErrorCode, ngErrorCode} from '../../src/ngtsc/diagnostics';
|
2019-06-06 15:22:32 -04:00
|
|
|
import {runInEachFileSystem} from '../../src/ngtsc/file_system/testing';
|
2019-04-27 18:26:13 -04:00
|
|
|
import {getTokenAtPosition} from '../../src/ngtsc/util/src/typescript';
|
2019-06-06 15:22:32 -04:00
|
|
|
import {loadStandardTestFiles} from '../helpers/src/mock_file_loading';
|
2019-03-08 14:32:49 -05:00
|
|
|
|
|
|
|
import {NgtscTestEnvironment} from './env';
|
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
const testFiles = loadStandardTestFiles();
|
|
|
|
|
|
|
|
runInEachFileSystem(() => {
|
|
|
|
describe('ngtsc module scopes', () => {
|
2020-04-07 15:43:43 -04:00
|
|
|
let env!: NgtscTestEnvironment;
|
2019-06-06 15:22:32 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
env = NgtscTestEnvironment.setup(testFiles);
|
|
|
|
env.tsconfig();
|
|
|
|
});
|
2019-03-08 14:32:49 -05:00
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
describe('diagnostics', () => {
|
2019-12-13 17:29:05 -05:00
|
|
|
describe('declarations', () => {
|
|
|
|
it('should detect when a random class is declared', () => {
|
|
|
|
env.write('test.ts', `
|
|
|
|
import {NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
export class RandomClass {}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [RandomClass],
|
|
|
|
})
|
|
|
|
export class Module {}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const diags = env.driveDiagnostics();
|
|
|
|
expect(diags.length).toBe(1);
|
|
|
|
const node = diagnosticToNode(diags[0], ts.isIdentifier);
|
|
|
|
expect(node.text).toEqual('RandomClass');
|
|
|
|
expect(diags[0].messageText).toContain('is not a directive, a component, or a pipe.');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect when a declaration lives outside the current compilation', () => {
|
|
|
|
env.write('dir.d.ts', `
|
|
|
|
import {ɵɵDirectiveDefWithMeta} from '@angular/core';
|
|
|
|
|
|
|
|
export declare class ExternalDir {
|
|
|
|
static ɵdir: ɵɵDirectiveDefWithMeta<ExternalDir, '[test]', never, never, never, never>;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
env.write('test.ts', `
|
|
|
|
import {NgModule} from '@angular/core';
|
|
|
|
import {ExternalDir} from './dir';
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [ExternalDir],
|
|
|
|
})
|
|
|
|
export class Module {}
|
|
|
|
`);
|
|
|
|
const diags = env.driveDiagnostics();
|
|
|
|
expect(diags.length).toBe(1);
|
|
|
|
const node = diagnosticToNode(diags[0], ts.isIdentifier);
|
|
|
|
expect(node.text).toEqual('ExternalDir');
|
|
|
|
expect(diags[0].messageText).toContain(`not a part of the current compilation`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect when a declaration is shared between two modules', () => {
|
|
|
|
env.write('test.ts', `
|
|
|
|
import {Directive, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: '[test]'})
|
|
|
|
export class TestDir {}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [TestDir]
|
|
|
|
})
|
|
|
|
export class ModuleA {}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [TestDir],
|
|
|
|
})
|
|
|
|
export class ModuleB {}
|
|
|
|
`);
|
|
|
|
const diags = env.driveDiagnostics();
|
|
|
|
expect(diags.length).toBe(1);
|
|
|
|
const node = findContainingClass(diagnosticToNode(diags[0], ts.isIdentifier));
|
2020-04-07 15:43:43 -04:00
|
|
|
expect(node.name!.text).toEqual('TestDir');
|
2019-12-13 17:29:05 -05:00
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
const relatedNodes = new Set(diags[0].relatedInformation!.map(
|
2019-12-13 17:29:05 -05:00
|
|
|
related =>
|
2020-04-07 15:43:43 -04:00
|
|
|
findContainingClass(diagnosticToNode(related, ts.isIdentifier)).name!.text));
|
2019-12-13 17:29:05 -05:00
|
|
|
expect(relatedNodes).toContain('ModuleA');
|
|
|
|
expect(relatedNodes).toContain('ModuleB');
|
|
|
|
expect(relatedNodes.size).toBe(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect when a declaration is repeated within the same module', () => {
|
|
|
|
env.write('test.ts', `
|
|
|
|
import {Directive, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: '[test]'})
|
|
|
|
export class TestDir {}
|
|
|
|
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [TestDir, TestDir],
|
|
|
|
})
|
|
|
|
export class Module {}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const diags = env.driveDiagnostics();
|
|
|
|
expect(diags.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect when a declaration is shared between two modules, and is repeated within them',
|
|
|
|
() => {
|
|
|
|
env.write('test.ts', `
|
|
|
|
import {Directive, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: '[test]'})
|
|
|
|
export class TestDir {}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [TestDir, TestDir]
|
|
|
|
})
|
|
|
|
export class ModuleA {}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [TestDir, TestDir],
|
|
|
|
})
|
|
|
|
export class ModuleB {}
|
|
|
|
`);
|
|
|
|
const diags = env.driveDiagnostics();
|
|
|
|
expect(diags.length).toBe(1);
|
|
|
|
const node = findContainingClass(diagnosticToNode(diags[0], ts.isIdentifier));
|
2020-04-07 15:43:43 -04:00
|
|
|
expect(node.name!.text).toEqual('TestDir');
|
2019-12-13 17:29:05 -05:00
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
const relatedNodes = new Set(diags[0].relatedInformation!.map(
|
2019-12-13 17:29:05 -05:00
|
|
|
related =>
|
2020-04-07 15:43:43 -04:00
|
|
|
findContainingClass(diagnosticToNode(related, ts.isIdentifier)).name!.text));
|
2019-12-13 17:29:05 -05:00
|
|
|
expect(relatedNodes).toContain('ModuleA');
|
|
|
|
expect(relatedNodes).toContain('ModuleB');
|
|
|
|
expect(relatedNodes.size).toBe(2);
|
|
|
|
});
|
|
|
|
});
|
2019-06-06 15:22:32 -04:00
|
|
|
describe('imports', () => {
|
|
|
|
it('should emit imports in a pure function call', () => {
|
|
|
|
env.write('test.ts', `
|
2019-03-29 16:31:22 -04:00
|
|
|
import {NgModule} from '@angular/core';
|
2019-04-04 14:41:52 -04:00
|
|
|
|
2019-03-29 16:31:22 -04:00
|
|
|
@NgModule({})
|
|
|
|
export class OtherModule {}
|
2019-04-04 14:41:52 -04:00
|
|
|
|
2019-03-29 16:31:22 -04:00
|
|
|
@NgModule({imports: [OtherModule]})
|
|
|
|
export class TestModule {}
|
|
|
|
`);
|
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
env.driveMain();
|
2019-03-29 16:31:22 -04:00
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
|
|
|
expect(jsContents).toContain('i0.ɵɵdefineNgModule({ type: TestModule });');
|
|
|
|
expect(jsContents)
|
|
|
|
.toContain(
|
fix(ivy): retain JIT metadata unless JIT mode is explicitly disabled (#33671)
NgModules in Ivy have a definition which contains various different bits
of metadata about the module. In particular, this metadata falls into two
categories:
* metadata required to use the module at runtime (for bootstrapping, etc)
in AOT-only applications.
* metadata required to depend on the module from a JIT-compiled app.
The latter metadata consists of the module's declarations, imports, and
exports. To support JIT usage, this metadata must be included in the
generated code, especially if that code is shipped to NPM. However, because
this metadata preserves the entire NgModule graph (references to all
directives and components in the app), it needs to be removed during
optimization for AOT-only builds.
Previously, this was done with a clever design:
1. The extra metadata was added by a function called `setNgModuleScope`.
A call to this function was generated after each NgModule.
2. This function call was marked as "pure" with a comment and used
`noSideEffects` internally, which causes optimizers to remove it.
The effect was that in dev mode or test mode (which use JIT), no optimizer
runs and the full NgModule metadata was available at runtime. But in
production (presumably AOT) builds, the optimizer runs and removes the JIT-
specific metadata.
However, there are cases where apps that want to use JIT in production, and
still make an optimized build. In this case, the JIT-specific metadata would
be erroneously removed. This commit solves that problem by adding an
`ngJitMode` global variable which guards all `setNgModuleScope` calls. An
optimizer can be configured to statically define this global to be `false`
for AOT-only builds, causing the extra metadata to be stripped.
A configuration for Terser used by the CLI is provided in `tooling.ts` which
sets `ngJitMode` to `false` when building AOT apps.
PR Close #33671
2019-11-07 17:18:44 -05:00
|
|
|
'function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(TestModule, { imports: [OtherModule] }); })();');
|
2019-03-29 16:31:22 -04:00
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
const dtsContents = env.getContents('test.d.ts');
|
|
|
|
expect(dtsContents)
|
|
|
|
.toContain(
|
2019-10-14 10:20:26 -04:00
|
|
|
'static ɵmod: i0.ɵɵNgModuleDefWithMeta<TestModule, never, [typeof OtherModule], never>');
|
2019-06-06 15:22:32 -04:00
|
|
|
});
|
2019-03-29 16:31:22 -04:00
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
it('should produce an error when an invalid class is imported', () => {
|
|
|
|
env.write('test.ts', `
|
2019-03-08 14:32:49 -05:00
|
|
|
import {NgModule} from '@angular/core';
|
2019-04-04 14:41:52 -04:00
|
|
|
|
2019-03-08 14:32:49 -05:00
|
|
|
class NotAModule {}
|
2019-04-04 14:41:52 -04:00
|
|
|
|
2019-03-08 14:32:49 -05:00
|
|
|
@NgModule({imports: [NotAModule]})
|
|
|
|
class IsAModule {}
|
|
|
|
`);
|
2019-06-06 15:22:32 -04:00
|
|
|
const [error] = env.driveDiagnostics();
|
|
|
|
expect(error).not.toBeUndefined();
|
|
|
|
expect(error.messageText).toContain('IsAModule');
|
|
|
|
expect(error.messageText).toContain('NgModule.imports');
|
|
|
|
expect(error.code).toEqual(ngErrorCode(ErrorCode.NGMODULE_INVALID_IMPORT));
|
|
|
|
expect(diagnosticToNode(error, ts.isIdentifier).text).toEqual('NotAModule');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should produce an error when a non-class is imported from a .d.ts dependency', () => {
|
|
|
|
env.write('dep.d.ts', `export declare let NotAClass: Function;`);
|
|
|
|
env.write('test.ts', `
|
2019-03-08 14:32:49 -05:00
|
|
|
import {NgModule} from '@angular/core';
|
|
|
|
import {NotAClass} from './dep';
|
2019-04-04 14:41:52 -04:00
|
|
|
|
2019-03-08 14:32:49 -05:00
|
|
|
@NgModule({imports: [NotAClass]})
|
|
|
|
class IsAModule {}
|
|
|
|
`);
|
2019-06-06 15:22:32 -04:00
|
|
|
const [error] = env.driveDiagnostics();
|
|
|
|
expect(error).not.toBeUndefined();
|
|
|
|
expect(error.messageText).toContain('IsAModule');
|
|
|
|
expect(error.messageText).toContain('NgModule.imports');
|
|
|
|
expect(error.code).toEqual(ngErrorCode(ErrorCode.VALUE_HAS_WRONG_TYPE));
|
|
|
|
expect(diagnosticToNode(error, ts.isIdentifier).text).toEqual('NotAClass');
|
|
|
|
});
|
2019-03-08 14:32:49 -05:00
|
|
|
});
|
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
describe('exports', () => {
|
|
|
|
it('should emit exports in a pure function call', () => {
|
|
|
|
env.write('test.ts', `
|
2019-03-29 16:31:22 -04:00
|
|
|
import {NgModule} from '@angular/core';
|
2019-04-04 14:41:52 -04:00
|
|
|
|
2019-03-29 16:31:22 -04:00
|
|
|
@NgModule({})
|
|
|
|
export class OtherModule {}
|
2019-04-04 14:41:52 -04:00
|
|
|
|
2019-03-29 16:31:22 -04:00
|
|
|
@NgModule({exports: [OtherModule]})
|
|
|
|
export class TestModule {}
|
|
|
|
`);
|
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
env.driveMain();
|
2019-03-29 16:31:22 -04:00
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
|
|
|
expect(jsContents).toContain('i0.ɵɵdefineNgModule({ type: TestModule });');
|
|
|
|
expect(jsContents)
|
|
|
|
.toContain(
|
fix(ivy): retain JIT metadata unless JIT mode is explicitly disabled (#33671)
NgModules in Ivy have a definition which contains various different bits
of metadata about the module. In particular, this metadata falls into two
categories:
* metadata required to use the module at runtime (for bootstrapping, etc)
in AOT-only applications.
* metadata required to depend on the module from a JIT-compiled app.
The latter metadata consists of the module's declarations, imports, and
exports. To support JIT usage, this metadata must be included in the
generated code, especially if that code is shipped to NPM. However, because
this metadata preserves the entire NgModule graph (references to all
directives and components in the app), it needs to be removed during
optimization for AOT-only builds.
Previously, this was done with a clever design:
1. The extra metadata was added by a function called `setNgModuleScope`.
A call to this function was generated after each NgModule.
2. This function call was marked as "pure" with a comment and used
`noSideEffects` internally, which causes optimizers to remove it.
The effect was that in dev mode or test mode (which use JIT), no optimizer
runs and the full NgModule metadata was available at runtime. But in
production (presumably AOT) builds, the optimizer runs and removes the JIT-
specific metadata.
However, there are cases where apps that want to use JIT in production, and
still make an optimized build. In this case, the JIT-specific metadata would
be erroneously removed. This commit solves that problem by adding an
`ngJitMode` global variable which guards all `setNgModuleScope` calls. An
optimizer can be configured to statically define this global to be `false`
for AOT-only builds, causing the extra metadata to be stripped.
A configuration for Terser used by the CLI is provided in `tooling.ts` which
sets `ngJitMode` to `false` when building AOT apps.
PR Close #33671
2019-11-07 17:18:44 -05:00
|
|
|
'(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(TestModule, { exports: [OtherModule] }); })();');
|
2019-03-29 16:31:22 -04:00
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
const dtsContents = env.getContents('test.d.ts');
|
|
|
|
expect(dtsContents)
|
|
|
|
.toContain(
|
2019-10-14 10:20:26 -04:00
|
|
|
'static ɵmod: i0.ɵɵNgModuleDefWithMeta<TestModule, never, never, [typeof OtherModule]>');
|
2019-06-06 15:22:32 -04:00
|
|
|
});
|
2019-03-29 16:31:22 -04:00
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
it('should produce an error when a non-NgModule class is exported', () => {
|
|
|
|
env.write('test.ts', `
|
2019-03-08 14:32:49 -05:00
|
|
|
import {NgModule} from '@angular/core';
|
2019-04-04 14:41:52 -04:00
|
|
|
|
2019-03-08 14:32:49 -05:00
|
|
|
class NotAModule {}
|
2019-04-04 14:41:52 -04:00
|
|
|
|
2019-03-08 14:32:49 -05:00
|
|
|
@NgModule({exports: [NotAModule]})
|
|
|
|
class IsAModule {}
|
|
|
|
`);
|
2019-06-06 15:22:32 -04:00
|
|
|
const [error] = env.driveDiagnostics();
|
|
|
|
expect(error).not.toBeUndefined();
|
|
|
|
expect(error.messageText).toContain('IsAModule');
|
|
|
|
expect(error.messageText).toContain('NgModule.exports');
|
|
|
|
expect(error.code).toEqual(ngErrorCode(ErrorCode.NGMODULE_INVALID_EXPORT));
|
|
|
|
expect(diagnosticToNode(error, ts.isIdentifier).text).toEqual('NotAModule');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should produce a transitive error when an invalid NgModule is exported', () => {
|
|
|
|
env.write('test.ts', `
|
2019-03-08 14:32:49 -05:00
|
|
|
import {NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
export class NotAModule {}
|
2019-04-04 14:41:52 -04:00
|
|
|
|
2019-03-08 14:32:49 -05:00
|
|
|
@NgModule({
|
|
|
|
imports: [NotAModule],
|
|
|
|
})
|
|
|
|
class InvalidModule {}
|
2019-04-04 14:41:52 -04:00
|
|
|
|
2019-03-08 14:32:49 -05:00
|
|
|
@NgModule({exports: [InvalidModule]})
|
|
|
|
class IsAModule {}
|
|
|
|
`);
|
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
// Find the diagnostic referencing InvalidModule, which should have come from IsAModule.
|
|
|
|
const error = env.driveDiagnostics().find(
|
|
|
|
error => diagnosticToNode(error, ts.isIdentifier).text === 'InvalidModule');
|
|
|
|
if (error === undefined) {
|
|
|
|
return fail('Expected to find a diagnostic referencing InvalidModule');
|
|
|
|
}
|
|
|
|
expect(error.messageText).toContain('IsAModule');
|
|
|
|
expect(error.messageText).toContain('NgModule.exports');
|
|
|
|
expect(error.code).toEqual(ngErrorCode(ErrorCode.NGMODULE_INVALID_EXPORT));
|
|
|
|
});
|
2019-03-08 14:32:49 -05:00
|
|
|
});
|
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
describe('re-exports', () => {
|
|
|
|
it('should produce an error when a non-declared/imported class is re-exported', () => {
|
|
|
|
env.write('test.ts', `
|
2019-03-08 14:32:49 -05:00
|
|
|
import {Directive, NgModule} from '@angular/core';
|
2019-04-04 14:41:52 -04:00
|
|
|
|
2019-03-08 14:32:49 -05:00
|
|
|
@Directive({selector: 'test'})
|
|
|
|
class Dir {}
|
2019-04-04 14:41:52 -04:00
|
|
|
|
2019-03-08 14:32:49 -05:00
|
|
|
@NgModule({exports: [Dir]})
|
|
|
|
class IsAModule {}
|
|
|
|
`);
|
2019-06-06 15:22:32 -04:00
|
|
|
const [error] = env.driveDiagnostics();
|
|
|
|
expect(error).not.toBeUndefined();
|
|
|
|
expect(error.messageText).toContain('IsAModule');
|
|
|
|
expect(error.messageText).toContain('NgModule.exports');
|
|
|
|
expect(error.code).toEqual(ngErrorCode(ErrorCode.NGMODULE_INVALID_REEXPORT));
|
|
|
|
expect(diagnosticToNode(error, ts.isIdentifier).text).toEqual('Dir');
|
|
|
|
});
|
2019-03-08 14:32:49 -05:00
|
|
|
});
|
2019-12-17 14:53:55 -05:00
|
|
|
|
|
|
|
it('should not produce component template type-check errors if its module is invalid', () => {
|
|
|
|
env.tsconfig({'fullTemplateTypeCheck': true});
|
|
|
|
|
|
|
|
// Set up 3 files, each of which declare an NgModule that's invalid in some way. This will
|
|
|
|
// produce a bunch of diagnostics related to the issues with the modules. Each module also
|
|
|
|
// declares a component with a template that references a <doesnt-exist> element. This test
|
|
|
|
// verifies that none of the produced diagnostics mention this nonexistent element, since
|
|
|
|
// no template type-checking should be performed for a component that's part of an invalid
|
|
|
|
// NgModule.
|
|
|
|
|
|
|
|
// This NgModule declares something which isn't a directive/pipe.
|
|
|
|
env.write('invalid-declaration.ts', `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test-cmp',
|
|
|
|
template: '<doesnt-exist></doesnt-exist>',
|
|
|
|
})
|
|
|
|
export class TestCmp {}
|
|
|
|
|
|
|
|
export class NotACmp {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [TestCmp, NotACmp]})
|
|
|
|
export class Module {}
|
|
|
|
`);
|
|
|
|
|
|
|
|
// This NgModule imports something which isn't an NgModule.
|
|
|
|
env.write('invalid-import.ts', `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test-cmp',
|
|
|
|
template: '<doesnt-exist></doesnt-exist>',
|
|
|
|
})
|
|
|
|
export class TestCmp {}
|
|
|
|
|
|
|
|
export class NotAModule {}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [TestCmp],
|
|
|
|
imports: [NotAModule],
|
|
|
|
})
|
|
|
|
export class Module {}
|
|
|
|
`);
|
|
|
|
|
|
|
|
// This NgModule imports a DepModule which itself is invalid (it declares something which
|
|
|
|
// isn't a directive/pipe).
|
|
|
|
env.write('transitive-error-in-import.ts', `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test-cmp',
|
|
|
|
template: '<doesnt-exist></doesnt-exist>',
|
|
|
|
})
|
|
|
|
export class TestCmp {}
|
|
|
|
|
|
|
|
export class NotACmp {}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [NotACmp],
|
|
|
|
exports: [NotACmp],
|
|
|
|
})
|
|
|
|
export class DepModule {}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [TestCmp],
|
|
|
|
imports: [DepModule],
|
|
|
|
})
|
|
|
|
export class Module {}
|
|
|
|
`);
|
|
|
|
|
|
|
|
for (const diag of env.driveDiagnostics()) {
|
|
|
|
// None of the diagnostics should be related to the fact that the component uses an
|
|
|
|
// unknown element, because in all cases the component's scope was invalid.
|
|
|
|
expect(diag.messageText)
|
|
|
|
.not.toContain(
|
|
|
|
'doesnt-exist',
|
|
|
|
'Template type-checking ran for a component, when it shouldn\'t have.');
|
|
|
|
}
|
|
|
|
});
|
2019-03-08 14:32:49 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
function diagnosticToNode<T extends ts.Node>(
|
2020-04-07 15:43:43 -04:00
|
|
|
diagnostic: ts.Diagnostic|Diagnostic|ts.DiagnosticRelatedInformation,
|
2019-12-13 17:29:05 -05:00
|
|
|
guard: (node: ts.Node) => node is T): T {
|
|
|
|
const diag = diagnostic as ts.Diagnostic | ts.DiagnosticRelatedInformation;
|
2019-06-06 15:22:32 -04:00
|
|
|
if (diag.file === undefined) {
|
|
|
|
throw new Error(`Expected ts.Diagnostic to have a file source`);
|
|
|
|
}
|
2020-04-07 15:43:43 -04:00
|
|
|
const node = getTokenAtPosition(diag.file, diag.start!);
|
2019-06-06 15:22:32 -04:00
|
|
|
expect(guard(node)).toBe(true);
|
|
|
|
return node as T;
|
2019-03-08 14:32:49 -05:00
|
|
|
}
|
2019-06-06 15:22:32 -04:00
|
|
|
});
|
2019-12-13 17:29:05 -05:00
|
|
|
|
|
|
|
function findContainingClass(node: ts.Node): ts.ClassDeclaration {
|
|
|
|
while (!ts.isClassDeclaration(node)) {
|
|
|
|
if (node.parent && node.parent !== node) {
|
|
|
|
node = node.parent;
|
|
|
|
} else {
|
|
|
|
throw new Error('Expected node to have a ClassDeclaration parent');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|