All files / src/rules in_statement_indentation.ts

97.83% Statements 45/46
97.14% Branches 34/35
100% Functions 7/7
97.83% Lines 45/46

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 1121x 1x   1x 1x 1x 1x 1x 1x 1x 1x     1x   14530x     1x   7266x     21667x                                   7x       7008x       114x       139x   139x   139x 32x 32x 3x 29x 1x       135x 731x 7x     724x 724x     724x 724x 724x 724x                             47x   724x 2982x 2648x   334x 7x 7x 7x 7x         135x      
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {IObject} from "../objects/_iobject";
import {Class} from "../objects";
import {BasicRuleConfig} from "./_basic_rule_config";
import * as Statements from "../abap/2_statements/statements";
import {IRuleMetadata, RuleTag} from "./_irule";
import {DDIC} from "../ddic";
import {Unknown, Comment} from "../abap/2_statements/statements/_statement";
import {EditHelper} from "../edit_helper";
import {Position} from "../position";
import {ABAPFile} from "../abap/abap_file";
 
export class InStatementIndentationConf extends BasicRuleConfig {
  /** Ignore global exception classes */
  public ignoreExceptions: boolean = true;
}
 
export class InStatementIndentation extends ABAPRule {
 
  private conf = new InStatementIndentationConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "in_statement_indentation",
      title: "In-statement indentation",
      // eslint-disable-next-line max-len
      shortDescription: `Checks alignment within block statement declarations which span multiple lines, such as multiple conditions in IF statements.`,
      badExample: `IF 1 = 1
  AND 2 = 2.
  WRITE 'hello'.
ENDIF.`,
      goodExample: `IF 1 = 1
    AND 2 = 2.
  WRITE 'hello'.
ENDIF.`,
      tags: [RuleTag.Whitespace, RuleTag.Quickfix, RuleTag.SingleFile],
    };
  }
 
  private getMessage(): string {
    return "Fix in-statement indentation";
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: InStatementIndentationConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile, obj: IObject) {
    const ret: Issue[] = [];
 
    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 [];
      }
    }
 
    for (const s of file.getStatements()) {
      if (s.get() instanceof Comment || s.get() instanceof Unknown) {
        continue;
      }
 
      const tokens = s.getTokens();
      Iif (tokens.length === 0) {
        continue;
      }
      const beginLine = tokens[0].getRow();
      let expected = tokens[0].getCol() + 2;
      const type = s.get();
      if (type instanceof Statements.If
          || type instanceof Statements.While
          || type instanceof Statements.Module
          || type instanceof Statements.SelectLoop
          || type instanceof Statements.FunctionModule
          || type instanceof Statements.Do
          || type instanceof Statements.At
          || type instanceof Statements.Catch
          || type instanceof Statements.When
          || type instanceof Statements.Cleanup
          || type instanceof Statements.Loop
          || type instanceof Statements.Form
          || type instanceof Statements.Else
          || type instanceof Statements.ElseIf
          || type instanceof Statements.MethodImplementation) {
        expected = expected + 2;
      }
      for (const t of tokens) {
        if (t.getRow() === beginLine) {
          continue;
        }
        if (t.getCol() < expected) {
          const fix = EditHelper.replaceRange(file, new Position(t.getRow(), 1), t.getStart(), " ".repeat(expected - 1));
          const issue = Issue.atToken(file, t, this.getMessage(), this.getMetadata().key, this.conf.severity, fix);
          ret.push(issue);
          break;
        }
      }
    }
 
    return ret;
  }
 
}