Replace nested ifs with &&

Signed-off-by: Graham <gpe@openrs2.dev>
Graham 4 years ago
parent a75db6122b
commit 90c21d87cf
  1. 30
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt

@ -2,6 +2,7 @@ package dev.openrs2.deob.ast.transform
import com.github.javaparser.ast.CompilationUnit
import com.github.javaparser.ast.NodeList
import com.github.javaparser.ast.expr.BinaryExpr
import com.github.javaparser.ast.expr.ConditionalExpr
import com.github.javaparser.ast.stmt.BlockStmt
import com.github.javaparser.ast.stmt.IfStmt
@ -208,6 +209,35 @@ class IfElseTransformer : Transformer() {
}
}
}
/*
* Rewrite:
*
* if (a) {
* if (b) {
* ...
* }
* }
*
* to:
*
* if (a && b) {
* ...
* }
*/
unit.walk { outerStmt: IfStmt ->
if (outerStmt.elseStmt.isPresent) {
return@walk
}
val innerStmt = outerStmt.thenStmt.getIf() ?: return@walk
if (innerStmt.elseStmt.isPresent) {
return@walk
}
outerStmt.condition = BinaryExpr(outerStmt.condition, innerStmt.condition, BinaryExpr.Operator.AND)
outerStmt.thenStmt = innerStmt.thenStmt
}
}
private fun Statement.isIf(): Boolean {

Loading…
Cancel
Save