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): improve multiple components match error #45645

Closed
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
9 changes: 7 additions & 2 deletions packages/core/src/render3/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@
*/

import {RuntimeError, RuntimeErrorCode} from '../errors';
import {Type} from '../interface/type';

import {TNode} from './interfaces/node';
import {LView, TVIEW} from './interfaces/view';
import {INTERPOLATION_DELIMITER} from './util/misc_utils';
import {stringifyForError} from './util/stringify_utils';



/** Called when there are multiple component selectors that match a given node */
export function throwMultipleComponentError(tNode: TNode): never {
export function throwMultipleComponentError(
tNode: TNode, first: Type<unknown>, second: Type<unknown>): never {
throw new RuntimeError(
RuntimeErrorCode.MULTIPLE_COMPONENTS_MATCH,
`Multiple components match node with tagname ${tNode.value}`);
`Multiple components match node with tagname ${tNode.value}: ` +
`${stringifyForError(first)} and ` +
`${stringifyForError(second)}`);
}

/** Throws an ExpressionChangedAfterChecked error if checkNoChanges mode is on. */
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/render3/instructions/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1404,7 +1404,11 @@ function findDirectiveDefMatches(
`"${tNode.value}" tags cannot be used as component hosts. ` +
`Please use a different tag to activate the ${stringify(def.type)} component.`);

if (tNode.flags & TNodeFlags.isComponentHost) throwMultipleComponentError(tNode);
if (tNode.flags & TNodeFlags.isComponentHost) {
// If another component has been matched previously, it's the first element in the
// `matches` array, see how we store components/directives in `matches` below.
throwMultipleComponentError(tNode, matches[0].type, def.type);
}
}
markAsComponentHost(tView, tNode);
// The component is always stored first with directives after.
Expand Down
27 changes: 27 additions & 0 deletions packages/core/test/acceptance/component_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,33 @@ describe('component', () => {
.toThrowError(
/"ng-template" tags cannot be used as component hosts. Please use a different tag to activate the Comp component/);
});

it('should throw when multiple components match the same element', () => {
@Component({
selector: 'comp',
template: '...',
})
class CompA {
}

@Component({
selector: 'comp',
template: '...',
})
class CompB {
}

@Component({
template: '<comp></comp>',
})
class App {
}

TestBed.configureTestingModule({declarations: [App, CompA, CompB]});
expect(() => TestBed.createComponent(App))
.toThrowError(
/NG0300: Multiple components match node with tagname comp: CompA and CompB/);
});
});

it('should use a new ngcontent attribute for child elements created w/ Renderer2', () => {
Expand Down