All files / src/rules keep_single_parameter_on_one_line.ts

96.62% Statements 172/178
92.98% Branches 53/57
100% Functions 15/15
96.62% Lines 172/178

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 1781x 1x 1x 1x 1x 1x 1x 1x 1x 20543x 20543x 20543x 20543x 1x 10272x 10272x 10272x 10272x 30648x 30648x 30648x 30648x 30648x 30648x 30648x 30648x 30648x 30648x 10272x 10272x 9881x 9881x 10272x 10272x 240x 240x 10272x 10272x 267x 267x 267x 19x 19x 248x 248x 267x 12x 12x 236x 267x 1346x 1346x 1346x 1346x 1346x 1346x 1233x 1233x 1346x 6x 6x 113x 236x 236x 236x 10272x 10272x 10272x 10272x 115x 115x 115x 10272x 10272x 116x 1x 1x 1x 1x 115x 115x 10272x 10272x 117x 11x 11x 1x 1x 11x 116x 116x 10272x 10272x 114x 4x 4x 1x 1x 4x 3x 113x 113x 10272x 10272x 6x 4x 4x 2x 2x     2x 4x 4x 4x 4x 2x 2x 10272x 10272x 10272x 120x 120x 922x 120x 120x 922x 922x 120x 120x 10272x 10272x 1350x 1350x 1350x 1350x 1350x 10272x 10272x 1x 1x 10272x 10272x 8x 4x 3x 2x 2x 3x 2x 2x 2x 4x 4x 8x 2x 2x 2x 2x 8x 1x     1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 10272x 10272x
import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {ABAPRule} from "./_abap_rule";
import {ExpressionNode, StatementNode} from "../abap/nodes";
import * as Expressions from "../abap/2_statements/expressions";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {ABAPObject} from "../objects/_abap_object";
 
export class KeepSingleParameterCallsOnOneLineConf extends BasicRuleConfig {
  /** Max line length, in characters */
  public length: number = 120;
}
 
export class KeepSingleParameterCallsOnOneLine extends ABAPRule {
  private conf = new KeepSingleParameterCallsOnOneLineConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "keep_single_parameter_on_one_line",
      title: "Keep single parameters on one line",
      shortDescription: `Keep single parameter calls on one line`,
      extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#keep-single-parameter-calls-on-one-line`,
      tags: [RuleTag.Whitespace, RuleTag.Styleguide, RuleTag.SingleFile],
      badExample: `call_method(\n  2 ).`,
      goodExample: `call_method( 2 ).`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: KeepSingleParameterCallsOnOneLineConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile, obj: ABAPObject): Issue[] {
    let issues: Issue[] = [];
 
    if (obj.getType() === "INTF") {
      return [];
    }
 
    const stru = file.getStructure();
    if (stru === undefined) {
      return [];
    }
 
    for (const s of file.getStatements()) {
      if (this.isMultiLine(s) === false
          || this.calcStatementLength(s) > this.getConfig().length
          || this.containsNewLineValue(s)
          || this.containsNewLineTableExpression(s)
          || this.containsFieldAssigments(s)
          || this.containsNewLineTemplate(s)) {
        continue;
      }
      for (const c of s.findAllExpressions(Expressions.MethodCallParam)) {
        issues = issues.concat(this.check(c, file));
      }
    }
 
    return issues;
  }
 
///////////////////////////////////////
 
  private containsFieldAssigments(s: StatementNode): boolean {
    const fs = s.findAllExpressions(Expressions.FieldAssignment);
    return fs.length > 1;
  }
 
  private containsNewLineTableExpression(s: StatementNode): boolean {
    for (const st of s.findAllExpressions(Expressions.TableExpression)) {
      if (st.getFirstToken().getRow() !== st.getLastToken().getRow()) {
        return true;
      }
    }
    return false;
  }
 
  private containsNewLineValue(s: StatementNode): boolean {
    for (const st of s.findAllExpressions(Expressions.Source)) {
      const first = st.getFirstToken().getStr().toUpperCase();
      if (first === "VALUE" && st.getFirstToken().getRow() !== st.getLastToken().getRow()) {
        return true;
      }
    }
    return false;
  }
 
  private containsNewLineTemplate(s: StatementNode): boolean {
    for (const st of s.findAllExpressions(Expressions.StringTemplate)) {
      for (const t of st.getAllTokens()) {
        if (t.getStr().includes("\\n")) {
          return true;
        }
      }
    }
    return false;
  }
 
  private check(c: ExpressionNode, file: ABAPFile): Issue[] {
    if (this.isSingleParameter(c) === true && this.isMultiLine(c) === true) {
 
      for (const sub of c.findAllExpressions(Expressions.MethodCallParam)) {
        if (this.isSingleParameter(sub) === false
            && this.isWithoutParameters(sub) === false) {
          return [];
        }
      }
 
      const message = "Keep single parameter on one line";
      return [Issue.atToken(file, c.getFirstToken(), message, this.getMetadata().key, this.conf.severity)];
    }
    return [];
  }
 
  // including first indentation, worst case calculation add space after each token
  private calcStatementLength(c: StatementNode): number {
    let length = 0;
    for (const t of c.getTokens()) {
      if (length === 0) {
        length = length + t.getStart().getCol();
      }
      length = length + t.getStr().length + 1;
    }
    return length;
  }
 
  private isMultiLine(c: ExpressionNode | StatementNode): boolean {
    const first = c.getFirstToken();
    const last = c.getLastToken();
 
    return first.getStart().getRow() < last.getStart().getRow();
  }
 
  private isWithoutParameters(c: ExpressionNode): boolean {
    return c.getChildren().length === 2;
  }
 
  private isSingleParameter(c: ExpressionNode): boolean {
    if (c.findDirectExpression(Expressions.Source)) {
      for (const params of c.findAllExpressions(Expressions.ParameterListS)) {
        if (params.getChildren().length > 1) {
          return false;
        }
      }
 
      return true;
    }
 
    const list = c.findDirectExpression(Expressions.ParameterListS);
    if (list) {
      return list.getChildren().length === 1;
    }
 
    const param = c.findDirectExpression(Expressions.MethodParameters);
    if (param) {
      if (param.getChildren().length > 2) {
        return false;
      }
      const paramsource = param.findDirectExpression(Expressions.ParameterListS);
      if (paramsource && paramsource.getChildren().length === 1) {
        return true;
      }
 
      const paramtarget = param.findDirectExpression(Expressions.ParameterListT);
      if (paramtarget && paramtarget.getChildren().length === 1) {
        return true;
      }
    }
 
    return false;
  }
 
}