2018-04-06 09:53:10 -07: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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
|
2018-05-30 16:02:53 -07:00
|
|
|
import {getDeclaration, makeProgram} from '../../testing/in_memory_typescript';
|
2018-12-18 09:48:15 -08:00
|
|
|
import {CtorParameter} from '../src/host';
|
|
|
|
|
import {TypeScriptReflectionHost} from '../src/typescript';
|
2019-03-20 12:10:58 +02:00
|
|
|
import {isNamedClassDeclaration} from '../src/util';
|
2018-04-06 09:53:10 -07:00
|
|
|
|
|
|
|
|
describe('reflector', () => {
|
|
|
|
|
describe('ctor params', () => {
|
|
|
|
|
it('should reflect a single argument', () => {
|
2018-05-30 16:02:53 -07:00
|
|
|
const {program} = makeProgram([{
|
2018-04-06 09:53:10 -07:00
|
|
|
name: 'entry.ts',
|
|
|
|
|
contents: `
|
|
|
|
|
class Bar {}
|
|
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
|
constructor(bar: Bar) {}
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
}]);
|
2019-03-20 12:10:58 +02:00
|
|
|
const clazz = getDeclaration(program, 'entry.ts', 'Foo', isNamedClassDeclaration);
|
2018-04-06 09:53:10 -07:00
|
|
|
const checker = program.getTypeChecker();
|
2018-06-13 10:33:04 -07:00
|
|
|
const host = new TypeScriptReflectionHost(checker);
|
|
|
|
|
const args = host.getConstructorParameters(clazz) !;
|
2018-04-06 09:53:10 -07:00
|
|
|
expect(args.length).toBe(1);
|
2018-06-13 10:33:04 -07:00
|
|
|
expectParameter(args[0], 'bar', 'Bar');
|
2018-04-06 09:53:10 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should reflect a decorated argument', () => {
|
2018-05-30 16:02:53 -07:00
|
|
|
const {program} = makeProgram([
|
2018-04-06 09:53:10 -07:00
|
|
|
{
|
|
|
|
|
name: 'dec.ts',
|
|
|
|
|
contents: `
|
|
|
|
|
export function dec(target: any, key: string, index: number) {
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'entry.ts',
|
|
|
|
|
contents: `
|
|
|
|
|
import {dec} from './dec';
|
|
|
|
|
class Bar {}
|
2018-12-07 12:10:26 -08:00
|
|
|
|
2018-04-06 09:53:10 -07:00
|
|
|
class Foo {
|
|
|
|
|
constructor(@dec bar: Bar) {}
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
}
|
|
|
|
|
]);
|
2019-03-20 12:10:58 +02:00
|
|
|
const clazz = getDeclaration(program, 'entry.ts', 'Foo', isNamedClassDeclaration);
|
2018-04-06 09:53:10 -07:00
|
|
|
const checker = program.getTypeChecker();
|
2018-06-13 10:33:04 -07:00
|
|
|
const host = new TypeScriptReflectionHost(checker);
|
|
|
|
|
const args = host.getConstructorParameters(clazz) !;
|
2018-04-06 09:53:10 -07:00
|
|
|
expect(args.length).toBe(1);
|
2018-06-13 10:33:04 -07:00
|
|
|
expectParameter(args[0], 'bar', 'Bar', 'dec', './dec');
|
2018-04-06 09:53:10 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should reflect a decorated argument with a call', () => {
|
2018-05-30 16:02:53 -07:00
|
|
|
const {program} = makeProgram([
|
2018-04-06 09:53:10 -07:00
|
|
|
{
|
|
|
|
|
name: 'dec.ts',
|
|
|
|
|
contents: `
|
|
|
|
|
export function dec(target: any, key: string, index: number) {
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'entry.ts',
|
|
|
|
|
contents: `
|
|
|
|
|
import {dec} from './dec';
|
|
|
|
|
class Bar {}
|
2018-12-07 12:10:26 -08:00
|
|
|
|
2018-04-06 09:53:10 -07:00
|
|
|
class Foo {
|
|
|
|
|
constructor(@dec bar: Bar) {}
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
}
|
|
|
|
|
]);
|
2019-03-20 12:10:58 +02:00
|
|
|
const clazz = getDeclaration(program, 'entry.ts', 'Foo', isNamedClassDeclaration);
|
2018-04-06 09:53:10 -07:00
|
|
|
const checker = program.getTypeChecker();
|
2018-06-13 10:33:04 -07:00
|
|
|
const host = new TypeScriptReflectionHost(checker);
|
|
|
|
|
const args = host.getConstructorParameters(clazz) !;
|
2018-04-06 09:53:10 -07:00
|
|
|
expect(args.length).toBe(1);
|
2018-06-13 10:33:04 -07:00
|
|
|
expectParameter(args[0], 'bar', 'Bar', 'dec', './dec');
|
2018-04-06 09:53:10 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should reflect a decorated argument with an indirection', () => {
|
2018-05-30 16:02:53 -07:00
|
|
|
const {program} = makeProgram([
|
2018-04-06 09:53:10 -07:00
|
|
|
{
|
|
|
|
|
name: 'bar.ts',
|
|
|
|
|
contents: `
|
|
|
|
|
export class Bar {}
|
|
|
|
|
`
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'entry.ts',
|
|
|
|
|
contents: `
|
|
|
|
|
import {Bar} from './bar';
|
|
|
|
|
import * as star from './bar';
|
2018-12-07 12:10:26 -08:00
|
|
|
|
2018-04-06 09:53:10 -07:00
|
|
|
class Foo {
|
|
|
|
|
constructor(bar: Bar, otherBar: star.Bar) {}
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
}
|
|
|
|
|
]);
|
2019-03-20 12:10:58 +02:00
|
|
|
const clazz = getDeclaration(program, 'entry.ts', 'Foo', isNamedClassDeclaration);
|
2018-04-06 09:53:10 -07:00
|
|
|
const checker = program.getTypeChecker();
|
2018-06-13 10:33:04 -07:00
|
|
|
const host = new TypeScriptReflectionHost(checker);
|
|
|
|
|
const args = host.getConstructorParameters(clazz) !;
|
2018-04-06 09:53:10 -07:00
|
|
|
expect(args.length).toBe(2);
|
2019-03-04 11:43:55 -08:00
|
|
|
expectParameter(args[0], 'bar', {moduleName: './bar', name: 'Bar'});
|
|
|
|
|
expectParameter(args[1], 'otherBar', {moduleName: './bar', name: 'Bar'});
|
2018-04-06 09:53:10 -07:00
|
|
|
});
|
2018-12-07 12:10:26 -08:00
|
|
|
|
2019-03-04 11:43:55 -08:00
|
|
|
it('should reflect an argument from an aliased import', () => {
|
|
|
|
|
const {program} = makeProgram([
|
|
|
|
|
{
|
|
|
|
|
name: 'bar.ts',
|
|
|
|
|
contents: `
|
|
|
|
|
export class Bar {}
|
|
|
|
|
`
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'entry.ts',
|
|
|
|
|
contents: `
|
|
|
|
|
import {Bar as LocalBar} from './bar';
|
|
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
|
constructor(bar: LocalBar) {}
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
}
|
|
|
|
|
]);
|
2019-03-20 12:10:58 +02:00
|
|
|
const clazz = getDeclaration(program, 'entry.ts', 'Foo', isNamedClassDeclaration);
|
2019-03-04 11:43:55 -08:00
|
|
|
const checker = program.getTypeChecker();
|
|
|
|
|
const host = new TypeScriptReflectionHost(checker);
|
|
|
|
|
const args = host.getConstructorParameters(clazz) !;
|
|
|
|
|
expect(args.length).toBe(1);
|
|
|
|
|
expectParameter(args[0], 'bar', {moduleName: './bar', name: 'Bar'});
|
|
|
|
|
});
|
2018-12-07 12:10:26 -08:00
|
|
|
|
2019-03-06 16:35:08 -08:00
|
|
|
it('should reflect an argument from a default import', () => {
|
|
|
|
|
const {program} = makeProgram([
|
|
|
|
|
{
|
|
|
|
|
name: 'bar.ts',
|
|
|
|
|
contents: `
|
|
|
|
|
export default class Bar {}
|
|
|
|
|
`
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'entry.ts',
|
|
|
|
|
contents: `
|
|
|
|
|
import Bar from './bar';
|
|
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
|
constructor(bar: Bar) {}
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
}
|
|
|
|
|
]);
|
2019-03-20 12:10:58 +02:00
|
|
|
const clazz = getDeclaration(program, 'entry.ts', 'Foo', isNamedClassDeclaration);
|
2019-03-06 16:35:08 -08:00
|
|
|
const checker = program.getTypeChecker();
|
|
|
|
|
const host = new TypeScriptReflectionHost(checker);
|
|
|
|
|
const args = host.getConstructorParameters(clazz) !;
|
|
|
|
|
expect(args.length).toBe(1);
|
fix(ivy): reuse default imports in type-to-value references (#29266)
This fixes an issue with commit b6f6b117. In this commit, default imports
processed in a type-to-value conversion were recorded as non-local imports
with a '*' name, and the ImportManager generated a new default import for
them. When transpiled to ES2015 modules, this resulted in the following
correct code:
import i3 from './module';
// somewhere in the file, a value reference of i3:
{type: i3}
However, when the AST with this synthetic import and reference was
transpiled to non-ES2015 modules (for example, to commonjs) an issue
appeared:
var module_1 = require('./module');
{type: i3}
TypeScript renames the imported identifier from i3 to module_1, but doesn't
substitute later references to i3. This is because the import and reference
are both synthetic, and never went through the TypeScript AST step of
"binding" which associates the reference to its import. This association is
important during emit when the identifiers might change.
Synthetic (transformer-added) imports will never be bound properly. The only
possible solution is to reuse the user's original import and the identifier
from it, which will be properly downleveled. The issue with this approach
(which prompted the fix in b6f6b117) is that if the import is only used in a
type position, TypeScript will mark it for deletion in the generated JS,
even though additional non-type usages are added in the transformer. This
again would leave a dangling import.
To work around this, it's necessary for the compiler to keep track of
identifiers that it emits which came from default imports, and tell TS not
to remove those imports during transpilation. A `DefaultImportTracker` class
is implemented to perform this tracking. It implements a
`DefaultImportRecorder` interface, which is used to record two significant
pieces of information:
* when a WrappedNodeExpr is generated which refers to a default imported
value, the ts.Identifier is associated to the ts.ImportDeclaration via
the recorder.
* when that WrappedNodeExpr is later emitted as part of the statement /
expression translators, the fact that the ts.Identifier was used is
also recorded.
Combined, this tracking gives the `DefaultImportTracker` enough information
to implement another TS transformer, which can recognize default imports
which were used in the output of the Ivy transform and can prevent them
from being elided. This is done by creating a new ts.ImportDeclaration for
the imports with the same ts.ImportClause. A test verifies that this works.
PR Close #29266
2019-03-11 16:54:07 -07:00
|
|
|
const param = args[0].typeValueReference;
|
|
|
|
|
if (param === null || !param.local) {
|
|
|
|
|
return fail('Expected local parameter');
|
|
|
|
|
}
|
|
|
|
|
expect(param).not.toBeNull();
|
|
|
|
|
expect(param.defaultImportStatement).not.toBeNull();
|
2019-03-06 16:35:08 -08:00
|
|
|
});
|
|
|
|
|
|
2019-03-04 11:43:55 -08:00
|
|
|
it('should reflect a nullable argument', () => {
|
2018-12-07 12:10:26 -08:00
|
|
|
const {program} = makeProgram([
|
|
|
|
|
{
|
|
|
|
|
name: 'bar.ts',
|
|
|
|
|
contents: `
|
|
|
|
|
export class Bar {}
|
|
|
|
|
`
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'entry.ts',
|
|
|
|
|
contents: `
|
|
|
|
|
import {Bar} from './bar';
|
|
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
|
constructor(bar: Bar|null) {}
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
}
|
|
|
|
|
]);
|
2019-03-20 12:10:58 +02:00
|
|
|
const clazz = getDeclaration(program, 'entry.ts', 'Foo', isNamedClassDeclaration);
|
2018-12-07 12:10:26 -08:00
|
|
|
const checker = program.getTypeChecker();
|
|
|
|
|
const host = new TypeScriptReflectionHost(checker);
|
|
|
|
|
const args = host.getConstructorParameters(clazz) !;
|
|
|
|
|
expect(args.length).toBe(1);
|
2019-03-04 11:43:55 -08:00
|
|
|
expectParameter(args[0], 'bar', {moduleName: './bar', name: 'Bar'});
|
2018-12-07 12:10:26 -08:00
|
|
|
});
|
2018-04-06 09:53:10 -07:00
|
|
|
});
|
2018-07-12 12:11:18 -07:00
|
|
|
|
|
|
|
|
it('should reflect a re-export', () => {
|
|
|
|
|
const {program} = makeProgram([
|
|
|
|
|
{name: '/node_modules/absolute/index.ts', contents: 'export class Target {}'},
|
|
|
|
|
{name: 'local1.ts', contents: `export {Target as AliasTarget} from 'absolute';`},
|
|
|
|
|
{name: 'local2.ts', contents: `export {AliasTarget as Target} from './local1';`}, {
|
|
|
|
|
name: 'entry.ts',
|
|
|
|
|
contents: `
|
|
|
|
|
import {Target} from './local2';
|
|
|
|
|
import {Target as DirectTarget} from 'absolute';
|
|
|
|
|
|
|
|
|
|
const target = Target;
|
|
|
|
|
const directTarget = DirectTarget;
|
|
|
|
|
`
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
const target = getDeclaration(program, 'entry.ts', 'target', ts.isVariableDeclaration);
|
|
|
|
|
if (target.initializer === undefined || !ts.isIdentifier(target.initializer)) {
|
|
|
|
|
return fail('Unexpected initializer for target');
|
|
|
|
|
}
|
|
|
|
|
const directTarget =
|
|
|
|
|
getDeclaration(program, 'entry.ts', 'directTarget', ts.isVariableDeclaration);
|
|
|
|
|
if (directTarget.initializer === undefined || !ts.isIdentifier(directTarget.initializer)) {
|
|
|
|
|
return fail('Unexpected initializer for directTarget');
|
|
|
|
|
}
|
|
|
|
|
const Target = target.initializer;
|
|
|
|
|
const DirectTarget = directTarget.initializer;
|
|
|
|
|
|
|
|
|
|
const checker = program.getTypeChecker();
|
|
|
|
|
const host = new TypeScriptReflectionHost(checker);
|
|
|
|
|
const targetDecl = host.getDeclarationOfIdentifier(Target);
|
|
|
|
|
const directTargetDecl = host.getDeclarationOfIdentifier(DirectTarget);
|
|
|
|
|
if (targetDecl === null) {
|
|
|
|
|
return fail('No declaration found for Target');
|
|
|
|
|
} else if (directTargetDecl === null) {
|
|
|
|
|
return fail('No declaration found for DirectTarget');
|
|
|
|
|
}
|
|
|
|
|
expect(targetDecl.node.getSourceFile().fileName).toBe('/node_modules/absolute/index.ts');
|
|
|
|
|
expect(ts.isClassDeclaration(targetDecl.node)).toBe(true);
|
|
|
|
|
expect(directTargetDecl.viaModule).toBe('absolute');
|
|
|
|
|
expect(directTargetDecl.node).toBe(targetDecl.node);
|
|
|
|
|
});
|
2018-04-06 09:53:10 -07:00
|
|
|
});
|
|
|
|
|
|
2018-06-13 10:33:04 -07:00
|
|
|
function expectParameter(
|
2019-03-04 11:43:55 -08:00
|
|
|
param: CtorParameter, name: string, type?: string | {name: string, moduleName: string},
|
|
|
|
|
decorator?: string, decoratorFrom?: string): void {
|
2018-06-13 10:33:04 -07:00
|
|
|
expect(param.name !).toEqual(name);
|
2018-04-06 09:53:10 -07:00
|
|
|
if (type === undefined) {
|
2019-03-04 11:43:55 -08:00
|
|
|
expect(param.typeValueReference).toBeNull();
|
2018-04-06 09:53:10 -07:00
|
|
|
} else {
|
2019-03-04 11:43:55 -08:00
|
|
|
if (param.typeValueReference === null) {
|
|
|
|
|
return fail(`Expected parameter ${name} to have a typeValueReference`);
|
|
|
|
|
}
|
|
|
|
|
if (param.typeValueReference.local && typeof type === 'string') {
|
|
|
|
|
expect(argExpressionToString(param.typeValueReference.expression)).toEqual(type);
|
|
|
|
|
} else if (!param.typeValueReference.local && typeof type !== 'string') {
|
|
|
|
|
expect(param.typeValueReference.moduleName).toEqual(type.moduleName);
|
|
|
|
|
expect(param.typeValueReference.name).toEqual(type.name);
|
|
|
|
|
} else {
|
|
|
|
|
return fail(
|
|
|
|
|
`Mismatch between typeValueReference and expected type: ${param.name} / ${param.typeValueReference.local}`);
|
|
|
|
|
}
|
2018-04-06 09:53:10 -07:00
|
|
|
}
|
|
|
|
|
if (decorator !== undefined) {
|
2018-06-13 10:33:04 -07:00
|
|
|
expect(param.decorators).not.toBeNull();
|
|
|
|
|
expect(param.decorators !.length).toBeGreaterThan(0);
|
|
|
|
|
expect(param.decorators !.some(
|
|
|
|
|
dec => dec.name === decorator && dec.import !== null &&
|
|
|
|
|
dec.import.from === decoratorFrom))
|
2018-04-06 09:53:10 -07:00
|
|
|
.toBe(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-07 12:10:26 -08:00
|
|
|
function argExpressionToString(name: ts.Node | null): string {
|
|
|
|
|
if (name == null) {
|
|
|
|
|
throw new Error('\'name\' argument can\'t be null');
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-06 09:53:10 -07:00
|
|
|
if (ts.isIdentifier(name)) {
|
|
|
|
|
return name.text;
|
|
|
|
|
} else if (ts.isPropertyAccessExpression(name)) {
|
|
|
|
|
return `${argExpressionToString(name.expression)}.${name.name.text}`;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`Unexpected node in arg expression: ${ts.SyntaxKind[name.kind]}.`);
|
|
|
|
|
}
|
2019-03-20 12:10:58 +02:00
|
|
|
}
|