fix: adjust `loc` property of `Program` nodes created from `<script>` elements (#17428)

* fix: adjust program location to prevent corrupted sourcemaps

* update changeset

* go the other way
pull/17431/head
Rich Harris 8 months ago committed by GitHub
parent 41e8643814
commit 044dce9da5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: adjust `loc` property of `Program` nodes created from `<script>` elements

@ -6,6 +6,7 @@ import { regex_not_newline_characters } from '../../patterns.js';
import * as e from '../../../errors.js'; import * as e from '../../../errors.js';
import * as w from '../../../warnings.js'; import * as w from '../../../warnings.js';
import { is_text_attribute } from '../../../utils/ast.js'; import { is_text_attribute } from '../../../utils/ast.js';
import { locator } from '../../../state.js';
const regex_closing_script_tag = /<\/script\s*>/; const regex_closing_script_tag = /<\/script\s*>/;
const regex_starts_with_closing_script_tag = /^<\/script\s*>/; const regex_starts_with_closing_script_tag = /^<\/script\s*>/;
@ -39,9 +40,15 @@ export function read_script(parser, start, attributes) {
parser.acorn_error(err); parser.acorn_error(err);
} }
// TODO is this necessary?
ast.start = script_start; ast.start = script_start;
if (ast.loc) {
// Acorn always uses `0` as the start of a `Program`, but for sourcemap purposes
// we need it to be the start of the `<script>` contents
({ line: ast.loc.start.line, column: ast.loc.start.column } = locator(start));
({ line: ast.loc.end.line, column: ast.loc.end.column } = locator(parser.index));
}
/** @type {'default' | 'module'} */ /** @type {'default' | 'module'} */
let context = 'default'; let context = 'default';

@ -63,7 +63,7 @@
}, },
"end": { "end": {
"line": 3, "line": 3,
"column": 0 "column": 9
} }
}, },
"body": [ "body": [

@ -73,7 +73,7 @@
}, },
"end": { "end": {
"line": 3, "line": 3,
"column": 0 "column": 9
} }
}, },
"body": [ "body": [

@ -21,7 +21,7 @@
}, },
"end": { "end": {
"line": 9, "line": 9,
"column": 0 "column": 9
} }
}, },
"body": [ "body": [

@ -190,7 +190,7 @@
}, },
"end": { "end": {
"line": 3, "line": 3,
"column": 0 "column": 9
} }
}, },
"body": [ "body": [

@ -258,7 +258,7 @@
}, },
"end": { "end": {
"line": 2, "line": 2,
"column": 0 "column": 9
} }
}, },
"body": [], "body": [],

@ -298,7 +298,7 @@
}, },
"end": { "end": {
"line": 31, "line": 31,
"column": 0 "column": 9
} }
}, },
"body": [ "body": [

@ -73,7 +73,7 @@
}, },
"end": { "end": {
"line": 3, "line": 3,
"column": 0 "column": 9
} }
}, },
"body": [ "body": [

@ -73,7 +73,7 @@
}, },
"end": { "end": {
"line": 3, "line": 3,
"column": 0 "column": 9
} }
}, },
"body": [ "body": [

@ -37,7 +37,7 @@
}, },
"end": { "end": {
"line": 3, "line": 3,
"column": 0 "column": 9
} }
}, },
"body": [], "body": [],

@ -24,12 +24,12 @@
"end": 77, "end": 77,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 5,
"column": 0 "column": 0
}, },
"end": { "end": {
"line": 7, "line": 7,
"column": 0 "column": 9
} }
}, },
"body": [ "body": [
@ -84,7 +84,7 @@
}, },
"end": { "end": {
"line": 3, "line": 3,
"column": 0 "column": 9
} }
}, },
"body": [ "body": [

@ -73,7 +73,7 @@
}, },
"end": { "end": {
"line": 3, "line": 3,
"column": 0 "column": 9
} }
}, },
"body": [ "body": [

@ -72,8 +72,8 @@
"column": 0 "column": 0
}, },
"end": { "end": {
"line": 3, "line": 8,
"column": 0 "column": 1
} }
}, },
"body": [ "body": [

@ -272,7 +272,7 @@
}, },
"end": { "end": {
"line": 3, "line": 3,
"column": 0 "column": 9
} }
}, },
"body": [ "body": [

@ -34,12 +34,12 @@
"end": 67, "end": 67,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 2,
"column": 0 "column": 0
}, },
"end": { "end": {
"line": 4, "line": 4,
"column": 0 "column": 9
} }
}, },
"body": [ "body": [

@ -296,7 +296,7 @@
}, },
"end": { "end": {
"line": 2, "line": 2,
"column": 0 "column": 9
} }
}, },
"body": [], "body": [],

@ -227,7 +227,7 @@
}, },
"end": { "end": {
"line": 3, "line": 3,
"column": 0 "column": 9
} }
}, },
"body": [ "body": [

@ -111,12 +111,12 @@
"end": 93, "end": 93,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 3,
"column": 0 "column": 0
}, },
"end": { "end": {
"line": 4, "line": 4,
"column": 0 "column": 9
} }
}, },
"body": [], "body": [],
@ -182,12 +182,12 @@
"end": 160, "end": 160,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 6,
"column": 0 "column": 0
}, },
"end": { "end": {
"line": 7, "line": 7,
"column": 0 "column": 9
} }
}, },
"body": [], "body": [],

@ -93,7 +93,7 @@
}, },
"end": { "end": {
"line": 3, "line": 3,
"column": 0 "column": 9
} }
}, },
"body": [], "body": [],

@ -230,7 +230,7 @@
}, },
"end": { "end": {
"line": 1, "line": 1,
"column": 18 "column": 27
} }
}, },
"body": [], "body": [],

@ -396,7 +396,7 @@
}, },
"end": { "end": {
"line": 3, "line": 3,
"column": 0 "column": 9
} }
}, },
"body": [ "body": [

Loading…
Cancel
Save