All files / src/rules chain_mainly_declarations.ts

94.65% Statements 177/187
83.05% Branches 49/59
100% Functions 7/7
94.65% Lines 177/187

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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 1881x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 15308x 1x 7655x 7655x 7655x 7655x 7655x 22825x 22825x 22825x 22825x 22825x 22825x 22825x 22825x 22825x 22825x 22825x 22825x 22825x 22825x 7655x 7655x 7346x 7346x 7655x 7655x 141x 141x 7655x 7655x 166x 166x 166x 166x 12x 12x 154x 154x 166x 845x 845x 745x 745x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 845x 89x 845x 1x 11x   10x   10x   10x   10x   10x   10x   10x   10x 1x 1x 9x 9x 845x 4x 4x 9x 9x 9x 9x 9x 9x 9x 9x 154x 154x 154x 7655x 7655x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x     9x 9x 9x 9x 9x 9x 5x 5x 4x 4x 4x 9x 9x 9x 9x 4x 4x 9x 9x 9x 7655x  
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import * as Statements from "../abap/2_statements/statements";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {EditHelper, IEdit} from "../edit_helper";
import {StatementNode} from "../abap/nodes";
import {Position} from "../position";
import {IStatement} from "../abap/2_statements/statements/_statement";
 
export class ChainMainlyDeclarationsConf extends BasicRuleConfig {
  /** Allow definition statements to be chained */
  public definitions: boolean = true;
  /** Allow WRITE statements to be chained */
  public write: boolean = true;
  /** Allow MOVE statements to be chained */
  public move: boolean = true;
  /** Allow REFRESH statements to be chained */
  public refresh: boolean = true;
  /** Allow UNASSIGN statements to be chained */
  public unassign: boolean = true;
  /** Allow CLEAR statements to be chained */
  public clear: boolean = true;
  /** Allow HIDE statements to be chained */
  public hide: boolean = true;
  /** Allow FREE statements to be chained */
  public free: boolean = true;
  /** Allow INCLUDE statements to be chained */
  public include: boolean = true;
  /** Allow CHECK statements to be chained */
  public check: boolean = true;
  /** Allow SORT statements to be chained */
  public sort: boolean = true;
}
 
export class ChainMainlyDeclarations extends ABAPRule {
 
  private conf = new ChainMainlyDeclarationsConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "chain_mainly_declarations",
      title: "Chain mainly declarations",
      shortDescription: `Chain mainly declarations, allows chaining for the configured statements, reports errors for other statements.`,
      extendedInformation: `
https://docs.abapopenchecks.org/checks/23/
 
https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/abenchained_statements_guidl.htm
`,
      tags: [RuleTag.SingleFile, RuleTag.Quickfix],
      badExample: `CALL METHOD: bar.`,
      goodExample: `CALL METHOD bar.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: ChainMainlyDeclarationsConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    const structure = file.getStructure();
    if (structure === undefined) {
      return [];
    }
 
    let previousRow: number | undefined;
    for (const statementNode of structure.findAllStatementNodes()) {
      const colon = statementNode.getColon();
      if (colon === undefined) {
        continue;
      }
      const statement = statementNode.get();
 
      if (this.conf.definitions === true
          && (statement instanceof Statements.ClassData
          || statement instanceof Statements.ClassDataBegin
          || statement instanceof Statements.ClassDataEnd
          || statement instanceof Statements.Static
          || statement instanceof Statements.StaticBegin
          || statement instanceof Statements.StaticEnd
          || statement instanceof Statements.Local
          || statement instanceof Statements.Constant
          || statement instanceof Statements.ConstantBegin
          || statement instanceof Statements.ConstantEnd
          || statement instanceof Statements.Controls
          || statement instanceof Statements.Parameter
          || statement instanceof Statements.SelectOption
          || statement instanceof Statements.SelectionScreen
          || statement instanceof Statements.Aliases
          || statement instanceof Statements.Tables
          || statement instanceof Statements.MethodDef
          || statement instanceof Statements.InterfaceDef
          || statement instanceof Statements.Type
          || statement instanceof Statements.TypeBegin
          || statement instanceof Statements.TypeEnd
          || statement instanceof Statements.TypeEnumBegin
          || statement instanceof Statements.TypeEnumEnd
          || statement instanceof Statements.TypeEnum
          || statement instanceof Statements.Events
          || statement instanceof Statements.Ranges
          || statement instanceof Statements.TypePools
          || statement instanceof Statements.FieldSymbol
          || statement instanceof Statements.Data
          || statement instanceof Statements.DataBegin
          || statement instanceof Statements.DataEnd)) {
        continue;
      } else if (this.conf.write === true && statement instanceof Statements.Write) {
        continue;
      } else if (this.conf.move === true && statement instanceof Statements.Move) {
        continue;
      } else if (this.conf.refresh === true && statement instanceof Statements.Refresh) {
        continue;
      } else if (this.conf.unassign === true && statement instanceof Statements.Unassign) {
        continue;
      } else if (this.conf.clear === true && statement instanceof Statements.Clear) {
        continue;
      } else if (this.conf.hide === true && statement instanceof Statements.Hide) {
        continue;
      } else if (this.conf.free === true && statement instanceof Statements.Free) {
        continue;
      } else if (this.conf.include === true && statement instanceof Statements.Include) {
        continue;
      } else if (this.conf.check === true && statement instanceof Statements.Check) {
        continue;
      } else if (this.conf.sort === true && statement instanceof Statements.Sort) {
        continue;
      }
 
      let prevFix: IEdit | undefined;
      if (previousRow === colon.getStart().getRow()) {
        prevFix = issues.pop()?.getFix();
      }
 
      const fix = this.getFix(file, statement, statementNode, prevFix);
 
      const message = "Chain mainly declarations";
      issues.push(Issue.atToken(file, statementNode.getFirstToken(), message, this.getMetadata().key, this.conf.severity, fix));
 
      previousRow = statementNode.getColon()!.getStart().getRow();
    }
 
    return issues;
  }
 
  private getFix(file: ABAPFile, statement: IStatement, statementNode: StatementNode, prevFix: IEdit | undefined): IEdit | undefined {
    if (statement instanceof Statements.ClassDataBegin ||
      statement instanceof Statements.ClassDataEnd ||
      statement instanceof Statements.StaticBegin ||
      statement instanceof Statements.StaticEnd ||
      statement instanceof Statements.ConstantBegin ||
      statement instanceof Statements.ConstantEnd ||
      statement instanceof Statements.TypeBegin ||
      statement instanceof Statements.TypeEnd ||
      statement instanceof Statements.TypeEnumBegin ||
      statement instanceof Statements.TypeEnumEnd ||
      statement instanceof Statements.DataBegin ||
      statement instanceof Statements.DataEnd) {
      return undefined;
    }
 
    let replacement = statementNode.concatTokens();
    replacement = replacement.replace(",", ".");
 
    let start: Position;
    if (prevFix === undefined) {
      start = statementNode.getStart();
    }
    else {
      start = statementNode.getTokens()[1].getStart();
    }
 
    let fix = EditHelper.replaceRange(file, start, statementNode.getEnd(), replacement);
 
    if (prevFix !== undefined) {
      fix = EditHelper.merge(fix, prevFix);
    }
 
    return fix;
  }
}