All files / src/rules align_type_expressions.ts

100% Statements 189/189
100% Branches 33/33
100% Functions 9/9
100% Lines 189/189

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 188 189 1901x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20979x 20979x 20979x 20979x 1x 10495x 10495x 10495x 10495x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 31281x 10495x 10495x 9952x 9952x 10495x 10495x 251x 251x 10495x 10495x 275x 275x 275x 275x 13x 13x 262x 262x 262x 275x 51x 51x 2x 51x 1x 1x 51x 259x 259x 259x 259x 259x 259x 10495x 10495x 89x 89x 89x 89x 88x 88x 1x 1x 87x 87x 88x 89x 86x 78x 78x 8x 8x 86x 4x 4x 4x 4x 8x 8x 8x 8x 8x 88x 88x 88x 10495x 10495x 259x 259x 259x 259x 59x 59x 59x 59x 28x 28x 28x 28x 28x 28x 28x 59x 59x 59x 13x 13x 13x 13x 13x 13x 13x 59x 59x 59x 259x 259x 259x 10495x 10495x 259x 259x 259x 33x 3x 3x 30x 30x 30x 30x 33x 47x 47x 47x 47x 47x 47x 30x 30x 30x 259x 259x 259x 10495x 10495x  
import {ABAPFile} from "../abap/abap_file";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {Class} from "../objects";
import {DDIC} from "../ddic";
import {IObject} from "../objects/_iobject";
import {IRuleMetadata, RuleTag} from "./_irule";
import {Issue} from "../issue";
import {Position} from "../position";
import {StructureNode} from "../abap/nodes";
import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import * as Structures from "../abap/3_structures/structures";
import {EditHelper, IEdit} from "../edit_helper";
 
type fields = {nameEnd: Position, after: Position}[];
 
export class AlignTypeExpressionsConf extends BasicRuleConfig {
  /** Ignore global exception classes */
  public ignoreExceptions: boolean = true;
}
 
export class AlignTypeExpressions extends ABAPRule {
  private conf = new AlignTypeExpressionsConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "align_type_expressions",
      title: "Align TYPE expressions",
      shortDescription: `Align TYPE expressions in statements`,
      extendedInformation: `
Currently works for METHODS + BEGIN OF
 
If BEGIN OF has an INCLUDE TYPE its ignored
 
Also note that clean ABAP does not recommend aligning TYPE clauses:
https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#dont-align-type-clauses`,
      tags: [RuleTag.SingleFile, RuleTag.Whitespace, RuleTag.Quickfix],
      badExample: `
TYPES: BEGIN OF foo,
         bar TYPE i,
         foobar TYPE i,
       END OF foo.
 
INTERFACE lif.
  METHODS bar
    IMPORTING
      foo TYPE i
      foobar TYPE i.
ENDINTERFACE.`,
      goodExample: `
TYPES: BEGIN OF foo,
         bar    TYPE i,
         foobar TYPE i,
       END OF foo.
 
INTERFACE lif.
  METHODS bar
    IMPORTING
      foo    TYPE i
      foobar TYPE i.
ENDINTERFACE.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: AlignTypeExpressionsConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile, obj: IObject) {
    const issues: Issue[] = [];
 
    const stru = file.getStructure();
    if (stru === undefined) {
      return issues; // parser error
    }
 
    const ddic = new DDIC(this.reg);
 
    if (obj instanceof Class) {
      const definition = obj.getClassDefinition();
      if (definition === undefined) {
        return [];
      } else if (this.conf.ignoreExceptions && ddic.isException(definition, obj)) {
        return [];
      }
    }
 
    issues.push(...this.checkTypes(stru, file));
    issues.push(...this.checkMethods(stru, file));
 
    return issues;
  }
 
  private check(fields: fields, column: number, file: ABAPFile): Issue[] {
    const issues: Issue[] = [];
 
    const rows = new Set<number>();
    for (const f of fields) {
      const row = f.after.getRow();
      if (rows.has(row)) {
        return [];
      }
      rows.add(row);
    }
 
    for (const f of fields) {
      if (f.after.getCol() === column) {
        continue;
      }
 
      let fix: IEdit | undefined = undefined;
      if (f.after.getCol() < column) {
        fix = EditHelper.insertAt(file, f.after, " ".repeat(column - f.after.getCol()));
      } else {
        fix = EditHelper.deleteRange(file, new Position(f.after.getRow(), column), f.after);
      }
 
      const message = `Align TYPE expressions to column ${column}`;
      const issue = Issue.atPosition(file, f.after, message, this.getMetadata().key, this.conf.severity, fix);
      issues.push(issue);
    }
 
    return issues;
  }
 
  private checkMethods(stru: StructureNode, file: ABAPFile): Issue[] {
    const issues: Issue[] = [];
 
    const methods = stru.findAllStatements(Statements.MethodDef);
    for (const m of methods) {
      const fields: fields = [];
      const params = m.findAllExpressions(Expressions.MethodParam);
      let column = 0;
      for (const p of params) {
        const children = p.getChildren();
        const name = children[children.length - 2];
        fields.push({
          nameEnd: name.getLastToken().getEnd(),
          after: p.findFirstExpression(Expressions.TypeParam)!.getFirstToken().getStart()});
        column = Math.max(column, name.getFirstToken().getEnd().getCol() + 1);
      }
 
      const ret = m.findFirstExpression(Expressions.MethodDefReturning);
      if (ret) {
        const children = ret.getChildren();
        const name = children[children.length - 2];
        fields.push({
          nameEnd: name.getLastToken().getEnd(),
          after: ret.findFirstExpression(Expressions.TypeParam)!.getFirstToken().getStart()});
        column = Math.max(column, name.getLastToken().getEnd().getCol() + 1);
      }
 
      issues.push(...this.check(fields, column, file));
    }
 
    return issues;
  }
 
  private checkTypes(stru: StructureNode, file: ABAPFile): Issue[] {
    const issues: Issue[] = [];
    const types = stru.findAllStructuresRecursive(Structures.Types);
    for (const t of types) {
      if (t.findDirectStatement(Statements.IncludeType)) {
        continue;
      }
 
      const fields: fields = [];
      let column = 0;
      const st = t.findDirectStatements(Statements.Type);
      for (const s of st) {
        const name = s.getChildren()[1];
        fields.push({
          nameEnd: name.getLastToken().getEnd(),
          after: s.getChildren()[2].getFirstToken().getStart()});
        column = Math.max(column, name.getFirstToken().getEnd().getCol() + 1);
      }
 
      issues.push(...this.check(fields, column, file));
    }
 
    return issues;
  }
 
}