All files / src/rules commented_code.ts

97.14% Statements 136/140
93.02% Branches 40/43
100% Functions 10/10
97.14% Lines 136/140

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 1401x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 35378x 35378x 35378x 35378x 1x 17690x 17690x 17690x 17690x 155428x 155428x 155428x 155428x 155428x 155428x 155428x 155428x 155428x 155428x 155428x 17690x 17690x 11x 11x 17690x 17690x 17097x 17097x 17690x 17690x 289x 289x 17690x 17690x 319x 319x 319x 319x 319x 319x 319x 319x 319x 2160x 54x 22x 22x 54x 54x 2160x 9x 9x 9x 2160x 319x 319x 20x 20x 319x 319x 319x 17690x 17690x 29x 29x 16x 16x 13x 13x 13x 13x 29x     13x 13x 29x 20x 20x 20x 20x 20x     20x 20x 20x 12x 1x 1x 11x 11x 11x 20x 29x 2x 2x 11x 11x 11x 11x 11x 17690x 17690x 17690x 17690x 17690x 17690x 12x 12x 12x 12x 7x 7x 5x 5x 12x 12x 12x 17690x 17690x 2160x 2160x 2160x 17690x
import {Issue} from "../issue";
import {Position} from "../position";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {Unknown, Empty, Comment} from "../abap/2_statements/statements/_statement";
import {ABAPObject} from "../objects/_abap_object";
import {FunctionGroup} from "../objects";
import {Include} from "../abap/2_statements/statements";
import {ABAPParser} from "../abap/abap_parser";
import {RuleTag, IRuleMetadata} from "./_irule";
import {EditHelper} from "../edit_helper";
import {ABAPFile} from "../abap/abap_file";
import {MemoryFile} from "../files/memory_file";
import {StatementNode} from "../abap/nodes";
 
export class CommentedCodeConf extends BasicRuleConfig {
  /** Allow INCLUDEs in function groups */
  public allowIncludeInFugr: boolean = true;
}
 
export class CommentedCode extends ABAPRule {
  private conf = new CommentedCodeConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "commented_code",
      title: "Find commented code",
      shortDescription: `Detects usage of commented out code.`,
      extendedInformation:
`https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#delete-code-instead-of-commenting-it
https://docs.abapopenchecks.org/checks/14/`,
      tags: [RuleTag.Styleguide, RuleTag.Quickfix, RuleTag.SingleFile],
      badExample: `* WRITE 'hello world'.`,
    };
  }
 
  private getMessage(): string {
    return "Commented code";
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: CommentedCodeConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile, obj: ABAPObject) {
    let issues: Issue[] = [];
 
    const rows = file.getRawRows();
 
    let code = "";
    let posEnd: Position | undefined = undefined;
    let posStart: Position | undefined = undefined;
 
    for (let i = 0; i < rows.length; i++) {
      if (this.isCommentLine(rows[i])) {
        if (code === "") {
          posStart = new Position(i + 1, 1);
        }
        code = code + rows[i].trim().substr(1) + "\n";
        posEnd = new Position(i + 1, rows[i].length + 1);
      } else if (code !== "" && posStart && posEnd) {
        issues = issues.concat(this.check(code.trim(), file, posStart, posEnd, obj));
        code = "";
      }
    }
 
    if (posStart && posEnd) {
      issues = issues.concat(this.check(code.trim(), file, posStart, posEnd, obj));
    }
 
    return issues;
  }
 
  private check(code: string, file: ABAPFile, posStart: Position, posEnd: Position, obj: ABAPObject): Issue[] {
    // assumption: code must end with "." in order to be valid ABAP
    if (code === "" || code.charAt(code.length - 1) !== ".") {
      return [];
    }
 
    const commented = new MemoryFile("_foobar.prog.abap", code);
    const abapFile = new ABAPParser().parse([commented]).output[0];
    const statementNodes = abapFile.getStatements();
    if (statementNodes.length === 0) {
      return [];
    }
 
    let containsStatement: boolean = false;
    for (let i = 0; i < statementNodes.length; i++) {
      const statementNode = statementNodes[i];
      const statement = statementNode.get();
      if (this.getConfig().allowIncludeInFugr === true
          && obj instanceof FunctionGroup
          && statement instanceof Include) {
        continue;
      }
      if (!(statement instanceof Unknown
          || statement instanceof Empty
          || statement instanceof Comment)) {
        if (this.isSplitFromText(statementNodes, i)) {
          continue;
        }
        containsStatement = true;
        break;
      }
    }
    if (!containsStatement) {
      return [];
    }
 
    const fix = EditHelper.deleteRange(file, posStart, posEnd);
    const issue = Issue.atRange(file, posStart, posEnd, this.getMessage(), this.getMetadata().key, this.conf.severity, fix);
    return [issue];
  }
 
  /**
   * The statement parser splits an Unknown statement at a line break if the rest parses as a statement.
   * In comment text, such a split is code only if no further text follows the statement on its line.
   */
  private isSplitFromText(statementNodes: readonly StatementNode[], index: number): boolean {
    const previous = statementNodes[index - 1];
    if (previous === undefined
        || !(previous.get() instanceof Unknown)
        || [".", ","].includes(previous.getLastToken().getStr())) {
      return false;
    }
    const next = statementNodes[index + 1];
    return next !== undefined
      && next.get() instanceof Unknown
      && next.getFirstToken().getRow() === statementNodes[index].getLastToken().getRow();
  }
 
  private isCommentLine(text: string): boolean {
    return (text.substr(0, 1) === "*")
      || (text.trim().substr(0, 1) === "\"" && text.trim().substr(1, 1) !== "!");
  }
}