All files / src/rules superfluous_value.ts

92.4% Statements 73/79
77.27% Branches 17/22
100% Functions 5/5
92.4% Lines 73/79

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 791x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11120x 11120x 11120x 11120x 33165x 33165x 33165x 33165x 33165x 33165x 33165x 33165x 33165x 33165x 11120x 11120x 10557x 10557x 11120x 11120x 266x 266x 11120x 11120x 280x 280x 280x 280x     280x 280x 280x 12x 12x 268x 280x 15x 12x 12x 3x 3x 15x     3x 15x 15x 2x 2x 1x 1x 15x     1x 1x 1x 1x 1x 1x 15x 268x 268x 268x 11120x 11120x
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import {IRuleMetadata, RuleTag} from "./_irule";
import {Version} from "../version";
import {ABAPFile} from "../abap/abap_file";
 
export class SuperfluousValueConf extends BasicRuleConfig {
}
 
export class SuperfluousValue extends ABAPRule {
  private conf = new SuperfluousValueConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "superfluous_value",
      title: "Superfluous VALUE",
      shortDescription: `Find superfluous VALUE expressions`,
      extendedInformation: `Left hand side is inline, VALUE is inferred, value body is simple, from v740sp02 and up`,
      tags: [RuleTag.SingleFile],
      badExample: `DATA(message_entry) = VALUE #( message_table[ msgno = msgno ] ).`,
      goodExample: `DATA(message_entry) = message_table[ msgno = msgno ].`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: SuperfluousValueConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const output: Issue[] = [];
 
    if (this.reg.getConfig().getVersion() < Version.v740sp02
        && this.reg.getConfig().getVersion() !== Version.Cloud) {
      return [];
    }
 
    const struc = file.getStructure();
    if (struc === undefined) {
      return []; // syntax error
    }
 
    for (const m of struc.findAllStatements(Statements.Move)) {
      if (m.findDirectExpression(Expressions.Target)?.findDirectExpression(Expressions.InlineData) === undefined) {
        continue;
      }
 
      const source = m.findDirectExpression(Expressions.Source);
      if (source === undefined) {
        continue;
      }
 
      const type = source.findDirectExpression(Expressions.TypeNameOrInfer)?.concatTokens();
      if (type !== "#") {
        continue;
      }
 
      const body = source.findDirectExpression(Expressions.ValueBody);
      if (body === undefined) {
        continue;
      }
 
      if (body.getChildren().length === 1) {
        const message = "Superfluous VALUE expression";
        const issue = Issue.atStatement(file, m, message, this.getMetadata().key, this.conf.severity);
        output.push(issue);
      }
    }
 
    return output;
  }
 
}