|
|
@ -2,7 +2,7 @@
|
|
|
|
/** @import { Binding } from '#compiler' */
|
|
|
|
/** @import { Binding } from '#compiler' */
|
|
|
|
/** @import { ComponentClientTransformState, ComponentContext } from '../types' */
|
|
|
|
/** @import { ComponentClientTransformState, ComponentContext } from '../types' */
|
|
|
|
import { dev } from '../../../../state.js';
|
|
|
|
import { dev } from '../../../../state.js';
|
|
|
|
import { build_pattern, extract_paths } from '../../../../utils/ast.js';
|
|
|
|
import { extract_paths } from '../../../../utils/ast.js';
|
|
|
|
import * as b from '#compiler/builders';
|
|
|
|
import * as b from '#compiler/builders';
|
|
|
|
import * as assert from '../../../../utils/assert.js';
|
|
|
|
import * as assert from '../../../../utils/assert.js';
|
|
|
|
import { get_rune } from '../../../scope.js';
|
|
|
|
import { get_rune } from '../../../scope.js';
|
|
|
@ -141,20 +141,20 @@ export function VariableDeclaration(node, context) {
|
|
|
|
b.declarator(declarator.id, create_state_declarator(declarator.id, value))
|
|
|
|
b.declarator(declarator.id, create_state_declarator(declarator.id, value))
|
|
|
|
);
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
const [pattern, replacements] = build_pattern(declarator.id, context.state.scope);
|
|
|
|
const tmp = context.state.scope.generate('tmp');
|
|
|
|
|
|
|
|
const paths = extract_paths(declarator.id);
|
|
|
|
declarations.push(
|
|
|
|
declarations.push(
|
|
|
|
b.declarator(pattern, value),
|
|
|
|
b.declarator(b.id(tmp), value),
|
|
|
|
.../** @type {[Identifier, Identifier][]} */ ([...replacements]).map(
|
|
|
|
...paths.map((path) => {
|
|
|
|
([original, replacement]) => {
|
|
|
|
const value = path.expression?.(b.id(tmp));
|
|
|
|
const binding = context.state.scope.get(original.name);
|
|
|
|
const binding = context.state.scope.get(/** @type {Identifier} */ (path.node).name);
|
|
|
|
return b.declarator(
|
|
|
|
return b.declarator(
|
|
|
|
original,
|
|
|
|
path.node,
|
|
|
|
binding?.kind === 'state' || binding?.kind === 'raw_state'
|
|
|
|
binding?.kind === 'state' || binding?.kind === 'raw_state'
|
|
|
|
? create_state_declarator(binding.node, replacement)
|
|
|
|
? create_state_declarator(binding.node, value)
|
|
|
|
: replacement
|
|
|
|
: value
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -170,7 +170,8 @@ export function VariableDeclaration(node, context) {
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
const [pattern, replacements] = build_pattern(declarator.id, context.state.scope);
|
|
|
|
const bindings = extract_paths(declarator.id);
|
|
|
|
|
|
|
|
|
|
|
|
const init = /** @type {CallExpression} */ (declarator.init);
|
|
|
|
const init = /** @type {CallExpression} */ (declarator.init);
|
|
|
|
|
|
|
|
|
|
|
|
/** @type {Identifier} */
|
|
|
|
/** @type {Identifier} */
|
|
|
@ -188,16 +189,10 @@ export function VariableDeclaration(node, context) {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < replacements.size; i++) {
|
|
|
|
for (let i = 0; i < bindings.length; i++) {
|
|
|
|
const [original, replacement] = [...replacements][i];
|
|
|
|
const binding = bindings[i];
|
|
|
|
declarations.push(
|
|
|
|
declarations.push(
|
|
|
|
b.declarator(
|
|
|
|
b.declarator(binding.node, b.call('$.derived', b.thunk(binding.expression(rhs))))
|
|
|
|
original,
|
|
|
|
|
|
|
|
b.call(
|
|
|
|
|
|
|
|
'$.derived',
|
|
|
|
|
|
|
|
b.arrow([], b.block([b.let(pattern, rhs), b.return(replacement)]))
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -309,19 +304,19 @@ function create_state_declarators(declarator, { scope, analysis }, value) {
|
|
|
|
];
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const [pattern, replacements] = build_pattern(declarator.id, scope);
|
|
|
|
const tmp = scope.generate('tmp');
|
|
|
|
|
|
|
|
const paths = extract_paths(declarator.id);
|
|
|
|
return [
|
|
|
|
return [
|
|
|
|
b.declarator(pattern, value),
|
|
|
|
b.declarator(b.id(tmp), value),
|
|
|
|
.../** @type {[Identifier, Identifier][]} */ ([...replacements]).map(
|
|
|
|
...paths.map((path) => {
|
|
|
|
([original, replacement]) => {
|
|
|
|
const value = path.expression?.(b.id(tmp));
|
|
|
|
const binding = scope.get(original.name);
|
|
|
|
const binding = scope.get(/** @type {Identifier} */ (path.node).name);
|
|
|
|
return b.declarator(
|
|
|
|
return b.declarator(
|
|
|
|
original,
|
|
|
|
path.node,
|
|
|
|
binding?.kind === 'state'
|
|
|
|
binding?.kind === 'state'
|
|
|
|
? b.call('$.mutable_source', replacement, analysis.immutable ? b.true : undefined)
|
|
|
|
? b.call('$.mutable_source', value, analysis.immutable ? b.true : undefined)
|
|
|
|
: replacement
|
|
|
|
: value
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
];
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|