Some cleanup in JS optimizer

This commit is contained in:
Alexey Andreev
2016-03-18 17:54:01 +03:00
parent 3571201e9c
commit a7fcbb614d
3 changed files with 13 additions and 4 deletions
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.js.inline.context.InliningContext
import org.jetbrains.kotlin.js.inline.context.NamingContext
import org.jetbrains.kotlin.js.inline.util.*
import org.jetbrains.kotlin.js.inline.util.rewriters.ReturnReplacingVisitor
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils.newVar
class FunctionInlineMutator
private constructor(
@@ -157,8 +157,7 @@ internal class TemporaryAssignmentElimination(private val root: JsBlock) {
val assignment = JsAstUtils.decomposeAssignmentToVariable(x.expression)
if (assignment != null) {
val (name, value) = assignment
val usage = getUsage(name)
val usage = getUsage(assignment.first)
if (usage is Usage.Declaration) {
usage.count++
}
@@ -190,7 +190,7 @@ internal class TemporaryVariableElimination(private val root: JsStatement) {
val name = x.name
if (x.qualifier == null && name != null && shouldConsiderTemporary(name)) {
hasChanges = true
val newExpr = definedValues[name]!!
val newExpr = definedValues[name] ?: JsLiteral.UNDEFINED
ctx.replaceMe(accept(newExpr))
usages[name] = usages[name]!! - 1
return false
@@ -201,6 +201,17 @@ internal class TemporaryVariableElimination(private val root: JsStatement) {
override fun visit(x: JsBreak, ctx: JsContext<*>) = false
override fun visit(x: JsContinue, ctx: JsContext<*>) = false
override fun visit(x: JsReturn, ctx: JsContext<*>): Boolean {
val returnValue = x.expression
if (returnValue is JsNameRef) {
val name = returnValue.name
if (returnValue.qualifier == null && name != null && name in temporary && definitions[name] ?: 0 == 0) {
x.expression = null
}
}
return super.visit(x, ctx)
}
}.accept(root)
}