Skip to content

Commit 5076613

Browse files
authored
disallow simultaneous foo / {foo} / bind:foo (#4343)
* disallow matching attributes/shorthands/bindings (#4325) * add tests * update changelog
1 parent 9e7df1e commit 5076613

File tree

6 files changed

+46
-11
lines changed

6 files changed

+46
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Svelte changelog
22

3+
## Unreleased
4+
5+
* Disallow attribute/prop names from matching two-way-bound names or `{shorthand}` attribute/prop names ([#4325](https://github.com/sveltejs/svelte/issues/4325))
6+
37
## 3.18.1
48

59
* Fix code generation error with adjacent inline and block comments ([#4312](https://github.com/sveltejs/svelte/issues/4312))

src/compiler/parse/state/tag.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,16 @@ function read_tag_name(parser: Parser) {
288288
function read_attribute(parser: Parser, unique_names: Set<string>) {
289289
const start = parser.index;
290290

291+
function check_unique(name: string) {
292+
if (unique_names.has(name)) {
293+
parser.error({
294+
code: `duplicate-attribute`,
295+
message: 'Attributes need to be unique'
296+
}, start);
297+
}
298+
unique_names.add(name);
299+
}
300+
291301
if (parser.eat('{')) {
292302
parser.allow_whitespace();
293303

@@ -310,6 +320,8 @@ function read_attribute(parser: Parser, unique_names: Set<string>) {
310320
parser.allow_whitespace();
311321
parser.eat('}', true);
312322

323+
check_unique(name);
324+
313325
return {
314326
start,
315327
end: parser.index,
@@ -341,17 +353,6 @@ function read_attribute(parser: Parser, unique_names: Set<string>) {
341353
const colon_index = name.indexOf(':');
342354
const type = colon_index !== -1 && get_directive_type(name.slice(0, colon_index));
343355

344-
if (unique_names.has(name)) {
345-
parser.error({
346-
code: `duplicate-attribute`,
347-
message: 'Attributes need to be unique'
348-
}, start);
349-
}
350-
351-
if (type !== "EventHandler") {
352-
unique_names.add(name);
353-
}
354-
355356
let value: any[] | true = true;
356357
if (parser.eat('=')) {
357358
parser.allow_whitespace();
@@ -367,6 +368,12 @@ function read_attribute(parser: Parser, unique_names: Set<string>) {
367368
if (type) {
368369
const [directive_name, ...modifiers] = name.slice(colon_index + 1).split('|');
369370

371+
if (type === 'Binding' && directive_name !== 'this') {
372+
check_unique(directive_name);
373+
} else if (type !== 'EventHandler') {
374+
check_unique(name);
375+
}
376+
370377
if (type === 'Ref') {
371378
parser.error({
372379
code: `invalid-ref-directive`,
@@ -410,6 +417,8 @@ function read_attribute(parser: Parser, unique_names: Set<string>) {
410417
return directive;
411418
}
412419

420+
check_unique(name);
421+
413422
return {
414423
start,
415424
end,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"code": "duplicate-attribute",
3+
"message": "Attributes need to be unique",
4+
"start": {
5+
"line": 1,
6+
"column": 17,
7+
"character": 17
8+
},
9+
"pos": 17
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<Widget foo={42} bind:foo/>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"code": "duplicate-attribute",
3+
"message": "Attributes need to be unique",
4+
"start": {
5+
"line": 1,
6+
"column": 17,
7+
"character": 17
8+
},
9+
"pos": 17
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div title='foo' {title}></div>

0 commit comments

Comments
 (0)