All files / src/rules sy_modification.ts

100% Statements 66/66
100% Branches 16/16
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 11278x 11278x 11278x 11278x 11278x 33679x 33679x 33679x 33679x 33679x 33679x 33679x 33679x 33679x 33679x 33679x 33679x 33679x 33679x 33679x 11278x 11278x 10789x 10789x 11278x 11278x 229x 229x 11278x 11278x 245x 245x 245x 20x 20x 225x 245x 24x 24x 24x 5x 5x 1x 1x 4x 4x 4x 4x 4x 24x 225x 225x 225x 11278x 11278x
import {Issue} from "../issue";
import * as Expressions from "../abap/2_statements/expressions";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {ABAPObject} from "../objects/_abap_object";
 
export class SyModificationConf extends BasicRuleConfig {
}
 
export class SyModification extends ABAPRule {
 
  private conf = new SyModificationConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "sy_modification",
      title: "Modification of SY fields",
      shortDescription: `Finds modification of sy fields`,
      extendedInformation: `https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abensystem_fields.htm
 
Changes to SY-TVAR* fields are not reported
 
Modification of SY fields is not allwed in ABAP Cloud language version,
https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/ABENSYSTEM_FIELDS.html`,
      tags: [RuleTag.SingleFile],
      badExample: `sy-uname = 2.
sy = sy.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: SyModificationConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile, obj: ABAPObject) {
    const issues: Issue[] = [];
 
    if (obj.getType() === "INTF") {
      return [];
    }
 
    for (const t of file.getStructure()?.findAllExpressions(Expressions.Target) || []) {
      const firstChild = t.getChildren()[0];
      if (firstChild.get() instanceof Expressions.TargetField
          && firstChild.getFirstToken().getStr().toUpperCase() === "SY") {
 
        if (t.concatTokens().toUpperCase().startsWith("SY-TVAR")) {
          continue;
        }
 
        const message = "Modification of SY field";
        const issue = Issue.atToken(file, firstChild.getFirstToken(), message, this.getMetadata().key, this.conf.severity);
        issues.push(issue);
      }
    }
 
    return issues;
  }
 
}