Do not interpret property string as MessageFormat if no format arguments were specified

Fixes IDEA-239069 "String ' is not properly capitalized" false positive

GitOrigin-RevId: 4fa1430f2f9131ce3a2561b7ac0b7e4f6dc92bbc
This commit is contained in:
Tagir Valeev
2020-05-04 07:36:17 +00:00
committed by intellij-monorepo-bot
parent dba42cc50f
commit f433a0c263
3 changed files with 20 additions and 15 deletions
@@ -95,7 +95,8 @@ public class TitleCapitalizationInspection extends AbstractBaseJavaLocalInspecti
return Value.of((PsiLiteralExpression)arg);
}
if (arg instanceof PsiMethodCallExpression) {
PsiMethod psiMethod = ((PsiMethodCallExpression)arg).resolveMethod();
PsiMethodCallExpression call = (PsiMethodCallExpression)arg;
PsiMethod psiMethod = call.resolveMethod();
PsiExpression returnValue = PropertyUtilBase.getGetterReturnExpression(psiMethod);
if (arg == returnValue) {
return null;
@@ -103,7 +104,7 @@ public class TitleCapitalizationInspection extends AbstractBaseJavaLocalInspecti
if (returnValue != null && processed.add(returnValue)) {
return getTitleValue(returnValue, processed);
}
return Value.of(getPropertyArgument((PsiMethodCallExpression)arg));
return Value.of(getPropertyArgument(call), call.getArgumentList().getExpressionCount() > 1);
}
if (arg instanceof PsiReferenceExpression) {
PsiElement result = ((PsiReferenceExpression)arg).resolve();
@@ -169,14 +170,14 @@ public class TitleCapitalizationInspection extends AbstractBaseJavaLocalInspecti
element.replace(newExpression);
}
else if (element instanceof PsiMethodCallExpression) {
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)element;
final PsiMethod method = methodCallExpression.resolveMethod();
final PsiMethodCallExpression call = (PsiMethodCallExpression)element;
final PsiMethod method = call.resolveMethod();
final PsiExpression returnValue = PropertyUtilBase.getGetterReturnExpression(method);
if (returnValue != null) {
doFix(project, returnValue);
}
final Property property = getPropertyArgument(methodCallExpression);
Value value = Value.of(property);
final Property property = getPropertyArgument(call);
Value value = Value.of(property, call.getArgumentList().getExpressionCount() > 1);
if (value == null) return;
property.setValue(value.fixCapitalization(myCapitalization));
}
@@ -211,19 +212,20 @@ public class TitleCapitalizationInspection extends AbstractBaseJavaLocalInspecti
default boolean canFix() { return true; }
@Contract("null -> null")
@Contract("null, _ -> null")
@Nullable
static Value of(@Nullable Property property) {
static Value of(@Nullable Property property, boolean useFormat) {
if (property == null) return null;
String value = property.getUnescapedValue();
if (value == null) return null;
try {
MessageFormat format = new MessageFormat(value);
return new PropertyValue(value, format);
}
catch (IllegalArgumentException e) {
return new TextValue(value);
if (useFormat) {
try {
MessageFormat format = new MessageFormat(value);
return new PropertyValue(value, format);
}
catch (IllegalArgumentException ignore) {}
}
return new TextValue(value);
}
@Nullable
@@ -29,5 +29,7 @@ class X {
test2(<warning descr="String 'Hello {0,choice,0#World|1#Universe}' is not properly capitalized. It should have sentence capitalization">message("property.choice.title", x)</warning>);
test2(<warning descr="String 'Hello {0,choice,0#World|1#universe}' is not properly capitalized. It should have sentence capitalization">message("property.choice.mixed", x)</warning>);
test2(message("property.choice.lower", x));
test2(message("property.sentence.with.quote"));
}
}
@@ -67,7 +67,8 @@ public class CapitalizationInspectionTest extends LightJavaCodeInsightFixtureTes
"property.parameterized=Hello {0}\n" +
"property.choice.title=Hello {0,choice,0#World|1#Universe}\n" +
"property.choice.mixed=Hello {0,choice,0#World|1#universe}\n" +
"property.choice.lower=Hello {0,choice,0#world|1#universe}\n";
"property.choice.lower=Hello {0,choice,0#world|1#universe}\n" +
"property.sentence.with.quote='return' is not allowed here";
myFixture.addFileToProject("MyBundle.properties", props);
doTest(false);
}