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 11590x 11590x 11590x 11590x 34633x 34633x 34633x 34633x 34633x 34633x 34633x 34633x 34633x 34633x 11590x 11590x 11055x 11055x 11590x 11590x 252x 252x 11590x 11590x 274x 274x 274x 274x     274x 274x 274x 12x 12x 262x 274x 9x 6x 6x 3x 3x 9x     3x 9x 9x 2x 2x 1x 1x 9x     1x 1x 1x 1x 1x 1x 9x 262x 262x 262x 11590x 11590x
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 {LanguageVersion, Release, releaseAtLeast} 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 (!releaseAtLeast(this.reg.getConfig().getRelease(), Release.v740sp02)
        && this.reg.getConfig().getLanguageVersion() !== LanguageVersion.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;
  }
 
}