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

97.9% Statements 326/333
96.81% Branches 91/94
100% Functions 29/29
97.9% Lines 326/333

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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 3331x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11941x 11941x 11941x 11941x 11941x 11941x 11941x 11941x 11941x 11941x 1x 1x 295236x 295236x 1x 1x 1x 1x 1x 1x 1x 1x 11941x 11941x 11941x 11941x 11941x 1x 1x 14504x 14504x 1x 1x 7349x 7349x 1x 1x 8475x 8475x 1x 1x 42x 42x 1x 1x 16727x 16727x 1x 1x 3628x     3628x 3628x 1x 1x 5195x 5195x 1x 1x 218x 218x 218x 742x 2x 2x 2x 2x 740x 740x 216x 216x 216x 1x 1x 2201x 2201x 2201x 2201x 5759x 2070x 1124x 1124x 2070x 4635x 4635x 1077x 1077x 1077x 1x 1x 12x 12x 12x 12x 30x 11x 10x 10x 11x 20x 20x 2x 2x 2x 1x 1x 1x 5x 5x 5x 5x 10x 5x 5x 10x 10x 5x 5x 5x 1x 1x 1x 634x 634x 634x 1725x 370x 264x 264x 370x 1461x 1461x 370x 370x 370x 1x 1x 493x 493x 493x 493x 1172x 1172x 223x 223x 949x 949x 270x 270x 270x 1x 1x 1364x 1364x 1364x 1364x 2313x 2313x 1049x 1049x 1264x 1264x 315x 315x 315x 1x 1x 3x 6x 6x 2x 2x 2x 2x 6x 1x 1x 1x 1x 1x 5x 7x 7x 7x 1x 1x 7x 4x 4x 4x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 5195x 5195x 1x 1x 1x 3x 3x 3x 9x 3x 3x 3x 3x 3x 3x 3x 9x 3x 3x 3x 1x 1x 3x 3x 3x 14x 8x 10x 3x 3x 10x 8x 14x 3x 3x 3x 1x 1x 2x 2x 2x 6x 2x 3x 2x 2x 3x 2x 6x 2x 2x 2x 1x 1x 817x     817x 817x 1x 1x 2x 2x 1x 1x 4937x 4937x 1x 1x 1x 1x 8x 8x 8x 8x 29x 29x 29x 29x 8x 8x 8x 1x 1x 3560x 3560x 764x 764x 2796x 2796x 3560x 2743x 2743x 1944x 1944x 2743x 852x 852x 3560x 814x 814x 38x 38x 38x 1x 1x
import {Position} from "../../position";
import {TypedIdentifier} from "../types/_typed_identifier";
import {Identifier} from "../4_file_information/_identifier";
import {IClassDefinition} from "../types/_class_definition";
import {IInterfaceDefinition} from "../types/_interface_definition";
import {IFormDefinition} from "../types/_form_definition";
import {IScopeData, IScopeIdentifier, IScopeVariable, ISpaghettiScopeNode, ISpaghettiScope} from "./_spaghetti_scope";
import {ReferenceType} from "./_reference";
 
abstract class ScopeData {
  private readonly data: IScopeData;
 
  public constructor() {
    this.data = {
      vars: {},
      cdefs: [],
      idefs: [],
      forms: [],
      types: {},
      deferred: [],
      references: [],
    };
  }
 
  public getData(): IScopeData {
    return this.data;
  }
}
 
export class SpaghettiScopeNode extends ScopeData implements ISpaghettiScopeNode {
  private readonly identifier: IScopeIdentifier;
  private readonly children: SpaghettiScopeNode[];
  private readonly parent: SpaghettiScopeNode | undefined;
 
  public constructor(identifier: IScopeIdentifier, parent: SpaghettiScopeNode | undefined) {
    super();
    this.identifier = identifier;
    this.parent = parent;
    this.children = [];
  }
 
  public getParent(): SpaghettiScopeNode | undefined {
    return this.parent;
  }
 
  public addChild(node: SpaghettiScopeNode) {
    this.children.push(node);
  }
 
  public getChildren(): SpaghettiScopeNode[] {
    return this.children;
  }
 
  public getFirstChild(): SpaghettiScopeNode | undefined {
    return this.children[0];
  }
 
  public getIdentifier(): IScopeIdentifier {
    return this.identifier;
  }
 
  public calcCoverage(): {start: Position, end: Position} {
    if (this.identifier.end === undefined) {
      throw new Error("internal error, caclCoverage");
    }
    return {start: this.identifier.start, end: this.identifier.end};
  }
 
  public setEnd(end: Position): void {
    this.identifier.end = end;
  }
 
  public findDeferred(name: string): Identifier | undefined {
    let search: SpaghettiScopeNode | undefined = this;
 
    while (search !== undefined) {
      for (const d of search.getData().deferred) {
        if (d.getStr().toUpperCase() === name.toUpperCase()) {
          return new Identifier(d, search.identifier.filename);
        }
      }
      search = search.getParent();
    }
 
    return undefined;
  }
 
  public findClassDefinition(name: string): IClassDefinition | undefined {
    let search: SpaghettiScopeNode | undefined = this;
 
    const upper = name.toUpperCase();
    while (search !== undefined) {
      for (const cdef of search.getData().cdefs) {
        if (cdef.getName().toUpperCase() === upper) {
          return cdef;
        }
      }
      search = search.getParent();
    }
 
    return undefined;
  }
 
  public findFormDefinition(name: string): IFormDefinition | undefined {
    let search: SpaghettiScopeNode | undefined = this;
 
    const upper = name.toUpperCase();
    while (search !== undefined) {
      for (const form of search.getData().forms) {
        if (form.getName().toUpperCase() === upper) {
          return form;
        }
      }
      search = search.getParent();
    }
 
    return undefined;
  }
 
  // todo, can be deleted, not called from anywhere?
  public listFormDefinitions(): IFormDefinition[] {
    let search: SpaghettiScopeNode | undefined = this;
    const ret: IFormDefinition[] = [];
 
    while (search !== undefined) {
      for (const form of search.getData().forms) {
        ret.push(form);
      }
      search = search.getParent();
    }
 
    return ret;
  }
 
  // todo, optimize
  public findInterfaceDefinition(name: string): IInterfaceDefinition | undefined {
    let search: SpaghettiScopeNode | undefined = this;
 
    while (search !== undefined) {
      for (const idef of search.getData().idefs) {
        if (idef.getName().toUpperCase() === name.toUpperCase()) {
          return idef;
        }
      }
      search = search.getParent();
    }
 
    return undefined;
  }
 
  public findType(name: string): TypedIdentifier | undefined {
    let search: SpaghettiScopeNode | undefined = this;
 
    const upper = name.toUpperCase();
    while (search !== undefined) {
      const data = search.getData();
      if (data.types[upper]) {
        return data.types[upper];
      }
      search = search.getParent();
    }
 
    return undefined;
  }
 
  public findVariable(name: string): TypedIdentifier | undefined {
    let search: SpaghettiScopeNode | undefined = this;
 
    const upper = name.toUpperCase();
    while (search !== undefined) {
      const data = search.getData();
      if (data.vars[upper]) {
        return data.vars[upper];
      }
      search = search.getParent();
    }
 
    return undefined;
  }
 
  public findWriteReference(pos: Position): TypedIdentifier | undefined {
    for (const r of this.getData().references) {
      if (r.referenceType === ReferenceType.DataWriteReference
          && r.position.getStart().equals(pos)) {
        if (r.resolved instanceof TypedIdentifier) {
          return r.resolved;
        }
      }
    }
 
    return undefined;
  }
 
  public findTableReference(pos: Position): string | undefined {
    for (const r of this.getData().references) {
      if (r.referenceType === ReferenceType.TableReference
          && r.position.getStart().equals(pos)
          && r.resolved) {
        return r.resolved.getName();
      }
    }
 
    return undefined;
  }
 
  // TODO, this method can be deleted? its only used in tests?
  public findScopeForVariable(name: string): IScopeIdentifier | undefined {
    let search: SpaghettiScopeNode | undefined = this;
 
    while (search !== undefined) {
      for (const local in search.getData().vars) {
        if (local === name.toUpperCase()) {
          return search.getIdentifier();
        }
      }
      search = search.getParent();
    }

    return undefined;
  }
 
}
 
export class SpaghettiScope implements ISpaghettiScope {
  private readonly node: SpaghettiScopeNode;
 
  public constructor(top: SpaghettiScopeNode) {
    this.node = top;
  }
 
  // list variable definitions across all nodes
  public listDefinitions(filename: string): IScopeVariable[] {
    const ret: IScopeVariable[] = [];
 
    for (const n of this.allNodes()) {
      if (n.getIdentifier().filename === filename) {
        const vars = n.getData().vars;
        for (const v in vars) {
          if (vars[v].getFilename() === filename) {
            ret.push({name: v, identifier: vars[v]});
          }
        }
      }
    }
 
    return ret;
  }
 
  public listReadPositions(filename: string): Identifier[] {
    const ret: Identifier[] = [];
 
    for (const n of this.allNodes()) {
      if (n.getIdentifier().filename === filename) {
        for (const v of n.getData().references) {
          if (v.referenceType === ReferenceType.DataReadReference && v.position.getFilename() === filename) {
            ret.push(v.position);
          }
        }
      }
    }
 
    return ret;
  }
 
  public listWritePositions(filename: string): Identifier[] {
    const ret: Identifier[] = [];
 
    for (const n of this.allNodes()) {
      if (n.getIdentifier().filename === filename) {
        for (const v of n.getData().references) {
          if (v.referenceType === ReferenceType.DataWriteReference && v.position.getFilename() === filename) {
            ret.push(v.position);
          }
        }
      }
    }
 
    return ret;
  }
 
  public lookupPosition(p: Position | undefined, filename: string | undefined): SpaghettiScopeNode | undefined {
    if (p === undefined || filename === undefined) {
      return undefined;
    }
    return this.lookupPositionTraverse(p, filename, this.node);
  }
 
  public getFirstChild() {
    return this.node.getFirstChild();
  }
 
  public getTop(): SpaghettiScopeNode {
    return this.node;
  }
 
/////////////////////////////
 
  private allNodes(): SpaghettiScopeNode[] {
    const ret: SpaghettiScopeNode[] = [];
    const stack: SpaghettiScopeNode[] = [this.node];
 
    while (stack.length > 0) {
      const current = stack.pop()!;
      ret.push(current);
      stack.push(...current.getChildren());
    }
 
    return ret;
  }
 
  private lookupPositionTraverse(p: Position, filename: string, node: SpaghettiScopeNode): SpaghettiScopeNode | undefined {
    const coverage = node.calcCoverage();
    if (node.getIdentifier().filename === filename && p.isBetween(coverage.start, coverage.end) === false) {
      return undefined;
    }
 
    // possible optimization: binary search the nodes
    for (const c of node.getChildren()) {
      const result = this.lookupPositionTraverse(p, filename, c);
      if (result !== undefined) {
        return result;
      }
    }
 
    if (node.getIdentifier().filename === filename
        && p.isBetween(coverage.start, coverage.end)) {
      return node;
    }
 
    return undefined;
  }
 
}