@oraichain/babel-plugin-operator-overloading
Advanced tools
Comparing version 1.0.2 to 1.0.3
86
index.js
@@ -1,12 +0,17 @@ | ||
const template = require("@babel/template").default; | ||
const { NodePath, PluginPass } = require("@babel/core"); | ||
const BabelTypes = require("@babel/types"); | ||
const template = require('@babel/template').default; | ||
const { NodePath, PluginPass } = require('@babel/core'); | ||
const BabelTypes = require('@babel/types'); | ||
const OperatorOverloadDirectiveName = "operator-overloading"; | ||
const OperatorOverloadDirectiveName = 'operator-overloading'; | ||
const methodMap = { | ||
"+": "add", | ||
"-": "sub", | ||
"*": "mul", | ||
"/": "div", | ||
'+': 'add', | ||
'-': 'sub', | ||
'*': 'mul', | ||
'/': 'div', | ||
'<': 'lt', | ||
'>': 'gt', | ||
'==': 'eq', | ||
'<=': 'le', | ||
'>=': 'ge' | ||
}; | ||
@@ -24,7 +29,6 @@ | ||
switch (leftNode.type) { | ||
case "NewExpression": | ||
if (classNames.includes(leftNode.callee.name)) | ||
return leftNode.callee.name; | ||
case 'NewExpression': | ||
if (classNames.includes(leftNode.callee.name)) return leftNode.callee.name; | ||
case "Identifier": | ||
case 'Identifier': | ||
const className = declarations[leftNode.name]; | ||
@@ -45,19 +49,8 @@ if (className) return className; | ||
function createBinaryTemplate(node, className) { | ||
const LHS = | ||
node.left.type === "BinaryExpression" | ||
? createBinaryTemplate(node.left, className) | ||
: node.left; | ||
const RHS = | ||
node.right.type === "BinaryExpression" | ||
? createBinaryTemplate(node.right, className) | ||
: node.right; | ||
const lhsAssign = node.left.type.endsWith("Literal") | ||
? `new ${className}(LHS)` | ||
: "LHS"; | ||
const method = methodMap[node.operator]; | ||
return template( | ||
method ? `${lhsAssign}.${method}(RHS)` : `${lhsAssign} ${node.operator} RHS` | ||
)({ | ||
const LHS = node.left.type === 'BinaryExpression' ? createBinaryTemplate(node.left, className) : node.left; | ||
const RHS = node.right.type === 'BinaryExpression' ? createBinaryTemplate(node.right, className) : node.right; | ||
const lhsAssign = node.left.type.endsWith('Literal') ? `new ${className}(LHS)` : 'LHS'; | ||
return template(`${lhsAssign}.${methodMap[node.operator]}(RHS)`)({ | ||
LHS, | ||
RHS, | ||
RHS | ||
}).expression; | ||
@@ -82,6 +75,6 @@ } | ||
classNames: state.opts.classNames ?? [], | ||
declarations: {}, | ||
declarations: {} | ||
}); | ||
} | ||
}, | ||
} | ||
}, | ||
@@ -94,5 +87,3 @@ | ||
VariableDeclaration(path, state) { | ||
const { declarations, classNames } = state.get( | ||
OperatorOverloadDirectiveName | ||
); | ||
const { declarations, classNames } = state.get(OperatorOverloadDirectiveName); | ||
@@ -102,3 +93,3 @@ for (const d of path.node.declarations) { | ||
switch (d.init.type) { | ||
case "NewExpression": | ||
case 'NewExpression': | ||
if (classNames.includes(d.init.callee.name)) { | ||
@@ -109,8 +100,4 @@ declarations[d.id.name] = d.init.callee.name; | ||
case "BinaryExpression": | ||
const className = checkClassNameReturn( | ||
d.init, | ||
declarations, | ||
classNames | ||
); | ||
case 'BinaryExpression': | ||
const className = checkClassNameReturn(d.init, declarations, classNames); | ||
if (className) { | ||
@@ -133,20 +120,11 @@ declarations[d.id.name] = className; | ||
const { declarations, classNames } = state.get( | ||
OperatorOverloadDirectiveName | ||
); | ||
const className = checkClassNameReturn( | ||
path.node, | ||
declarations, | ||
classNames | ||
); | ||
const { declarations, classNames } = state.get(OperatorOverloadDirectiveName); | ||
const className = checkClassNameReturn(path.node, declarations, classNames); | ||
if (className) { | ||
const expressionStatement = createBinaryTemplate( | ||
path.node, | ||
className | ||
); | ||
const expressionStatement = createBinaryTemplate(path.node, className); | ||
path.replaceWith(t.expressionStatement(expressionStatement, [])); | ||
} | ||
}, | ||
}, | ||
} | ||
} | ||
}; | ||
}; |
{ | ||
"name": "@oraichain/babel-plugin-operator-overloading", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "files": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3718
111