All files / src/rules align_pseudo_comments.ts

97.33% Statements 73/75
94.11% Branches 16/17
100% Functions 5/5
97.33% Lines 73/75

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 761x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14691x 14691x 14691x 14691x 43918x 43918x 43918x 43918x 43918x 43918x 43918x 43918x 43918x 14691x 14691x 14120x 14120x 14691x 14691x 264x 264x 14691x 14691x 303x 303x 303x 303x 303x 1676x 1637x 1637x 1637x 39x 39x 1676x 20x 1676x     19x 19x 1676x 12x 12x 19x 19x 1676x 11x 11x 10x 11x 1x 1x 1x 11x 11x 11x 1676x 303x 303x 303x 14691x 14691x  
import {ABAPFile} from "../abap/abap_file";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {Issue} from "../issue";
import {Comment} from "../abap/2_statements/statements/_statement";
import {Position} from "../position";
import {EditHelper, IEdit} from "../edit_helper";
 
export class AlignPseudoCommentsConf extends BasicRuleConfig {
}
 
export class AlignPseudoComments extends ABAPRule {
  private conf = new AlignPseudoCommentsConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "align_pseudo_comments",
      title: "Align pseudo comments",
      shortDescription: `Align code inspector pseudo comments in statements`,
      tags: [RuleTag.SingleFile, RuleTag.Whitespace, RuleTag.Quickfix],
      badExample: `WRITE 'sdf'. "#EC sdf`,
      goodExample: `WRITE 'sdf'.                                                "#EC sdf`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: AlignPseudoCommentsConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    let previousEnd: Position | undefined = undefined;
 
    for (const statement of file.getStatements()) {
      if (!(statement.get() instanceof Comment)) {
        previousEnd = statement.getLastToken().getEnd();
        continue;
      }
      const commentLength = statement.concatTokens().length;
      const firstCommentToken = statement.getFirstToken();
      if (firstCommentToken.getStr().startsWith(`"#`) === false) {
        continue;
      } else if (previousEnd === undefined) {
        continue;
      }
 
      let expectedColumn = 61;
      if (commentLength > 10) {
        expectedColumn = 72 - commentLength;
      }
 
      const col = firstCommentToken.getStart().getCol();
      if (previousEnd.getCol() < expectedColumn && col !== expectedColumn) {
        let fix: IEdit | undefined;
        if (col < expectedColumn) {
          fix = EditHelper.insertAt(file, firstCommentToken.getStart(), " ".repeat(expectedColumn - col));
        } else {
          const from = new Position(firstCommentToken.getStart().getRow(), expectedColumn);
          fix = EditHelper.deleteRange(file, from, firstCommentToken.getStart());
        }
        const message = "Align pseudo comment to column " + expectedColumn;
        issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, fix));
      }
    }
 
    return issues;
  }
 
}