diff --git a/packages/core/src/di/interface/defs.ts b/packages/core/src/di/interface/defs.ts index 01e5261da4..f30abbdd09 100644 --- a/packages/core/src/di/interface/defs.ts +++ b/packages/core/src/di/interface/defs.ts @@ -223,17 +223,34 @@ export function getInheritedInjectableDef(type: any): ɵɵInjectableDef|nu (type[NG_PROV_DEF_FALLBACK] && type[NG_PROV_DEF_FALLBACK]())); if (def) { + const typeName = getTypeName(type); // TODO(FW-1307): Re-add ngDevMode when closure can handle it // ngDevMode && console.warn( - `DEPRECATED: DI is instantiating a token "${type.name}" that inherits its @Injectable decorator but does not provide one itself.\n` + - `This will become an error in v10. Please add @Injectable() to the "${type.name}" class.`); + `DEPRECATED: DI is instantiating a token "${typeName}" that inherits its @Injectable decorator but does not provide one itself.\n` + + `This will become an error in v10. Please add @Injectable() to the "${typeName}" class.`); return def; } else { return null; } } +/** Gets the name of a type, accounting for some cross-browser differences. */ +function getTypeName(type: any): string { + // `Function.prototype.name` behaves differently between IE and other browsers. In most browsers + // it'll always return the name of the function itself, no matter how many other functions it + // inherits from. On IE the function doesn't have its own `name` property, but it takes it from + // the lowest level in the prototype chain. E.g. if we have `class Foo extends Parent` most + // browsers will evaluate `Foo.name` to `Foo` while IE will return `Parent`. We work around + // the issue by converting the function to a string and parsing its name out that way via a regex. + if (type.hasOwnProperty('name')) { + return type.name; + } + + const match = ('' + type).match(/^function\s*([^\s(]+)/); + return match === null ? '' : match[1]; +} + /** * Read the injector def type in a way which is immune to accidentally reading inherited value. * diff --git a/packages/core/test/bundling/injection/bundle.golden_symbols.json b/packages/core/test/bundling/injection/bundle.golden_symbols.json index b8cef691a6..885d5971d5 100644 --- a/packages/core/test/bundling/injection/bundle.golden_symbols.json +++ b/packages/core/test/bundling/injection/bundle.golden_symbols.json @@ -137,6 +137,9 @@ { "name": "getOwnDefinition" }, + { + "name": "getTypeName" + }, { "name": "getUndecoratedInjectableFactory" },