Skip to content

Commit

Permalink
add tests for NeedlessClearRule
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgrassau committed Oct 29, 2023
1 parent e60855d commit 1bfa4ea
Show file tree
Hide file tree
Showing 4 changed files with 390 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1285,9 +1285,15 @@ public final boolean isAssignment(boolean allowInlineDeclaration, boolean allowT

public final boolean splitOutLeadingCommentLines(Command originalCommand) throws UnexpectedSyntaxAfterChanges {
boolean splitOut = false;
Token firstCode = getFirstCodeToken();
int firstLineBreaks = (firstCode == null) ? 1 : firstCode.lineBreaks;
while (firstToken.isCommentLine() && tokenCount > 1) {
Token commentLine = firstToken;
commentLine.removeFromCommand(false, true); // skip the integrity test, as it will be performed below

// the topmost comment line gets the number of line breaks from the first code Token, all others keep their number of line breaks
commentLine.lineBreaks = Math.max(splitOut ? commentLine.lineBreaks : firstLineBreaks, 1);

Command newCommand = Command.create(commentLine, originalCommand);
try {
newCommand.finishBuild(getSourceTextStart(), getSourceTextEnd());
Expand All @@ -1300,8 +1306,11 @@ public final boolean splitOutLeadingCommentLines(Command originalCommand) throws
splitOut = true;
}

if (splitOut)
if (splitOut) {
if (firstCode != null)
firstCode.lineBreaks = 1;
testReferentialIntegrity(true);
}

return splitOut;
}
Expand Down Expand Up @@ -3087,7 +3096,7 @@ public final boolean matchesPattern() {
// return changesSyField(ABAP.SyField.SUBRC) && SyFieldAnalyzer.getSyFieldReadersFor(ABAP.SyField.SUBRC, this).size() >= 2;
// - getCommandsRelatedToPatternMatch() can then return SyFieldAnalyzer.getSyFieldReadersFor(ABAP.SyField.SUBRC, this);

return firstToken.matchesDeep(true, TokenSearch.ASTERISK, "AUTHORITY-CHECK", "DISABLE", "BEGIN");
return false;
}

public final ArrayList<Command> getCommandsRelatedToPatternMatch() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class NeedlessClearRule extends RuleForDeclarations {
public RuleReference[] getReferences() { return references; }

@Override
public RuleID[] getDependentRules() { return new RuleID[] { RuleID.LOCAL_DECLARATION_ORDER, RuleID.CHAIN_OF_ONE, RuleID.UNUSED_VARIABLES, RuleID.INSET }; }
public RuleID[] getDependentRules() { return new RuleID[] { RuleID.LOCAL_DECLARATION_ORDER, RuleID.CHAIN_OF_ONE, RuleID.UNUSED_VARIABLES, RuleID.INSET, RuleID.UPPER_AND_LOWER_CASE }; }

@Override
public String getExample() {
Expand Down Expand Up @@ -233,7 +233,7 @@ private boolean canRemoveClear(Token token, LocalVariables localVariables, boole

// if configured, exclude cases where a structure is cleared which is assigned directly afterwards
if (configKeepStrucBeforeAssign.getValue()) {
Command next = token.getParentCommand().getNext();
Command next = token.getParentCommand().getNextNonCommentCommand();
if (next != null && next.isAssignment(false, false) && next.getFirstCodeToken().textStartsWith(text + "-")) {
return false;
}
Expand Down
Loading

0 comments on commit 1bfa4ea

Please sign in to comment.