All files / src/rules field_symbol_never_assigned.ts

100% Statements 163/163
96.36% Branches 53/55
100% Functions 9/9
100% Lines 163/163

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 1641x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21102x 21102x 21102x 21102x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 204503x 21102x 21102x 20205x 20205x 21102x 21102x 374x 374x 21102x 21102x 426x 426x 426x 426x 426x 3024x 3024x 3024x 3024x 117x 3024x 2907x 494x 2907x 117x 117x 117x 3024x 426x 426x 426x 21102x 21102x 21102x 21102x 117x 117x 117x 117x 117x 603x 603x 603x 603x 5x 603x 96x 96x 95x 95x 96x 598x 2x 2x 500x 500x 112x 112x 112x 117x 94x 94x 77x 77x 17x 17x 17x 112x 112x 21102x 21102x 21102x 1741x 3213x 1900x 1900x 1313x 1313x 3213x 3213x 26x 26x 26x 26x 3213x 4x 1287x 42x 42x 39x 39x 1283x 1241x 1241x 3213x 1741x 21102x 21102x 21102x 11x 11x 11x 11x 11x 21102x 21102x 112x 112x 37x 112x 3x 3x 72x 72x 21102x 21102x  
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {ABAPRule} from "./_abap_rule";
import {IRuleMetadata, RuleTag} from "./_irule";
import {StatementNode} from "../abap/nodes/statement_node";
import {ExpressionNode} from "../abap/nodes/expression_node";
import {TokenNode} from "../abap/nodes/token_node";
import {ABAPFile} from "../abap/abap_file";
import {AbstractToken} from "../abap/1_lexer/tokens/abstract_token";
import {MacroCall, Unknown} from "../abap/2_statements/statements/_statement";
 
export class FieldSymbolNeverAssignedConf extends BasicRuleConfig {
}
 
export class FieldSymbolNeverAssigned extends ABAPRule {
  private conf = new FieldSymbolNeverAssignedConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "field_symbol_never_assigned",
      title: "Field symbol never assigned",
      shortDescription: `Reports a local field symbol that is used, but never assigned in its method, form or function module`,
      extendedInformation: `A field symbol declared with FIELD-SYMBOLS in a method, form or function module can only be
assigned in that procedure. If no ASSIGN ... TO <fs> or ... ASSIGNING <fs> of it exists there,
reading or writing it raises the runtime error GETWA_NOT_ASSIGNED. This is typically a copy-paste
error, where a statement uses a field symbol other than the one assigned before it.
 
Assignments are ASSIGN, ASSIGN LOCAL COPY, and ASSIGNING of LOOP, READ TABLE, INSERT, APPEND,
MODIFY and COLLECT, anywhere in the procedure. The order of the statements is not checked.
 
Not reported: field symbols only used in IS [NOT] ASSIGNED or UNASSIGN, field symbols declared
with DEFAULT, inline declarations, global field symbols, and procedures that contain a macro
call, an INCLUDE or a statement that cannot be parsed.`,
      tags: [RuleTag.SingleFile],
      badExample: `FIELD-SYMBOLS <type> TYPE ty_type.
FIELD-SYMBOLS <attr> TYPE ty_attr.
LOOP AT types ASSIGNING <type>.
  CLEAR <attr>-author.
ENDLOOP.`,
      goodExample: `FIELD-SYMBOLS <type> TYPE ty_type.
LOOP AT types ASSIGNING <type>.
  CLEAR <type>-author.
ENDLOOP.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: FieldSymbolNeverAssignedConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile): Issue[] {
    const issues: Issue[] = [];
    const statements = file.getStatements();
 
    let start: number | undefined = undefined;
    for (let i = 0; i < statements.length; i++) {
      const s = statements[i].get();
      if (s instanceof Statements.MethodImplementation
          || s instanceof Statements.Form
          || s instanceof Statements.FunctionModule) {
        start = i;
      } else if (start !== undefined
          && (s instanceof Statements.EndMethod
          || s instanceof Statements.EndForm
          || s instanceof Statements.EndFunction)) {
        issues.push(...this.checkProcedure(file, statements.slice(start, i + 1)));
        start = undefined;
      }
    }
 
    return issues;
  }
 
////////////////
 
  private checkProcedure(file: ABAPFile, statements: readonly StatementNode[]): Issue[] {
    const declared = new Map<string, string>();
    const assigned = new Set<string>();
    const used = new Map<string, AbstractToken>();
 
    for (const statement of statements) {
      const s = statement.get();
      if (s instanceof Unknown
          || s instanceof MacroCall
          || s instanceof Statements.Include) {
        return [];
      } else if (s instanceof Statements.FieldSymbol) {
        const name = statement.findDirectExpression(Expressions.FieldSymbol)?.concatTokens();
        if (name !== undefined && statement.findDirectTokenByText("DEFAULT") === undefined) {
          declared.set(name.toUpperCase(), name);
        }
        continue;
      } else if (s instanceof Statements.Unassign) {
        continue;
      }
      this.walk(statement, assigned, used);
    }
 
    const issues: Issue[] = [];
    const procedure = this.procedureName(statements[0]);
    for (const [upper, name] of declared) {
      const token = used.get(upper);
      if (token === undefined || assigned.has(upper)) {
        continue;
      }
      const message = `Field symbol ${name} is never assigned in this ${procedure}`;
      issues.push(Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity));
    }
    return issues;
  }
 
  /** collects the assigned field symbols, and the first use of each other field symbol */
  private walk(node: StatementNode | ExpressionNode, assigned: Set<string>, used: Map<string, AbstractToken>): void {
    for (const child of node.getChildren()) {
      if (child instanceof TokenNode) {
        continue;
      }
      const e = child.get();
      if (e instanceof Expressions.FSTarget
          || e instanceof Expressions.InlineFS
          || (e instanceof Expressions.TargetFieldSymbol && this.isBinding(node))) {
        const name = child.findFirstExpression(Expressions.FieldSymbol)?.concatTokens();
        if (name !== undefined) {
          assigned.add(name.toUpperCase());
        }
      } else if (e instanceof Expressions.Compare && child.findDirectTokenByText("ASSIGNED") !== undefined) {
        continue;
      } else if (e instanceof Expressions.FieldSymbol) {
        const upper = child.concatTokens().toUpperCase();
        if (used.has(upper) === false) {
          used.set(upper, child.getFirstToken());
        }
      } else {
        this.walk(child as ExpressionNode, assigned, used);
      }
    }
  }
 
  /** TargetFieldSymbol directly in these binds the field symbol, elsewhere it is a write to the assigned data */
  private isBinding(parent: StatementNode | ExpressionNode): boolean {
    const p = parent.get();
    return p instanceof Statements.AssignLocalCopy
      || p instanceof Expressions.For
      || p instanceof Expressions.InlineLoopDefinition;
  }
 
  private procedureName(statement: StatementNode): string {
    const s = statement.get();
    if (s instanceof Statements.Form) {
      return "form";
    } else if (s instanceof Statements.FunctionModule) {
      return "function module";
    }
    return "method";
  }
 
}