All files / src/abap/4_file_information abap_file_information.ts

97.14% Statements 409/421
81.72% Branches 76/93
100% Functions 17/17
97.14% Lines 409/421

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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 4211x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7432x 7432x 7432x 7432x 7432x 7432x 7432x 1x 1x 686x 686x 1x 1x 1984x 1984x 1x 1x 83x 83x 90x 81x 81x 90x 2x 2x 1x 1x 4579x 4579x 1x 1x 974x 974x 950x 932x 932x 950x 42x 42x 1x 1x 157x 157x 161x 142x 142x 161x 15x 15x 1x 1x 269x 269x 1x 1x 1x 1x 7432x 576x 576x 6856x 6856x 6856x 6856x 7432x 1033x 1033x 653x 653x 653x 653x 653x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 6856x 7432x 324x 324x 324x 324x 324x 324x 324x 324x 324x 6856x 1x 1x 6856x 282x 282x     282x 282x 282x 282x 282x 282x 282x 282x 282x 282x 282x 282x 282x 282x 282x 282x 282x 282x 282x 6856x 1x 1x 6856x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1x 1128x   1127x 5x 5x 1128x 1128x 1128x 6x 1128x   1122x     1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 1128x 6856x 1x 1x 1x 1x 1410x 1410x 113x 113x 113x 1x 1x 1x 1x 113x 113x 113x 113x         113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 1410x 1410x 1x 1x 3666x 2482x 2482x 1184x 1184x 3666x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 1184x 1184x 1184x 1x 1x 3666x 2482x 2482x 1184x 1184x 3666x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 1184x 1184x 1184x 1x 1x 3666x 2482x 2482x 1184x 1184x 3666x 90x 90x 1094x 1094x 3666x 179x 179x 179x 179x 179x 179x 179x 179x 179x 3666x 23x 23x 23x 23x 23x 23x 23x 23x 23x 3666x 106x 106x 106x 106x 106x 106x 106x 106x 106x 1094x 1094x 1094x 1x 1x 3666x 2482x 2482x 1184x 1184x 3666x 783x 783x     783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 1184x 1184x 1x 1x 1x 783x 783x 783x 783x 277x 293x 293x 293x 293x 293x 293x 293x 293x 293x 277x 783x 783x 783x 21x 22x 22x 22x 22x 22x 22x 22x 22x 22x 21x 783x 783x 783x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 783x 783x 783x 126x 126x 126x 126x 126x 126x 126x 126x 126x 783x 783x 783x 1x 1x
import * as Structures from "../3_structures/structures";
import * as Expressions from "../2_statements/expressions";
import * as Statements from "../2_statements/statements";
import {IABAPFileInformation, InfoClassImplementation, InfoClassDefinition, InfoMethodDefinition, InfoInterfaceDefinition, InfoAttribute, InfoAlias, AttributeLevel, InfoMethodParameter, MethodParameterDirection, InfoFormDefinition, InfoImplementing, InfoConstant, Duration, RiskLevel} from "./_abap_file_information";
import {StructureNode, StatementNode} from "../nodes";
import {Identifier} from "./_identifier";
import * as Tokens from "../1_lexer/tokens";
import {Visibility} from "./visibility";
 
export class ABAPFileInformation implements IABAPFileInformation {
  private readonly interfaces: InfoInterfaceDefinition[];
  private readonly classes: InfoClassDefinition[];
  private readonly forms: InfoFormDefinition[];
  private readonly implementations: InfoClassImplementation[];
  private readonly filename: string;
 
  public constructor(structure: StructureNode | undefined, filename: string) {
    this.forms = [];
    this.implementations = [];
    this.interfaces = [];
    this.classes = [];
    this.filename = filename;
    this.parse(structure);
  }
 
  public listClassImplementations(): readonly InfoClassImplementation[] {
    return this.implementations;
  }
 
  public listInterfaceDefinitions(): readonly InfoInterfaceDefinition[] {
    return this.interfaces;
  }
 
  public getInterfaceDefinitionByName(name: string): InfoInterfaceDefinition | undefined {
    const upper = name.toUpperCase();
    for (const i of this.listInterfaceDefinitions()) {
      if (i.identifier.getName().toUpperCase() === upper) {
        return i;
      }
    }
    return undefined;
  }
 
  public listClassDefinitions(): readonly InfoClassDefinition[] {
    return this.classes;
  }
 
  public getClassDefinitionByName(name: string): InfoClassDefinition | undefined {
    const upper = name.toUpperCase();
    for (const d of this.listClassDefinitions()) {
      if (d.identifier.getName().toUpperCase() === upper) {
        return d;
      }
    }
    return undefined;
  }
 
  public getClassImplementationByName(name: string): InfoClassImplementation | undefined {
    const upper = name.toUpperCase();
    for (const impl of this.listClassImplementations()) {
      if (impl.identifier.getName().toUpperCase() === upper) {
        return impl;
      }
    }
    return undefined;
  }
 
  public listFormDefinitions(): InfoFormDefinition[] {
    return this.forms;
  }
 
  ///////////////////////
 
  private parse(structure: StructureNode | undefined): void {
    if (structure === undefined) {
      return;
    }
 
    this.parseClasses(structure);
    this.parseInterfaces(structure);
 
    for (const found of structure.findAllStructures(Structures.ClassImplementation)) {
      const methods = [];
      for (const method of found.findAllStructures(Structures.Method)) {
        const methodName = method.findFirstExpression(Expressions.MethodName)?.getFirstToken();
        if (methodName) {
          methods.push(new Identifier(methodName, this.filename));
        }
      }
 
      const name = found.findFirstStatement(Statements.ClassImplementation)!.findFirstExpression(Expressions.ClassName)!.getFirstToken();
      this.implementations.push({
        name: name.getStr(),
        identifier: new Identifier(name, this.filename),
        methods,
      });
    }
 
    for (const statement of structure.findAllStructures(Structures.Form)) {
      // FORMs can contain a dash in the name
      const pos = statement.findFirstExpression(Expressions.FormName)!.getFirstToken().getStart();
      const name = statement.findFirstExpression(Expressions.FormName)!.concatTokens();
      const nameToken = new Tokens.Identifier(pos, name);
      this.forms.push({
        name: nameToken.getStr(),
        identifier: new Identifier(nameToken, this.filename),
      });
    }
  }
 
  private parseInterfaces(structure: StructureNode) {
    for (const found of structure.findDirectStructures(Structures.Interface)) {
      const i = found.findFirstStatement(Statements.Interface);
      if (i === undefined) {
        throw new Error("Interface expected, parseInterfaces");
      }
      const interfaceName = i.findDirectExpression(Expressions.InterfaceName)!.getFirstToken();
      const methods = this.parseMethodDefinition(found, Visibility.Public);
      const attributes = this.parseAttributes(found, Visibility.Public);
      const aliases = this.parseAliases(found, Visibility.Public);
      const constants = this.parseConstants(found, Visibility.Public);
      const g = i.findDirectExpression(Expressions.ClassGlobal);
 
      this.interfaces.push({
        name: interfaceName.getStr(),
        identifier: new Identifier(interfaceName, this.filename),
        isLocal: g === undefined,
        isGlobal: g !== undefined,
        interfaces: this.getImplementing(found),
        aliases,
        methods,
        constants,
        attributes,
      });
    }
  }
 
  private parseClasses(structure: StructureNode) {
    for (const found of structure.findAllStructures(Structures.ClassDefinition)) {
      const className = found.findFirstStatement(Statements.ClassDefinition)!.findFirstExpression(Expressions.ClassName)!.getFirstToken();
 
      const methods = this.parseMethodDefinition(found.findFirstStructure(Structures.PublicSection), Visibility.Public);
      methods.push(...this.parseMethodDefinition(found.findFirstStructure(Structures.ProtectedSection), Visibility.Protected));
      methods.push(...this.parseMethodDefinition(found.findFirstStructure(Structures.PrivateSection), Visibility.Private));
 
      const attributes = this.parseAttributes(found.findFirstStructure(Structures.PublicSection), Visibility.Public);
      attributes.push(...this.parseAttributes(found.findFirstStructure(Structures.ProtectedSection), Visibility.Protected));
      attributes.push(...this.parseAttributes(found.findFirstStructure(Structures.PrivateSection), Visibility.Private));
 
      const aliases = this.parseAliases(found.findFirstStructure(Structures.PublicSection), Visibility.Public);
      aliases.push(...this.parseAliases(found.findFirstStructure(Structures.ProtectedSection), Visibility.Protected));
      aliases.push(...this.parseAliases(found.findFirstStructure(Structures.PrivateSection), Visibility.Private));
 
      const constants = this.parseConstants(found.findFirstStructure(Structures.PublicSection), Visibility.Public);
      constants.push(...this.parseConstants(found.findFirstStructure(Structures.ProtectedSection), Visibility.Protected));
      constants.push(...this.parseConstants(found.findFirstStructure(Structures.PrivateSection), Visibility.Private));
 
      const superClassName = found.findFirstExpression(Expressions.SuperClassName)?.getFirstToken().getStr();
      const containsGlobal = found.findFirstExpression(Expressions.ClassGlobal);
      const cdef = found.findFirstStatement(Statements.ClassDefinition);
      const concat = cdef?.concatTokens().toUpperCase() || "";
 
      let riskLevel: RiskLevel | undefined;
      if (concat.includes("RISK LEVEL CRITICAL")) {
        riskLevel = RiskLevel.critical;
      } else if (concat.includes("RISK LEVEL DANGEROUS")) {
        riskLevel = RiskLevel.dangerous;
      } else if (concat.includes("RISK LEVEL HARMLESS")) {
        riskLevel = RiskLevel.harmless;
      }
 
      let duration: Duration | undefined;
      if (concat.includes("DURATION SHORT")) {
        duration = Duration.short;
      } else if (concat.includes("DURATION LONG")) {
        duration = Duration.long;
      } else if (concat.includes("DURATION MEDIUM")) {
        duration = Duration.medium;
      }
 
      this.classes.push({
        name: className.getStr(),
        identifier: new Identifier(className, this.filename),
        isLocal: containsGlobal === undefined,
        isGlobal: containsGlobal !== undefined,
        methods,
        superClassName,
        interfaces: this.getImplementing(found),
        isForTesting: concat.includes(" FOR TESTING"),
        duration,
        riskLevel,
        isAbstract: cdef?.findDirectTokenByText("ABSTRACT") !== undefined,
        isSharedMemory: concat.includes(" SHARED MEMORY ENABLED"),
        isFinal: found.findFirstExpression(Expressions.ClassFinal) !== undefined,
        aliases,
        attributes,
        constants,
      });
    }
  }
 
  ///////////////////
 
  private getImplementing(input: StructureNode): InfoImplementing[] {
    const ret: InfoImplementing[] = [];
    for (const node of input.findAllStatements(Statements.InterfaceDef)) {
      const abstract = node.findDirectExpression(Expressions.AbstractMethods);
      const abstractMethods: string[] = [];
      if (abstract) {
        for (const m of abstract.findDirectExpressions(Expressions.MethodName)) {
          abstractMethods.push(m.concatTokens().toUpperCase());
        }
      }
 
      const final = node.findDirectExpression(Expressions.FinalMethods);
      const finalMethods: string[] = [];
      if (final) {
        for (const m of final.findDirectExpressions(Expressions.MethodName)) {
          finalMethods.push(m.concatTokens().toUpperCase());
        }
      }
 
      const concat = node.concatTokens().toUpperCase();
      const allAbstract = concat.includes(" ALL METHODS ABSTRACT");
      const partial = concat.includes(" PARTIALLY IMPLEMENTED");
      const name = node.findFirstExpression(Expressions.InterfaceName)!.getFirstToken().getStr().toUpperCase();
      ret.push({
        name,
        partial,
        allAbstract,
        abstractMethods,
        finalMethods,
      });
    }
    return ret;
  }
 
  private parseAliases(node: StructureNode | undefined, visibility: Visibility): InfoAlias[] {
    if (node === undefined) {
      return [];
    }
 
    const ret: InfoAlias[] = [];
    for (const a of node.findAllStatements(Statements.Aliases)) {
      const name = a.findFirstExpression(Expressions.SimpleName)!.getFirstToken();
      const comp = a.findFirstExpression(Expressions.Field)!.getFirstToken();
 
      ret.push({
        name: name.getStr(),
        identifier: new Identifier(name, this.filename),
        visibility,
        component: comp.getStr(),
      });
    }
 
    return ret;
  }
 
  private parseConstants(node: StructureNode | undefined, visibility: Visibility): InfoConstant[] {
    if (node === undefined) {
      return [];
    }
 
    const results: InfoConstant[] = [];
    for (const constant of node.findAllStatements(Statements.Constant)) {
      const name = constant.findFirstExpression(Expressions.DefinitionName)!.getFirstToken();
      const typeName = constant.findFirstExpression(Expressions.TypeName);
 
      // VALUE `const_value` -> `const_value`
      const literal = constant.findFirstExpression(Expressions.Value)?.getTokens()[1].getStr() ?? "``";
      // `const_value` -> const_value
      const value = literal.slice(1, literal?.length - 1);
 
      results.push({
        name: name!.getStr(),
        typeName: typeName ? typeName.getFirstToken().getStr() : "",
        value: value,
        identifier: new Identifier(name, this.filename),
        visibility,
      });
    }
 
    return results;
  }
 
  private parseAttributes(node: StructureNode | undefined, visibility: Visibility): InfoAttribute[] {
    if (node === undefined) {
      return [];
    }
 
    const contents = node.findFirstStructure(Structures.SectionContents);
    if (contents === undefined) {
      return [];
    }
 
    const ret: InfoAttribute[] = [];
    for (const d of contents.findDirectStatements(Statements.Data)) {
      const name = d.findFirstExpression(Expressions.DefinitionName)!.getFirstToken();
      ret.push({
        name: name.getStr(),
        identifier: new Identifier(name, this.filename),
        level: AttributeLevel.Instance,
        readOnly: d.concatTokens().toUpperCase().includes(" READ-ONLY"),
        visibility,
      });
    }
    for (const d of contents.findDirectStatements(Statements.ClassData)) {
      const name = d.findFirstExpression(Expressions.DefinitionName)!.getFirstToken();
      ret.push({
        name: name.getStr(),
        identifier: new Identifier(name, this.filename),
        level: AttributeLevel.Static,
        readOnly: d.concatTokens().toUpperCase().includes(" READ-ONLY"),
        visibility,
      });
    }
    for (const d of contents.findDirectStatements(Statements.Constant)) {
      const name = d.findFirstExpression(Expressions.DefinitionName)!.getFirstToken();
      ret.push({
        name: name.getStr(),
        identifier: new Identifier(name, this.filename),
        level: AttributeLevel.Constant,
        readOnly: true,
        visibility,
      });
    }
 
    return ret;
  }
 
  private parseMethodDefinition(node: StructureNode | undefined, visibility: Visibility): InfoMethodDefinition[] {
    if (node === undefined) {
      return [];
    }
 
    const methods: InfoMethodDefinition[] = [];
    for (const def of node.findAllStatements(Statements.MethodDef)) {
      const methodName = def.findDirectExpression(Expressions.MethodName)?.getFirstToken();
      if (methodName === undefined) {
        continue;
      }
 
      const parameters = this.parseMethodParameters(def);
 
      methods.push({
        name: methodName.getStr(),
        identifier: new Identifier(methodName, this.filename),
        isRedefinition: def.findDirectExpression(Expressions.Redefinition) !== undefined,
        isForTesting: def.concatTokens().toUpperCase().includes(" FOR TESTING"),
        isAbstract: def.findDirectExpression(Expressions.Abstract) !== undefined,
        isEventHandler: def.findDirectExpression(Expressions.EventHandler) !== undefined,
        visibility,
        parameters,
        exceptions: [], // todo
      });
    }
    return methods;
  }
 
  // todo, refactor this method, it is too long
  private parseMethodParameters(node: StatementNode): InfoMethodParameter[] {
    const ret: InfoMethodParameter[] = [];
 
    const importing = node.findFirstExpression(Expressions.MethodDefImporting);
    if (importing) {
      for (const param of importing.findAllExpressions(Expressions.MethodParam)) {
        const name = param.findDirectExpression(Expressions.MethodParamName)?.getFirstToken();
        if (name) {
          ret.push({
            name: name.getStr().replace("!", ""),
            identifier: new Identifier(name, this.filename),
            direction: MethodParameterDirection.Importing,
          });
        }
      }
    }
 
    const exporting = node.findFirstExpression(Expressions.MethodDefExporting);
    if (exporting) {
      for (const param of exporting.findAllExpressions(Expressions.MethodParam)) {
        const name = param.findDirectExpression(Expressions.MethodParamName)?.getFirstToken();
        if (name) {
          ret.push({
            name: name.getStr().replace("!", ""),
            identifier: new Identifier(name, this.filename),
            direction: MethodParameterDirection.Exporting,
          });
        }
      }
    }
 
    const changing = node.findFirstExpression(Expressions.MethodDefChanging);
    if (changing) {
      for (const param of changing.findAllExpressions(Expressions.MethodParam)) {
        const name = param.findDirectExpression(Expressions.MethodParamName)?.getFirstToken();
        if (name) {
          ret.push({
            name: name.getStr().replace("!", ""),
            identifier: new Identifier(name, this.filename),
            direction: MethodParameterDirection.Changing,
          });
        }
      }
    }
 
    const returning = node.findFirstExpression(Expressions.MethodDefReturning);
    if (returning) {
      const name = returning.findDirectExpression(Expressions.MethodParamName)?.getFirstToken();
      if (name) {
        ret.push({
          name: name.getStr().replace("!", ""),
          identifier: new Identifier(name, this.filename),
          direction: MethodParameterDirection.Returning,
        });
      }
    }
 
    return ret;
  }
 
}