Skip to content

Commit 8fa8ce4

Browse files
Edsilva1cirras
authored andcommitted
Add an ignore list to MixedNames
1 parent 67a9797 commit 8fa8ce4

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- `RedundantJump` analysis rule, which flags redundant jump statements, e.g., `Continue`, `Exit`.
1313
- `LoopExecutingAtMostOnce` analysis rule, which flags loop statements that can execute at most once.
14+
- `excludedNames` rule property to the `MixedNames` rule.
1415
- **API:** `RepeatStatementNode::getGuardExpression` method.
1516
- **API:** `RepeatStatementNode::getStatementList` method.
1617
- **API:** `CaseStatementNode::getSelectorExpression` method.

delphi-checks/src/main/java/au/com/integradev/delphi/checks/MixedNamesCheck.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
*/
2323
package au.com.integradev.delphi.checks;
2424

25+
import com.google.common.base.Splitter;
26+
import com.google.common.collect.ImmutableSortedSet;
2527
import java.util.List;
28+
import java.util.Set;
2629
import org.apache.commons.lang3.StringUtils;
2730
import org.sonar.check.Rule;
31+
import org.sonar.check.RuleProperty;
2832
import org.sonar.plugins.communitydelphi.api.ast.ArgumentListNode;
2933
import org.sonar.plugins.communitydelphi.api.ast.AttributeNode;
3034
import org.sonar.plugins.communitydelphi.api.ast.DelphiNode;
@@ -48,12 +52,30 @@ public class MixedNamesCheck extends DelphiCheck {
4852
private static final String MESSAGE = "Avoid mixing names (found: \"%s\" expected: \"%s\").";
4953
private static final String QUICK_FIX_MESSAGE = "Correct to \"%s\"";
5054

55+
private Set<String> excludedSet;
56+
57+
@RuleProperty(
58+
key = "excludedNames",
59+
description = "List of names to ignore, separated by a comma.")
60+
public String excludedNames = "";
61+
62+
@Override
63+
public void start(DelphiCheckContext context) {
64+
excludedSet =
65+
ImmutableSortedSet.copyOf(
66+
String.CASE_INSENSITIVE_ORDER, Splitter.on(',').trimResults().split(excludedNames));
67+
}
68+
5169
@Override
5270
public DelphiCheckContext visit(NameReferenceNode reference, DelphiCheckContext context) {
5371
NameDeclaration declaration = reference.getNameDeclaration();
5472
NameOccurrence occurrence = reference.getNameOccurrence();
5573

5674
if (declaration != null) {
75+
if (excludedSet.contains(occurrence.getImage())) {
76+
return context;
77+
}
78+
5779
if (declaration instanceof UnitImportNameDeclaration) {
5880
// Checks the occurrence against the original unit declaration instead of the import
5981
// declaration

delphi-checks/src/test/java/au/com/integradev/delphi/checks/MixedNamesCheckTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,47 @@
2323
import org.junit.jupiter.api.Test;
2424
import org.junit.jupiter.params.ParameterizedTest;
2525
import org.junit.jupiter.params.provider.ValueSource;
26+
import org.sonar.plugins.communitydelphi.api.check.DelphiCheck;
2627

2728
class MixedNamesCheckTest {
29+
private static DelphiCheck createCheck() {
30+
MixedNamesCheck check = new MixedNamesCheck();
31+
check.excludedNames = "Foo,Bar";
32+
return check;
33+
}
34+
35+
@Test
36+
void testNamesInExcludedListShouldNotAddIssue() {
37+
CheckVerifier.newVerifier()
38+
.withCheck(createCheck())
39+
.onFile(
40+
new DelphiTestUnitBuilder()
41+
.appendImpl("procedure Test;")
42+
.appendImpl("var")
43+
.appendImpl(" Foo: Boolean;")
44+
.appendImpl(" Bar: Boolean;")
45+
.appendImpl("begin")
46+
.appendImpl(" foo := True;")
47+
.appendImpl(" BAR := True;")
48+
.appendImpl("end;"))
49+
.verifyNoIssues();
50+
}
51+
52+
@Test
53+
void testDifferentlyCasedNameFromExcludedListShouldNotAddIssue() {
54+
CheckVerifier.newVerifier()
55+
.withCheck(createCheck())
56+
.onFile(
57+
new DelphiTestUnitBuilder()
58+
.appendImpl("procedure Test;")
59+
.appendImpl("var")
60+
.appendImpl(" FOO: Boolean;")
61+
.appendImpl("begin")
62+
.appendImpl(" foo := True;")
63+
.appendImpl("end;"))
64+
.verifyNoIssues();
65+
}
66+
2867
@Test
2968
void testMatchingVarNamesShouldNotAddIssue() {
3069
CheckVerifier.newVerifier()

0 commit comments

Comments
 (0)