Skip to content

Commit c2a96ed

Browse files
committed
Fix FPs on wrapped type headers in VisibilityKeywordIndentation
1 parent 53f7d67 commit c2a96ed

File tree

3 files changed

+118
-11
lines changed

3 files changed

+118
-11
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- False positives around wrapped type declarations in `VisibilityKeywordIndentation`.
13+
1014
## [1.14.1] - 2025-03-05
1115

1216
### Fixed

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
import au.com.integradev.delphi.utils.IndentationUtils;
2222
import org.sonar.check.Rule;
23-
import org.sonar.plugins.communitydelphi.api.ast.DelphiNode;
23+
import org.sonar.plugins.communitydelphi.api.ast.NameDeclarationNode;
24+
import org.sonar.plugins.communitydelphi.api.ast.TypeDeclarationNode;
2425
import org.sonar.plugins.communitydelphi.api.ast.VisibilityNode;
2526
import org.sonar.plugins.communitydelphi.api.check.DelphiCheck;
2627
import org.sonar.plugins.communitydelphi.api.check.DelphiCheckContext;
@@ -32,19 +33,21 @@ public class VisibilityKeywordIndentationCheck extends DelphiCheck {
3233
private static final String MESSAGE =
3334
"Indent this visibility specifier to the indentation level of the containing type.";
3435

35-
private static String getExpectedIndentation(DelphiNode node) {
36-
var visibilityNode = (VisibilityNode) node;
37-
// Class/Record/etc. -> VisibilitySection -> Visibility
38-
var parent = visibilityNode.getParent().getParent();
39-
return IndentationUtils.getLineIndentation(parent);
40-
}
41-
4236
@Override
4337
public DelphiCheckContext visit(VisibilityNode visibilityNode, DelphiCheckContext context) {
44-
if (!IndentationUtils.getLineIndentation(visibilityNode)
45-
.equals(getExpectedIndentation(visibilityNode))) {
46-
reportIssue(context, visibilityNode, MESSAGE);
38+
var declaration = visibilityNode.getNthParent(3);
39+
40+
if (declaration instanceof TypeDeclarationNode) {
41+
NameDeclarationNode typeName = ((TypeDeclarationNode) declaration).getTypeNameNode();
42+
43+
String actual = IndentationUtils.getLineIndentation(visibilityNode);
44+
String expected = IndentationUtils.getLineIndentation(typeName);
45+
46+
if (!actual.equals(expected)) {
47+
reportIssue(context, visibilityNode, MESSAGE);
48+
}
4749
}
50+
4851
return super.visit(visibilityNode, context);
4952
}
5053
}

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,104 @@ void testUnindentedVisibilitySpecifierShouldAddIssue() {
9696
.appendDecl(" end;"))
9797
.verifyIssues();
9898
}
99+
100+
@Test
101+
void testIndentedVisibilitySpecifierWithWrappedClassDeclarationShouldNotAddIssue() {
102+
CheckVerifier.newVerifier()
103+
.withCheck(new VisibilityKeywordIndentationCheck())
104+
.onFile(
105+
new DelphiTestUnitBuilder()
106+
.appendDecl("type")
107+
.appendDecl(" TFoo =")
108+
.appendDecl(" class(TObject)")
109+
.appendDecl(" private")
110+
.appendDecl(" procedure Proc;")
111+
.appendDecl(" end;"))
112+
.verifyNoIssues();
113+
}
114+
115+
@Test
116+
void testIndentedVisibilitySpecifierWithMisalignedCustomAttributeShouldNotAddIssue() {
117+
CheckVerifier.newVerifier()
118+
.withCheck(new VisibilityKeywordIndentationCheck())
119+
.onFile(
120+
new DelphiTestUnitBuilder()
121+
.appendDecl("type")
122+
.appendDecl(" [Foo]")
123+
.appendDecl(" TBar = class(TObject)")
124+
.appendDecl(" private")
125+
.appendDecl(" procedure Baz;")
126+
.appendDecl(" end;"))
127+
.verifyNoIssues();
128+
}
129+
130+
@Test
131+
void testIndentedVisibilitySpecifierWithSameLineCustomAttributeShouldNotAddIssue() {
132+
CheckVerifier.newVerifier()
133+
.withCheck(new VisibilityKeywordIndentationCheck())
134+
.onFile(
135+
new DelphiTestUnitBuilder()
136+
.appendDecl("type")
137+
.appendDecl(" [Foo] TBar = class(TObject)")
138+
.appendDecl(" private")
139+
.appendDecl(" procedure Baz;")
140+
.appendDecl(" end;"))
141+
.verifyNoIssues();
142+
}
143+
144+
@Test
145+
void testRightIndentedVisibilitySpecifierInVarAnonymousRecordShouldNotAddIssue() {
146+
CheckVerifier.newVerifier()
147+
.withCheck(new VisibilityKeywordIndentationCheck())
148+
.onFile(
149+
new DelphiTestUnitBuilder()
150+
.appendDecl("var")
151+
.appendDecl(" Foo: record")
152+
.appendDecl(" public")
153+
.appendDecl(" procedure Proc;")
154+
.appendDecl(" end;"))
155+
.verifyNoIssues();
156+
}
157+
158+
@Test
159+
void testLeftIndentedVisibilitySpecifierInVarAnonymousRecordShouldNotAddIssue() {
160+
CheckVerifier.newVerifier()
161+
.withCheck(new VisibilityKeywordIndentationCheck())
162+
.onFile(
163+
new DelphiTestUnitBuilder()
164+
.appendDecl("var")
165+
.appendDecl(" Foo: record")
166+
.appendDecl(" public")
167+
.appendDecl(" procedure Proc;")
168+
.appendDecl(" end;"))
169+
.verifyNoIssues();
170+
}
171+
172+
@Test
173+
void testRightIndentedVisibilitySpecifierInConstAnonymousRecordShouldNotAddIssue() {
174+
CheckVerifier.newVerifier()
175+
.withCheck(new VisibilityKeywordIndentationCheck())
176+
.onFile(
177+
new DelphiTestUnitBuilder()
178+
.appendDecl("const")
179+
.appendDecl(" Foo: record")
180+
.appendDecl(" public")
181+
.appendDecl(" procedure Proc;")
182+
.appendDecl(" end = ();"))
183+
.verifyNoIssues();
184+
}
185+
186+
@Test
187+
void testLeftIndentedVisibilitySpecifierInConstAnonymousRecordShouldNotAddIssue() {
188+
CheckVerifier.newVerifier()
189+
.withCheck(new VisibilityKeywordIndentationCheck())
190+
.onFile(
191+
new DelphiTestUnitBuilder()
192+
.appendDecl("const")
193+
.appendDecl(" Foo: record")
194+
.appendDecl(" public")
195+
.appendDecl(" procedure Proc;")
196+
.appendDecl(" end = ();"))
197+
.verifyNoIssues();
198+
}
99199
}

0 commit comments

Comments
 (0)