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

[lit-html] Add DEV_MODE error if duplicate attribute bindings are encountered #4523

Merged
merged 8 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 6 additions & 0 deletions .changeset/forty-schools-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'lit-html': patch
'lit': patch
---

Add a DEV_MODE error to catch duplicate attribute bindings that otherwise create silent errors.
20 changes: 20 additions & 0 deletions packages/lit-html/src/lit-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,26 @@ class Template {
}
nodeIndex++;
}

if (DEV_MODE) {
// If there was a duplicate attribute on a tag, then when the tag is
// parsed into an element the attribute gets de-duplicated. We can detect
// this mismatch if we haven't precisely consumed every attribute name
// when preparing the template.
AndrewJakubowicz marked this conversation as resolved.
Show resolved Hide resolved
if (attrNames.length !== attrNameIndex) {
throw new Error(
`Detected duplicate attribute bindings. This occurs if your template ` +
`has duplicate attributes on an element tag. For example ` +
`"<input ?disabled=\${true} ?disabled=\${false}>" contains a ` +
`duplicate "disabled" attribute. The error was detected in ` +
`the following template: \n` +
'`' +
strings.join('${...}') +
'`'
);
}
}

// We could set walker.currentNode to another node here to prevent a memory
// leak, but every time we prepare a template, we immediately render it
// and re-use the walker in new TemplateInstance._clone().
Expand Down
18 changes: 18 additions & 0 deletions packages/lit-html/src/test/lit-html_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2193,6 +2193,24 @@ suite('lit-html', () => {
);
});

skipTestIfCompiled('Duplicate attributes throw', () => {
assert.throws(() => {
render(
html`<input ?disabled=${true} ?disabled=${false} fooAttribute=${'potato'}>`,
container
);
}, `Detected duplicate attribute bindings. This occurs if your template has duplicate attributes on an element tag. For example "<input ?disabled=\${true} ?disabled=\${false}>" contains a duplicate "disabled" attribute. The error was detected in the following template: \n\`<input ?disabled=\${...} ?disabled=\${...} fooAttribute=\${...}>\``);
});

test('Matching attribute bindings across elements should not throw', () => {
assert.doesNotThrow(() => {
render(
html`<input ?disabled=${true}><input ?disabled=${false}>`,
container
);
});
});

test('Expressions inside nested templates throw in dev mode', () => {
// top level
assert.throws(() => {
Expand Down