babel-plugin-transform-bigint
Advanced tools
Comparing version 1.0.23 to 1.0.24
@@ -17,2 +17,4 @@ // to run this test file use `npx jest` in the parent folder or `npm run test` | ||
const b = 1n; | ||
const array = [1n]; | ||
let i = 1; | ||
@@ -23,2 +25,4 @@ o.x.y += b; | ||
o.x[y + z] += b; | ||
array[i] += b; | ||
array[0] += b; | ||
`; | ||
@@ -25,0 +29,0 @@ const {code} = babel.transform(example, {plugins: [plugin]}); |
57
index.js
@@ -579,2 +579,19 @@ // see https://github.com/babel/babel/pull/6015 | ||
AssignmentExpression: function (path, state) { | ||
const isConstant = function (path) { | ||
if (types.isStringLiteral(path.node)) { | ||
return true; | ||
} | ||
if (types.isNumericLiteral(path.node)) { | ||
return true; | ||
} | ||
if (types.isIdentifier(path.node)) { | ||
const binding = path.scope.getBinding(path.node.name); | ||
if (binding == null) { | ||
console.warn('unknown identifier: ' + path.node.name); | ||
return false; | ||
} | ||
return binding.constant; | ||
} | ||
return false; | ||
}; | ||
if (types.isMemberExpression(path.node.left) && types.isIdentifier(path.node.left.object) && path.node.left.object.name === 'arguments') { | ||
@@ -592,20 +609,28 @@ throw new RangeError('arguments should not be used'); | ||
if (types.isMemberExpression(left)) { | ||
const x = path.scope.generateUidIdentifier('x'); | ||
path.scope.push({id: x}); | ||
if (left.computed && !types.isStringLiteral(left.property)) { | ||
const y = path.scope.generateUidIdentifier('y'); | ||
// object[property] += right -> (x = object, y = property, x[y] = x[y] + right) | ||
const expressions = []; | ||
let x = left.object; | ||
if (!isConstant(path.get('left').get('object'))) { | ||
x = path.scope.generateUidIdentifier('x'); | ||
path.scope.push({id: x}); | ||
expressions.push(types.assignmentExpression('=', x, left.object)); | ||
} | ||
let y = left.property; | ||
if (!isConstant(path.get('left').get('property'))) { | ||
y = path.scope.generateUidIdentifier('y'); | ||
path.scope.push({id: y}); | ||
// object[property] += right -> (x = object, y = property, x[y] = x[y] + right) | ||
path.replaceWith(types.sequenceExpression([ | ||
types.assignmentExpression('=', x, left.object), | ||
types.assignmentExpression('=', y, left.computed ? left.property : types.StringLiteral(left.property.name)), | ||
types.assignmentExpression('=', types.memberExpression(x, y, true), types.callExpression(types.memberExpression(types.identifier(JSBI), types.identifier(functionName)), [types.memberExpression(x, y, true), right])) | ||
])); | ||
expressions.push(types.assignmentExpression('=', y, left.property)); | ||
} | ||
const assignment = types.assignmentExpression('=', | ||
types.memberExpression(x, y, left.computed), | ||
types.callExpression( | ||
types.memberExpression(types.identifier(JSBI), types.identifier(functionName)), | ||
[types.memberExpression(x, y, left.computed), right] | ||
) | ||
); | ||
expressions.push(assignment); | ||
if (expressions.length === 1) { | ||
path.replaceWith(expressions[0]); | ||
} else { | ||
const y = left.computed ? left.property : types.StringLiteral(left.property.name); | ||
// object[property] += right -> (x = object, x[property] = x[property] + right) | ||
path.replaceWith(types.sequenceExpression([ | ||
types.assignmentExpression('=', x, left.object), | ||
types.assignmentExpression('=', types.memberExpression(x, y, true), types.callExpression(types.memberExpression(types.identifier(JSBI), types.identifier(functionName)), [types.memberExpression(x, y, true), right])) | ||
])); | ||
path.replaceWith(types.sequenceExpression(expressions)); | ||
} | ||
@@ -612,0 +637,0 @@ } else { |
{ | ||
"name": "babel-plugin-transform-bigint", | ||
"version": "1.0.23", | ||
"version": "1.0.24", | ||
"description": "A plugin for babel to transform `x * y` into something like `JSBI.multiply(x, y)` to support bigints.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
54758
926