Skip to content

Commit

Permalink
refactor(core): remove deprecated OpaqueToken
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `OpaqueToken` has been removed as it was deprecated since v4. Use `InjectionToken` instead.
  • Loading branch information
ocombe committed Aug 31, 2017
1 parent c7e1bda commit 6c8a794
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 65 deletions.
2 changes: 1 addition & 1 deletion aio/content/examples/dependency-injection/e2e-spec.ts
Expand Up @@ -117,7 +117,7 @@ describe('Dependency Injection Tests', function () {
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';
expect(element(by.css('#p9')).getText()).toEqual(expectedMsg);
});
Expand Down
8 changes: 1 addition & 7 deletions aio/content/guide/metadata.md
Expand Up @@ -280,13 +280,7 @@ Most importantly, the compiler only generates code to create instances of certai
### New instances
The compiler only allows metadata that create instances of these Angular classes.
Class | Module
-----------------|--------------
`OpaqueToken` | `@angular/core`
`InjectionToken` | `@angular/core`
The compiler only allows metadata that create instances of the class `InjectionToken` from `@angular/core`.
### Annotations/Decorators
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler-cli/integrationtest/src/module.ts
Expand Up @@ -8,8 +8,8 @@

import {ApplicationRef, NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {MATERIAL_SANITY_CHECKS, MdButtonModule} from '@angular/material';
import {ServerModule} from '@angular/platform-server';
import {MdButtonModule} from '@angular2-material/button';
import {FlatModule} from 'flat_module';
// Note: don't refer to third_party_src as we want to test that
// we can compile components from node_modules!
Expand Down Expand Up @@ -69,6 +69,7 @@ export {SomeModule as JitSummariesSomeModule} from './jit_summaries';
providers: [
SomeService,
{provide: CUSTOM, useValue: {name: 'some name'}},
{provide: MATERIAL_SANITY_CHECKS, useValue: false},
],
entryComponents: [
AnimateCmp,
Expand Down
3 changes: 1 addition & 2 deletions packages/compiler-cli/integrationtest/test/basic_spec.ts
Expand Up @@ -37,8 +37,7 @@ describe('template codegen output', () => {
});

it('should write .ngfactory.js for .d.ts inputs', () => {
const factoryOutput =
path.join('node_modules', '@angular2-material', 'button', 'button.ngfactory.js');
const factoryOutput = path.join('node_modules', '@angular', 'common', 'common.ngfactory.js');
expect(fs.existsSync(factoryOutput)).toBeTruthy();
});

Expand Down
8 changes: 3 additions & 5 deletions packages/compiler/src/aot/static_reflector.ts
Expand Up @@ -44,7 +44,6 @@ 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 @@ -271,7 +270,6 @@ 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 @@ -435,8 +433,8 @@ export class StaticReflector implements CompileReflector {
}
if (expression instanceof StaticSymbol) {
// Stop simplification at builtin symbols or if we are in a reference context
if (expression === self.injectionToken || expression === self.opaqueToken ||
self.conversionMap.has(expression) || references > 0) {
if (expression === self.injectionToken || self.conversionMap.has(expression) ||
references > 0) {
return expression;
} else {
const staticSymbol = expression;
Expand Down Expand Up @@ -563,7 +561,7 @@ export class StaticReflector implements CompileReflector {
staticSymbol = simplifyInContext(
context, expression['expression'], depth + 1, /* references */ 0);
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,
// but rather return the symbol to which the InjectionToken is assigned to.
return context;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/di.ts
Expand Up @@ -21,4 +21,4 @@ export {ReflectiveInjector} from './di/reflective_injector';
export {StaticProvider, ValueProvider, ExistingProvider, FactoryProvider, Provider, TypeProvider, ClassProvider} from './di/provider';
export {ResolvedReflectiveFactory, ResolvedReflectiveProvider} from './di/reflective_provider';
export {ReflectiveKey} from './di/reflective_key';
export {InjectionToken, OpaqueToken} from './di/injection_token';
export {InjectionToken} from './di/injection_token';
36 changes: 2 additions & 34 deletions packages/core/src/di/injection_token.ts
Expand Up @@ -6,35 +6,6 @@
* 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.
*
Expand All @@ -57,11 +28,8 @@ export class OpaqueToken {
*
* @stable
*/
export class InjectionToken<T> extends OpaqueToken {
// This unused property is needed here so that TS can differentiate InjectionToken from
// 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); }
export class InjectionToken<T> {
constructor(protected _desc: string) {}

toString(): string { return `InjectionToken ${this._desc}`; }

Expand Down
6 changes: 2 additions & 4 deletions packages/tsc-wrapped/test/collector_spec.ts
Expand Up @@ -734,10 +734,8 @@ describe('Collector', () => {
it('should treat exported class expressions as a class', () => {
const source = ts.createSourceFile(
'', `
export const InjectionToken: {new<T>(desc: string): InjectionToken<T>;} = class extends OpaqueToken {
constructor(desc: string) {
super(desc);
}
export const InjectionToken: {new<T>(desc: string): InjectionToken<T>;} = class {
constructor(protected _desc: string) {}
toString(): string { return \`InjectionToken ${this._desc}\`; }
} as any;`,
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci/offline_compiler_test.sh
Expand Up @@ -17,7 +17,7 @@ PKGS=(
jasmine@2.4.1
webpack@2.1.0-beta.21
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:-.}
Expand Down
12 changes: 3 additions & 9 deletions tools/public_api_guard/core/core.d.ts
Expand Up @@ -465,8 +465,9 @@ export interface InjectDecorator {
}

/** @stable */
export declare class InjectionToken<T> extends OpaqueToken {
constructor(desc: string);
export declare class InjectionToken<T> {
protected _desc: string;
constructor(_desc: string);
toString(): string;
}

Expand Down Expand Up @@ -661,13 +662,6 @@ export interface OnInit {
ngOnInit(): void;
}

/** @deprecated */
export declare class OpaqueToken {
protected _desc: string;
constructor(_desc: string);
toString(): string;
}

/** @stable */
export declare const Optional: OptionalDecorator;

Expand Down

0 comments on commit 6c8a794

Please sign in to comment.