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

97.37% Statements 148/152
80% Branches 56/70
100% Functions 16/16
97.37% Lines 148/152

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 3651x 1x 1x 1x   1x 1x 1x   1x               5036x 5036x 5036x 5036x 5036x 5036x       386x       916x       40x 40x 47x 39x     1x       2312x       571x 571x 547x 546x     25x       109x 109x 113x 97x     12x       144x           5036x 498x     4538x 4538x   4538x 571x 571x 309x 309x 309x       571x 571x           4538x   196x 196x 196x 196x               4538x 172x 172x     172x 172x 172x 172x   172x   172x                           4538x 650x   650x 650x 650x   650x 650x 650x   650x 650x 650x   650x 650x 650x   650x                                       822x 822x 81x 81x 81x 1x 1x       81x 81x 81x           81x 81x 81x 81x 81x               822x       2122x 1454x     668x 668x 15x 15x   15x               668x       2122x 1454x     668x 668x 62x     606x 606x 114x 114x               606x 16x 16x               606x 42x 42x                 606x       2122x 1454x     668x 668x 394x 394x       394x   394x                       668x         394x   394x 394x 65x 73x 73x 73x                 394x 394x 12x 13x 13x 13x                 394x 394x 1x 1x 1x 1x                 394x 394x 75x 75x 75x               394x      
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} 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();
        Eif (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);
      Iif (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 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,
        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 superClassName = found.findFirstExpression(Expressions.SuperClassName)?.getFirstToken().getStr();
      const containsGlobal = found.findFirstExpression(Expressions.ClassGlobal);
      const concat = found.findFirstStatement(Statements.ClassDefinition)!.concatTokens().toUpperCase();
 
      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"),
        isAbstract: concat.includes(" ABSTRACT"),
        isFinal: found.findFirstExpression(Expressions.ClassFinal) !== undefined,
        aliases,
        attributes,
      });
    }
  }
 
///////////////////
 
  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[] = [];
      Iif (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 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();
      Iif (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();
        Eif (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();
        Eif (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();
        Eif (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();
      Eif (name) {
        ret.push({
          name: name.getStr().replace("!", ""),
          identifier: new Identifier(name, this.filename),
          direction: MethodParameterDirection.Returning,
        });
      }
    }
 
    return ret;
  }
 
}