Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-transform-currency-operators

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-currency-operators - npm Package Compare versions

Comparing version 1.0.0-alpha.2 to 1.0.0-beta.1

8

package.json
{
"name": "babel-plugin-transform-currency-operators",
"version": "1.0.0-alpha.2",
"version": "1.0.0-beta.1",
"homepage": "An experimental babel plugin for transforming currency.js operators",

@@ -9,3 +9,6 @@ "description": "https://github.com/scurker/babel-plugin-transform-currency-operators",

"coverage": "nyc mocha --compilers js:babel-core/register ./test/index.js",
"report:html": "nyc report --reporter=html",
"report:text-lcov": "nyc report --reporter=text-lcov",
"lint": "eslint .",
"prettier": "prettier --single-quote --trailing-comma es5 --write '{src/**/*.js,test/**/*.js}'",
"test": "mocha --compilers js:babel-core/register"

@@ -38,4 +41,5 @@ },

"mocha": "^3.5.0",
"nyc": "^11.2.1"
"nyc": "^11.2.1",
"prettier": "^1.6.1"
}
}

@@ -1,2 +0,2 @@

# babel-plugin-transform-currency-operators [![Build Status](https://travis-ci.org/scurker/babel-plugin-transform-currency-operators.svg?branch=master)](https://travis-ci.org/scurker/babel-plugin-transform-currency-operators) [![Coverage Status](https://coveralls.io/repos/scurker/babel-plugin-transform-currency-operators/badge.svg?branch=test-coverage&service=github)](https://coveralls.io/github/scurker/babel-plugin-transform-currency-operators?branch=test-coverage) [![npm](https://img.shields.io/npm/v/babel-plugin-transform-currency-operators.svg?style=flat)](https://www.npmjs.com/package/babel-plugin-transform-currency-operators)
# babel-plugin-transform-currency-operators [![Build Status](https://travis-ci.org/scurker/babel-plugin-transform-currency-operators.svg?branch=master)](https://travis-ci.org/scurker/babel-plugin-transform-currency-operators) [![Coverage Status](https://coveralls.io/repos/github/scurker/babel-plugin-transform-currency-operators/badge.svg?branch=master)](https://coveralls.io/github/scurker/babel-plugin-transform-currency-operators?branch=master) [![npm](https://img.shields.io/npm/v/babel-plugin-transform-currency-operators.svg?style=flat)](https://www.npmjs.com/package/babel-plugin-transform-currency-operators)

@@ -19,2 +19,5 @@ > An experimental babel plugin for transforming [currency.js](https://github.com/scurker/currency.js) operators.

var MultiDecimal = value => currency(value, { decimals: 3 });
var currency7 = MultiDecimal(1.234) + 5.678;
if(currency1 < currency2) { ... }

@@ -37,2 +40,5 @@ if(currency1 > currency2) { ... }

var MultiDecimal = value => currency(value, { decimals: 3 });
var currency7 = MultiDecimal(1.234).add(5.678);
if(currency1.value < currency2.value) { ... }

@@ -39,0 +45,0 @@ if(currency1.value > currency2.value) { ... }

@@ -5,16 +5,8 @@ const arithmeticOperators = {

'/': 'divide',
'*': 'multiply'
'*': 'multiply',
};
const compareOperators = [
'===',
'==',
'>',
'<',
'>=',
'<='
];
const compareOperators = ['===', '==', '>', '<', '>=', '<='];
export default function transformCurrencyOperators({ types: t }) {
function expressionCallsCurrency(node, identifiers) {

@@ -25,2 +17,6 @@ if (!node) {

if (t.isBinaryExpression(node)) {
return expressionCallsCurrency(node.left, identifiers);
}
if (t.isCallExpression(node)) {

@@ -34,6 +30,24 @@ return expressionCallsCurrency(node.callee, identifiers);

if (t.isBinaryExpression(node)) {
return expressionCallsCurrency(node.left, identifiers);
if (t.isFunctionExpression(node)) {
return expressionCallsCurrency(
node.body.body.find(n => t.isReturnStatement(n)),
identifiers
);
}
if (t.isArrowFunctionExpression(node)) {
if (t.isCallExpression(node.body)) {
return expressionCallsCurrency(node.body, identifiers);
} else if (t.isBlockStatement(node.body)) {
return expressionCallsCurrency(
node.body.body.find(n => t.isReturnStatement(n)),
identifiers
);
}
}
if (t.isReturnStatement(node)) {
return expressionCallsCurrency(node.argument.callee, identifiers);
}
return t.isIdentifier(node) && identifiers.includes(node.name);

@@ -44,29 +58,46 @@ }

let { node } = path
, currencyVariables = [];
, currencyVariables = [methodName];
const VariableVisitor = {
const Visitors = {
FunctionDeclaration({ node }) {
if (
expressionCallsCurrency(
node.body.body.find(n => t.isReturnStatement(n)),
[methodName, ...currencyVariables]
)
) {
currencyVariables.push(node.id.name);
}
},
VariableDeclarator({ node }) {
if (expressionCallsCurrency(node.init, [methodName, ...currencyVariables])) {
if (expressionCallsCurrency(node.init, currencyVariables)) {
currencyVariables.push(node.id.name);
}
}
},
AssignmentExpression({ node }) {
let { left, right } = node;
if (!expressionCallsCurrency(right, currencyVariables)) {
currencyVariables = currencyVariables.filter(x => x !== left.name);
} else {
currencyVariables.push(left.name);
}
},
};
if(t.isIdentifier(node)) {
// Attempt to find any currency variables in scope
let currentPath = path.parentPath;
while(currentPath) {
currentPath.traverse(VariableVisitor);
currentPath = currentPath.parentPath;
}
// Attempt to find any currency variables in scope
let currentPath = path.parentPath;
while (currentPath) {
currentPath.traverse(Visitors);
currentPath = currentPath.parentPath;
}
return node && expressionCallsCurrency(node, [methodName]) || (t.isIdentifier(node) && currencyVariables.includes(node.name));
return node && expressionCallsCurrency(node, currencyVariables);
}
function buildExpression(node, methodName) {
let { operator, left, right } = node;
function buildExpression(path, methodName) {
let { node } = path
, { operator, left, right } = node;
if (t.isBinaryExpression(left)) {
left = buildExpression(left);
left = buildExpression(path.get('left'));
}

@@ -84,10 +115,12 @@

);
} else if(compareOperators.includes(operator)) {
} else if (
compareOperators.includes(operator) &&
!t.isMemberExpression(node.left)
) {
return t.binaryExpression(
operator,
t.memberExpression(
left,
t.identifier('value')
),
expressionContainsCurrency({ node: right }, methodName) ? t.memberExpression(right, t.identifier('value')) : right
t.memberExpression(left, t.identifier('value')),
expressionContainsCurrency(path.get('right'), methodName)
? t.memberExpression(right, t.identifier('value'))
: right
);

@@ -100,9 +133,11 @@ } else {

return {
visitor: {
VariableDeclarator({ node }, { opts }) {
let { init } = node;
if(t.isCallExpression(init) && init.callee.name === 'require' && init.arguments[0].value === 'currency.js') {
if (
t.isCallExpression(init) &&
init.callee.name === 'require' &&
init.arguments[0].value === 'currency.js'
) {
opts.hasCurrency = true;

@@ -118,4 +153,6 @@ opts.methodName = node.id.name;

if(source.value === 'currency.js') {
let defaultImport = specifiers.find(specifier => t.isImportDefaultSpecifier(specifier));
if (source.value === 'currency.js') {
let defaultImport = specifiers.find(specifier =>
t.isImportDefaultSpecifier(specifier)
);
opts.hasCurrency = true;

@@ -129,16 +166,16 @@ opts.methodName = defaultImport.local.name;

BinaryExpression(path, { opts }) {
if(opts.hasCurrency && expressionContainsCurrency(path.get('left'), opts.methodName)) {
if (
opts.hasCurrency &&
expressionContainsCurrency(path.get('left'), opts.methodName)
) {
// Prevent replacement nodes from being visited multiple times
path.stop();
return path.replaceWith(buildExpression(path.node, opts.methodName));
return path.replaceWith(buildExpression(path, opts.methodName));
}
return;
}
}
}
}
},
},
};
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc