All files / scope/items scopeTypeItem.js

75% Statements 6/8
42.86% Branches 3/7
33.33% Functions 1/3
75% Lines 6/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 55 56 57 58 59 60 61 62 63                                                                        3x   3x   3x 3x   3x                       1x              
import ScopeItem from '../scopeItem';
 
/**
 * Describes a declaration of a type.
 */
export default class ScopeTypeItem extends ScopeItem {
    /**
     * Creates an intenral declaration of a type. When passing the "subscope",
     * don't create a new one or anything, just pass the `CodeBlock`'s `Scope`
     * that your node has. If for some weird reason you need to create a scope,
     * don't set the `parentScope` of the scope, the `superclass` attribute will
     * do that for you.
     * 
     * @param {string} rootId - The root primary identifier of this type.
     * @param {Scope} subscope - All items in the class's scope
     * @param {Object} data - Information about the class
     * @param {ScopeTypeItem[]} data.castables - Types which this can safely be
     *     cast to. We'll assume that you've done all the checks because if
     *     something is wrong here expect big giant segfaults. If you have a
     *     superclass, specified it'll go both in the superclass field and here.
     * @param {ScopeTypeItem} data.superclass - The superclass (not interface)
     *     of the current class, don't specify if there is none. You don't need
     *     to resolve inheritance or anything.
     * @param {boolean} data.isInterface - Whether the type is an interface.
     *     This is used to determine how casting will occur and dynamic dispatch
     *     so ensure that it is not possible to declare fields.
     */
    constructor(
        rootId: string,
        subscope: Scope = null,
        {
            castables = [],
            superclass = ScopeTypeItem.RootClass,
            isInterface = false
        } = {}
    ) {
        super(rootId);
        
        this.isInterface = isInterface;
        
        this.castsables = castables;
        this.superclass = superclass;
        
        this.subscope = subscope;
    }
    
    /** @override */
    equal(ref: ScopeItem): boolean {
        return ref.rootId === this.rootId; 
    }
    
    /**
     * The default classes all items inherit from.
     */
    static RootClass = do {
        new ScopeTypeItem("Object", null, {})
    }
    
    /** @return {string} */
    toString() {
        return `${this.isInterface ? "interface" : "class"} ${this.rootId}`
    }
}