All files / vsl/scope hoistTo.js

0% Statements 0/5
0% Branches 0/3
0% Functions 0/1
0% Lines 0/4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19                                     
/**
 * Hoists `CodeBlock`s to a root one, used for things such as module imports.
 * 
 * @param {Scope} root - root scope as source. Items will be hoisted onto here.
 * @param {Scope} hoistee - An array of scopes to hoist onto the root
 * @param {boolean} [priority=true] - If true: if a hoistee has an item which
 *     the root also has, the hoistee should be valid and the root should be
 *     blamed.
 * @return {?ScopeItem} If returned, this is the conflicting scope item
 *     (i.e. error)
 */
export default function hoistTo(root: Scope, hoistee: Scope, priority: boolean = true): ScopeItem {
    for (let [k, value] of hoistee) {
        let res = root.set(value);
        if (res === false) return root.get(value);
    }
    
    return null;
}