All files / transform/passes VerifyFunctionAccessScope.js

71.43% Statements 10/14
66.67% Branches 8/12
75% Functions 3/4
75% Lines 9/12
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                              19x           3x 3x   3x   3x   1x     2x   2x     2x                  
import Transformation from '../transformation.js';
import TransformError from '../transformError.js';
import AccessModifiers from '../data/accessModifiers';
import t from '../../parser/nodes';
 
/**
 * Verifies and determines if a functions access modifiers are valid within its
 * scope.
 * 
 * An example is if a function declared `static func` is outside of a function.
 * This does not implement access modifers either but just checks a function's
 * scope
 */
export default class VerifyFunctionAccessScope extends Transformation {
    constructor() {
        super(t.FunctionStatement, "Verify::FunctionAccessScope");
    }
    
    /** @overide */
    modify(node: Node, tool: ASTTool) {
        // statement = node.parentScope?.parentNode
        let statement = node.parentScope
        statement = statement && statement.parentNode;
        
        let accessModifiers = node.access;
        
        if (statement instanceof t.ClassStatement || statement instanceof t.InterfaceStatement) {
            // A class function
            return;
        }
        
        Eif (node.parentScope.rootScope === true) {
            // Top-level function
            Iif (AccessModifiers.Membership.some(i => accessModifiers.find(x => x === i))) {
                throw new TransformError("Non-methods may not be defined as static.", node)
            }
            return;
        }
        
        // Nested function
        if (accessModifiers.length > 0) {
            throw new TransformError("Functions inside scopes may not have any modifiers.", node);
        }
    }
}