Skip to content

Commit af46902

Browse files
committed
rollback
1 parent 11c7022 commit af46902

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed
Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { Expression, Pattern, Identifier } from 'estree' */
1+
/** @import { Expression, Pattern } from 'estree' */
22
/** @import { AST } from '#compiler' */
33
/** @import { ComponentContext } from '../types' */
44
import { dev } from '../../../../state.js';
@@ -12,29 +12,29 @@ import { get_value } from './shared/declarations.js';
1212
* @param {ComponentContext} context
1313
*/
1414
export function ConstTag(node, context) {
15-
/** @type {Identifier} */
16-
let id;
17-
1815
const declaration = node.declaration.declarations[0];
1916
// TODO we can almost certainly share some code with $derived(...)
2017
if (declaration.id.type === 'Identifier') {
21-
id = declaration.id;
2218
context.state.init.push(
2319
b.const(
24-
id,
20+
declaration.id,
2521
create_derived(
2622
context.state,
2723
b.thunk(/** @type {Expression} */ (context.visit(declaration.init)))
2824
)
2925
)
3026
);
3127

32-
context.state.transform[id.name] = { read: get_value };
28+
context.state.transform[declaration.id.name] = { read: get_value };
3329

34-
id = declaration.id;
30+
// we need to eagerly evaluate the expression in order to hit any
31+
// 'Cannot access x before initialization' errors
32+
if (dev) {
33+
context.state.init.push(b.stmt(b.call('$.get', declaration.id)));
34+
}
3535
} else {
3636
const identifiers = extract_identifiers(declaration.id);
37-
id = b.id(context.state.scope.generate('computed_const'));
37+
const tmp = b.id(context.state.scope.generate('computed_const'));
3838

3939
const transform = { ...context.state.transform };
4040

@@ -59,26 +59,18 @@ export function ConstTag(node, context) {
5959
])
6060
);
6161

62-
context.state.init.push(b.const(id, create_derived(context.state, fn)));
62+
context.state.init.push(b.const(tmp, create_derived(context.state, fn)));
63+
64+
// we need to eagerly evaluate the expression in order to hit any
65+
// 'Cannot access x before initialization' errors
66+
if (dev) {
67+
context.state.init.push(b.stmt(b.call('$.get', tmp)));
68+
}
6369

6470
for (const node of identifiers) {
6571
context.state.transform[node.name] = {
66-
read: (node) => b.member(b.call('$.get', id), node)
72+
read: (node) => b.member(b.call('$.get', tmp), node)
6773
};
6874
}
6975
}
70-
71-
// we need to eagerly evaluate the expression in order to hit any
72-
// 'Cannot access x before initialization' errors
73-
if (dev) {
74-
const parent = context.path.at(-1);
75-
const boundary = parent?.type === 'SvelteBoundary' ? parent : null;
76-
const statement = b.stmt(b.call('$.get', id));
77-
if (boundary) {
78-
boundary.const_dev_statements ||= [];
79-
boundary.const_dev_statements.push(statement);
80-
} else {
81-
context.state.init.push(statement);
82-
}
83-
}
8476
}

packages/svelte/types/index.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ declare module 'svelte/animate' {
622622
}
623623

624624
declare module 'svelte/compiler' {
625-
import type { Expression, Identifier, ArrayExpression, ArrowFunctionExpression, VariableDeclaration, VariableDeclarator, MemberExpression, Node, ObjectExpression, Pattern, Program, ChainExpression, SimpleCallExpression, SequenceExpression, ExpressionStatement } from 'estree';
625+
import type { Expression, Identifier, ArrayExpression, ArrowFunctionExpression, VariableDeclaration, VariableDeclarator, MemberExpression, Node, ObjectExpression, Pattern, Program, ChainExpression, SimpleCallExpression, SequenceExpression } from 'estree';
626626
import type { SourceMap } from 'magic-string';
627627
import type { Location } from 'locate-character';
628628
/**
@@ -1134,8 +1134,6 @@ declare module 'svelte/compiler' {
11341134
name: string;
11351135
attributes: Array<Attribute | SpreadAttribute | Directive>;
11361136
fragment: Fragment;
1137-
/** Id */
1138-
const_dev_statements?: ExpressionStatement[];
11391137
}
11401138

11411139
export interface Component extends BaseElement {

0 commit comments

Comments
 (0)