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>
pull/17331/head
Ryan Atkinson 2 days ago committed by GitHub
parent 7ec9e4b3b1
commit 3cabb52534
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: set AST `root.start` to `0` and `root.end` to `template.length`

@ -117,21 +117,8 @@ export class Parser {
e.unexpected_eof(this.index);
}
if (this.root.fragment.nodes.length) {
let start = /** @type {number} */ (this.root.fragment.nodes[0].start);
while (regex_whitespace.test(template[start])) start += 1;
let end = /** @type {number} */ (
this.root.fragment.nodes[this.root.fragment.nodes.length - 1].end
);
while (regex_whitespace.test(template[end - 1])) end -= 1;
this.root.start = start;
this.root.end = end;
} else {
// @ts-ignore
this.root.start = this.root.end = null;
}
this.root.start = 0;
this.root.end = template.length;
const options_index = this.root.fragment.nodes.findIndex(
/** @param {any} thing */

@ -1,7 +1,7 @@
{
"css": null,
"js": [],
"start": 37,
"start": 0,
"end": 117,
"type": "Root",
"fragment": {

@ -2,7 +2,7 @@
"css": null,
"js": [],
"start": 0,
"end": 27,
"end": 76,
"type": "Root",
"fragment": {
"type": "Fragment",

@ -1079,7 +1079,7 @@
}
},
"js": [],
"start": 808,
"start": 0,
"end": 820,
"type": "Root",
"fragment": {

@ -398,8 +398,8 @@
}
},
"js": [],
"start": null,
"end": null,
"start": 0,
"end": 386,
"type": "Root",
"fragment": {
"type": "Fragment",

@ -1,7 +1,7 @@
{
"css": null,
"js": [],
"start": 30,
"start": 0,
"end": 192,
"type": "Root",
"fragment": {

@ -1,7 +1,7 @@
{
"css": null,
"js": [],
"start": 45,
"start": 0,
"end": 119,
"type": "Root",
"fragment": {

@ -2,7 +2,7 @@
"css": null,
"js": [],
"start": 0,
"end": 102,
"end": 169,
"type": "Root",
"fragment": {
"type": "Fragment",

@ -0,0 +1,6 @@
<script>
// script and style but no markup
</script>
<style>
div { color: red; }
</style>

@ -0,0 +1,112 @@
{
"css": {
"type": "StyleSheet",
"start": 54,
"end": 91,
"attributes": [],
"children": [
{
"type": "Rule",
"prelude": {
"type": "SelectorList",
"start": 63,
"end": 66,
"children": [
{
"type": "ComplexSelector",
"start": 63,
"end": 66,
"children": [
{
"type": "RelativeSelector",
"combinator": null,
"selectors": [
{
"type": "TypeSelector",
"name": "div",
"start": 63,
"end": 66
}
],
"start": 63,
"end": 66
}
]
}
]
},
"block": {
"type": "Block",
"start": 67,
"end": 82,
"children": [
{
"type": "Declaration",
"start": 69,
"end": 79,
"property": "color",
"value": "red"
}
]
},
"start": 63,
"end": 82
}
],
"content": {
"start": 61,
"end": 83,
"styles": "\n\tdiv { color: red; }\n",
"comment": null
}
},
"js": [],
"start": 0,
"end": 91,
"type": "Root",
"fragment": {
"type": "Fragment",
"nodes": [
{
"type": "Text",
"start": 53,
"end": 54,
"raw": "\n",
"data": "\n"
}
]
},
"options": null,
"instance": {
"type": "Script",
"start": 0,
"end": 53,
"context": "default",
"content": {
"type": "Program",
"start": 8,
"end": 44,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 0
}
},
"body": [],
"sourceType": "module",
"trailingComments": [
{
"type": "Line",
"value": " script and style but no markup",
"start": 10,
"end": 43
}
]
},
"attributes": []
}
}

@ -77,7 +77,7 @@
},
"js": [],
"start": 0,
"end": 35,
"end": 205,
"type": "Root",
"fragment": {
"type": "Fragment",

@ -1,7 +1,7 @@
{
"css": null,
"js": [],
"start": 29,
"start": 0,
"end": 101,
"type": "Root",
"fragment": {

@ -1,7 +1,7 @@
{
"css": null,
"js": [],
"start": 54,
"start": 0,
"end": 173,
"type": "Root",
"fragment": {

Loading…
Cancel
Save