From 59325900dfdfab93b2c0ea2d1b3a61438b066644 Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Wed, 19 Jun 2019 18:47:25 +0700 Subject: [PATCH] KotlinInvalidBundleOrPropertyInspection: fix false positive for a bundle with several properties files #KT-31359 Fixed --- ...KotlinInvalidBundleOrPropertyInspection.kt | 20 +++++++++---------- .../before/kMessage.kt | 1 + .../before/lang_de.properties | 1 + .../before/lang_en.properties | 2 ++ .../before/unresolvedPropertyReference.kt | 1 + .../before/validPropertyKeys.kt | 2 ++ .../invalidBundleOrProperty/expected.xml | 16 +++++++++++++++ 7 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 idea/testData/multiFileInspections/invalidBundleOrProperty/before/lang_de.properties create mode 100644 idea/testData/multiFileInspections/invalidBundleOrProperty/before/lang_en.properties diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/i18n/inspections/KotlinInvalidBundleOrPropertyInspection.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/i18n/inspections/KotlinInvalidBundleOrPropertyInspection.kt index 5ccbb1ef9ec..0016407dc78 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/i18n/inspections/KotlinInvalidBundleOrPropertyInspection.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/i18n/inspections/KotlinInvalidBundleOrPropertyInspection.kt @@ -35,23 +35,23 @@ import org.jetbrains.kotlin.resolve.calls.model.VarargValueArgument import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull class KotlinInvalidBundleOrPropertyInspection : AbstractKotlinInspection() { - override fun getDisplayName() = CodeInsightBundle.message("inspection.unresolved.property.key.reference.name") + override fun getDisplayName(): String = CodeInsightBundle.message("inspection.unresolved.property.key.reference.name") override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { return object : KtVisitorVoid() { private fun processResourceBundleReference(ref: ResourceBundleReference, template: KtStringTemplateExpression) { - if (ref.resolve() == null) { + if (ref.multiResolve(true).isEmpty()) { holder.registerProblem( - template, - CodeInsightBundle.message("inspection.invalid.resource.bundle.reference", ref.canonicalText), - ProblemHighlightType.LIKE_UNKNOWN_SYMBOL, - TextRange(0, template.textLength) + template, + CodeInsightBundle.message("inspection.invalid.resource.bundle.reference", ref.canonicalText), + ProblemHighlightType.LIKE_UNKNOWN_SYMBOL, + TextRange(0, template.textLength) ) } } private fun processPropertyReference(ref: PropertyReference, template: KtStringTemplateExpression) { - val property = ref.resolve() as? Property + val property = ref.multiResolve(true).firstOrNull()?.element as? Property if (property == null) { if (!ref.isSoft) { holder.registerProblem( @@ -66,7 +66,7 @@ class KotlinInvalidBundleOrPropertyInspection : AbstractKotlinInspection() { } val argument = template.parents.firstIsInstanceOrNull() ?: return - if (argument.getArgumentExpression() != KtPsiUtil.deparenthesize(template) ) return + if (argument.getArgumentExpression() != KtPsiUtil.deparenthesize(template)) return val callExpression = argument.getStrictParentOfType() ?: return val resolvedCall = callExpression.resolveToCall() ?: return @@ -85,8 +85,8 @@ class KotlinInvalidBundleOrPropertyInspection : AbstractKotlinInspection() { val actualArgumentCount = messageArgument.arguments.size if (actualArgumentCount < expectedArgumentCount) { val description = CodeInsightBundle.message( - "property.has.more.parameters.than.passed", - ref.canonicalText, expectedArgumentCount, actualArgumentCount + "property.has.more.parameters.than.passed", + ref.canonicalText, expectedArgumentCount, actualArgumentCount ) holder.registerProblem(template, description, ProblemHighlightType.GENERIC_ERROR) } diff --git a/idea/testData/multiFileInspections/invalidBundleOrProperty/before/kMessage.kt b/idea/testData/multiFileInspections/invalidBundleOrProperty/before/kMessage.kt index 6323c3dc9f5..33f0b92f1c8 100644 --- a/idea/testData/multiFileInspections/invalidBundleOrProperty/before/kMessage.kt +++ b/idea/testData/multiFileInspections/invalidBundleOrProperty/before/kMessage.kt @@ -3,4 +3,5 @@ import org.jetbrains.annotations.PropertyKey object K { fun message(@PropertyKey(resourceBundle = "TestBundle") key: String, vararg args: Any) = key fun message2(@PropertyKey(resourceBundle = "TestBundle") key: String, n: Int, vararg args: Any) = key + fun message3(@PropertyKey(resourceBundle = "lang") key: String) = key } \ No newline at end of file diff --git a/idea/testData/multiFileInspections/invalidBundleOrProperty/before/lang_de.properties b/idea/testData/multiFileInspections/invalidBundleOrProperty/before/lang_de.properties new file mode 100644 index 00000000000..484dba33e0a --- /dev/null +++ b/idea/testData/multiFileInspections/invalidBundleOrProperty/before/lang_de.properties @@ -0,0 +1 @@ +test=de \ No newline at end of file diff --git a/idea/testData/multiFileInspections/invalidBundleOrProperty/before/lang_en.properties b/idea/testData/multiFileInspections/invalidBundleOrProperty/before/lang_en.properties new file mode 100644 index 00000000000..190c044f143 --- /dev/null +++ b/idea/testData/multiFileInspections/invalidBundleOrProperty/before/lang_en.properties @@ -0,0 +1,2 @@ +test=en +tests=eee \ No newline at end of file diff --git a/idea/testData/multiFileInspections/invalidBundleOrProperty/before/unresolvedPropertyReference.kt b/idea/testData/multiFileInspections/invalidBundleOrProperty/before/unresolvedPropertyReference.kt index cac6edbf9b7..d220cea69af 100644 --- a/idea/testData/multiFileInspections/invalidBundleOrProperty/before/unresolvedPropertyReference.kt +++ b/idea/testData/multiFileInspections/invalidBundleOrProperty/before/unresolvedPropertyReference.kt @@ -3,4 +3,5 @@ fun test() { K.message("foo.baz") J.message("foo.baz", "arg") J.message("foo.baz") + K.message3("www") } \ No newline at end of file diff --git a/idea/testData/multiFileInspections/invalidBundleOrProperty/before/validPropertyKeys.kt b/idea/testData/multiFileInspections/invalidBundleOrProperty/before/validPropertyKeys.kt index b9c2e1523cc..ae8cae15519 100644 --- a/idea/testData/multiFileInspections/invalidBundleOrProperty/before/validPropertyKeys.kt +++ b/idea/testData/multiFileInspections/invalidBundleOrProperty/before/validPropertyKeys.kt @@ -3,4 +3,6 @@ fun test() { K.message("foo.bar", "arg1", "arg2") J.message("foo.bar", "arg") J.message("foo.bar", "arg1", "arg2") + K.message3("test") + K.message3("tests") } \ No newline at end of file diff --git a/idea/testData/multiFileInspections/invalidBundleOrProperty/expected.xml b/idea/testData/multiFileInspections/invalidBundleOrProperty/expected.xml index aa160f3a396..26a89738ca6 100644 --- a/idea/testData/multiFileInspections/invalidBundleOrProperty/expected.xml +++ b/idea/testData/multiFileInspections/invalidBundleOrProperty/expected.xml @@ -70,4 +70,20 @@ Invalid property key Invalid resource bundle reference 'TestBundle2' + + unresolvedPropertyReference.kt + 6 + testInvalidBundleOrProperty_InvalidBundleOrProperty_0 + + Invalid property key + String literal 'www' doesn't appear to be valid property key + + + unresolvedBundleReference.kt + 5 + testInvalidBundleOrProperty_InvalidBundleOrProperty_0 + + Invalid property key + Invalid resource bundle reference 'TestBundle2' +