Skip to content

Commit d2e14e3

Browse files
committed
WIP
1 parent df9e875 commit d2e14e3

File tree

6 files changed

+23
-5
lines changed

6 files changed

+23
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export function Identifier(node, context) {
9090
if (binding) {
9191
if (context.state.expression) {
9292
context.state.expression.dependencies.add(binding);
93+
context.state.expression.references.add(binding);
9394
context.state.expression.has_state ||=
9495
binding.kind !== 'static' &&
9596
!binding.is_function() &&

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ export function visit_function(node, context) {
1313
scope: context.state.scope
1414
};
1515

16+
if (context.state.expression) {
17+
for (const [name] of context.state.scope.references) {
18+
const binding = context.state.scope.get(name);
19+
20+
if (binding) {
21+
context.state.expression.references.add(binding);
22+
}
23+
}
24+
}
25+
1626
context.next({
1727
...context.state,
1828
function_depth: context.state.function_depth + 1,

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { extract_identifiers } from '../../../../utils/ast.js';
66
import * as b from '#compiler/builders';
77
import { create_derived } from '../utils.js';
88
import { get_value } from './shared/declarations.js';
9-
import { build_legacy_expression } from './shared/utils.js';
9+
import { build_legacy_expression, build_legacy_expression_2 } from './shared/utils.js';
1010

1111
/**
1212
* @param {AST.ConstTag} node
@@ -18,7 +18,7 @@ export function ConstTag(node, context) {
1818
if (declaration.id.type === 'Identifier') {
1919
const init = context.state.analysis.runes
2020
? /** @type {Expression} */ (context.visit(declaration.init))
21-
: build_legacy_expression(declaration.init, context);
21+
: build_legacy_expression_2(context, declaration.init, node.metadata.expression);
2222
context.state.init.push(b.const(declaration.id, create_derived(context.state, b.thunk(init))));
2323

2424
context.state.transform[declaration.id.name] = { read: get_value };
@@ -46,7 +46,11 @@ export function ConstTag(node, context) {
4646
// instead of destructuring it only to return a new object
4747
const init = context.state.analysis.runes
4848
? /** @type {Expression} */ (context.visit(declaration.init, child_state))
49-
: build_legacy_expression(declaration.init, { ...context, state: child_state });
49+
: build_legacy_expression_2(
50+
{ ...context, state: child_state },
51+
declaration.init,
52+
node.metadata.expression
53+
);
5054
const fn = b.arrow(
5155
[],
5256
b.block([

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ export function build_legacy_expression_2(context, expression, metadata) {
450450

451451
const sequence = b.sequence([]);
452452

453-
for (const binding of metadata.dependencies) {
453+
for (const binding of metadata.references) {
454454
if (binding.kind === 'normal') {
455455
continue;
456456
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export function create_attribute(name, start, end, value) {
6262
export function create_expression_metadata() {
6363
return {
6464
dependencies: new Set(),
65+
references: new Set(),
6566
has_state: false,
6667
has_call: false,
6768
has_member_expression: false,

packages/svelte/src/compiler/types/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,10 @@ export type DeclarationKind =
279279
| 'synthetic';
280280

281281
export interface ExpressionMetadata {
282-
/** All the bindings that are referenced inside this expression */
282+
/** All the bindings that are referenced eagerly (not inside functions) in this expression */
283283
dependencies: Set<Binding>;
284+
/** All the bindings that are referenced inside this expression, including inside functions */
285+
references: Set<Binding>;
284286
/** True if the expression references state directly, or _might_ (via member/call expressions) */
285287
has_state: boolean;
286288
/** True if the expression involves a call expression (often, it will need to be wrapped in a derived) */

0 commit comments

Comments
 (0)