JS: decomposition of vars
This commit is contained in:
@@ -76,6 +76,25 @@ class ExpressionDecomposer private (
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: add test case (after KT-7371 fix): var a = foo(), b = foo() + inlineBar()
|
||||
override fun visit(x: JsVars, ctx: JsContext<*>): Boolean {
|
||||
val vars = x.getVars()
|
||||
var prevVars = SmartList<JsVars.JsVar>()
|
||||
|
||||
for (jsVar in vars) {
|
||||
if (jsVar in containsExtractable && prevVars.isNotEmpty()) {
|
||||
addStatement(JsVars(prevVars, x.isMultiline()))
|
||||
prevVars = SmartList<JsVars.JsVar>()
|
||||
}
|
||||
|
||||
jsVar.initExpression = accept(jsVar.initExpression)
|
||||
prevVars.add(jsVar)
|
||||
}
|
||||
|
||||
vars.clear()
|
||||
vars.addAll(prevVars)
|
||||
return false
|
||||
}
|
||||
|
||||
override fun visit(x: JsWhile, ctx: JsContext<*>): Boolean {
|
||||
x.process(true)
|
||||
|
||||
@@ -97,4 +97,8 @@ public var JsBinaryOperation.arg2: JsExpression
|
||||
set(value) = setArg2(value)
|
||||
|
||||
public val JsBinaryOperation.operator: JsBinaryOperator
|
||||
get() = getOperator()
|
||||
get() = getOperator()
|
||||
|
||||
public var JsVars.JsVar.initExpression: JsExpression?
|
||||
get() = getInitExpression()
|
||||
set(value) = setInitExpression(value)
|
||||
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
// CHECK_NOT_CALLED: component2
|
||||
|
||||
class A(val x: Int, val y: Int)
|
||||
|
||||
fun A.component1(): Int = fizz(x)
|
||||
|
||||
inline
|
||||
fun A.component2(): Int = buzz(y)
|
||||
|
||||
fun box(): String {
|
||||
val (a, b) = A(1, 2)
|
||||
assertEquals(a, 1)
|
||||
assertEquals(b, 2)
|
||||
assertEquals("fizz(1);buzz(2);", pullLog())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package foo
|
||||
|
||||
// CHECK_NOT_CALLED: component2
|
||||
// CHECK_NOT_CALLED: component3
|
||||
// CHECK_NOT_CALLED: component5
|
||||
|
||||
class A(val a: Int, val b: Int, val c: Int, val d: Int, val e: Int)
|
||||
|
||||
fun A.component1(): Int = fizz(a)
|
||||
|
||||
inline
|
||||
fun A.component2(): Int = buzz(b)
|
||||
|
||||
inline
|
||||
fun A.component3(): Int = buzz(c)
|
||||
|
||||
fun A.component4(): Int = fizz(d)
|
||||
|
||||
inline
|
||||
fun A.component5(): Int = buzz(e)
|
||||
|
||||
fun box(): String {
|
||||
val (a, b, c, d, e) = A(1, 2, 3, 4, 5)
|
||||
|
||||
assertEquals(1, a)
|
||||
assertEquals(2, b)
|
||||
assertEquals(3, c)
|
||||
assertEquals(4, d)
|
||||
assertEquals(5, e)
|
||||
assertEquals("fizz(1);buzz(2);buzz(3);fizz(4);buzz(5);", pullLog())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user