All files / src/rules max_one_method_parameter_per_line.ts

100% Statements 65/65
100% Branches 14/14
100% Functions 5/5
100% Lines 65/65

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 651x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8922x 8922x 8922x 8922x 26610x 26610x 26610x 26610x 26610x 26610x 26610x 26610x 26610x 26610x 26610x 26610x 26610x 26610x 26610x 26610x 8922x 8922x 8485x 8485x 8922x 8922x 202x 202x 8922x 8922x 218x 218x 218x 46x 46x 17x 14x 14x 14x 3x 17x 1x 1x 1x 3x 3x 3x 46x 218x 218x 218x 8922x 8922x
import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {ExpressionNode} from "../abap/nodes/expression_node";
 
export class MaxOneMethodParameterPerLineConf extends BasicRuleConfig {
}
 
export class MaxOneMethodParameterPerLine extends ABAPRule {
  private conf = new MaxOneMethodParameterPerLineConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "max_one_method_parameter_per_line",
      title: "Max one method parameter definition per line",
      shortDescription: `Keep max one method parameter description per line`,
      tags: [RuleTag.SingleFile, RuleTag.Whitespace],
      badExample: `
METHODS apps_scope_token
  IMPORTING
    body TYPE bodyapps_scope_token client_id TYPE str.`,
      goodExample: `
METHODS apps_scope_token
  IMPORTING
    body      TYPE bodyapps_scope_token
    client_id TYPE str.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: MaxOneMethodParameterPerLineConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    for (const statement of file.getStructure()?.findAllStatements(Statements.MethodDef) || []) {
      let prev: ExpressionNode | undefined = undefined;
      for (const p of statement.findAllExpressions(Expressions.MethodParam)) {
        if (prev === undefined) {
          prev = p;
          continue;
        }
 
        if (prev.getFirstToken().getStart().getRow() === p.getFirstToken().getStart().getRow()) {
          const issue = Issue.atToken(file, prev.getFirstToken(), this.getMetadata().title, this.getMetadata().key, this.conf.severity);
          issues.push(issue);
        }
 
        prev = p;
      }
    }
 
    return issues;
  }
 
}