From 2f5b745a621deac2343578b407acb68aa8157309 Mon Sep 17 00:00:00 2001 From: Elliot Hillary Date: Tue, 10 Oct 2023 16:48:51 +1100 Subject: [PATCH] Include methods in `UnitLevelKeywordIndentation` --- CHANGELOG.md | 1 + .../UnitLevelKeywordIndentationCheck.java | 32 +++++++++++++++++-- .../UnitLevelKeywordIndentationCheckTest.java | 2 +- .../Correct.pas | 16 +++++++++- .../Indented.pas | 17 +++++++++- 5 files changed, 62 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f81a55d84..15508f427 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Exclude dpr and dpk files in `UnusedImport`. - Exclude dpr and dpk files in `ImportSpecificity`. - Exclude uses clauses of dpr and dpk files in `LineTooLong`. +- Include methods in `UnitLevelKeywordIndentation`. - Always enforce the `Attribute` suffix in `AttributeName`. - Improve name resolution for declarations within types. - Improve type resolution for array accesses into variants. diff --git a/delphi-checks/src/main/java/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheck.java b/delphi-checks/src/main/java/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheck.java index 32ed861f7..b3b0ffbc2 100644 --- a/delphi-checks/src/main/java/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheck.java +++ b/delphi-checks/src/main/java/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheck.java @@ -29,6 +29,8 @@ import org.sonar.plugins.communitydelphi.api.ast.InitializationSectionNode; import org.sonar.plugins.communitydelphi.api.ast.InterfaceSectionNode; import org.sonar.plugins.communitydelphi.api.ast.LibraryDeclarationNode; +import org.sonar.plugins.communitydelphi.api.ast.MethodDeclarationNode; +import org.sonar.plugins.communitydelphi.api.ast.MethodImplementationNode; import org.sonar.plugins.communitydelphi.api.ast.PackageDeclarationNode; import org.sonar.plugins.communitydelphi.api.ast.ProgramDeclarationNode; import org.sonar.plugins.communitydelphi.api.ast.TypeSectionNode; @@ -44,13 +46,13 @@ @DeprecatedRuleKey(ruleKey = "UnitLevelKeywordIndentationRule", repositoryKey = "delph") @Rule(key = "UnitLevelKeywordIndentation") public class UnitLevelKeywordIndentationCheck extends DelphiCheck { - private static final String MESSAGE = "Unindent this unit-level keyword."; + private static final String MESSAGE = "Unindent this top-level element."; private static void checkNodeIndentation(DelphiNode node, DelphiCheckContext context) { - if (!IndentationUtils.getLineIndentation(node).equals("")) { + if (!IndentationUtils.getLineIndentation(node).isEmpty()) { context .newIssue() - .onFilePosition(FilePosition.from(node.getToken())) + .onFilePosition(FilePosition.from(node.getFirstToken())) .withMessage(MESSAGE) .report(); } @@ -154,6 +156,30 @@ public DelphiCheckContext visit( return super.visit(compoundStatementNode, context); } + @Override + public DelphiCheckContext visit( + MethodDeclarationNode methodDeclarationNode, DelphiCheckContext context) { + if (methodDeclarationNode.getParent() instanceof InterfaceSectionNode) { + checkNodeIndentation(methodDeclarationNode, context); + } + return super.visit(methodDeclarationNode, context); + } + + @Override + public DelphiCheckContext visit( + MethodImplementationNode methodImplementationNode, DelphiCheckContext context) { + checkNodeIndentation(methodImplementationNode, context); + + DelphiNode block = methodImplementationNode.getBlock(); + + if (block != null) { + checkNodeIndentation(block, context); + checkNodeIndentation(getEnd(block), context); + } + + return super.visit(methodImplementationNode, context); + } + @Override public DelphiCheckContext visit( InitializationSectionNode initializationSectionNode, DelphiCheckContext context) { diff --git a/delphi-checks/src/test/java/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheckTest.java b/delphi-checks/src/test/java/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheckTest.java index 2f7ee96a2..339a21d23 100644 --- a/delphi-checks/src/test/java/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheckTest.java +++ b/delphi-checks/src/test/java/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheckTest.java @@ -46,7 +46,7 @@ void testIndentedUnitShouldAddIssues() { CheckVerifier.newVerifier() .withCheck(new UnitLevelKeywordIndentationCheck()) .onFile(DelphiTestFileBuilder.fromResource(INDENTED_PAS)) - .verifyIssueOnLine(1, 3, 5, 9, 13, 15, 19, 23, 25, 27); + .verifyIssueOnLine(1, 3, 5, 9, 13, 15, 17, 21, 28, 29, 31, 33, 34, 36, 38, 40, 42); } @Test diff --git a/delphi-checks/src/test/resources/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheck/Correct.pas b/delphi-checks/src/test/resources/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheck/Correct.pas index 3b820af41..11828ed32 100644 --- a/delphi-checks/src/test/resources/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheck/Correct.pas +++ b/delphi-checks/src/test/resources/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheck/Correct.pas @@ -10,6 +10,8 @@ interface TMyObject = class(TObject) end; +procedure MyProc; + implementation uses @@ -17,9 +19,21 @@ implementation Math; type - TMyObject = class(TObject) + TMyOtherObject = class(TObject) + private + class procedure MyClassProc; end; +class procedure TMyOtherObject.MyClassProc; +begin + Writeln('Hello world'); +end; + +procedure MyProc; +begin + Writeln('Hello world'); +end; + initialization finalization diff --git a/delphi-checks/src/test/resources/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheck/Indented.pas b/delphi-checks/src/test/resources/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheck/Indented.pas index 8d4519089..ae5da2991 100644 --- a/delphi-checks/src/test/resources/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheck/Indented.pas +++ b/delphi-checks/src/test/resources/au/com/integradev/delphi/checks/UnitLevelKeywordIndentationCheck/Indented.pas @@ -10,6 +10,8 @@ TMyObject = class(TObject) end; + procedure MyProc; + implementation uses @@ -17,9 +19,22 @@ TMyObject = class(TObject) Math; type - TMyObject = class(TObject) + TMyOtherObject = class(TObject) + private + class procedure MyClassProc; end; + + class procedure TMyOtherObject.MyClassProc; + begin + Writeln('Hello world'); + end; + + procedure MyProc; + begin + Writeln('Hello world'); + end; + initialization finalization