From 1a8aab0e384c5706a9cf34aba69f56b050b1e549 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 26 Nov 2024 15:13:34 -0500 Subject: [PATCH] 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 --- packages/svelte/src/compiler/index.js | 1 - packages/svelte/src/compiler/legacy.js | 8 ----- .../src/compiler/phases/1-parse/index.js | 24 ++------------- .../compiler/phases/1-parse/read/script.js | 1 - .../compiler/phases/1-parse/state/element.js | 29 ++++++------------- .../src/compiler/phases/1-parse/state/tag.js | 22 ++++++-------- .../src/compiler/phases/1-parse/state/text.js | 2 +- .../src/compiler/phases/2-analyze/index.js | 4 +-- .../server/visitors/shared/element.js | 4 --- .../src/compiler/phases/3-transform/utils.js | 1 - packages/svelte/src/compiler/phases/nodes.js | 1 - .../svelte/src/compiler/types/template.d.ts | 2 -- 12 files changed, 23 insertions(+), 76 deletions(-) diff --git a/packages/svelte/src/compiler/index.js b/packages/svelte/src/compiler/index.js index 2b3ad99d8b..fbd9c16e5d 100644 --- a/packages/svelte/src/compiler/index.js +++ b/packages/svelte/src/compiler/index.js @@ -120,7 +120,6 @@ function to_public_ast(source, ast, modern) { if (modern) { const clean = (/** @type {any} */ node) => { delete node.metadata; - delete node.parent; }; ast.options?.attributes.forEach((attribute) => { diff --git a/packages/svelte/src/compiler/legacy.js b/packages/svelte/src/compiler/legacy.js index e603da312b..52b4bd5860 100644 --- a/packages/svelte/src/compiler/legacy.js +++ b/packages/svelte/src/compiler/legacy.js @@ -45,8 +45,6 @@ export function convert(source, ast) { return /** @type {Legacy.LegacyRoot} */ ( walk(root, null, { _(node, { next }) { - // @ts-ignore - delete node.parent; // @ts-ignore delete node.metadata; next(); @@ -62,8 +60,6 @@ export function convert(source, ast) { idx = node.fragment.nodes.length; } - // @ts-ignore - delete options.__raw__.parent; node.fragment.nodes.splice(idx, 0, /** @type {any} */ (options).__raw__); } @@ -85,15 +81,11 @@ export function convert(source, ast) { } if (instance) { - // @ts-ignore - delete instance.parent; // @ts-ignore delete instance.attributes; } if (module) { - // @ts-ignore - delete module.parent; // @ts-ignore delete module.attributes; } diff --git a/packages/svelte/src/compiler/phases/1-parse/index.js b/packages/svelte/src/compiler/phases/1-parse/index.js index de29aa5261..7639c2f0ed 100644 --- a/packages/svelte/src/compiler/phases/1-parse/index.js +++ b/packages/svelte/src/compiler/phases/1-parse/index.js @@ -262,30 +262,12 @@ export class Parser { } /** - * @template T - * @param {Omit} node + * @template {AST.Fragment['nodes'][number]} T + * @param {T} node * @returns {T} */ append(node) { - const current = this.current(); - const fragment = this.fragments.at(-1); - - Object.defineProperties(node, { - prev: { - enumerable: false, - value: fragment?.nodes.at(-1) ?? null - }, - parent: { - enumerable: false, - configurable: true, - value: current - } - }); - - // @ts-expect-error - fragment.nodes.push(node); - - // @ts-expect-error + this.fragments.at(-1)?.nodes.push(node); return node; } } diff --git a/packages/svelte/src/compiler/phases/1-parse/read/script.js b/packages/svelte/src/compiler/phases/1-parse/read/script.js index 9675e5bbd8..85023cb781 100644 --- a/packages/svelte/src/compiler/phases/1-parse/read/script.js +++ b/packages/svelte/src/compiler/phases/1-parse/read/script.js @@ -84,7 +84,6 @@ export function read_script(parser, start, attributes) { end: parser.index, context, content: ast, - parent: null, // @ts-ignore attributes }; diff --git a/packages/svelte/src/compiler/phases/1-parse/state/element.js b/packages/svelte/src/compiler/phases/1-parse/state/element.js index 9082b76c49..6eed811393 100644 --- a/packages/svelte/src/compiler/phases/1-parse/state/element.js +++ b/packages/svelte/src/compiler/phases/1-parse/state/element.js @@ -56,7 +56,6 @@ export default function element(parser) { const data = parser.read_until(regex_closing_comment); parser.eat('-->', true); - /** @type {ReturnType>} */ parser.append({ type: 'Comment', start, @@ -153,8 +152,7 @@ export default function element(parser) { scoped: false, has_spread: false, path: [] - }, - parent: null + } } : /** @type {ElementLike} */ ({ type, @@ -163,7 +161,6 @@ export default function element(parser) { name, attributes: [], fragment: create_fragment(true), - parent: null, metadata: { // unpopulated at first, differs between types } @@ -348,8 +345,7 @@ export default function element(parser) { end, type: 'Text', data, - raw: data, - parent: null + raw: data }; element.fragment.nodes.push(node); @@ -422,8 +418,7 @@ function read_static_attribute(parser) { end: quoted ? parser.index - 1 : parser.index, type: 'Text', raw: raw, - data: decode_character_references(raw, true), - parent: null + data: decode_character_references(raw, true) } ]; } @@ -457,7 +452,6 @@ function read_attribute(parser) { start, end: parser.index, expression, - parent: null, metadata: { expression: create_expression_metadata() } @@ -486,7 +480,6 @@ function read_attribute(parser) { type: 'Identifier', name }, - parent: null, metadata: { expression: create_expression_metadata() } @@ -531,7 +524,6 @@ function read_attribute(parser) { name: directive_name, modifiers: /** @type {Array<'important'>} */ (modifiers), value, - parent: null, metadata: { expression: create_expression_metadata() } @@ -556,19 +548,20 @@ function read_attribute(parser) { } /** @type {Directive} */ - // @ts-expect-error TODO can't figure out this error const directive = { start, end, type, name: directive_name, - modifiers, expression, metadata: { expression: create_expression_metadata() } }; + // @ts-expect-error we do this separately from the declaration to avoid upsetting typescript + directive.modifiers = modifiers; + if (directive.type === 'TransitionDirective') { const direction = name.slice(0, colon_index); directive.intro = direction === 'in' || direction === 'transition'; @@ -623,8 +616,7 @@ function read_attribute_value(parser) { end: parser.index - 1, type: 'Text', raw: '', - data: '', - parent: null + data: '' } ]; } @@ -681,8 +673,7 @@ function read_sequence(parser, done, location) { end: -1, type: 'Text', raw: '', - data: '', - parent: null + data: '' }; /** @type {Array} */ @@ -729,7 +720,6 @@ function read_sequence(parser, done, location) { start: index, end: parser.index, expression, - parent: null, metadata: { expression: create_expression_metadata() } @@ -742,8 +732,7 @@ function read_sequence(parser, done, location) { end: -1, type: 'Text', raw: '', - data: '', - parent: null + data: '' }; } else { current_chunk.raw += parser.template[parser.index++]; diff --git a/packages/svelte/src/compiler/phases/1-parse/state/tag.js b/packages/svelte/src/compiler/phases/1-parse/state/tag.js index de118960ec..1632099c29 100644 --- a/packages/svelte/src/compiler/phases/1-parse/state/tag.js +++ b/packages/svelte/src/compiler/phases/1-parse/state/tag.js @@ -33,7 +33,6 @@ export default function tag(parser) { parser.allow_whitespace(); parser.eat('}', true); - /** @type {ReturnType>} */ parser.append({ type: 'ExpressionTag', start, @@ -53,7 +52,7 @@ function open(parser) { if (parser.eat('if')) { parser.require_whitespace(); - /** @type {ReturnType>} */ + /** @type {AST.IfBlock} */ const block = parser.append({ type: 'IfBlock', elseif: false, @@ -174,7 +173,7 @@ function open(parser) { parser.eat('}', true); - /** @type {ReturnType>} */ + /** @type {AST.EachBlock} */ const block = parser.append({ type: 'EachBlock', start, @@ -198,7 +197,7 @@ function open(parser) { const expression = read_expression(parser); parser.allow_whitespace(); - /** @type {ReturnType>} */ + /** @type {AST.AwaitBlock} */ const block = parser.append({ type: 'AwaitBlock', start, @@ -252,7 +251,7 @@ function open(parser) { parser.eat('}', true); - /** @type {ReturnType>} */ + /** @type {AST.KeyBlock} */ const block = parser.append({ type: 'KeyBlock', start, @@ -303,7 +302,7 @@ function open(parser) { parser.allow_whitespace(); parser.eat('}', true); - /** @type {ReturnType>} */ + /** @type {AST.SnippetBlock} */ const block = parser.append({ type: 'SnippetBlock', start, @@ -355,7 +354,7 @@ function next(parser) { let elseif_start = start - 1; while (parser.template[elseif_start] !== '{') elseif_start -= 1; - /** @type {ReturnType>} */ + /** @type {AST.IfBlock} */ const child = parser.append({ start: elseif_start, end: -1, @@ -499,7 +498,6 @@ function special(parser) { parser.allow_whitespace(); parser.eat('}', true); - /** @type {ReturnType>} */ parser.append({ type: 'HtmlTag', start, @@ -537,7 +535,6 @@ function special(parser) { parser.eat('}', true); } - /** @type {ReturnType>} */ parser.append({ type: 'DebugTag', start, @@ -570,7 +567,6 @@ function special(parser) { parser.eat('}', true); - /** @type {ReturnType>} */ parser.append({ type: 'ConstTag', start, @@ -601,15 +597,15 @@ function special(parser) { parser.allow_whitespace(); parser.eat('}', true); - /** @type {ReturnType>} */ parser.append({ type: 'RenderTag', start, end: parser.index, - expression: expression, + expression: /** @type {AST.RenderTag['expression']} */ (expression), metadata: { dynamic: false, - args_with_call_expression: new Set() + args_with_call_expression: new Set(), + path: [] } }); } diff --git a/packages/svelte/src/compiler/phases/1-parse/state/text.js b/packages/svelte/src/compiler/phases/1-parse/state/text.js index 388ffa19c0..d0af3a846a 100644 --- a/packages/svelte/src/compiler/phases/1-parse/state/text.js +++ b/packages/svelte/src/compiler/phases/1-parse/state/text.js @@ -12,7 +12,7 @@ export default function text(parser) { data += parser.template[parser.index++]; } - /** @type {ReturnType>} */ + /** @type {AST.Text} */ parser.append({ type: 'Text', start, diff --git a/packages/svelte/src/compiler/phases/2-analyze/index.js b/packages/svelte/src/compiler/phases/2-analyze/index.js index 0d8caa0a4b..ce34544eb4 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/index.js +++ b/packages/svelte/src/compiler/phases/2-analyze/index.js @@ -758,8 +758,7 @@ export function analyze_component(root, source, options) { data: ` ${analysis.css.hash}`, raw: ` ${analysis.css.hash}`, start: -1, - end: -1, - parent: null + end: -1 }; if (Array.isArray(class_attribute.value)) { @@ -775,7 +774,6 @@ export function analyze_component(root, source, options) { type: 'Text', data: analysis.css.hash, raw: analysis.css.hash, - parent: null, start: -1, end: -1 } diff --git a/packages/svelte/src/compiler/phases/3-transform/server/visitors/shared/element.js b/packages/svelte/src/compiler/phases/3-transform/server/visitors/shared/element.js index c386c4f7c0..2ab5d9b9fd 100644 --- a/packages/svelte/src/compiler/phases/3-transform/server/visitors/shared/element.js +++ b/packages/svelte/src/compiler/phases/3-transform/server/visitors/shared/element.js @@ -135,7 +135,6 @@ export function build_element_attributes(node, context) { type: 'ExpressionTag', start: -1, end: -1, - parent: attribute, expression: is_checkbox ? b.call( b.member(attribute.expression, 'includes'), @@ -159,7 +158,6 @@ export function build_element_attributes(node, context) { type: 'ExpressionTag', start: -1, end: -1, - parent: attribute, expression: attribute.expression, metadata: { expression: create_expression_metadata() @@ -376,7 +374,6 @@ function build_class_directives(class_directives, class_attribute) { type: 'Text', start: -1, end: -1, - parent: class_attribute, data: ' ', raw: ' ' }); @@ -386,7 +383,6 @@ function build_class_directives(class_directives, class_attribute) { type: 'ExpressionTag', start: -1, end: -1, - parent: class_attribute, expression: b.call( b.member(b.call(b.member(b.array(expressions), 'filter'), b.id('Boolean')), b.id('join')), b.literal(' ') diff --git a/packages/svelte/src/compiler/phases/3-transform/utils.js b/packages/svelte/src/compiler/phases/3-transform/utils.js index 809c627098..a1b8dc0d03 100644 --- a/packages/svelte/src/compiler/phases/3-transform/utils.js +++ b/packages/svelte/src/compiler/phases/3-transform/utils.js @@ -277,7 +277,6 @@ export function clean_nodes( trimmed.push({ type: 'Comment', data: '', - parent: first.parent, start: -1, end: -1 }); diff --git a/packages/svelte/src/compiler/phases/nodes.js b/packages/svelte/src/compiler/phases/nodes.js index ead525aaa1..1a1c21bb4b 100644 --- a/packages/svelte/src/compiler/phases/nodes.js +++ b/packages/svelte/src/compiler/phases/nodes.js @@ -43,7 +43,6 @@ export function create_attribute(name, start, end, value) { end, name, value, - parent: null, metadata: { expression: create_expression_metadata(), delegated: null diff --git a/packages/svelte/src/compiler/types/template.d.ts b/packages/svelte/src/compiler/types/template.d.ts index 59456412bd..0be0b25ab4 100644 --- a/packages/svelte/src/compiler/types/template.d.ts +++ b/packages/svelte/src/compiler/types/template.d.ts @@ -37,8 +37,6 @@ export namespace AST { type: string; start: number; end: number; - /** @internal This is set during parsing on elements/components/expressions/text (but not attributes etc) */ - parent: SvelteNode | null; } export interface Fragment {