Skip to content

Commit 08b2eda

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

File tree

3 files changed

+120
-5
lines changed

3 files changed

+120
-5
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: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
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.ConstDeclarationNode;
24+
import org.sonar.plugins.communitydelphi.api.ast.TypeDeclarationNode;
25+
import org.sonar.plugins.communitydelphi.api.ast.VarDeclarationNode;
2426
import org.sonar.plugins.communitydelphi.api.ast.VisibilityNode;
2527
import org.sonar.plugins.communitydelphi.api.check.DelphiCheck;
2628
import org.sonar.plugins.communitydelphi.api.check.DelphiCheckContext;
@@ -32,11 +34,20 @@ public class VisibilityKeywordIndentationCheck extends DelphiCheck {
3234
private static final String MESSAGE =
3335
"Indent this visibility specifier to the indentation level of the containing type.";
3436

35-
private static String getExpectedIndentation(DelphiNode node) {
36-
var visibilityNode = (VisibilityNode) node;
37+
private static String getExpectedIndentation(VisibilityNode node) {
3738
// Class/Record/etc. -> VisibilitySection -> Visibility
38-
var parent = visibilityNode.getParent().getParent();
39-
return IndentationUtils.getLineIndentation(parent);
39+
var anchor = node.getParent().getParent();
40+
41+
var declaration = anchor.getParent();
42+
if (declaration instanceof TypeDeclarationNode) {
43+
anchor = ((TypeDeclarationNode) declaration).getTypeNameNode();
44+
} else if (declaration instanceof VarDeclarationNode) {
45+
anchor = ((VarDeclarationNode) declaration).getNameDeclarationList();
46+
} else if (declaration instanceof ConstDeclarationNode) {
47+
anchor = ((ConstDeclarationNode) declaration).getNameDeclarationNode();
48+
}
49+
50+
return IndentationUtils.getLineIndentation(anchor);
4051
}
4152

4253
@Override

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 testTooIndentedVisibilitySpecifierInVarAnonymousRecordShouldAddIssue() {
146+
CheckVerifier.newVerifier()
147+
.withCheck(new VisibilityKeywordIndentationCheck())
148+
.onFile(
149+
new DelphiTestUnitBuilder()
150+
.appendDecl("var")
151+
.appendDecl(" Foo: record")
152+
.appendDecl(" public // Noncompliant")
153+
.appendDecl(" procedure Proc;")
154+
.appendDecl(" end;"))
155+
.verifyIssues();
156+
}
157+
158+
@Test
159+
void testCorrectlyIndentedVisibilitySpecifierInVarAnonymousRecordShouldNotAddIssue() {
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 testTooIndentedVisibilitySpecifierInConstAnonymousRecordShouldAddIssue() {
174+
CheckVerifier.newVerifier()
175+
.withCheck(new VisibilityKeywordIndentationCheck())
176+
.onFile(
177+
new DelphiTestUnitBuilder()
178+
.appendDecl("const")
179+
.appendDecl(" Foo: record")
180+
.appendDecl(" public // Noncompliant")
181+
.appendDecl(" procedure Proc;")
182+
.appendDecl(" end = ();"))
183+
.verifyIssues();
184+
}
185+
186+
@Test
187+
void testCorrectlyIndentedVisibilitySpecifierInConstAnonymousRecordShouldNotAddIssue() {
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)