Skip to content

Commit

Permalink
sojsonv7: fix bug in purifyFunction #75
Browse files Browse the repository at this point in the history
Signed-off-by: echo094 <[email protected]>
  • Loading branch information
echo094 authored Feb 25, 2024
1 parent 278668d commit 5c365b6
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/plugin/sojsonv7.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,22 +784,42 @@ function unlockEnv(ast) {
traverse(ast, { StringLiteral: unlockLint })
}

/**
* If a function acts as follows:
* A = function (p1, p2) { return p1 + p2 }
*
* Convert its call to a binary expression:
* A(a, b) => a + b
*/
function purifyFunction(path) {
const node = path.node
if (!t.isIdentifier(node.left) || !t.isFunctionExpression(node.right)) {
const left = path.get('left')
const right = path.get('right')
if (!left.isIdentifier() || !right.isFunctionExpression()) {
return
}
const name = left.node.name
const params = right.node.params
if (params.length !== 2) {
return
}
const name = node.left.name
if (node.right.body.body.length !== 1) {
const name1 = params[0].name
const name2 = params[1].name
if (right.node.body.body.length !== 1) {
return
}
let retStmt = node.right.body.body[0]
let retStmt = right.node.body.body[0]
if (!t.isReturnStatement(retStmt)) {
return
}
if (!t.isBinaryExpression(retStmt.argument, { operator: '+' })) {
return
}
if (
retStmt.argument.left?.name !== name1 ||
retStmt.argument.right?.name !== name2
) {
return
}
const fnPath = path.getFunctionParent() || path.scope.path
fnPath.traverse({
CallExpression: function (_path) {
Expand Down

0 comments on commit 5c365b6

Please sign in to comment.