Search in sources :

Example 6 with ModulePath

use of com.google.javascript.jscomp.deps.ModuleLoader.ModulePath in project closure-compiler by google.

the class ProcessCommonJSModules method removeIIFEWrapper.

/**
 * UMD modules are often wrapped in an IIFE for cases where they are used as scripts instead of
 * modules. Remove the wrapper.
 * @return Whether an IIFE wrapper was found and removed.
 */
private boolean removeIIFEWrapper(Node root) {
    checkState(root.isScript());
    Node n = root.getFirstChild();
    // Skip any empty statements from those
    while (n != null && n.isEmpty()) {
        n = n.getNext();
    }
    // and it must be an expression statement.
    if (n == null || !n.isExprResult() || n.getNext() != null) {
        return false;
    }
    // ! ~ void can be repeated any number of times
    if (n != null && n.hasChildren() && n.getFirstChild().isNot()) {
        n = n.getFirstChild();
    }
    Node call = n.getFirstChild();
    if (call == null || !call.isCall()) {
        return false;
    }
    // Find the IIFE call and function nodes
    Node fnc;
    if (call.getFirstChild().isFunction()) {
        fnc = n.getFirstFirstChild();
    } else if (call.getFirstChild().isGetProp() && call.getFirstFirstChild().isFunction() && call.getFirstChild().getString().equals("call")) {
        fnc = call.getFirstFirstChild();
        // We only support explicitly binding "this" to the parent "this" or "exports"
        if (!(call.getSecondChild() != null && (call.getSecondChild().isThis() || call.getSecondChild().matchesQualifiedName(EXPORTS)))) {
            return false;
        }
    } else {
        return false;
    }
    if (NodeUtil.doesFunctionReferenceOwnArgumentsObject(fnc)) {
        return false;
    }
    CompilerInput ci = compiler.getInput(root.getInputId());
    ModulePath modulePath = ci.getPath();
    if (modulePath == null) {
        return false;
    }
    String iifeLabel = getModuleName(modulePath) + "_iifeWrapper";
    FunctionToBlockMutator mutator = new FunctionToBlockMutator(compiler, compiler.getUniqueNameIdSupplier());
    Node block = mutator.mutateWithoutRenaming(iifeLabel, fnc, call, null, false, false);
    root.removeChildren();
    root.addChildrenToFront(block.removeChildren());
    reportNestedScopesDeleted(fnc);
    compiler.reportChangeToEnclosingScope(root);
    return true;
}
Also used : ModulePath(com.google.javascript.jscomp.deps.ModuleLoader.ModulePath) Node(com.google.javascript.rhino.Node)

Aggregations

ModulePath (com.google.javascript.jscomp.deps.ModuleLoader.ModulePath)6 Node (com.google.javascript.rhino.Node)6 ModuleMap (com.google.javascript.jscomp.modules.ModuleMap)3 Module (com.google.javascript.jscomp.modules.Module)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Binding (com.google.javascript.jscomp.modules.Binding)1 ModuleMetadataMap (com.google.javascript.jscomp.modules.ModuleMetadataMap)1 JSType (com.google.javascript.rhino.jstype.JSType)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1