Skip to content

Commit

Permalink
fix(language-service): do not report errors for OpaqueToken (#19427)
Browse files Browse the repository at this point in the history
`OpaqueToken` was removed from angular but the language-service
should not report errors when it is used for older versions of
angular.
  • Loading branch information
chuckjaz authored and vicb committed Sep 28, 2017
1 parent 82e4923 commit c1b029a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/compiler/src/aot/static_reflector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class StaticReflector implements CompileReflector {
private methodCache = new Map<StaticSymbol, {[key: string]: boolean}>();
private conversionMap = new Map<StaticSymbol, (context: StaticSymbol, args: any[]) => any>();
private injectionToken: StaticSymbol;
private opaqueToken: StaticSymbol;
private ROUTES: StaticSymbol;
private ANALYZE_FOR_ENTRY_COMPONENTS: StaticSymbol;
private annotationForParentClassWithSummaryKind =
Expand Down Expand Up @@ -270,6 +271,7 @@ export class StaticReflector implements CompileReflector {

private initializeConversionMap(): void {
this.injectionToken = this.findDeclaration(ANGULAR_CORE, 'InjectionToken');
this.opaqueToken = this.findDeclaration(ANGULAR_CORE, 'OpaqueToken');
this.ROUTES = this.tryFindDeclaration(ANGULAR_ROUTER, 'ROUTES');
this.ANALYZE_FOR_ENTRY_COMPONENTS =
this.findDeclaration(ANGULAR_CORE, 'ANALYZE_FOR_ENTRY_COMPONENTS');
Expand Down Expand Up @@ -561,9 +563,12 @@ export class StaticReflector implements CompileReflector {
staticSymbol = simplifyInContext(
context, expression['expression'], depth + 1, /* references */ 0);
if (staticSymbol instanceof StaticSymbol) {
if (staticSymbol === self.injectionToken) {
if (staticSymbol === self.injectionToken || staticSymbol === self.opaqueToken) {
// if somebody calls new InjectionToken, don't create an InjectionToken,
// but rather return the symbol to which the InjectionToken is assigned to.

// OpaqueToken is supported too as it is required by the language service to
// support v4 and prior versions of Angular.
return context;
}
const argExpressions: any[] = expression['arguments'] || [];
Expand Down
22 changes: 22 additions & 0 deletions packages/language-service/test/diagnostics_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,28 @@ describe('diagnostics', () => {
expect(diagnostic).toEqual([]);
});

it('should not report errors for using the now removed OpaqueToken (support for v4)', () => {
const app_component = `
import { Component, Inject, OpaqueToken } from '@angular/core';
import { NgForm } from '@angular/common';
export const token = new OpaqueToken();
@Component({
selector: 'example-app',
template: '...'
})
export class AppComponent {
constructor (@Inject(token) value: string) {}
onSubmit(form: NgForm) {}
}
`;
const fileName = '/app/app.component.ts';
mockHost.override(fileName, app_component);
const diagnostics = ngService.getDiagnostics(fileName);
expect(diagnostics).toEqual([]);
});

function addCode(code: string, cb: (fileName: string, content?: string) => void) {
const fileName = '/app/app.component.ts';
const originalContent = mockHost.getFileContent(fileName);
Expand Down

0 comments on commit c1b029a

Please sign in to comment.