From f0764016f917636fc3d85d85bd56b6eb0d26a044 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 3 Nov 2017 14:51:06 -0700 Subject: [PATCH] revert: feat(core): add source to `StaticInjectorError` message (#19482) (#20171) This reverts commit faa621218e0f55b38cb2bcf58e5e93066030e361. --- packages/core/src/application_ref.ts | 12 ++--- packages/core/src/di/injector.ts | 33 +++----------- packages/core/src/view/ng_module.ts | 2 +- packages/core/src/view/provider.ts | 2 +- packages/core/src/view/util.ts | 7 +-- packages/core/test/view/provider_spec.ts | 12 ++--- packages/core/testing/src/test_bed.ts | 10 ++--- packages/examples/core/di/ts/provider_spec.ts | 2 +- .../test/browser/bootstrap_spec.ts | 45 +++---------------- .../src/common/downgrade_component_adapter.ts | 7 ++- tools/public_api_guard/core/core.d.ts | 7 +-- 11 files changed, 34 insertions(+), 105 deletions(-) diff --git a/packages/core/src/application_ref.ts b/packages/core/src/application_ref.ts index 87e8bfb1ff..95f7ebbe97 100644 --- a/packages/core/src/application_ref.ts +++ b/packages/core/src/application_ref.ts @@ -102,8 +102,7 @@ export function createPlatformFactory( parentPlatformFactory: ((extraProviders?: StaticProvider[]) => PlatformRef) | null, name: string, providers: StaticProvider[] = []): (extraProviders?: StaticProvider[]) => PlatformRef { - const desc = `Platform: ${name}`; - const marker = new InjectionToken(desc); + const marker = new InjectionToken(`Platform: ${name}`); return (extraProviders: StaticProvider[] = []) => { let platform = getPlatform(); if (!platform || platform.injector.get(ALLOW_MULTIPLE_PLATFORMS, false)) { @@ -111,9 +110,8 @@ export function createPlatformFactory( parentPlatformFactory( providers.concat(extraProviders).concat({provide: marker, useValue: true})); } else { - const injectedProviders: StaticProvider[] = - providers.concat(extraProviders).concat({provide: marker, useValue: true}); - createPlatform(Injector.create({providers: injectedProviders, name: desc})); + createPlatform(Injector.create( + providers.concat(extraProviders).concat({provide: marker, useValue: true}))); } } return assertPlatform(marker); @@ -226,12 +224,10 @@ export class PlatformRef { // pass that as parent to the NgModuleFactory. const ngZoneOption = options ? options.ngZone : undefined; const ngZone = getNgZone(ngZoneOption); - const providers: StaticProvider[] = [{provide: NgZone, useValue: ngZone}]; // Attention: Don't use ApplicationRef.run here, // as we want to be sure that all possible constructor calls are inside `ngZone.run`! return ngZone.run(() => { - const ngZoneInjector = Injector.create( - {providers: providers, parent: this.injector, name: moduleFactory.moduleType.name}); + const ngZoneInjector = Injector.create([{provide: NgZone, useValue: ngZone}], this.injector); const moduleRef = >moduleFactory.create(ngZoneInjector); const exceptionHandler: ErrorHandler = moduleRef.injector.get(ErrorHandler, null); if (!exceptionHandler) { diff --git a/packages/core/src/di/injector.ts b/packages/core/src/di/injector.ts index e6df410642..a0a380a414 100644 --- a/packages/core/src/di/injector.ts +++ b/packages/core/src/di/injector.ts @@ -8,12 +8,12 @@ import {Type} from '../type'; import {stringify} from '../util'; + import {resolveForwardRef} from './forward_ref'; import {InjectionToken} from './injection_token'; import {Inject, Optional, Self, SkipSelf} from './metadata'; import {ConstructorProvider, ExistingProvider, FactoryProvider, StaticClassProvider, StaticProvider, ValueProvider} from './provider'; -export const SOURCE = '__source'; const _THROW_IF_NOT_FOUND = new Object(); export const THROW_IF_NOT_FOUND = _THROW_IF_NOT_FOUND; @@ -64,13 +64,6 @@ export abstract class Injector { */ abstract get(token: any, notFoundValue?: any): any; - /** - * @deprecated from v5 use the new signature Injector.create(options) - */ - static create(providers: StaticProvider[], parent?: Injector): Injector; - - static create(options: {providers: StaticProvider[], parent?: Injector, name?: string}): Injector; - /** * Create a new Injector which is configure using `StaticProvider`s. * @@ -78,14 +71,8 @@ export abstract class Injector { * * {@example core/di/ts/provider_spec.ts region='ConstructorProvider'} */ - static create( - options: StaticProvider[]|{providers: StaticProvider[], parent?: Injector, name?: string}, - parent?: Injector): Injector { - if (Array.isArray(options)) { - return new StaticInjector(options, parent); - } else { - return new StaticInjector(options.providers, options.parent, options.name || null); - } + static create(providers: StaticProvider[], parent?: Injector): Injector { + return new StaticInjector(providers, parent); } } @@ -116,14 +103,11 @@ const NO_NEW_LINE = 'ɵ'; export class StaticInjector implements Injector { readonly parent: Injector; - readonly source: string|null; private _records: Map; - constructor( - providers: StaticProvider[], parent: Injector = NULL_INJECTOR, source: string|null = null) { + constructor(providers: StaticProvider[], parent: Injector = NULL_INJECTOR) { this.parent = parent; - this.source = source; const records = this._records = new Map(); records.set( Injector, {token: Injector, fn: IDENT, deps: EMPTY, value: this, useNew: false}); @@ -138,10 +122,7 @@ export class StaticInjector implements Injector { return tryResolveToken(token, record, this._records, this.parent, notFoundValue); } catch (e) { const tokenPath: any[] = e[NG_TEMP_TOKEN_PATH]; - if (token[SOURCE]) { - tokenPath.unshift(token[SOURCE]); - } - e.message = formatError('\n' + e.message, tokenPath, this.source); + e.message = formatError('\n' + e.message, tokenPath); e[NG_TOKEN_PATH] = tokenPath; e[NG_TEMP_TOKEN_PATH] = null; throw e; @@ -355,7 +336,7 @@ function computeDeps(provider: StaticProvider): DependencyRecord[] { return deps; } -function formatError(text: string, obj: any, source: string | null = null): string { +function formatError(text: string, obj: any): string { text = text && text.charAt(0) === '\n' && text.charAt(1) == NO_NEW_LINE ? text.substr(2) : text; let context = stringify(obj); if (obj instanceof Array) { @@ -371,7 +352,7 @@ function formatError(text: string, obj: any, source: string | null = null): stri } context = `{${parts.join(', ')}}`; } - return `StaticInjectorError${source ? '(' + source + ')' : ''}[${context}]: ${text.replace(NEW_LINE, '\n ')}`; + return `StaticInjectorError[${context}]: ${text.replace(NEW_LINE, '\n ')}`; } function staticError(text: string, obj: any): Error { diff --git a/packages/core/src/view/ng_module.ts b/packages/core/src/view/ng_module.ts index 7734b8517c..5c57bf278e 100644 --- a/packages/core/src/view/ng_module.ts +++ b/packages/core/src/view/ng_module.ts @@ -25,7 +25,7 @@ export function moduleProvideDef( // lowered the expression and then stopped evaluating it, // i.e. also didn't unwrap it. value = resolveForwardRef(value); - const depDefs = splitDepsDsl(deps, token.name); + const depDefs = splitDepsDsl(deps); return { // will bet set by the module definition index: -1, diff --git a/packages/core/src/view/provider.ts b/packages/core/src/view/provider.ts index 3b53b61b6e..f8ae12049c 100644 --- a/packages/core/src/view/provider.ts +++ b/packages/core/src/view/provider.ts @@ -83,7 +83,7 @@ export function _def( // i.e. also didn't unwrap it. value = resolveForwardRef(value); - const depDefs = splitDepsDsl(deps, token.name); + const depDefs = splitDepsDsl(deps); return { // will bet set by the view definition diff --git a/packages/core/src/view/util.ts b/packages/core/src/view/util.ts index 832f05cdbe..a1b3219a9e 100644 --- a/packages/core/src/view/util.ts +++ b/packages/core/src/view/util.ts @@ -7,10 +7,10 @@ */ import {WrappedValue, devModeEqual} from '../change_detection/change_detection'; -import {SOURCE} from '../di/injector'; import {ViewEncapsulation} from '../metadata/view'; import {RendererType2} from '../render/api'; import {looseIdentical, stringify} from '../util'; + import {expressionChangedAfterItHasBeenCheckedError} from './errors'; import {BindingDef, BindingFlags, Definition, DefinitionFactory, DepDef, DepFlags, ElementData, NodeDef, NodeFlags, QueryValueType, Services, ViewData, ViewDefinition, ViewDefinitionFactory, ViewFlags, ViewState, asElementData, asTextData} from './types'; @@ -209,7 +209,7 @@ export function splitMatchedQueriesDsl( return {matchedQueries, references, matchedQueryIds}; } -export function splitDepsDsl(deps: ([DepFlags, any] | any)[], sourceName?: string): DepDef[] { +export function splitDepsDsl(deps: ([DepFlags, any] | any)[]): DepDef[] { return deps.map(value => { let token: any; let flags: DepFlags; @@ -219,9 +219,6 @@ export function splitDepsDsl(deps: ([DepFlags, any] | any)[], sourceName?: strin flags = DepFlags.None; token = value; } - if (token && (typeof token === 'function' || typeof token === 'object') && sourceName) { - Object.defineProperty(token, SOURCE, {value: sourceName, configurable: true}); - } return {flags, token, tokenKey: tokenKey(token)}; }); } diff --git a/packages/core/test/view/provider_spec.ts b/packages/core/test/view/provider_spec.ts index fed49c5ca8..f1941e687e 100644 --- a/packages/core/test/view/provider_spec.ts +++ b/packages/core/test/view/provider_spec.ts @@ -147,8 +147,8 @@ export function main() { expect(() => createAndGetRootNodes(compViewDef(rootElNodes))) .toThrowError( - 'StaticInjectorError(DynamicTestModule)[SomeService -> Dep]: \n' + - ' StaticInjectorError(Platform: core)[SomeService -> Dep]: \n' + + 'StaticInjectorError[Dep]: \n' + + ' StaticInjectorError[Dep]: \n' + ' NullInjectorError: No provider for Dep!'); const nonRootElNodes = [ @@ -161,8 +161,8 @@ export function main() { expect(() => createAndGetRootNodes(compViewDef(nonRootElNodes))) .toThrowError( - 'StaticInjectorError(DynamicTestModule)[SomeService -> Dep]: \n' + - ' StaticInjectorError(Platform: core)[SomeService -> Dep]: \n' + + 'StaticInjectorError[Dep]: \n' + + ' StaticInjectorError[Dep]: \n' + ' NullInjectorError: No provider for Dep!'); }); @@ -186,8 +186,8 @@ export function main() { directiveDef(1, NodeFlags.None, null, 0, SomeService, ['nonExistingDep']) ]))) .toThrowError( - 'StaticInjectorError(DynamicTestModule)[nonExistingDep]: \n' + - ' StaticInjectorError(Platform: core)[nonExistingDep]: \n' + + 'StaticInjectorError[nonExistingDep]: \n' + + ' StaticInjectorError[nonExistingDep]: \n' + ' NullInjectorError: No provider for nonExistingDep!'); }); diff --git a/packages/core/testing/src/test_bed.ts b/packages/core/testing/src/test_bed.ts index e0e1950217..5eaa822063 100644 --- a/packages/core/testing/src/test_bed.ts +++ b/packages/core/testing/src/test_bed.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {ApplicationInitStatus, CompilerOptions, Component, Directive, InjectionToken, Injector, ModuleWithComponentFactories, NgModule, NgModuleFactory, NgModuleRef, NgZone, Optional, Pipe, PlatformRef, Provider, SchemaMetadata, SkipSelf, StaticProvider, Type, ɵDepFlags as DepFlags, ɵNodeFlags as NodeFlags, ɵclearProviderOverrides as clearProviderOverrides, ɵoverrideProvider as overrideProvider, ɵstringify as stringify} from '@angular/core'; +import {ApplicationInitStatus, CompilerOptions, Component, Directive, InjectionToken, Injector, ModuleWithComponentFactories, NgModule, NgModuleFactory, NgModuleRef, NgZone, Optional, Pipe, PlatformRef, Provider, SchemaMetadata, SkipSelf, Type, ɵDepFlags as DepFlags, ɵNodeFlags as NodeFlags, ɵclearProviderOverrides as clearProviderOverrides, ɵoverrideProvider as overrideProvider, ɵstringify as stringify} from '@angular/core'; import {AsyncTestCompleter} from './async_test_completer'; import {ComponentFixture} from './component_fixture'; @@ -328,12 +328,8 @@ export class TestBed implements Injector { } } const ngZone = new NgZone({enableLongStackTrace: true}); - const providers: StaticProvider[] = [{provide: NgZone, useValue: ngZone}]; - const ngZoneInjector = Injector.create({ - providers: providers, - parent: this.platform.injector, - name: this._moduleFactory.moduleType.name - }); + const ngZoneInjector = + Injector.create([{provide: NgZone, useValue: ngZone}], this.platform.injector); this._moduleRef = this._moduleFactory.create(ngZoneInjector); // ApplicationInitStatus.runInitializers() is marked @internal to core. So casting to any // before accessing it. diff --git a/packages/examples/core/di/ts/provider_spec.ts b/packages/examples/core/di/ts/provider_spec.ts index e50e36428e..4a273df1dc 100644 --- a/packages/examples/core/di/ts/provider_spec.ts +++ b/packages/examples/core/di/ts/provider_spec.ts @@ -135,7 +135,7 @@ export function main() { name = 'square'; } - const injector = Injector.create({providers: [{provide: Square, deps: []}]}); + const injector = Injector.create([{provide: Square, deps: []}]); const shape: Square = injector.get(Square); expect(shape.name).toEqual('square'); diff --git a/packages/platform-browser/test/browser/bootstrap_spec.ts b/packages/platform-browser/test/browser/bootstrap_spec.ts index e028cbbc1f..f1b0cf2926 100644 --- a/packages/platform-browser/test/browser/bootstrap_spec.ts +++ b/packages/platform-browser/test/browser/bootstrap_spec.ts @@ -7,12 +7,12 @@ */ import {isPlatformBrowser} from '@angular/common'; -import {APP_INITIALIZER, CUSTOM_ELEMENTS_SCHEMA, Compiler, Component, Directive, ErrorHandler, Inject, Input, LOCALE_ID, NgModule, OnDestroy, PLATFORM_ID, PLATFORM_INITIALIZER, Pipe, Provider, StaticProvider, Type, VERSION, createPlatformFactory} from '@angular/core'; +import {APP_INITIALIZER, CUSTOM_ELEMENTS_SCHEMA, Compiler, Component, Directive, ErrorHandler, Inject, Input, LOCALE_ID, NgModule, OnDestroy, PLATFORM_ID, PLATFORM_INITIALIZER, Pipe, Provider, StaticProvider, VERSION, createPlatformFactory, ɵstringify as stringify} from '@angular/core'; import {ApplicationRef, destroyPlatform} from '@angular/core/src/application_ref'; import {Console} from '@angular/core/src/console'; import {ComponentRef} from '@angular/core/src/linker/component_factory'; import {Testability, TestabilityRegistry} from '@angular/core/src/testability/testability'; -import {AsyncTestCompleter, Log, afterEach, beforeEach, beforeEachProviders, describe, iit, inject, it} from '@angular/core/testing/src/testing_internal'; +import {AsyncTestCompleter, Log, afterEach, beforeEach, beforeEachProviders, ddescribe, describe, iit, inject, it} from '@angular/core/testing/src/testing_internal'; import {BrowserModule} from '@angular/platform-browser'; import {platformBrowserDynamic} from '@angular/platform-browser-dynamic'; import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; @@ -112,11 +112,10 @@ class DummyConsole implements Console { class TestModule {} -function bootstrap( - cmpType: any, providers: Provider[] = [], platformProviders: StaticProvider[] = [], - imports: Type[] = []): Promise { +function bootstrap(cmpType: any, providers: Provider[] = [], platformProviders: StaticProvider[] = [ +]): Promise { @NgModule({ - imports: [BrowserModule, ...imports], + imports: [BrowserModule], declarations: [cmpType], bootstrap: [cmpType], providers: providers, @@ -184,40 +183,6 @@ export function main() { }); })); - it('should throw if no provider', inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { - const logger = new MockConsole(); - const errorHandler = new ErrorHandler(); - errorHandler._console = logger as any; - - class IDontExist {} - - @Component({selector: 'cmp', template: 'Cmp'}) - class CustomCmp { - constructor(iDontExist: IDontExist) {} - } - - @Component({ - selector: 'hello-app', - template: '', - }) - class RootCmp { - } - - @NgModule({declarations: [CustomCmp], exports: [CustomCmp]}) - class CustomModule { - } - - bootstrap(RootCmp, [{provide: ErrorHandler, useValue: errorHandler}], [], [ - CustomModule - ]).then(null, (e: Error) => { - expect(e.message).toContain(`StaticInjectorError(TestModule)[CustomCmp -> IDontExist]: - StaticInjectorError(Platform: core)[CustomCmp -> IDontExist]: - NullInjectorError: No provider for IDontExist!`); - async.done(); - return null; - }); - })); - if (getDOM().supportsDOMEvents()) { it('should forward the error to promise when bootstrap fails', inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { diff --git a/packages/upgrade/src/common/downgrade_component_adapter.ts b/packages/upgrade/src/common/downgrade_component_adapter.ts index 4e44bfcf9f..74509791f5 100644 --- a/packages/upgrade/src/common/downgrade_component_adapter.ts +++ b/packages/upgrade/src/common/downgrade_component_adapter.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {ApplicationRef, ChangeDetectorRef, ComponentFactory, ComponentRef, EventEmitter, Injector, OnChanges, SimpleChange, SimpleChanges, StaticProvider, Testability, TestabilityRegistry, Type} from '@angular/core'; +import {ApplicationRef, ChangeDetectorRef, ComponentFactory, ComponentRef, EventEmitter, Injector, OnChanges, SimpleChange, SimpleChanges, Testability, TestabilityRegistry, Type} from '@angular/core'; import * as angular from './angular1'; import {PropertyBinding} from './component_info'; @@ -54,9 +54,8 @@ export class DowngradeComponentAdapter { } createComponent(projectableNodes: Node[][]) { - const providers: StaticProvider[] = [{provide: $SCOPE, useValue: this.componentScope}]; - const childInjector = Injector.create( - {providers: providers, parent: this.parentInjector, name: 'DowngradeComponentAdapter'}); + const childInjector = + Injector.create([{provide: $SCOPE, useValue: this.componentScope}], this.parentInjector); this.componentRef = this.componentFactory.create(childInjector, projectableNodes, this.element[0]); diff --git a/tools/public_api_guard/core/core.d.ts b/tools/public_api_guard/core/core.d.ts index b4edf322f4..51fe21bb95 100644 --- a/tools/public_api_guard/core/core.d.ts +++ b/tools/public_api_guard/core/core.d.ts @@ -476,12 +476,7 @@ export declare abstract class Injector { /** @deprecated */ abstract get(token: any, notFoundValue?: any): any; static NULL: Injector; static THROW_IF_NOT_FOUND: Object; - /** @deprecated */ static create(providers: StaticProvider[], parent?: Injector): Injector; - static create(options: { - providers: StaticProvider[]; - parent?: Injector; - name?: string; - }): Injector; + static create(providers: StaticProvider[], parent?: Injector): Injector; } /** @stable */