refactor(core): remove deprecated `OpaqueToken` (#18971)

BREAKING CHANGE: `OpaqueToken` has been removed as it was deprecated since v4. Use `InjectionToken` instead.
PR Close #18971
This commit is contained in:
Olivier Combe 2017-08-31 10:56:26 +02:00 committed by Miško Hevery
parent 36d37cc6ae
commit 3c4eef8a86
10 changed files with 19 additions and 65 deletions

View File

@ -117,7 +117,7 @@ describe('Dependency Injection Tests', function () {
expect(element(by.css('#p8')).getText()).toEqual(expectedMsg); expect(element(by.css('#p8')).getText()).toEqual(expectedMsg);
}); });
it('P9 (OpaqueToken) displays as expected', function () { it('P9 (InjectionToken) displays as expected', function () {
expectedMsg = 'APP_CONFIG Application title is Dependency Injection'; expectedMsg = 'APP_CONFIG Application title is Dependency Injection';
expect(element(by.css('#p9')).getText()).toEqual(expectedMsg); expect(element(by.css('#p9')).getText()).toEqual(expectedMsg);
}); });

View File

@ -280,13 +280,7 @@ Most importantly, the compiler only generates code to create instances of certai
### New instances ### New instances
The compiler only allows metadata that create instances of these Angular classes. The compiler only allows metadata that create instances of the class `InjectionToken` from `@angular/core`.
Class | Module
-----------------|--------------
`OpaqueToken` | `@angular/core`
`InjectionToken` | `@angular/core`
### Annotations/Decorators ### Annotations/Decorators

View File

@ -8,8 +8,8 @@
import {ApplicationRef, NgModule} from '@angular/core'; import {ApplicationRef, NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms'; import {FormsModule} from '@angular/forms';
import {MATERIAL_SANITY_CHECKS, MdButtonModule} from '@angular/material';
import {ServerModule} from '@angular/platform-server'; import {ServerModule} from '@angular/platform-server';
import {MdButtonModule} from '@angular2-material/button';
import {FlatModule} from 'flat_module'; import {FlatModule} from 'flat_module';
// Note: don't refer to third_party_src as we want to test that // Note: don't refer to third_party_src as we want to test that
// we can compile components from node_modules! // we can compile components from node_modules!
@ -69,6 +69,9 @@ export {SomeModule as JitSummariesSomeModule} from './jit_summaries';
providers: [ providers: [
SomeService, SomeService,
{provide: CUSTOM, useValue: {name: 'some name'}}, {provide: CUSTOM, useValue: {name: 'some name'}},
// disable sanity check for material because it throws an error when used server-side
// see https://github.com/angular/material2/issues/6292
{provide: MATERIAL_SANITY_CHECKS, useValue: false},
], ],
entryComponents: [ entryComponents: [
AnimateCmp, AnimateCmp,

View File

@ -37,8 +37,7 @@ describe('template codegen output', () => {
}); });
it('should write .ngfactory.js for .d.ts inputs', () => { it('should write .ngfactory.js for .d.ts inputs', () => {
const factoryOutput = const factoryOutput = path.join('node_modules', '@angular', 'common', 'index.ngfactory.js');
path.join('node_modules', '@angular2-material', 'button', 'button.ngfactory.js');
expect(fs.existsSync(factoryOutput)).toBeTruthy(); expect(fs.existsSync(factoryOutput)).toBeTruthy();
}); });

View File

@ -44,7 +44,6 @@ 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 =
@ -271,7 +270,6 @@ 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');
@ -435,8 +433,8 @@ export class StaticReflector implements CompileReflector {
} }
if (expression instanceof StaticSymbol) { if (expression instanceof StaticSymbol) {
// Stop simplification at builtin symbols or if we are in a reference context // Stop simplification at builtin symbols or if we are in a reference context
if (expression === self.injectionToken || expression === self.opaqueToken || if (expression === self.injectionToken || self.conversionMap.has(expression) ||
self.conversionMap.has(expression) || references > 0) { references > 0) {
return expression; return expression;
} else { } else {
const staticSymbol = expression; const staticSymbol = expression;
@ -563,7 +561,7 @@ 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 || staticSymbol === self.opaqueToken) { if (staticSymbol === self.injectionToken) {
// 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.
return context; return context;

View File

@ -21,4 +21,4 @@ export {ReflectiveInjector} from './di/reflective_injector';
export {StaticProvider, ValueProvider, ExistingProvider, FactoryProvider, Provider, TypeProvider, ClassProvider} from './di/provider'; export {StaticProvider, ValueProvider, ExistingProvider, FactoryProvider, Provider, TypeProvider, ClassProvider} from './di/provider';
export {ResolvedReflectiveFactory, ResolvedReflectiveProvider} from './di/reflective_provider'; export {ResolvedReflectiveFactory, ResolvedReflectiveProvider} from './di/reflective_provider';
export {ReflectiveKey} from './di/reflective_key'; export {ReflectiveKey} from './di/reflective_key';
export {InjectionToken, OpaqueToken} from './di/injection_token'; export {InjectionToken} from './di/injection_token';

View File

@ -6,35 +6,6 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
/**
* Creates a token that can be used in a DI Provider.
*
* ### Example ([live demo](http://plnkr.co/edit/Ys9ezXpj2Mnoy3Uc8KBp?p=preview))
*
* ```typescript
* var t = new OpaqueToken("value");
*
* var injector = Injector.resolveAndCreate([
* {provide: t, useValue: "bindingValue"}
* ]);
*
* expect(injector.get(t)).toEqual("bindingValue");
* ```
*
* Using an `OpaqueToken` is preferable to using strings as tokens because of possible collisions
* caused by multiple providers using the same string as two different tokens.
*
* Using an `OpaqueToken` is preferable to using an `Object` as tokens because it provides better
* error messages.
* @deprecated since v4.0.0 because it does not support type information, use `InjectionToken<?>`
* instead.
*/
export class OpaqueToken {
constructor(protected _desc: string) {}
toString(): string { return `Token ${this._desc}`; }
}
/** /**
* Creates a token that can be used in a DI Provider. * Creates a token that can be used in a DI Provider.
* *
@ -57,11 +28,8 @@ export class OpaqueToken {
* *
* @stable * @stable
*/ */
export class InjectionToken<T> extends OpaqueToken { export class InjectionToken<T> {
// This unused property is needed here so that TS can differentiate InjectionToken from constructor(protected _desc: string) {}
// OpaqueToken since otherwise they would have the same shape and be treated as equivalent.
private _differentiate_from_OpaqueToken_structurally: any;
constructor(desc: string) { super(desc); }
toString(): string { return `InjectionToken ${this._desc}`; } toString(): string { return `InjectionToken ${this._desc}`; }

View File

@ -734,10 +734,8 @@ describe('Collector', () => {
it('should treat exported class expressions as a class', () => { it('should treat exported class expressions as a class', () => {
const source = ts.createSourceFile( const source = ts.createSourceFile(
'', ` '', `
export const InjectionToken: {new<T>(desc: string): InjectionToken<T>;} = class extends OpaqueToken { export const InjectionToken: {new<T>(desc: string): InjectionToken<T>;} = class {
constructor(desc: string) { constructor(protected _desc: string) {}
super(desc);
}
toString(): string { return \`InjectionToken ${this._desc}\`; } toString(): string { return \`InjectionToken ${this._desc}\`; }
} as any;`, } as any;`,

View File

@ -17,7 +17,7 @@ PKGS=(
jasmine@2.4.1 jasmine@2.4.1
webpack@2.1.0-beta.21 webpack@2.1.0-beta.21
source-map-loader@0.2.0 source-map-loader@0.2.0
@angular2-material/{core,button}@2.0.0-alpha.8-1 @angular/{material,cdk}@2.0.0-beta.10
) )
TMPDIR=${TMPDIR:-.} TMPDIR=${TMPDIR:-.}

View File

@ -465,8 +465,9 @@ export interface InjectDecorator {
} }
/** @stable */ /** @stable */
export declare class InjectionToken<T> extends OpaqueToken { export declare class InjectionToken<T> {
constructor(desc: string); protected _desc: string;
constructor(_desc: string);
toString(): string; toString(): string;
} }
@ -661,13 +662,6 @@ export interface OnInit {
ngOnInit(): void; ngOnInit(): void;
} }
/** @deprecated */
export declare class OpaqueToken {
protected _desc: string;
constructor(_desc: string);
toString(): string;
}
/** @stable */ /** @stable */
export declare const Optional: OptionalDecorator; export declare const Optional: OptionalDecorator;