Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `RedundantJump` analysis rule, which flags redundant jump statements, e.g., `Continue`, `Exit`.
- `LoopExecutingAtMostOnce` analysis rule, which flags loop statements that can execute at most once.
- `excludedNames` rule property to the `MixedNames` rule.
- **API:** `RepeatStatementNode::getGuardExpression` method.
- **API:** `RepeatStatementNode::getStatementList` method.
- **API:** `CaseStatementNode::getSelectorExpression` method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
*/
package au.com.integradev.delphi.checks;

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

private Set<String> excludedSet;

@RuleProperty(
key = "excludedNames",
description = "List of names to ignore, separated by a comma.")
public String excludedNames = "";

@Override
public void start(DelphiCheckContext context) {
excludedSet =
ImmutableSortedSet.copyOf(
String.CASE_INSENSITIVE_ORDER, Splitter.on(',').trimResults().split(excludedNames));
}

@Override
public DelphiCheckContext visit(NameReferenceNode reference, DelphiCheckContext context) {
NameDeclaration declaration = reference.getNameDeclaration();
NameOccurrence occurrence = reference.getNameOccurrence();

if (declaration != null) {
if (excludedSet.contains(occurrence.getImage())) {
return context;
}

if (declaration instanceof UnitImportNameDeclaration) {
// Checks the occurrence against the original unit declaration instead of the import
// declaration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,47 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.sonar.plugins.communitydelphi.api.check.DelphiCheck;

class MixedNamesCheckTest {
private static DelphiCheck createCheck() {
MixedNamesCheck check = new MixedNamesCheck();
check.excludedNames = "Foo,Bar";
return check;
}

@Test
void testNamesInExcludedListShouldNotAddIssue() {
CheckVerifier.newVerifier()
.withCheck(createCheck())
.onFile(
new DelphiTestUnitBuilder()
.appendImpl("procedure Test;")
.appendImpl("var")
.appendImpl(" Foo: Boolean;")
.appendImpl(" Bar: Boolean;")
.appendImpl("begin")
.appendImpl(" foo := True;")
.appendImpl(" BAR := True;")
.appendImpl("end;"))
.verifyNoIssues();
}

@Test
void testDifferentlyCasedNameFromExcludedListShouldNotAddIssue() {
CheckVerifier.newVerifier()
.withCheck(createCheck())
.onFile(
new DelphiTestUnitBuilder()
.appendImpl("procedure Test;")
.appendImpl("var")
.appendImpl(" FOO: Boolean;")
.appendImpl("begin")
.appendImpl(" foo := True;")
.appendImpl("end;"))
.verifyNoIssues();
}

@Test
void testMatchingVarNamesShouldNotAddIssue() {
CheckVerifier.newVerifier()
Expand Down