Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): better error message when directive extends a component #45658

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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');
});
});