2016-06-23 12:47:54 -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-11-14 20:37:47 -05:00
import { StaticReflector , StaticReflectorHost , StaticSymbol } from '@angular/compiler' ;
2016-11-15 12:19:00 -05:00
import { HostListener , Inject , animate , group , keyframes , sequence , state , style , transition , trigger } from '@angular/core' ;
2016-06-17 16:11:00 -04:00
import { MetadataCollector } from '@angular/tsc-wrapped' ;
import * as ts from 'typescript' ;
2016-07-27 05:05:07 -04:00
2016-06-17 16:11:00 -04:00
// This matches .ts files but not .d.ts files.
const TS_EXT = /(^.|(?!\.d)..)\.ts$/ ;
2016-05-31 12:15:17 -04:00
2016-04-30 15:27:37 -04:00
describe ( 'StaticReflector' , ( ) = > {
2016-11-12 08:08:58 -05:00
const noContext = new StaticSymbol ( '' , '' ) ;
2016-04-30 15:27:37 -04:00
let host : StaticReflectorHost ;
let reflector : StaticReflector ;
2016-02-18 13:53:21 -05:00
2016-04-30 15:27:37 -04:00
beforeEach ( ( ) = > {
host = new MockReflectorHost ( ) ;
reflector = new StaticReflector ( host ) ;
} ) ;
2016-03-24 13:03:10 -04:00
2016-04-30 15:27:37 -04:00
function simplify ( context : StaticSymbol , value : any ) {
return reflector . simplify ( context , value ) ;
}
2016-02-18 13:53:21 -05:00
2016-04-30 15:27:37 -04:00
it ( 'should get annotations for NgFor' , ( ) = > {
2016-11-12 08:08:58 -05:00
const NgFor = host . findDeclaration ( 'angular2/src/common/directives/ng_for' , 'NgFor' ) ;
const annotations = reflector . annotations ( NgFor ) ;
2016-04-30 15:27:37 -04:00
expect ( annotations . length ) . toEqual ( 1 ) ;
2016-11-12 08:08:58 -05:00
const annotation = annotations [ 0 ] ;
2016-04-30 15:27:37 -04:00
expect ( annotation . selector ) . toEqual ( '[ngFor][ngForOf]' ) ;
expect ( annotation . inputs ) . toEqual ( [ 'ngForTrackBy' , 'ngForOf' , 'ngForTemplate' ] ) ;
2016-03-24 13:03:10 -04:00
} ) ;
2016-04-30 15:27:37 -04:00
it ( 'should get constructor for NgFor' , ( ) = > {
2016-11-12 08:08:58 -05:00
const NgFor = host . findDeclaration ( 'angular2/src/common/directives/ng_for' , 'NgFor' ) ;
const ViewContainerRef =
2016-04-30 15:27:37 -04:00
host . findDeclaration ( 'angular2/src/core/linker/view_container_ref' , 'ViewContainerRef' ) ;
2016-11-12 08:08:58 -05:00
const TemplateRef =
host . findDeclaration ( 'angular2/src/core/linker/template_ref' , 'TemplateRef' ) ;
const IterableDiffers = host . findDeclaration (
2016-04-30 15:27:37 -04:00
'angular2/src/core/change_detection/differs/iterable_differs' , 'IterableDiffers' ) ;
2016-11-12 08:08:58 -05:00
const ChangeDetectorRef = host . findDeclaration (
2016-04-30 15:27:37 -04:00
'angular2/src/core/change_detection/change_detector_ref' , 'ChangeDetectorRef' ) ;
2016-11-12 08:08:58 -05:00
const parameters = reflector . parameters ( NgFor ) ;
2016-06-08 19:38:52 -04:00
expect ( parameters ) . toEqual ( [
[ ViewContainerRef ] , [ TemplateRef ] , [ IterableDiffers ] , [ ChangeDetectorRef ]
] ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should get annotations for HeroDetailComponent' , ( ) = > {
2016-11-12 08:08:58 -05:00
const HeroDetailComponent =
2016-04-30 15:27:37 -04:00
host . findDeclaration ( 'src/app/hero-detail.component' , 'HeroDetailComponent' ) ;
2016-11-12 08:08:58 -05:00
const annotations = reflector . annotations ( HeroDetailComponent ) ;
2016-04-30 15:27:37 -04:00
expect ( annotations . length ) . toEqual ( 1 ) ;
2016-11-12 08:08:58 -05:00
const annotation = annotations [ 0 ] ;
2016-04-30 15:27:37 -04:00
expect ( annotation . selector ) . toEqual ( 'my-hero-detail' ) ;
2016-06-08 19:38:52 -04:00
expect ( annotation . animations ) . toEqual ( [ trigger ( 'myAnimation' , [
state ( 'state1' , style ( { 'background' : 'white' } ) ) ,
transition (
'* => *' ,
sequence ( [ group ( [ animate (
'1s 0.5s' ,
keyframes ( [ style ( { 'background' : 'blue' } ) , style ( { 'background' : 'red' } ) ] ) ) ] ) ] ) )
] ) ] ) ;
2016-04-30 15:27:37 -04:00
} ) ;
2016-06-02 19:40:38 -04:00
it ( 'should throw and exception for unsupported metadata versions' , ( ) = > {
2016-11-12 08:08:58 -05:00
const e = host . findDeclaration ( 'src/version-error' , 'e' ) ;
2016-06-08 19:38:52 -04:00
expect ( ( ) = > reflector . annotations ( e ) )
. toThrow ( new Error (
'Metadata version mismatch for module /tmp/src/version-error.d.ts, found version 100, expected 1' ) ) ;
2016-06-02 19:40:38 -04:00
} ) ;
2016-04-30 15:27:37 -04:00
it ( 'should get and empty annotation list for an unknown class' , ( ) = > {
2016-11-12 08:08:58 -05:00
const UnknownClass = host . findDeclaration ( 'src/app/app.component' , 'UnknownClass' ) ;
const annotations = reflector . annotations ( UnknownClass ) ;
2016-04-30 15:27:37 -04:00
expect ( annotations ) . toEqual ( [ ] ) ;
} ) ;
it ( 'should get propMetadata for HeroDetailComponent' , ( ) = > {
2016-11-12 08:08:58 -05:00
const HeroDetailComponent =
2016-04-30 15:27:37 -04:00
host . findDeclaration ( 'src/app/hero-detail.component' , 'HeroDetailComponent' ) ;
2016-11-12 08:08:58 -05:00
const props = reflector . propMetadata ( HeroDetailComponent ) ;
2016-04-30 15:27:37 -04:00
expect ( props [ 'hero' ] ) . toBeTruthy ( ) ;
2016-09-12 22:14:17 -04:00
expect ( props [ 'onMouseOver' ] ) . toEqual ( [ new HostListener ( 'mouseover' , [ '$event' ] ) ] ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should get an empty object from propMetadata for an unknown class' , ( ) = > {
2016-11-12 08:08:58 -05:00
const UnknownClass = host . findDeclaration ( 'src/app/app.component' , 'UnknownClass' ) ;
const properties = reflector . propMetadata ( UnknownClass ) ;
2016-04-30 15:27:37 -04:00
expect ( properties ) . toEqual ( { } ) ;
} ) ;
it ( 'should get empty parameters list for an unknown class ' , ( ) = > {
2016-11-12 08:08:58 -05:00
const UnknownClass = host . findDeclaration ( 'src/app/app.component' , 'UnknownClass' ) ;
const parameters = reflector . parameters ( UnknownClass ) ;
2016-04-30 15:27:37 -04:00
expect ( parameters ) . toEqual ( [ ] ) ;
} ) ;
2016-06-03 18:43:09 -04:00
it ( 'should provide context for errors reported by the collector' , ( ) = > {
2016-11-12 08:08:58 -05:00
const SomeClass = host . findDeclaration ( 'src/error-reporting' , 'SomeClass' ) ;
2016-06-08 19:38:52 -04:00
expect ( ( ) = > reflector . annotations ( SomeClass ) )
. toThrow ( new Error (
2016-07-11 20:26:35 -04:00
'Error encountered resolving symbol values statically. A reasonable error message (position 13:34 in the original .ts file), resolving symbol ErrorSym in /tmp/src/error-references.d.ts, resolving symbol Link2 in /tmp/src/error-references.d.ts, resolving symbol Link1 in /tmp/src/error-references.d.ts, resolving symbol SomeClass in /tmp/src/error-reporting.d.ts, resolving symbol SomeClass in /tmp/src/error-reporting.d.ts' ) ) ;
2016-06-03 18:43:09 -04:00
} ) ;
2016-04-30 15:27:37 -04:00
it ( 'should simplify primitive into itself' , ( ) = > {
expect ( simplify ( noContext , 1 ) ) . toBe ( 1 ) ;
expect ( simplify ( noContext , true ) ) . toBe ( true ) ;
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , 'some value' ) ) . toBe ( 'some value' ) ;
2016-04-30 15:27:37 -04:00
} ) ;
2016-10-11 18:47:44 -04:00
it ( 'should simplify a static symbol into itself' , ( ) = > {
const staticSymbol = new StaticSymbol ( '' , '' ) ;
expect ( simplify ( noContext , staticSymbol ) ) . toBe ( staticSymbol ) ;
} ) ;
2016-06-08 19:38:52 -04:00
it ( 'should simplify an array into a copy of the array' , ( ) = > {
expect ( simplify ( noContext , [ 1 , 2 , 3 ] ) ) . toEqual ( [ 1 , 2 , 3 ] ) ;
} ) ;
2016-04-30 15:27:37 -04:00
it ( 'should simplify an object to a copy of the object' , ( ) = > {
2016-11-12 08:08:58 -05:00
const expr = { a : 1 , b : 2 , c : 3 } ;
2016-04-30 15:27:37 -04:00
expect ( simplify ( noContext , expr ) ) . toEqual ( expr ) ;
} ) ;
it ( 'should simplify &&' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '&&' , left : true , right : true } ) ) )
. toBe ( true ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '&&' , left : true , right : false } ) ) )
. toBe ( false ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '&&' , left : false , right : true } ) ) )
. toBe ( false ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '&&' , left : false , right : false } ) ) )
. toBe ( false ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify ||' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '||' , left : true , right : true } ) ) )
. toBe ( true ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '||' , left : true , right : false } ) ) )
. toBe ( true ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '||' , left : false , right : true } ) ) )
. toBe ( true ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '||' , left : false , right : false } ) ) )
. toBe ( false ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify &' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '&' , left : 0x22 , right : 0x0F } ) ) )
. toBe ( 0x22 & 0x0F ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '&' , left : 0x22 , right : 0xF0 } ) ) )
. toBe ( 0x22 & 0xF0 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify |' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '|' , left : 0x22 , right : 0x0F } ) ) )
. toBe ( 0x22 | 0x0F ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '|' , left : 0x22 , right : 0xF0 } ) ) )
. toBe ( 0x22 | 0xF0 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify ^' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '|' , left : 0x22 , right : 0x0F } ) ) )
. toBe ( 0x22 | 0x0F ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '|' , left : 0x22 , right : 0xF0 } ) ) )
. toBe ( 0x22 | 0xF0 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify ==' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '==' , left : 0x22 , right : 0x22 } ) ) )
. toBe ( 0x22 == 0x22 ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '==' , left : 0x22 , right : 0xF0 } ) ) )
2016-08-30 21:07:40 -04:00
. toBe ( 0x22 as any == 0xF0 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify !=' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '!=' , left : 0x22 , right : 0x22 } ) ) )
. toBe ( 0x22 != 0x22 ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '!=' , left : 0x22 , right : 0xF0 } ) ) )
2016-08-30 21:07:40 -04:00
. toBe ( 0x22 as any != 0xF0 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify ===' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '===' , left : 0x22 , right : 0x22 } ) ) )
. toBe ( 0x22 === 0x22 ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '===' , left : 0x22 , right : 0xF0 } ) ) )
2016-08-30 21:07:40 -04:00
. toBe ( 0x22 as any === 0xF0 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify !==' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '!==' , left : 0x22 , right : 0x22 } ) ) )
. toBe ( 0x22 !== 0x22 ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '!==' , left : 0x22 , right : 0xF0 } ) ) )
2016-08-30 21:07:40 -04:00
. toBe ( 0x22 as any !== 0xF0 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify >' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '>' , left : 1 , right : 1 } ) ) )
. toBe ( 1 > 1 ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '>' , left : 1 , right : 0 } ) ) )
. toBe ( 1 > 0 ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '>' , left : 0 , right : 1 } ) ) )
. toBe ( 0 > 1 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify >=' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '>=' , left : 1 , right : 1 } ) ) )
. toBe ( 1 >= 1 ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '>=' , left : 1 , right : 0 } ) ) )
. toBe ( 1 >= 0 ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '>=' , left : 0 , right : 1 } ) ) )
. toBe ( 0 >= 1 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify <=' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '<=' , left : 1 , right : 1 } ) ) )
. toBe ( 1 <= 1 ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '<=' , left : 1 , right : 0 } ) ) )
. toBe ( 1 <= 0 ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '<=' , left : 0 , right : 1 } ) ) )
. toBe ( 0 <= 1 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify <' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '<' , left : 1 , right : 1 } ) ) )
. toBe ( 1 < 1 ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '<' , left : 1 , right : 0 } ) ) )
. toBe ( 1 < 0 ) ;
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '<' , left : 0 , right : 1 } ) ) )
. toBe ( 0 < 1 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify <<' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '<<' , left : 0x55 , right : 2 } ) ) )
. toBe ( 0x55 << 2 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify >>' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '>>' , left : 0x55 , right : 2 } ) ) )
. toBe ( 0x55 >> 2 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify +' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '+' , left : 0x55 , right : 2 } ) ) )
. toBe ( 0x55 + 2 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify -' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '-' , left : 0x55 , right : 2 } ) ) )
. toBe ( 0x55 - 2 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify *' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '*' , left : 0x55 , right : 2 } ) ) )
. toBe ( 0x55 * 2 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify /' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '/' , left : 0x55 , right : 2 } ) ) )
. toBe ( 0x55 / 2 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify %' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'binop' , operator : '%' , left : 0x55 , right : 2 } ) ) )
. toBe ( 0x55 % 2 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify prefix -' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'pre' , operator : '-' , operand : 2 } ) ) ) . toBe ( - 2 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify prefix ~' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'pre' , operator : '~' , operand : 2 } ) ) ) . toBe ( ~ 2 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify prefix !' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'pre' , operator : '!' , operand : true } ) ) ) . toBe ( ! true ) ;
expect ( simplify ( noContext , ( { __symbolic : 'pre' , operator : '!' , operand : false } ) ) ) . toBe ( ! false ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify an array index' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify ( noContext , ( { __symbolic : 'index' , expression : [ 1 , 2 , 3 ] , index : 2 } ) ) ) . toBe ( 3 ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify an object index' , ( ) = > {
2016-11-12 08:08:58 -05:00
const expr = { __symbolic : 'select' , expression : { a : 1 , b : 2 , c : 3 } , member : 'b' } ;
2016-04-30 15:27:37 -04:00
expect ( simplify ( noContext , expr ) ) . toBe ( 2 ) ;
} ) ;
it ( 'should simplify a module reference' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify (
new StaticSymbol ( '/src/cases' , '' ) ,
( { __symbolic : 'reference' , module : './extern' , name : 's' } ) ) )
. toEqual ( 's' ) ;
2016-04-30 15:27:37 -04:00
} ) ;
it ( 'should simplify a non existing reference as a static symbol' , ( ) = > {
2016-06-08 19:38:52 -04:00
expect ( simplify (
new StaticSymbol ( '/src/cases' , '' ) ,
( { __symbolic : 'reference' , module : './extern' , name : 'nonExisting' } ) ) )
2016-05-02 12:38:46 -04:00
. toEqual ( host . getStaticSymbol ( '/src/extern.d.ts' , 'nonExisting' ) ) ;
2016-04-30 15:27:37 -04:00
} ) ;
2016-06-13 18:56:51 -04:00
feat(browser): use AppModules for bootstrap in the browser
This introduces the `BrowserModule` to be used for long form
bootstrap and offline compile bootstrap:
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {bootstrapModuleFactory} from ‘@angular/platform-browser’;
bootstrapModuleFactory(MyModuleNgFactory);
// runtime compile long form
import {bootstrapModule} from ‘@angular/platform-browser-dynamic’;
bootstrapModule(MyModule);
```
The short form, `bootstrap(...)`, can now creates a module on the fly,
given `directives`, `pipes, `providers`, `precompile` and `modules`
properties.
Related changes:
- make `SanitizationService`, `SecurityContext` public in `@angular/core` so that the offline compiler can resolve the token
- move `AnimationDriver` to `platform-browser` and make it
public so that the offline compiler can resolve the token
BREAKING CHANGES:
- short form bootstrap does no longer allow
to inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead.
To provide custom providers for the compiler,
create a custom compiler via `browserCompiler({providers: [...]})`
and pass that into the `bootstrap` method.
2016-06-30 16:07:17 -04:00
it ( 'should simplify a function reference as a static symbol' , ( ) = > {
expect ( simplify (
new StaticSymbol ( '/src/cases' , 'myFunction' ) ,
( { __symbolic : 'function' , parameters : [ 'a' ] , value : [ ] } ) ) )
. toEqual ( host . getStaticSymbol ( '/src/cases' , 'myFunction' ) ) ;
} ) ;
2016-06-13 18:56:51 -04:00
it ( 'should simplify values initialized with a function call' , ( ) = > {
expect ( simplify ( new StaticSymbol ( '/tmp/src/function-reference.ts' , '' ) , {
__symbolic : 'reference' ,
name : 'one'
} ) ) . toEqual ( [ 'some-value' ] ) ;
expect ( simplify ( new StaticSymbol ( '/tmp/src/function-reference.ts' , '' ) , {
__symbolic : 'reference' ,
2016-07-25 08:29:20 -04:00
name : 'three'
} ) ) . toEqual ( 3 ) ;
2016-06-13 18:56:51 -04:00
} ) ;
it ( 'should error on direct recursive calls' , ( ) = > {
expect (
( ) = > simplify (
new StaticSymbol ( '/tmp/src/function-reference.ts' , '' ) ,
{ __symbolic : 'reference' , name : 'recursion' } ) )
. toThrow ( new Error (
2016-07-25 08:29:20 -04:00
'Recursion not supported, resolving symbol recursive in /tmp/src/function-recursive.d.ts, resolving symbol recursion in /tmp/src/function-reference.ts, resolving symbol in /tmp/src/function-reference.ts' ) ) ;
2016-06-13 18:56:51 -04:00
} ) ;
2016-11-09 19:29:22 -05:00
it ( 'should record data about the error in the exception' , ( ) = > {
let threw = false ;
try {
const metadata = host . getMetadataFor ( '/tmp/src/invalid-metadata.ts' ) ;
expect ( metadata ) . toBeDefined ( ) ;
if ( ! Array . isArray ( metadata ) ) {
const module Metadata : any = metadata [ 'metadata' ] ;
expect ( module Metadata ) . toBeDefined ( ) ;
const classData : any = module Metadata [ 'InvalidMetadata' ] ;
expect ( classData ) . toBeDefined ( ) ;
simplify ( new StaticSymbol ( '/tmp/src/invalid-metadata.ts' , '' ) , classData . decorators [ 0 ] ) ;
}
} catch ( e ) {
expect ( e . fileName ) . toBe ( '/tmp/src/invalid-metadata.ts' ) ;
threw = true ;
}
expect ( threw ) . toBe ( true ) ;
} ) ;
2016-06-13 18:56:51 -04:00
it ( 'should error on indirect recursive calls' , ( ) = > {
expect (
( ) = > simplify (
new StaticSymbol ( '/tmp/src/function-reference.ts' , '' ) ,
{ __symbolic : 'reference' , name : 'indirectRecursion' } ) )
. toThrow ( new Error (
2016-07-25 08:29:20 -04:00
'Recursion not supported, resolving symbol indirectRecursion2 in /tmp/src/function-recursive.d.ts, resolving symbol indirectRecursion1 in /tmp/src/function-recursive.d.ts, resolving symbol indirectRecursion in /tmp/src/function-reference.ts, resolving symbol in /tmp/src/function-reference.ts' ) ) ;
2016-06-13 18:56:51 -04:00
} ) ;
2016-06-17 16:11:00 -04:00
2016-06-13 18:56:51 -04:00
it ( 'should simplify a spread expression' , ( ) = > {
expect ( simplify ( new StaticSymbol ( '/tmp/src/spread.ts' , '' ) , {
__symbolic : 'reference' ,
name : 'spread'
} ) ) . toEqual ( [ 0 , 1 , 2 , 3 , 4 , 5 ] ) ;
} ) ;
2016-06-17 16:11:00 -04:00
it ( 'should be able to get metadata from a ts file' , ( ) = > {
2016-11-12 08:08:58 -05:00
const metadata = reflector . getModuleMetadata ( '/tmp/src/custom-decorator-reference.ts' ) ;
2016-06-17 16:11:00 -04:00
expect ( metadata ) . toEqual ( {
__symbolic : 'module' ,
version : 1 ,
metadata : {
Foo : {
__symbolic : 'class' ,
decorators : [ {
__symbolic : 'call' ,
expression :
{ __symbolic : 'reference' , module : './custom-decorator' , name : 'CustomDecorator' }
} ] ,
members : {
foo : [ {
__symbolic : 'property' ,
decorators : [ {
__symbolic : 'call' ,
expression : {
__symbolic : 'reference' ,
module : './custom-decorator' ,
name : 'CustomDecorator'
}
} ]
} ]
}
}
}
} ) ;
} ) ;
it ( 'should be able to get metadata for a class containing a custom decorator' , ( ) = > {
2016-11-12 08:08:58 -05:00
const props = reflector . propMetadata (
2016-06-17 16:11:00 -04:00
host . getStaticSymbol ( '/tmp/src/custom-decorator-reference.ts' , 'Foo' ) ) ;
expect ( props ) . toEqual ( { foo : [ ] } ) ;
} ) ;
2016-11-15 12:19:00 -05:00
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 ) ] ] ) ;
} ) ;
2016-06-17 16:11:00 -04:00
it ( 'should report an error for invalid function calls' , ( ) = > {
expect (
( ) = >
reflector . annotations ( host . getStaticSymbol ( '/tmp/src/invalid-calls.ts' , 'MyComponent' ) ) )
. toThrow ( new Error (
` Error encountered resolving symbol values statically. Calling function 'someFunction', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol MyComponent in /tmp/src/invalid-calls.ts, resolving symbol MyComponent in /tmp/src/invalid-calls.ts ` ) ) ;
} ) ;
2016-07-26 13:18:35 -04:00
it ( 'should be able to get metadata for a class containing a static method call' , ( ) = > {
const annotations = reflector . annotations (
host . getStaticSymbol ( '/tmp/src/static-method-call.ts' , 'MyComponent' ) ) ;
expect ( annotations . length ) . toBe ( 1 ) ;
expect ( annotations [ 0 ] . providers ) . toEqual ( { provider : 'a' , useValue : 100 } ) ;
} ) ;
2016-07-27 22:26:59 -04:00
it ( 'should be able to get metadata for a class containing a static field reference' , ( ) = > {
const annotations =
reflector . annotations ( host . getStaticSymbol ( '/tmp/src/static-field-reference.ts' , 'Foo' ) ) ;
expect ( annotations . length ) . toBe ( 1 ) ;
expect ( annotations [ 0 ] . providers ) . toEqual ( [ { provider : 'a' , useValue : 'Some string' } ] ) ;
} ) ;
2016-07-28 20:32:29 -04:00
it ( 'should be able to get the metadata for a class calling a method with a conditional expression' ,
( ) = > {
const annotations = reflector . annotations (
host . getStaticSymbol ( '/tmp/src/static-method-call.ts' , 'MyCondComponent' ) ) ;
expect ( annotations . length ) . toBe ( 1 ) ;
expect ( annotations [ 0 ] . providers ) . toEqual ( [
[ { provider : 'a' , useValue : '1' } ] , [ { provider : 'a' , useValue : '2' } ]
] ) ;
} ) ;
2016-07-29 12:10:45 -04:00
it ( 'should be able to get the metadata for a class calling a method with default parameters' ,
( ) = > {
const annotations = reflector . annotations (
host . getStaticSymbol ( '/tmp/src/static-method-call.ts' , 'MyDefaultsComponent' ) ) ;
expect ( annotations . length ) . toBe ( 1 ) ;
expect ( annotations [ 0 ] . providers ) . toEqual ( [ [ 'a' , true , false ] ] ) ;
} ) ;
2016-08-23 14:58:12 -04:00
it ( 'should be able to get metadata with a reference to a static method' , ( ) = > {
const annotations = reflector . annotations (
host . getStaticSymbol ( '/tmp/src/static-method-ref.ts' , 'MethodReference' ) ) ;
expect ( annotations . length ) . toBe ( 1 ) ;
2016-09-12 12:44:20 -04:00
expect ( annotations [ 0 ] . providers [ 0 ] . useValue . members [ 0 ] ) . toEqual ( 'staticMethod' ) ;
2016-08-23 14:58:12 -04:00
} ) ;
2016-04-30 15:27:37 -04:00
} ) ;
2016-03-24 13:03:10 -04:00
class MockReflectorHost implements StaticReflectorHost {
2016-04-29 00:54:02 -04:00
private staticTypeCache = new Map < string , StaticSymbol > ( ) ;
2016-06-17 16:11:00 -04:00
private collector = new MetadataCollector ( ) ;
constructor ( ) { }
2016-04-29 00:54:02 -04:00
2016-05-01 14:22:39 -04:00
angularImportLocations() {
return {
coreDecorators : 'angular2/src/core/metadata' ,
2016-09-12 23:30:42 -04:00
diDecorators : 'angular2/src/core/di/metadata' ,
2016-05-01 14:22:39 -04:00
diMetadata : 'angular2/src/core/di/metadata' ,
2016-06-13 18:56:51 -04:00
diOpaqueToken : 'angular2/src/core/di/opaque_token' ,
2016-05-31 12:15:17 -04:00
animationMetadata : 'angular2/src/core/animation/metadata' ,
2016-05-01 14:22:39 -04:00
provider : 'angular2/src/core/di/provider'
} ;
}
2016-10-06 18:10:44 -04:00
getCanonicalFileName ( fileName : string ) : string { return fileName ; }
2016-08-23 14:58:12 -04:00
getStaticSymbol ( declarationFile : string , name : string , members? : string [ ] ) : StaticSymbol {
2016-11-12 08:08:58 -05:00
const cacheKey = ` ${ declarationFile } : ${ name } ${ members ? '.' + members . join ( '.' ) : '' } ` ;
let result = this . staticTypeCache . get ( cacheKey ) ;
2016-09-30 12:26:53 -04:00
if ( ! result ) {
2016-08-23 14:58:12 -04:00
result = new StaticSymbol ( declarationFile , name , members ) ;
2016-04-29 00:54:02 -04:00
this . staticTypeCache . set ( cacheKey , result ) ;
}
return result ;
2016-02-18 13:53:21 -05:00
}
2016-04-29 00:54:02 -04:00
// In tests, assume that symbols are not re-exported
2016-04-29 00:58:51 -04:00
findDeclaration ( module Path : string , symbolName : string , containingFile? : string ) : StaticSymbol {
2016-04-26 00:29:06 -04:00
function splitPath ( path : string ) : string [ ] { return path . split ( /\/|\\/g ) ; }
function resolvePath ( pathParts : string [ ] ) : string {
2016-11-12 08:08:58 -05:00
const result : string [ ] = [ ] ;
2016-10-21 18:14:44 -04:00
pathParts . forEach ( ( part , index ) = > {
2016-04-26 00:29:06 -04:00
switch ( part ) {
case '' :
case '.' :
if ( index > 0 ) return ;
break ;
case '..' :
if ( index > 0 && result . length != 0 ) result . pop ( ) ;
return ;
}
result . push ( part ) ;
} ) ;
return result . join ( '/' ) ;
}
function pathTo ( from : string , to : string ) : string {
let result = to ;
if ( to . startsWith ( '.' ) ) {
2016-11-12 08:08:58 -05:00
const fromParts = splitPath ( from ) ;
2016-04-26 00:29:06 -04:00
fromParts . pop ( ) ; // remove the file name.
2016-11-12 08:08:58 -05:00
const toParts = splitPath ( to ) ;
2016-04-26 00:29:06 -04:00
result = resolvePath ( fromParts . concat ( toParts ) ) ;
}
return result ;
}
2016-04-29 00:54:02 -04:00
if ( module Path.indexOf ( '.' ) === 0 ) {
2016-07-26 13:18:35 -04:00
const baseName = pathTo ( containingFile , module Path ) ;
const tsName = baseName + '.ts' ;
if ( this . getMetadataFor ( tsName ) ) {
return this . getStaticSymbol ( tsName , symbolName ) ;
}
return this . getStaticSymbol ( baseName + '.d.ts' , symbolName ) ;
2016-04-26 00:29:06 -04:00
}
2016-05-02 12:38:46 -04:00
return this . getStaticSymbol ( '/tmp/' + module Path + '.d.ts' , symbolName ) ;
2016-04-26 00:29:06 -04:00
}
2016-03-24 13:03:10 -04:00
getMetadataFor ( module Id : string ) : any {
2016-11-12 08:08:58 -05:00
const data : { [ key : string ] : any } = {
2016-06-08 13:44:04 -04:00
'/tmp/angular2/src/common/forms-deprecated/directives.d.ts' : [ {
2016-06-08 19:38:52 -04:00
'__symbolic' : 'module' ,
'version' : 1 ,
'metadata' : {
'FORM_DIRECTIVES' : [
2016-04-30 15:27:37 -04:00
{
2016-06-08 19:38:52 -04:00
'__symbolic' : 'reference' ,
'name' : 'NgFor' ,
'module' : 'angular2/src/common/directives/ng_for'
2016-02-18 13:53:21 -05:00
}
2016-04-30 15:27:37 -04:00
]
}
2016-06-02 19:40:38 -04:00
} ] ,
2016-04-30 15:27:37 -04:00
'/tmp/angular2/src/common/directives/ng_for.d.ts' : {
2016-06-08 19:38:52 -04:00
'__symbolic' : 'module' ,
'version' : 1 ,
'metadata' : {
'NgFor' : {
'__symbolic' : 'class' ,
'decorators' : [
2016-02-18 13:53:21 -05:00
{
2016-06-08 19:38:52 -04:00
'__symbolic' : 'call' ,
'expression' : {
'__symbolic' : 'reference' ,
'name' : 'Directive' ,
'module' : '../../core/metadata'
2016-04-30 15:27:37 -04:00
} ,
2016-06-08 19:38:52 -04:00
'arguments' : [
2016-04-30 15:27:37 -04:00
{
2016-06-08 19:38:52 -04:00
'selector' : '[ngFor][ngForOf]' ,
'inputs' : [ 'ngForTrackBy' , 'ngForOf' , 'ngForTemplate' ]
2016-04-30 15:27:37 -04:00
}
]
}
] ,
2016-06-08 19:38:52 -04:00
'members' : {
'__ctor__' : [
2016-04-30 15:27:37 -04:00
{
2016-06-08 19:38:52 -04:00
'__symbolic' : 'constructor' ,
'parameters' : [
2016-04-30 15:27:37 -04:00
{
2016-06-08 19:38:52 -04:00
'__symbolic' : 'reference' ,
'module' : '../../core/linker/view_container_ref' ,
'name' : 'ViewContainerRef'
2016-04-30 15:27:37 -04:00
} ,
{
2016-06-08 19:38:52 -04:00
'__symbolic' : 'reference' ,
'module' : '../../core/linker/template_ref' ,
'name' : 'TemplateRef'
2016-04-30 15:27:37 -04:00
} ,
{
2016-06-08 19:38:52 -04:00
'__symbolic' : 'reference' ,
'module' : '../../core/change_detection/differs/iterable_differs' ,
'name' : 'IterableDiffers'
2016-04-30 15:27:37 -04:00
} ,
2016-02-18 13:53:21 -05:00
{
2016-06-08 19:38:52 -04:00
'__symbolic' : 'reference' ,
'module' : '../../core/change_detection/change_detector_ref' ,
'name' : 'ChangeDetectorRef'
2016-02-18 13:53:21 -05:00
}
2016-04-30 15:27:37 -04:00
]
}
]
}
}
}
} ,
'/tmp/angular2/src/core/linker/view_container_ref.d.ts' :
2016-06-08 19:38:52 -04:00
{ version : 1 , 'metadata' : { 'ViewContainerRef' : { '__symbolic' : 'class' } } } ,
2016-04-30 15:27:37 -04:00
'/tmp/angular2/src/core/linker/template_ref.d.ts' :
2016-06-08 19:38:52 -04:00
{ version : 1 , 'module' : './template_ref' , 'metadata' : { 'TemplateRef' : { '__symbolic' : 'class' } } } ,
2016-04-30 15:27:37 -04:00
'/tmp/angular2/src/core/change_detection/differs/iterable_differs.d.ts' :
2016-06-08 19:38:52 -04:00
{ version : 1 , 'metadata' : { 'IterableDiffers' : { '__symbolic' : 'class' } } } ,
2016-04-30 15:27:37 -04:00
'/tmp/angular2/src/core/change_detection/change_detector_ref.d.ts' :
2016-06-08 19:38:52 -04:00
{ version : 1 , 'metadata' : { 'ChangeDetectorRef' : { '__symbolic' : 'class' } } } ,
2016-04-30 15:27:37 -04:00
'/tmp/src/app/hero-detail.component.d.ts' : {
2016-06-08 19:38:52 -04:00
'__symbolic' : 'module' ,
'version' : 1 ,
'metadata' : {
'HeroDetailComponent' : {
'__symbolic' : 'class' ,
'decorators' : [
2016-04-12 12:40:37 -04:00
{
2016-06-08 19:38:52 -04:00
'__symbolic' : 'call' ,
'expression' : {
'__symbolic' : 'reference' ,
'name' : 'Component' ,
'module' : 'angular2/src/core/metadata'
2016-04-30 15:27:37 -04:00
} ,
2016-06-08 19:38:52 -04:00
'arguments' : [
2016-04-30 15:27:37 -04:00
{
2016-06-08 19:38:52 -04:00
'selector' : 'my-hero-detail' ,
'template' :
'\n <div *ngIf="hero">\n <h2>{{hero.name}} details!</h2>\n <div><label>id: </label>{{hero.id}}</div>\n <div>\n <label>name: </label>\n <input [(ngModel)]="hero.name" placeholder="name"/>\n </div>\n </div>\n' ,
'animations' : [ {
'__symbolic' : 'call' ,
'expression' : {
'__symbolic' : 'reference' ,
'name' : 'trigger' ,
'module' : 'angular2/src/core/animation/metadata'
2016-05-31 12:15:17 -04:00
} ,
2016-06-08 19:38:52 -04:00
'arguments' : [
'myAnimation' ,
[ { '__symbolic' : 'call' ,
'expression' : {
'__symbolic' : 'reference' ,
'name' : 'state' ,
'module' : 'angular2/src/core/animation/metadata'
2016-05-31 12:15:17 -04:00
} ,
2016-06-08 19:38:52 -04:00
'arguments' : [
'state1' ,
{ '__symbolic' : 'call' ,
'expression' : {
'__symbolic' : 'reference' ,
'name' : 'style' ,
'module' : 'angular2/src/core/animation/metadata'
2016-05-31 12:15:17 -04:00
} ,
2016-06-08 19:38:52 -04:00
'arguments' : [
{ 'background' : 'white' }
2016-05-31 12:15:17 -04:00
]
}
]
} , {
2016-06-08 19:38:52 -04:00
'__symbolic' : 'call' ,
'expression' : {
'__symbolic' : 'reference' ,
'name' : 'transition' ,
'module' : 'angular2/src/core/animation/metadata'
2016-05-31 12:15:17 -04:00
} ,
2016-06-08 19:38:52 -04:00
'arguments' : [
'* => *' ,
2016-05-31 12:15:17 -04:00
{
2016-06-08 19:38:52 -04:00
'__symbolic' : 'call' ,
'expression' : {
'__symbolic' : 'reference' ,
'name' : 'sequence' ,
'module' : 'angular2/src/core/animation/metadata'
2016-05-31 12:15:17 -04:00
} ,
2016-06-08 19:38:52 -04:00
'arguments' : [ [ { '__symbolic' : 'call' ,
'expression' : {
'__symbolic' : 'reference' ,
'name' : 'group' ,
'module' : 'angular2/src/core/animation/metadata'
2016-05-31 12:15:17 -04:00
} ,
2016-06-08 19:38:52 -04:00
'arguments' : [ [ {
'__symbolic' : 'call' ,
'expression' : {
'__symbolic' : 'reference' ,
'name' : 'animate' ,
'module' : 'angular2/src/core/animation/metadata'
2016-05-31 12:15:17 -04:00
} ,
2016-06-08 19:38:52 -04:00
'arguments' : [
'1s 0.5s' ,
{ '__symbolic' : 'call' ,
'expression' : {
'__symbolic' : 'reference' ,
'name' : 'keyframes' ,
'module' : 'angular2/src/core/animation/metadata'
2016-05-31 12:15:17 -04:00
} ,
2016-06-08 19:38:52 -04:00
'arguments' : [ [ { '__symbolic' : 'call' ,
'expression' : {
'__symbolic' : 'reference' ,
'name' : 'style' ,
'module' : 'angular2/src/core/animation/metadata'
2016-05-31 12:15:17 -04:00
} ,
2016-06-08 19:38:52 -04:00
'arguments' : [ { 'background' : 'blue' } ]
2016-05-31 12:15:17 -04:00
} , {
2016-06-08 19:38:52 -04:00
'__symbolic' : 'call' ,
'expression' : {
'__symbolic' : 'reference' ,
'name' : 'style' ,
'module' : 'angular2/src/core/animation/metadata'
2016-05-31 12:15:17 -04:00
} ,
2016-06-08 19:38:52 -04:00
'arguments' : [ { 'background' : 'red' } ]
2016-05-31 12:15:17 -04:00
} ] ]
}
]
} ] ]
} ] ]
}
]
}
]
2016-04-30 15:27:37 -04:00
]
2016-05-31 12:15:17 -04:00
} ]
} ]
} ] ,
2016-06-08 19:38:52 -04:00
'members' : {
'hero' : [
2016-04-30 15:27:37 -04:00
{
2016-06-08 19:38:52 -04:00
'__symbolic' : 'property' ,
'decorators' : [
2016-04-12 12:40:37 -04:00
{
2016-06-08 19:38:52 -04:00
'__symbolic' : 'call' ,
'expression' : {
'__symbolic' : 'reference' ,
'name' : 'Input' ,
'module' : 'angular2/src/core/metadata'
2016-04-30 15:27:37 -04:00
}
2016-04-12 12:40:37 -04:00
}
2016-04-30 15:27:37 -04:00
]
}
2016-07-27 05:05:07 -04:00
] ,
'onMouseOver' : [
{
'__symbolic' : 'method' ,
'decorators' : [
{
'__symbolic' : 'call' ,
'expression' : {
'__symbolic' : 'reference' ,
'module' : 'angular2/src/core/metadata' ,
'name' : 'HostListener'
} ,
'arguments' : [
'mouseover' ,
[
'$event'
]
]
}
]
}
]
2016-04-30 15:27:37 -04:00
}
}
}
} ,
2016-06-08 19:38:52 -04:00
'/src/extern.d.ts' : { '__symbolic' : 'module' , 'version' : 1 , metadata : { s : 's' } } ,
'/tmp/src/version-error.d.ts' : { '__symbolic' : 'module' , 'version' : 100 , metadata : { e : 's' } } ,
2016-06-03 18:43:09 -04:00
'/tmp/src/error-reporting.d.ts' : {
2016-06-08 19:38:52 -04:00
__symbolic : 'module' ,
2016-06-03 18:43:09 -04:00
version : 1 ,
metadata : {
SomeClass : {
2016-06-08 19:38:52 -04:00
__symbolic : 'class' ,
2016-06-03 18:43:09 -04:00
decorators : [
{
2016-06-08 19:38:52 -04:00
__symbolic : 'call' ,
2016-06-03 18:43:09 -04:00
expression : {
2016-06-08 19:38:52 -04:00
__symbolic : 'reference' ,
name : 'Component' ,
module : 'angular2/src/core/metadata'
2016-06-03 18:43:09 -04:00
} ,
arguments : [
{
2016-08-19 15:51:01 -04:00
entryComponents : [
2016-06-03 18:43:09 -04:00
{
2016-06-08 19:38:52 -04:00
__symbolic : 'reference' ,
module : 'src/error-references' ,
name : 'Link1' ,
2016-06-03 18:43:09 -04:00
}
]
}
]
}
] ,
}
}
} ,
'/tmp/src/error-references.d.ts' : {
2016-06-08 19:38:52 -04:00
__symbolic : 'module' ,
2016-06-03 18:43:09 -04:00
version : 1 ,
metadata : {
Link1 : {
2016-06-08 19:38:52 -04:00
__symbolic : 'reference' ,
module : 'src/error-references' ,
name : 'Link2'
2016-06-03 18:43:09 -04:00
} ,
Link2 : {
2016-06-08 19:38:52 -04:00
__symbolic : 'reference' ,
module : 'src/error-references' ,
name : 'ErrorSym'
2016-06-03 18:43:09 -04:00
} ,
ErrorSym : {
2016-06-08 19:38:52 -04:00
__symbolic : 'error' ,
message : 'A reasonable error message' ,
2016-06-03 18:43:09 -04:00
line : 12 ,
character : 33
}
}
2016-06-13 18:56:51 -04:00
} ,
'/tmp/src/function-declaration.d.ts' : {
__symbolic : 'module' ,
version : 1 ,
metadata : {
one : {
__symbolic : 'function' ,
parameters : [ 'a' ] ,
value : [
{ __symbolic : 'reference' , name : 'a' }
]
} ,
add : {
__symbolic : 'function' ,
parameters : [ 'a' , 'b' ] ,
value : {
__symbolic : 'binop' ,
operator : '+' ,
left : { __symbolic : 'reference' , name : 'a' } ,
2016-07-25 08:29:20 -04:00
right : {
__symbolic : 'binop' ,
operator : '+' ,
left : { __symbolic : 'reference' , name : 'b' } ,
right : { __symbolic : 'reference' , name : 'oneLiteral' }
}
2016-06-13 18:56:51 -04:00
}
2016-07-25 08:29:20 -04:00
} ,
oneLiteral : 1
2016-06-13 18:56:51 -04:00
}
} ,
'/tmp/src/function-reference.ts' : {
__symbolic : 'module' ,
version : 1 ,
metadata : {
one : {
__symbolic : 'call' ,
expression : {
__symbolic : 'reference' ,
module : './function-declaration' ,
name : 'one'
} ,
arguments : [ 'some-value' ]
} ,
2016-07-25 08:29:20 -04:00
three : {
2016-06-13 18:56:51 -04:00
__symbolic : 'call' ,
expression : {
__symbolic : 'reference' ,
module : './function-declaration' ,
name : 'add'
} ,
arguments : [ 1 , 1 ]
} ,
recursion : {
__symbolic : 'call' ,
expression : {
__symbolic : 'reference' ,
module : './function-recursive' ,
name : 'recursive'
} ,
arguments : [ 1 ]
} ,
indirectRecursion : {
__symbolic : 'call' ,
expression : {
__symbolic : 'reference' ,
module : './function-recursive' ,
name : 'indirectRecursion1'
} ,
arguments : [ 1 ]
}
}
} ,
'/tmp/src/function-recursive.d.ts' : {
__symbolic : 'modules' ,
version : 1 ,
metadata : {
recursive : {
__symbolic : 'function' ,
parameters : [ 'a' ] ,
value : {
__symbolic : 'call' ,
expression : {
__symbolic : 'reference' ,
module : './function-recursive' ,
name : 'recursive' ,
} ,
arguments : [
{
__symbolic : 'reference' ,
name : 'a'
}
]
}
} ,
indirectRecursion1 : {
__symbolic : 'function' ,
parameters : [ 'a' ] ,
value : {
__symbolic : 'call' ,
expression : {
__symbolic : 'reference' ,
module : './function-recursive' ,
name : 'indirectRecursion2' ,
} ,
arguments : [
{
__symbolic : 'reference' ,
name : 'a'
}
]
}
} ,
indirectRecursion2 : {
__symbolic : 'function' ,
parameters : [ 'a' ] ,
value : {
__symbolic : 'call' ,
expression : {
__symbolic : 'reference' ,
module : './function-recursive' ,
name : 'indirectRecursion1' ,
} ,
arguments : [
{
__symbolic : 'reference' ,
name : 'a'
}
]
}
}
} ,
} ,
'/tmp/src/spread.ts' : {
__symbolic : 'module' ,
version : 1 ,
metadata : {
spread : [ 0 , { __symbolic : 'spread' , expression : [ 1 , 2 , 3 , 4 ] } , 5 ]
}
2016-06-17 16:11:00 -04:00
} ,
'/tmp/src/custom-decorator.ts' : `
export function CustomDecorator ( ) : any {
return ( ) = > { } ;
}
` ,
'/tmp/src/custom-decorator-reference.ts' : `
import { CustomDecorator } from './custom-decorator' ;
@CustomDecorator ( )
export class Foo {
@CustomDecorator ( ) get foo ( ) : string { return '' ; }
}
` ,
'/tmp/src/invalid-calll-definitions.ts' : `
export function someFunction ( a : any ) {
if ( Array . isArray ( a ) ) {
return a ;
}
return undefined ;
}
` ,
'/tmp/src/invalid-calls.ts' : `
import { someFunction } from './nvalid-calll-definitions.ts' ;
import { Component } from 'angular2/src/core/metadata' ;
import { NgIf } from 'angular2/common' ;
@Component ( {
selector : 'my-component' ,
2016-08-19 15:51:01 -04:00
entryComponents : [ someFunction ( [ NgIf ] ) ]
2016-06-17 16:11:00 -04:00
} )
export class MyComponent { }
@someFunction ( )
@Component ( {
selector : 'my-component' ,
2016-08-19 15:51:01 -04:00
entryComponents : [ NgIf ]
2016-06-17 16:11:00 -04:00
} )
export class MyOtherComponent { }
2016-07-26 13:18:35 -04:00
` ,
'/tmp/src/static-method.ts' : `
import { Component } from 'angular2/src/core/metadata' ;
@Component ( {
selector : 'stub'
} )
export class MyModule {
static with ( data : any ) {
return { provider : 'a' , useValue : data }
}
2016-07-28 20:32:29 -04:00
static condMethod ( cond : boolean ) {
return [ { provider : 'a' , useValue : cond ? '1' : '2' } ] ;
}
2016-07-29 12:10:45 -04:00
static defaultsMethod ( a , b = true , c = false ) {
return [ a , b , c ] ;
}
2016-07-26 13:18:35 -04:00
}
` ,
'/tmp/src/static-method-call.ts' : `
import { Component } from 'angular2/src/core/metadata' ;
import { MyModule } from './static-method' ;
@Component ( {
providers : MyModule.with ( 100 )
} )
export class MyComponent { }
2016-07-28 20:32:29 -04:00
@Component ( {
providers : [ MyModule . condMethod ( true ) , MyModule . condMethod ( false ) ]
} )
export class MyCondComponent { }
2016-07-29 12:10:45 -04:00
@Component ( {
providers : [ MyModule . defaultsMethod ( 'a' ) ]
} )
export class MyDefaultsComponent { }
2016-07-27 22:26:59 -04:00
` ,
'/tmp/src/static-field.ts' : `
import { Injectable } from 'angular2/core' ;
@Injectable ( )
export class MyModule {
static VALUE = 'Some string' ;
}
` ,
'/tmp/src/static-field-reference.ts' : `
import { Component } from 'angular2/src/core/metadata' ;
import { MyModule } from './static-field' ;
@Component ( {
providers : [ { provider : 'a' , useValue : MyModule.VALUE } ]
} )
export class Foo { }
2016-08-23 14:58:12 -04:00
` ,
'/tmp/src/static-method-def.ts' : `
export class ClassWithStatics {
static staticMethod() { }
}
` ,
'/tmp/src/static-method-ref.ts' : `
import { Component } from 'angular2/src/core/metadata' ;
import { ClassWithStatics } from './static-method-def' ;
@Component ( {
providers : [ { provider : 'a' , useValue : ClassWithStatics.staticMethod } ]
} )
export class MethodReference {
}
2016-11-09 19:29:22 -05:00
` ,
'/tmp/src/invalid-metadata.ts' : `
import { Component } from 'angular2/src/core/metadata' ;
@Component ( {
providers : [ { provider : 'a' , useValue : ( ( ) = > 1 ) ( ) } ]
} )
export class InvalidMetadata { }
2016-11-15 12:19:00 -05:00
` ,
'/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 ;
}
2016-06-17 16:11:00 -04:00
`
2016-04-30 15:27:37 -04:00
} ;
2016-06-17 16:11:00 -04:00
if ( data [ module Id ] && module Id.match ( TS_EXT ) ) {
2016-11-12 08:08:58 -05:00
const text = data [ module Id ] ;
2016-06-17 16:11:00 -04:00
if ( typeof text === 'string' ) {
2016-11-12 08:08:58 -05:00
const sf = ts . createSourceFile (
2016-11-09 19:29:22 -05:00
module Id , data [ module Id ] , ts . ScriptTarget . ES5 , /* setParentNodes */ true ) ;
2016-11-12 08:08:58 -05:00
const diagnostics : ts.Diagnostic [ ] = ( < any > sf ) . parseDiagnostics ;
2016-06-17 16:11:00 -04:00
if ( diagnostics && diagnostics . length ) {
throw Error ( ` Error encountered during parse of file ${ module Id } ` ) ;
}
return this . collector . getMetadata ( sf ) ;
}
}
2016-04-30 15:27:37 -04:00
return data [ module Id ] ;
2016-03-24 13:03:10 -04:00
}
}