All files / src/rules line_break_multiple_parameters.ts

97.67% Statements 84/86
95.23% Branches 20/21
100% Functions 6/6
97.67% Lines 84/86

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 871x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22558x 22558x 22558x 22558x 1x 11280x 11280x 11280x 11280x 11280x 33691x 33691x 33691x 33691x 33691x 33691x 33691x 33691x 33691x 33691x 11280x 11280x 10789x 10789x 11280x 11280x 230x 230x 11280x 11280x 250x 250x 250x 20x 20x 230x 250x 1304x 10x 1x 1x 9x 9x 10x     9x 10x 12x 12x 12x 3x 3x 3x 9x 9x 12x 7x 7x 7x 7x 7x 7x 7x 7x 7x 9x 9x 9x 9x 1304x 230x 230x 230x 11280x 11280x  
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import * as Expressions from "../abap/2_statements/expressions";
import {IRuleMetadata, RuleTag} from "./_irule";
import {EditHelper} from "../edit_helper";
import {VirtualPosition} from "../virtual_position";
import {ABAPFile} from "../abap/abap_file";
import {ABAPObject} from "../objects/_abap_object";
 
export class LineBreakMultipleParametersConf extends BasicRuleConfig {
  /** Amount of allowed parameters on one line */
  public count: number = 1;
}
 
export class LineBreakMultipleParameters extends ABAPRule {
 
  private conf = new LineBreakMultipleParametersConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "line_break_multiple_parameters",
      title: "Line break multiple parameters",
      shortDescription: `Line break multiple parameters`,
      extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#line-break-multiple-parameters`,
      badExample: `method( parameter1 = value parameter2 = value ).`,
      goodExample: `method( parameter1 = value\n        parameter2 = value ).`,
      tags: [RuleTag.Whitespace, RuleTag.Styleguide, RuleTag.Quickfix, RuleTag.SingleFile],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: LineBreakMultipleParametersConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile, obj: ABAPObject) {
    const issues: Issue[] = [];
 
    if (obj.getType() === "INTF") {
      return [];
    }
 
    for (const s of file.getStatements()) {
      for (const e of s.findAllExpressions(Expressions.ParameterListS)) {
        if (s.getFirstToken().getStart() instanceof VirtualPosition) {
          continue; // skip macro content
        }
 
        const parameters = e.findDirectExpressions(Expressions.ParameterS);
        if (parameters.length <= 1) {
          continue;
        }
        let previous = parameters[0];
        for (let i = 1; i < parameters.length; i++) {
          const current = parameters[i];
 
          if (this.conf.count && i < this.conf.count) {
            previous = current;
            continue;
          }
 
          const first = current.getFirstToken();
          if (previous.getFirstToken().getRow() === first.getRow()) {
            const fix = EditHelper.insertAt(file, first.getStart(), "\n" + " ".repeat(parameters[0].getFirstToken().getStart().getCol() - 1));
            issues.push(Issue.atToken(
              file,
              current.getFirstToken(),
              this.getMetadata().title,
              this.getMetadata().key,
              this.conf.severity,
              fix));
          }
          previous = current;
        }
 
      }
    }
 
    return issues;
  }
 
}