All files / src/rules keep_single_parameter_on_one_line.ts

96.66% Statements 174/180
93.1% Branches 54/58
100% Functions 15/15
96.66% Lines 174/180

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 178 179 1801x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23302x 23302x 23302x 23302x 1x 11652x 11652x 11652x 11652x 34807x 34807x 34807x 34807x 34807x 34807x 34807x 34807x 34807x 34807x 11652x 11652x 11286x 11286x 11652x 11652x 239x 239x 11652x 11652x 270x 270x 270x 20x 20x 250x 250x 270x 12x 12x 238x 270x 1351x 1351x 1351x 1351x 1351x 1351x 1215x 1215x 1351x 8x 8x 136x 238x 238x 238x 11652x 11652x 11652x 11652x 138x 138x 138x 11652x 11652x 139x 1x 1x 1x 1x 138x 138x 11652x 11652x 140x 12x 12x 1x 1x 12x 139x 139x 11652x 11652x 137x 4x 4x 1x 1x 4x 3x 136x 136x 11652x 11652x 8x 6x 6x 2x 2x     2x 6x 6x 6x 6x 6x 2x 2x 11652x 11652x 11652x 144x 144x 1112x 144x 144x 1112x 1112x 144x 144x 11652x 11652x 1357x 1357x 1357x 1357x 1357x 11652x 11652x 1x 1x 11652x 11652x 10x 5x 3x 2x 2x 3x 3x 3x 3x 5x 5x 10x 2x 2x 3x 3x 10x 2x     2x 2x     2x 2x 2x 2x 2x 2x 1x 1x 1x 11652x 11652x
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";
import {EditHelper} from "../edit_helper";
 
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, RuleTag.Quickfix],
      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";
      const fix = EditHelper.replaceRange(file, c.getFirstToken().getStart(), c.getLastToken().getEnd(), c.concatTokens());
      return [Issue.atToken(file, c.getFirstToken(), message, this.getMetadata().key, this.conf.severity, fix)];
    }
    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;
  }
 
}