IG: new "Lambda parameter naming convention" inspection

This commit is contained in:
Bas Leijdekkers
2015-05-13 16:47:49 +02:00
parent a3fe6000e8
commit 244f808a8d
6 changed files with 199 additions and 2 deletions
@@ -2159,5 +2159,10 @@ array.objects.equals.display.name='Objects.equals()' called on arrays
array.objects.equals.problem.descriptor=<code>Objects.#ref()</code> on arrays should probably be 'Arrays.equals()' #loc
array.objects.deep.equals.problem.descriptor=<code>Objects.#ref()</code> on arrays should probably be 'Arrays.deepEquals()' #loc
extends.throwable.display.name=Class directly extends 'java.lang.Throwable'
anonymous.extends.throwable.problem.descriptor=Anonymous class directly extends 'java.lang.Throwable' \#loc
extends.throwable.problem.descriptor=class <code>\#ref</code> directly extends 'java.lang.Throwable' \#loc
anonymous.extends.throwable.problem.descriptor=Anonymous class directly extends 'java.lang.Throwable' #loc
extends.throwable.problem.descriptor=class <code>#ref</code> directly extends 'java.lang.Throwable' #loc
lambda.parameter.naming.convention.display.name=Lambda parameter naming convention
lambda.parameter.naming.convention.problem.descriptor.short=Lambda parameter name <code>#ref</code> is too short #loc
lambda.parameter.naming.convention.problem.descriptor.long=Lambda parameter name <code>#ref</code> is too long #loc
lambda.parameter.naming.convention.problem.descriptor.regex.mismatch=Lambda parameter name <code>#ref</code> doesn''t match regex ''{0}'' #loc
@@ -0,0 +1,98 @@
/*
* Copyright 2000-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 com.siyeh.ig.naming;
import com.intellij.psi.*;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspectionVisitor;
import org.jetbrains.annotations.NotNull;
/**
* @author Bas Leijdekkers
*/
public class LambdaParameterNamingConventionInspectionBase extends ConventionInspection {
private static final int DEFAULT_MIN_LENGTH = 1;
private static final int DEFAULT_MAX_LENGTH = 20;
@Override
@NotNull
public String getID() {
return "LambdaParameterNamingConvention";
}
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message("lambda.parameter.naming.convention.display.name");
}
@Override
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
return true;
}
@Override
@NotNull
public String buildErrorString(Object... infos) {
final String parameterName = (String)infos[0];
if (parameterName.length() < getMinLength()) {
return InspectionGadgetsBundle.message("lambda.parameter.naming.convention.problem.descriptor.short");
}
else if (parameterName.length() > getMaxLength()) {
return InspectionGadgetsBundle.message("lambda.parameter.naming.convention.problem.descriptor.long");
}
else {
return InspectionGadgetsBundle.message("lambda.parameter.naming.convention.problem.descriptor.regex.mismatch", getRegex());
}
}
@Override
protected String getDefaultRegex() {
return "[a-z][A-Za-z\\d]*";
}
@Override
protected int getDefaultMinLength() {
return DEFAULT_MIN_LENGTH;
}
@Override
protected int getDefaultMaxLength() {
return DEFAULT_MAX_LENGTH;
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new NamingConventionsVisitor();
}
private class NamingConventionsVisitor extends BaseInspectionVisitor {
@Override
public void visitParameter(@NotNull PsiParameter variable) {
final PsiElement scope = variable.getDeclarationScope();
if (!(scope instanceof PsiLambdaExpression)) {
return;
}
final String name = variable.getName();
if (name == null || isValid(name)) {
return;
}
registerVariableError(variable, name);
}
}
}
@@ -0,0 +1,30 @@
/*
* Copyright 2003-2015 Dave Griffith, Bas Leijdekkers
*
* 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 com.siyeh.ig.naming;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.RenameFix;
/**
* @author Bas Leijdekkers
*/
public class LambdaParameterNamingConventionInspection extends LambdaParameterNamingConventionInspectionBase {
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
return new RenameFix();
}
}
@@ -0,0 +1,13 @@
<html>
<body>
Reports lambda parameters whose names are either too short, too long, or do not follow
the specified regular expression pattern.
<!-- tooltip end -->
<p>
Use the fields below to specify minimum length, maximum length and regular expression expected for method parameter names.
Specify <b>0</b> to not check the length of names. Regular expressions are in standard <b>java.util.regex</b> format.
<p>
<small>New in 15</small>
<p>
</body>
</html>
@@ -0,0 +1,12 @@
public class LambdaParameterNamingConvention {
void m(int a) {}
void n(int abcd) {
F f = (<warning descr="Lambda parameter name 'i' is too short">i</warning>) -> 10;
F g = abc -> 12;
}
interface F {
int a(int i);
}
}
@@ -0,0 +1,39 @@
/*
* Copyright 2000-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 com.siyeh.ig.naming;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.siyeh.ig.LightInspectionTestCase;
import junit.framework.TestCase;
import org.jetbrains.annotations.Nullable;
/**
* @author Bas Leijdekkers
*/
public class LambdaParameterNamingConventionInspectionTest extends LightInspectionTestCase {
public void testLambdaParameterNamingConvention() {
doTest();
}
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
final LambdaParameterNamingConventionInspection inspection = new LambdaParameterNamingConventionInspection();
inspection.m_minLength = 2;
return inspection;
}
}