in src/template.js [37:94]
visitIdentifier: function(path) {
this.traverse(path);
const node = path.node;
const parent = path.parent.node;
// If this identifier is not one of our generated ones, do nothing
const varIndex = varNames.indexOf(node.name);
if (varIndex === -1) {
return;
}
let replacement = nodes[varIndex];
nodes[varIndex] = null;
// If the replacement is an array, we need to explode the nodes in context
if (Array.isArray(replacement)) {
if (types.Function.check(parent) &&
parent.params.indexOf(node) > -1) {
// Function parameters: function foo(${bar}) {}
splice(parent.params, node, replacement);
} else if (types.VariableDeclarator.check(parent)) {
// Variable declarations: var foo = ${bar}, baz = 42;
splice(
path.parent.parent.node.declarations,
parent,
replacement
);
} else if (types.ArrayExpression.check(parent)) {
// Arrays: var foo = [${bar}, baz];
splice(parent.elements, node, replacement);
} else if (types.Property.check(parent) && parent.shorthand) {
// Objects: var foo = {${bar}, baz: 42};
splice(
path.parent.parent.node.properties,
parent,
replacement
);
} else if (types.CallExpression.check(parent) &&
parent.arguments.indexOf(node) > -1) {
// Function call arguments: foo(${bar}, baz)
splice(parent.arguments, node, replacement);
} else if (types.ExpressionStatement.check(parent)) {
// Generic sequence of statements: { ${foo}; bar; }
path.parent.replace.apply(
path.parent,
replacement.map(ensureStatement)
);
} else {
// Every else, let recast take care of it
path.replace.apply(path, replacement);
}
} else if (types.ExpressionStatement.check(parent)) {
path.parent.replace(ensureStatement(replacement));
} else {
path.replace(replacement);
}
}