All files / src/rules omit_parameter_name.ts

88.88% Statements 120/135
77.27% Branches 34/44
100% Functions 7/7
88.88% Lines 120/135

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 1351x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10333x 10333x 10333x 10333x 10333x 30805x 30805x 30805x 30805x 30805x 30805x 30805x 30805x 30805x 30805x 30805x 30805x 30805x 10333x 10333x 252x 252x 252x 10333x 10333x 9816x 9816x 10333x 10333x 241x 241x 10333x 10333x 318x 318x 82x 82x 236x 236x 236x 318x 243x 243x 12x 12x 231x 243x 17x     17x 17x 17x 11x 11x 6x 17x     6x 17x     6x 6x 17x     6x 6x 17x 1x 1x 17x 17x 17x 5x 5x 5x 5x 5x 5x     5x 17x 231x 236x 236x 236x 10333x 10333x 10333x 10333x 6x 6x     6x 6x 15x 15x 9x 15x 6x 1x 6x 5x 5x 6x 15x       10333x 10333x
import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {IRegistry} from "../_iregistry";
import * as Expressions from "../abap/2_statements/expressions";
import {IObject} from "../objects/_iobject";
import {ABAPObject} from "../objects/_abap_object";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {AbstractToken} from "../abap/1_lexer/tokens/abstract_token";
import {ISpaghettiScope} from "../abap/5_syntax/_spaghetti_scope";
import {ReferenceType} from "../abap/5_syntax/_reference";
import {MethodDefinition} from "../abap/types/method_definition";
import {EditHelper} from "../edit_helper";
import {BuiltInMethod} from "../abap/5_syntax/_builtin";
import {IMethodParameters} from "../abap/types/_method_parameters";
 
export class OmitParameterNameConf extends BasicRuleConfig {
}
 
export class OmitParameterName implements IRule {
  private reg: IRegistry;
  private conf = new OmitParameterNameConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "omit_parameter_name",
      title: "Omit parameter name",
      shortDescription: `Omit the parameter name in single parameter calls`,
      extendedInformation: `
https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#omit-the-parameter-name-in-single-parameter-calls
 
EXPORTING must already be omitted for this rule to take effect, https://rules.abaplint.org/exporting/`,
      tags: [RuleTag.Styleguide, RuleTag.Quickfix],
      badExample: `method( param = 2 ).`,
      goodExample: `method( 2 ).`,
    };
  }
 
  public initialize(reg: IRegistry) {
    this.reg = reg;
    return this;
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: OmitParameterNameConf) {
    this.conf = conf;
  }
 
  public run(obj: IObject): Issue[] {
    const issues: Issue[] = [];
    if (!(obj instanceof ABAPObject) || obj.getType() === "INTF") {
      return [];
    }
 
    const spaghetti = new SyntaxLogic(this.reg, obj).run().spaghetti;
 
    for (const file of obj.getABAPFiles()) {
      const stru = file.getStructure();
      if (stru === undefined) {
        continue;
      }
 
      for (const c of stru.findAllExpressions(Expressions.MethodCall)) {
        if (c.findFirstExpression(Expressions.MethodParameters)) {
          continue;
        }
        // hmm, this will break for nested method calls?
        const parameters = c.findAllExpressions(Expressions.ParameterS);
        if (parameters.length > 1 || parameters.length === 0) {
          continue;
        }
        const name = c.findDirectExpression(Expressions.MethodName);
        if (name === undefined) {
          continue;
        }
        const param = c.findDirectExpression(Expressions.MethodCallParam);
        if (param === undefined) {
          continue;
        }
 
        const ref = this.findMethodReference(name.getFirstToken(), spaghetti, file.getFilename());
        if (ref === undefined) {
          continue;
        }
 
        const i = ref.getDefaultImporting();
        if (i === undefined) {
          continue;
        }
        const p = parameters[0].findDirectExpression(Expressions.ParameterName)?.getFirstToken();
 
        if (p?.getStr().toUpperCase() === i.toUpperCase()) {
          const message = "Omit default parameter name \"" + i + "\"";
          const end = parameters[0].findDirectExpression(Expressions.Source)?.getFirstToken().getStart();
          if (end) {
            const fix = EditHelper.deleteRange(file, p.getStart(), end);
            issues.push(Issue.atRange(file, p.getStart(), end, message, this.getMetadata().key, this.getConfig().severity, fix));
          } else {
            issues.push(Issue.atToken(file, name.getFirstToken(), message, this.getMetadata().key, this.getConfig().severity));
          }
        }
      }
    }
 
    return issues;
  }
 
///////////////////
 
  private findMethodReference(token: AbstractToken, spaghetti: ISpaghettiScope, filename: string): undefined | IMethodParameters {
    const scope = spaghetti.lookupPosition(token.getStart(), filename);
    if (scope === undefined) {
      return undefined;
    }
 
    for (const r of scope.getData().references) {
      if (r.referenceType !== ReferenceType.MethodReference
          && r.referenceType !== ReferenceType.BuiltinMethodReference) {
        continue;
      } else if (r.position.getStart().equals(token.getStart())) {
        if (r.resolved instanceof BuiltInMethod) {
          return r.resolved;
        } else if (r.resolved instanceof MethodDefinition) {
          return r.resolved.getParameters();
        }
      }
    }

    return undefined;
  }
 
}