pull/16100/head
Rich Harris 4 months ago
parent df9e8758b0
commit d2e14e328b

@ -90,6 +90,7 @@ export function Identifier(node, context) {
if (binding) { if (binding) {
if (context.state.expression) { if (context.state.expression) {
context.state.expression.dependencies.add(binding); context.state.expression.dependencies.add(binding);
context.state.expression.references.add(binding);
context.state.expression.has_state ||= context.state.expression.has_state ||=
binding.kind !== 'static' && binding.kind !== 'static' &&
!binding.is_function() && !binding.is_function() &&

@ -13,6 +13,16 @@ export function visit_function(node, context) {
scope: context.state.scope scope: context.state.scope
}; };
if (context.state.expression) {
for (const [name] of context.state.scope.references) {
const binding = context.state.scope.get(name);
if (binding) {
context.state.expression.references.add(binding);
}
}
}
context.next({ context.next({
...context.state, ...context.state,
function_depth: context.state.function_depth + 1, function_depth: context.state.function_depth + 1,

@ -6,7 +6,7 @@ import { extract_identifiers } from '../../../../utils/ast.js';
import * as b from '#compiler/builders'; import * as b from '#compiler/builders';
import { create_derived } from '../utils.js'; import { create_derived } from '../utils.js';
import { get_value } from './shared/declarations.js'; import { get_value } from './shared/declarations.js';
import { build_legacy_expression } from './shared/utils.js'; import { build_legacy_expression, build_legacy_expression_2 } from './shared/utils.js';
/** /**
* @param {AST.ConstTag} node * @param {AST.ConstTag} node
@ -18,7 +18,7 @@ export function ConstTag(node, context) {
if (declaration.id.type === 'Identifier') { if (declaration.id.type === 'Identifier') {
const init = context.state.analysis.runes const init = context.state.analysis.runes
? /** @type {Expression} */ (context.visit(declaration.init)) ? /** @type {Expression} */ (context.visit(declaration.init))
: build_legacy_expression(declaration.init, context); : build_legacy_expression_2(context, declaration.init, node.metadata.expression);
context.state.init.push(b.const(declaration.id, create_derived(context.state, b.thunk(init)))); context.state.init.push(b.const(declaration.id, create_derived(context.state, b.thunk(init))));
context.state.transform[declaration.id.name] = { read: get_value }; context.state.transform[declaration.id.name] = { read: get_value };
@ -46,7 +46,11 @@ export function ConstTag(node, context) {
// instead of destructuring it only to return a new object // instead of destructuring it only to return a new object
const init = context.state.analysis.runes const init = context.state.analysis.runes
? /** @type {Expression} */ (context.visit(declaration.init, child_state)) ? /** @type {Expression} */ (context.visit(declaration.init, child_state))
: build_legacy_expression(declaration.init, { ...context, state: child_state }); : build_legacy_expression_2(
{ ...context, state: child_state },
declaration.init,
node.metadata.expression
);
const fn = b.arrow( const fn = b.arrow(
[], [],
b.block([ b.block([

@ -450,7 +450,7 @@ export function build_legacy_expression_2(context, expression, metadata) {
const sequence = b.sequence([]); const sequence = b.sequence([]);
for (const binding of metadata.dependencies) { for (const binding of metadata.references) {
if (binding.kind === 'normal') { if (binding.kind === 'normal') {
continue; continue;
} }

@ -62,6 +62,7 @@ export function create_attribute(name, start, end, value) {
export function create_expression_metadata() { export function create_expression_metadata() {
return { return {
dependencies: new Set(), dependencies: new Set(),
references: new Set(),
has_state: false, has_state: false,
has_call: false, has_call: false,
has_member_expression: false, has_member_expression: false,

@ -279,8 +279,10 @@ export type DeclarationKind =
| 'synthetic'; | 'synthetic';
export interface ExpressionMetadata { export interface ExpressionMetadata {
/** All the bindings that are referenced inside this expression */ /** All the bindings that are referenced eagerly (not inside functions) in this expression */
dependencies: Set<Binding>; dependencies: Set<Binding>;
/** All the bindings that are referenced inside this expression, including inside functions */
references: Set<Binding>;
/** True if the expression references state directly, or _might_ (via member/call expressions) */ /** True if the expression references state directly, or _might_ (via member/call expressions) */
has_state: boolean; has_state: boolean;
/** True if the expression involves a call expression (often, it will need to be wrapped in a derived) */ /** True if the expression involves a call expression (often, it will need to be wrapped in a derived) */

Loading…
Cancel
Save