|
|
|
|
@ -15,9 +15,17 @@ import flatten_reference from '../../utils/flatten_reference.js';
|
|
|
|
|
* in_rest_element?: boolean;
|
|
|
|
|
* }}
|
|
|
|
|
*/
|
|
|
|
|
export function unpack_destructuring({ contexts, node, modifier = (node) => node, default_modifier = (node) => node, scope, component, context_rest_properties, in_rest_element = false }) {
|
|
|
|
|
if (!node)
|
|
|
|
|
return;
|
|
|
|
|
export function unpack_destructuring({
|
|
|
|
|
contexts,
|
|
|
|
|
node,
|
|
|
|
|
modifier = (node) => node,
|
|
|
|
|
default_modifier = (node) => node,
|
|
|
|
|
scope,
|
|
|
|
|
component,
|
|
|
|
|
context_rest_properties,
|
|
|
|
|
in_rest_element = false
|
|
|
|
|
}) {
|
|
|
|
|
if (!node) return;
|
|
|
|
|
if (node.type === 'Identifier') {
|
|
|
|
|
contexts.push({
|
|
|
|
|
type: 'DestructuredVariable',
|
|
|
|
|
@ -28,25 +36,23 @@ export function unpack_destructuring({ contexts, node, modifier = (node) => node
|
|
|
|
|
if (in_rest_element) {
|
|
|
|
|
context_rest_properties.set(node.name, node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (node.type === 'ArrayPattern') {
|
|
|
|
|
} else if (node.type === 'ArrayPattern') {
|
|
|
|
|
node.elements.forEach((element, i) => {
|
|
|
|
|
if (!element) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (element.type === 'RestElement') {
|
|
|
|
|
} else if (element.type === 'RestElement') {
|
|
|
|
|
unpack_destructuring({
|
|
|
|
|
contexts,
|
|
|
|
|
node: element.argument,
|
|
|
|
|
modifier: (node) => /** @type {import('estree').Node} */ (x `${modifier(node)}.slice(${i})`),
|
|
|
|
|
modifier: (node) =>
|
|
|
|
|
/** @type {import('estree').Node} */ (x`${modifier(node)}.slice(${i})`),
|
|
|
|
|
default_modifier,
|
|
|
|
|
scope,
|
|
|
|
|
component,
|
|
|
|
|
context_rest_properties,
|
|
|
|
|
in_rest_element: true
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if (element.type === 'AssignmentPattern') {
|
|
|
|
|
} else if (element.type === 'AssignmentPattern') {
|
|
|
|
|
const n = contexts.length;
|
|
|
|
|
mark_referenced(element.right, scope, component);
|
|
|
|
|
unpack_destructuring({
|
|
|
|
|
@ -54,14 +60,20 @@ export function unpack_destructuring({ contexts, node, modifier = (node) => node
|
|
|
|
|
node: element.left,
|
|
|
|
|
modifier: (node) => x`${modifier(node)}[${i}]`,
|
|
|
|
|
default_modifier: (node, to_ctx) =>
|
|
|
|
|
/** @type {import('estree').Node} */ (x `${node} !== undefined ? ${node} : ${update_reference(contexts, n, element.right, to_ctx)}`),
|
|
|
|
|
/** @type {import('estree').Node} */ (
|
|
|
|
|
x`${node} !== undefined ? ${node} : ${update_reference(
|
|
|
|
|
contexts,
|
|
|
|
|
n,
|
|
|
|
|
element.right,
|
|
|
|
|
to_ctx
|
|
|
|
|
)}`
|
|
|
|
|
),
|
|
|
|
|
scope,
|
|
|
|
|
component,
|
|
|
|
|
context_rest_properties,
|
|
|
|
|
in_rest_element
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
} else {
|
|
|
|
|
unpack_destructuring({
|
|
|
|
|
contexts,
|
|
|
|
|
node: element,
|
|
|
|
|
@ -74,8 +86,7 @@ export function unpack_destructuring({ contexts, node, modifier = (node) => node
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if (node.type === 'ObjectPattern') {
|
|
|
|
|
} else if (node.type === 'ObjectPattern') {
|
|
|
|
|
const used_properties = [];
|
|
|
|
|
node.properties.forEach((property) => {
|
|
|
|
|
if (property.type === 'RestElement') {
|
|
|
|
|
@ -83,15 +94,16 @@ export function unpack_destructuring({ contexts, node, modifier = (node) => node
|
|
|
|
|
contexts,
|
|
|
|
|
node: property.argument,
|
|
|
|
|
modifier: (node) =>
|
|
|
|
|
/** @type {import('estree').Node} */ (x `@object_without_properties(${modifier(node)}, [${used_properties}])`),
|
|
|
|
|
/** @type {import('estree').Node} */ (
|
|
|
|
|
x`@object_without_properties(${modifier(node)}, [${used_properties}])`
|
|
|
|
|
),
|
|
|
|
|
default_modifier,
|
|
|
|
|
scope,
|
|
|
|
|
component,
|
|
|
|
|
context_rest_properties,
|
|
|
|
|
in_rest_element: true
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if (property.type === 'Property') {
|
|
|
|
|
} else if (property.type === 'Property') {
|
|
|
|
|
const key = property.key;
|
|
|
|
|
const value = property.value;
|
|
|
|
|
|
|
|
|
|
@ -107,14 +119,12 @@ export function unpack_destructuring({ contexts, node, modifier = (node) => node
|
|
|
|
|
});
|
|
|
|
|
new_modifier = (node) => x`${modifier(node)}[${property_name}]`;
|
|
|
|
|
used_properties.push(x`${property_name}`);
|
|
|
|
|
}
|
|
|
|
|
else if (key.type === 'Identifier') {
|
|
|
|
|
} else if (key.type === 'Identifier') {
|
|
|
|
|
// e.g. { someProperty: ... }
|
|
|
|
|
const property_name = key.name;
|
|
|
|
|
new_modifier = (node) => x`${modifier(node)}.${property_name}`;
|
|
|
|
|
used_properties.push(x`"${property_name}"`);
|
|
|
|
|
}
|
|
|
|
|
else if (key.type === 'Literal') {
|
|
|
|
|
} else if (key.type === 'Literal') {
|
|
|
|
|
// e.g. { "property-in-quotes": ... } or { 14: ... }
|
|
|
|
|
const property_name = key.value;
|
|
|
|
|
new_modifier = (node) => x`${modifier(node)}["${property_name}"]`;
|
|
|
|
|
@ -129,14 +139,20 @@ export function unpack_destructuring({ contexts, node, modifier = (node) => node
|
|
|
|
|
node: value.left,
|
|
|
|
|
modifier: new_modifier,
|
|
|
|
|
default_modifier: (node, to_ctx) =>
|
|
|
|
|
/** @type {import('estree').Node} */ (x `${node} !== undefined ? ${node} : ${update_reference(contexts, n, value.right, to_ctx)}`),
|
|
|
|
|
/** @type {import('estree').Node} */ (
|
|
|
|
|
x`${node} !== undefined ? ${node} : ${update_reference(
|
|
|
|
|
contexts,
|
|
|
|
|
n,
|
|
|
|
|
value.right,
|
|
|
|
|
to_ctx
|
|
|
|
|
)}`
|
|
|
|
|
),
|
|
|
|
|
scope,
|
|
|
|
|
component,
|
|
|
|
|
context_rest_properties,
|
|
|
|
|
in_rest_element
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
} else {
|
|
|
|
|
// e.g. { property } or { property: newName }
|
|
|
|
|
unpack_destructuring({
|
|
|
|
|
contexts,
|
|
|
|
|
@ -162,13 +178,11 @@ export function unpack_destructuring({ contexts, node, modifier = (node) => node
|
|
|
|
|
* @returns {import("C:/repos/svelte/svelte/node_modules/.pnpm/@types+estree@1.0.0/node_modules/@types/estree/index").import('estree').Node}
|
|
|
|
|
*/
|
|
|
|
|
function update_reference(contexts, n, expression, to_ctx) {
|
|
|
|
|
|
|
|
|
|
/** @param {import('estree').Identifier} node */
|
|
|
|
|
const find_from_context = (node) => {
|
|
|
|
|
for (let i = n; i < contexts.length; i++) {
|
|
|
|
|
const cur_context = contexts[i];
|
|
|
|
|
if (cur_context.type !== 'DestructuredVariable')
|
|
|
|
|
continue;
|
|
|
|
|
if (cur_context.type !== 'DestructuredVariable') continue;
|
|
|
|
|
const { key } = cur_context;
|
|
|
|
|
if (node.name === key.name) {
|
|
|
|
|
throw new Error(`Cannot access '${node.name}' before initialization`);
|
|
|
|
|
@ -183,7 +197,12 @@ function update_reference(contexts, n, expression, to_ctx) {
|
|
|
|
|
expression = /** @type {import('estree').Expression} */ (clone(expression));
|
|
|
|
|
walk(expression, {
|
|
|
|
|
enter(node, parent) {
|
|
|
|
|
if (is_reference(/** @type {import('is-reference').NodeWithPropertyDefinition} */ (node), /** @type {import('is-reference').NodeWithPropertyDefinition} */ (parent))) {
|
|
|
|
|
if (
|
|
|
|
|
is_reference(
|
|
|
|
|
/** @type {import('is-reference').NodeWithPropertyDefinition} */ (node),
|
|
|
|
|
/** @type {import('is-reference').NodeWithPropertyDefinition} */ (parent)
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
this.replace(find_from_context(/** @type {import('estree').Identifier} */ (node)));
|
|
|
|
|
this.skip();
|
|
|
|
|
}
|
|
|
|
|
@ -210,7 +229,6 @@ function mark_referenced(node, scope, component) {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @typedef {DestructuredVariable | ComputedProperty} Context */
|
|
|
|
|
|
|
|
|
|
/** @typedef {Object} ComputedProperty
|
|
|
|
|
|