All files / parser/nodes assignmentStatement.js

0% Statements 0/8
0% Branches 0/9
0% Functions 0/5
0% Lines 0/8
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                                                                                                           
import Node from './node';
 
/**
 * Matches any assignment type.
 * 
 * This matches any generic binary expression.
 */
export default class AssignmentStatement extends Node {
    
    /**
     * Creates a wrapper ExperssionStatement
     * 
     * @param {AssignmentType} type The assignment type
     * @param {TypedIdentifier} identifier The variable's identifier & type
     * @param {Expression} value The variable's inital value
     * @param {Object} position a position from nearley
     */
    constructor (type: AssignmentType, identifier: TypedIdentifier, value: Expression, position: Object) {
        super(position);
        
        /** @type {AssignmentType} */
        this.type = type;
        /** @type {TypedIdentifier} */
        this.identifier = identifier;
        /** @type {Expression} */
        this.value = value;
    }
    
    /** @override */
    get identifierPath() {
        return this.identifier;
    }
    
    /** @override */
    get children() {
        return ['identifier', 'value'];
    }
    
    /**
     * Gets the type of the assignment
     */
    get exprType() {
        return this.identifier.type || this.value.exprType || null;
    }
    
    /** @override */
    toString() {
        let t;
        return (this.type === 0 ? "var" : "let") +
            ` ${this.identifier.identifier}` +
            ` ${(t = this.exprType) ? `: ${t} ` : ""}` +
            (this.value ? "= " + this.value : "");
    }
}