Skip to content

Commit 02e9f00

Browse files
committed
revert #15813
1 parent 2e27c5d commit 02e9f00

File tree

6 files changed

+74
-136
lines changed

6 files changed

+74
-136
lines changed

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

Lines changed: 32 additions & 37 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 { build_pattern, 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';
@@ -141,20 +141,20 @@ export function VariableDeclaration(node, context) {
141141
b.declarator(declarator.id, create_state_declarator(declarator.id, value))
142142
);
143143
} else {
144-
const [pattern, replacements] = build_pattern(declarator.id, context.state.scope);
144+
const tmp = context.state.scope.generate('tmp');
145+
const paths = extract_paths(declarator.id);
145146
declarations.push(
146-
b.declarator(pattern, value),
147-
.../** @type {[Identifier, Identifier][]} */ ([...replacements]).map(
148-
([original, replacement]) => {
149-
const binding = context.state.scope.get(original.name);
150-
return b.declarator(
151-
original,
152-
binding?.kind === 'state' || binding?.kind === 'raw_state'
153-
? create_state_declarator(binding.node, replacement)
154-
: replacement
155-
);
156-
}
157-
)
147+
b.declarator(b.id(tmp), value),
148+
...paths.map((path) => {
149+
const value = path.expression?.(b.id(tmp));
150+
const binding = context.state.scope.get(/** @type {Identifier} */ (path.node).name);
151+
return b.declarator(
152+
path.node,
153+
binding?.kind === 'state' || binding?.kind === 'raw_state'
154+
? create_state_declarator(binding.node, value)
155+
: value
156+
);
157+
})
158158
);
159159
}
160160

@@ -170,7 +170,8 @@ export function VariableDeclaration(node, context) {
170170
)
171171
);
172172
} else {
173-
const [pattern, replacements] = build_pattern(declarator.id, context.state.scope);
173+
const bindings = extract_paths(declarator.id);
174+
174175
const init = /** @type {CallExpression} */ (declarator.init);
175176

176177
/** @type {Identifier} */
@@ -188,16 +189,10 @@ export function VariableDeclaration(node, context) {
188189
);
189190
}
190191

191-
for (let i = 0; i < replacements.size; i++) {
192-
const [original, replacement] = [...replacements][i];
192+
for (let i = 0; i < bindings.length; i++) {
193+
const binding = bindings[i];
193194
declarations.push(
194-
b.declarator(
195-
original,
196-
b.call(
197-
'$.derived',
198-
b.arrow([], b.block([b.let(pattern, rhs), b.return(replacement)]))
199-
)
200-
)
195+
b.declarator(binding.node, b.call('$.derived', b.thunk(binding.expression(rhs))))
201196
);
202197
}
203198
}
@@ -309,19 +304,19 @@ function create_state_declarators(declarator, { scope, analysis }, value) {
309304
];
310305
}
311306

312-
const [pattern, replacements] = build_pattern(declarator.id, scope);
307+
const tmp = scope.generate('tmp');
308+
const paths = extract_paths(declarator.id);
313309
return [
314-
b.declarator(pattern, value),
315-
.../** @type {[Identifier, Identifier][]} */ ([...replacements]).map(
316-
([original, replacement]) => {
317-
const binding = scope.get(original.name);
318-
return b.declarator(
319-
original,
320-
binding?.kind === 'state'
321-
? b.call('$.mutable_source', replacement, analysis.immutable ? b.true : undefined)
322-
: replacement
323-
);
324-
}
325-
)
310+
b.declarator(b.id(tmp), value),
311+
...paths.map((path) => {
312+
const value = path.expression?.(b.id(tmp));
313+
const binding = scope.get(/** @type {Identifier} */ (path.node).name);
314+
return b.declarator(
315+
path.node,
316+
binding?.kind === 'state'
317+
? b.call('$.mutable_source', value, analysis.immutable ? b.true : undefined)
318+
: value
319+
);
320+
})
326321
];
327322
}

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

Lines changed: 8 additions & 5 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_pattern, build_fallback, extract_paths } 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';
@@ -188,10 +188,13 @@ function create_state_declarators(declarator, scope, value) {
188188
return [b.declarator(declarator.id, value)];
189189
}
190190

191-
const [pattern, replacements] = build_pattern(declarator.id, scope);
191+
const tmp = scope.generate('tmp');
192+
const paths = extract_paths(declarator.id);
192193
return [
193-
b.declarator(pattern, value),
194-
// TODO inject declarator for opts, so we can use it below
195-
...[...replacements].map(([original, replacement]) => b.declarator(original, replacement))
194+
b.declarator(b.id(tmp), value), // TODO inject declarator for opts, so we can use it below
195+
...paths.map((path) => {
196+
const value = path.expression?.(b.id(tmp));
197+
return b.declarator(path.node, value);
198+
})
196199
];
197200
}

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

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/** @import { AssignmentExpression, AssignmentOperator, Expression, Identifier, Node, Pattern } from 'estree' */
1+
/** @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 { build_pattern, 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,60 +23,47 @@ export function visit_assignment_expression(node, context, build_assignment) {
2323

2424
let changed = false;
2525

26-
const [pattern, replacements] = build_pattern(node.left, context.state.scope);
26+
const assignments = extract_paths(node.left).map((path) => {
27+
const value = path.expression?.(rhs);
2728

28-
const assignments = [
29-
b.let(pattern, rhs),
30-
...[...replacements].map(([original, replacement]) => {
31-
let assignment = build_assignment(node.operator, original, replacement, context);
32-
if (assignment !== null) changed = true;
33-
return b.stmt(
34-
assignment ??
35-
b.assignment(
36-
node.operator,
37-
/** @type {Identifier} */ (context.visit(original)),
38-
/** @type {Expression} */ (context.visit(replacement))
39-
)
40-
);
41-
})
42-
];
29+
let assignment = build_assignment('=', path.node, value, context);
30+
if (assignment !== null) changed = true;
31+
32+
return (
33+
assignment ??
34+
b.assignment(
35+
'=',
36+
/** @type {Pattern} */ (context.visit(path.node)),
37+
/** @type {Expression} */ (context.visit(value))
38+
)
39+
);
40+
});
4341

4442
if (!changed) {
4543
// No change to output -> nothing to transform -> we can keep the original assignment
4644
return null;
4745
}
4846

4947
const is_standalone = /** @type {Node} */ (context.path.at(-1)).type.endsWith('Statement');
50-
const block = b.block(assignments);
48+
const sequence = b.sequence(assignments);
5149

5250
if (!is_standalone) {
5351
// this is part of an expression, we need the sequence to end with the value
54-
block.body.push(b.return(rhs));
52+
sequence.expressions.push(rhs);
5553
}
5654

57-
if (is_standalone && !should_cache) {
58-
return block;
59-
}
55+
if (should_cache) {
56+
// the right hand side is a complex expression, wrap in an IIFE to cache it
57+
const iife = b.arrow([rhs], sequence);
6058

61-
const iife = b.arrow(should_cache ? [rhs] : [], block);
59+
const iife_is_async =
60+
is_expression_async(value) ||
61+
assignments.some((assignment) => is_expression_async(assignment));
6262

63-
const iife_is_async =
64-
is_expression_async(value) ||
65-
assignments.some(
66-
(assignment) =>
67-
(assignment.type === 'ExpressionStatement' &&
68-
is_expression_async(assignment.expression)) ||
69-
(assignment.type === 'VariableDeclaration' &&
70-
assignment.declarations.some(
71-
(declaration) =>
72-
is_expression_async(declaration.id) ||
73-
(declaration.init && is_expression_async(declaration.init))
74-
))
75-
);
63+
return iife_is_async ? b.await(b.call(b.async(iife), value)) : b.call(iife, value);
64+
}
7665

77-
return iife_is_async
78-
? b.await(b.call(b.async(iife), should_cache ? value : undefined))
79-
: b.call(iife, should_cache ? value : undefined);
66+
return sequence;
8067
}
8168

8269
if (node.left.type !== 'Identifier' && node.left.type !== 'MemberExpression') {

packages/svelte/src/compiler/phases/scope.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,10 +630,9 @@ export class Scope {
630630

631631
/**
632632
* @param {string} preferred_name
633-
* @param {(name: string, counter: number) => string} [generator]
634633
* @returns {string}
635634
*/
636-
generate(preferred_name, generator = (name, counter) => `${name}_${counter}`) {
635+
generate(preferred_name) {
637636
if (this.#porous) {
638637
return /** @type {Scope} */ (this.parent).generate(preferred_name);
639638
}
@@ -648,7 +647,7 @@ export class Scope {
648647
this.root.conflicts.has(name) ||
649648
is_reserved(name)
650649
) {
651-
name = generator(preferred_name, n++);
650+
name = `${preferred_name}_${n++}`;
652651
}
653652

654653
this.references.set(name, []);

packages/svelte/src/compiler/utils/ast.js

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
/** @import { AST, Scope } from '#compiler' */
1+
/** @import { AST } from '#compiler' */
22
/** @import * as ESTree from 'estree' */
33
import { walk } from 'zimmerframe';
44
import * as b from '#compiler/builders';
5-
import is_reference from 'is-reference';
65

76
/**
87
* Gets the left-most identifier of a member expression or identifier.
@@ -129,49 +128,6 @@ export function unwrap_pattern(pattern, nodes = []) {
129128
return nodes;
130129
}
131130

132-
/**
133-
* @param {ESTree.Pattern} id
134-
* @param {Scope} scope
135-
* @returns {[ESTree.Pattern, Map<ESTree.Identifier | ESTree.MemberExpression, ESTree.Identifier>]}
136-
*/
137-
export function build_pattern(id, scope) {
138-
/** @type {Map<ESTree.Identifier | ESTree.MemberExpression, ESTree.Identifier>} */
139-
const map = new Map();
140-
141-
/** @type {Map<string, string>} */
142-
const names = new Map();
143-
144-
let counter = 0;
145-
146-
for (const node of unwrap_pattern(id)) {
147-
const name = scope.generate(`$$${++counter}`, (_, counter) => `$$${counter}`);
148-
149-
map.set(node, b.id(name));
150-
151-
if (node.type === 'Identifier') {
152-
names.set(node.name, name);
153-
}
154-
}
155-
156-
const pattern = walk(id, null, {
157-
Identifier(node, context) {
158-
if (is_reference(node, /** @type {ESTree.Pattern} */ (context.path.at(-1)))) {
159-
const name = names.get(node.name);
160-
if (name) return b.id(name);
161-
}
162-
},
163-
164-
MemberExpression(node, context) {
165-
const n = map.get(node);
166-
if (n) return n;
167-
168-
context.next();
169-
}
170-
});
171-
172-
return [pattern, map];
173-
}
174-
175131
/**
176132
* Extracts all identifiers from a pattern.
177133
* @param {ESTree.Pattern} pattern

packages/svelte/tests/snapshot/samples/destructured-assignments/_expected/client/index.svelte.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ let c = 3;
77
let d = 4;
88

99
export function update(array) {
10-
{
11-
let [$$1, $$2] = array;
12-
13-
$.set(a, $$1, true);
14-
$.set(b, $$2, true);
15-
};
10+
(
11+
$.set(a, array[0], true),
12+
$.set(b, array[1], true)
13+
);
1614

1715
[c, d] = array;
1816
}

0 commit comments

Comments
 (0)