All files / parser/nodes propertyExpression.js

83.33% Statements 5/6
50% Branches 1/2
66.67% Functions 2/3
83.33% Lines 5/6
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                                                    31x     31x     31x     31x                   9x    
import Node from './node';
 
/**
 * Matches a property expression.
 * 
 * A property expression is anything in the form `head tail`.
 * 
 * A tail can have various markers including:
 *  - accessors through `.`
 *  - subscript `[ ... ]`
 *  - function calls `( ... )`
 *  - etc
 * 
 * Make sure you specify tail still if it doesn't exist
 */
export default class PropertyExpression extends Node {
    
    /**
     * Matches a member-expression e.g. `(E).b`
     * 
     * @param {Expression} head - the primary expression
     * @param {Identifier|Subscript|FunctionCall} tail - The right part of the part of the node
     * @param {boolean} optional - Whether the RHS is optional.
     * @param {Object} position a position from nearley
     */
    constructor(head: any, tail: any, optional: boolean, position: Object) {
        super(position);
        
        /** @type {Expression} */
        this.head = head;
        
        /** @type {Identifier|Subscript|FunctionCall} */
        this.tail = tail;
        
        /** @type {boolean} */
        this.optional = optional;
    }
    
    /** @override */
    get children() {
        return ['head', 'tail'];
    }
    
    /** @override */
    toString() {
        return `(${this.head}).${this.tail}${this.optional ? '?' : ''}`;
    }
}