From 6c1a39fff198a6f926321e6130976f3312cf51ae Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 26 Nov 2024 10:58:05 -0500 Subject: [PATCH 1/8] make get_possible_element_siblings non-recursive --- .../phases/2-analyze/css/css-prune.js | 80 +++++++++---------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js b/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js index 6b9410e4b5ee..e8116a034d2f 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js +++ b/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js @@ -932,66 +932,66 @@ function find_previous_sibling(node) { } /** - * @param {Compiler.SvelteNode} node + * @param {Compiler.AST.RegularElement | Compiler.AST.SvelteElement} element * @param {boolean} adjacent_only * @returns {Map} */ -function get_possible_element_siblings(node, adjacent_only) { +function get_possible_element_siblings(element, adjacent_only) { /** @type {Map} */ const result = new Map(); /** @type {Compiler.SvelteNode} */ - let prev = node; - while ((prev = find_previous_sibling(prev))) { - if (prev.type === 'RegularElement') { - const has_slot_attribute = prev.attributes.some( - (attr) => attr.type === 'Attribute' && attr.name.toLowerCase() === 'slot' - ); + let node = element; + let path = element.metadata.path; + let i = path.length; + + while (i--) { + let current = node; + + while ((node = find_previous_sibling(node))) { + if (node.type === 'RegularElement') { + const has_slot_attribute = node.attributes.some( + (attr) => attr.type === 'Attribute' && attr.name.toLowerCase() === 'slot' + ); - if (!has_slot_attribute) { - result.set(prev, NODE_DEFINITELY_EXISTS); + if (!has_slot_attribute) { + result.set(node, NODE_DEFINITELY_EXISTS); - if (adjacent_only) { + if (adjacent_only) { + return result; + } + } + } else if (is_block(node)) { + const possible_last_child = get_possible_last_child(node, adjacent_only); + add_to_map(possible_last_child, result); + if (adjacent_only && has_definite_elements(possible_last_child)) { return result; } + } else if ( + node.type === 'SlotElement' || + node.type === 'RenderTag' || + node.type === 'SvelteElement' + ) { + result.set(node, NODE_PROBABLY_EXISTS); + // Special case: slots, render tags and svelte:element tags could resolve to no siblings, + // so we want to continue until we find a definite sibling even with the adjacent-only combinator } - } else if (is_block(prev)) { - const possible_last_child = get_possible_last_child(prev, adjacent_only); - add_to_map(possible_last_child, result); - if (adjacent_only && has_definite_elements(possible_last_child)) { - return result; - } - } else if ( - prev.type === 'SlotElement' || - prev.type === 'RenderTag' || - prev.type === 'SvelteElement' - ) { - result.set(prev, NODE_PROBABLY_EXISTS); - // Special case: slots, render tags and svelte:element tags could resolve to no siblings, - // so we want to continue until we find a definite sibling even with the adjacent-only combinator } - } - /** @type {Compiler.SvelteNode | null} */ - let parent = node; + let parent = path[i]; - while ( - // @ts-expect-error TODO - (parent = parent?.parent) && - is_block(parent) - ) { - const possible_siblings = get_possible_element_siblings(parent, adjacent_only); - add_to_map(possible_siblings, result); + if (parent.type === 'Fragment') { + parent = path[--i]; + } + + if (!parent || !is_block(parent)) break; - // @ts-expect-error - if (parent.type === 'EachBlock' && !parent.fallback?.nodes.includes(node)) { + if (parent.type === 'EachBlock' && !parent.fallback?.nodes.includes(current)) { // `{#each ...}{/each}` — `` can be previous sibling of `` add_to_map(get_possible_last_child(parent, adjacent_only), result); } - if (adjacent_only && has_definite_elements(possible_siblings)) { - break; - } + node = parent; } return result; From 5ce70131702180c56880771bd5444bad6fbb54d5 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 26 Nov 2024 12:11:42 -0500 Subject: [PATCH 2/8] treat slots as blocks --- .../phases/2-analyze/css/css-prune.js | 74 ++++--------------- 1 file changed, 13 insertions(+), 61 deletions(-) diff --git a/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js b/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js index e8116a034d2f..f3a5d77d7771 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js +++ b/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js @@ -880,57 +880,6 @@ function get_element_parent(node) { return null; } -/** - * Finds the given node's previous sibling in the DOM - * - * The Svelte `` is just a placeholder and is not actually real. Any children nodes - * in `` are 'flattened' and considered as the same level as the ``'s siblings - * - * e.g. - * ```html - *

Heading 1

- * - *

Heading 2

- *
- * ``` - * - * is considered to look like: - * ```html - *

Heading 1

- *

Heading 2

- * ``` - * @param {Compiler.SvelteNode} node - * @returns {Compiler.SvelteNode} - */ -function find_previous_sibling(node) { - /** @type {Compiler.SvelteNode} */ - let current_node = node; - - while ( - // @ts-expect-error TODO - !current_node.prev && - // @ts-expect-error TODO - current_node.parent?.type === 'SlotElement' - ) { - // @ts-expect-error TODO - current_node = current_node.parent; - } - - // @ts-expect-error - current_node = current_node.prev; - - while (current_node?.type === 'SlotElement') { - const slot_children = current_node.fragment.nodes; - if (slot_children.length > 0) { - current_node = slot_children[slot_children.length - 1]; - } else { - break; - } - } - - return current_node; -} - /** * @param {Compiler.AST.RegularElement | Compiler.AST.SvelteElement} element * @param {boolean} adjacent_only @@ -948,7 +897,8 @@ function get_possible_element_siblings(element, adjacent_only) { while (i--) { let current = node; - while ((node = find_previous_sibling(node))) { + // @ts-expect-error + while ((node = node.prev)) { if (node.type === 'RegularElement') { const has_slot_attribute = node.attributes.some( (attr) => attr.type === 'Attribute' && attr.name.toLowerCase() === 'slot' @@ -962,16 +912,16 @@ function get_possible_element_siblings(element, adjacent_only) { } } } else if (is_block(node)) { + if (node.type === 'SlotElement') { + result.set(node, NODE_PROBABLY_EXISTS); + } + const possible_last_child = get_possible_last_child(node, adjacent_only); add_to_map(possible_last_child, result); if (adjacent_only && has_definite_elements(possible_last_child)) { return result; } - } else if ( - node.type === 'SlotElement' || - node.type === 'RenderTag' || - node.type === 'SvelteElement' - ) { + } else if (node.type === 'RenderTag' || node.type === 'SvelteElement') { result.set(node, NODE_PROBABLY_EXISTS); // Special case: slots, render tags and svelte:element tags could resolve to no siblings, // so we want to continue until we find a definite sibling even with the adjacent-only combinator @@ -998,7 +948,7 @@ function get_possible_element_siblings(element, adjacent_only) { } /** - * @param {Compiler.AST.EachBlock | Compiler.AST.IfBlock | Compiler.AST.AwaitBlock | Compiler.AST.KeyBlock} node + * @param {Compiler.AST.EachBlock | Compiler.AST.IfBlock | Compiler.AST.AwaitBlock | Compiler.AST.KeyBlock | Compiler.AST.SlotElement} node * @param {boolean} adjacent_only * @returns {Map} */ @@ -1022,6 +972,7 @@ function get_possible_last_child(node, adjacent_only) { break; case 'KeyBlock': + case 'SlotElement': fragments.push(node.fragment); break; } @@ -1029,7 +980,7 @@ function get_possible_last_child(node, adjacent_only) { /** @type {NodeMap} */ const result = new Map(); - let exhaustive = true; + let exhaustive = node.type !== 'SlotElement'; for (const fragment of fragments) { if (fragment == null) { @@ -1121,13 +1072,14 @@ function loop_child(children, adjacent_only) { /** * @param {Compiler.SvelteNode} node - * @returns {node is Compiler.AST.IfBlock | Compiler.AST.EachBlock | Compiler.AST.AwaitBlock | Compiler.AST.KeyBlock} + * @returns {node is Compiler.AST.IfBlock | Compiler.AST.EachBlock | Compiler.AST.AwaitBlock | Compiler.AST.KeyBlock | Compiler.AST.SlotElement} */ function is_block(node) { return ( node.type === 'IfBlock' || node.type === 'EachBlock' || node.type === 'AwaitBlock' || - node.type === 'KeyBlock' + node.type === 'KeyBlock' || + node.type === 'SlotElement' ); } From 91ba2035bc9cd1544247a2d2deaef4439da2d1e6 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 26 Nov 2024 13:00:45 -0500 Subject: [PATCH 3/8] simplify --- .../compiler/phases/2-analyze/css/css-prune.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js b/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js index f3a5d77d7771..de0eec9aa4b4 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js +++ b/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js @@ -895,7 +895,7 @@ function get_possible_element_siblings(element, adjacent_only) { let i = path.length; while (i--) { - let current = node; + let fragment = /** @type {Compiler.AST.Fragment} */ (path[i--]); // @ts-expect-error while ((node = node.prev)) { @@ -928,20 +928,14 @@ function get_possible_element_siblings(element, adjacent_only) { } } - let parent = path[i]; + node = path[i]; - if (parent.type === 'Fragment') { - parent = path[--i]; - } - - if (!parent || !is_block(parent)) break; + if (!node || !is_block(node)) break; - if (parent.type === 'EachBlock' && !parent.fallback?.nodes.includes(current)) { + if (node.type === 'EachBlock' && fragment === node.body) { // `{#each ...}
{/each}` — `` can be previous sibling of `` - add_to_map(get_possible_last_child(parent, adjacent_only), result); + add_to_map(get_possible_last_child(node, adjacent_only), result); } - - node = parent; } return result; From 8b1f4e2096a1c0b443a7e97fa87950b7f688c4f9 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 26 Nov 2024 13:08:20 -0500 Subject: [PATCH 4/8] simplify --- .../phases/2-analyze/css/css-prune.js | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js b/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js index de0eec9aa4b4..dec85c03612a 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js +++ b/packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js @@ -888,17 +888,20 @@ function get_element_parent(node) { function get_possible_element_siblings(element, adjacent_only) { /** @type {Map} */ const result = new Map(); + const path = element.metadata.path; /** @type {Compiler.SvelteNode} */ - let node = element; - let path = element.metadata.path; + let current = element; + let i = path.length; while (i--) { - let fragment = /** @type {Compiler.AST.Fragment} */ (path[i--]); + const fragment = /** @type {Compiler.AST.Fragment} */ (path[i--]); + let j = fragment.nodes.indexOf(current); + + while (j--) { + const node = fragment.nodes[j]; - // @ts-expect-error - while ((node = node.prev)) { if (node.type === 'RegularElement') { const has_slot_attribute = node.attributes.some( (attr) => attr.type === 'Attribute' && attr.name.toLowerCase() === 'slot' @@ -928,13 +931,13 @@ function get_possible_element_siblings(element, adjacent_only) { } } - node = path[i]; + current = path[i]; - if (!node || !is_block(node)) break; + if (!current || !is_block(current)) break; - if (node.type === 'EachBlock' && fragment === node.body) { + if (current.type === 'EachBlock' && fragment === current.body) { // `{#each ...}{/each}` — `` can be previous sibling of `` - add_to_map(get_possible_last_child(node, adjacent_only), result); + add_to_map(get_possible_last_child(current, adjacent_only), result); } } From 8dd5e6d196a3c7b2c9e913e42341d9f80903bbc6 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 26 Nov 2024 13:15:46 -0500 Subject: [PATCH 5/8] add test --- .../unused-ts-as-expression/_config.js | 20 +++++++++++++++++++ .../unused-ts-as-expression/expected.css | 4 ++++ .../unused-ts-as-expression/input.svelte | 13 ++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 packages/svelte/tests/css/samples/unused-ts-as-expression/_config.js create mode 100644 packages/svelte/tests/css/samples/unused-ts-as-expression/expected.css create mode 100644 packages/svelte/tests/css/samples/unused-ts-as-expression/input.svelte diff --git a/packages/svelte/tests/css/samples/unused-ts-as-expression/_config.js b/packages/svelte/tests/css/samples/unused-ts-as-expression/_config.js new file mode 100644 index 000000000000..e2679428cc78 --- /dev/null +++ b/packages/svelte/tests/css/samples/unused-ts-as-expression/_config.js @@ -0,0 +1,20 @@ +import { test } from '../../test'; + +export default test({ + warnings: [ + { + code: 'css_unused_selector', + end: { + character: 127, + column: 28, + line: 10 + }, + message: 'Unused CSS selector "[data-active=\'true\'] > span"', + start: { + character: 100, + column: 1, + line: 10 + } + } + ] +}); diff --git a/packages/svelte/tests/css/samples/unused-ts-as-expression/expected.css b/packages/svelte/tests/css/samples/unused-ts-as-expression/expected.css new file mode 100644 index 000000000000..571d2acfbc69 --- /dev/null +++ b/packages/svelte/tests/css/samples/unused-ts-as-expression/expected.css @@ -0,0 +1,4 @@ + + /* (unused) [data-active='true'] > span { + background-color: red; + }*/ diff --git a/packages/svelte/tests/css/samples/unused-ts-as-expression/input.svelte b/packages/svelte/tests/css/samples/unused-ts-as-expression/input.svelte new file mode 100644 index 000000000000..24fc0096bf7e --- /dev/null +++ b/packages/svelte/tests/css/samples/unused-ts-as-expression/input.svelte @@ -0,0 +1,13 @@ + + +
+ +
+ + From 732da8334c23673775243062c83d5f665b9d9ee3 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 26 Nov 2024 13:16:24 -0500 Subject: [PATCH 6/8] changeset --- .changeset/tender-balloons-relate.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tender-balloons-relate.md diff --git a/.changeset/tender-balloons-relate.md b/.changeset/tender-balloons-relate.md new file mode 100644 index 000000000000..ea7ce094b624 --- /dev/null +++ b/.changeset/tender-balloons-relate.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: disregard TypeScript nodes when pruning CSS From f105e48e97f4bb03751bc7ff3a71b4b5d4730310 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 26 Nov 2024 13:51:09 -0500 Subject: [PATCH 7/8] chore: remove node.parent and node.prev --- 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 | 28 ++++++------------- .../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 -- 10 files changed, 13 insertions(+), 61 deletions(-) diff --git a/packages/svelte/src/compiler/index.js b/packages/svelte/src/compiler/index.js index 2b3ad99d8b8a..fbd9c16e5d53 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 e603da312b59..52b4bd586077 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 de29aa526101..7639c2f0eda8 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 9675e5bbd829..85023cb781ce 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 9082b76c4972..2d96db9b0db6 100644 --- a/packages/svelte/src/compiler/phases/1-parse/state/element.js +++ b/packages/svelte/src/compiler/phases/1-parse/state/element.js @@ -153,8 +153,7 @@ export default function element(parser) { scoped: false, has_spread: false, path: [] - }, - parent: null + } } : /** @type {ElementLike} */ ({ type, @@ -163,7 +162,6 @@ export default function element(parser) { name, attributes: [], fragment: create_fragment(true), - parent: null, metadata: { // unpopulated at first, differs between types } @@ -348,8 +346,7 @@ export default function element(parser) { end, type: 'Text', data, - raw: data, - parent: null + raw: data }; element.fragment.nodes.push(node); @@ -422,8 +419,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 +453,6 @@ function read_attribute(parser) { start, end: parser.index, expression, - parent: null, metadata: { expression: create_expression_metadata() } @@ -486,7 +481,6 @@ function read_attribute(parser) { type: 'Identifier', name }, - parent: null, metadata: { expression: create_expression_metadata() } @@ -531,7 +525,6 @@ function read_attribute(parser) { name: directive_name, modifiers: /** @type {Array<'important'>} */ (modifiers), value, - parent: null, metadata: { expression: create_expression_metadata() } @@ -556,19 +549,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 +617,7 @@ function read_attribute_value(parser) { end: parser.index - 1, type: 'Text', raw: '', - data: '', - parent: null + data: '' } ]; } @@ -681,8 +674,7 @@ function read_sequence(parser, done, location) { end: -1, type: 'Text', raw: '', - data: '', - parent: null + data: '' }; /** @type {Array} */ @@ -729,7 +721,6 @@ function read_sequence(parser, done, location) { start: index, end: parser.index, expression, - parent: null, metadata: { expression: create_expression_metadata() } @@ -742,8 +733,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/2-analyze/index.js b/packages/svelte/src/compiler/phases/2-analyze/index.js index 0d8caa0a4b62..ce34544eb4c8 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 c386c4f7c05e..2ab5d9b9fdfa 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 809c627098e8..a1b8dc0d03d6 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 ead525aaa149..1a1c21bb4b0c 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 59456412bda7..0be0b25ab458 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 { From 467c6ed427fb22beadcba43a87b3f3b23a36c563 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 26 Nov 2024 14:01:52 -0500 Subject: [PATCH 8/8] simplify --- .../compiler/phases/1-parse/state/element.js | 1 - .../src/compiler/phases/1-parse/state/tag.js | 22 ++++++++----------- .../src/compiler/phases/1-parse/state/text.js | 2 +- 3 files changed, 10 insertions(+), 15 deletions(-) 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 2d96db9b0db6..6eed81139310 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, 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 de118960ecea..1632099c2930 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 388ffa19c079..d0af3a846a55 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,