Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tsickle): support ctorParams in function closure #12876

Merged
merged 1 commit into from Nov 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion modules/@angular/compiler-cli/src/static_reflector.ts
Expand Up @@ -132,7 +132,6 @@ export class StaticReflector implements ReflectorReader {
const ctor = (<any[]>ctorData).find(a => a['__symbolic'] == 'constructor');
const parameterTypes = <any[]>this.simplify(type, ctor['parameters'] || []);
const parameterDecorators = <any[]>this.simplify(type, ctor['parameterDecorators'] || []);

parameters = [];
parameterTypes.forEach((paramType, index) => {
const nestedResult: any[] = [];
Expand Down
21 changes: 20 additions & 1 deletion modules/@angular/compiler-cli/test/static_reflector_spec.ts
Expand Up @@ -7,7 +7,7 @@
*/

import {StaticReflector, StaticReflectorHost, StaticSymbol} from '@angular/compiler-cli/src/static_reflector';
import {HostListener, animate, group, keyframes, sequence, state, style, transition, trigger} from '@angular/core';
import {HostListener, Inject, animate, group, keyframes, sequence, state, style, transition, trigger} from '@angular/core';
import {MetadataCollector} from '@angular/tsc-wrapped';
import * as ts from 'typescript';

Expand Down Expand Up @@ -410,6 +410,13 @@ describe('StaticReflector', () => {
expect(props).toEqual({foo: []});
});

it('should read ctor parameters with forwardRef', () => {
const src = '/tmp/src/forward-ref.ts';
const dep = host.getStaticSymbol(src, 'Dep');
const props = reflector.parameters(host.getStaticSymbol(src, 'Forward'));
expect(props).toEqual([[dep, new Inject(dep)]]);
});

it('should report an error for invalid function calls', () => {
expect(
() =>
Expand Down Expand Up @@ -1068,6 +1075,18 @@ class MockReflectorHost implements StaticReflectorHost {
providers: [ { provider: 'a', useValue: (() => 1)() }]
})
export class InvalidMetadata {}
`,
'/tmp/src/forward-ref.ts': `
import {forwardRef} from 'angular2/core';
import {Component} from 'angular2/src/core/metadata';
import {Inject} from 'angular2/src/core/di/metadata';
@Component({})
export class Forward {
constructor(@Inject(forwardRef(() => Dep)) d: Dep) {}
}
export class Dep {
@Input f: Forward;
}
`
};

Expand Down
Expand Up @@ -56,8 +56,12 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
}

// API of tsickle for lowering decorators to properties on the class.
if ((<any>type).ctorParameters) {
const ctorParameters = (<any>type).ctorParameters;
const tsickleCtorParams = (<any>type).ctorParameters;
if (tsickleCtorParams) {
// Newer tsickle uses a function closure
// Retain the non-function case for compatibility with older tsickle
const ctorParameters =
typeof tsickleCtorParams === 'function' ? tsickleCtorParams() : tsickleCtorParams;
const paramTypes = ctorParameters.map((ctorParam: any) => ctorParam && ctorParam.type);
const paramAnnotations = ctorParameters.map(
(ctorParam: any) =>
Expand Down
19 changes: 19 additions & 0 deletions modules/@angular/core/test/reflection/reflector_spec.ts
Expand Up @@ -89,6 +89,25 @@ export function main() {
const p = reflector.parameters(ClassWithoutDecorators);
expect(p.length).toEqual(2);
});

// See https://github.com/angular/tsickle/issues/261
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a test for the old style ctorParams?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's here in the first assertion

it('should read forwardRef down-leveled type', () => {
class Dep {}
class ForwardLegacy {
constructor(d: Dep) {}
// Older tsickle had a bug: wrote a forward reference
static ctorParameters = [{type: Dep}];
}
expect(reflector.parameters(ForwardLegacy)).toEqual([[Dep]]);
class Forward {
constructor(d: Dep) {}
// Newer tsickle generates a functionClosure
static ctorParameters = () => [{type: ForwardDep}];
}
class ForwardDep {}
expect(reflector.parameters(Forward)).toEqual([[ForwardDep]]);
});

});

describe('propMetadata', () => {
Expand Down