Search in sources :

Example 1 with GlobalizedModuleName

use of com.google.javascript.jscomp.ModuleRenaming.GlobalizedModuleName in project closure-compiler by google.

the class Es6RewriteModules method visitRequireOrGet.

private void visitRequireOrGet(NodeTraversal t, Node requireCall, Node parent, boolean isRequire) {
    if (!requireCall.hasTwoChildren() || !requireCall.getLastChild().isStringLit()) {
        t.report(requireCall, INVALID_REQUIRE_NAMESPACE);
        return;
    }
    // Module has already been turned into a script at this point.
    if (isRequire && !t.getScope().isGlobal()) {
        t.report(requireCall, INVALID_CLOSURE_CALL_SCOPE_ERROR);
        return;
    }
    String namespace = requireCall.getLastChild().getString();
    boolean isStoredInDeclaration = NodeUtil.isDeclaration(parent.getParent());
    if (isStoredInDeclaration && !parent.getParent().isConst()) {
        compiler.report(JSError.make(parent.getParent(), LHS_OF_GOOG_REQUIRE_MUST_BE_CONST));
    }
    ModuleMetadata m = moduleMetadataMap.getModulesByGoogNamespace().get(namespace);
    boolean isFromFallbackMetadata = m == null;
    if (isFromFallbackMetadata) {
        t.report(requireCall, MISSING_MODULE_OR_PROVIDE, namespace);
        m = getFallbackMetadataForNamespace(namespace);
    }
    if (isStoredInDeclaration) {
        if (isRequire) {
            Node toDetach;
            if (parent.isDestructuringLhs()) {
                checkState(parent.getFirstChild().isObjectPattern());
                toDetach = parent.getParent();
                for (Node child = parent.getFirstFirstChild(); child != null; child = child.getNext()) {
                    checkState(child.isStringKey() && child.getFirstChild().isName(), child);
                    GlobalizedModuleName rep = getGlobalNameAndType(m, namespace, isFromFallbackMetadata).getprop(child.getString());
                    namesToInlineByAlias.put(child.getFirstChild().getString(), rep);
                }
            } else if (parent.isName()) {
                GlobalizedModuleName alias = getGlobalNameAndType(m, namespace, isFromFallbackMetadata);
                namesToInlineByAlias.put(parent.getString(), alias);
                toDetach = parent.getParent();
            } else {
                checkState(parent.isExprResult());
                toDetach = parent;
            }
            toDetach.detach();
        } else {
            GlobalizedModuleName name = getGlobalNameAndType(m, namespace, isFromFallbackMetadata);
            Node replacement = name.toQname(astFactory).srcrefTree(requireCall);
            requireCall.replaceWith(replacement);
        }
    } else {
        checkState(requireCall.getParent().isExprResult());
        requireCall.getParent().detach();
    }
}
Also used : Node(com.google.javascript.rhino.Node) ModuleMetadata(com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata) GlobalizedModuleName(com.google.javascript.jscomp.ModuleRenaming.GlobalizedModuleName)

Example 2 with GlobalizedModuleName

use of com.google.javascript.jscomp.ModuleRenaming.GlobalizedModuleName in project closure-compiler by google.

the class Es6RewriteModules method inlineAliasedTypes.

private void inlineAliasedTypes(NodeTraversal t, Node typeNode) {
    if (typeNode.isStringLit()) {
        String name = typeNode.getString();
        List<String> split = DOT_SPLITTER.limit(2).splitToList(name);
        // We've already removed the alias.
        if (t.getScope().getVar(split.get(0)) == null) {
            GlobalizedModuleName replacement = namesToInlineByAlias.get(split.get(0));
            if (replacement != null) {
                String rest = "";
                if (split.size() == 2) {
                    rest = "." + split.get(1);
                }
                typeNode.setOriginalName(name);
                typeNode.setString(replacement.aliasName().join() + rest);
                t.reportCodeChange();
            }
        }
    }
    for (Node child = typeNode.getFirstChild(); child != null; child = child.getNext()) {
        inlineAliasedTypes(t, child);
    }
}
Also used : Node(com.google.javascript.rhino.Node) GlobalizedModuleName(com.google.javascript.jscomp.ModuleRenaming.GlobalizedModuleName)

Aggregations

GlobalizedModuleName (com.google.javascript.jscomp.ModuleRenaming.GlobalizedModuleName)2 Node (com.google.javascript.rhino.Node)2 ModuleMetadata (com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata)1