Extract Function: Do not report "Moving out of scope" warning on nested declarations

This commit is contained in:
Alexey Sedunov
2014-07-04 14:23:52 +04:00
parent 52c107504d
commit 59b33fcba4
6 changed files with 70 additions and 3 deletions
@@ -515,14 +515,19 @@ private fun ExtractionData.inferParametersInfo(
return null return null
} }
private fun ExtractionData.checkDeclarationsMovingOutOfScope(controlFlow: ControlFlow): ErrorMessage? { private fun ExtractionData.checkDeclarationsMovingOutOfScope(
enclosingDeclaration: JetDeclaration,
controlFlow: ControlFlow
): ErrorMessage? {
val declarationsOutOfScope = HashSet<JetNamedDeclaration>() val declarationsOutOfScope = HashSet<JetNamedDeclaration>()
if (controlFlow is JumpBasedControlFlow) { if (controlFlow is JumpBasedControlFlow) {
controlFlow.elementToInsertAfterCall.accept( controlFlow.elementToInsertAfterCall.accept(
object: JetTreeVisitorVoid() { object: JetTreeVisitorVoid() {
override fun visitSimpleNameExpression(expression: JetSimpleNameExpression) { override fun visitSimpleNameExpression(expression: JetSimpleNameExpression) {
val target = expression.getReference()?.resolve() val target = expression.getReference()?.resolve()
if (target is JetNamedDeclaration && target.isInsideOf(originalElements)) { if (target is JetNamedDeclaration
&& target.isInsideOf(originalElements)
&& target.getParentByType(javaClass<JetDeclaration>(), true) == enclosingDeclaration) {
declarationsOutOfScope.add(target) declarationsOutOfScope.add(target)
} }
} }
@@ -608,7 +613,7 @@ fun ExtractionData.performAnalysis(): AnalysisResult {
) )
} }
checkDeclarationsMovingOutOfScope(controlFlow)?.let { messages.add(it) } checkDeclarationsMovingOutOfScope(enclosingDeclaration!!, controlFlow)?.let { messages.add(it) }
val functionNameValidator = val functionNameValidator =
JetNameValidatorImpl( JetNameValidatorImpl(
@@ -0,0 +1,13 @@
class T(val n: Int)
// SIBLING:
fun foo() {
<selection>bar { t ->
val k = 1
t.n + k + 1
}</selection>
}
fun bar(f: (T) -> Int) {
}
@@ -0,0 +1,17 @@
class T(val n: Int)
// SIBLING:
fun foo() {
unit()
}
fun unit() {
bar { t ->
val k = 1
t.n + k + 1
}
}
fun bar(f: (T) -> Int) {
}
@@ -0,0 +1,9 @@
class T(val n: Int)
// SIBLING:
fun foo() {
<selection>if (true) {
val k = 1
T().n + k + 1
}</selection>
}
@@ -0,0 +1,13 @@
class T(val n: Int)
// SIBLING:
fun foo() {
unit()
}
fun unit() {
if (true) {
val k = 1
T().n + k + 1
}
}
@@ -590,6 +590,16 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/controlFlow/outputValues/singleOutputValueWithWhenElse.kt"); doExtractFunctionTest("idea/testData/refactoring/extractFunction/controlFlow/outputValues/singleOutputValueWithWhenElse.kt");
} }
@TestMetadata("valuesUsedInLambdaOnly.kt")
public void testValuesUsedInLambdaOnly() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/controlFlow/outputValues/valuesUsedInLambdaOnly.kt");
}
@TestMetadata("valuesUsedInNestedBlock.kt")
public void testValuesUsedInNestedBlock() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/controlFlow/outputValues/valuesUsedInNestedBlock.kt");
}
} }
@TestMetadata("idea/testData/refactoring/extractFunction/controlFlow/throws") @TestMetadata("idea/testData/refactoring/extractFunction/controlFlow/throws")