Skip to content

Commit

Permalink
fix(compiler): throw error for duplicate template references
Browse files Browse the repository at this point in the history
Adds an error if a reference is used more than once on the same element (e.g. `<div #a #a>`).
We used to have this error in ViewEngine, but it wasn't ported over to Ivy.

Fixes #40536.
  • Loading branch information
crisbeto committed Jan 23, 2021
1 parent fc64fa8 commit 0e73214
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/compiler/src/render3/r3_template_transform.ts
Expand Up @@ -458,6 +458,8 @@ class HtmlAstToIvyAst implements html.Visitor {
this.reportError(`"-" is not allowed in reference names`, sourceSpan);
} else if (identifier.length === 0) {
this.reportError(`Reference does not have a name`, sourceSpan);
} else if (references.some(reference => reference.name === identifier)) {
this.reportError(`Reference "#${identifier}" is defined several times`, sourceSpan);
}

references.push(new t.Reference(identifier, value, sourceSpan, keySpan, valueSpan));
Expand Down
10 changes: 10 additions & 0 deletions packages/compiler/test/render3/r3_template_transform_spec.ts
Expand Up @@ -273,6 +273,11 @@ describe('R3 template transform', () => {
]);
});

it('should report an error if a reference is used multiple times on the same template', () => {
expect(() => parse('<ng-template #a #a></ng-template>'))
.toThrowError(/Reference "#a" is defined several times/);
});

it('should parse variables via let-...', () => {
expectFromHtml('<ng-template let-a="b"></ng-template>').toEqual([
['Template'],
Expand Down Expand Up @@ -463,6 +468,11 @@ describe('R3 template transform', () => {
it('should report missing reference names', () => {
expect(() => parse('<div #></div>')).toThrowError(/Reference does not have a name/);
});

it('should report an error if a reference is used multiple times on the same element', () => {
expect(() => parse('<div #a #a></div>'))
.toThrowError(/Reference "#a" is defined several times/);
});
});

describe('literal attribute', () => {
Expand Down

0 comments on commit 0e73214

Please sign in to comment.