Skip to content

Commit d7f5f59

Browse files
committed
warn on self-closing non-void special elements
1 parent 669d2d7 commit d7f5f59

File tree

4 files changed

+79
-11
lines changed

4 files changed

+79
-11
lines changed

packages/svelte/src/compiler/phases/2-analyze/validation.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,11 +676,21 @@ const validation = {
676676
error(node, 'invalid-style-directive-modifier');
677677
}
678678
},
679-
SvelteHead(node) {
679+
SvelteHead(node, context) {
680680
const attribute = node.attributes[0];
681681
if (attribute) {
682682
error(attribute, 'illegal-svelte-head-attribute');
683683
}
684+
685+
if (context.state.analysis.source[node.end - 2] === '/') {
686+
warn(
687+
context.state.analysis.warnings,
688+
node,
689+
context.path,
690+
'invalid-self-closing-tag',
691+
node.name
692+
);
693+
}
684694
},
685695
SvelteElement(node, context) {
686696
validate_element(node, context);
@@ -704,6 +714,16 @@ const validation = {
704714
error(attribute, 'invalid-svelte-fragment-attribute');
705715
}
706716
}
717+
718+
if (context.state.analysis.source[node.end - 2] === '/') {
719+
warn(
720+
context.state.analysis.warnings,
721+
node,
722+
context.path,
723+
'invalid-self-closing-tag',
724+
node.name
725+
);
726+
}
707727
},
708728
SlotElement(node) {
709729
for (const attribute of node.attributes) {

packages/svelte/src/compiler/warnings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ const options = {
253253
const misc = {
254254
/** @param {string} name */
255255
'invalid-self-closing-tag': (name) =>
256-
`Self-closing HTML tags for non-void elements are ambiguous — use <${name} ...></${name}> rather than <${name} ... />`
256+
`Self-closing tags for non-void elements are ambiguous — use <${name} ...></${name}> rather than <${name} ... />`
257257
};
258258

259259
/** @satisfies {Warnings} */
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1-
<!-- valid -->
2-
<link />
1+
<!-- valid - foreign namespace -->
32
<svg><g /></svg>
43

5-
<!-- invalid -->
4+
<!-- not reported - void tags -->
5+
<link />
6+
<br/>
7+
<svelte:options />
8+
<svelte:window on:event={() => null} />
9+
<svelte:document on:event={() => null} />
10+
<svelte:body on:event={() => null} />
11+
12+
<!-- not reported - deprecated -->
13+
<slot />
14+
15+
<!-- not reported - components -->
16+
{#if true}
17+
<svelte:self />
18+
{/if}
19+
<Foo />
20+
<svelte:component this={"foo"} />
21+
22+
<!-- not reported - `this` can be bound to both void and non-void tags -->
23+
<svelte:element this="dif" />
24+
25+
<!-- invalid and reported -->
626
<div />
727
<my-thing />
28+
<svelte:head />
29+
<Foo>
30+
<svelte:fragment slot="footer" />
31+
</Foo>
Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,50 @@
11
[
22
{
33
"code": "invalid-self-closing-tag",
4-
"message": "Self-closing HTML tags for non-void elements are ambiguous — use <div ...></div> rather than <div ... />",
4+
"message": "Self-closing tags for non-void elements are ambiguous — use <div ...></div> rather than <div ... />",
55
"start": {
6-
"line": 6,
6+
"line": 26,
77
"column": 0
88
},
99
"end": {
10-
"line": 6,
10+
"line": 26,
1111
"column": 7
1212
}
1313
},
1414
{
1515
"code": "invalid-self-closing-tag",
16-
"message": "Self-closing HTML tags for non-void elements are ambiguous — use <my-thing ...></my-thing> rather than <my-thing ... />",
16+
"message": "Self-closing tags for non-void elements are ambiguous — use <my-thing ...></my-thing> rather than <my-thing ... />",
1717
"start": {
18-
"line": 7,
18+
"line": 27,
1919
"column": 0
2020
},
2121
"end": {
22-
"line": 7,
22+
"line": 27,
2323
"column": 12
2424
}
25+
},
26+
{
27+
"code": "invalid-self-closing-tag",
28+
"message": "Self-closing tags for non-void elements are ambiguous — use <svelte:head ...></svelte:head> rather than <svelte:head ... />",
29+
"start": {
30+
"line": 28,
31+
"column": 0
32+
},
33+
"end": {
34+
"line": 28,
35+
"column": 15
36+
}
37+
},
38+
{
39+
"code": "invalid-self-closing-tag",
40+
"message": "Self-closing tags for non-void elements are ambiguous — use <svelte:fragment ...></svelte:fragment> rather than <svelte:fragment ... />",
41+
"start": {
42+
"line": 30,
43+
"column": 1
44+
},
45+
"end": {
46+
"line": 30,
47+
"column": 34
48+
}
2549
}
2650
]

0 commit comments

Comments
 (0)