Resource Bundles: Inspections on invalid property key/resource bundle references
#KT-6946 In Progress
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
This inspection verifies that arguments passed to functions with parameters annotated as @PropertyKey are valid property keys in the respective properties files. It also verifies that the resourceBundle argument of the @PropertyKey annotation is an existing resource bundle.
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -1191,6 +1191,13 @@
|
|||||||
level="WARNING"
|
level="WARNING"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInvalidBundleOrPropertyInspection"
|
||||||
|
displayName="Invalid property key"
|
||||||
|
groupName="Kotlin"
|
||||||
|
enabledByDefault="true"
|
||||||
|
level="ERROR"
|
||||||
|
/>
|
||||||
|
|
||||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||||
|
|
||||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||||
|
|||||||
+104
@@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2015 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.kotlin.idea.inspections
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.CodeInsightBundle
|
||||||
|
import com.intellij.codeInspection.ProblemHighlightType
|
||||||
|
import com.intellij.codeInspection.ProblemsHolder
|
||||||
|
import com.intellij.codeInspection.i18n.JavaI18nUtil
|
||||||
|
import com.intellij.lang.properties.ResourceBundleReference
|
||||||
|
import com.intellij.lang.properties.psi.Property
|
||||||
|
import com.intellij.lang.properties.references.PropertyReference
|
||||||
|
import com.intellij.openapi.util.TextRange
|
||||||
|
import com.intellij.psi.PsiElementVisitor
|
||||||
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.parents
|
||||||
|
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||||
|
import org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument
|
||||||
|
import org.jetbrains.kotlin.resolve.calls.model.VarargValueArgument
|
||||||
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||||
|
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
||||||
|
|
||||||
|
class KotlinInvalidBundleOrPropertyInspection : AbstractKotlinInspection() {
|
||||||
|
override fun getDisplayName() = CodeInsightBundle.message("inspection.unresolved.property.key.reference.name")
|
||||||
|
|
||||||
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||||
|
return object : JetVisitorVoid() {
|
||||||
|
private fun processResourceBundleReference(ref: ResourceBundleReference, template: JetStringTemplateExpression) {
|
||||||
|
if (ref.resolve() == null) {
|
||||||
|
holder.registerProblem(
|
||||||
|
template,
|
||||||
|
CodeInsightBundle.message("inspection.invalid.resource.bundle.reference", ref.canonicalText),
|
||||||
|
ProblemHighlightType.LIKE_UNKNOWN_SYMBOL,
|
||||||
|
TextRange(0, template.textLength)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun processPropertyReference(ref: PropertyReference, template: JetStringTemplateExpression) {
|
||||||
|
val property = ref.resolve() as? Property
|
||||||
|
if (property == null) {
|
||||||
|
holder.registerProblem(
|
||||||
|
template,
|
||||||
|
CodeInsightBundle.message("inspection.unresolved.property.key.reference.message", ref.canonicalText),
|
||||||
|
ProblemHighlightType.LIKE_UNKNOWN_SYMBOL,
|
||||||
|
TextRange(0, template.textLength),
|
||||||
|
*ref.quickFixes
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val argument = template.parents.firstIsInstanceOrNull<JetValueArgument>() ?: return
|
||||||
|
if (argument.getArgumentExpression() != JetPsiUtil.deparenthesize(template) ) return
|
||||||
|
|
||||||
|
val callExpression = argument.getStrictParentOfType<JetCallExpression>() ?: return
|
||||||
|
val resolvedCall = callExpression.getResolvedCall(callExpression.analyze(BodyResolveMode.PARTIAL)) ?: return
|
||||||
|
|
||||||
|
val resolvedArguments = resolvedCall.valueArgumentsByIndex ?: return
|
||||||
|
val keyArgumentIndex = resolvedArguments.indexOfFirst { it is ExpressionValueArgument && it.valueArgument == argument }
|
||||||
|
if (keyArgumentIndex < 0) return
|
||||||
|
|
||||||
|
val callable = resolvedCall.resultingDescriptor
|
||||||
|
if (callable.valueParameters.size() != keyArgumentIndex + 2) return
|
||||||
|
|
||||||
|
val messageArgument = resolvedArguments[keyArgumentIndex + 1] as? VarargValueArgument ?: return
|
||||||
|
if (messageArgument.arguments.singleOrNull()?.getSpreadElement() != null) return
|
||||||
|
|
||||||
|
val expectedArgumentCount = JavaI18nUtil.getPropertyValuePlaceholdersCount(property.value ?: "")
|
||||||
|
val actualArgumentCount = messageArgument.arguments.size()
|
||||||
|
if (actualArgumentCount < expectedArgumentCount) {
|
||||||
|
val description = CodeInsightBundle.message(
|
||||||
|
"property.has.more.parameters.than.passed",
|
||||||
|
ref.canonicalText, expectedArgumentCount, actualArgumentCount
|
||||||
|
)
|
||||||
|
holder.registerProblem(template, description, ProblemHighlightType.GENERIC_ERROR)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun visitStringTemplateExpression(expression: JetStringTemplateExpression) {
|
||||||
|
for (ref in expression.references) {
|
||||||
|
when (ref) {
|
||||||
|
is ResourceBundleReference -> processResourceBundleReference(ref, expression)
|
||||||
|
is PropertyReference -> processPropertyReference(ref, expression)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
package org.jetbrains.annotations
|
||||||
|
|
||||||
|
@Retention(AnnotationRetention.BINARY)
|
||||||
|
@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.LOCAL_VARIABLE, AnnotationTarget.FIELD)
|
||||||
|
public annotation class PropertyKey(public val resourceBundle: String)
|
||||||
+2
@@ -0,0 +1,2 @@
|
|||||||
|
foo.bar = test: {0}
|
||||||
|
foo.bar2 = test: {0}, {1}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
import org.jetbrains.annotations.PropertyKey;
|
||||||
|
|
||||||
|
class J {
|
||||||
|
static String message(@PropertyKey(resourceBundle = "TestBundle") String key, Object... args) {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
fun test() {
|
||||||
|
K.message("foo.bar")
|
||||||
|
J.message("foo.bar")
|
||||||
|
K.message(key = "foo.bar", args = 1)
|
||||||
|
K.message("foo.bar2", *arrayOf(1, 2))
|
||||||
|
K.message2("foo.bar", 1)
|
||||||
|
}
|
||||||
Vendored
+6
@@ -0,0 +1,6 @@
|
|||||||
|
import org.jetbrains.annotations.PropertyKey
|
||||||
|
|
||||||
|
private val TEST_BUNDLE2 = "TestBundle2"
|
||||||
|
|
||||||
|
fun message(@PropertyKey(resourceBundle = "TestBundle2") key: String, vararg args: Any) = key
|
||||||
|
fun message2(@PropertyKey(resourceBundle = TEST_BUNDLE2) key: String, vararg args: Any) = key
|
||||||
Vendored
+6
@@ -0,0 +1,6 @@
|
|||||||
|
fun test() {
|
||||||
|
K.message("foo.baz", "arg")
|
||||||
|
K.message("foo.baz")
|
||||||
|
J.message("foo.baz", "arg")
|
||||||
|
J.message("foo.baz")
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
fun test() {
|
||||||
|
K.message("foo.bar", "arg")
|
||||||
|
K.message("foo.bar", "arg1", "arg2")
|
||||||
|
J.message("foo.bar", "arg")
|
||||||
|
J.message("foo.bar", "arg1", "arg2")
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
<problems>
|
||||||
|
<problem>
|
||||||
|
<file>unresolvedPropertyReference.kt</file>
|
||||||
|
<line>2</line>
|
||||||
|
<module>testInvalidBundleOrProperty_InvalidBundleOrProperty_0</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="unresolvedPropertyReference.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Invalid property key</problem_class>
|
||||||
|
<description>String literal 'foo.baz' doesn't appear to be valid property key</description>
|
||||||
|
</problem>
|
||||||
|
|
||||||
|
<problem>
|
||||||
|
<file>unresolvedPropertyReference.kt</file>
|
||||||
|
<line>3</line>
|
||||||
|
<module>testInvalidBundleOrProperty_InvalidBundleOrProperty_0</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="unresolvedPropertyReference.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Invalid property key</problem_class>
|
||||||
|
<description>String literal 'foo.baz' doesn't appear to be valid property key</description>
|
||||||
|
</problem>
|
||||||
|
|
||||||
|
<problem>
|
||||||
|
<file>unresolvedPropertyReference.kt</file>
|
||||||
|
<line>4</line>
|
||||||
|
<module>testInvalidBundleOrProperty_InvalidBundleOrProperty_0</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="unresolvedPropertyReference.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Invalid property key</problem_class>
|
||||||
|
<description>String literal 'foo.baz' doesn't appear to be valid property key</description>
|
||||||
|
</problem>
|
||||||
|
|
||||||
|
<problem>
|
||||||
|
<file>unresolvedPropertyReference.kt</file>
|
||||||
|
<line>5</line>
|
||||||
|
<module>testInvalidBundleOrProperty_InvalidBundleOrProperty_0</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="unresolvedPropertyReference.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Invalid property key</problem_class>
|
||||||
|
<description>String literal 'foo.baz' doesn't appear to be valid property key</description>
|
||||||
|
</problem>
|
||||||
|
|
||||||
|
<problem>
|
||||||
|
<file>tooFewArguments.kt</file>
|
||||||
|
<line>2</line>
|
||||||
|
<module>testInvalidBundleOrProperty_InvalidBundleOrProperty_0</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="tooFewArguments.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Invalid property key</problem_class>
|
||||||
|
<description>Property 'foo.bar' expected 1 parameter, passed 0</description>
|
||||||
|
</problem>
|
||||||
|
|
||||||
|
<problem>
|
||||||
|
<file>tooFewArguments.kt</file>
|
||||||
|
<line>3</line>
|
||||||
|
<module>testInvalidBundleOrProperty_InvalidBundleOrProperty_0</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="tooFewArguments.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Invalid property key</problem_class>
|
||||||
|
<description>Property 'foo.bar' expected 1 parameter, passed 0</description>
|
||||||
|
</problem>
|
||||||
|
|
||||||
|
<problem>
|
||||||
|
<file>unresolvedBundleReference.kt</file>
|
||||||
|
<line>3</line>
|
||||||
|
<module>testInvalidBundleOrProperty_InvalidBundleOrProperty_0</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="unresolvedBundleReference.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Invalid property key</problem_class>
|
||||||
|
<description>Invalid resource bundle reference 'TestBundle2'</description>
|
||||||
|
</problem>
|
||||||
|
|
||||||
|
<problem>
|
||||||
|
<file>unresolvedBundleReference.kt</file>
|
||||||
|
<line>5</line>
|
||||||
|
<module>testInvalidBundleOrProperty_InvalidBundleOrProperty_0</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="unresolvedBundleReference.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Invalid property key</problem_class>
|
||||||
|
<description>Invalid resource bundle reference 'TestBundle2'</description>
|
||||||
|
</problem>
|
||||||
|
</problems>
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"inspectionClass": "org.jetbrains.kotlin.idea.inspections.KotlinInvalidBundleOrPropertyInspection",
|
||||||
|
"withRuntime": "true"
|
||||||
|
}
|
||||||
+6
@@ -35,6 +35,12 @@ public class JetMultiFileInspectionTestGenerated extends AbstractJetMultiFileIns
|
|||||||
JetTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File("idea/testData/multiFileInspections"), Pattern.compile("^(.+)\\.test$"));
|
JetTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File("idea/testData/multiFileInspections"), Pattern.compile("^(.+)\\.test$"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("invalidBundleOrProperty/invalidBundleOrProperty.test")
|
||||||
|
public void testInvalidBundleOrProperty_InvalidBundleOrProperty() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/multiFileInspections/invalidBundleOrProperty/invalidBundleOrProperty.test");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("mismatchedProjectAndDirectory/mismatchedProjectAndDirectory.test")
|
@TestMetadata("mismatchedProjectAndDirectory/mismatchedProjectAndDirectory.test")
|
||||||
public void testMismatchedProjectAndDirectory_MismatchedProjectAndDirectory() throws Exception {
|
public void testMismatchedProjectAndDirectory_MismatchedProjectAndDirectory() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/multiFileInspections/mismatchedProjectAndDirectory/mismatchedProjectAndDirectory.test");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/multiFileInspections/mismatchedProjectAndDirectory/mismatchedProjectAndDirectory.test");
|
||||||
|
|||||||
Reference in New Issue
Block a user