2016-10-23 16:37:15 -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
|
|
|
|
*/
|
|
|
|
|
2016-03-22 20:11:42 -04:00
|
|
|
import * as ts from 'typescript';
|
2016-06-08 14:14:43 -04:00
|
|
|
|
2016-03-22 20:11:42 -04:00
|
|
|
import {MetadataCollector} from '../src/collector';
|
2017-07-26 17:58:07 -04:00
|
|
|
import {ClassMetadata, ConstructorMetadata, MetadataEntry, ModuleMetadata, isClassMetadata, isMetadataGlobalReferenceExpression} from '../src/schema';
|
2016-03-22 20:11:42 -04:00
|
|
|
|
2016-06-08 14:14:43 -04:00
|
|
|
import {Directory, Host, expectValidSources} from './typescript.mocks';
|
2016-03-22 20:11:42 -04:00
|
|
|
|
|
|
|
describe('Collector', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const documentRegistry = ts.createDocumentRegistry();
|
2016-11-10 14:58:55 -05:00
|
|
|
let host: Host;
|
2016-03-22 20:11:42 -04:00
|
|
|
let service: ts.LanguageService;
|
|
|
|
let program: ts.Program;
|
|
|
|
let collector: MetadataCollector;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2016-05-31 14:00:39 -04:00
|
|
|
host = new Host(FILES, [
|
2016-07-28 20:32:29 -04:00
|
|
|
'/app/app.component.ts',
|
|
|
|
'/app/cases-data.ts',
|
|
|
|
'/app/error-cases.ts',
|
|
|
|
'/promise.ts',
|
|
|
|
'/unsupported-1.ts',
|
|
|
|
'/unsupported-2.ts',
|
2017-03-01 17:45:00 -05:00
|
|
|
'/unsupported-3.ts',
|
2017-01-25 16:40:55 -05:00
|
|
|
'class-arity.ts',
|
2016-07-28 20:32:29 -04:00
|
|
|
'import-star.ts',
|
2016-11-17 12:52:38 -05:00
|
|
|
'exported-classes.ts',
|
2016-07-28 20:32:29 -04:00
|
|
|
'exported-functions.ts',
|
|
|
|
'exported-enum.ts',
|
2017-08-15 10:39:54 -04:00
|
|
|
'exported-type.ts',
|
2016-07-28 20:32:29 -04:00
|
|
|
'exported-consts.ts',
|
2016-08-02 17:38:31 -04:00
|
|
|
'local-symbol-ref.ts',
|
2016-08-22 20:37:48 -04:00
|
|
|
'local-function-ref.ts',
|
|
|
|
'local-symbol-ref-func.ts',
|
|
|
|
'local-symbol-ref-func-dynamic.ts',
|
2016-08-22 13:26:22 -04:00
|
|
|
'private-enum.ts',
|
2016-08-02 14:45:14 -04:00
|
|
|
're-exports.ts',
|
2016-12-14 14:37:01 -05:00
|
|
|
're-exports-2.ts',
|
2016-12-16 18:33:47 -05:00
|
|
|
'export-as.d.ts',
|
2016-07-28 20:32:29 -04:00
|
|
|
'static-field-reference.ts',
|
|
|
|
'static-method.ts',
|
|
|
|
'static-method-call.ts',
|
|
|
|
'static-method-with-if.ts',
|
2016-07-29 12:10:45 -04:00
|
|
|
'static-method-with-default.ts',
|
2016-11-18 18:17:44 -05:00
|
|
|
'class-inheritance.ts',
|
|
|
|
'class-inheritance-parent.ts',
|
2017-03-15 12:24:56 -04:00
|
|
|
'class-inheritance-declarations.d.ts',
|
|
|
|
'interface-reference.ts'
|
2016-05-31 14:00:39 -04:00
|
|
|
]);
|
2016-06-13 18:56:51 -04:00
|
|
|
service = ts.createLanguageService(host, documentRegistry);
|
2016-03-22 20:11:42 -04:00
|
|
|
program = service.getProgram();
|
2016-12-12 13:49:17 -05:00
|
|
|
collector = new MetadataCollector({quotedNames: true});
|
2016-03-22 20:11:42 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not have errors in test data', () => { expectValidSources(service, program); });
|
|
|
|
|
|
|
|
it('should return undefined for modules that have no metadata', () => {
|
2017-03-15 12:24:56 -04:00
|
|
|
const sourceFile = program.getSourceFile('app/empty.ts');
|
2016-05-31 14:00:39 -04:00
|
|
|
const metadata = collector.getMetadata(sourceFile);
|
2016-03-22 20:11:42 -04:00
|
|
|
expect(metadata).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
2017-08-15 10:39:54 -04:00
|
|
|
it('should return an interface reference for types', () => {
|
|
|
|
const sourceFile = program.getSourceFile('/exported-type.ts');
|
|
|
|
const metadata = collector.getMetadata(sourceFile);
|
|
|
|
expect(metadata).toEqual(
|
|
|
|
{__symbolic: 'module', version: 3, metadata: {SomeType: {__symbolic: 'interface'}}});
|
|
|
|
});
|
|
|
|
|
2017-03-15 12:24:56 -04:00
|
|
|
it('should return an interface reference for interfaces', () => {
|
|
|
|
const sourceFile = program.getSourceFile('app/hero.ts');
|
|
|
|
const metadata = collector.getMetadata(sourceFile);
|
|
|
|
expect(metadata).toEqual(
|
|
|
|
{__symbolic: 'module', version: 3, metadata: {Hero: {__symbolic: 'interface'}}});
|
|
|
|
});
|
|
|
|
|
2016-05-26 13:45:37 -04:00
|
|
|
it('should be able to collect a simple component\'s metadata', () => {
|
2016-03-22 20:11:42 -04:00
|
|
|
const sourceFile = program.getSourceFile('app/hero-detail.component.ts');
|
2016-05-31 14:00:39 -04:00
|
|
|
const metadata = collector.getMetadata(sourceFile);
|
2016-03-22 20:11:42 -04:00
|
|
|
expect(metadata).toEqual({
|
|
|
|
__symbolic: 'module',
|
2016-12-14 18:28:51 -05:00
|
|
|
version: 3,
|
2016-03-22 20:11:42 -04:00
|
|
|
metadata: {
|
|
|
|
HeroDetailComponent: {
|
|
|
|
__symbolic: 'class',
|
2016-05-26 13:45:37 -04:00
|
|
|
decorators: [{
|
|
|
|
__symbolic: 'call',
|
2016-05-31 14:00:39 -04:00
|
|
|
expression: {__symbolic: 'reference', module: 'angular2/core', name: 'Component'},
|
2016-05-26 13:45:37 -04:00
|
|
|
arguments: [{
|
|
|
|
selector: 'my-hero-detail',
|
|
|
|
template: `
|
2016-03-22 20:11:42 -04:00
|
|
|
<div *ngIf="hero">
|
|
|
|
<h2>{{hero.name}} details!</h2>
|
|
|
|
<div><label>id: </label>{{hero.id}}</div>
|
|
|
|
<div>
|
|
|
|
<label>name: </label>
|
|
|
|
<input [(ngModel)]="hero.name" placeholder="name"/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`
|
2016-05-26 13:45:37 -04:00
|
|
|
}]
|
|
|
|
}],
|
2016-03-22 20:11:42 -04:00
|
|
|
members: {
|
2016-05-26 13:45:37 -04:00
|
|
|
hero: [{
|
|
|
|
__symbolic: 'property',
|
|
|
|
decorators: [{
|
|
|
|
__symbolic: 'call',
|
|
|
|
expression:
|
2016-05-31 14:00:39 -04:00
|
|
|
{__symbolic: 'reference', module: 'angular2/core', name: 'Input'}
|
2016-05-26 13:45:37 -04:00
|
|
|
}]
|
|
|
|
}]
|
2016-03-22 20:11:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-26 13:45:37 -04:00
|
|
|
it('should be able to get a more complicated component\'s metadata', () => {
|
2016-03-22 20:11:42 -04:00
|
|
|
const sourceFile = program.getSourceFile('/app/app.component.ts');
|
2016-05-31 14:00:39 -04:00
|
|
|
const metadata = collector.getMetadata(sourceFile);
|
2016-03-22 20:11:42 -04:00
|
|
|
expect(metadata).toEqual({
|
|
|
|
__symbolic: 'module',
|
2016-12-14 18:28:51 -05:00
|
|
|
version: 3,
|
2016-03-22 20:11:42 -04:00
|
|
|
metadata: {
|
|
|
|
AppComponent: {
|
|
|
|
__symbolic: 'class',
|
2016-05-26 13:45:37 -04:00
|
|
|
decorators: [{
|
|
|
|
__symbolic: 'call',
|
2016-05-31 14:00:39 -04:00
|
|
|
expression: {__symbolic: 'reference', module: 'angular2/core', name: 'Component'},
|
2016-05-26 13:45:37 -04:00
|
|
|
arguments: [{
|
|
|
|
selector: 'my-app',
|
|
|
|
template: `
|
2016-03-22 20:11:42 -04:00
|
|
|
<h2>My Heroes</h2>
|
|
|
|
<ul class="heroes">
|
|
|
|
<li *ngFor="#hero of heroes"
|
|
|
|
(click)="onSelect(hero)"
|
|
|
|
[class.selected]="hero === selectedHero">
|
|
|
|
<span class="badge">{{hero.id | lowercase}}</span> {{hero.name | uppercase}}
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
<my-hero-detail [hero]="selectedHero"></my-hero-detail>
|
|
|
|
`,
|
2016-05-26 13:45:37 -04:00
|
|
|
directives: [
|
|
|
|
{
|
|
|
|
__symbolic: 'reference',
|
2016-05-31 14:00:39 -04:00
|
|
|
module: './hero-detail.component',
|
2016-05-26 13:45:37 -04:00
|
|
|
name: 'HeroDetailComponent',
|
|
|
|
},
|
2016-05-31 14:00:39 -04:00
|
|
|
{__symbolic: 'reference', module: 'angular2/common', name: 'NgFor'}
|
2016-05-26 13:45:37 -04:00
|
|
|
],
|
2016-05-31 14:00:39 -04:00
|
|
|
providers: [{__symbolic: 'reference', module: './hero.service', default: true}],
|
2016-05-26 13:45:37 -04:00
|
|
|
pipes: [
|
2016-05-31 14:00:39 -04:00
|
|
|
{__symbolic: 'reference', module: 'angular2/common', name: 'LowerCasePipe'},
|
|
|
|
{__symbolic: 'reference', module: 'angular2/common', name: 'UpperCasePipe'}
|
2016-03-22 20:11:42 -04:00
|
|
|
]
|
2016-05-26 13:45:37 -04:00
|
|
|
}]
|
|
|
|
}],
|
2016-03-22 20:11:42 -04:00
|
|
|
members: {
|
2016-05-26 13:45:37 -04:00
|
|
|
__ctor__: [{
|
|
|
|
__symbolic: 'constructor',
|
2016-05-31 14:00:39 -04:00
|
|
|
parameters: [{__symbolic: 'reference', module: './hero.service', default: true}]
|
2016-05-26 13:45:37 -04:00
|
|
|
}],
|
2016-05-04 12:11:04 -04:00
|
|
|
onSelect: [{__symbolic: 'method'}],
|
|
|
|
ngOnInit: [{__symbolic: 'method'}],
|
|
|
|
getHeroes: [{__symbolic: 'method'}]
|
2016-03-22 20:11:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return the values of exported variables', () => {
|
|
|
|
const sourceFile = program.getSourceFile('/app/mock-heroes.ts');
|
2016-05-31 14:00:39 -04:00
|
|
|
const metadata = collector.getMetadata(sourceFile);
|
2016-03-22 20:11:42 -04:00
|
|
|
expect(metadata).toEqual({
|
|
|
|
__symbolic: 'module',
|
2016-12-14 18:28:51 -05:00
|
|
|
version: 3,
|
2016-03-22 20:11:42 -04:00
|
|
|
metadata: {
|
|
|
|
HEROES: [
|
2016-12-12 13:49:17 -05:00
|
|
|
{'id': 11, 'name': 'Mr. Nice', '$quoted$': ['id', 'name']},
|
|
|
|
{'id': 12, 'name': 'Narco', '$quoted$': ['id', 'name']},
|
|
|
|
{'id': 13, 'name': 'Bombasto', '$quoted$': ['id', 'name']},
|
|
|
|
{'id': 14, 'name': 'Celeritas', '$quoted$': ['id', 'name']},
|
|
|
|
{'id': 15, 'name': 'Magneta', '$quoted$': ['id', 'name']},
|
|
|
|
{'id': 16, 'name': 'RubberMan', '$quoted$': ['id', 'name']},
|
|
|
|
{'id': 17, 'name': 'Dynama', '$quoted$': ['id', 'name']},
|
|
|
|
{'id': 18, 'name': 'Dr IQ', '$quoted$': ['id', 'name']},
|
|
|
|
{'id': 19, 'name': 'Magma', '$quoted$': ['id', 'name']},
|
|
|
|
{'id': 20, 'name': 'Tornado', '$quoted$': ['id', 'name']}
|
2016-03-22 20:11:42 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
let casesFile: ts.SourceFile;
|
|
|
|
let casesMetadata: ModuleMetadata;
|
2016-03-25 13:16:06 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
casesFile = program.getSourceFile('/app/cases-data.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
casesMetadata = collector.getMetadata(casesFile) !;
|
2016-03-25 13:16:06 -04:00
|
|
|
});
|
|
|
|
|
2016-05-31 14:00:39 -04:00
|
|
|
it('should provide any reference for an any ctor parameter type', () => {
|
2016-03-25 13:16:06 -04:00
|
|
|
const casesAny = <ClassMetadata>casesMetadata.metadata['CaseAny'];
|
2016-03-22 20:11:42 -04:00
|
|
|
expect(casesAny).toBeTruthy();
|
2017-07-18 15:52:48 -04:00
|
|
|
const ctorData = casesAny.members !['__ctor__'];
|
2016-05-31 14:00:39 -04:00
|
|
|
expect(ctorData).toEqual(
|
|
|
|
[{__symbolic: 'constructor', parameters: [{__symbolic: 'reference', name: 'any'}]}]);
|
2016-03-22 20:11:42 -04:00
|
|
|
});
|
2016-03-25 13:16:06 -04:00
|
|
|
|
2016-04-26 00:29:06 -04:00
|
|
|
it('should record annotations on set and get declarations', () => {
|
2016-03-25 13:16:06 -04:00
|
|
|
const propertyData = {
|
2016-05-26 13:45:37 -04:00
|
|
|
name: [{
|
|
|
|
__symbolic: 'property',
|
|
|
|
decorators: [{
|
|
|
|
__symbolic: 'call',
|
|
|
|
expression: {__symbolic: 'reference', module: 'angular2/core', name: 'Input'},
|
|
|
|
arguments: ['firstName']
|
|
|
|
}]
|
|
|
|
}]
|
2016-03-25 13:16:06 -04:00
|
|
|
};
|
|
|
|
const caseGetProp = <ClassMetadata>casesMetadata.metadata['GetProp'];
|
|
|
|
expect(caseGetProp.members).toEqual(propertyData);
|
|
|
|
const caseSetProp = <ClassMetadata>casesMetadata.metadata['SetProp'];
|
|
|
|
expect(caseSetProp.members).toEqual(propertyData);
|
|
|
|
const caseFullProp = <ClassMetadata>casesMetadata.metadata['FullProp'];
|
|
|
|
expect(caseFullProp.members).toEqual(propertyData);
|
|
|
|
});
|
2016-05-31 14:00:39 -04:00
|
|
|
|
|
|
|
it('should record references to parameterized types', () => {
|
|
|
|
const casesForIn = <ClassMetadata>casesMetadata.metadata['NgFor'];
|
|
|
|
expect(casesForIn).toEqual({
|
|
|
|
__symbolic: 'class',
|
|
|
|
decorators: [{
|
|
|
|
__symbolic: 'call',
|
|
|
|
expression: {__symbolic: 'reference', module: 'angular2/core', name: 'Injectable'}
|
|
|
|
}],
|
|
|
|
members: {
|
|
|
|
__ctor__: [{
|
|
|
|
__symbolic: 'constructor',
|
|
|
|
parameters: [{
|
|
|
|
__symbolic: 'reference',
|
|
|
|
name: 'ClassReference',
|
|
|
|
arguments: [{__symbolic: 'reference', name: 'NgForRow'}]
|
|
|
|
}]
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report errors for destructured imports', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const unsupported1 = program.getSourceFile('/unsupported-1.ts');
|
|
|
|
const metadata = collector.getMetadata(unsupported1);
|
2016-05-31 14:00:39 -04:00
|
|
|
expect(metadata).toEqual({
|
|
|
|
__symbolic: 'module',
|
2016-12-14 18:28:51 -05:00
|
|
|
version: 3,
|
2016-05-31 14:00:39 -04:00
|
|
|
metadata: {
|
2016-06-09 16:23:29 -04:00
|
|
|
a: {__symbolic: 'error', message: 'Destructuring not supported', line: 1, character: 16},
|
2016-08-22 20:37:48 -04:00
|
|
|
b: {__symbolic: 'error', message: 'Destructuring not supported', line: 1, character: 19},
|
2016-06-09 16:23:29 -04:00
|
|
|
c: {__symbolic: 'error', message: 'Destructuring not supported', line: 2, character: 16},
|
2016-08-22 20:37:48 -04:00
|
|
|
d: {__symbolic: 'error', message: 'Destructuring not supported', line: 2, character: 19},
|
|
|
|
e: {__symbolic: 'error', message: 'Variable not initialized', line: 3, character: 15}
|
2016-05-31 14:00:39 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-06-09 16:23:29 -04:00
|
|
|
it('should report an error for references to unexpected types', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const unsupported1 = program.getSourceFile('/unsupported-2.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(unsupported1) !;
|
2016-11-12 08:08:58 -05:00
|
|
|
const barClass = <ClassMetadata>metadata.metadata['Bar'];
|
2017-07-18 15:52:48 -04:00
|
|
|
const ctor = <ConstructorMetadata>barClass.members !['__ctor__'][0];
|
|
|
|
const parameter = ctor.parameters ![0];
|
2016-06-03 18:43:09 -04:00
|
|
|
expect(parameter).toEqual({
|
|
|
|
__symbolic: 'error',
|
2016-06-09 16:23:29 -04:00
|
|
|
message: 'Reference to non-exported class',
|
2016-08-22 20:37:48 -04:00
|
|
|
line: 3,
|
|
|
|
character: 4,
|
2016-06-09 16:23:29 -04:00
|
|
|
context: {className: 'Foo'}
|
2016-06-03 18:43:09 -04:00
|
|
|
});
|
2016-05-31 14:00:39 -04:00
|
|
|
});
|
2016-06-09 16:23:29 -04:00
|
|
|
|
|
|
|
it('should be able to handle import star type references', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const importStar = program.getSourceFile('/import-star.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(importStar) !;
|
2016-11-12 08:08:58 -05:00
|
|
|
const someClass = <ClassMetadata>metadata.metadata['SomeClass'];
|
2017-07-18 15:52:48 -04:00
|
|
|
const ctor = <ConstructorMetadata>someClass.members !['__ctor__'][0];
|
2016-11-12 08:08:58 -05:00
|
|
|
const parameters = ctor.parameters;
|
2016-06-09 16:23:29 -04:00
|
|
|
expect(parameters).toEqual([
|
|
|
|
{__symbolic: 'reference', module: 'angular2/common', name: 'NgFor'}
|
|
|
|
]);
|
|
|
|
});
|
2016-06-13 18:56:51 -04:00
|
|
|
|
2016-11-17 12:52:38 -05:00
|
|
|
it('should record all exported classes', () => {
|
|
|
|
const sourceFile = program.getSourceFile('/exported-classes.ts');
|
|
|
|
const metadata = collector.getMetadata(sourceFile);
|
|
|
|
expect(metadata).toEqual({
|
|
|
|
__symbolic: 'module',
|
2016-12-14 18:28:51 -05:00
|
|
|
version: 3,
|
2016-11-17 12:52:38 -05:00
|
|
|
metadata: {
|
|
|
|
SimpleClass: {__symbolic: 'class'},
|
|
|
|
AbstractClass: {__symbolic: 'class'},
|
|
|
|
DeclaredClass: {__symbolic: 'class'}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-06-13 18:56:51 -04:00
|
|
|
it('should be able to record functions', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const exportedFunctions = program.getSourceFile('/exported-functions.ts');
|
|
|
|
const metadata = collector.getMetadata(exportedFunctions);
|
2016-06-13 18:56:51 -04:00
|
|
|
expect(metadata).toEqual({
|
|
|
|
__symbolic: 'module',
|
2016-12-14 18:28:51 -05:00
|
|
|
version: 3,
|
2016-06-13 18:56:51 -04:00
|
|
|
metadata: {
|
|
|
|
one: {
|
|
|
|
__symbolic: 'function',
|
|
|
|
parameters: ['a', 'b', 'c'],
|
|
|
|
value: {
|
|
|
|
a: {__symbolic: 'reference', name: 'a'},
|
|
|
|
b: {__symbolic: 'reference', name: 'b'},
|
|
|
|
c: {__symbolic: 'reference', name: 'c'}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
two: {
|
|
|
|
__symbolic: 'function',
|
|
|
|
parameters: ['a', 'b', 'c'],
|
|
|
|
value: {
|
|
|
|
a: {__symbolic: 'reference', name: 'a'},
|
|
|
|
b: {__symbolic: 'reference', name: 'b'},
|
|
|
|
c: {__symbolic: 'reference', name: 'c'}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
three: {
|
|
|
|
__symbolic: 'function',
|
|
|
|
parameters: ['a', 'b', 'c'],
|
|
|
|
value: [
|
|
|
|
{__symbolic: 'reference', name: 'a'}, {__symbolic: 'reference', name: 'b'},
|
|
|
|
{__symbolic: 'reference', name: 'c'}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
supportsState: {
|
|
|
|
__symbolic: 'function',
|
|
|
|
parameters: [],
|
|
|
|
value: {
|
|
|
|
__symbolic: 'pre',
|
|
|
|
operator: '!',
|
|
|
|
operand: {
|
|
|
|
__symbolic: 'pre',
|
|
|
|
operator: '!',
|
|
|
|
operand: {
|
|
|
|
__symbolic: 'select',
|
|
|
|
expression: {
|
|
|
|
__symbolic: 'select',
|
|
|
|
expression: {__symbolic: 'reference', name: 'window'},
|
|
|
|
member: 'history'
|
|
|
|
},
|
|
|
|
member: 'pushState'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-17 12:52:38 -05:00
|
|
|
},
|
|
|
|
complexFn: {__symbolic: 'function'},
|
|
|
|
declaredFn: {__symbolic: 'function'}
|
2016-06-13 18:56:51 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to handle import star type references', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const importStar = program.getSourceFile('/import-star.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(importStar) !;
|
2016-11-12 08:08:58 -05:00
|
|
|
const someClass = <ClassMetadata>metadata.metadata['SomeClass'];
|
2017-07-18 15:52:48 -04:00
|
|
|
const ctor = <ConstructorMetadata>someClass.members !['__ctor__'][0];
|
2016-11-12 08:08:58 -05:00
|
|
|
const parameters = ctor.parameters;
|
2016-06-13 18:56:51 -04:00
|
|
|
expect(parameters).toEqual([
|
|
|
|
{__symbolic: 'reference', module: 'angular2/common', name: 'NgFor'}
|
|
|
|
]);
|
|
|
|
});
|
2016-07-11 14:50:33 -04:00
|
|
|
|
|
|
|
it('should be able to collect the value of an enum', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const enumSource = program.getSourceFile('/exported-enum.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(enumSource) !;
|
2016-11-12 08:08:58 -05:00
|
|
|
const someEnum: any = metadata.metadata['SomeEnum'];
|
2016-07-11 14:50:33 -04:00
|
|
|
expect(someEnum).toEqual({A: 0, B: 1, C: 100, D: 101});
|
|
|
|
});
|
|
|
|
|
2016-08-22 13:26:22 -04:00
|
|
|
it('should ignore a non-export enum', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const enumSource = program.getSourceFile('/private-enum.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(enumSource) !;
|
2016-11-12 08:08:58 -05:00
|
|
|
const publicEnum: any = metadata.metadata['PublicEnum'];
|
|
|
|
const privateEnum: any = metadata.metadata['PrivateEnum'];
|
2016-08-22 13:26:22 -04:00
|
|
|
expect(publicEnum).toEqual({a: 0, b: 1, c: 2});
|
|
|
|
expect(privateEnum).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
2016-07-11 14:50:33 -04:00
|
|
|
it('should be able to collect enums initialized from consts', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const enumSource = program.getSourceFile('/exported-enum.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(enumSource) !;
|
2016-11-12 08:08:58 -05:00
|
|
|
const complexEnum: any = metadata.metadata['ComplexEnum'];
|
2016-07-11 14:50:33 -04:00
|
|
|
expect(complexEnum).toEqual({
|
|
|
|
A: 0,
|
|
|
|
B: 1,
|
|
|
|
C: 30,
|
|
|
|
D: 40,
|
|
|
|
E: {__symbolic: 'reference', module: './exported-consts', name: 'constValue'}
|
|
|
|
});
|
|
|
|
});
|
2016-07-26 13:18:35 -04:00
|
|
|
|
|
|
|
it('should be able to collect a simple static method', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const staticSource = program.getSourceFile('/static-method.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(staticSource) !;
|
2016-07-26 13:18:35 -04:00
|
|
|
expect(metadata).toBeDefined();
|
2016-11-12 08:08:58 -05:00
|
|
|
const classData = <ClassMetadata>metadata.metadata['MyModule'];
|
2016-07-26 13:18:35 -04:00
|
|
|
expect(classData).toBeDefined();
|
|
|
|
expect(classData.statics).toEqual({
|
|
|
|
with: {
|
|
|
|
__symbolic: 'function',
|
|
|
|
parameters: ['comp'],
|
|
|
|
value: [
|
|
|
|
{__symbolic: 'reference', name: 'MyModule'},
|
|
|
|
{provider: 'a', useValue: {__symbolic: 'reference', name: 'comp'}}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to collect a call to a static method', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const staticSource = program.getSourceFile('/static-method-call.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(staticSource) !;
|
2016-07-26 13:18:35 -04:00
|
|
|
expect(metadata).toBeDefined();
|
2016-11-12 08:08:58 -05:00
|
|
|
const classData = <ClassMetadata>metadata.metadata['Foo'];
|
2016-07-26 13:18:35 -04:00
|
|
|
expect(classData).toBeDefined();
|
|
|
|
expect(classData.decorators).toEqual([{
|
|
|
|
__symbolic: 'call',
|
|
|
|
expression: {__symbolic: 'reference', module: 'angular2/core', name: 'Component'},
|
|
|
|
arguments: [{
|
|
|
|
providers: {
|
|
|
|
__symbolic: 'call',
|
|
|
|
expression: {
|
|
|
|
__symbolic: 'select',
|
2016-08-30 21:07:40 -04:00
|
|
|
expression: {__symbolic: 'reference', module: './static-method', name: 'MyModule'},
|
2016-07-26 13:18:35 -04:00
|
|
|
member: 'with'
|
|
|
|
},
|
|
|
|
arguments: ['a']
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}]);
|
|
|
|
});
|
2016-07-27 22:26:59 -04:00
|
|
|
|
|
|
|
it('should be able to collect a static field', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const staticSource = program.getSourceFile('/static-field.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(staticSource) !;
|
2016-07-27 22:26:59 -04:00
|
|
|
expect(metadata).toBeDefined();
|
2016-11-12 08:08:58 -05:00
|
|
|
const classData = <ClassMetadata>metadata.metadata['MyModule'];
|
2016-07-27 22:26:59 -04:00
|
|
|
expect(classData).toBeDefined();
|
|
|
|
expect(classData.statics).toEqual({VALUE: 'Some string'});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to collect a reference to a static field', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const staticSource = program.getSourceFile('/static-field-reference.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(staticSource) !;
|
2016-07-27 22:26:59 -04:00
|
|
|
expect(metadata).toBeDefined();
|
2016-11-12 08:08:58 -05:00
|
|
|
const classData = <ClassMetadata>metadata.metadata['Foo'];
|
2016-07-27 22:26:59 -04:00
|
|
|
expect(classData).toBeDefined();
|
|
|
|
expect(classData.decorators).toEqual([{
|
|
|
|
__symbolic: 'call',
|
|
|
|
expression: {__symbolic: 'reference', module: 'angular2/core', name: 'Component'},
|
|
|
|
arguments: [{
|
|
|
|
providers: [{
|
|
|
|
provide: 'a',
|
|
|
|
useValue: {
|
|
|
|
__symbolic: 'select',
|
2016-08-30 21:07:40 -04:00
|
|
|
expression: {__symbolic: 'reference', module: './static-field', name: 'MyModule'},
|
2016-07-27 22:26:59 -04:00
|
|
|
member: 'VALUE'
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}]
|
|
|
|
}]);
|
|
|
|
});
|
2016-07-28 20:32:29 -04:00
|
|
|
|
|
|
|
it('should be able to collect a method with a conditional expression', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const source = program.getSourceFile('/static-method-with-if.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(source) !;
|
2016-07-28 20:32:29 -04:00
|
|
|
expect(metadata).toBeDefined();
|
2016-11-12 08:08:58 -05:00
|
|
|
const classData = <ClassMetadata>metadata.metadata['MyModule'];
|
2016-07-28 20:32:29 -04:00
|
|
|
expect(classData).toBeDefined();
|
|
|
|
expect(classData.statics).toEqual({
|
|
|
|
with: {
|
|
|
|
__symbolic: 'function',
|
|
|
|
parameters: ['cond'],
|
|
|
|
value: [
|
|
|
|
{__symbolic: 'reference', name: 'MyModule'}, {
|
|
|
|
provider: 'a',
|
|
|
|
useValue: {
|
|
|
|
__symbolic: 'if',
|
|
|
|
condition: {__symbolic: 'reference', name: 'cond'},
|
|
|
|
thenExpression: '1',
|
|
|
|
elseExpression: '2'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-07-29 12:10:45 -04:00
|
|
|
|
|
|
|
it('should be able to collect a method with a default parameter', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const source = program.getSourceFile('/static-method-with-default.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(source) !;
|
2016-07-29 12:10:45 -04:00
|
|
|
expect(metadata).toBeDefined();
|
2016-11-12 08:08:58 -05:00
|
|
|
const classData = <ClassMetadata>metadata.metadata['MyModule'];
|
2016-07-29 12:10:45 -04:00
|
|
|
expect(classData).toBeDefined();
|
|
|
|
expect(classData.statics).toEqual({
|
|
|
|
with: {
|
|
|
|
__symbolic: 'function',
|
|
|
|
parameters: ['comp', 'foo', 'bar'],
|
|
|
|
defaults: [undefined, true, false],
|
|
|
|
value: [
|
|
|
|
{__symbolic: 'reference', name: 'MyModule'}, {
|
|
|
|
__symbolic: 'if',
|
|
|
|
condition: {__symbolic: 'reference', name: 'foo'},
|
|
|
|
thenExpression: {provider: 'a', useValue: {__symbolic: 'reference', name: 'comp'}},
|
|
|
|
elseExpression: {provider: 'b', useValue: {__symbolic: 'reference', name: 'comp'}}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__symbolic: 'if',
|
|
|
|
condition: {__symbolic: 'reference', name: 'bar'},
|
|
|
|
thenExpression: {provider: 'c', useValue: {__symbolic: 'reference', name: 'comp'}},
|
|
|
|
elseExpression: {provider: 'd', useValue: {__symbolic: 'reference', name: 'comp'}}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-08-02 14:45:14 -04:00
|
|
|
|
|
|
|
it('should be able to collect re-exported symbols', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const source = program.getSourceFile('/re-exports.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(source) !;
|
2016-08-02 14:45:14 -04:00
|
|
|
expect(metadata.exports).toEqual([
|
|
|
|
{from: './static-field', export: ['MyModule']},
|
2016-08-30 21:07:40 -04:00
|
|
|
{from: './static-field-reference', export: [{name: 'Foo', as: 'OtherModule'}]},
|
2016-08-02 14:45:14 -04:00
|
|
|
{from: 'angular2/core'}
|
|
|
|
]);
|
|
|
|
});
|
2016-08-02 17:38:31 -04:00
|
|
|
|
2016-12-16 18:33:47 -05:00
|
|
|
it('should be able to collect a export as symbol', () => {
|
|
|
|
const source = program.getSourceFile('export-as.d.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(source) !;
|
2016-12-16 18:33:47 -05:00
|
|
|
expect(metadata.metadata).toEqual({SomeFunction: {__symbolic: 'function'}});
|
|
|
|
});
|
|
|
|
|
2016-12-14 14:37:01 -05:00
|
|
|
it('should be able to collect exports with no module specifier', () => {
|
|
|
|
const source = program.getSourceFile('/re-exports-2.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(source) !;
|
2016-12-14 14:37:01 -05:00
|
|
|
expect(metadata.metadata).toEqual({
|
2016-12-16 18:33:47 -05:00
|
|
|
MyClass: Object({__symbolic: 'class'}),
|
2016-12-14 14:37:01 -05:00
|
|
|
OtherModule: {__symbolic: 'reference', module: './static-field-reference', name: 'Foo'},
|
2016-12-16 18:33:47 -05:00
|
|
|
MyOtherModule: {__symbolic: 'reference', module: './static-field', name: 'MyModule'}
|
2016-12-14 14:37:01 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-02 17:38:31 -04:00
|
|
|
it('should collect an error symbol if collecting a reference to a non-exported symbol', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const source = program.getSourceFile('/local-symbol-ref.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(source) !;
|
2016-08-02 17:38:31 -04:00
|
|
|
expect(metadata.metadata).toEqual({
|
|
|
|
REQUIRED_VALIDATOR: {
|
|
|
|
__symbolic: 'error',
|
|
|
|
message: 'Reference to a local symbol',
|
|
|
|
line: 3,
|
2016-08-22 20:37:48 -04:00
|
|
|
character: 8,
|
2016-08-02 17:38:31 -04:00
|
|
|
context: {name: 'REQUIRED'}
|
|
|
|
},
|
|
|
|
SomeComponent: {
|
|
|
|
__symbolic: 'class',
|
|
|
|
decorators: [{
|
|
|
|
__symbolic: 'call',
|
|
|
|
expression: {__symbolic: 'reference', module: 'angular2/core', name: 'Component'},
|
|
|
|
arguments: [{providers: [{__symbolic: 'reference', name: 'REQUIRED_VALIDATOR'}]}]
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-08-22 20:37:48 -04:00
|
|
|
|
|
|
|
it('should collect an error symbol if collecting a reference to a non-exported function', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const source = program.getSourceFile('/local-function-ref.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(source) !;
|
2016-08-22 20:37:48 -04:00
|
|
|
expect(metadata.metadata).toEqual({
|
|
|
|
REQUIRED_VALIDATOR: {
|
|
|
|
__symbolic: 'error',
|
|
|
|
message: 'Reference to a non-exported function',
|
|
|
|
line: 3,
|
|
|
|
character: 13,
|
|
|
|
context: {name: 'required'}
|
|
|
|
},
|
|
|
|
SomeComponent: {
|
|
|
|
__symbolic: 'class',
|
|
|
|
decorators: [{
|
|
|
|
__symbolic: 'call',
|
|
|
|
expression: {__symbolic: 'reference', module: 'angular2/core', name: 'Component'},
|
|
|
|
arguments: [{providers: [{__symbolic: 'reference', name: 'REQUIRED_VALIDATOR'}]}]
|
|
|
|
}]
|
|
|
|
}
|
2016-10-23 16:52:57 -04:00
|
|
|
});
|
2016-08-22 20:37:48 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should collect an error for a simple function that references a local variable', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const source = program.getSourceFile('/local-symbol-ref-func.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(source) !;
|
2016-08-22 20:37:48 -04:00
|
|
|
expect(metadata.metadata).toEqual({
|
|
|
|
foo: {
|
|
|
|
__symbolic: 'function',
|
|
|
|
parameters: ['index'],
|
|
|
|
value: {
|
|
|
|
__symbolic: 'error',
|
|
|
|
message: 'Reference to a local symbol',
|
|
|
|
line: 1,
|
|
|
|
character: 8,
|
|
|
|
context: {name: 'localSymbol'}
|
|
|
|
}
|
|
|
|
}
|
2016-10-23 16:52:57 -04:00
|
|
|
});
|
2016-08-22 20:37:48 -04:00
|
|
|
});
|
|
|
|
|
2017-03-15 12:24:56 -04:00
|
|
|
it('should collect any for interface parameter reference', () => {
|
|
|
|
const source = program.getSourceFile('/interface-reference.ts');
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(source) !;
|
2017-03-15 12:24:56 -04:00
|
|
|
expect((metadata.metadata['SomeClass'] as ClassMetadata).members).toEqual({
|
|
|
|
__ctor__: [{
|
|
|
|
__symbolic: 'constructor',
|
|
|
|
parameterDecorators: [[{
|
|
|
|
__symbolic: 'call',
|
|
|
|
expression: {__symbolic: 'reference', module: 'angular2/core', name: 'Inject'},
|
|
|
|
arguments: ['a']
|
|
|
|
}]],
|
|
|
|
parameters: [{__symbolic: 'reference', name: 'any'}]
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-05-22 14:19:09 -04:00
|
|
|
describe('with interpolations', () => {
|
|
|
|
function e(expr: string, prefix?: string) {
|
2017-07-13 19:16:56 -04:00
|
|
|
const metadata = collectSource(`${prefix || ''} export let value = ${expr};`);
|
2017-05-22 14:19:09 -04:00
|
|
|
return expect(metadata.metadata['value']);
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should be able to collect a raw interpolated string',
|
|
|
|
() => { e('`simple value`').toBe('simple value'); });
|
|
|
|
|
|
|
|
it('should be able to interpolate a single value',
|
|
|
|
() => { e('`${foo}`', 'const foo = "foo value"').toBe('foo value'); });
|
|
|
|
|
|
|
|
it('should be able to interpolate multiple values', () => {
|
|
|
|
e('`foo:${foo}, bar:${bar}, end`', 'const foo = "foo"; const bar = "bar";')
|
|
|
|
.toBe('foo:foo, bar:bar, end');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to interpolate with an imported reference', () => {
|
|
|
|
e('`external:${external}`', 'import {external} from "./external";').toEqual({
|
|
|
|
__symbolic: 'binop',
|
|
|
|
operator: '+',
|
|
|
|
left: 'external:',
|
|
|
|
right: {
|
|
|
|
__symbolic: 'reference',
|
|
|
|
module: './external',
|
|
|
|
name: 'external',
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should simplify a redundant template', () => {
|
|
|
|
e('`${external}`', 'import {external} from "./external";')
|
|
|
|
.toEqual({__symbolic: 'reference', module: './external', name: 'external'});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to collect complex template with imported references', () => {
|
|
|
|
e('`foo:${foo}, bar:${bar}, end`', 'import {foo, bar} from "./external";').toEqual({
|
|
|
|
__symbolic: 'binop',
|
|
|
|
operator: '+',
|
|
|
|
left: {
|
|
|
|
__symbolic: 'binop',
|
|
|
|
operator: '+',
|
|
|
|
left: {
|
|
|
|
__symbolic: 'binop',
|
|
|
|
operator: '+',
|
|
|
|
left: {
|
|
|
|
__symbolic: 'binop',
|
|
|
|
operator: '+',
|
|
|
|
left: 'foo:',
|
|
|
|
right: {__symbolic: 'reference', module: './external', name: 'foo'}
|
|
|
|
},
|
|
|
|
right: ', bar:'
|
|
|
|
},
|
|
|
|
right: {__symbolic: 'reference', module: './external', name: 'bar'}
|
|
|
|
},
|
|
|
|
right: ', end'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should reject a tagged literal', () => {
|
|
|
|
e('tag`some value`').toEqual({
|
|
|
|
__symbolic: 'error',
|
|
|
|
message: 'Tagged template expressions are not supported in metadata',
|
|
|
|
line: 0,
|
|
|
|
character: 20
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-20 20:34:44 -04:00
|
|
|
it('should ignore |null or |undefined in type expressions', () => {
|
2017-07-13 19:16:56 -04:00
|
|
|
const metadata = collectSource(`
|
2017-04-20 20:34:44 -04:00
|
|
|
import {Foo} from './foo';
|
|
|
|
export class SomeClass {
|
|
|
|
constructor (a: Foo, b: Foo | null, c: Foo | undefined, d: Foo | undefined | null, e: Foo | undefined | null | Foo) {}
|
|
|
|
}
|
2017-07-13 19:16:56 -04:00
|
|
|
`);
|
2017-04-20 20:34:44 -04:00
|
|
|
expect((metadata.metadata['SomeClass'] as ClassMetadata).members).toEqual({
|
|
|
|
__ctor__: [{
|
|
|
|
__symbolic: 'constructor',
|
|
|
|
parameters: [
|
|
|
|
{__symbolic: 'reference', module: './foo', name: 'Foo'},
|
|
|
|
{__symbolic: 'reference', module: './foo', name: 'Foo'},
|
|
|
|
{__symbolic: 'reference', module: './foo', name: 'Foo'},
|
|
|
|
{__symbolic: 'reference', module: './foo', name: 'Foo'},
|
|
|
|
{__symbolic: 'reference', module: './foo', name: 'Foo'}
|
|
|
|
]
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-07-13 18:17:16 -04:00
|
|
|
it('should treat exported class expressions as a class', () => {
|
|
|
|
const source = ts.createSourceFile(
|
|
|
|
'', `
|
|
|
|
export const InjectionToken: {new<T>(desc: string): InjectionToken<T>;} = class extends OpaqueToken {
|
|
|
|
constructor(desc: string) {
|
|
|
|
super(desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
toString(): string { return \`InjectionToken ${this._desc}\`; }
|
|
|
|
} as any;`,
|
|
|
|
ts.ScriptTarget.Latest, true);
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(source) !;
|
2017-07-13 18:17:16 -04:00
|
|
|
expect(metadata.metadata).toEqual({InjectionToken: {__symbolic: 'class'}});
|
|
|
|
});
|
|
|
|
|
2016-08-22 20:37:48 -04:00
|
|
|
describe('in strict mode', () => {
|
|
|
|
it('should throw if an error symbol is collecting a reference to a non-exported symbol', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const source = program.getSourceFile('/local-symbol-ref.ts');
|
2016-08-22 20:37:48 -04:00
|
|
|
expect(() => collector.getMetadata(source, true)).toThrowError(/Reference to a local symbol/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if an error if collecting a reference to a non-exported function', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const source = program.getSourceFile('/local-function-ref.ts');
|
2016-08-22 20:37:48 -04:00
|
|
|
expect(() => collector.getMetadata(source, true))
|
|
|
|
.toThrowError(/Reference to a non-exported function/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw for references to unexpected types', () => {
|
2017-03-01 17:45:00 -05:00
|
|
|
const unsupported2 = program.getSourceFile('/unsupported-2.ts');
|
|
|
|
expect(() => collector.getMetadata(unsupported2, true))
|
2016-08-22 20:37:48 -04:00
|
|
|
.toThrowError(/Reference to non-exported class/);
|
|
|
|
});
|
2017-03-01 17:45:00 -05:00
|
|
|
|
|
|
|
it('should throw for errors in a static method', () => {
|
|
|
|
const unsupported3 = program.getSourceFile('/unsupported-3.ts');
|
|
|
|
expect(() => collector.getMetadata(unsupported3, true))
|
|
|
|
.toThrowError(/Reference to a non-exported class/);
|
|
|
|
});
|
2016-10-23 16:52:57 -04:00
|
|
|
});
|
2016-11-10 14:58:55 -05:00
|
|
|
|
|
|
|
describe('with invalid input', () => {
|
|
|
|
it('should not throw with a class with no name', () => {
|
|
|
|
const fileName = '/invalid-class.ts';
|
|
|
|
override(fileName, 'export class');
|
2016-11-12 08:08:58 -05:00
|
|
|
const invalidClass = program.getSourceFile(fileName);
|
2016-11-10 14:58:55 -05:00
|
|
|
expect(() => collector.getMetadata(invalidClass)).not.toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not throw with a function with no name', () => {
|
|
|
|
const fileName = '/invalid-function.ts';
|
|
|
|
override(fileName, 'export function');
|
2016-11-12 08:08:58 -05:00
|
|
|
const invalidFunction = program.getSourceFile(fileName);
|
2016-11-10 14:58:55 -05:00
|
|
|
expect(() => collector.getMetadata(invalidFunction)).not.toThrow();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-18 18:17:44 -05:00
|
|
|
describe('inheritance', () => {
|
|
|
|
it('should record `extends` clauses for declared classes', () => {
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(program.getSourceFile('/class-inheritance.ts')) !;
|
2016-11-18 18:17:44 -05:00
|
|
|
expect(metadata.metadata['DeclaredChildClass'])
|
|
|
|
.toEqual({__symbolic: 'class', extends: {__symbolic: 'reference', name: 'ParentClass'}});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should record `extends` clauses for classes in the same file', () => {
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(program.getSourceFile('/class-inheritance.ts')) !;
|
2016-11-18 18:17:44 -05:00
|
|
|
expect(metadata.metadata['ChildClassSameFile'])
|
|
|
|
.toEqual({__symbolic: 'class', extends: {__symbolic: 'reference', name: 'ParentClass'}});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should record `extends` clauses for classes in a different file', () => {
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(program.getSourceFile('/class-inheritance.ts')) !;
|
2016-11-18 18:17:44 -05:00
|
|
|
expect(metadata.metadata['ChildClassOtherFile']).toEqual({
|
|
|
|
__symbolic: 'class',
|
|
|
|
extends: {
|
|
|
|
__symbolic: 'reference',
|
|
|
|
module: './class-inheritance-parent',
|
|
|
|
name: 'ParentClassFromOtherFile'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-01-25 16:40:55 -05:00
|
|
|
|
|
|
|
function expectClass(entry: MetadataEntry): entry is ClassMetadata {
|
|
|
|
const result = isClassMetadata(entry);
|
|
|
|
expect(result).toBeTruthy();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should collect the correct arity for a class', () => {
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(program.getSourceFile('/class-arity.ts')) !;
|
2017-01-25 16:40:55 -05:00
|
|
|
|
|
|
|
const zero = metadata.metadata['Zero'];
|
|
|
|
if (expectClass(zero)) expect(zero.arity).toBeUndefined();
|
|
|
|
const one = metadata.metadata['One'];
|
|
|
|
if (expectClass(one)) expect(one.arity).toBe(1);
|
|
|
|
const two = metadata.metadata['Two'];
|
|
|
|
if (expectClass(two)) expect(two.arity).toBe(2);
|
|
|
|
const three = metadata.metadata['Three'];
|
|
|
|
if (expectClass(three)) expect(three.arity).toBe(3);
|
|
|
|
const nine = metadata.metadata['Nine'];
|
|
|
|
if (expectClass(nine)) expect(nine.arity).toBe(9);
|
|
|
|
});
|
2016-11-18 18:17:44 -05:00
|
|
|
});
|
|
|
|
|
2017-05-22 13:18:44 -04:00
|
|
|
describe('regerssion', () => {
|
|
|
|
it('should be able to collect a short-hand property value', () => {
|
2017-07-13 19:16:56 -04:00
|
|
|
const metadata = collectSource(`
|
2017-05-22 13:18:44 -04:00
|
|
|
const children = { f1: 1 };
|
|
|
|
export const r = [
|
|
|
|
{path: ':locale', children}
|
|
|
|
];
|
2017-06-20 13:36:04 -04:00
|
|
|
`);
|
2017-05-22 13:18:44 -04:00
|
|
|
expect(metadata.metadata).toEqual({r: [{path: ':locale', children: {f1: 1}}]});
|
|
|
|
});
|
2017-06-20 13:36:04 -04:00
|
|
|
|
|
|
|
// #17518
|
|
|
|
it('should skip a default function', () => {
|
2017-07-13 19:16:56 -04:00
|
|
|
const metadata = collectSource(`
|
2017-06-20 13:36:04 -04:00
|
|
|
export default function () {
|
|
|
|
|
|
|
|
const mainRoutes = [
|
|
|
|
{name: 'a', abstract: true, component: 'main'},
|
|
|
|
|
|
|
|
{name: 'a.welcome', url: '/welcome', component: 'welcome'}
|
|
|
|
];
|
|
|
|
|
|
|
|
return mainRoutes;
|
|
|
|
|
|
|
|
}`);
|
|
|
|
expect(metadata).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip a named default export', () => {
|
2017-07-13 19:16:56 -04:00
|
|
|
const metadata = collectSource(`
|
2017-06-20 13:36:04 -04:00
|
|
|
function mainRoutes() {
|
|
|
|
|
|
|
|
const mainRoutes = [
|
|
|
|
{name: 'a', abstract: true, component: 'main'},
|
|
|
|
|
|
|
|
{name: 'a.welcome', url: '/welcome', component: 'welcome'}
|
|
|
|
];
|
|
|
|
|
|
|
|
return mainRoutes;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
exports = foo;
|
|
|
|
`);
|
|
|
|
expect(metadata).toBeUndefined();
|
|
|
|
});
|
2017-07-13 17:55:59 -04:00
|
|
|
|
|
|
|
it('should be able to collect an invalid access expression', () => {
|
|
|
|
const source = createSource(`
|
|
|
|
import {Component} from '@angular/core';
|
|
|
|
|
|
|
|
const value = [];
|
|
|
|
@Component({
|
|
|
|
provider: [{provide: 'some token', useValue: value[]}]
|
|
|
|
})
|
|
|
|
export class MyComponent {}
|
|
|
|
`);
|
2017-07-18 15:52:48 -04:00
|
|
|
const metadata = collector.getMetadata(source) !;
|
2017-07-13 17:55:59 -04:00
|
|
|
expect(metadata.metadata.MyComponent).toEqual({
|
|
|
|
__symbolic: 'class',
|
|
|
|
decorators: [{
|
|
|
|
__symbolic: 'error',
|
|
|
|
message: 'Expression form not supported',
|
|
|
|
line: 5,
|
|
|
|
character: 55
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
2017-05-22 13:18:44 -04:00
|
|
|
});
|
|
|
|
|
2017-07-13 19:16:56 -04:00
|
|
|
describe('references', () => {
|
|
|
|
beforeEach(() => { collector = new MetadataCollector({quotedNames: true}); });
|
|
|
|
|
|
|
|
it('should record a reference to an exported field of a useValue', () => {
|
|
|
|
const metadata = collectSource(`
|
|
|
|
export var someValue = 1;
|
|
|
|
export const v = {
|
|
|
|
useValue: someValue
|
|
|
|
};
|
|
|
|
`);
|
|
|
|
expect(metadata.metadata['someValue']).toEqual(1);
|
|
|
|
expect(metadata.metadata['v']).toEqual({
|
|
|
|
useValue: {__symbolic: 'reference', name: 'someValue'}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should leave external references in place in an object literal', () => {
|
|
|
|
const metadata = collectSource(`
|
|
|
|
export const myLambda = () => [1, 2, 3];
|
|
|
|
const indirect = [{a: 1, b: 3: c: myLambda}];
|
|
|
|
export const v = {
|
|
|
|
v: {i: indirect}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
expect(metadata.metadata['v']).toEqual({
|
|
|
|
v: {i: [{a: 1, b: 3, c: {__symbolic: 'reference', name: 'myLambda'}}]}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should leave an external reference in place in an array literal', () => {
|
|
|
|
const metadata = collectSource(`
|
|
|
|
export const myLambda = () => [1, 2, 3];
|
|
|
|
const indirect = [1, 3, myLambda}];
|
|
|
|
export const v = {
|
|
|
|
v: {i: indirect}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
expect(metadata.metadata['v']).toEqual({
|
|
|
|
v: {i: [1, 3, {__symbolic: 'reference', name: 'myLambda'}]}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-07-26 17:58:07 -04:00
|
|
|
describe('substitutions', () => {
|
|
|
|
const lambdaTemp = 'lambdaTemp';
|
|
|
|
|
|
|
|
it('should be able to substitute a lambda', () => {
|
|
|
|
const source = createSource(`
|
|
|
|
const b = 1;
|
2017-08-15 10:39:54 -04:00
|
|
|
export const a = () => b;
|
2017-07-26 17:58:07 -04:00
|
|
|
`);
|
|
|
|
const metadata = collector.getMetadata(source, /* strict */ false, (value, node) => {
|
|
|
|
if (node.kind === ts.SyntaxKind.ArrowFunction) {
|
|
|
|
return {__symbolic: 'reference', name: lambdaTemp};
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
});
|
|
|
|
expect(metadata !.metadata['a']).toEqual({__symbolic: 'reference', name: lambdaTemp});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should compose substitution functions', () => {
|
|
|
|
const collector = new MetadataCollector({
|
|
|
|
substituteExpression: (value, node) => isMetadataGlobalReferenceExpression(value) &&
|
|
|
|
value.name == lambdaTemp ?
|
|
|
|
{__symbolic: 'reference', name: value.name + '2'} :
|
|
|
|
value
|
|
|
|
});
|
|
|
|
const source = createSource(`
|
|
|
|
const b = 1;
|
2017-08-15 10:39:54 -04:00
|
|
|
export const a = () => b;
|
2017-07-26 17:58:07 -04:00
|
|
|
`);
|
|
|
|
const metadata = collector.getMetadata(source, /* strict */ false, (value, node) => {
|
|
|
|
if (node.kind === ts.SyntaxKind.ArrowFunction) {
|
|
|
|
return {__symbolic: 'reference', name: lambdaTemp};
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
});
|
|
|
|
expect(metadata !.metadata['a']).toEqual({__symbolic: 'reference', name: lambdaTemp + '2'});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-10 14:58:55 -05:00
|
|
|
function override(fileName: string, content: string) {
|
|
|
|
host.overrideFile(fileName, content);
|
|
|
|
host.addFile(fileName);
|
|
|
|
program = service.getProgram();
|
|
|
|
}
|
2017-07-13 19:16:56 -04:00
|
|
|
|
|
|
|
function collectSource(content: string): ModuleMetadata {
|
|
|
|
const sourceFile = createSource(content);
|
2017-07-18 15:52:48 -04:00
|
|
|
return collector.getMetadata(sourceFile) !;
|
2017-07-13 19:16:56 -04:00
|
|
|
}
|
2016-03-22 20:11:42 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: Do not use \` in a template literal as it confuses clang-format
|
|
|
|
const FILES: Directory = {
|
|
|
|
'app': {
|
|
|
|
'app.component.ts': `
|
2016-04-26 00:29:06 -04:00
|
|
|
import {Component as MyComponent, OnInit} from 'angular2/core';
|
|
|
|
import * as common from 'angular2/common';
|
2016-03-22 20:11:42 -04:00
|
|
|
import {Hero} from './hero';
|
|
|
|
import {HeroDetailComponent} from './hero-detail.component';
|
2016-04-26 00:29:06 -04:00
|
|
|
import HeroService from './hero.service';
|
|
|
|
// thrown away
|
|
|
|
import 'angular2/core';
|
2016-05-04 12:11:04 -04:00
|
|
|
|
2016-04-26 00:29:06 -04:00
|
|
|
@MyComponent({
|
2016-03-22 20:11:42 -04:00
|
|
|
selector: 'my-app',
|
2016-05-26 13:45:37 -04:00
|
|
|
template:` +
|
|
|
|
'`' +
|
|
|
|
`
|
2016-03-22 20:11:42 -04:00
|
|
|
<h2>My Heroes</h2>
|
|
|
|
<ul class="heroes">
|
|
|
|
<li *ngFor="#hero of heroes"
|
|
|
|
(click)="onSelect(hero)"
|
|
|
|
[class.selected]="hero === selectedHero">
|
|
|
|
<span class="badge">{{hero.id | lowercase}}</span> {{hero.name | uppercase}}
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
<my-hero-detail [hero]="selectedHero"></my-hero-detail>
|
|
|
|
` +
|
2016-05-26 13:45:37 -04:00
|
|
|
'`' +
|
|
|
|
`,
|
2016-04-26 00:29:06 -04:00
|
|
|
directives: [HeroDetailComponent, common.NgFor],
|
2016-03-22 20:11:42 -04:00
|
|
|
providers: [HeroService],
|
2016-04-26 00:29:06 -04:00
|
|
|
pipes: [common.LowerCasePipe, common.UpperCasePipe]
|
2016-03-22 20:11:42 -04:00
|
|
|
})
|
|
|
|
export class AppComponent implements OnInit {
|
|
|
|
public title = 'Tour of Heroes';
|
|
|
|
public heroes: Hero[];
|
|
|
|
public selectedHero: Hero;
|
|
|
|
|
|
|
|
constructor(private _heroService: HeroService) { }
|
|
|
|
|
|
|
|
onSelect(hero: Hero) { this.selectedHero = hero; }
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.getHeroes()
|
|
|
|
}
|
|
|
|
|
|
|
|
getHeroes() {
|
2017-07-07 19:55:17 -04:00
|
|
|
this._heroService.getHeroesSlowly().then(heroes => this.heroes = heroes);
|
2016-03-22 20:11:42 -04:00
|
|
|
}
|
|
|
|
}`,
|
|
|
|
'hero.ts': `
|
|
|
|
export interface Hero {
|
|
|
|
id: number;
|
|
|
|
name: string;
|
|
|
|
}`,
|
2017-03-15 12:24:56 -04:00
|
|
|
'empty.ts': ``,
|
2016-03-22 20:11:42 -04:00
|
|
|
'hero-detail.component.ts': `
|
|
|
|
import {Component, Input} from 'angular2/core';
|
|
|
|
import {Hero} from './hero';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-hero-detail',
|
2016-05-26 13:45:37 -04:00
|
|
|
template: ` +
|
|
|
|
'`' +
|
|
|
|
`
|
2016-03-22 20:11:42 -04:00
|
|
|
<div *ngIf="hero">
|
|
|
|
<h2>{{hero.name}} details!</h2>
|
|
|
|
<div><label>id: </label>{{hero.id}}</div>
|
|
|
|
<div>
|
|
|
|
<label>name: </label>
|
|
|
|
<input [(ngModel)]="hero.name" placeholder="name"/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2016-05-26 13:45:37 -04:00
|
|
|
` +
|
|
|
|
'`' +
|
|
|
|
`,
|
2016-03-22 20:11:42 -04:00
|
|
|
})
|
|
|
|
export class HeroDetailComponent {
|
|
|
|
@Input() public hero: Hero;
|
|
|
|
}`,
|
|
|
|
'mock-heroes.ts': `
|
2016-04-26 00:29:06 -04:00
|
|
|
import {Hero as Hero} from './hero';
|
2016-03-22 20:11:42 -04:00
|
|
|
|
|
|
|
export const HEROES: Hero[] = [
|
|
|
|
{"id": 11, "name": "Mr. Nice"},
|
|
|
|
{"id": 12, "name": "Narco"},
|
|
|
|
{"id": 13, "name": "Bombasto"},
|
|
|
|
{"id": 14, "name": "Celeritas"},
|
|
|
|
{"id": 15, "name": "Magneta"},
|
|
|
|
{"id": 16, "name": "RubberMan"},
|
|
|
|
{"id": 17, "name": "Dynama"},
|
|
|
|
{"id": 18, "name": "Dr IQ"},
|
|
|
|
{"id": 19, "name": "Magma"},
|
|
|
|
{"id": 20, "name": "Tornado"}
|
|
|
|
];`,
|
2016-04-26 00:29:06 -04:00
|
|
|
'default-exporter.ts': `
|
|
|
|
let a: string;
|
|
|
|
export default a;
|
|
|
|
`,
|
2016-03-22 20:11:42 -04:00
|
|
|
'hero.service.ts': `
|
|
|
|
import {Injectable} from 'angular2/core';
|
|
|
|
import {HEROES} from './mock-heroes';
|
|
|
|
import {Hero} from './hero';
|
|
|
|
|
|
|
|
@Injectable()
|
2016-04-26 00:29:06 -04:00
|
|
|
class HeroService {
|
2016-03-22 20:11:42 -04:00
|
|
|
getHeros() {
|
|
|
|
return Promise.resolve(HEROES);
|
|
|
|
}
|
|
|
|
|
|
|
|
getHeroesSlowly() {
|
|
|
|
return new Promise<Hero[]>(resolve =>
|
|
|
|
setTimeout(()=>resolve(HEROES), 2000)); // 2 seconds
|
|
|
|
}
|
2016-04-26 00:29:06 -04:00
|
|
|
}
|
|
|
|
export default HeroService;`,
|
2016-03-22 20:11:42 -04:00
|
|
|
'cases-data.ts': `
|
2016-03-25 13:16:06 -04:00
|
|
|
import {Injectable, Input} from 'angular2/core';
|
2016-03-22 20:11:42 -04:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class CaseAny {
|
|
|
|
constructor(param: any) {}
|
|
|
|
}
|
2016-05-04 12:11:04 -04:00
|
|
|
|
2016-03-25 13:16:06 -04:00
|
|
|
@Injectable()
|
|
|
|
export class GetProp {
|
|
|
|
private _name: string;
|
|
|
|
@Input('firstName') get name(): string {
|
|
|
|
return this._name;
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 12:11:04 -04:00
|
|
|
|
2016-03-25 13:16:06 -04:00
|
|
|
@Injectable()
|
|
|
|
export class SetProp {
|
|
|
|
private _name: string;
|
|
|
|
@Input('firstName') set name(value: string) {
|
|
|
|
this._name = value;
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 12:11:04 -04:00
|
|
|
|
2016-03-25 13:16:06 -04:00
|
|
|
@Injectable()
|
|
|
|
export class FullProp {
|
|
|
|
private _name: string;
|
|
|
|
@Input('firstName') get name(): string {
|
|
|
|
return this._name;
|
|
|
|
}
|
|
|
|
set name(value: string) {
|
|
|
|
this._name = value;
|
|
|
|
}
|
|
|
|
}
|
2016-05-31 14:00:39 -04:00
|
|
|
|
|
|
|
export class ClassReference<T> { }
|
|
|
|
export class NgForRow {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class NgFor {
|
|
|
|
constructor (public ref: ClassReference<NgForRow>) {}
|
|
|
|
}
|
2016-03-22 20:11:42 -04:00
|
|
|
`,
|
2016-05-31 14:00:39 -04:00
|
|
|
'error-cases.ts': `
|
2016-04-26 00:29:06 -04:00
|
|
|
import HeroService from './hero.service';
|
2016-03-22 20:11:42 -04:00
|
|
|
|
|
|
|
export class CaseCtor {
|
|
|
|
constructor(private _heroService: HeroService) { }
|
|
|
|
}
|
|
|
|
`
|
|
|
|
},
|
|
|
|
'promise.ts': `
|
|
|
|
interface PromiseLike<T> {
|
|
|
|
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): PromiseLike<TResult>;
|
|
|
|
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => void): PromiseLike<TResult>;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Promise<T> {
|
|
|
|
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>;
|
|
|
|
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => void): Promise<TResult>;
|
|
|
|
catch(onrejected?: (reason: any) => T | PromiseLike<T>): Promise<T>;
|
|
|
|
catch(onrejected?: (reason: any) => void): Promise<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface PromiseConstructor {
|
|
|
|
prototype: Promise<any>;
|
|
|
|
new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;
|
|
|
|
reject(reason: any): Promise<void>;
|
|
|
|
reject<T>(reason: any): Promise<T>;
|
|
|
|
resolve<T>(value: T | PromiseLike<T>): Promise<T>;
|
|
|
|
resolve(): Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
declare var Promise: PromiseConstructor;
|
|
|
|
`,
|
2017-01-25 16:40:55 -05:00
|
|
|
'class-arity.ts': `
|
|
|
|
export class Zero {}
|
|
|
|
export class One<T> {}
|
|
|
|
export class Two<T, V> {}
|
|
|
|
export class Three<T1, T2, T3> {}
|
|
|
|
export class Nine<T1, T2, T3, T4, T5, T6, T7, T8, T9> {}
|
|
|
|
`,
|
2016-05-31 14:00:39 -04:00
|
|
|
'unsupported-1.ts': `
|
|
|
|
export let {a, b} = {a: 1, b: 2};
|
|
|
|
export let [c, d] = [1, 2];
|
|
|
|
export let e;
|
|
|
|
`,
|
|
|
|
'unsupported-2.ts': `
|
|
|
|
import {Injectable} from 'angular2/core';
|
2016-06-13 18:56:51 -04:00
|
|
|
|
2016-05-31 14:00:39 -04:00
|
|
|
class Foo {}
|
2016-06-13 18:56:51 -04:00
|
|
|
|
2016-05-31 14:00:39 -04:00
|
|
|
@Injectable()
|
|
|
|
export class Bar {
|
|
|
|
constructor(private f: Foo) {}
|
|
|
|
}
|
|
|
|
`,
|
2017-03-01 17:45:00 -05:00
|
|
|
'unsupported-3.ts': `
|
|
|
|
class Foo {}
|
|
|
|
|
|
|
|
export class SomeClass {
|
|
|
|
static someStatic() {
|
|
|
|
return Foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
2017-03-15 12:24:56 -04:00
|
|
|
'interface-reference.ts': `
|
|
|
|
import {Injectable, Inject} from 'angular2/core';
|
|
|
|
export interface Test {}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SomeClass {
|
|
|
|
constructor(@Inject("a") test: Test) {}
|
|
|
|
}
|
|
|
|
`,
|
2016-06-09 16:23:29 -04:00
|
|
|
'import-star.ts': `
|
|
|
|
import {Injectable} from 'angular2/core';
|
|
|
|
import * as common from 'angular2/common';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SomeClass {
|
|
|
|
constructor(private f: common.NgFor) {}
|
|
|
|
}
|
|
|
|
`,
|
2016-11-17 12:52:38 -05:00
|
|
|
'exported-classes.ts': `
|
|
|
|
export class SimpleClass {}
|
|
|
|
export abstract class AbstractClass {}
|
|
|
|
export declare class DeclaredClass {}
|
|
|
|
`,
|
2016-11-18 18:17:44 -05:00
|
|
|
'class-inheritance-parent.ts': `
|
|
|
|
export class ParentClassFromOtherFile {}
|
|
|
|
`,
|
|
|
|
'class-inheritance.ts': `
|
|
|
|
import {ParentClassFromOtherFile} from './class-inheritance-parent';
|
|
|
|
|
|
|
|
export class ParentClass {}
|
|
|
|
|
|
|
|
export declare class DeclaredChildClass extends ParentClass {}
|
|
|
|
|
|
|
|
export class ChildClassSameFile extends ParentClass {}
|
|
|
|
|
|
|
|
export class ChildClassOtherFile extends ParentClassFromOtherFile {}
|
|
|
|
`,
|
2016-06-13 18:56:51 -04:00
|
|
|
'exported-functions.ts': `
|
|
|
|
export function one(a: string, b: string, c: string) {
|
|
|
|
return {a: a, b: b, c: c};
|
|
|
|
}
|
|
|
|
export function two(a: string, b: string, c: string) {
|
|
|
|
return {a, b, c};
|
|
|
|
}
|
|
|
|
export function three({a, b, c}: {a: string, b: string, c: string}) {
|
|
|
|
return [a, b, c];
|
|
|
|
}
|
|
|
|
export function supportsState(): boolean {
|
|
|
|
return !!window.history.pushState;
|
|
|
|
}
|
2016-11-17 12:52:38 -05:00
|
|
|
export function complexFn(x: any): boolean {
|
|
|
|
if (x) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export declare function declaredFn();
|
2016-06-13 18:56:51 -04:00
|
|
|
`,
|
2017-08-15 10:39:54 -04:00
|
|
|
'exported-type.ts': `
|
|
|
|
export type SomeType = 'a' | 'b';
|
|
|
|
`,
|
2016-07-11 14:50:33 -04:00
|
|
|
'exported-enum.ts': `
|
|
|
|
import {constValue} from './exported-consts';
|
|
|
|
|
|
|
|
export const someValue = 30;
|
|
|
|
export enum SomeEnum { A, B, C = 100, D };
|
|
|
|
export enum ComplexEnum { A, B, C = someValue, D = someValue + 10, E = constValue };
|
|
|
|
`,
|
|
|
|
'exported-consts.ts': `
|
|
|
|
export const constValue = 100;
|
|
|
|
`,
|
2016-07-26 13:18:35 -04:00
|
|
|
'static-method.ts': `
|
|
|
|
export class MyModule {
|
|
|
|
static with(comp: any): any[] {
|
|
|
|
return [
|
|
|
|
MyModule,
|
|
|
|
{ provider: 'a', useValue: comp }
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
2016-07-29 12:10:45 -04:00
|
|
|
'static-method-with-default.ts': `
|
|
|
|
export class MyModule {
|
|
|
|
static with(comp: any, foo: boolean = true, bar: boolean = false): any[] {
|
|
|
|
return [
|
|
|
|
MyModule,
|
|
|
|
foo ? { provider: 'a', useValue: comp } : {provider: 'b', useValue: comp},
|
|
|
|
bar ? { provider: 'c', useValue: comp } : {provider: 'd', useValue: comp}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
2016-07-26 13:18:35 -04:00
|
|
|
'static-method-call.ts': `
|
|
|
|
import {Component} from 'angular2/core';
|
2016-08-30 21:07:40 -04:00
|
|
|
import {MyModule} from './static-method';
|
2016-07-26 13:18:35 -04:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
providers: MyModule.with('a')
|
|
|
|
})
|
|
|
|
export class Foo { }
|
|
|
|
`,
|
2016-07-27 22:26:59 -04:00
|
|
|
'static-field.ts': `
|
|
|
|
export class MyModule {
|
|
|
|
static VALUE = 'Some string';
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
'static-field-reference.ts': `
|
|
|
|
import {Component} from 'angular2/core';
|
2016-08-30 21:07:40 -04:00
|
|
|
import {MyModule} from './static-field';
|
2016-07-27 22:26:59 -04:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
providers: [ { provide: 'a', useValue: MyModule.VALUE } ]
|
|
|
|
})
|
|
|
|
export class Foo { }
|
|
|
|
`,
|
2016-07-28 20:32:29 -04:00
|
|
|
'static-method-with-if.ts': `
|
|
|
|
export class MyModule {
|
|
|
|
static with(cond: boolean): any[] {
|
|
|
|
return [
|
|
|
|
MyModule,
|
|
|
|
{ provider: 'a', useValue: cond ? '1' : '2' }
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
2016-08-02 14:45:14 -04:00
|
|
|
're-exports.ts': `
|
|
|
|
export {MyModule} from './static-field';
|
2016-08-30 21:07:40 -04:00
|
|
|
export {Foo as OtherModule} from './static-field-reference';
|
2016-08-02 14:45:14 -04:00
|
|
|
export * from 'angular2/core';
|
|
|
|
`,
|
2016-12-14 14:37:01 -05:00
|
|
|
're-exports-2.ts': `
|
|
|
|
import {MyModule} from './static-field';
|
|
|
|
import {Foo as OtherModule} from './static-field-reference';
|
|
|
|
class MyClass {}
|
|
|
|
export {OtherModule, MyModule as MyOtherModule, MyClass};
|
|
|
|
`,
|
2016-12-16 18:33:47 -05:00
|
|
|
'export-as.d.ts': `
|
|
|
|
declare function someFunction(): void;
|
|
|
|
export { someFunction as SomeFunction };
|
|
|
|
`,
|
2016-08-02 17:38:31 -04:00
|
|
|
'local-symbol-ref.ts': `
|
|
|
|
import {Component, Validators} from 'angular2/core';
|
|
|
|
|
2016-08-22 20:37:48 -04:00
|
|
|
var REQUIRED;
|
2016-08-02 17:38:31 -04:00
|
|
|
|
|
|
|
export const REQUIRED_VALIDATOR: any = {
|
|
|
|
provide: 'SomeToken',
|
|
|
|
useValue: REQUIRED,
|
|
|
|
multi: true
|
|
|
|
};
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
providers: [REQUIRED_VALIDATOR]
|
|
|
|
})
|
|
|
|
export class SomeComponent {}
|
|
|
|
`,
|
2016-08-22 13:26:22 -04:00
|
|
|
'private-enum.ts': `
|
|
|
|
export enum PublicEnum { a, b, c }
|
|
|
|
enum PrivateEnum { e, f, g }
|
|
|
|
`,
|
2016-08-22 20:37:48 -04:00
|
|
|
'local-function-ref.ts': `
|
|
|
|
import {Component, Validators} from 'angular2/core';
|
|
|
|
|
|
|
|
function required() {}
|
|
|
|
|
|
|
|
export const REQUIRED_VALIDATOR: any = {
|
|
|
|
provide: 'SomeToken',
|
|
|
|
useValue: required,
|
|
|
|
multi: true
|
|
|
|
};
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
providers: [REQUIRED_VALIDATOR]
|
|
|
|
})
|
|
|
|
export class SomeComponent {}
|
|
|
|
`,
|
|
|
|
'local-symbol-ref-func.ts': `
|
|
|
|
var localSymbol: any[];
|
|
|
|
|
|
|
|
export function foo(index: number): string {
|
|
|
|
return localSymbol[index];
|
|
|
|
}
|
|
|
|
`,
|
2016-03-22 20:11:42 -04:00
|
|
|
'node_modules': {
|
|
|
|
'angular2': {
|
|
|
|
'core.d.ts': `
|
|
|
|
export interface Type extends Function { }
|
|
|
|
export interface TypeDecorator {
|
|
|
|
<T extends Type>(type: T): T;
|
|
|
|
(target: Object, propertyKey?: string | symbol, parameterIndex?: number): void;
|
|
|
|
annotations: any[];
|
|
|
|
}
|
|
|
|
export interface ComponentDecorator extends TypeDecorator { }
|
|
|
|
export interface ComponentFactory {
|
|
|
|
(obj: {
|
|
|
|
selector?: string;
|
|
|
|
inputs?: string[];
|
|
|
|
outputs?: string[];
|
|
|
|
properties?: string[];
|
|
|
|
events?: string[];
|
|
|
|
host?: {
|
|
|
|
[key: string]: string;
|
|
|
|
};
|
|
|
|
bindings?: any[];
|
|
|
|
providers?: any[];
|
|
|
|
exportAs?: string;
|
|
|
|
moduleId?: string;
|
|
|
|
queries?: {
|
|
|
|
[key: string]: any;
|
|
|
|
};
|
|
|
|
viewBindings?: any[];
|
|
|
|
viewProviders?: any[];
|
|
|
|
templateUrl?: string;
|
|
|
|
template?: string;
|
|
|
|
styleUrls?: string[];
|
|
|
|
styles?: string[];
|
|
|
|
directives?: Array<Type | any[]>;
|
|
|
|
pipes?: Array<Type | any[]>;
|
|
|
|
}): ComponentDecorator;
|
|
|
|
}
|
|
|
|
export declare var Component: ComponentFactory;
|
|
|
|
export interface InputFactory {
|
|
|
|
(bindingPropertyName?: string): any;
|
|
|
|
new (bindingPropertyName?: string): any;
|
|
|
|
}
|
|
|
|
export declare var Input: InputFactory;
|
|
|
|
export interface InjectableFactory {
|
|
|
|
(): any;
|
|
|
|
}
|
|
|
|
export declare var Injectable: InjectableFactory;
|
2017-03-15 12:24:56 -04:00
|
|
|
export interface InjectFactory {
|
|
|
|
(binding?: any): any;
|
|
|
|
new (binding?: any): any;
|
|
|
|
}
|
|
|
|
export declare var Inject: InjectFactory;
|
2016-03-22 20:11:42 -04:00
|
|
|
export interface OnInit {
|
|
|
|
ngOnInit(): any;
|
|
|
|
}
|
2016-08-02 17:38:31 -04:00
|
|
|
export class Validators {
|
|
|
|
static required(): void;
|
|
|
|
}
|
2016-03-22 20:11:42 -04:00
|
|
|
`,
|
|
|
|
'common.d.ts': `
|
|
|
|
export declare class NgFor {
|
|
|
|
ngForOf: any;
|
|
|
|
ngForTemplate: any;
|
|
|
|
ngDoCheck(): void;
|
|
|
|
}
|
|
|
|
export declare class LowerCasePipe {
|
|
|
|
transform(value: string, args?: any[]): string;
|
|
|
|
}
|
|
|
|
export declare class UpperCasePipe {
|
|
|
|
transform(value: string, args?: any[]): string;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-06-20 13:36:04 -04:00
|
|
|
|
|
|
|
function createSource(text: string): ts.SourceFile {
|
|
|
|
return ts.createSourceFile('', text, ts.ScriptTarget.Latest, true);
|
|
|
|
}
|