Skip to content

Commit 1a8aab0

Browse files
authored
chore: remove node.parent and node.prev (#14447)
* make get_possible_element_siblings non-recursive * treat slots as blocks * simplify * simplify * add test * changeset * chore: remove node.parent and node.prev * simplify
1 parent 610bc98 commit 1a8aab0

File tree

12 files changed

+23
-76
lines changed

12 files changed

+23
-76
lines changed

packages/svelte/src/compiler/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ function to_public_ast(source, ast, modern) {
120120
if (modern) {
121121
const clean = (/** @type {any} */ node) => {
122122
delete node.metadata;
123-
delete node.parent;
124123
};
125124

126125
ast.options?.attributes.forEach((attribute) => {

packages/svelte/src/compiler/legacy.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ export function convert(source, ast) {
4545
return /** @type {Legacy.LegacyRoot} */ (
4646
walk(root, null, {
4747
_(node, { next }) {
48-
// @ts-ignore
49-
delete node.parent;
5048
// @ts-ignore
5149
delete node.metadata;
5250
next();
@@ -62,8 +60,6 @@ export function convert(source, ast) {
6260
idx = node.fragment.nodes.length;
6361
}
6462

65-
// @ts-ignore
66-
delete options.__raw__.parent;
6763
node.fragment.nodes.splice(idx, 0, /** @type {any} */ (options).__raw__);
6864
}
6965

@@ -85,15 +81,11 @@ export function convert(source, ast) {
8581
}
8682

8783
if (instance) {
88-
// @ts-ignore
89-
delete instance.parent;
9084
// @ts-ignore
9185
delete instance.attributes;
9286
}
9387

9488
if (module) {
95-
// @ts-ignore
96-
delete module.parent;
9789
// @ts-ignore
9890
delete module.attributes;
9991
}

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

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -262,30 +262,12 @@ export class Parser {
262262
}
263263

264264
/**
265-
* @template T
266-
* @param {Omit<T, 'prev' | 'parent'>} node
265+
* @template {AST.Fragment['nodes'][number]} T
266+
* @param {T} node
267267
* @returns {T}
268268
*/
269269
append(node) {
270-
const current = this.current();
271-
const fragment = this.fragments.at(-1);
272-
273-
Object.defineProperties(node, {
274-
prev: {
275-
enumerable: false,
276-
value: fragment?.nodes.at(-1) ?? null
277-
},
278-
parent: {
279-
enumerable: false,
280-
configurable: true,
281-
value: current
282-
}
283-
});
284-
285-
// @ts-expect-error
286-
fragment.nodes.push(node);
287-
288-
// @ts-expect-error
270+
this.fragments.at(-1)?.nodes.push(node);
289271
return node;
290272
}
291273
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ export function read_script(parser, start, attributes) {
8484
end: parser.index,
8585
context,
8686
content: ast,
87-
parent: null,
8887
// @ts-ignore
8988
attributes
9089
};

packages/svelte/src/compiler/phases/1-parse/state/element.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export default function element(parser) {
5656
const data = parser.read_until(regex_closing_comment);
5757
parser.eat('-->', true);
5858

59-
/** @type {ReturnType<typeof parser.append<AST.Comment>>} */
6059
parser.append({
6160
type: 'Comment',
6261
start,
@@ -153,8 +152,7 @@ export default function element(parser) {
153152
scoped: false,
154153
has_spread: false,
155154
path: []
156-
},
157-
parent: null
155+
}
158156
}
159157
: /** @type {ElementLike} */ ({
160158
type,
@@ -163,7 +161,6 @@ export default function element(parser) {
163161
name,
164162
attributes: [],
165163
fragment: create_fragment(true),
166-
parent: null,
167164
metadata: {
168165
// unpopulated at first, differs between types
169166
}
@@ -348,8 +345,7 @@ export default function element(parser) {
348345
end,
349346
type: 'Text',
350347
data,
351-
raw: data,
352-
parent: null
348+
raw: data
353349
};
354350

355351
element.fragment.nodes.push(node);
@@ -422,8 +418,7 @@ function read_static_attribute(parser) {
422418
end: quoted ? parser.index - 1 : parser.index,
423419
type: 'Text',
424420
raw: raw,
425-
data: decode_character_references(raw, true),
426-
parent: null
421+
data: decode_character_references(raw, true)
427422
}
428423
];
429424
}
@@ -457,7 +452,6 @@ function read_attribute(parser) {
457452
start,
458453
end: parser.index,
459454
expression,
460-
parent: null,
461455
metadata: {
462456
expression: create_expression_metadata()
463457
}
@@ -486,7 +480,6 @@ function read_attribute(parser) {
486480
type: 'Identifier',
487481
name
488482
},
489-
parent: null,
490483
metadata: {
491484
expression: create_expression_metadata()
492485
}
@@ -531,7 +524,6 @@ function read_attribute(parser) {
531524
name: directive_name,
532525
modifiers: /** @type {Array<'important'>} */ (modifiers),
533526
value,
534-
parent: null,
535527
metadata: {
536528
expression: create_expression_metadata()
537529
}
@@ -556,19 +548,20 @@ function read_attribute(parser) {
556548
}
557549

558550
/** @type {Directive} */
559-
// @ts-expect-error TODO can't figure out this error
560551
const directive = {
561552
start,
562553
end,
563554
type,
564555
name: directive_name,
565-
modifiers,
566556
expression,
567557
metadata: {
568558
expression: create_expression_metadata()
569559
}
570560
};
571561

562+
// @ts-expect-error we do this separately from the declaration to avoid upsetting typescript
563+
directive.modifiers = modifiers;
564+
572565
if (directive.type === 'TransitionDirective') {
573566
const direction = name.slice(0, colon_index);
574567
directive.intro = direction === 'in' || direction === 'transition';
@@ -623,8 +616,7 @@ function read_attribute_value(parser) {
623616
end: parser.index - 1,
624617
type: 'Text',
625618
raw: '',
626-
data: '',
627-
parent: null
619+
data: ''
628620
}
629621
];
630622
}
@@ -681,8 +673,7 @@ function read_sequence(parser, done, location) {
681673
end: -1,
682674
type: 'Text',
683675
raw: '',
684-
data: '',
685-
parent: null
676+
data: ''
686677
};
687678

688679
/** @type {Array<AST.Text | AST.ExpressionTag>} */
@@ -729,7 +720,6 @@ function read_sequence(parser, done, location) {
729720
start: index,
730721
end: parser.index,
731722
expression,
732-
parent: null,
733723
metadata: {
734724
expression: create_expression_metadata()
735725
}
@@ -742,8 +732,7 @@ function read_sequence(parser, done, location) {
742732
end: -1,
743733
type: 'Text',
744734
raw: '',
745-
data: '',
746-
parent: null
735+
data: ''
747736
};
748737
} else {
749738
current_chunk.raw += parser.template[parser.index++];

packages/svelte/src/compiler/phases/1-parse/state/tag.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export default function tag(parser) {
3333
parser.allow_whitespace();
3434
parser.eat('}', true);
3535

36-
/** @type {ReturnType<typeof parser.append<AST.ExpressionTag>>} */
3736
parser.append({
3837
type: 'ExpressionTag',
3938
start,
@@ -53,7 +52,7 @@ function open(parser) {
5352
if (parser.eat('if')) {
5453
parser.require_whitespace();
5554

56-
/** @type {ReturnType<typeof parser.append<AST.IfBlock>>} */
55+
/** @type {AST.IfBlock} */
5756
const block = parser.append({
5857
type: 'IfBlock',
5958
elseif: false,
@@ -174,7 +173,7 @@ function open(parser) {
174173

175174
parser.eat('}', true);
176175

177-
/** @type {ReturnType<typeof parser.append<AST.EachBlock>>} */
176+
/** @type {AST.EachBlock} */
178177
const block = parser.append({
179178
type: 'EachBlock',
180179
start,
@@ -198,7 +197,7 @@ function open(parser) {
198197
const expression = read_expression(parser);
199198
parser.allow_whitespace();
200199

201-
/** @type {ReturnType<typeof parser.append<AST.AwaitBlock>>} */
200+
/** @type {AST.AwaitBlock} */
202201
const block = parser.append({
203202
type: 'AwaitBlock',
204203
start,
@@ -252,7 +251,7 @@ function open(parser) {
252251

253252
parser.eat('}', true);
254253

255-
/** @type {ReturnType<typeof parser.append<AST.KeyBlock>>} */
254+
/** @type {AST.KeyBlock} */
256255
const block = parser.append({
257256
type: 'KeyBlock',
258257
start,
@@ -303,7 +302,7 @@ function open(parser) {
303302
parser.allow_whitespace();
304303
parser.eat('}', true);
305304

306-
/** @type {ReturnType<typeof parser.append<AST.SnippetBlock>>} */
305+
/** @type {AST.SnippetBlock} */
307306
const block = parser.append({
308307
type: 'SnippetBlock',
309308
start,
@@ -355,7 +354,7 @@ function next(parser) {
355354
let elseif_start = start - 1;
356355
while (parser.template[elseif_start] !== '{') elseif_start -= 1;
357356

358-
/** @type {ReturnType<typeof parser.append<AST.IfBlock>>} */
357+
/** @type {AST.IfBlock} */
359358
const child = parser.append({
360359
start: elseif_start,
361360
end: -1,
@@ -499,7 +498,6 @@ function special(parser) {
499498
parser.allow_whitespace();
500499
parser.eat('}', true);
501500

502-
/** @type {ReturnType<typeof parser.append<AST.HtmlTag>>} */
503501
parser.append({
504502
type: 'HtmlTag',
505503
start,
@@ -537,7 +535,6 @@ function special(parser) {
537535
parser.eat('}', true);
538536
}
539537

540-
/** @type {ReturnType<typeof parser.append<AST.DebugTag>>} */
541538
parser.append({
542539
type: 'DebugTag',
543540
start,
@@ -570,7 +567,6 @@ function special(parser) {
570567

571568
parser.eat('}', true);
572569

573-
/** @type {ReturnType<typeof parser.append<AST.ConstTag>>} */
574570
parser.append({
575571
type: 'ConstTag',
576572
start,
@@ -601,15 +597,15 @@ function special(parser) {
601597
parser.allow_whitespace();
602598
parser.eat('}', true);
603599

604-
/** @type {ReturnType<typeof parser.append<AST.RenderTag>>} */
605600
parser.append({
606601
type: 'RenderTag',
607602
start,
608603
end: parser.index,
609-
expression: expression,
604+
expression: /** @type {AST.RenderTag['expression']} */ (expression),
610605
metadata: {
611606
dynamic: false,
612-
args_with_call_expression: new Set()
607+
args_with_call_expression: new Set(),
608+
path: []
613609
}
614610
});
615611
}

packages/svelte/src/compiler/phases/1-parse/state/text.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default function text(parser) {
1212
data += parser.template[parser.index++];
1313
}
1414

15-
/** @type {ReturnType<typeof parser.append<AST.Text>>} */
15+
/** @type {AST.Text} */
1616
parser.append({
1717
type: 'Text',
1818
start,

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,7 @@ export function analyze_component(root, source, options) {
758758
data: ` ${analysis.css.hash}`,
759759
raw: ` ${analysis.css.hash}`,
760760
start: -1,
761-
end: -1,
762-
parent: null
761+
end: -1
763762
};
764763

765764
if (Array.isArray(class_attribute.value)) {
@@ -775,7 +774,6 @@ export function analyze_component(root, source, options) {
775774
type: 'Text',
776775
data: analysis.css.hash,
777776
raw: analysis.css.hash,
778-
parent: null,
779777
start: -1,
780778
end: -1
781779
}

packages/svelte/src/compiler/phases/3-transform/server/visitors/shared/element.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ export function build_element_attributes(node, context) {
135135
type: 'ExpressionTag',
136136
start: -1,
137137
end: -1,
138-
parent: attribute,
139138
expression: is_checkbox
140139
? b.call(
141140
b.member(attribute.expression, 'includes'),
@@ -159,7 +158,6 @@ export function build_element_attributes(node, context) {
159158
type: 'ExpressionTag',
160159
start: -1,
161160
end: -1,
162-
parent: attribute,
163161
expression: attribute.expression,
164162
metadata: {
165163
expression: create_expression_metadata()
@@ -376,7 +374,6 @@ function build_class_directives(class_directives, class_attribute) {
376374
type: 'Text',
377375
start: -1,
378376
end: -1,
379-
parent: class_attribute,
380377
data: ' ',
381378
raw: ' '
382379
});
@@ -386,7 +383,6 @@ function build_class_directives(class_directives, class_attribute) {
386383
type: 'ExpressionTag',
387384
start: -1,
388385
end: -1,
389-
parent: class_attribute,
390386
expression: b.call(
391387
b.member(b.call(b.member(b.array(expressions), 'filter'), b.id('Boolean')), b.id('join')),
392388
b.literal(' ')

packages/svelte/src/compiler/phases/3-transform/utils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ export function clean_nodes(
277277
trimmed.push({
278278
type: 'Comment',
279279
data: '',
280-
parent: first.parent,
281280
start: -1,
282281
end: -1
283282
});

0 commit comments

Comments
 (0)