Skip to content

Commit df361a2

Browse files
dummdidummbenmccannRich Harris
authored
chore: warn about : in attributes and props (#8633)
closes #6823 --------- Co-authored-by: Ben McCann <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent 5a4b48b commit df361a2

File tree

10 files changed

+61
-41
lines changed

10 files changed

+61
-41
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* Treat slots as if they don't exist when using CSS adjacent and general sibling combinators ([#8284](https://github.com/sveltejs/svelte/issues/8284))
2828
* Fix transitions so that they don't require a `style-src 'unsafe-inline'` Content Security Policy (CSP) ([#6662](https://github.com/sveltejs/svelte/issues/6662)).
2929
* Explicitly disallow `var` declarations extending the reactive statement scope ([#6800](https://github.com/sveltejs/svelte/pull/6800))
30+
* Warn about `:` in attributes and props to prevent ambiguity with Svelte directives ([#6823](https://github.com/sveltejs/svelte/issues/6823))
3031

3132
## 3.59.1
3233

src/compiler/compile/compiler_warnings.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,5 +301,10 @@ export default {
301301
code: 'avoid-mouse-events-on-document',
302302
message:
303303
'Mouse enter/leave events on the document are not supported in all browsers and should be avoided'
304+
},
305+
illegal_attribute_character: {
306+
code: 'illegal-attribute-character',
307+
message:
308+
"Attributes should not contain ':' characters to prevent ambiguity with Svelte directives"
304309
}
305310
};

src/compiler/compile/nodes/Attribute.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import add_to_set from '../utils/add_to_set.js';
33
import Node from './shared/Node.js';
44
import Expression from './shared/Expression.js';
55
import { x } from 'code-red';
6+
import compiler_warnings from '../compiler_warnings.js';
67

78
/** @extends Node<'Attribute' | 'Spread', import('./Element.js').default> */
89
export default class Attribute extends Node {
@@ -39,6 +40,7 @@ export default class Attribute extends Node {
3940
constructor(component, parent, scope, info) {
4041
super(component, parent, scope, info);
4142
this.scope = scope;
43+
4244
if (info.type === 'Spread') {
4345
this.name = null;
4446
this.is_spread = true;
@@ -64,10 +66,22 @@ export default class Attribute extends Node {
6466
}
6567
);
6668
}
69+
6770
if (this.dependencies.size > 0) {
6871
parent.cannot_use_innerhtml();
6972
parent.not_static_content();
7073
}
74+
75+
// TODO Svelte 5: Think about moving this into the parser and make it an error
76+
if (
77+
this.name &&
78+
this.name.includes(':') &&
79+
!this.name.startsWith('xmlns:') &&
80+
!this.name.startsWith('xlink:') &&
81+
!this.name.startsWith('xml:')
82+
) {
83+
component.warn(this, compiler_warnings.illegal_attribute_character);
84+
}
7185
}
7286
get_dependencies() {
7387
if (this.is_spread) return this.expression.dynamic_dependencies();

src/compiler/parse/state/tag.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,6 @@ function get_directive_type(name) {
433433
if (name === 'style') return 'StyleDirective';
434434
if (name === 'on') return 'EventHandler';
435435
if (name === 'let') return 'Let';
436-
if (name === 'ref') return 'Ref';
437436
if (name === 'in' || name === 'out' || name === 'transition') return 'Transition';
438437
}
439438

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
import C from './irrelevant';
3+
</script>
4+
5+
<button on:click />
6+
<button xml:click />
7+
<button xmlns:click />
8+
<button xlink:click />
9+
<C on:click />
10+
<C xml:click />
11+
<C xmlns:click />
12+
<C xlink:click />
13+
14+
<button foo:bar />
15+
<C foo:bar />
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"code": "illegal-attribute-character",
4+
"end": {
5+
"column": 15,
6+
"line": 14
7+
},
8+
"message": "Attributes should not contain ':' characters to prevent ambiguity with Svelte directives",
9+
"start": {
10+
"column": 8,
11+
"line": 14
12+
}
13+
},
14+
{
15+
"code": "illegal-attribute-character",
16+
"end": {
17+
"column": 10,
18+
"line": 15
19+
},
20+
"message": "Attributes should not contain ':' characters to prevent ambiguity with Svelte directives",
21+
"start": {
22+
"column": 3,
23+
"line": 15
24+
}
25+
}
26+
]

test/validator/samples/ref-not-supported-in-css/errors.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/validator/samples/ref-not-supported-in-css/input.svelte

Lines changed: 0 additions & 11 deletions
This file was deleted.

test/validator/samples/ref-not-supported/errors.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/validator/samples/ref-not-supported/input.svelte

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)