This commit is contained in:
cilame 2021-11-20 19:42:25 +08:00
parent a5d60f905d
commit e82aec626c

View File

@ -32358,19 +32358,27 @@ function ConditionVarToIf(path) {
// } else { // } else {
// var a = 22; // var a = 22;
// } // }
let {id, init} = path.node; if (t.isForStatement(path.parentPath)) return;
if (!t.isConditionalExpression(init)) return; var decl = path.node.declarations
const ParentPath = path.parentPath; var rpls = []
const ParentNode = path.parent; var togg = false
if (!t.isVariableDeclaration(ParentNode)) return; for (var i = 0; i < decl.length; i++) {
if (t.isForStatement(ParentPath.parentPath)) return; if (t.isConditionalExpression(decl[i].init)){
let kind = ParentNode.kind; togg = true
let {test, consequent, alternate} = init; let {test, consequent, alternate} = decl[i].init;
ParentPath.replaceWith(t.ifStatement( rpls.push(t.ifStatement(
test, test,
t.blockStatement([t.variableDeclaration(kind, [t.variableDeclarator(id, consequent)]),]), t.blockStatement([t.variableDeclaration(path.node.kind, [t.variableDeclarator(decl[i].id, consequent)]),]),
t.blockStatement([t.variableDeclaration(kind, [t.variableDeclarator(id, alternate)]),]) t.blockStatement([t.variableDeclaration(path.node.kind, [t.variableDeclarator(decl[i].id, alternate)]),])
)); ))
}else{
rpls.push(t.VariableDeclaration(path.node.kind, [decl[i]]))
}
}
if (togg){
path.replaceWithMultiple(rpls)
path.stop()
}
} }
function RemoveComma(path) { function RemoveComma(path) {
@ -32920,7 +32928,7 @@ function muti_process_defusion(jscode){
traverse(ast, {ConditionalExpression: TransCondition,}); // 三元表达式 traverse(ast, {ConditionalExpression: TransCondition,}); // 三元表达式
traverse(ast, {ExpressionStatement: ConditionToIf,}); // 三元表达式转换成if traverse(ast, {ExpressionStatement: ConditionToIf,}); // 三元表达式转换成if
traverse(ast, {ExpressionStatement: And2If}); // 单行语句如果是 a && b 形式转换成 if(a){ b } traverse(ast, {ExpressionStatement: And2If}); // 单行语句如果是 a && b 形式转换成 if(a){ b }
traverse(ast, {VariableDeclarator: ConditionVarToIf,}); // 赋值语句的 三元表达式转换成if traverse(ast, {VariableDeclaration: ConditionVarToIf,}); // 赋值语句的 三元表达式转换成if
traverse(ast, {ExpressionStatement: RemoveComma,}); // 逗号表达式转换 traverse(ast, {ExpressionStatement: RemoveComma,}); // 逗号表达式转换
traverse(ast, {VariableDeclaration: RemoveVarComma,}); // 赋值语句的 逗号表达式转换 traverse(ast, {VariableDeclaration: RemoveVarComma,}); // 赋值语句的 逗号表达式转换
traverse(ast, {MemberExpression: FormatMember,}); // obj['func1']['func2']() --> obj.func1.func2() traverse(ast, {MemberExpression: FormatMember,}); // obj['func1']['func2']() --> obj.func1.func2()
@ -32948,7 +32956,7 @@ function muti_process_sojsondefusion(jscode){
traverse(ast, {ConditionalExpression: TransCondition,}); // 三元表达式 traverse(ast, {ConditionalExpression: TransCondition,}); // 三元表达式
traverse(ast, {ExpressionStatement: ConditionToIf,}); // 三元表达式转换成if traverse(ast, {ExpressionStatement: ConditionToIf,}); // 三元表达式转换成if
traverse(ast, {ExpressionStatement: And2If}); // 单行语句如果是 a && b 形式转换成 if(a){ b } traverse(ast, {ExpressionStatement: And2If}); // 单行语句如果是 a && b 形式转换成 if(a){ b }
traverse(ast, {VariableDeclarator: ConditionVarToIf,}); // 赋值语句的 三元表达式转换成if traverse(ast, {VariableDeclaration: ConditionVarToIf,}); // 赋值语句的 三元表达式转换成if
traverse(ast, {ExpressionStatement: RemoveComma,}); // 逗号表达式转换 traverse(ast, {ExpressionStatement: RemoveComma,}); // 逗号表达式转换
traverse(ast, {VariableDeclaration: RemoveVarComma,}); // 赋值语句的 逗号表达式转换 traverse(ast, {VariableDeclaration: RemoveVarComma,}); // 赋值语句的 逗号表达式转换
traverse(ast, {MemberExpression: FormatMember,}); // obj['func1']['func2']() --> obj.func1.func2() traverse(ast, {MemberExpression: FormatMember,}); // obj['func1']['func2']() --> obj.func1.func2()
@ -32974,7 +32982,7 @@ function muti_process_obdefusion(jscode){
traverse(ast, {NumericLiteral: delExtra,}) // 清理二进制显示内容 traverse(ast, {NumericLiteral: delExtra,}) // 清理二进制显示内容
traverse(ast, {ConditionalExpression: TransCondition,}); // 三元表达式 traverse(ast, {ConditionalExpression: TransCondition,}); // 三元表达式
traverse(ast, {ExpressionStatement: ConditionToIf,}); // 三元表达式转换成if traverse(ast, {ExpressionStatement: ConditionToIf,}); // 三元表达式转换成if
traverse(ast, {VariableDeclarator: ConditionVarToIf,}); // 赋值语句的 三元表达式转换成if traverse(ast, {VariableDeclaration: ConditionVarToIf,}); // 赋值语句的 三元表达式转换成if
traverse(ast, {ExpressionStatement: RemoveComma,}); // 逗号表达式转换 traverse(ast, {ExpressionStatement: RemoveComma,}); // 逗号表达式转换
traverse(ast, {VariableDeclaration: RemoveVarComma,}); // 赋值语句的 逗号表达式转换 traverse(ast, {VariableDeclaration: RemoveVarComma,}); // 赋值语句的 逗号表达式转换
traverse(ast, {MemberExpression: FormatMember,}); // obj['func1']['func2']() --> obj.func1.func2() traverse(ast, {MemberExpression: FormatMember,}); // obj['func1']['func2']() --> obj.func1.func2()