Skip to content

Commit

Permalink
Exclude dpr and dpk imports in LineTooLong
Browse files Browse the repository at this point in the history
  • Loading branch information
Cirras committed Oct 16, 2023
1 parent ae9c752 commit 12ee42d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Clean up descriptions of all plugin properties.
- Exclude dpr and dpk files in `UnusedImport`.
- Exclude dpr and dpk files in `ImportSpecificity`.
- Exclude uses clauses of dpr and dpk files in `LineTooLong`.
- Improve name resolution for declarations within types.
- Improve type resolution for array accesses into variants.
- Improve parsing and type modeling around `AnsiString` types with specified code pages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
*/
package au.com.integradev.delphi.checks;

import java.util.function.IntPredicate;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.plugins.communitydelphi.api.ast.DelphiAst;
import org.sonar.plugins.communitydelphi.api.ast.UsesClauseNode;
import org.sonar.plugins.communitydelphi.api.check.DelphiCheck;
import org.sonar.plugins.communitydelphi.api.check.DelphiCheckContext;
import org.sonar.plugins.communitydelphi.api.check.FilePosition;
Expand All @@ -39,7 +41,13 @@ public class TooLongLineCheck extends DelphiCheck {

@Override
public DelphiCheckContext visit(DelphiAst ast, DelphiCheckContext context) {
IntPredicate isLineExcluded = getLineExclusionFilter(ast);

for (int i = 0; i < context.getFileLines().size(); ++i) {
if (isLineExcluded.test(i)) {
continue;
}

String line = context.getFileLines().get(i);
int lineLength = getLineLength(line);

Expand All @@ -55,9 +63,20 @@ public DelphiCheckContext visit(DelphiAst ast, DelphiCheckContext context) {
.report();
}
}

return context;
}

private static IntPredicate getLineExclusionFilter(DelphiAst ast) {
if (ast.isPackage() || ast.isProgram()) {
UsesClauseNode usesClause = ast.getFirstChildOfType(UsesClauseNode.class);
if (usesClause != null) {
return line -> usesClause.getBeginLine() <= line && usesClause.getEndLine() >= line;
}
}
return line -> false;
}

private static int getLineLength(String line) {
int length = line.length();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package au.com.integradev.delphi.checks;

import au.com.integradev.delphi.builders.DelphiTestProgramBuilder;
import au.com.integradev.delphi.builders.DelphiTestUnitBuilder;
import au.com.integradev.delphi.checks.verifier.CheckVerifier;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -66,4 +67,18 @@ void testTrailingWhitespaceLineShouldNotAddIssue() {
.appendImpl("end;"))
.verifyNoIssues();
}

@Test
void testProgramImportShouldNotAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new TooLongLineCheck())
.onFile(
new DelphiTestProgramBuilder()
.appendDecl("uses")
.appendDecl(
" Fooooooooooooooooooooooooooooooooooooooooooooooooo"
+ ".Baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar"
+ ".Baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz;"))
.verifyNoIssues();
}
}

0 comments on commit 12ee42d

Please sign in to comment.