All files / src/abap/5_syntax/expressions inline_field_definition.ts

92.59% Statements 50/54
60% Branches 6/10
100% Functions 1/1
92.59% Lines 50/54

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 551x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 104x 104x 104x 104x 104x 104x 104x 104x 104x     104x 104x 104x 102x 102x 104x 104x 2x 2x 104x 48x 48x 104x     104x 104x 104x 1x 1x 1x 1x 103x 103x 103x 103x 103x 103x 103x 1x  
import {ExpressionNode, StatementNode} from "../../nodes";
import {TypedIdentifier, IdentifierMeta} from "../../types/_typed_identifier";
import {Source} from "./source";
import * as Expressions from "../../2_statements/expressions";
import {AbstractType} from "../../types/basic/_abstract_type";
import {BasicTypes} from "../basic_types";
import {UnknownType} from "../../types/basic/unknown_type";
import {ReferenceType} from "../_reference";
import {CheckSyntaxKey, SyntaxInput, syntaxIssue} from "../_syntax_input";
import {VoidType} from "../../types/basic";
 
export class InlineFieldDefinition {
  public static runSyntax(
    node: ExpressionNode | StatementNode,
    input: SyntaxInput,
    targetType?: AbstractType): AbstractType | undefined {
 
    let type: AbstractType | undefined = undefined;
 
    const field = (node.findDirectExpression(Expressions.Field)
      || node.findDirectExpression(Expressions.FieldSymbol))?.getFirstToken();
    if (field === undefined) {
      return undefined;
    }
 
    const source = node.findDirectExpression(Expressions.Source);
    if (source) {
      type = Source.runSyntax(source, input);
    }
    const typeName = node.findDirectExpression(Expressions.TypeName);
    if (typeName) {
      type = new BasicTypes(input).parseType(typeName);
    }
    if (targetType !== undefined) {
      type = targetType;
    }
    if (type === undefined) {
      type = new UnknownType("InlineFieldDefinition, fallback");
    }
 
    const name = field.getStr();
    if (input.scope.findVariable(name) !== undefined) {
      const message = `Variable ${name} already defined`;
      input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
      return VoidType.get(CheckSyntaxKey);
    }
 
    const identifier = new TypedIdentifier(field, input.filename, type, [IdentifierMeta.InlineDefinition]);
    input.scope.addReference(field, identifier, ReferenceType.DataWriteReference, input.filename);
    input.scope.addIdentifier(identifier);
 
    return type;
  }
}