Skip to content

Commit 617e179

Browse files
authored
fix: preserve node locations for better sourcemaps (#17269)
* preserve node locations * preserve component IDs * update tests * use IDs for elements * more * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * tweak * tidy up * changeset * oops
1 parent 6d696be commit 617e179

File tree

135 files changed

+2298
-352
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+2298
-352
lines changed

.changeset/gentle-views-matter.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: preserve node locations for better sourcemaps

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

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/** @import { AST } from '#compiler' */
2+
/** @import { Location } from 'locate-character' */
3+
/** @import * as ESTree from 'estree' */
24
// @ts-expect-error acorn type definitions are borked in the release we use
35
import { isIdentifierStart, isIdentifierChar } from 'acorn';
46
import fragment from './state/fragment.js';
@@ -218,31 +220,45 @@ export class Parser {
218220
return result;
219221
}
220222

221-
/** @param {any} allow_reserved */
222-
read_identifier(allow_reserved = false) {
223+
/**
224+
* @returns {ESTree.Identifier & { start: number, end: number, loc: { start: Location, end: Location } }}
225+
*/
226+
read_identifier() {
223227
const start = this.index;
228+
let end = start;
229+
let name = '';
224230

225-
let i = this.index;
231+
const code = /** @type {number} */ (this.template.codePointAt(this.index));
226232

227-
const code = /** @type {number} */ (this.template.codePointAt(i));
228-
if (!isIdentifierStart(code, true)) return null;
233+
if (isIdentifierStart(code, true)) {
234+
let i = this.index;
235+
end += code <= 0xffff ? 1 : 2;
229236

230-
i += code <= 0xffff ? 1 : 2;
237+
while (end < this.template.length) {
238+
const code = /** @type {number} */ (this.template.codePointAt(end));
231239

232-
while (i < this.template.length) {
233-
const code = /** @type {number} */ (this.template.codePointAt(i));
234-
235-
if (!isIdentifierChar(code, true)) break;
236-
i += code <= 0xffff ? 1 : 2;
237-
}
240+
if (!isIdentifierChar(code, true)) break;
241+
end += code <= 0xffff ? 1 : 2;
242+
}
238243

239-
const identifier = this.template.slice(this.index, (this.index = i));
244+
name = this.template.slice(start, end);
245+
this.index = end;
240246

241-
if (!allow_reserved && is_reserved(identifier)) {
242-
e.unexpected_reserved_word(start, identifier);
247+
if (is_reserved(name)) {
248+
e.unexpected_reserved_word(start, name);
249+
}
243250
}
244251

245-
return identifier;
252+
return {
253+
type: 'Identifier',
254+
name,
255+
start,
256+
end,
257+
loc: {
258+
start: state.locator(start),
259+
end: state.locator(end)
260+
}
261+
};
246262
}
247263

248264
/** @param {RegExp} pattern */

packages/svelte/src/compiler/phases/1-parse/read/context.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
/** @import { Location } from 'locate-character' */
21
/** @import { Pattern } from 'estree' */
32
/** @import { Parser } from '../index.js' */
43
import { match_bracket } from '../utils/bracket.js';
54
import { parse_expression_at } from '../acorn.js';
65
import { regex_not_newline_characters } from '../../patterns.js';
76
import * as e from '../../../errors.js';
8-
import { locator } from '../../../state.js';
97

108
/**
119
* @param {Parser} parser
@@ -15,20 +13,13 @@ export default function read_pattern(parser) {
1513
const start = parser.index;
1614
let i = parser.index;
1715

18-
const name = parser.read_identifier();
16+
const id = parser.read_identifier();
1917

20-
if (name !== null) {
18+
if (id.name !== '') {
2119
const annotation = read_type_annotation(parser);
2220

2321
return {
24-
type: 'Identifier',
25-
name,
26-
start,
27-
loc: {
28-
start: /** @type {Location} */ (locator(start)),
29-
end: /** @type {Location} */ (locator(parser.index))
30-
},
31-
end: parser.index,
22+
...id,
3223
typeAnnotation: annotation
3324
};
3425
}

0 commit comments

Comments
 (0)