All files / parser/nodes functionStatement.js

88.89% Statements 8/9
66.67% Branches 4/6
75% Functions 3/4
88.89% Lines 8/9
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                                                    57x     57x 57x 57x 57x 57x                   6x         3x    
import Node from './node';
 
/**
 * Wraps a function
 * 
 */
export default class FunctionStatement extends Node {
    
    /**
     * Constructs a generic function statement
     * 
     * @param {string[]} access - The access modifiers of the node
     * @param {Identifier} name - The name of the given function
     * @param {FunctionArgument[]} args - The arguments of the function
     * @param {Type} returnType - The function's returnType.
     * @param {Node[]} statements - The statements in the function body.
     * @param {Object} position - a position from nearley
     */
    constructor(
        access: string[],
        name: Identifier,
        args: FunctionArgument[],
        returnType: Type,
        statements: Node[],
        position: Object
    ) {
        super(position);
        
        /** @type {string} */
        this.access = access;
        this.name = name;
        this.args = args || [];
        this.returnType = returnType;
        this.statements = statements;
    }
    
    /** @override */
    get identifierPath() {
        return this.name;
    }
    
    /** @override */
    get children() {
        return ['name', 'args', 'returnType', 'statements'];
    }
    
    /** @override */
    toString() {
        return `${this.access.join(" ")}${this.access.length ? " " : ""}func ${this.name.identifier.rootId}(${this.args.join(", ")})${this.returnType ? " -> " + this.returnType : ""} ${this.statements}`
    }
}