All files / src/rules in_statement_indentation.ts

98.36% Statements 120/122
97.61% Branches 41/42
100% Functions 7/7
98.36% Lines 120/122

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 1231x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22006x 22006x 22006x 22006x 22006x 22006x 1x 11004x 11004x 11004x 11004x 11004x 32826x 32826x 32826x 32826x 32826x 32826x 32826x 32826x 32826x 32826x 32826x 32826x 32826x 32826x 32826x 32826x 32826x 32826x 32826x 32826x 32826x 11004x 11004x 8x 8x 11004x 11004x 10451x 10451x 11004x 11004x 261x 261x 11004x 11004x 285x 285x 285x 285x 285x 53x 53x 4x 53x 1x 1x 53x 280x 285x 1545x 1545x 1545x 31x 31x 1514x 1514x 1545x     1514x 1514x 1514x 1514x 1545x 1545x 1545x 1545x 1545x 1545x 1545x 1545x 1545x 1545x 1545x 1545x 1545x 1545x 1545x 143x 143x 1545x 6262x 5478x 5478x 6262x 8x 8x 8x 8x 8x 6262x 1514x 280x 280x 280x 11004x 11004x  
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, NativeSQL} 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 {
  /** Additional indent for first statement of blocks */
  public blockStatements: number = 2;
  /** 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",
      shortDescription: "Checks alignment within statements which span multiple lines.",
      extendedInformation: `Lines following the first line should be indented once (2 spaces).
 
For block declaration statements, lines after the first should be indented an additional time (default: +2 spaces)
to distinguish them better from code within the block.`,
      badExample: `IF 1 = 1
  AND 2 = 2.
  WRITE 'hello' &&
  'world'.
ENDIF.`,
      goodExample: `IF 1 = 1
    AND 2 = 2.
  WRITE 'hello' &&
    'world'.
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
          || s.get() instanceof NativeSQL) {
        continue;
      }
 
      const tokens = s.getTokens();
      if (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.Case
          || 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 + this.conf.blockStatements;
      }
      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;
  }
 
}