KotlinInvalidBundleOrPropertyInspection: fix false positive for a bundle with several properties files

#KT-31359 Fixed
This commit is contained in:
Dmitry Gridin
2019-06-19 18:47:25 +07:00
parent d9808b70b6
commit 59325900df
7 changed files with 33 additions and 10 deletions
@@ -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<KtValueArgument>() ?: return
if (argument.getArgumentExpression() != KtPsiUtil.deparenthesize(template) ) return
if (argument.getArgumentExpression() != KtPsiUtil.deparenthesize(template)) return
val callExpression = argument.getStrictParentOfType<KtCallExpression>() ?: 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)
}
@@ -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
}
@@ -0,0 +1 @@
test=de
@@ -0,0 +1,2 @@
test=en
tests=eee
@@ -3,4 +3,5 @@ fun test() {
K.message("foo.baz")
J.message("foo.baz", "arg")
J.message("foo.baz")
K.message3("www")
}
@@ -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")
}
@@ -70,4 +70,20 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Invalid property key</problem_class>
<description>Invalid resource bundle reference 'TestBundle2'</description>
</problem>
<problem>
<file>unresolvedPropertyReference.kt</file>
<line>6</line>
<module>testInvalidBundleOrProperty_InvalidBundleOrProperty_0</module>
<entry_point TYPE="method" FQNAME="UnresolvedPropertyReferenceKt void test()"/>
<problem_class severity="ERROR" attribute_key="WRONG_REFERENCES_ATTRIBUTES">Invalid property key</problem_class>
<description>String literal 'www' doesn't appear to be valid property key</description>
</problem>
<problem>
<file>unresolvedBundleReference.kt</file>
<line>5</line>
<module>testInvalidBundleOrProperty_InvalidBundleOrProperty_0</module>
<entry_point TYPE="method" FQNAME="UnresolvedBundleReferenceKt java.lang.String message(java.lang.String key, java.lang.Object... args)" />
<problem_class severity="ERROR" attribute_key="WRONG_REFERENCES_ATTRIBUTES">Invalid property key</problem_class>
<description>Invalid resource bundle reference 'TestBundle2'</description>
</problem>
</problems>