All files / src/rules unnecessary_return.ts

100% Statements 66/66
100% Branches 13/13
100% Functions 5/5
100% Lines 66/66

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 661x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9427x 9427x 9427x 9427x 28111x 28111x 28111x 28111x 28111x 28111x 28111x 28111x 28111x 28111x 28111x 28111x 28111x 28111x 28111x 9427x 9427x 8959x 8959x 9427x 9427x 218x 218x 9427x 9427x 238x 238x 238x 238x 12x 12x 226x 226x 238x 1126x 1126x 1126x 1126x 9x 1126x 3x 3x 3x 3x 3x 1126x 226x 226x 226x 9427x 9427x
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 * as Statements from "../abap/2_statements/statements";
import {EditHelper} from "../edit_helper";
 
export class UnnecessaryReturnConf extends BasicRuleConfig {
}
 
export class UnnecessaryReturn extends ABAPRule {
  private conf = new UnnecessaryReturnConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "unnecessary_return",
      title: "Unnecessary Return",
      shortDescription: `Finds unnecessary RETURN statements`,
      extendedInformation: `Finds unnecessary RETURN statements`,
      tags: [RuleTag.SingleFile, RuleTag.Quickfix],
      badExample: `METHOD hello.
  ...
  RETURN.
ENDMETHOD.`,
      goodExample: `METHOD hello.
  ...
ENDMETHOD.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: UnnecessaryReturnConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    const structure = file.getStructure();
    if (structure === undefined) {
      return [];
    }
 
    const statements = file.getStatements();
    for (let i = 0; i < statements.length - 1; i++) {
      const node = statements[i];
      const next = statements[i + 1];
      if (node.get() instanceof Statements.Return
          && (next.get() instanceof Statements.EndMethod
          || next.get() instanceof Statements.EndForm
          || next.get() instanceof Statements.EndFunction)) {
 
        const message = "Unnecessary RETURN";
        const fix = EditHelper.deleteStatement(file, node);
        issues.push(Issue.atStatement(file, node, message, this.getMetadata().key, this.getConfig().severity, fix));
      }
    }
 
    return issues;
  }
 
}