All files / src/rules in_statement_indentation.ts

98.33% Statements 118/120
97.56% Branches 40/41
100% Functions 7/7
98.33% Lines 118/120

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 1211x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20848x 20848x 20848x 20848x 20848x 20848x 1x 10425x 10425x 10425x 10425x 10425x 31106x 31106x 31106x 31106x 31106x 31106x 31106x 31106x 31106x 31106x 31106x 31106x 31106x 31106x 31106x 31106x 31106x 31106x 31106x 31106x 31106x 10425x 10425x 8x 8x 10425x 10425x 9906x 9906x 10425x 10425x 244x 244x 10425x 10425x 268x 268x 268x 268x 268x 53x 53x 4x 53x 1x 1x 53x 263x 268x 1451x 26x 26x 1425x 1425x 1451x     1425x 1425x 1425x 1425x 1451x 1451x 1451x 1451x 1451x 1451x 1451x 1451x 1451x 1451x 1451x 1451x 1451x 1451x 1451x 131x 131x 1451x 5892x 5158x 5158x 5892x 8x 8x 8x 8x 8x 5892x 1425x 263x 263x 263x 10425x 10425x  
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 {
  /** 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) {
        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;
  }
 
}