use estree-walker

pull/5230/head
M. Habib Rosyad 5 years ago
parent 1a815326c4
commit 0beee4e492
No known key found for this signature in database
GPG Key ID: D5C899B22EB5EA25

@ -1,5 +1,7 @@
import { x } from 'code-red'; import { x } from 'code-red';
import { Node, Identifier } from 'estree'; import { Node, Identifier } from 'estree';
import { walk } from 'estree-walker';
import is_reference from 'is-reference';
export interface Context { export interface Context {
key: Identifier; key: Identifier;
@ -27,13 +29,7 @@ export function unpack_destructuring(contexts: Context[], node: Node, modifier:
} else if (element && element.type === 'AssignmentPattern') { } else if (element && element.type === 'AssignmentPattern') {
const n = contexts.length; const n = contexts.length;
unpack_destructuring(contexts, element.left, node => { unpack_destructuring(contexts, element.left, node => x`${modifier(node)}[${i}] !== undefined ? ${modifier(node)}[${i}] : ${update_reference(contexts, n, element.right, node)}` as Node);
const alternate = JSON.parse(JSON.stringify(element.right));
update_reference(contexts.slice(0, n), node, alternate);
return x`${modifier(node)}[${i}] !== undefined ? ${modifier(node)}[${i}] : ${alternate.replacement || alternate}` as Node;
});
} else { } else {
unpack_destructuring(contexts, element, node => x`${modifier(node)}[${i}]` as Node); unpack_destructuring(contexts, element, node => x`${modifier(node)}[${i}]` as Node);
} }
@ -56,13 +52,7 @@ export function unpack_destructuring(contexts: Context[], node: Node, modifier:
if (value.type === 'AssignmentPattern') { if (value.type === 'AssignmentPattern') {
const n = contexts.length; const n = contexts.length;
unpack_destructuring(contexts, value.left, node => { unpack_destructuring(contexts, value.left, node => x`${modifier(node)}.${key.name} !== undefined ? ${modifier(node)}.${key.name} : ${update_reference(contexts, n, value.right, node)}` as Node);
const alternate = JSON.parse(JSON.stringify(value.right));
update_reference(contexts.slice(0, n), node, alternate);
return x`${modifier(node)}.${key.name} !== undefined ? ${modifier(node)}.${key.name} : ${alternate.replacement || alternate}` as Node;
});
} else { } else {
unpack_destructuring(contexts, value, node => x`${modifier(node)}.${key.name}` as Node); unpack_destructuring(contexts, value, node => x`${modifier(node)}.${key.name}` as Node);
} }
@ -71,36 +61,36 @@ export function unpack_destructuring(contexts: Context[], node: Node, modifier:
} }
} }
function update_reference(contexts: Context[], node: Node, parent: any, property?: any) { function update_reference(contexts: Context[], n: number, info, replacement: Node): Node {
const current_node = (property !== undefined && parent[property] || parent); if (!n) return info;
if (!current_node || !contexts.length) return; let copy = JSON.parse(JSON.stringify(info)) as Node;
const replace = (node: Identifier, callback: (node: Node, context: any) => void, context?: any) => {
for (let i = 0; i < n; i++) {
const { key, modifier } = contexts[i];
if (current_node.type === 'Identifier') { if (node.name === key.name) {
contexts.forEach((context) => { callback(modifier(replacement), context);
const { key, modifier } = context; break;
}
if (current_node.name === key.name) { }
const replacement = modifier(node); };
if (property === undefined) { if (copy.type === 'Identifier') {
current_node.replacement = replacement; replace(copy, (node: Node, _context: any) => copy = node);
} else { } else {
parent[property] = replacement; walk(copy, {
enter(node, parent: Node) {
if (!['Identifier', 'BinaryExpression', 'CallExpression', 'MemberExpression'].includes(node.type)) {
return this.skip();
}
if (is_reference(node, parent)) {
replace(node as Identifier, (node: Node, context: any) => context && context.replace(node), this);
} }
} }
}); });
} else if (current_node.type === 'BinaryExpression') {
update_reference(contexts, node, current_node, 'left');
update_reference(contexts, node, current_node, 'right');
} else if (current_node.type === 'CallExpression') {
for (let i = 0; i < current_node.arguments.length; i += 1) {
update_reference(contexts, node, current_node.arguments, i);
} }
update_reference(contexts, node, current_node, 'callee'); return copy;
} else if (current_node.type === 'MemberExpression') {
update_reference(contexts, node, current_node, 'object');
update_reference(contexts, node, current_node, 'property');
}
} }

Loading…
Cancel
Save