All files / transform/passes ResolveFunctionDeclaration.js

80% Statements 12/15
50% Branches 3/6
100% Functions 2/2
80% Lines 12/15
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                          19x       3x 3x     3x   3x 1x       1x               1x             3x   3x         3x   3x      
import Transformation from '../transformation.js';
import TokenType from '../../parser/vsltokentype';
import t from '../../parser/nodes';
 
import mangleTypeChildren from '../helper/mangleTypeChildren';
import generateFunctionMangle from '../helper/generateFunctionMangle';
 
/**
 * This resolves and mangles a function declaration. This applies to functions
 * and does not support lambdas or such.
 */
export default class ResolveFunctionDeclaration extends Transformation {
    constructor() {
        super(t.FunctionStatement, "Resolve::FunctionDeclaration");
    }
    
    modify(node: Node, tool: ASTTool) {
        let rootName = node.name.identifier.rootId;
        let args = node.args || [];
        
        // Generate 2D mangled arg names  
        let resArgs = new Array(args.length);
        
        for (var i = 0; i < args.length; i++) {
            Iif (!args[i].typedId.type) {
                throw new TypeError(`Function ${rootName} has no type for pos ${i}.`);
            }
 
            Iif (args[i].typedId.type instanceof t.Identifier) {
                resArgs[i] = [
                    args[i].typedId.identifier.identifier.rootId,
                    args[i].typedId.type.identifier.rootId
                ];
                continue;
            }
 
            resArgs[i] = [
                args[i].typedId.identifier.identifier.rootId,
                mangleTypeChildren(args[i].typedId.type.path, tool)
            ];
        }
        
        // Now that everything is simplified, we can 
        var oldQueueQualifier = node.name;
        
        node.name = new t.Identifier(
            generateFunctionMangle(rootName, resArgs, tool),
            node.name.position
        );
 
        tool.gc(oldQueueQualifier);
        
        tool.notifyScopeChange();
    }
}