Eliminate redundant RHS of JS comma expression
This commit is contained in:
+20
-6
@@ -36,12 +36,26 @@ class RedundantStatementElimination(private val root: JsFunction) {
|
||||
private fun process() {
|
||||
object : JsVisitorWithContextImpl() {
|
||||
override fun visit(x: JsExpressionStatement, ctx: JsContext<JsNode>): Boolean {
|
||||
if (!x.expression.isSuspend && (x.synthetic || x.expression.synthetic)) {
|
||||
val replacement = replace(x.expression)
|
||||
if (replacement.size != 1 || replacement[0] != x.expression) {
|
||||
hasChanges = true
|
||||
ctx.addPrevious(replacement.map { JsExpressionStatement(it).apply { synthetic = true } })
|
||||
ctx.removeMe()
|
||||
if (!x.expression.isSuspend) {
|
||||
val expression = x.expression
|
||||
if (x.synthetic || expression.synthetic) {
|
||||
val replacement = replace(x.expression)
|
||||
if (replacement.size != 1 || replacement[0] != x.expression) {
|
||||
hasChanges = true
|
||||
ctx.addPrevious(replacement.map { JsExpressionStatement(it).apply { synthetic = true } })
|
||||
ctx.removeMe()
|
||||
}
|
||||
}
|
||||
else if (expression is JsBinaryOperation && expression.operator == JsBinaryOperator.COMMA) {
|
||||
var currentExpression = expression
|
||||
while (currentExpression is JsBinaryOperation && currentExpression.operator == JsBinaryOperator.COMMA) {
|
||||
val replacement = replace(currentExpression.arg2)
|
||||
if (replacement.isNotEmpty()) break
|
||||
currentExpression = currentExpression.arg1
|
||||
}
|
||||
if (currentExpression != expression) {
|
||||
ctx.replaceMe(JsExpressionStatement(currentExpression).apply { copyMetadataFrom(x) })
|
||||
}
|
||||
}
|
||||
}
|
||||
return super.visit(x, ctx)
|
||||
|
||||
Reference in New Issue
Block a user