Skip to content

Commit

Permalink
fix(compiler): don't throw when Symbol is used as DI token
Browse files Browse the repository at this point in the history
Closes #13314
  • Loading branch information
Dzmitry Shylovich authored and Dzmitry Shylovich committed Dec 29, 2016
1 parent 6b02b80 commit aa0955b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 9 additions & 0 deletions modules/@angular/compiler/src/compile_metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function _sanitizeIdentifier(name: string): string {
}

let _anonymousTypeIndex = 0;
const symbols = new Map<Symbol, string>();

export function identifierName(compileIdentifier: CompileIdentifierMetadata): string {
if (!compileIdentifier || !compileIdentifier.reference) {
Expand All @@ -90,6 +91,14 @@ export function identifierName(compileIdentifier: CompileIdentifierMetadata): st
if (ref instanceof StaticSymbol) {
return ref.name;
}
if (typeof ref === 'symbol') {
if (symbols.has(ref)) {
return symbols.get(ref);
}
const symbolStr = ref.toString().replace(/\(|\)/g, '_');
symbols.set(ref, symbolStr);
return symbolStr;
}
if (ref['__anonymousType']) {
return ref['__anonymousType'];
}
Expand Down
19 changes: 18 additions & 1 deletion modules/@angular/compiler/test/integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Component, Directive, Input} from '@angular/core';
import {Component, Directive, Inject, Input} from '@angular/core';
import {ComponentFixture, TestBed, async} from '@angular/core/testing';
import {By} from '@angular/platform-browser/src/dom/debug/by';
import {expect} from '@angular/platform-browser/testing/matchers';
Expand Down Expand Up @@ -38,6 +38,23 @@ export function main() {
}));
});

it('should not throw when Symbol is used as DI token', async(() => {
const SOME_SYMBOL = Symbol('SomeSymbol');

@Component({selector: 'symbol', template: ''})
class CmpWithSymbol {
constructor(@Inject(SOME_SYMBOL) public symbol: string) {}
}

TestBed.configureTestingModule({
declarations: [CmpWithSymbol],
providers: [{provide: SOME_SYMBOL, useValue: 'value'}]
});

const fixture = TestBed.createComponent(CmpWithSymbol);
fixture.detectChanges();
expect(fixture.componentInstance.symbol).toEqual('value');
}));
});
}

Expand Down

0 comments on commit aa0955b

Please sign in to comment.