All files / src/rules _type_lookup.ts

95.2% Statements 119/125
85.48% Branches 53/62
100% Functions 9/9
95.2% Lines 119/125

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 1261x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 27x 5x 5x 27x 1x 1x 1x 1x 532x 532x 532x 2342x 2342x 2342x 524x 524x 2342x 8x 8x 1x 1x 71x 71x 71x 1x 1x 1x 79449x 79449x 79449x 79449x 1x 1x 118x 118x 12x 12x 118x 118x 1x 1x 1x 1x 355x 355x 355x 355x 415x 415x 415x 415x 415x 415x 338x 415x 17x       17x 77x 1x 60x   59x 10x 59x 26x 26x 415x 7x 7x 415x 322x 322x 1x 1x 173x 173x 173x 173x 173x 1x 1x 163x 163x     163x 163x 1x 1x 4377x 4377x 4377x 2084x 2084x 2084x 84x 84x 2084x 4377x 3598x 3598x 4377x 4377x  
import * as Expressions from "../abap/2_statements/expressions";
import {ExpressionNode, TokenNode} from "../abap/nodes";
import {Dash, InstanceArrow, StaticArrow} from "../abap/1_lexer/tokens";
import {AbstractToken} from "../abap/1_lexer/tokens/abstract_token";
import {ISpaghettiScope, ISpaghettiScopeNode} from "../abap/5_syntax/_spaghetti_scope";
import {ReferenceType} from "../abap/5_syntax/_reference";
import {TypedIdentifier} from "../abap/types/_typed_identifier";
import {Identifier} from "../abap/4_file_information/_identifier";
import {AbstractType} from "../abap/types/basic/_abstract_type";
import {DataReference, StructureType, TableType} from "../abap/types/basic";
import {ABAPFile} from "../abap/abap_file";
import {Position} from "../position";
import {VirtualPosition} from "../virtual_position";
import {MethodDefinition} from "../abap/types";
 
/** What the reference at the position resolves to, optionally only references of the given types */
export function resolvedAt(scope: ISpaghettiScopeNode | undefined, start: Position,
                           types?: ReferenceType[]): Identifier | undefined {
  for (const r of scope?.getData().references ?? []) {
    if (r.position.getStart().equals(start) && (types === undefined || types.includes(r.referenceType))) {
      return r.resolved;
    }
  }
  return undefined;
}
 
/** The data object a reference at the position resolves to, optionally only references of the given types */
export function typedIdentifierAt(scope: ISpaghettiScopeNode | undefined, start: Position,
                                  types?: ReferenceType[]): TypedIdentifier | undefined {
  for (const r of scope?.getData().references ?? []) {
    if (r.position.getStart().equals(start)
        && (types === undefined || types.includes(r.referenceType))
        && r.resolved instanceof TypedIdentifier) {
      return r.resolved;
    }
  }
  return undefined;
}
 
/** The data object referenced at the position, looked up in the scope of the position */
export function findTypedIdentifier(start: Position, spaghetti: ISpaghettiScope, file: ABAPFile): TypedIdentifier | undefined {
  return typedIdentifierAt(spaghetti.lookupPosition(start, file.getFilename()), start);
}
 
/** The token at the index follows "-", "->" or "=>", so it is a component, attribute or method name, not a variable.
 * The minus of a calculation is a different token type. "~" is part of the identifier token */
export function isComponentName(tokens: readonly AbstractToken[], index: number): boolean {
  const previous = tokens[index - 1];
  return previous instanceof Dash || previous instanceof InstanceArrow || previous instanceof StaticArrow;
}
 
/** The FieldChain of a Source that is only a field chain, or a Target as it is, else undefined */
export function operandChain(node: ExpressionNode): ExpressionNode | undefined {
  if (node.get() instanceof Expressions.Target) {
    return node;
  }
  return node.getChildren().length === 1 ? node.findDirectExpression(Expressions.FieldChain) : undefined;
}
 
/** The type of a chain of field, field symbol, attribute and component names, also through the header line of a
 * table, and with throughRows through dereferences and table expressions. Undefined for offsets, lengths, calls,
 * inline declarations and anything else, and when a name is not resolved */
export function chainType(children: readonly (ExpressionNode | TokenNode)[],
                          scope: ISpaghettiScopeNode | undefined, throughRows = false): AbstractType | undefined {
  let type: AbstractType | undefined = undefined;
  for (const child of children) {
    const get = child.get();
    if (get instanceof Expressions.SourceField
        || get instanceof Expressions.SourceFieldSymbol
        || get instanceof Expressions.TargetField
        || get instanceof Expressions.TargetFieldSymbol
        || get instanceof Expressions.AttributeName) {
      type = typedIdentifierAt(scope, child.getFirstToken().getStart())?.getType();
    } else if (get instanceof Expressions.ComponentName) {
      if (type instanceof TableType) {
        // the header line
        type = type.getRowType();
      }
      type = type instanceof StructureType ? type.getComponentByName(child.concatTokens()) : undefined;
    } else if (throughRows === true && get instanceof Expressions.Dereference) {
      type = type instanceof DataReference ? type.getType() : undefined;
    } else if (throughRows === true && get instanceof Expressions.TableExpression) {
      type = type instanceof TableType ? type.getRowType() : undefined;
    } else if (get instanceof Expressions.ClassName || get instanceof StaticArrow) {
      continue;
    } else if (!(get instanceof Dash) && !(get instanceof InstanceArrow)) {
      return undefined;
    }
    if (type === undefined) {
      return undefined;
    }
  }
  return type;
}
 
/** chainType( ) of a node, in the scope of its first token */
export function nodeChainType(node: ExpressionNode, spaghetti: ISpaghettiScope, file: ABAPFile,
                              throughRows = false): AbstractType | undefined {
  const scope = spaghetti.lookupPosition(node.getFirstToken().getStart(), file.getFilename());
  return chainType(node.getChildren(), scope, throughRows);
}
 
/** A key for a position in a file, a position inside a macro call also includes the position in the macro */
export function positionKey(p: Position): string {
  if (p instanceof VirtualPosition) {
    return p.getRow() + ":" + p.getCol() + ":" + p.vrow + ":" + p.vcol;
  }
  return p.getRow() + ":" + p.getCol();
}
 
/** The methods referenced in the file, by positionKey( ) of the method name, from the scope and all below it */
export function methodReferences(scope: ISpaghettiScopeNode, filename: string,
                                 found = new Map<string, MethodDefinition>()): Map<string, MethodDefinition> {
  for (const r of scope.getData().references) {
    if (r.referenceType === ReferenceType.MethodReference
        && r.position.getFilename() === filename
        && r.resolved instanceof MethodDefinition) {
      found.set(positionKey(r.position.getStart()), r.resolved);
    }
  }
  for (const child of scope.getChildren()) {
    methodReferences(child, filename, found);
  }
  return found;
}