pull/16015/head
Rich Harris 4 months ago
parent bda21bf2cc
commit 8e021c8147

@ -19,7 +19,7 @@ export function VariableDeclarator(node, context) {
if (context.state.analysis.runes) { if (context.state.analysis.runes) {
const init = node.init; const init = node.init;
const rune = get_rune(init, context.state.scope); const rune = get_rune(init, context.state.scope);
const paths = extract_paths(node.id, b.id('dummy')); const { paths } = extract_paths(node.id, b.id('dummy'));
for (const path of paths) { for (const path of paths) {
validate_identifier_name(context.state.scope.get(/** @type {Identifier} */ (path.node).name)); validate_identifier_name(context.state.scope.get(/** @type {Identifier} */ (path.node).name));

@ -234,7 +234,9 @@ export function EachBlock(node, context) {
} else if (node.context) { } else if (node.context) {
const unwrapped = (flags & EACH_ITEM_REACTIVE) !== 0 ? b.call('$.get', item) : item; const unwrapped = (flags & EACH_ITEM_REACTIVE) !== 0 ? b.call('$.get', item) : item;
for (const path of extract_paths(node.context, unwrapped)) { const { paths } = extract_paths(node.context, unwrapped);
for (const path of paths) {
const name = /** @type {Identifier} */ (path.node).name; const name = /** @type {Identifier} */ (path.node).name;
const needs_derived = path.has_default_value; // to ensure that default value is only called once const needs_derived = path.has_default_value; // to ensure that default value is only called once

@ -43,7 +43,7 @@ export function SnippetBlock(node, context) {
let arg_alias = `$$arg${i}`; let arg_alias = `$$arg${i}`;
args.push(b.id(arg_alias)); args.push(b.id(arg_alias));
const paths = extract_paths(argument, b.maybe_call(b.id(arg_alias))); const { paths } = extract_paths(argument, b.maybe_call(b.id(arg_alias)));
for (const path of paths) { for (const path of paths) {
const name = /** @type {Identifier} */ (path.node).name; const name = /** @type {Identifier} */ (path.node).name;

@ -142,7 +142,7 @@ export function VariableDeclaration(node, context) {
); );
} else { } else {
const tmp = b.id(context.state.scope.generate('tmp')); const tmp = b.id(context.state.scope.generate('tmp'));
const paths = extract_paths(declarator.id, tmp); const { paths } = extract_paths(declarator.id, tmp);
declarations.push( declarations.push(
b.declarator(tmp, value), b.declarator(tmp, value),
...paths.map((path) => { ...paths.map((path) => {
@ -183,7 +183,9 @@ export function VariableDeclaration(node, context) {
); );
} }
for (const path of extract_paths(declarator.id, rhs)) { const { paths } = extract_paths(declarator.id, rhs);
for (const path of paths) {
declarations.push( declarations.push(
b.declarator(path.node, b.call('$.derived', b.thunk(path.expression))) b.declarator(path.node, b.call('$.derived', b.thunk(path.expression)))
); );
@ -218,7 +220,7 @@ export function VariableDeclaration(node, context) {
// Turn export let into props. It's really really weird because export let { x: foo, z: [bar]} = .. // Turn export let into props. It's really really weird because export let { x: foo, z: [bar]} = ..
// means that foo and bar are the props (i.e. the leafs are the prop names), not x and z. // means that foo and bar are the props (i.e. the leafs are the prop names), not x and z.
const tmp = b.id(context.state.scope.generate('tmp')); const tmp = b.id(context.state.scope.generate('tmp'));
const paths = extract_paths(declarator.id, tmp); const { paths } = extract_paths(declarator.id, tmp);
declarations.push( declarations.push(
b.declarator( b.declarator(
@ -298,7 +300,7 @@ function create_state_declarators(declarator, { scope, analysis }, value) {
} }
const tmp = b.id(scope.generate('tmp')); const tmp = b.id(scope.generate('tmp'));
const paths = extract_paths(declarator.id, tmp); const { paths } = extract_paths(declarator.id, tmp);
return [ return [
b.declarator(tmp, value), b.declarator(tmp, value),
...paths.map((path) => { ...paths.map((path) => {

@ -121,7 +121,7 @@ export function VariableDeclaration(node, context) {
// Turn export let into props. It's really really weird because export let { x: foo, z: [bar]} = .. // Turn export let into props. It's really really weird because export let { x: foo, z: [bar]} = ..
// means that foo and bar are the props (i.e. the leafs are the prop names), not x and z. // means that foo and bar are the props (i.e. the leafs are the prop names), not x and z.
const tmp = b.id(context.state.scope.generate('tmp')); const tmp = b.id(context.state.scope.generate('tmp'));
const paths = extract_paths(declarator.id, tmp); const { paths } = extract_paths(declarator.id, tmp);
declarations.push( declarations.push(
b.declarator( b.declarator(
tmp, tmp,
@ -189,7 +189,7 @@ function create_state_declarators(declarator, scope, value) {
} }
const tmp = b.id(scope.generate('tmp')); const tmp = b.id(scope.generate('tmp'));
const paths = extract_paths(declarator.id, tmp); const { paths } = extract_paths(declarator.id, tmp);
return [ return [
b.declarator(tmp, value), // TODO inject declarator for opts, so we can use it below b.declarator(tmp, value), // TODO inject declarator for opts, so we can use it below
...paths.map((path) => { ...paths.map((path) => {

@ -23,7 +23,9 @@ export function visit_assignment_expression(node, context, build_assignment) {
let changed = false; let changed = false;
const assignments = extract_paths(node.left, rhs).map((path) => { const { paths } = extract_paths(node.left, rhs);
const assignments = paths.map((path) => {
const value = path.expression; const value = path.expression;
let assignment = build_assignment('=', path.node, value, context); let assignment = build_assignment('=', path.node, value, context);

@ -237,10 +237,15 @@ export function extract_identifiers_from_destructuring(node, nodes = []) {
* Extracts all destructured assignments from a pattern. * Extracts all destructured assignments from a pattern.
* @param {ESTree.Node} param * @param {ESTree.Node} param
* @param {ESTree.Expression} initial * @param {ESTree.Expression} initial
* @returns {DestructuredAssignment[]} * @returns {{ paths: DestructuredAssignment[] }}
*/ */
export function extract_paths(param, initial) { export function extract_paths(param, initial) {
return _extract_paths([], param, initial, initial, false); /** @type {DestructuredAssignment[]} */
const paths = [];
_extract_paths(paths, param, initial, initial, false);
return { paths };
} }
/** /**

Loading…
Cancel
Save