All files / src/rules line_break_multiple_parameters.ts

96.97% Statements 32/33
87.5% Branches 7/8
100% Functions 5/5
96.88% Lines 31/32

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 781x 1x 1x 1x 1x 1x 1x       1x     1x   7266x     21666x                       7008x       114x       135x   135x 10x     125x 691x 6x 1x     5x 5x     5x 5x 5x 5x 5x 3x 3x               5x           125x      
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 "../position";
import {ABAPFile} from "../abap/abap_file";
import {ABAPObject} from "../objects/_abap_object";
 
export class LineBreakMultipleParametersConf extends BasicRuleConfig {
}
 
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);
        Iif (parameters.length <= 1) {
          continue;
        }
        let previous = parameters[0];
        for (let i = 1; i < parameters.length; i++) {
          const current = parameters[i];
          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;
  }
 
}