UNPKG

react-magnetic-di

Version:
40 lines (33 loc) 1.48 kB
var _require = require('./utils'), assert = _require.assert, getComponentDeclaration = _require.getComponentDeclaration; function processReference(t, ref, isEnabled) { assert.isValidBlock(t, ref); assert.isValidCall(t, ref); // from the arguments of the method we generate the list of dependency identifiers var args = ref.container.arguments; var dependencyIdentifiers = args.map(function (v) { return t.identifier(v.name); }); var statement = ref.getStatementParent(); // if should not be enabled, just remove the statement and exit if (!isEnabled) { statement.remove(); return; } // generating variable declarations with array destructuring // assigning them the result of the method call, with arguments // now wrapped in an array ref.scope.push({ id: t.arrayPattern(dependencyIdentifiers), init: t.callExpression(ref.node, [t.arrayExpression(args), getComponentDeclaration(t, ref.scope) || t.nullLiteral()]) }); args.forEach(function (argIdentifier) { // for each argument we get the dependency variable name // then we rename it locally so we get a new unique identifier. // Then we manually revert just the argument identifier name back, // so it still points to the original dependency identifier name var name = argIdentifier.name; ref.scope.rename(name); argIdentifier.name = name; }); // remove the original statement statement.remove(); } module.exports = processReference;