All files / src/rules commented_code.ts

96.58% Statements 113/117
91.18% Branches 31/34
100% Functions 9/9
96.58% Lines 113/117

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 1171x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15306x 15306x 15306x 15306x 1x 7654x 7654x 7654x 7654x 30171x 30171x 30171x 30171x 30171x 30171x 30171x 30171x 30171x 30171x 7654x 7654x 10x 10x 7654x 7654x 7360x 7360x 7654x 7654x 139x 139x 7654x 7654x 167x 167x 167x 167x 167x 167x 167x 167x 167x 1069x 34x 16x 16x 34x 34x 1069x 6x 6x 6x 1069x 167x 167x 14x 14x 167x 167x 167x 7654x 7654x 20x 20x 9x 9x 11x 11x 11x 11x 20x     11x 11x 20x 14x 14x 14x 14x     14x 14x 14x 10x 10x 10x 14x 20x 1x 1x 10x 10x 10x 10x 10x 7654x 7654x 1069x 1069x 1069x 7654x
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";
 
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],
    };
  }
 
  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 (const statementNode of statementNodes) {
      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)) {
        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];
  }
 
  private isCommentLine(text: string): boolean {
    return (text.substr(0, 1) === "*")
      || (text.trim().substr(0, 1) === "\"" && text.trim().substr(1, 1) !== "!");
  }
}