Skip to content

Commit

Permalink
chore: remove node.parent and node.prev (#14447)
Browse files Browse the repository at this point in the history
* make get_possible_element_siblings non-recursive

* treat slots as blocks

* simplify

* simplify

* add test

* changeset

* chore: remove node.parent and node.prev

* simplify
  • Loading branch information
Rich-Harris authored Nov 26, 2024
1 parent 610bc98 commit 1a8aab0
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 76 deletions.
1 change: 0 additions & 1 deletion packages/svelte/src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
8 changes: 0 additions & 8 deletions packages/svelte/src/compiler/legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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__);
}

Expand All @@ -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;
}
Expand Down
24 changes: 3 additions & 21 deletions packages/svelte/src/compiler/phases/1-parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,30 +262,12 @@ export class Parser {
}

/**
* @template T
* @param {Omit<T, 'prev' | 'parent'>} 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;
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/svelte/src/compiler/phases/1-parse/read/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export function read_script(parser, start, attributes) {
end: parser.index,
context,
content: ast,
parent: null,
// @ts-ignore
attributes
};
Expand Down
29 changes: 9 additions & 20 deletions packages/svelte/src/compiler/phases/1-parse/state/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export default function element(parser) {
const data = parser.read_until(regex_closing_comment);
parser.eat('-->', true);

/** @type {ReturnType<typeof parser.append<AST.Comment>>} */
parser.append({
type: 'Comment',
start,
Expand Down Expand Up @@ -153,8 +152,7 @@ export default function element(parser) {
scoped: false,
has_spread: false,
path: []
},
parent: null
}
}
: /** @type {ElementLike} */ ({
type,
Expand All @@ -163,7 +161,6 @@ export default function element(parser) {
name,
attributes: [],
fragment: create_fragment(true),
parent: null,
metadata: {
// unpopulated at first, differs between types
}
Expand Down Expand Up @@ -348,8 +345,7 @@ export default function element(parser) {
end,
type: 'Text',
data,
raw: data,
parent: null
raw: data
};

element.fragment.nodes.push(node);
Expand Down Expand Up @@ -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)
}
];
}
Expand Down Expand Up @@ -457,7 +452,6 @@ function read_attribute(parser) {
start,
end: parser.index,
expression,
parent: null,
metadata: {
expression: create_expression_metadata()
}
Expand Down Expand Up @@ -486,7 +480,6 @@ function read_attribute(parser) {
type: 'Identifier',
name
},
parent: null,
metadata: {
expression: create_expression_metadata()
}
Expand Down Expand Up @@ -531,7 +524,6 @@ function read_attribute(parser) {
name: directive_name,
modifiers: /** @type {Array<'important'>} */ (modifiers),
value,
parent: null,
metadata: {
expression: create_expression_metadata()
}
Expand All @@ -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';
Expand Down Expand Up @@ -623,8 +616,7 @@ function read_attribute_value(parser) {
end: parser.index - 1,
type: 'Text',
raw: '',
data: '',
parent: null
data: ''
}
];
}
Expand Down Expand Up @@ -681,8 +673,7 @@ function read_sequence(parser, done, location) {
end: -1,
type: 'Text',
raw: '',
data: '',
parent: null
data: ''
};

/** @type {Array<AST.Text | AST.ExpressionTag>} */
Expand Down Expand Up @@ -729,7 +720,6 @@ function read_sequence(parser, done, location) {
start: index,
end: parser.index,
expression,
parent: null,
metadata: {
expression: create_expression_metadata()
}
Expand All @@ -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++];
Expand Down
22 changes: 9 additions & 13 deletions packages/svelte/src/compiler/phases/1-parse/state/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export default function tag(parser) {
parser.allow_whitespace();
parser.eat('}', true);

/** @type {ReturnType<typeof parser.append<AST.ExpressionTag>>} */
parser.append({
type: 'ExpressionTag',
start,
Expand All @@ -53,7 +52,7 @@ function open(parser) {
if (parser.eat('if')) {
parser.require_whitespace();

/** @type {ReturnType<typeof parser.append<AST.IfBlock>>} */
/** @type {AST.IfBlock} */
const block = parser.append({
type: 'IfBlock',
elseif: false,
Expand Down Expand Up @@ -174,7 +173,7 @@ function open(parser) {

parser.eat('}', true);

/** @type {ReturnType<typeof parser.append<AST.EachBlock>>} */
/** @type {AST.EachBlock} */
const block = parser.append({
type: 'EachBlock',
start,
Expand All @@ -198,7 +197,7 @@ function open(parser) {
const expression = read_expression(parser);
parser.allow_whitespace();

/** @type {ReturnType<typeof parser.append<AST.AwaitBlock>>} */
/** @type {AST.AwaitBlock} */
const block = parser.append({
type: 'AwaitBlock',
start,
Expand Down Expand Up @@ -252,7 +251,7 @@ function open(parser) {

parser.eat('}', true);

/** @type {ReturnType<typeof parser.append<AST.KeyBlock>>} */
/** @type {AST.KeyBlock} */
const block = parser.append({
type: 'KeyBlock',
start,
Expand Down Expand Up @@ -303,7 +302,7 @@ function open(parser) {
parser.allow_whitespace();
parser.eat('}', true);

/** @type {ReturnType<typeof parser.append<AST.SnippetBlock>>} */
/** @type {AST.SnippetBlock} */
const block = parser.append({
type: 'SnippetBlock',
start,
Expand Down Expand Up @@ -355,7 +354,7 @@ function next(parser) {
let elseif_start = start - 1;
while (parser.template[elseif_start] !== '{') elseif_start -= 1;

/** @type {ReturnType<typeof parser.append<AST.IfBlock>>} */
/** @type {AST.IfBlock} */
const child = parser.append({
start: elseif_start,
end: -1,
Expand Down Expand Up @@ -499,7 +498,6 @@ function special(parser) {
parser.allow_whitespace();
parser.eat('}', true);

/** @type {ReturnType<typeof parser.append<AST.HtmlTag>>} */
parser.append({
type: 'HtmlTag',
start,
Expand Down Expand Up @@ -537,7 +535,6 @@ function special(parser) {
parser.eat('}', true);
}

/** @type {ReturnType<typeof parser.append<AST.DebugTag>>} */
parser.append({
type: 'DebugTag',
start,
Expand Down Expand Up @@ -570,7 +567,6 @@ function special(parser) {

parser.eat('}', true);

/** @type {ReturnType<typeof parser.append<AST.ConstTag>>} */
parser.append({
type: 'ConstTag',
start,
Expand Down Expand Up @@ -601,15 +597,15 @@ function special(parser) {
parser.allow_whitespace();
parser.eat('}', true);

/** @type {ReturnType<typeof parser.append<AST.RenderTag>>} */
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: []
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/phases/1-parse/state/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function text(parser) {
data += parser.template[parser.index++];
}

/** @type {ReturnType<typeof parser.append<AST.Text>>} */
/** @type {AST.Text} */
parser.append({
type: 'Text',
start,
Expand Down
4 changes: 1 addition & 3 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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()
Expand Down Expand Up @@ -376,7 +374,6 @@ function build_class_directives(class_directives, class_attribute) {
type: 'Text',
start: -1,
end: -1,
parent: class_attribute,
data: ' ',
raw: ' '
});
Expand All @@ -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(' ')
Expand Down
1 change: 0 additions & 1 deletion packages/svelte/src/compiler/phases/3-transform/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ export function clean_nodes(
trimmed.push({
type: 'Comment',
data: '',
parent: first.parent,
start: -1,
end: -1
});
Expand Down
1 change: 0 additions & 1 deletion packages/svelte/src/compiler/phases/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export function create_attribute(name, start, end, value) {
end,
name,
value,
parent: null,
metadata: {
expression: create_expression_metadata(),
delegated: null
Expand Down
Loading

0 comments on commit 1a8aab0

Please sign in to comment.