Search in sources :

Example 6 with Binding

use of com.google.javascript.jscomp.modules.Binding in project closure-compiler by google.

the class Es6RewriteModules method createExportsObject.

private Node createExportsObject(String moduleName, NodeTraversal t, Node script, AstFactory.Type moduleObjectType) {
    Node moduleObject = astFactory.createObjectLit(moduleObjectType);
    // Going to get renamed by RenameGlobalVars, so the name we choose here doesn't matter as long
    // as it doesn't collide with an existing variable. (We can't use `moduleName` since then
    // RenameGlobalVars will rename all references to `moduleName` incorrectly). We'll fix the name
    // in visitScript after the global renaming to ensure it has a name that is deterministic from
    // the path.
    // 
    // So after this method we'll have:
    // var $jscomp$tmp$exports$module$name = {};
    // module$name.exportName = localName;
    // 
    // After RenameGlobalVars:
    // var $jscomp$tmp$exports$module$nameglobalized = {};
    // module$name.exportName = localName$globalized;
    // 
    // After visitScript:
    // var module$name = {};
    // module$name.exportName = localName$globalized;
    Node moduleVar = astFactory.createSingleVarNameDeclaration("$jscomp$tmp$exports$module$name", moduleObject);
    moduleVar.getFirstChild().putBooleanProp(Node.MODULE_EXPORT, true);
    // TODO(b/144593112): Stop adding JSDoc when this pass moves to always be after typechecking.
    JSDocInfo.Builder infoBuilder = JSDocInfo.builder();
    infoBuilder.recordConstancy();
    moduleVar.setJSDocInfo(infoBuilder.build());
    moduleVar.getFirstChild().setDeclaredConstantVar(true);
    script.addChildToBack(moduleVar.srcrefTreeIfMissing(script));
    Module thisModule = moduleMap.getModule(t.getInput().getPath());
    for (Map.Entry<String, Binding> entry : thisModule.namespace().entrySet()) {
        String exportedName = entry.getKey();
        Binding binding = entry.getValue();
        Node nodeForSourceInfo = binding.sourceNode();
        boolean mutated = binding.isMutated();
        QualifiedName boundVariableQualifiedName = ModuleRenaming.getGlobalName(binding);
        checkState(boundVariableQualifiedName.isSimple(), "unexpected qualified name: %s", boundVariableQualifiedName);
        String boundVariableName = boundVariableQualifiedName.getRoot();
        Node getProp = astFactory.createGetPropWithoutColor(astFactory.createName(moduleName, moduleObjectType), exportedName);
        getProp.putBooleanProp(Node.MODULE_EXPORT, true);
        if (typedefs.contains(exportedName)) {
            // /** @typedef {foo} */
            // moduleName.foo;
            JSDocInfo.Builder builder = JSDocInfo.builder().parseDocumentation();
            JSTypeExpression typeExpr = new JSTypeExpression(astFactory.createString(exportedName).srcref(nodeForSourceInfo), script.getSourceFileName());
            builder.recordTypedef(typeExpr);
            JSDocInfo info = builder.build();
            getProp.setJSDocInfo(info);
            Node exprResult = astFactory.exprResult(getProp).srcrefTreeIfMissing(nodeForSourceInfo);
            script.addChildToBack(exprResult);
        } else if (mutated) {
            final Node globalExportName = astFactory.createName(boundVariableName, type(getProp));
            addGetterExport(script, nodeForSourceInfo, moduleObject, exportedName, globalExportName);
            NodeUtil.addFeatureToScript(t.getCurrentScript(), Feature.GETTER, compiler);
        } else {
            // Avoid the extra complexity of using getters when the property isn't mutated.
            // exports.foo = foo;
            Node assign = astFactory.createAssign(getProp, astFactory.createName(boundVariableName, type(getProp)));
            // TODO(b/144593112): Stop adding JSDoc when this pass moves to always be after typechecking
            JSDocInfo.Builder builder = JSDocInfo.builder().parseDocumentation();
            builder.recordConstancy();
            JSDocInfo info = builder.build();
            assign.setJSDocInfo(info);
            script.addChildToBack(astFactory.exprResult(assign).srcrefTreeIfMissing(nodeForSourceInfo));
        }
    }
    return moduleVar;
}
Also used : Binding(com.google.javascript.jscomp.modules.Binding) Node(com.google.javascript.rhino.Node) QualifiedName(com.google.javascript.rhino.QualifiedName) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo) Module(com.google.javascript.jscomp.modules.Module) ModuleMetadataMap(com.google.javascript.jscomp.modules.ModuleMetadataMap) HashMap(java.util.HashMap) Map(java.util.Map) ModuleMap(com.google.javascript.jscomp.modules.ModuleMap)

Aggregations

Binding (com.google.javascript.jscomp.modules.Binding)6 Node (com.google.javascript.rhino.Node)4 Module (com.google.javascript.jscomp.modules.Module)3 ModuleMap (com.google.javascript.jscomp.modules.ModuleMap)3 Map (java.util.Map)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 JSType (com.google.javascript.rhino.jstype.JSType)2 ModulePath (com.google.javascript.jscomp.deps.ModuleLoader.ModulePath)1 ModuleMetadataMap (com.google.javascript.jscomp.modules.ModuleMetadataMap)1 ModuleMetadata (com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata)1 JSDocInfo (com.google.javascript.rhino.JSDocInfo)1 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)1 QualifiedName (com.google.javascript.rhino.QualifiedName)1 HashMap (java.util.HashMap)1