All files / src/rules align_type_expressions.ts

100% Statements 218/218
100% Branches 44/44
100% Functions 10/10
100% Lines 218/218

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 214 215 216 217 218 2191x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 43565x 43565x 43565x 43565x 1x 21790x 21790x 21790x 21790x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 65197x 21790x 21790x 20768x 20768x 21790x 21790x 422x 422x 21790x 21790x 480x 480x 480x 480x 14x 14x 466x 466x 466x 480x 89x 89x 2x 89x 1x 1x 89x 463x 463x 463x 463x 463x 463x 463x 21790x 21790x 144x 144x 144x 144x 141x 141x 1x 1x 140x 140x 143x 144x 139x 129x 129x 10x 10x 139x 6x 139x 4x 4x 10x 10x 10x 10x 10x 143x 143x 143x 21790x 21790x 463x 463x 463x 463x 88x 88x 88x 88x 40x 40x 40x 40x 40x 40x 40x 88x 88x 88x 15x 15x 15x 15x 15x 15x 15x 88x 88x 88x 463x 463x 463x 21790x 21790x 463x 463x 463x 463x 14x 14x 14x 14x 12x 12x 12x 12x 12x 12x 12x 14x 14x 14x 463x 463x 463x 21790x 21790x 463x 463x 463x 47x 5x 5x 42x 42x 42x 42x 47x 76x 76x 76x 76x 76x 2x 2x 74x 74x 74x 74x 74x 42x 42x 42x 463x 463x 463x 21790x 21790x  
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";
import {Punctuation} from "../abap/1_lexer/tokens";
 
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;
      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 children = s.getChildren();
        const length = s.findDirectExpression(Expressions.ConstantFieldLength);
        const nameEnd = (length ?? children[1]).getLastToken().getEnd();
        const after = children[length ? 3 : 2];
        if (after === undefined || after.getFirstToken() instanceof Punctuation) {
          continue; // no TYPE clause, eg. "foo(1)," with implicit type
        }
        fields.push({
          nameEnd: nameEnd,
          after: after.getFirstToken().getStart()});
        column = Math.max(column, nameEnd.getCol() + 1);
      }
 
      issues.push(...this.check(fields, column, file));
    }
 
    return issues;
  }
 
}