2018-04-06 12:53:10 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
import {NgtscTestEnvironment} from './env';
|
2018-04-06 12:53:10 -04:00
|
|
|
|
2018-11-20 18:20:19 -05:00
|
|
|
const trim = (input: string): string => input.replace(/\s+/g, ' ').trim();
|
|
|
|
|
2018-04-06 12:53:10 -04:00
|
|
|
describe('ngtsc behavioral tests', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
if (!NgtscTestEnvironment.supported) {
|
2018-04-06 12:53:10 -04:00
|
|
|
// These tests should be excluded from the non-Bazel build.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
let env !: NgtscTestEnvironment;
|
2018-04-06 12:53:10 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
beforeEach(() => { env = NgtscTestEnvironment.setup(); });
|
2018-04-06 12:53:10 -04:00
|
|
|
|
2018-05-31 18:50:02 -04:00
|
|
|
it('should compile Injectables without errors', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
2018-04-06 12:53:10 -04:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class Dep {}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class Service {
|
|
|
|
constructor(dep: Dep) {}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
2018-04-06 12:53:10 -04:00
|
|
|
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
2018-04-06 12:53:10 -04:00
|
|
|
expect(jsContents).toContain('Dep.ngInjectableDef =');
|
|
|
|
expect(jsContents).toContain('Service.ngInjectableDef =');
|
|
|
|
expect(jsContents).not.toContain('__decorate');
|
2018-09-25 18:35:03 -04:00
|
|
|
const dtsContents = env.getContents('test.d.ts');
|
2018-07-13 17:32:46 -04:00
|
|
|
expect(dtsContents).toContain('static ngInjectableDef: i0.ɵInjectableDef<Dep>;');
|
|
|
|
expect(dtsContents).toContain('static ngInjectableDef: i0.ɵInjectableDef<Service>;');
|
2018-04-06 12:53:10 -04:00
|
|
|
});
|
2018-05-31 18:50:02 -04:00
|
|
|
|
|
|
|
it('should compile Components without errors', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
2018-05-31 18:50:02 -04:00
|
|
|
import {Component} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test-cmp',
|
|
|
|
template: 'this is a test',
|
|
|
|
})
|
|
|
|
export class TestCmp {}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
2018-05-31 18:50:02 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
2018-05-31 18:50:02 -04:00
|
|
|
expect(jsContents).toContain('TestCmp.ngComponentDef = i0.ɵdefineComponent');
|
|
|
|
expect(jsContents).not.toContain('__decorate');
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const dtsContents = env.getContents('test.d.ts');
|
2018-09-21 15:12:06 -04:00
|
|
|
expect(dtsContents)
|
|
|
|
.toContain(
|
|
|
|
'static ngComponentDef: i0.ɵComponentDefWithMeta<TestCmp, \'test-cmp\', never, {}, {}, never>');
|
2018-05-31 18:50:02 -04:00
|
|
|
});
|
|
|
|
|
2018-06-26 18:01:09 -04:00
|
|
|
it('should compile Components without errors', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
2018-06-26 18:01:09 -04:00
|
|
|
import {Component} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test-cmp',
|
|
|
|
templateUrl: './dir/test.html',
|
|
|
|
})
|
|
|
|
export class TestCmp {}
|
|
|
|
`);
|
2018-09-25 18:35:03 -04:00
|
|
|
env.write('dir/test.html', '<p>Hello World</p>');
|
2018-06-26 18:01:09 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
2018-06-26 18:01:09 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
2018-06-26 18:01:09 -04:00
|
|
|
expect(jsContents).toContain('Hello World');
|
|
|
|
});
|
|
|
|
|
2018-11-29 17:17:51 -05:00
|
|
|
it('should compile components with styleUrls', () => {
|
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
|
|
|
import {Component} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test-cmp',
|
|
|
|
styleUrls: ['./dir/style.css'],
|
|
|
|
template: '',
|
|
|
|
})
|
|
|
|
export class TestCmp {}
|
|
|
|
`);
|
|
|
|
env.write('dir/style.css', ':host { background-color: blue; }');
|
|
|
|
|
|
|
|
env.driveMain();
|
|
|
|
|
|
|
|
const jsContents = env.getContents('test.js');
|
|
|
|
expect(jsContents).toContain('background-color: blue');
|
|
|
|
});
|
|
|
|
|
2018-05-31 18:50:02 -04:00
|
|
|
it('should compile NgModules without errors', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
2018-05-31 18:50:02 -04:00
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test-cmp',
|
|
|
|
template: 'this is a test',
|
|
|
|
})
|
|
|
|
export class TestCmp {}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [TestCmp],
|
2018-08-28 17:19:33 -04:00
|
|
|
bootstrap: [TestCmp],
|
2018-05-31 18:50:02 -04:00
|
|
|
})
|
|
|
|
export class TestModule {}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
2018-05-31 18:50:02 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
2018-05-31 18:50:02 -04:00
|
|
|
expect(jsContents)
|
|
|
|
.toContain(
|
2018-08-28 17:19:33 -04:00
|
|
|
'i0.ɵdefineNgModule({ type: TestModule, bootstrap: [TestCmp], ' +
|
2018-05-31 18:50:02 -04:00
|
|
|
'declarations: [TestCmp], imports: [], exports: [] })');
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const dtsContents = env.getContents('test.d.ts');
|
2018-05-31 18:50:02 -04:00
|
|
|
expect(dtsContents)
|
fix(ivy): use 'typeof' and 'never' for type metadata (#24862)
Previously ngtsc would use a tuple of class types for listing metadata
in .d.ts files. For example, an @NgModule's declarations might be
represented with the type:
[NgIf, NgForOf, NgClass]
If the module had no declarations, an empty tuple [] would be produced.
This has two problems.
1. If the class type has generic type parameters, TypeScript will
complain that they're not provided.
2. The empty tuple type is not actually legal.
This commit addresses both problems.
1. Class types are now represented using the `typeof` operator, so the
above declarations would be represented as:
[typeof NgIf, typeof NgForOf, typeof NgClass].
Since typeof operates on a value, it doesn't require generic type
arguments.
2. Instead of an empty tuple, `never` is used to indicate no metadata.
PR Close #24862
2018-07-17 16:34:20 -04:00
|
|
|
.toContain(
|
2018-09-21 15:12:06 -04:00
|
|
|
'static ngComponentDef: i0.ɵComponentDefWithMeta<TestCmp, \'test-cmp\', never, {}, {}, never>');
|
|
|
|
expect(dtsContents)
|
|
|
|
.toContain(
|
|
|
|
'static ngModuleDef: i0.ɵNgModuleDefWithMeta<TestModule, [typeof TestCmp], never, never>');
|
2018-05-31 18:50:02 -04:00
|
|
|
expect(dtsContents).not.toContain('__decorate');
|
|
|
|
});
|
2018-06-18 19:28:02 -04:00
|
|
|
|
|
|
|
it('should compile NgModules with services without errors', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
2018-06-18 19:28:02 -04:00
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
export class Token {}
|
|
|
|
|
|
|
|
@NgModule({})
|
|
|
|
export class OtherModule {}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test-cmp',
|
|
|
|
template: 'this is a test',
|
|
|
|
})
|
|
|
|
export class TestCmp {}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [TestCmp],
|
|
|
|
providers: [{provide: Token, useValue: 'test'}],
|
|
|
|
imports: [OtherModule],
|
|
|
|
})
|
|
|
|
export class TestModule {}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
2018-06-18 19:28:02 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
2018-06-18 19:28:02 -04:00
|
|
|
expect(jsContents).toContain('i0.ɵdefineNgModule({ type: TestModule,');
|
|
|
|
expect(jsContents)
|
|
|
|
.toContain(
|
|
|
|
`TestModule.ngInjectorDef = i0.defineInjector({ factory: ` +
|
2018-07-16 19:36:31 -04:00
|
|
|
`function TestModule_Factory(t) { return new (t || TestModule)(); }, providers: [{ provide: ` +
|
2018-07-10 12:59:29 -04:00
|
|
|
`Token, useValue: 'test' }], imports: [[OtherModule]] });`);
|
2018-06-18 19:28:02 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const dtsContents = env.getContents('test.d.ts');
|
2018-06-18 19:28:02 -04:00
|
|
|
expect(dtsContents)
|
fix(ivy): use 'typeof' and 'never' for type metadata (#24862)
Previously ngtsc would use a tuple of class types for listing metadata
in .d.ts files. For example, an @NgModule's declarations might be
represented with the type:
[NgIf, NgForOf, NgClass]
If the module had no declarations, an empty tuple [] would be produced.
This has two problems.
1. If the class type has generic type parameters, TypeScript will
complain that they're not provided.
2. The empty tuple type is not actually legal.
This commit addresses both problems.
1. Class types are now represented using the `typeof` operator, so the
above declarations would be represented as:
[typeof NgIf, typeof NgForOf, typeof NgClass].
Since typeof operates on a value, it doesn't require generic type
arguments.
2. Instead of an empty tuple, `never` is used to indicate no metadata.
PR Close #24862
2018-07-17 16:34:20 -04:00
|
|
|
.toContain(
|
2018-09-21 15:12:06 -04:00
|
|
|
'static ngModuleDef: i0.ɵNgModuleDefWithMeta<TestModule, [typeof TestCmp], [typeof OtherModule], never>');
|
2018-06-29 17:17:42 -04:00
|
|
|
expect(dtsContents).toContain('static ngInjectorDef: i0.ɵInjectorDef');
|
2018-06-18 19:28:02 -04:00
|
|
|
});
|
2018-06-26 13:44:22 -04:00
|
|
|
|
fix(ivy): force new imports for .d.ts files (#25080)
When ngtsc encounters a reference to a type (for example, a Component
type listed in an NgModule declarations array), it traces the import
of that type and attempts to determine the best way to refer to it.
In the event the type is defined in the same file where a reference
is being generated, the identifier of the type is used. If the type
was imported, ngtsc has a choice. It can use the identifier from the
original import, or it can write a new import to the module where the
type came from.
ngtsc has a bug currently when it elects to rely on the user's import.
When writing a .d.ts file, the user's import may have been elided as
the type was not referred to from the type side of the program. Thus,
in .d.ts files ngtsc must always assume the import may not exist, and
generate a new one.
In .js output the import is guaranteed to still exist, so it's
preferable for ngtsc to continue using the existing import if one is
available.
This commit changes how @angular/compiler writes type definitions, and
allows it to use a different expression to write a type definition than
is used to write the value. This allows ngtsc to specify that types in
type definitions should always be imported. A corresponding change to
the staticallyResolve() Reference system allows the choice of which
type of import to use when generating an Expression from a Reference.
PR Close #25080
2018-07-24 19:10:15 -04:00
|
|
|
it('should compile NgModules with references to local components', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
fix(ivy): force new imports for .d.ts files (#25080)
When ngtsc encounters a reference to a type (for example, a Component
type listed in an NgModule declarations array), it traces the import
of that type and attempts to determine the best way to refer to it.
In the event the type is defined in the same file where a reference
is being generated, the identifier of the type is used. If the type
was imported, ngtsc has a choice. It can use the identifier from the
original import, or it can write a new import to the module where the
type came from.
ngtsc has a bug currently when it elects to rely on the user's import.
When writing a .d.ts file, the user's import may have been elided as
the type was not referred to from the type side of the program. Thus,
in .d.ts files ngtsc must always assume the import may not exist, and
generate a new one.
In .js output the import is guaranteed to still exist, so it's
preferable for ngtsc to continue using the existing import if one is
available.
This commit changes how @angular/compiler writes type definitions, and
allows it to use a different expression to write a type definition than
is used to write the value. This allows ngtsc to specify that types in
type definitions should always be imported. A corresponding change to
the staticallyResolve() Reference system allows the choice of which
type of import to use when generating an Expression from a Reference.
PR Close #25080
2018-07-24 19:10:15 -04:00
|
|
|
import {NgModule} from '@angular/core';
|
|
|
|
import {Foo} from './foo';
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [Foo],
|
|
|
|
})
|
|
|
|
export class FooModule {}
|
|
|
|
`);
|
2018-09-25 18:35:03 -04:00
|
|
|
env.write('foo.ts', `
|
fix(ivy): force new imports for .d.ts files (#25080)
When ngtsc encounters a reference to a type (for example, a Component
type listed in an NgModule declarations array), it traces the import
of that type and attempts to determine the best way to refer to it.
In the event the type is defined in the same file where a reference
is being generated, the identifier of the type is used. If the type
was imported, ngtsc has a choice. It can use the identifier from the
original import, or it can write a new import to the module where the
type came from.
ngtsc has a bug currently when it elects to rely on the user's import.
When writing a .d.ts file, the user's import may have been elided as
the type was not referred to from the type side of the program. Thus,
in .d.ts files ngtsc must always assume the import may not exist, and
generate a new one.
In .js output the import is guaranteed to still exist, so it's
preferable for ngtsc to continue using the existing import if one is
available.
This commit changes how @angular/compiler writes type definitions, and
allows it to use a different expression to write a type definition than
is used to write the value. This allows ngtsc to specify that types in
type definitions should always be imported. A corresponding change to
the staticallyResolve() Reference system allows the choice of which
type of import to use when generating an Expression from a Reference.
PR Close #25080
2018-07-24 19:10:15 -04:00
|
|
|
import {Component} from '@angular/core';
|
|
|
|
@Component({selector: 'foo', template: ''})
|
|
|
|
export class Foo {}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
fix(ivy): force new imports for .d.ts files (#25080)
When ngtsc encounters a reference to a type (for example, a Component
type listed in an NgModule declarations array), it traces the import
of that type and attempts to determine the best way to refer to it.
In the event the type is defined in the same file where a reference
is being generated, the identifier of the type is used. If the type
was imported, ngtsc has a choice. It can use the identifier from the
original import, or it can write a new import to the module where the
type came from.
ngtsc has a bug currently when it elects to rely on the user's import.
When writing a .d.ts file, the user's import may have been elided as
the type was not referred to from the type side of the program. Thus,
in .d.ts files ngtsc must always assume the import may not exist, and
generate a new one.
In .js output the import is guaranteed to still exist, so it's
preferable for ngtsc to continue using the existing import if one is
available.
This commit changes how @angular/compiler writes type definitions, and
allows it to use a different expression to write a type definition than
is used to write the value. This allows ngtsc to specify that types in
type definitions should always be imported. A corresponding change to
the staticallyResolve() Reference system allows the choice of which
type of import to use when generating an Expression from a Reference.
PR Close #25080
2018-07-24 19:10:15 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
|
|
|
const dtsContents = env.getContents('test.d.ts');
|
fix(ivy): force new imports for .d.ts files (#25080)
When ngtsc encounters a reference to a type (for example, a Component
type listed in an NgModule declarations array), it traces the import
of that type and attempts to determine the best way to refer to it.
In the event the type is defined in the same file where a reference
is being generated, the identifier of the type is used. If the type
was imported, ngtsc has a choice. It can use the identifier from the
original import, or it can write a new import to the module where the
type came from.
ngtsc has a bug currently when it elects to rely on the user's import.
When writing a .d.ts file, the user's import may have been elided as
the type was not referred to from the type side of the program. Thus,
in .d.ts files ngtsc must always assume the import may not exist, and
generate a new one.
In .js output the import is guaranteed to still exist, so it's
preferable for ngtsc to continue using the existing import if one is
available.
This commit changes how @angular/compiler writes type definitions, and
allows it to use a different expression to write a type definition than
is used to write the value. This allows ngtsc to specify that types in
type definitions should always be imported. A corresponding change to
the staticallyResolve() Reference system allows the choice of which
type of import to use when generating an Expression from a Reference.
PR Close #25080
2018-07-24 19:10:15 -04:00
|
|
|
|
|
|
|
expect(jsContents).toContain('import { Foo } from \'./foo\';');
|
|
|
|
expect(jsContents).not.toMatch(/as i[0-9] from '.\/foo'/);
|
|
|
|
expect(dtsContents).toContain('as i1 from \'./foo\';');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should compile NgModules with references to absolute components', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
fix(ivy): force new imports for .d.ts files (#25080)
When ngtsc encounters a reference to a type (for example, a Component
type listed in an NgModule declarations array), it traces the import
of that type and attempts to determine the best way to refer to it.
In the event the type is defined in the same file where a reference
is being generated, the identifier of the type is used. If the type
was imported, ngtsc has a choice. It can use the identifier from the
original import, or it can write a new import to the module where the
type came from.
ngtsc has a bug currently when it elects to rely on the user's import.
When writing a .d.ts file, the user's import may have been elided as
the type was not referred to from the type side of the program. Thus,
in .d.ts files ngtsc must always assume the import may not exist, and
generate a new one.
In .js output the import is guaranteed to still exist, so it's
preferable for ngtsc to continue using the existing import if one is
available.
This commit changes how @angular/compiler writes type definitions, and
allows it to use a different expression to write a type definition than
is used to write the value. This allows ngtsc to specify that types in
type definitions should always be imported. A corresponding change to
the staticallyResolve() Reference system allows the choice of which
type of import to use when generating an Expression from a Reference.
PR Close #25080
2018-07-24 19:10:15 -04:00
|
|
|
import {NgModule} from '@angular/core';
|
|
|
|
import {Foo} from 'foo';
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [Foo],
|
|
|
|
})
|
|
|
|
export class FooModule {}
|
|
|
|
`);
|
2018-09-25 18:35:03 -04:00
|
|
|
env.write('node_modules/foo/index.d.ts', `
|
fix(ivy): force new imports for .d.ts files (#25080)
When ngtsc encounters a reference to a type (for example, a Component
type listed in an NgModule declarations array), it traces the import
of that type and attempts to determine the best way to refer to it.
In the event the type is defined in the same file where a reference
is being generated, the identifier of the type is used. If the type
was imported, ngtsc has a choice. It can use the identifier from the
original import, or it can write a new import to the module where the
type came from.
ngtsc has a bug currently when it elects to rely on the user's import.
When writing a .d.ts file, the user's import may have been elided as
the type was not referred to from the type side of the program. Thus,
in .d.ts files ngtsc must always assume the import may not exist, and
generate a new one.
In .js output the import is guaranteed to still exist, so it's
preferable for ngtsc to continue using the existing import if one is
available.
This commit changes how @angular/compiler writes type definitions, and
allows it to use a different expression to write a type definition than
is used to write the value. This allows ngtsc to specify that types in
type definitions should always be imported. A corresponding change to
the staticallyResolve() Reference system allows the choice of which
type of import to use when generating an Expression from a Reference.
PR Close #25080
2018-07-24 19:10:15 -04:00
|
|
|
import * as i0 from '@angular/core';
|
|
|
|
export class Foo {
|
|
|
|
static ngComponentDef: i0.ɵComponentDef<Foo, 'foo'>;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
fix(ivy): force new imports for .d.ts files (#25080)
When ngtsc encounters a reference to a type (for example, a Component
type listed in an NgModule declarations array), it traces the import
of that type and attempts to determine the best way to refer to it.
In the event the type is defined in the same file where a reference
is being generated, the identifier of the type is used. If the type
was imported, ngtsc has a choice. It can use the identifier from the
original import, or it can write a new import to the module where the
type came from.
ngtsc has a bug currently when it elects to rely on the user's import.
When writing a .d.ts file, the user's import may have been elided as
the type was not referred to from the type side of the program. Thus,
in .d.ts files ngtsc must always assume the import may not exist, and
generate a new one.
In .js output the import is guaranteed to still exist, so it's
preferable for ngtsc to continue using the existing import if one is
available.
This commit changes how @angular/compiler writes type definitions, and
allows it to use a different expression to write a type definition than
is used to write the value. This allows ngtsc to specify that types in
type definitions should always be imported. A corresponding change to
the staticallyResolve() Reference system allows the choice of which
type of import to use when generating an Expression from a Reference.
PR Close #25080
2018-07-24 19:10:15 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
|
|
|
const dtsContents = env.getContents('test.d.ts');
|
fix(ivy): force new imports for .d.ts files (#25080)
When ngtsc encounters a reference to a type (for example, a Component
type listed in an NgModule declarations array), it traces the import
of that type and attempts to determine the best way to refer to it.
In the event the type is defined in the same file where a reference
is being generated, the identifier of the type is used. If the type
was imported, ngtsc has a choice. It can use the identifier from the
original import, or it can write a new import to the module where the
type came from.
ngtsc has a bug currently when it elects to rely on the user's import.
When writing a .d.ts file, the user's import may have been elided as
the type was not referred to from the type side of the program. Thus,
in .d.ts files ngtsc must always assume the import may not exist, and
generate a new one.
In .js output the import is guaranteed to still exist, so it's
preferable for ngtsc to continue using the existing import if one is
available.
This commit changes how @angular/compiler writes type definitions, and
allows it to use a different expression to write a type definition than
is used to write the value. This allows ngtsc to specify that types in
type definitions should always be imported. A corresponding change to
the staticallyResolve() Reference system allows the choice of which
type of import to use when generating an Expression from a Reference.
PR Close #25080
2018-07-24 19:10:15 -04:00
|
|
|
|
|
|
|
expect(jsContents).toContain('import { Foo } from \'foo\';');
|
|
|
|
expect(jsContents).not.toMatch(/as i[0-9] from 'foo'/);
|
|
|
|
expect(dtsContents).toContain('as i1 from \'foo\';');
|
|
|
|
});
|
|
|
|
|
2018-06-26 13:44:22 -04:00
|
|
|
it('should compile Pipes without errors', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
2018-06-26 13:44:22 -04:00
|
|
|
import {Pipe} from '@angular/core';
|
|
|
|
|
|
|
|
@Pipe({
|
|
|
|
name: 'test-pipe',
|
|
|
|
pure: false,
|
|
|
|
})
|
|
|
|
export class TestPipe {}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
2018-06-26 13:44:22 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
|
|
|
const dtsContents = env.getContents('test.d.ts');
|
2018-06-26 13:44:22 -04:00
|
|
|
|
|
|
|
expect(jsContents)
|
|
|
|
.toContain(
|
|
|
|
'TestPipe.ngPipeDef = i0.ɵdefinePipe({ name: "test-pipe", type: TestPipe, ' +
|
2018-07-16 19:36:31 -04:00
|
|
|
'factory: function TestPipe_Factory(t) { return new (t || TestPipe)(); }, pure: false })');
|
2018-09-21 15:12:06 -04:00
|
|
|
expect(dtsContents)
|
|
|
|
.toContain('static ngPipeDef: i0.ɵPipeDefWithMeta<TestPipe, \'test-pipe\'>;');
|
2018-06-26 13:44:22 -04:00
|
|
|
});
|
|
|
|
|
2018-07-03 19:13:54 -04:00
|
|
|
it('should compile pure Pipes without errors', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
2018-07-03 19:13:54 -04:00
|
|
|
import {Pipe} from '@angular/core';
|
|
|
|
|
|
|
|
@Pipe({
|
|
|
|
name: 'test-pipe',
|
|
|
|
})
|
|
|
|
export class TestPipe {}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
2018-07-03 19:13:54 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
|
|
|
const dtsContents = env.getContents('test.d.ts');
|
2018-07-03 19:13:54 -04:00
|
|
|
|
|
|
|
expect(jsContents)
|
|
|
|
.toContain(
|
|
|
|
'TestPipe.ngPipeDef = i0.ɵdefinePipe({ name: "test-pipe", type: TestPipe, ' +
|
2018-07-16 19:36:31 -04:00
|
|
|
'factory: function TestPipe_Factory(t) { return new (t || TestPipe)(); }, pure: true })');
|
2018-09-21 15:12:06 -04:00
|
|
|
expect(dtsContents)
|
|
|
|
.toContain('static ngPipeDef: i0.ɵPipeDefWithMeta<TestPipe, \'test-pipe\'>;');
|
2018-07-03 19:13:54 -04:00
|
|
|
});
|
|
|
|
|
2018-06-26 13:44:22 -04:00
|
|
|
it('should compile Pipes with dependencies', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
2018-06-26 13:44:22 -04:00
|
|
|
import {Pipe} from '@angular/core';
|
|
|
|
|
|
|
|
export class Dep {}
|
|
|
|
|
|
|
|
@Pipe({
|
|
|
|
name: 'test-pipe',
|
|
|
|
pure: false,
|
|
|
|
})
|
|
|
|
export class TestPipe {
|
|
|
|
constructor(dep: Dep) {}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
2018-06-26 13:44:22 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
2018-07-16 19:36:31 -04:00
|
|
|
expect(jsContents).toContain('return new (t || TestPipe)(i0.ɵdirectiveInject(Dep));');
|
2018-06-26 13:44:22 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should include @Pipes in @NgModule scopes', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
2018-06-26 13:44:22 -04:00
|
|
|
import {Component, NgModule, Pipe} from '@angular/core';
|
|
|
|
|
|
|
|
@Pipe({name: 'test'})
|
|
|
|
export class TestPipe {}
|
|
|
|
|
|
|
|
@Component({selector: 'test-cmp', template: '{{value | test}}'})
|
|
|
|
export class TestCmp {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [TestPipe, TestCmp]})
|
|
|
|
export class TestModule {}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
2018-06-26 13:44:22 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
2018-06-26 13:44:22 -04:00
|
|
|
expect(jsContents).toContain('pipes: [TestPipe]');
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const dtsContents = env.getContents('test.d.ts');
|
fix(ivy): use 'typeof' and 'never' for type metadata (#24862)
Previously ngtsc would use a tuple of class types for listing metadata
in .d.ts files. For example, an @NgModule's declarations might be
represented with the type:
[NgIf, NgForOf, NgClass]
If the module had no declarations, an empty tuple [] would be produced.
This has two problems.
1. If the class type has generic type parameters, TypeScript will
complain that they're not provided.
2. The empty tuple type is not actually legal.
This commit addresses both problems.
1. Class types are now represented using the `typeof` operator, so the
above declarations would be represented as:
[typeof NgIf, typeof NgForOf, typeof NgClass].
Since typeof operates on a value, it doesn't require generic type
arguments.
2. Instead of an empty tuple, `never` is used to indicate no metadata.
PR Close #24862
2018-07-17 16:34:20 -04:00
|
|
|
expect(dtsContents)
|
2018-09-21 15:12:06 -04:00
|
|
|
.toContain(
|
|
|
|
'i0.ɵNgModuleDefWithMeta<TestModule, [typeof TestPipe, typeof TestCmp], never, never>');
|
2018-06-26 13:44:22 -04:00
|
|
|
});
|
2018-07-09 14:36:30 -04:00
|
|
|
|
2018-07-09 14:39:15 -04:00
|
|
|
it('should unwrap a ModuleWithProviders function if a generic type is provided for it', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write(`test.ts`, `
|
2018-07-09 14:36:30 -04:00
|
|
|
import {NgModule} from '@angular/core';
|
|
|
|
import {RouterModule} from 'router';
|
|
|
|
|
|
|
|
@NgModule({imports: [RouterModule.forRoot()]})
|
|
|
|
export class TestModule {}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.write('node_modules/router/index.d.ts', `
|
2018-07-09 14:36:30 -04:00
|
|
|
import {ModuleWithProviders} from '@angular/core';
|
|
|
|
|
|
|
|
declare class RouterModule {
|
|
|
|
static forRoot(): ModuleWithProviders<RouterModule>;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
2018-07-10 12:59:29 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
2018-07-10 12:59:29 -04:00
|
|
|
expect(jsContents).toContain('imports: [[RouterModule.forRoot()]]');
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const dtsContents = env.getContents('test.d.ts');
|
2018-07-09 14:36:30 -04:00
|
|
|
expect(dtsContents).toContain(`import * as i1 from 'router';`);
|
fix(ivy): use 'typeof' and 'never' for type metadata (#24862)
Previously ngtsc would use a tuple of class types for listing metadata
in .d.ts files. For example, an @NgModule's declarations might be
represented with the type:
[NgIf, NgForOf, NgClass]
If the module had no declarations, an empty tuple [] would be produced.
This has two problems.
1. If the class type has generic type parameters, TypeScript will
complain that they're not provided.
2. The empty tuple type is not actually legal.
This commit addresses both problems.
1. Class types are now represented using the `typeof` operator, so the
above declarations would be represented as:
[typeof NgIf, typeof NgForOf, typeof NgClass].
Since typeof operates on a value, it doesn't require generic type
arguments.
2. Instead of an empty tuple, `never` is used to indicate no metadata.
PR Close #24862
2018-07-17 16:34:20 -04:00
|
|
|
expect(dtsContents)
|
2018-09-21 15:12:06 -04:00
|
|
|
.toContain('i0.ɵNgModuleDefWithMeta<TestModule, never, [typeof i1.RouterModule], never>');
|
2018-07-09 14:36:30 -04:00
|
|
|
});
|
2018-07-10 12:57:48 -04:00
|
|
|
|
|
|
|
it('should inject special types according to the metadata', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write(`test.ts`, `
|
2018-07-10 12:57:48 -04:00
|
|
|
import {
|
|
|
|
Attribute,
|
|
|
|
ChangeDetectorRef,
|
|
|
|
Component,
|
|
|
|
ElementRef,
|
|
|
|
Injector,
|
2018-08-16 11:43:29 -04:00
|
|
|
Renderer2,
|
2018-07-10 12:57:48 -04:00
|
|
|
TemplateRef,
|
|
|
|
ViewContainerRef,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test',
|
|
|
|
template: 'Test',
|
|
|
|
})
|
|
|
|
class FooCmp {
|
|
|
|
constructor(
|
|
|
|
@Attribute("test") attr: string,
|
|
|
|
cdr: ChangeDetectorRef,
|
|
|
|
er: ElementRef,
|
|
|
|
i: Injector,
|
2018-08-16 11:43:29 -04:00
|
|
|
r2: Renderer2,
|
2018-07-10 12:57:48 -04:00
|
|
|
tr: TemplateRef,
|
|
|
|
vcr: ViewContainerRef,
|
|
|
|
) {}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
2018-07-10 12:57:48 -04:00
|
|
|
expect(jsContents)
|
|
|
|
.toContain(
|
2018-10-23 17:28:15 -04:00
|
|
|
`factory: function FooCmp_Factory(t) { return new (t || FooCmp)(i0.ɵinjectAttribute("test"), i0.ɵdirectiveInject(ChangeDetectorRef), i0.ɵdirectiveInject(ElementRef), i0.ɵdirectiveInject(Injector), i0.ɵdirectiveInject(Renderer2), i0.ɵdirectiveInject(TemplateRef), i0.ɵdirectiveInject(ViewContainerRef)); }`);
|
2018-07-10 12:57:48 -04:00
|
|
|
});
|
2018-07-18 12:32:36 -04:00
|
|
|
|
|
|
|
it('should generate queries for components', () => {
|
2018-10-03 16:49:24 -04:00
|
|
|
|
|
|
|
// Helper functions to construct RegExps for output validation
|
|
|
|
const varRegExp = (name: string): RegExp => new RegExp(`var \\w+ = \\[\"${name}\"\\];`);
|
|
|
|
const queryRegExp = (id: number | null, descend: boolean, ref?: string): RegExp => {
|
|
|
|
const maybeRef = ref ? `, ${ref}` : ``;
|
|
|
|
return new RegExp(`i0\\.ɵquery\\(${id}, \\w+, ${descend}${maybeRef}\\)`);
|
|
|
|
};
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write(`test.ts`, `
|
2018-07-18 12:32:36 -04:00
|
|
|
import {Component, ContentChild, ContentChildren, TemplateRef, ViewChild} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test',
|
|
|
|
template: '<div #foo></div>',
|
|
|
|
queries: {
|
|
|
|
'mview': new ViewChild('test1'),
|
|
|
|
'mcontent': new ContentChild('test2'),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
class FooCmp {
|
|
|
|
@ContentChild('bar', {read: TemplateRef}) child: any;
|
|
|
|
@ContentChildren(TemplateRef) children: any;
|
|
|
|
get aview(): any { return null; }
|
|
|
|
@ViewChild('accessor') set aview(value: any) {}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
2018-10-03 16:49:24 -04:00
|
|
|
expect(jsContents).toMatch(varRegExp('bar'));
|
|
|
|
expect(jsContents).toMatch(varRegExp('test1'));
|
|
|
|
expect(jsContents).toMatch(varRegExp('test2'));
|
|
|
|
expect(jsContents).toMatch(varRegExp('accessor'));
|
2018-08-14 19:48:58 -04:00
|
|
|
expect(jsContents).toContain(`i0.ɵquery(null, TemplateRef, false)`);
|
2018-10-03 16:49:24 -04:00
|
|
|
expect(jsContents)
|
|
|
|
.toMatch(queryRegExp(
|
|
|
|
null, true, 'TemplateRef')); // match `i0.ɵquery(null, _c0, true, TemplateRef)`
|
|
|
|
expect(jsContents).toMatch(queryRegExp(null, true)); // match `i0.ɵquery(null, _c0, true)`
|
|
|
|
expect(jsContents).toMatch(queryRegExp(0, true)); // match `i0.ɵquery(0, _c0, true)`
|
|
|
|
expect(jsContents).toMatch(queryRegExp(1, true)); // match `i0.ɵquery(1, _c0, true)`
|
2018-07-18 12:32:36 -04:00
|
|
|
});
|
2018-07-18 12:50:16 -04:00
|
|
|
|
2018-07-24 19:05:23 -04:00
|
|
|
it('should handle queries that use forwardRef', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write(`test.ts`, `
|
2018-07-24 19:05:23 -04:00
|
|
|
import {Component, ContentChild, TemplateRef, ViewContainerRef, forwardRef} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test',
|
|
|
|
template: '<div #foo></div>',
|
|
|
|
})
|
|
|
|
class FooCmp {
|
|
|
|
@ContentChild(forwardRef(() => TemplateRef)) child: any;
|
|
|
|
|
|
|
|
@ContentChild(forwardRef(function() { return ViewContainerRef; })) child2: any;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
2018-08-14 19:48:58 -04:00
|
|
|
expect(jsContents).toContain(`i0.ɵquery(null, TemplateRef, true)`);
|
|
|
|
expect(jsContents).toContain(`i0.ɵquery(null, ViewContainerRef, true)`);
|
2018-07-24 19:05:23 -04:00
|
|
|
});
|
|
|
|
|
2018-11-20 18:20:19 -05:00
|
|
|
it('should generate host listeners for components', () => {
|
|
|
|
env.tsconfig();
|
|
|
|
env.write(`test.ts`, `
|
|
|
|
import {Component, HostListener} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test',
|
|
|
|
template: 'Test'
|
|
|
|
})
|
|
|
|
class FooCmp {
|
|
|
|
@HostListener('document:click', ['$event.target'])
|
|
|
|
onClick(eventTarget: HTMLElement): void {}
|
|
|
|
|
|
|
|
@HostListener('window:scroll')
|
|
|
|
onScroll(event: any): void {}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
|
|
|
const hostBindingsFn = `
|
|
|
|
hostBindings: function FooCmp_HostBindings(rf, ctx, elIndex) {
|
|
|
|
if (rf & 1) {
|
|
|
|
i0.ɵlistener("click", function FooCmp_click_HostBindingHandler($event) { return ctx.onClick($event.target); });
|
|
|
|
i0.ɵlistener("scroll", function FooCmp_scroll_HostBindingHandler($event) { return ctx.onScroll(); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
expect(trim(jsContents)).toContain(trim(hostBindingsFn));
|
|
|
|
});
|
|
|
|
|
2018-07-18 12:50:16 -04:00
|
|
|
it('should generate host bindings for directives', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write(`test.ts`, `
|
2018-07-18 12:50:16 -04:00
|
|
|
import {Component, HostBinding, HostListener, TemplateRef} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test',
|
|
|
|
template: 'Test',
|
|
|
|
host: {
|
|
|
|
'[attr.hello]': 'foo',
|
|
|
|
'(click)': 'onClick($event)',
|
|
|
|
'[prop]': 'bar',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
class FooCmp {
|
|
|
|
onClick(event: any): void {}
|
|
|
|
|
|
|
|
@HostBinding('class.someclass')
|
|
|
|
get someClass(): boolean { return false; }
|
|
|
|
|
2018-10-16 13:28:23 -04:00
|
|
|
@HostListener('change', ['arg1', 'arg2', 'arg3'])
|
2018-07-18 12:50:16 -04:00
|
|
|
onChange(event: any, arg: any): void {}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
2018-11-20 18:20:19 -05:00
|
|
|
const hostBindingsFn = `
|
|
|
|
hostBindings: function FooCmp_HostBindings(rf, ctx, elIndex) {
|
|
|
|
if (rf & 1) {
|
2018-11-30 20:34:36 -05:00
|
|
|
i0.ɵallocHostVars(2);
|
2018-11-20 18:20:19 -05:00
|
|
|
i0.ɵlistener("click", function FooCmp_click_HostBindingHandler($event) { return ctx.onClick($event); });
|
|
|
|
i0.ɵlistener("change", function FooCmp_change_HostBindingHandler($event) { return ctx.onChange(ctx.arg1, ctx.arg2, ctx.arg3); });
|
|
|
|
i0.ɵelementStyling(_c0, null, null, ctx);
|
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
i0.ɵelementAttribute(elIndex, "hello", i0.ɵbind(ctx.foo));
|
|
|
|
i0.ɵelementProperty(elIndex, "prop", i0.ɵbind(ctx.bar));
|
|
|
|
i0.ɵelementClassProp(elIndex, 0, ctx.someClass, ctx);
|
|
|
|
i0.ɵelementStylingApply(elIndex, ctx);
|
|
|
|
}
|
2018-10-16 13:28:23 -04:00
|
|
|
}
|
|
|
|
`;
|
2018-11-20 18:20:19 -05:00
|
|
|
expect(trim(jsContents)).toContain(trim(hostBindingsFn));
|
2018-10-16 13:28:23 -04:00
|
|
|
});
|
|
|
|
|
2018-11-20 18:20:19 -05:00
|
|
|
it('should generate host listeners for directives within hostBindings section', () => {
|
2018-10-16 13:28:23 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write(`test.ts`, `
|
|
|
|
import {Directive, HostListener} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: '[test]',
|
|
|
|
})
|
2018-11-20 18:20:19 -05:00
|
|
|
class Dir {
|
2018-10-16 13:28:23 -04:00
|
|
|
@HostListener('change', ['arg'])
|
|
|
|
onChange(event: any, arg: any): void {}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
2018-11-20 18:20:19 -05:00
|
|
|
const hostBindingsFn = `
|
|
|
|
hostBindings: function Dir_HostBindings(rf, ctx, elIndex) {
|
|
|
|
if (rf & 1) {
|
|
|
|
i0.ɵlistener("change", function Dir_change_HostBindingHandler($event) { return ctx.onChange(ctx.arg); });
|
|
|
|
}
|
2018-10-16 13:28:23 -04:00
|
|
|
}
|
|
|
|
`;
|
2018-11-20 18:20:19 -05:00
|
|
|
expect(trim(jsContents)).toContain(trim(hostBindingsFn));
|
2018-07-18 12:50:16 -04:00
|
|
|
});
|
2018-07-25 14:16:00 -04:00
|
|
|
|
2018-11-16 12:57:23 -05:00
|
|
|
it('should use proper default value for preserveWhitespaces config param', () => {
|
|
|
|
env.tsconfig(); // default is `false`
|
|
|
|
env.write(`test.ts`, `
|
|
|
|
import {Component} from '@angular/core';
|
|
|
|
@Component({
|
|
|
|
selector: 'test',
|
|
|
|
preserveWhitespaces: false,
|
|
|
|
template: \`
|
|
|
|
<div>
|
|
|
|
Template with whitespaces
|
|
|
|
</div>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
class FooCmp {}
|
|
|
|
`);
|
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
|
|
|
expect(jsContents).toContain('text(1, " Template with whitespaces ");');
|
|
|
|
});
|
|
|
|
|
2018-11-20 13:51:16 -05:00
|
|
|
it('should take preserveWhitespaces config option into account', () => {
|
|
|
|
env.tsconfig({preserveWhitespaces: true});
|
|
|
|
env.write(`test.ts`, `
|
|
|
|
import {Component} from '@angular/core';
|
|
|
|
@Component({
|
|
|
|
selector: 'test',
|
|
|
|
template: \`
|
|
|
|
<div>
|
|
|
|
Template with whitespaces
|
|
|
|
</div>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
class FooCmp {}
|
|
|
|
`);
|
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
|
|
|
expect(jsContents)
|
|
|
|
.toContain('text(2, "\\n Template with whitespaces\\n ");');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('@Component\'s preserveWhitespaces should override the one defined in config', () => {
|
|
|
|
env.tsconfig({preserveWhitespaces: true});
|
|
|
|
env.write(`test.ts`, `
|
|
|
|
import {Component} from '@angular/core';
|
|
|
|
@Component({
|
|
|
|
selector: 'test',
|
|
|
|
preserveWhitespaces: false,
|
|
|
|
template: \`
|
|
|
|
<div>
|
|
|
|
Template with whitespaces
|
|
|
|
</div>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
class FooCmp {}
|
|
|
|
`);
|
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
|
|
|
expect(jsContents).toContain('text(1, " Template with whitespaces ");');
|
|
|
|
});
|
|
|
|
|
2018-11-16 12:57:23 -05:00
|
|
|
it('should use proper default value for i18nUseExternalIds config param', () => {
|
|
|
|
env.tsconfig(); // default is `true`
|
|
|
|
env.write(`test.ts`, `
|
|
|
|
import {Component} from '@angular/core';
|
|
|
|
@Component({
|
|
|
|
selector: 'test',
|
|
|
|
template: '<div i18n>Some text</div>'
|
|
|
|
})
|
|
|
|
class FooCmp {}
|
|
|
|
`);
|
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
|
|
|
expect(jsContents).toContain('i18n(1, MSG_EXTERNAL_8321000940098097247);');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should take i18nUseExternalIds config option into account', () => {
|
|
|
|
env.tsconfig({i18nUseExternalIds: false});
|
|
|
|
env.write(`test.ts`, `
|
|
|
|
import {Component} from '@angular/core';
|
|
|
|
@Component({
|
|
|
|
selector: 'test',
|
|
|
|
template: '<div i18n>Some text</div>'
|
|
|
|
})
|
|
|
|
class FooCmp {}
|
|
|
|
`);
|
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
|
|
|
expect(jsContents).toContain('i18n(1, MSG_TEST_TS_0);');
|
|
|
|
});
|
|
|
|
|
2018-07-25 14:16:00 -04:00
|
|
|
it('should correctly recognize local symbols', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write('module.ts', `
|
2018-07-25 14:16:00 -04:00
|
|
|
import {NgModule} from '@angular/core';
|
|
|
|
import {Dir, Comp} from './test';
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [Dir, Comp],
|
|
|
|
exports: [Dir, Comp],
|
|
|
|
})
|
|
|
|
class Module {}
|
|
|
|
`);
|
2018-09-25 18:35:03 -04:00
|
|
|
env.write(`test.ts`, `
|
2018-07-25 14:16:00 -04:00
|
|
|
import {Component, Directive} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: '[dir]',
|
|
|
|
})
|
|
|
|
export class Dir {}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test',
|
|
|
|
template: '<div dir>Test</div>',
|
|
|
|
})
|
|
|
|
export class Comp {}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
2018-07-25 14:16:00 -04:00
|
|
|
expect(jsContents).not.toMatch(/import \* as i[0-9] from ['"].\/test['"]/);
|
|
|
|
});
|
2018-07-28 01:57:44 -04:00
|
|
|
|
2018-08-06 03:56:43 -04:00
|
|
|
it('should generate exportAs declarations', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
2018-08-06 03:56:43 -04:00
|
|
|
import {Component, Directive} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: '[test]',
|
|
|
|
exportAs: 'foo',
|
|
|
|
})
|
|
|
|
class Dir {}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
2018-08-06 03:56:43 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
2018-08-06 03:56:43 -04:00
|
|
|
expect(jsContents).toContain(`exportAs: "foo"`);
|
|
|
|
});
|
|
|
|
|
2018-07-28 01:57:44 -04:00
|
|
|
it('should generate correct factory stubs for a test module', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig({'allowEmptyCodegenFiles': true});
|
2018-07-28 01:57:44 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.write('test.ts', `
|
2018-07-28 01:57:44 -04:00
|
|
|
import {Injectable, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class NotAModule {}
|
|
|
|
|
|
|
|
@NgModule({})
|
|
|
|
export class TestModule {}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.write('empty.ts', `
|
2018-07-28 01:57:44 -04:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class NotAModule {}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
2018-07-28 01:57:44 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const factoryContents = env.getContents('test.ngfactory.js');
|
2018-07-28 01:57:44 -04:00
|
|
|
expect(factoryContents).toContain(`import * as i0 from '@angular/core';`);
|
|
|
|
expect(factoryContents).toContain(`import { NotAModule, TestModule } from './test';`);
|
|
|
|
expect(factoryContents)
|
|
|
|
.toContain(`export var TestModuleNgFactory = new i0.ɵNgModuleFactory(TestModule);`);
|
|
|
|
expect(factoryContents).not.toContain(`NotAModuleNgFactory`);
|
|
|
|
expect(factoryContents).not.toContain('ɵNonEmptyModule');
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const emptyFactory = env.getContents('empty.ngfactory.js');
|
2018-07-28 01:57:44 -04:00
|
|
|
expect(emptyFactory).toContain(`import * as i0 from '@angular/core';`);
|
|
|
|
expect(emptyFactory).toContain(`export var ɵNonEmptyModule = true;`);
|
|
|
|
});
|
2018-08-06 05:48:26 -04:00
|
|
|
|
2018-10-16 18:07:46 -04:00
|
|
|
it('should generate a summary stub for decorated classes in the input file only', () => {
|
|
|
|
env.tsconfig({'allowEmptyCodegenFiles': true});
|
|
|
|
|
|
|
|
env.write('test.ts', `
|
|
|
|
import {Injectable, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
export class NotAModule {}
|
|
|
|
|
|
|
|
@NgModule({})
|
|
|
|
export class TestModule {}
|
|
|
|
`);
|
|
|
|
|
|
|
|
env.driveMain();
|
|
|
|
|
|
|
|
const summaryContents = env.getContents('test.ngsummary.js');
|
|
|
|
expect(summaryContents).toEqual(`export var TestModuleNgSummary = null;\n`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('it should generate empty export when there are no other summary symbols, to ensure the output is a valid ES module',
|
|
|
|
() => {
|
|
|
|
env.tsconfig({'allowEmptyCodegenFiles': true});
|
|
|
|
env.write('empty.ts', `
|
|
|
|
export class NotAModule {}
|
|
|
|
`);
|
|
|
|
|
|
|
|
env.driveMain();
|
|
|
|
|
|
|
|
const emptySummary = env.getContents('empty.ngsummary.js');
|
|
|
|
// The empty export ensures this js file is still an ES module.
|
|
|
|
expect(emptySummary).toEqual(`export var ɵempty = null;\n`);
|
|
|
|
});
|
|
|
|
|
2018-08-06 05:48:26 -04:00
|
|
|
it('should compile a banana-in-a-box inside of a template', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
2018-08-06 05:48:26 -04:00
|
|
|
import {Component} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: '<div *tmpl [(bananaInABox)]="prop"></div>',
|
|
|
|
selector: 'test'
|
|
|
|
})
|
|
|
|
class TestCmp {}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
2018-08-06 05:48:26 -04:00
|
|
|
});
|
2018-07-16 19:36:31 -04:00
|
|
|
|
|
|
|
it('generates inherited factory definitions', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write(`test.ts`, `
|
2018-07-16 19:36:31 -04:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
|
|
|
|
class Dep {}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
class Base {
|
|
|
|
constructor(dep: Dep) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
class Child extends Base {}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
class GrandChild extends Child {
|
|
|
|
constructor() {
|
|
|
|
super(null!);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
2018-07-16 19:36:31 -04:00
|
|
|
|
|
|
|
expect(jsContents)
|
|
|
|
.toContain('function Base_Factory(t) { return new (t || Base)(i0.inject(Dep)); }');
|
|
|
|
expect(jsContents).toContain('var ɵChild_BaseFactory = i0.ɵgetInheritedFactory(Child)');
|
|
|
|
expect(jsContents)
|
|
|
|
.toContain('function Child_Factory(t) { return ɵChild_BaseFactory((t || Child)); }');
|
|
|
|
expect(jsContents)
|
|
|
|
.toContain('function GrandChild_Factory(t) { return new (t || GrandChild)(); }');
|
|
|
|
});
|
2018-08-06 08:49:35 -04:00
|
|
|
|
2018-08-22 14:37:07 -04:00
|
|
|
it('generates base factories for directives', () => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write(`test.ts`, `
|
2018-08-22 14:37:07 -04:00
|
|
|
import {Directive} from '@angular/core';
|
|
|
|
|
|
|
|
class Base {}
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: '[test]',
|
|
|
|
})
|
|
|
|
class Dir extends Base {
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
2018-08-22 14:37:07 -04:00
|
|
|
|
|
|
|
expect(jsContents).toContain('var ɵDir_BaseFactory = i0.ɵgetInheritedFactory(Dir)');
|
|
|
|
});
|
|
|
|
|
2018-08-06 08:49:35 -04:00
|
|
|
it('should wrap "directives" in component metadata in a closure when forward references are present',
|
|
|
|
() => {
|
2018-09-25 18:35:03 -04:00
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
2018-08-06 08:49:35 -04:00
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'cmp-a',
|
|
|
|
template: '<cmp-b></cmp-b>',
|
|
|
|
})
|
|
|
|
class CmpA {}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'cmp-b',
|
|
|
|
template: 'This is B',
|
|
|
|
})
|
|
|
|
class CmpB {}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [CmpA, CmpB],
|
|
|
|
})
|
|
|
|
class Module {}
|
|
|
|
`);
|
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
env.driveMain();
|
2018-08-06 08:49:35 -04:00
|
|
|
|
2018-09-25 18:35:03 -04:00
|
|
|
const jsContents = env.getContents('test.js');
|
2018-08-06 08:49:35 -04:00
|
|
|
expect(jsContents).toContain('directives: function () { return [CmpB]; }');
|
|
|
|
});
|
2018-10-30 14:19:10 -04:00
|
|
|
|
|
|
|
it('should emit setClassMetadata calls for all types', () => {
|
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
|
|
|
import {Component, Directive, Injectable, NgModule, Pipe} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({selector: 'cmp', template: 'I am a component!'}) class TestComponent {}
|
|
|
|
@Directive({selector: 'dir'}) class TestDirective {}
|
|
|
|
@Injectable() class TestInjectable {}
|
|
|
|
@NgModule({declarations: [TestComponent, TestDirective]}) class TestNgModule {}
|
|
|
|
@Pipe({name: 'pipe'}) class TestPipe {}
|
|
|
|
`);
|
|
|
|
|
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
|
|
|
expect(jsContents).toContain('ɵsetClassMetadata(TestComponent, ');
|
|
|
|
expect(jsContents).toContain('ɵsetClassMetadata(TestDirective, ');
|
|
|
|
expect(jsContents).toContain('ɵsetClassMetadata(TestInjectable, ');
|
|
|
|
expect(jsContents).toContain('ɵsetClassMetadata(TestNgModule, ');
|
|
|
|
expect(jsContents).toContain('ɵsetClassMetadata(TestPipe, ');
|
|
|
|
});
|
2018-11-20 11:20:16 -05:00
|
|
|
|
|
|
|
it('should compile a template using multiple directives with the same selector', () => {
|
|
|
|
env.tsconfig();
|
|
|
|
env.write('test.ts', `
|
|
|
|
import {Component, Directive, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: '[test]'})
|
|
|
|
class DirA {}
|
|
|
|
|
|
|
|
@Directive({selector: '[test]'})
|
|
|
|
class DirB {}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: '<div test></div>',
|
|
|
|
})
|
|
|
|
class Cmp {}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [Cmp, DirA, DirB],
|
|
|
|
})
|
|
|
|
class Module {}
|
|
|
|
`);
|
|
|
|
|
|
|
|
env.driveMain();
|
|
|
|
const jsContents = env.getContents('test.js');
|
|
|
|
expect(jsContents).toMatch(/directives: \[DirA,\s+DirB\]/);
|
|
|
|
});
|
2018-04-06 12:53:10 -04:00
|
|
|
});
|