babel-plugin-transform-currency-operators
Advanced tools
Comparing version 1.0.0-beta.1 to 1.0.0-beta.2
{ | ||
"name": "babel-plugin-transform-currency-operators", | ||
"version": "1.0.0-beta.1", | ||
"version": "1.0.0-beta.2", | ||
"homepage": "An experimental babel plugin for transforming currency.js operators", | ||
@@ -5,0 +5,0 @@ "description": "https://github.com/scurker/babel-plugin-transform-currency-operators", |
@@ -50,8 +50,8 @@ const arithmeticOperators = { | ||
return t.isIdentifier(node) && identifiers.includes(node.name); | ||
return t.isIdentifier(node) && identifiers.has(node.name); | ||
} | ||
function expressionContainsCurrency(path, methodName) { | ||
let { node } = path | ||
, currencyVariables = [methodName]; | ||
function pathContainsCurrency(refPath, methodName) { | ||
let { node } = refPath | ||
, currencyVariables = new Set([methodName]); | ||
@@ -63,11 +63,17 @@ const Visitors = { | ||
node.body.body.find(n => t.isReturnStatement(n)), | ||
[methodName, ...currencyVariables] | ||
currencyVariables | ||
) | ||
) { | ||
currencyVariables.push(node.id.name); | ||
currencyVariables.add(node.id.name); | ||
} | ||
}, | ||
VariableDeclarator({ node }) { | ||
VariableDeclarator({ scope, node }) { | ||
if (expressionCallsCurrency(node.init, currencyVariables)) { | ||
currencyVariables.push(node.id.name); | ||
let binding = scope.bindings[node.id.name] || {}; | ||
if ( | ||
binding.kind !== 'let' || | ||
(binding.kind === 'let' && binding.scope === refPath.scope) | ||
) { | ||
currencyVariables.add(node.id.name); | ||
} | ||
} | ||
@@ -78,5 +84,5 @@ }, | ||
if (!expressionCallsCurrency(right, currencyVariables)) { | ||
currencyVariables = currencyVariables.filter(x => x !== left.name); | ||
currencyVariables.delete(left.name); | ||
} else { | ||
currencyVariables.push(left.name); | ||
currencyVariables.add(left.name); | ||
} | ||
@@ -87,3 +93,3 @@ }, | ||
// Attempt to find any currency variables in scope | ||
let currentPath = path.parentPath; | ||
let currentPath = refPath.parentPath; | ||
while (currentPath) { | ||
@@ -122,3 +128,3 @@ currentPath.traverse(Visitors); | ||
t.memberExpression(left, t.identifier('value')), | ||
expressionContainsCurrency(path.get('right'), methodName) | ||
pathContainsCurrency(path.get('right'), methodName) | ||
? t.memberExpression(right, t.identifier('value')) | ||
@@ -166,3 +172,3 @@ : right | ||
opts.hasCurrency && | ||
expressionContainsCurrency(path.get('left'), opts.methodName) | ||
pathContainsCurrency(path.get('left'), opts.methodName) | ||
) { | ||
@@ -169,0 +175,0 @@ // Prevent replacement nodes from being visited multiple times |
9580
153