Socket
Socket
Sign inDemoInstall

babel-plugin-transform-bigint

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-bigint - npm Package Compare versions

Comparing version 1.0.8 to 1.0.9

165

index.js

@@ -58,10 +58,26 @@ // see https://github.com/babel/babel/pull/6015

}
visited.set(path, true);
visited.set(path, maybeJSBI);
const result = canBeBigIntInternal(path);
visited.set(path, result);
if (result === maybeJSBI) {
visited.delete(path);
} else {
visited.set(path, result);
}
return result;
};
const and = function (a, b) {
if (a === maybeJSBI) {
return b;
}
if (b === maybeJSBI) {
return a;
}
if (a === JSBI && b === JSBI) {
return JSBI;
}
return false;
};
const canBeBigIntInternal = function (path) {
if (path.node.type === 'BigIntLiteral') {
return true;
return JSBI;
}

@@ -78,6 +94,6 @@ if (path.node.type === 'NumericLiteral') {

if (path.node.type === 'BinaryExpression') {
return canBeBigInt(path.get('left')) && canBeBigInt(path.get('right'));
return and(canBeBigInt(path.get('left')), canBeBigInt(path.get('right')));
}
if (path.node.type === 'AssignmentExpression') {
return canBeBigInt(path.get('left')) && canBeBigInt(path.get('right'));
return and(canBeBigInt(path.get('left')), canBeBigInt(path.get('right')));
}

@@ -89,3 +105,3 @@ if (path.node.type === 'Identifier') {

const x = binding.path.get('init');
if (x.node != null && !canBeBigInt(x)) {
if (x.node != null && canBeBigInt(x) === false) {
return false;

@@ -95,4 +111,4 @@ }

for (const path of binding.referencePaths) {
if (!canBeBigInt(path.parentPath)) {
//return false;
if (path.parentPath.node.type === 'BinaryExpression' && getFunctionName(path.parentPath.node.operator) != null && canBeBigInt(path.parentPath) === false) {
return false;
}

@@ -105,6 +121,33 @@ }

}
return true;
if (binding != null && binding.constant) {
const ifStatement = path.findParent(path => path.isIfStatement());
const variableName = path.node.name;
if (ifStatement != null) {
const tmp = ifStatement.get('test');
if (tmp.node.operator === '&&') {
const checkTypeOf = function (node) {
if (node.type === 'BinaryExpression' && node.operator === '===') {
if (node.left.type === 'UnaryExpression' && node.left.operator === 'typeof') {
if (node.left.argument.type === 'Identifier' && node.left.argument.name === variableName) {
if (node.right.type === 'StringLiteral' && node.right.value === 'number') {
return true;
}
}
}
}
return false;
};
if (checkTypeOf(tmp.node.left)) {
return false;
}
if (checkTypeOf(tmp.node.right)) {
return false;
}
}
}
}
return maybeJSBI;
}
if (path.node.type === 'ConditionalExpression') {
return canBeBigInt(path.get('consequent')) || canBeBigInt(path.get('alternate'));
return canBeBigInt(path.get('consequent')) !== false || canBeBigInt(path.get('alternate')) !== false ? maybeJSBI : false;
}

@@ -136,2 +179,6 @@ if (path.node.type === 'FunctionExpression') {

}
if (path.node.callee.type === 'Identifier' &&
path.node.callee.name === 'BigInt') {
return JSBI;
}
}

@@ -146,4 +193,3 @@ if (path.node.type === 'CallExpression') {

if (statement.type === 'ReturnStatement') {
if (!canBeBigInt(statement.get('argument'))) {
//console.log(path.node.callee.name);
if (canBeBigInt(statement.get('argument')) === false) {
return false;

@@ -157,9 +203,83 @@ }

}
if (path.node.type === 'UpdateExpression') {
return canBeBigInt(path.get('argument'));
}
//TODO:
return true;
return maybeJSBI;
};
const JSBI = 'JSBI';
const maybeJSBI = 'maybeJSBI';
//const maybeJSBI = JSBI;
const IMPORT_PATH = './jsbi.mjs';
const maybeJSBICode = `
var maybeJSBI = {
BigInt: function BigInt(a) {
return JSBI.BigInt(a);
},
toNumber: function toNumber(a) {
return typeof a === "object" ? JSBI.toNumber(a) : Number(a);
},
add: function add(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.add(a, b) : a + b;
},
subtract: function subtract(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.subtract(a, b) : a - b;
},
multiply: function multiply(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.multiply(a, b) : a * b;
},
divide: function divide(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.divide(a, b) : a / b;
},
remainder: function remainder(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.remainder(a, b) : a % b;
},
exponentiate: function exponentiate(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.exponentiate(a, b) : (typeof a === "bigint" && typeof b === "bigint" ? new Function("a**b", "a", "b")(a, b) : Math.pow(a, b));
},
leftShift: function leftShift(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.leftShift(a, b) : a << b;
},
signedRightShift: function signedRightShift(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.signedRightShift(a, b) : a >> b;
},
bitwiseAnd: function bitwiseAnd(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.bitwiseAnd(a, b) : a & b;
},
bitwiseOr: function bitwiseOr(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.bitwiseOr(a, b) : a | b;
},
bitwiseXor: function bitwiseXor(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.bitwiseXor(a, b) : a ^ b;
},
lessThan: function lessThan(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.lessThan(a, b) : a < b;
},
greaterThan: function greaterThan(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.greaterThan(a, b) : a > b;
},
lessThanOrEqual: function lessOrEqualThan(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.lessThanOrEqual(a, b) : a <= b;
},
greaterThanOrEqual: function greaterOrEqualThan(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.greaterThanOrEqual(a, b) : a >= b;
},
equal: function equal(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.equal(a, b) : a === b;
},
notEqual: function notEqual(a, b) {
return typeof a === "object" && typeof b === "object" ? JSBI.notEqual(a, b) : a !== b;
},
unaryMinus: function unaryMinus(a) {
return typeof a === "object" ? JSBI.unaryMinus(a) : -a;
},
bitwiseNot: function bitwiseNot(a) {
return typeof a === "object" ? JSBI.bitwiseNot(a) : ~a;
}
};
`;
//const maybeJSBICode = '';
return {

@@ -170,3 +290,4 @@ inherits: syntaxBigInt,

if (path.node.callee.name === 'Number') {
if (canBeBigInt(path.get('arguments')[0])) {
const JSBI = canBeBigInt(path.get('arguments')[0]);
if (JSBI !== false) {
path.replaceWith(types.callExpression(types.memberExpression(types.identifier(JSBI), types.identifier('toNumber')), path.node.arguments));

@@ -191,3 +312,4 @@ }

BinaryExpression: function (path, state) {
if (canBeBigInt(path)) {
const JSBI = canBeBigInt(path);
if (JSBI !== false) {
const operator = path.node.operator;

@@ -202,3 +324,4 @@ const functionName = getFunctionName(operator) || getRelationalFunctionName(operator);

UnaryExpression: function (path, state) {
if (canBeBigInt(path)) {
const JSBI = canBeBigInt(path);
if (JSBI !== false) {
const functionName = getUnaryFunctionName(path.node.operator);

@@ -212,3 +335,4 @@ if (functionName !== null) {

UpdateExpression: function (path, state) {
if (canBeBigInt(path)) {
const JSBI = canBeBigInt(path);
if (JSBI !== false) {
const operator = path.node.operator;

@@ -267,3 +391,4 @@ const prefix = path.node.prefix;

AssignmentExpression: function (path, state) {
if (canBeBigInt(path)) {
const JSBI = canBeBigInt(path);
if (JSBI !== false) {
const operator = path.node.operator;

@@ -301,4 +426,8 @@ if (operator.endsWith('=')) {

}
},
post: function (state) {
//console.log(state);
state.ast.program.body.unshift(babel.parse(maybeJSBICode).program.body[0]);
}
};
};

4

package.json
{
"name": "babel-plugin-transform-bigint",
"version": "1.0.8",
"version": "1.0.9",
"description": "A plugin for babel to transform `x * y` into something like `JSBI.multiply(x, y)` to support bigints.",
"main": "index.js",
"scripts": {
"test": "npx babel --plugins=./index.js tests.js"
"test": "npx babel --plugins=module:./index.js tests.js"
},

@@ -9,0 +9,0 @@ "author": "",

@@ -36,7 +36,1 @@ // to run the test use a command: `npx babel --plugins=./index.js tests.js`

*/
Number('0x12');
const bigint = 1n;
Number(bigint);
const a = {};
a.a === undefined;
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