fix: error on duplicate style and class directive (#13097)

Closes #13095
pull/13113/head
Paolo Ricciuti 3 weeks ago committed by GitHub
parent 35bd12b156
commit 0d51474e29
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: error on duplicate style and class directive

@ -189,12 +189,24 @@ export default function element(parser) {
let attribute;
while ((attribute = read(parser))) {
if (attribute.type === 'Attribute' || attribute.type === 'BindDirective') {
if (unique_names.includes(attribute.name)) {
// animate and transition can only be specified once per element so no need
// to check here, use can be used multiple times, same for the on directive
// finally let already has error handling in case of duplicate variable names
if (
attribute.type === 'Attribute' ||
attribute.type === 'BindDirective' ||
attribute.type === 'StyleDirective' ||
attribute.type === 'ClassDirective'
) {
// `bind:attribute` and `attribute` are just the same but `class:attribute`,
// `style:attribute` and `attribute` are different and should be allowed together
// so we concatenate the type while normalizing the type for BindDirective
const type = attribute.type === 'BindDirective' ? 'Attribute' : attribute.type;
if (unique_names.includes(type + attribute.name)) {
e.attribute_duplicate(attribute);
// <svelte:element bind:this this=..> is allowed
} else if (attribute.name !== 'this') {
unique_names.push(attribute.name);
unique_names.push(type + attribute.name);
}
}

@ -0,0 +1,9 @@
import { test } from '../../test';
export default test({
error: {
code: 'attribute_duplicate',
message: 'Attributes need to be unique',
position: [23, 40]
}
});

@ -0,0 +1,9 @@
import { test } from '../../test';
export default test({
error: {
code: 'attribute_duplicate',
message: 'Attributes need to be unique',
position: [23, 42]
}
});

@ -0,0 +1 @@
<div color="red" style:color="red" class:color={true} ></div>
Loading…
Cancel
Save