Skip to content

Commit

Permalink
fix(core): better error message when directive extends a component (#…
Browse files Browse the repository at this point in the history
…45658)

We throw an error when a directive is trying to extend a component, but we don't actually say which class is responsible which can be difficult to track down. These changes add the two class names to the error message.

PR Close #45658
  • Loading branch information
crisbeto authored and dylhunn committed Apr 18, 2022
1 parent d68333e commit 9317f51
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
Expand Up @@ -14,6 +14,7 @@ import {ComponentDef, ContentQueriesFunction, DirectiveDef, DirectiveDefFeature,
import {TAttributes} from '../interfaces/node';
import {isComponentDef} from '../interfaces/type_checks';
import {mergeHostAttrs} from '../util/attrs_utils';
import {stringifyForError} from '../util/stringify_utils';

export function getSuperType(type: Type<any>): Type<any>&
{ɵcmp?: ComponentDef<any>, ɵdir?: DirectiveDef<any>} {
Expand Down Expand Up @@ -41,7 +42,9 @@ export function ɵɵInheritDefinitionFeature(definition: DirectiveDef<any>|Compo
} else {
if (superType.ɵcmp) {
const errorMessage = (typeof ngDevMode === 'undefined' || ngDevMode) ?
'Directives cannot inherit Components' :
`Directives cannot inherit Components. Directive ${
stringifyForError(definition.type)} is attempting to extend component ${
stringifyForError(superType)}` :
'';
throw new RuntimeError(RuntimeErrorCode.INVALID_INHERITANCE, errorMessage);
}
Expand Down
Expand Up @@ -40,7 +40,9 @@ describe('inheritance', () => {

expect(() => {
TestBed.createComponent(App);
}).toThrowError('NG0903: Directives cannot inherit Components');
})
.toThrowError(
'NG0903: Directives cannot inherit Components. Directive MyDirective is attempting to extend component MyComponent');
});

describe('multiple children', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/test/linker/inheritance_integration_spec.ts
Expand Up @@ -73,6 +73,7 @@ describe('Inheritance logic', () => {
const template = '<div directiveExtendsComponent>Some content</div>';
TestBed.overrideComponent(App, {set: {template}});
expect(() => TestBed.createComponent(App))
.toThrowError('NG0903: Directives cannot inherit Components');
.toThrowError(
'NG0903: Directives cannot inherit Components. Directive DirectiveExtendsComponent is attempting to extend component ComponentA');
});
});

0 comments on commit 9317f51

Please sign in to comment.