Skip to content

Commit bda21bf

Browse files
committed
WIP
1 parent bed2f0c commit bda21bf

File tree

8 files changed

+31
-207
lines changed

8 files changed

+31
-207
lines changed

packages/svelte/src/compiler/migrate/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ const instance_script = {
604604
// Turn export let into props. It's really really weird because export let { x: foo, z: [bar]} = ..
605605
// means that foo and bar are the props (i.e. the leafs are the prop names), not x and z.
606606
// const tmp = b.id(state.scope.generate('tmp'));
607-
// const paths = destructure(declarator.id, tmp);
607+
// const paths = extract_paths(declarator.id, tmp);
608608
// state.props_pre.push(
609609
// b.declaration('const', tmp, visit(declarator.init!) as Expression)
610610
// );

packages/svelte/src/compiler/phases/2-analyze/visitors/VariableDeclarator.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as e from '../../../errors.js';
77
import * as w from '../../../warnings.js';
88
import { extract_paths } from '../../../utils/ast.js';
99
import { equal } from '../../../utils/assert.js';
10+
import * as b from '#compiler/builders';
1011

1112
/**
1213
* @param {VariableDeclarator} node
@@ -18,7 +19,7 @@ export function VariableDeclarator(node, context) {
1819
if (context.state.analysis.runes) {
1920
const init = node.init;
2021
const rune = get_rune(init, context.state.scope);
21-
const paths = extract_paths(node.id);
22+
const paths = extract_paths(node.id, b.id('dummy'));
2223

2324
for (const path of paths) {
2425
validate_identifier_name(context.state.scope.get(/** @type {Identifier} */ (path.node).name));

packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
EACH_ITEM_REACTIVE
1111
} from '../../../../../constants.js';
1212
import { dev } from '../../../../state.js';
13-
import { destructure, object } from '../../../../utils/ast.js';
13+
import { extract_paths, object } from '../../../../utils/ast.js';
1414
import * as b from '#compiler/builders';
1515
import { build_getter } from '../utils.js';
1616
import { get_value } from './shared/declarations.js';
@@ -234,7 +234,7 @@ export function EachBlock(node, context) {
234234
} else if (node.context) {
235235
const unwrapped = (flags & EACH_ITEM_REACTIVE) !== 0 ? b.call('$.get', item) : item;
236236

237-
for (const path of destructure(node.context, unwrapped)) {
237+
for (const path of extract_paths(node.context, unwrapped)) {
238238
const name = /** @type {Identifier} */ (path.node).name;
239239
const needs_derived = path.has_default_value; // to ensure that default value is only called once
240240

packages/svelte/src/compiler/phases/3-transform/client/visitors/SnippetBlock.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/** @import { AST } from '#compiler' */
33
/** @import { ComponentContext } from '../types' */
44
import { dev } from '../../../../state.js';
5-
import { destructure } from '../../../../utils/ast.js';
5+
import { extract_paths } from '../../../../utils/ast.js';
66
import * as b from '#compiler/builders';
77
import { get_value } from './shared/declarations.js';
88

@@ -43,7 +43,7 @@ export function SnippetBlock(node, context) {
4343
let arg_alias = `$$arg${i}`;
4444
args.push(b.id(arg_alias));
4545

46-
const paths = destructure(argument, b.maybe_call(b.id(arg_alias)));
46+
const paths = extract_paths(argument, b.maybe_call(b.id(arg_alias)));
4747

4848
for (const path of paths) {
4949
const name = /** @type {Identifier} */ (path.node).name;

packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/** @import { Binding } from '#compiler' */
33
/** @import { ComponentClientTransformState, ComponentContext } from '../types' */
44
import { dev } from '../../../../state.js';
5-
import { destructure, extract_paths } from '../../../../utils/ast.js';
5+
import { extract_paths } from '../../../../utils/ast.js';
66
import * as b from '#compiler/builders';
77
import * as assert from '../../../../utils/assert.js';
88
import { get_rune } from '../../../scope.js';
@@ -142,7 +142,7 @@ export function VariableDeclaration(node, context) {
142142
);
143143
} else {
144144
const tmp = b.id(context.state.scope.generate('tmp'));
145-
const paths = destructure(declarator.id, tmp);
145+
const paths = extract_paths(declarator.id, tmp);
146146
declarations.push(
147147
b.declarator(tmp, value),
148148
...paths.map((path) => {
@@ -170,29 +170,22 @@ export function VariableDeclaration(node, context) {
170170
)
171171
);
172172
} else {
173-
const paths = extract_paths(declarator.id);
174-
175173
const init = /** @type {CallExpression} */ (declarator.init);
176174

177-
/** @type {Identifier} */
178-
let id;
179175
let rhs = value;
180176

181-
if (rune === '$derived' && init.arguments[0].type === 'Identifier') {
182-
id = init.arguments[0];
183-
} else {
184-
id = b.id(context.state.scope.generate('$$d'));
177+
if (rune !== '$derived' || init.arguments[0].type !== 'Identifier') {
178+
const id = b.id(context.state.scope.generate('$$d'));
185179
rhs = b.call('$.get', id);
186180

187181
declarations.push(
188182
b.declarator(id, b.call('$.derived', rune === '$derived.by' ? value : b.thunk(value)))
189183
);
190184
}
191185

192-
for (let i = 0; i < paths.length; i++) {
193-
const path = paths[i];
186+
for (const path of extract_paths(declarator.id, rhs)) {
194187
declarations.push(
195-
b.declarator(path.node, b.call('$.derived', b.thunk(path.expression(rhs))))
188+
b.declarator(path.node, b.call('$.derived', b.thunk(path.expression)))
196189
);
197190
}
198191
}
@@ -225,7 +218,7 @@ export function VariableDeclaration(node, context) {
225218
// Turn export let into props. It's really really weird because export let { x: foo, z: [bar]} = ..
226219
// means that foo and bar are the props (i.e. the leafs are the prop names), not x and z.
227220
const tmp = b.id(context.state.scope.generate('tmp'));
228-
const paths = destructure(declarator.id, tmp);
221+
const paths = extract_paths(declarator.id, tmp);
229222

230223
declarations.push(
231224
b.declarator(
@@ -305,7 +298,7 @@ function create_state_declarators(declarator, { scope, analysis }, value) {
305298
}
306299

307300
const tmp = b.id(scope.generate('tmp'));
308-
const paths = destructure(declarator.id, tmp);
301+
const paths = extract_paths(declarator.id, tmp);
309302
return [
310303
b.declarator(tmp, value),
311304
...paths.map((path) => {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/** @import { Context } from '../types.js' */
44
/** @import { ComponentAnalysis } from '../../../types.js' */
55
/** @import { Scope } from '../../../scope.js' */
6-
import { build_fallback, destructure } from '../../../../utils/ast.js';
6+
import { build_fallback, extract_paths } from '../../../../utils/ast.js';
77
import * as b from '#compiler/builders';
88
import { get_rune } from '../../../scope.js';
99
import { walk } from 'zimmerframe';
@@ -121,7 +121,7 @@ export function VariableDeclaration(node, context) {
121121
// Turn export let into props. It's really really weird because export let { x: foo, z: [bar]} = ..
122122
// means that foo and bar are the props (i.e. the leafs are the prop names), not x and z.
123123
const tmp = b.id(context.state.scope.generate('tmp'));
124-
const paths = destructure(declarator.id, tmp);
124+
const paths = extract_paths(declarator.id, tmp);
125125
declarations.push(
126126
b.declarator(
127127
tmp,
@@ -189,7 +189,7 @@ function create_state_declarators(declarator, scope, value) {
189189
}
190190

191191
const tmp = b.id(scope.generate('tmp'));
192-
const paths = destructure(declarator.id, tmp);
192+
const paths = extract_paths(declarator.id, tmp);
193193
return [
194194
b.declarator(tmp, value), // TODO inject declarator for opts, so we can use it below
195195
...paths.map((path) => {

packages/svelte/src/compiler/phases/3-transform/shared/assignments.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @import { AssignmentExpression, AssignmentOperator, Expression, Node, Pattern } from 'estree' */
22
/** @import { Context as ClientContext } from '../client/types.js' */
33
/** @import { Context as ServerContext } from '../server/types.js' */
4-
import { destructure, is_expression_async } from '../../../utils/ast.js';
4+
import { extract_paths, is_expression_async } from '../../../utils/ast.js';
55
import * as b from '#compiler/builders';
66

77
/**
@@ -23,7 +23,7 @@ export function visit_assignment_expression(node, context, build_assignment) {
2323

2424
let changed = false;
2525

26-
const assignments = destructure(node.left, rhs).map((path) => {
26+
const assignments = extract_paths(node.left, rhs).map((path) => {
2727
const value = path.expression;
2828

2929
let assignment = build_assignment('=', path.node, value, context);

0 commit comments

Comments
 (0)