fix(language-service): do not report errors for `OpaqueToken` (#19427)

`OpaqueToken` was removed from angular but the language-service
should not report errors when it is used for older versions of
angular.
This commit is contained in:
Chuck Jazdzewski 2017-09-28 09:30:34 -07:00 committed by Victor Berchet
parent 82e49230ff
commit c1b029a413
2 changed files with 28 additions and 1 deletions

View File

@ -44,6 +44,7 @@ export class StaticReflector implements CompileReflector {
private methodCache = new Map<StaticSymbol, {[key: string]: boolean}>(); private methodCache = new Map<StaticSymbol, {[key: string]: boolean}>();
private conversionMap = new Map<StaticSymbol, (context: StaticSymbol, args: any[]) => any>(); private conversionMap = new Map<StaticSymbol, (context: StaticSymbol, args: any[]) => any>();
private injectionToken: StaticSymbol; private injectionToken: StaticSymbol;
private opaqueToken: StaticSymbol;
private ROUTES: StaticSymbol; private ROUTES: StaticSymbol;
private ANALYZE_FOR_ENTRY_COMPONENTS: StaticSymbol; private ANALYZE_FOR_ENTRY_COMPONENTS: StaticSymbol;
private annotationForParentClassWithSummaryKind = private annotationForParentClassWithSummaryKind =
@ -270,6 +271,7 @@ export class StaticReflector implements CompileReflector {
private initializeConversionMap(): void { private initializeConversionMap(): void {
this.injectionToken = this.findDeclaration(ANGULAR_CORE, 'InjectionToken'); this.injectionToken = this.findDeclaration(ANGULAR_CORE, 'InjectionToken');
this.opaqueToken = this.findDeclaration(ANGULAR_CORE, 'OpaqueToken');
this.ROUTES = this.tryFindDeclaration(ANGULAR_ROUTER, 'ROUTES'); this.ROUTES = this.tryFindDeclaration(ANGULAR_ROUTER, 'ROUTES');
this.ANALYZE_FOR_ENTRY_COMPONENTS = this.ANALYZE_FOR_ENTRY_COMPONENTS =
this.findDeclaration(ANGULAR_CORE, 'ANALYZE_FOR_ENTRY_COMPONENTS'); this.findDeclaration(ANGULAR_CORE, 'ANALYZE_FOR_ENTRY_COMPONENTS');
@ -561,9 +563,12 @@ export class StaticReflector implements CompileReflector {
staticSymbol = simplifyInContext( staticSymbol = simplifyInContext(
context, expression['expression'], depth + 1, /* references */ 0); context, expression['expression'], depth + 1, /* references */ 0);
if (staticSymbol instanceof StaticSymbol) { 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, // if somebody calls new InjectionToken, don't create an InjectionToken,
// but rather return the symbol to which the InjectionToken is assigned to. // 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; return context;
} }
const argExpressions: any[] = expression['arguments'] || []; const argExpressions: any[] = expression['arguments'] || [];

View File

@ -314,6 +314,28 @@ describe('diagnostics', () => {
expect(diagnostic).toEqual([]); 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) { function addCode(code: string, cb: (fileName: string, content?: string) => void) {
const fileName = '/app/app.component.ts'; const fileName = '/app/app.component.ts';
const originalContent = mockHost.getFileContent(fileName); const originalContent = mockHost.getFileContent(fileName);