All files / src/rules slow_parameter_passing.ts

98.64% Statements 146/148
83.33% Branches 30/36
100% Functions 8/8
98.64% Lines 146/148

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 1491x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11720x 11720x 11720x 11720x 11720x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 35003x 11720x 11720x 11181x 11181x 11720x 11720x 253x 253x 11720x 11720x 263x 263x 263x 11720x 11720x 351x 351x 351x 79x 79x 272x 272x 272x 272x 351x 71x 71x 7x 7x 7x 7x 71x 63x 63x 63x 54x 63x     9x 63x 6x 6x 6x 6x 6x 6x 6x 153x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 153x 6x 6x 6x 6x 63x 64x 272x 272x 272x 11720x 11720x 9x 9x 9x 18x 18x 18x 3x 3x 18x 9x 9x 9x 11720x 11720x 1283x 1283x 1283x 71x 1283x 1212x 1011x 1011x 1212x 1283x 1283x 1283x 11720x 11720x  
import {BasicRuleConfig} from "./_basic_rule_config";
import {Issue} from "../issue";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {IObject} from "../objects/_iobject";
import {ABAPObject} from "../objects/_abap_object";
import {IRegistry} from "../_iregistry";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {ISpaghettiScopeNode} from "../abap/5_syntax/_spaghetti_scope";
import {ScopeType} from "../abap/5_syntax/_scope_type";
import {IdentifierMeta, TypedIdentifier} from "../abap/types/_typed_identifier";
import {Position} from "../position";
import {ReferenceType} from "../abap/5_syntax/_reference";
import {EditHelper, IEdit} from "../edit_helper";
 
export class SlowParameterPassingConf extends BasicRuleConfig {
}
 
export class SlowParameterPassing implements IRule {
  private reg: IRegistry;
  private conf = new SlowParameterPassingConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "slow_parameter_passing",
      title: "Slow Parameter Passing",
      shortDescription: `Detects slow pass by value passing for methods where parameter is not changed`,
      extendedInformation: `Method parameters defined in interfaces is not checked`,
      tags: [RuleTag.Performance, RuleTag.Quickfix],
      badExample: `CLASS lcl DEFINITION.
  PUBLIC SECTION.
    METHODS bar IMPORTING VALUE(sdf) TYPE string.
ENDCLASS.
CLASS lcl IMPLEMENTATION.
  METHOD bar.
    WRITE sdf.
  ENDMETHOD.
ENDCLASS.`,
      goodExample: `CLASS lcl DEFINITION.
  PUBLIC SECTION.
    METHODS bar IMPORTING sdf TYPE string.
ENDCLASS.
CLASS lcl IMPLEMENTATION.
  METHOD bar.
    WRITE sdf.
  ENDMETHOD.
ENDCLASS.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: SlowParameterPassingConf): void {
    this.conf = conf;
  }
 
  public initialize(reg: IRegistry): IRule {
    this.reg = reg;
    return this;
  }
 
  public run(obj: IObject): readonly Issue[] {
    const issues: Issue[] = [];
 
    if (!(obj instanceof ABAPObject)) {
      return [];
    }
 
    const top = new SyntaxLogic(this.reg, obj).run().spaghetti.getTop();
    const methods = this.listMethodNodes(top);
 
    for (const m of methods) {
      const vars = m.getData().vars;
      if (m.getIdentifier().sname.includes("~")) {
        // skip methods defined in interfaces
        // todo: checking for just "~" is not correct, there might be ALIASES
        continue;
      }
      for (const v in vars) {
        const id = vars[v];
        if (id.getMeta().includes(IdentifierMeta.MethodImporting) === false
            || id.getMeta().includes(IdentifierMeta.PassByValue) === false) {
          continue;
        } else if (this.reg.isFileDependency(id.getFilename()) === true) {
          continue;
        }
        const writes = this.listWritePositions(m, id);
        if (writes.length === 0) {
          const message = "Parameter " + id.getName() + " passed by VALUE but not changed";
          let fix: IEdit | undefined = undefined;
 
          const file = obj.getABAPFiles().find(f => f.getFilename() === id.getFilename());
          if (file) {
            const tokens = file.getTokens();
            for (let i = 0; i < tokens.length; i++) {
              if (tokens[i].getStart().equals(id.getToken().getStart())) {
                const prev1 = tokens[i - 1];
                const prev2 = tokens[i - 2];
                const next1 = tokens[i + 1];
 
                if (prev1?.getStr() === "("
                    && prev2?.getStr().toUpperCase() === "VALUE"
                    && next1?.getStr() === ")") {
                  fix = EditHelper.replaceRange(file, prev2.getStart(), next1.getEnd(), id.getToken().getStr());
                }
                break;
              }
            }
          }
 
          issues.push(Issue.atIdentifier(id, message, this.getMetadata().key, this.getConfig().severity, fix));
        }
      }
    }
 
    return issues;
  }
 
  private listWritePositions(node: ISpaghettiScopeNode, id: TypedIdentifier): Position[] {
    const ret: Position[] = [];
 
    for (const v of node.getData().references) {
      if (v.referenceType === ReferenceType.DataWriteReference
          && v.resolved?.getFilename() === id.getFilename()
          && v.resolved?.getStart().equals(id.getStart())) {
        ret.push(v.position.getStart());
      }
    }
 
    return ret;
  }
 
  private listMethodNodes(node: ISpaghettiScopeNode): ISpaghettiScopeNode[] {
    const ret: ISpaghettiScopeNode[] = [];
 
    if (node.getIdentifier().stype === ScopeType.Method) {
      ret.push(node);
    } else {
      for (const c of node.getChildren()) {
        ret.push(...this.listMethodNodes(c));
      }
    }
 
    return ret;
  }
 
}