All files / src/rules align_type_expressions.ts

100% Statements 213/213
100% Branches 38/38
100% Functions 10/10
100% Lines 213/213

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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 2141x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24164x 24164x 24164x 24164x 1x 12088x 12088x 12088x 12088x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 36098x 12088x 12088x 11536x 11536x 12088x 12088x 255x 255x 12088x 12088x 288x 288x 288x 288x 13x 13x 275x 275x 275x 288x 56x 56x 2x 56x 1x 1x 56x 272x 272x 272x 272x 272x 272x 272x 12088x 12088x 123x 123x 123x 123x 115x 115x 1x 1x 114x 114x 122x 123x 113x 104x 104x 9x 9x 113x 5x 113x 4x 4x 9x 9x 9x 9x 9x 122x 122x 122x 12088x 12088x 272x 272x 272x 272x 74x 74x 74x 74x 34x 34x 34x 34x 34x 34x 34x 74x 74x 74x 15x 15x 15x 15x 15x 15x 15x 74x 74x 74x 272x 272x 272x 12088x 12088x 272x 272x 272x 272x 14x 14x 14x 14x 12x 12x 12x 12x 12x 12x 12x 14x 14x 14x 272x 272x 272x 12088x 12088x 272x 272x 272x 40x 5x 5x 35x 35x 35x 35x 40x 54x 54x 54x 54x 54x 54x 35x 35x 35x 272x 272x 272x 12088x 12088x  
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));
    issues.push(...this.checkEvents(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 checkEvents(stru: StructureNode, file: ABAPFile): Issue[] {
    const issues: Issue[] = [];
 
    const events = stru.findAllStatements(Statements.Events);
    for (const e of events) {
      const fields: fields = [];
      const params = e.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);
      }
 
      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;
  }
 
}