All files / src/rules no_exclamation_escape.ts

100% Statements 52/52
100% Branches 8/8
100% Functions 5/5
100% Lines 52/52

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 531x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11088x 11088x 11088x 11088x 181081x 181081x 181081x 181081x 181081x 181081x 181081x 181081x 181081x 181081x 181081x 11088x 11088x 10568x 10568x 11088x 11088x 244x 244x 11088x 11088x 274x 274x 274x 6262x 4520x 4520x 7x 7x 7x 7x 4520x 6262x 274x 274x 274x 11088x  
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {EditHelper} from "../edit_helper";
import {Identifier} from "../abap/1_lexer/tokens";
 
export class NoExclamationEscapeConf extends BasicRuleConfig {
}
 
export class NoExclamationEscape extends ABAPRule {
  private conf = new NoExclamationEscapeConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "no_exclamation_escape",
      title: "No exclamation escape",
      shortDescription: `Detects and removes exclamation marks (!) used to escape identifiers`,
      // eslint-disable-next-line max-len
      extendedInformation: `Exclamation marks are not needed when the identifier is not a reserved keyword, or could be avoided by renaming.`,
      tags: [RuleTag.SingleFile, RuleTag.Quickfix],
      badExample: "methods CONVERT changing !CO_sdf type ref to ZCL_sdf optional.",
      goodExample: "methods CONVERT changing CO_sdf type ref to ZCL_sdf optional.",
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: NoExclamationEscapeConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile): Issue[] {
    const issues: Issue[] = [];
 
    for (const token of file.getTokens()) {
      if (token instanceof Identifier) {
        const str = token.getStr();
        if (str.startsWith("!")) {
          const replacement = str.substring(1);
          const fix = EditHelper.replaceToken(file, token, replacement);
          issues.push(Issue.atToken(file, token, "Do not use exclamation mark to escape identifiers", this.getMetadata().key, this.conf.severity, fix));
        }
      }
    }
 
    return issues;
  }
}