Skip to content

Commit 3cabb52

Browse files
fix: set Root start/end to null when fragment contains only whitespace (#17125)
* fix: set Root start/end to null when fragment contains only whitespace * format * always set root.start to 0 and root.end to template.length * Update .changeset/fruity-knives-ring.md --------- Co-authored-by: Rich Harris <rich.harris@vercel.com> Co-authored-by: Rich Harris <hello@rich-harris.dev>
1 parent 7ec9e4b commit 3cabb52

File tree

14 files changed

+136
-26
lines changed

14 files changed

+136
-26
lines changed

.changeset/fruity-knives-ring.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: set AST `root.start` to `0` and `root.end` to `template.length`

packages/svelte/src/compiler/phases/1-parse/index.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,8 @@ export class Parser {
117117
e.unexpected_eof(this.index);
118118
}
119119

120-
if (this.root.fragment.nodes.length) {
121-
let start = /** @type {number} */ (this.root.fragment.nodes[0].start);
122-
while (regex_whitespace.test(template[start])) start += 1;
123-
124-
let end = /** @type {number} */ (
125-
this.root.fragment.nodes[this.root.fragment.nodes.length - 1].end
126-
);
127-
while (regex_whitespace.test(template[end - 1])) end -= 1;
128-
129-
this.root.start = start;
130-
this.root.end = end;
131-
} else {
132-
// @ts-ignore
133-
this.root.start = this.root.end = null;
134-
}
120+
this.root.start = 0;
121+
this.root.end = template.length;
135122

136123
const options_index = this.root.fragment.nodes.findIndex(
137124
/** @param {any} thing */

packages/svelte/tests/parser-modern/samples/comment-before-function-binding/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"css": null,
33
"js": [],
4-
"start": 37,
4+
"start": 0,
55
"end": 117,
66
"type": "Root",
77
"fragment": {

packages/svelte/tests/parser-modern/samples/comment-before-script/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"css": null,
33
"js": [],
44
"start": 0,
5-
"end": 27,
5+
"end": 76,
66
"type": "Root",
77
"fragment": {
88
"type": "Fragment",

packages/svelte/tests/parser-modern/samples/css-nth-syntax/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@
10791079
}
10801080
},
10811081
"js": [],
1082-
"start": 808,
1082+
"start": 0,
10831083
"end": 820,
10841084
"type": "Root",
10851085
"fragment": {

packages/svelte/tests/parser-modern/samples/css-pseudo-classes/output.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,8 @@
398398
}
399399
},
400400
"js": [],
401-
"start": null,
402-
"end": null,
401+
"start": 0,
402+
"end": 386,
403403
"type": "Root",
404404
"fragment": {
405405
"type": "Fragment",

packages/svelte/tests/parser-modern/samples/generic-snippets/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"css": null,
33
"js": [],
4-
"start": 30,
4+
"start": 0,
55
"end": 192,
66
"type": "Root",
77
"fragment": {

packages/svelte/tests/parser-modern/samples/loose-valid-each-as/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"css": null,
33
"js": [],
4-
"start": 45,
4+
"start": 0,
55
"end": 119,
66
"type": "Root",
77
"fragment": {

packages/svelte/tests/parser-modern/samples/options/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"css": null,
33
"js": [],
44
"start": 0,
5-
"end": 102,
5+
"end": 169,
66
"type": "Root",
77
"fragment": {
88
"type": "Fragment",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
// script and style but no markup
3+
</script>
4+
<style>
5+
div { color: red; }
6+
</style>

0 commit comments

Comments
 (0)