All files / src/rules keep_single_parameter_on_one_line.ts

96.53% Statements 167/173
92.72% Branches 51/55
100% Functions 15/15
96.53% Lines 167/173

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 1731x 1x 1x 1x 1x 1x 1x 1x 1x 17843x 17843x 17843x 17843x 1x 8922x 8922x 8922x 8922x 26612x 26612x 26612x 26612x 26612x 26612x 26612x 26612x 26612x 26612x 8922x 8922x 8579x 8579x 8922x 8922x 202x 202x 8922x 8922x 229x 229x 229x 18x 18x 211x 211x 229x 12x 12x 199x 229x 1133x 1133x 1133x 1133x 1133x 1133x 1133x 1044x 1044x 1133x 5x 5x 89x 199x 199x 199x 8922x 8922x 8922x 8922x 91x 91x 91x 8922x 8922x 92x 1x 1x 1x 1x 91x 91x 8922x 8922x 93x 10x 10x 1x 1x 10x 92x 92x 8922x 8922x 90x 4x 4x 1x 1x 4x 3x 89x 89x 8922x 8922x 5x 5x 5x 3x 3x 1x 1x 3x 4x 4x 4x 4x     8922x 8922x 8922x 94x 94x 709x 94x 94x 709x 709x 94x 94x 8922x 8922x 1138x 1138x 1138x 1138x 1138x 8922x 8922x 2x 2x 8922x 8922x 8x 3x 3x 5x 5x 8x 3x 3x 2x 2x 8x 1x     1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 8922x 8922x
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()) {
      // todo, add length as configurable setting
      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)) {
      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;
  }
 
}