All files / src/abap/5_syntax _super_constructor_call.ts

98.03% Statements 200/204
89.77% Branches 79/88
100% Functions 9/9
98.03% Lines 200/204

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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 2051x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29385x 29385x 29385x 27223x 27223x 2162x 2162x 29385x 29385x 2126x 2126x 36x 36x 29385x 145x 145x 145x     145x 36x 36x 36x 29385x 3x 3x 3x 3x 33x 33x 33x 33x 33x 33x 29385x 8x 8x 8x 29385x 1x 1x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 1x 1x 104x 104x 179x 179x 179x 33x 33x 179x 71x 71x 1x 1x 1x 33x 33x 33x 33x 33x 33x 33x 33x 33x 16x 16x 71x 71x 1x 1x 70x 70x 71x 71x 7x 7x 71x 63x 25x 25x 1x 1x 1x 1x 71x 71x 66x 66x 5x 71x     71x 71x 1x 1x 1x 9x 9x 3x 9x 4x 4x 9x 9x 1x 1x 1x 33x 33x 33x 33x 88x 88x 88x 5x 5x 83x 88x 36x 36x 88x 143x 128x 128x 143x 88x 22x 22x 22x 22x 22x 83x 83x 83x 33x 33x 83x 88x 33x 33x 1x 1x 1x 33x 33x 33x 66x 66x 66x 170x 3x 3x 167x 170x 170x 170x 170x 66x 33x 33x 1x 1x 237x 237x 1x 1x  
import * as Expressions from "../2_statements/expressions";
import * as Statements from "../2_statements/statements";
import {MacroCall, NativeSQL, Unknown} from "../2_statements/statements/_statement";
import {Dash} from "../1_lexer/tokens/dash";
import {Identifier as IdentifierToken} from "../1_lexer/tokens/identifier";
import {AbstractToken} from "../1_lexer/tokens/abstract_token";
import {StatementNode, StructureNode} from "../nodes";
import {VirtualPosition} from "../../virtual_position";
import {Identifier} from "../4_file_information/_identifier";
import {IClassDefinition} from "../types/_class_definition";
import {IInterfaceDefinition} from "../types/_interface_definition";
import {ISpaghettiScopeNode} from "./_spaghetti_scope";
import {SyntaxInput, syntaxIssue} from "./_syntax_input";
 
/** The constructor of a subclass must call SUPER->CONSTRUCTOR, and before the call instance
 *  attributes, instance methods and ME cannot be used. Runs when the method has been traversed,
 *  scopeNode is the outermost scope of the method, holding the references of its body. */
export class SuperConstructorCall {
 
  public static check(method: StructureNode, scopeNode: ISpaghettiScopeNode | undefined, input: SyntaxInput): void {
    const methodStatement = method.findDirectStatement(Statements.MethodImplementation);
    const methodName = methodStatement?.findDirectExpression(Expressions.MethodName)?.concatTokens();
    if (methodStatement === undefined || methodName?.toUpperCase() !== "CONSTRUCTOR") {
      return;
    }
 
    const classDefinition = input.scope.findClassDefinition(input.scope.getName());
    const superName = classDefinition?.getSuperClass();
    if (classDefinition === undefined || superName === undefined || superName.toUpperCase() === "OBJECT") {
      return;
    }
 
    const statements = method.findAllStatementNodes();
    for (const s of statements) {
      const type = s.get();
      if (type instanceof Unknown || type instanceof MacroCall
          || s.getFirstToken().getStart() instanceof VirtualPosition) {
        return;
      }
    }
 
    const callIndex = statements.findIndex(s => SuperConstructorCall.isSuperConstructorCall(s));
    // at most one issue is reported per constructor
    if (callIndex < 0) {
      const message = "The constructor of a subclass must call SUPER->CONSTRUCTOR";
      input.issues.push(syntaxIssue(input, methodStatement.getFirstToken(), message));
      return;
    }
 
    // the statements before the call, the call itself can pass instance attributes, eg. previous = previous,
    // declarations can refer to instance attributes for typing, eg. DATA ls_row LIKE LINE OF mt_table
    const before = statements.slice(1, callIndex)
      .filter(s => !(s.get() instanceof NativeSQL) && !SuperConstructorCall.isDeclaration(s));
    const offending = SuperConstructorCall.findInstanceAccess(before, classDefinition, scopeNode, input);
    if (offending !== undefined) {
      const message = "Instance attributes, instance methods and ME can only be accessed after SUPER->CONSTRUCTOR is called";
      input.issues.push(syntaxIssue(input, offending, message));
    }
  }
 
  private static isDeclaration(statement: StatementNode): boolean {
    const type = statement.get();
    return type instanceof Statements.Data
      || type instanceof Statements.DataBegin
      || type instanceof Statements.DataEnd
      || type instanceof Statements.Constant
      || type instanceof Statements.ConstantBegin
      || type instanceof Statements.ConstantEnd
      || type instanceof Statements.Type
      || type instanceof Statements.TypeBegin
      || type instanceof Statements.TypeEnd
      || type instanceof Statements.Static
      || type instanceof Statements.StaticBegin
      || type instanceof Statements.StaticEnd
      || type instanceof Statements.IncludeType
      || type instanceof Statements.FieldSymbol;
  }
 
  private static isSuperConstructorCall(statement: StatementNode): boolean {
    const tokens = statement.getTokens();
    for (let i = 0; i < tokens.length - 2; i++) {
      if (tokens[i].getStr().toUpperCase() === "SUPER"
          && tokens[i + 1].getStr() === "->"
          && tokens[i + 2].getStr().toUpperCase() === "CONSTRUCTOR") {
        return true;
      }
    }
    return false;
  }
 
  /** returns the first offending token */
  private static findInstanceAccess(
    statements: StatementNode[],
    classDefinition: IClassDefinition,
    scopeNode: ISpaghettiScopeNode | undefined,
    input: SyntaxInput): AbstractToken | undefined {
 
    const instance = SuperConstructorCall.listInstanceMembers(classDefinition, input);
    const references = SuperConstructorCall.listReferences(scopeNode, input.filename);
 
    for (const statement of statements) {
      const tokens = statement.getTokens();
      for (let i = 0; i < tokens.length; i++) {
        const token = tokens[i];
        if (SuperConstructorCall.isMe(tokens, i)) {
          return token;
        }
        const resolved = references.get(SuperConstructorCall.key(token));
        if (resolved !== undefined
            && resolved.some(r => instance.has(r))
            && SuperConstructorCall.isOwnAccess(tokens, i)) {
          return token;
        }
      }
    }
    return undefined;
  }
 
  /** ME used as a value, me->component is allowed for constants and static components,
   *  instance components are found via the references */
  private static isMe(tokens: readonly AbstractToken[], index: number): boolean {
    const token = tokens[index];
    if (!(token instanceof IdentifierToken) || token.getStr().toUpperCase() !== "ME") {
      return false;
    }
    const prev = tokens[index - 1];
    if (prev !== undefined && (prev instanceof Dash || ["->", "=>", "~"].includes(prev.getStr()))) {
      return false;
    }
    return tokens[index + 1]?.getStr() !== "->";
  }
 
  /** the member is accessed via the implicit or explicit self reference, not via another object or class */
  private static isOwnAccess(tokens: readonly AbstractToken[], index: number): boolean {
    const prev = tokens[index - 1];
    if (prev === undefined) {
      return true;
    } else if (prev.getStr() === "->") {
      return tokens[index - 2]?.getStr().toUpperCase() === "ME";
    }
    return !(prev instanceof Dash || ["=>", "~"].includes(prev.getStr()));
  }
 
  /** instance attributes and methods of the class, its super classes and implemented interfaces */
  private static listInstanceMembers(classDefinition: IClassDefinition, input: SyntaxInput): Set<Identifier> {
    const ret = new Set<Identifier>();
    const visited = new Set<string>();
    const stack: (IClassDefinition | IInterfaceDefinition)[] = [classDefinition];
    while (stack.length > 0) {
      const current = stack.pop()!;
      const name = current.getName().toUpperCase();
      if (visited.has(name)) {
        continue;
      }
      visited.add(name);
      for (const a of current.getAttributes().getInstance()) {
        ret.add(a);
      }
      for (const m of current.getMethodDefinitions().getAll()) {
        if (m.isStatic() === false) {
          ret.add(m);
        }
      }
      for (const i of current.getImplementing()) {
        const intf = input.scope.findInterfaceDefinition(i.name);
        if (intf !== undefined) {
          stack.push(intf);
        }
      }
      if ("getSuperClass" in current) {
        const sup = input.scope.findClassDefinition(current.getSuperClass());
        if (sup !== undefined) {
          stack.push(sup);
        }
      }
    }
    return ret;
  }
 
  /** resolved references of the method body by token position */
  private static listReferences(node: ISpaghettiScopeNode | undefined, filename: string): Map<string, Identifier[]> {
    const ret = new Map<string, Identifier[]>();
    const stack: ISpaghettiScopeNode[] = node ? [node] : [];
    while (stack.length > 0) {
      const current = stack.pop()!;
      stack.push(...current.getChildren());
      for (const r of current.getData().references) {
        if (r.resolved === undefined || r.position.getFilename() !== filename) {
          continue;
        }
        const key = SuperConstructorCall.key(r.position.getToken());
        const list = ret.get(key) ?? [];
        list.push(r.resolved);
        ret.set(key, list);
      }
    }
    return ret;
  }
 
  private static key(token: AbstractToken): string {
    return token.getRow() + ":" + token.getCol();
  }
 
}