From 328607a3ab85f935abe9b3fdb3ef690e9665ee3d Mon Sep 17 00:00:00 2001 From: Zack Grannan Date: Sun, 30 Mar 2014 21:20:13 -0700 Subject: [PATCH] Added ifThenToElvisInspection Conflicts: idea/src/META-INF/plugin.xml --- .../inspectionDescriptions/IfThenToElvis.html | 5 ++ idea/src/META-INF/plugin.xml | 20 +++-- .../inspections/IfThenToElvisInspection.kt | 22 +++++ .../ifThenToElvis/inspectionData/expected.xml | 82 +++++++++++++++++++ .../inspectionData/inspections.test | 1 + .../JetInspectionTestGenerated.java | 5 ++ .../CodeTransformationTestGenerated.java | 8 +- 7 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 idea/resources/inspectionDescriptions/IfThenToElvis.html create mode 100644 idea/src/org/jetbrains/jet/plugin/inspections/IfThenToElvisInspection.kt create mode 100644 idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml create mode 100644 idea/testData/intentions/branched/ifThenToElvis/inspectionData/inspections.test diff --git a/idea/resources/inspectionDescriptions/IfThenToElvis.html b/idea/resources/inspectionDescriptions/IfThenToElvis.html new file mode 100644 index 00000000000..ca092bf5b45 --- /dev/null +++ b/idea/resources/inspectionDescriptions/IfThenToElvis.html @@ -0,0 +1,5 @@ + + +This inspection reports any if-then expressions that can be folded into elvis (?:) expressions + + diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index ae90fef2ed3..62e41feb49d 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -638,12 +638,6 @@ Kotlin - org.jetbrains.jet.plugin.intentions.SwapBinaryExpression @@ -665,6 +659,20 @@ Kotlin + + + + diff --git a/idea/src/org/jetbrains/jet/plugin/inspections/IfThenToElvisInspection.kt b/idea/src/org/jetbrains/jet/plugin/inspections/IfThenToElvisInspection.kt new file mode 100644 index 00000000000..b61fac4a93d --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/inspections/IfThenToElvisInspection.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.inspections + +import org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.IfThenToElvisIntention +import org.jetbrains.jet.lang.psi.JetIfExpression + +public class IfThenToElvisInspection : IntentionBasedInspection(IfThenToElvisIntention()) diff --git a/idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml b/idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml new file mode 100644 index 00000000000..2198f4442e1 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml @@ -0,0 +1,82 @@ + + + willNotInlineClassProperty.kt + 3 + light_idea_test_case + + If-Then foldable to '?:'. + Replace 'if' expression with elvis expression + + + rhsNotEqualsNull.kt + 9 + light_idea_test_case + + If-Then foldable to '?:'. + Replace 'if' expression with elvis expression + + + rhsEqualsNull.kt + 8 + light_idea_test_case + + If-Then foldable to '?:'. + Replace 'if' expression with elvis expression + + + lhsNotEqualsNull.kt + 8 + light_idea_test_case + + If-Then foldable to '?:'. + Replace 'if' expression with elvis expression + + + lhsEqualsNull.kt + 8 + light_idea_test_case + + If-Then foldable to '?:'. + Replace 'if' expression with elvis expression + + + ifAsExpression.kt + 8 + light_idea_test_case + + If-Then foldable to '?:'. + Replace 'if' expression with elvis expression + + + ifAndElseNotInBlocks.kt + 8 + light_idea_test_case + + If-Then foldable to '?:'. + Replace 'if' expression with elvis expression + + + ifAndElseBothInBlocks.kt + 8 + light_idea_test_case + + If-Then foldable to '?:'. + Replace 'if' expression with elvis expression + + + doesNotinlineValueOutsideOfScope.kt + 8 + light_idea_test_case + + If-Then foldable to '?:'. + Replace 'if' expression with elvis expression + + + doesNotinlineValueIfUsedMoreThanOnce.kt + 11 + light_idea_test_case + + If-Then foldable to '?:'. + Replace 'if' expression with elvis expression + + diff --git a/idea/testData/intentions/branched/ifThenToElvis/inspectionData/inspections.test b/idea/testData/intentions/branched/ifThenToElvis/inspectionData/inspections.test new file mode 100644 index 00000000000..4a2fe005169 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/inspectionData/inspections.test @@ -0,0 +1 @@ +// INSPECTION_CLASS: org.jetbrains.jet.plugin.inspections.IfThenToElvisInspection diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/JetInspectionTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/JetInspectionTestGenerated.java index 8c31f4c530e..42f1f5651e5 100644 --- a/idea/tests/org/jetbrains/jet/plugin/codeInsight/JetInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/JetInspectionTestGenerated.java @@ -41,4 +41,9 @@ public class JetInspectionTestGenerated extends AbstractJetInspectionTest { doTest("idea/testData/intentions/attributeCallReplacements/replaceGetIntention/inspectionData/inspections.test"); } + @TestMetadata("branched/ifThenToElvis/inspectionData/inspections.test") + public void testBranched_ifThenToElvis_inspectionData_Inspections_test() throws Exception { + doTest("idea/testData/intentions/branched/ifThenToElvis/inspectionData/inspections.test"); + } + } diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java index 00ab17613f1..78569dc468a 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java @@ -96,6 +96,7 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT } @TestMetadata("idea/testData/intentions/branched/ifThenToElvis") + @InnerTestClasses({}) public static class IfThenToElvis extends AbstractCodeTransformationTest { public void testAllFilesPresentInIfThenToElvis() throws Exception { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/branched/ifThenToElvis"), Pattern.compile("^(.+)\\.kt$"), true); @@ -236,6 +237,11 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT doTestIfThenToElvis("idea/testData/intentions/branched/ifThenToElvis/willNotInlineClassProperty.kt"); } + public static Test innerSuite() { + TestSuite suite = new TestSuite("IfThenToElvis"); + suite.addTestSuite(IfThenToElvis.class); + return suite; + } } @TestMetadata("idea/testData/intentions/branched/safeAccessToIfThen") @@ -3418,7 +3424,7 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT public static Test suite() { TestSuite suite = new TestSuite("CodeTransformationTestGenerated"); suite.addTestSuite(ElvisToIfThen.class); - suite.addTestSuite(IfThenToElvis.class); + suite.addTest(IfThenToElvis.innerSuite()); suite.addTestSuite(SafeAccessToIfThen.class); suite.addTestSuite(IfThenToSafeAccess.class); suite.addTestSuite(IfToAssignment.class);