All files / src/rules mix_returning.ts

100% Statements 70/70
100% Branches 14/14
100% Functions 6/6
100% Lines 70/70

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 721x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11149x 11149x 11149x 11149x 11149x 33242x 33242x 33242x 33242x 33242x 33242x 33242x 33242x 33242x 33242x 33242x 33242x 33242x 33242x 33242x 33242x 33242x 11149x 11149x 1x 1x 11149x 11149x 10583x 10583x 11149x 11149x 266x 266x 11149x 11149x 280x 280x 280x 12x 12x 268x 280x 60x 48x 48x 12x 60x 1x 1x 1x 1x 60x 268x 268x 268x 11149x 11149x    
/* eslint-disable max-len */
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
 
export class MixReturningConf extends BasicRuleConfig {
}
 
export class MixReturning extends ABAPRule {
 
  private conf = new MixReturningConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "mix_returning",
      title: "Mix of returning and exporting",
      shortDescription: `Checks that methods don't have a mixture of returning and exporting/changing parameters`,
      extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#use-either-returning-or-exporting-or-changing-but-not-a-combination
 
This syntax is not allowed on versions earlier than 740sp02, https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-us/abennews-740-abap_objects.htm#!ABAP_MODIFICATION_1@1@`,
      tags: [RuleTag.Styleguide, RuleTag.SingleFile, RuleTag.Syntax],
      badExample: `CLASS lcl DEFINITION.
  PUBLIC SECTION.
    METHODS
      foobar
        EXPORTING foo TYPE i
        RETURNING VALUE(rv_string) TYPE string.
ENDCLASS.`,
    };
  }
 
  private getMessage(): string {
    return "Don't mix RETURNING and EXPORTING/CHANGING parameters in a single method.";
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: MixReturningConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile): Issue[] {
    const ret: Issue[] = [];
    const stru = file.getStructure();
    if (stru === undefined) {
      return [];
    }
 
    for (const def of stru.findAllStatements(Statements.MethodDef)) {
      if (!def.findFirstExpression(Expressions.MethodDefReturning)) {
        continue;
      }
      if (def.findFirstExpression(Expressions.MethodDefExporting)
          || def.findFirstExpression(Expressions.MethodDefChanging)) {
        const token = def.getFirstToken();
        const issue = Issue.atToken(file, token, this.getMessage(), this.getMetadata().key, this.conf.severity);
        ret.push(issue);
      }
    }
 
    return ret;
  }
 
}