Search in sources :

Example 1 with ModuleMetadata

use of com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata 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 ModuleMetadata

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

the class Es6RewriteModules method visitImport.

private void visitImport(NodeTraversal t, Node importDecl, Node parent) {
    checkArgument(parent.isModuleBody(), parent);
    String importName = importDecl.getLastChild().getString();
    boolean isNamespaceImport = importName.startsWith("goog:");
    if (isNamespaceImport) {
        // Allow importing Closure namespace objects (e.g. from goog.provide or goog.module) as
        // import ... from 'goog:my.ns.Object'.
        String namespace = importName.substring("goog:".length());
        ModuleMetadata m = moduleMetadataMap.getModulesByGoogNamespace().get(namespace);
        if (m == null) {
            t.report(importDecl, MISSING_MODULE_OR_PROVIDE, namespace);
        } else {
            checkState(m.isEs6Module() || m.isGoogModule() || m.isGoogProvide());
        }
    } else {
        ModuleLoader.ModulePath modulePath = t.getInput().getPath().resolveJsModule(importName, importDecl.getSourceFileName(), importDecl.getLineno(), importDecl.getCharno());
        if (modulePath == null) {
            // The module loader issues an error
            // Fall back to assuming the module is a file path
            modulePath = t.getInput().getPath().resolveModuleAsPath(importName);
        }
        maybeAddImportedFileReferenceToSymbolTable(importDecl.getLastChild(), modulePath.toString());
    // TODO(johnplaisted): Use ModuleMetadata to ensure the path required is CommonJs or ES6 and
    // if not give a better error.
    }
    for (Node child = importDecl.getFirstChild(); child != null; child = child.getNext()) {
        if (child.isImportSpecs()) {
            for (Node grandChild = child.getFirstChild(); grandChild != null; grandChild = grandChild.getNext()) {
                maybeAddAliasToSymbolTable(grandChild.getFirstChild(), t.getSourceName());
                checkState(grandChild.hasTwoChildren());
            }
        } else if (child.isImportStar()) {
            // import * as ns from "mod"
            maybeAddAliasToSymbolTable(child, t.getSourceName());
        }
    }
    importDecl.detach();
    t.reportCodeChange();
}
Also used : ModuleLoader(com.google.javascript.jscomp.deps.ModuleLoader) Node(com.google.javascript.rhino.Node) ModuleMetadata(com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata)

Example 3 with ModuleMetadata

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

the class JsFileFullParser method recordModuleMetadata.

private static void recordModuleMetadata(FileInfo info, ModuleMetadata module) {
    info.importedModules.addAll(module.es6ImportSpecifiers().elementSet());
    // anything at all.
    if (module.usesClosure()) {
        info.provides.addAll(module.googNamespaces());
        info.requires.addAll(module.stronglyRequiredGoogNamespaces());
        info.typeRequires.addAll(module.weaklyRequiredGoogNamespaces());
        info.testonly = module.isTestOnly();
    }
    // Traverse any nested modules (goog.loadModule calls).
    for (ModuleMetadata nested : module.nestedModules()) {
        recordModuleMetadata(info, nested);
    }
}
Also used : ModuleMetadata(com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata) GatherModuleMetadata(com.google.javascript.jscomp.GatherModuleMetadata)

Example 4 with ModuleMetadata

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

the class PolymerBehaviorExtractor method resolveReferenceToLegacyGoogModule.

/**
 * Handles resolving behaviors if they are references to legacy modules
 *
 * <p>Returns null if the name is not from a legacy module, and resolution should continue
 * normally.
 */
private ResolveBehaviorNameResult resolveReferenceToLegacyGoogModule(String name) {
    int dot = name.length();
    while (dot >= 0) {
        String subNamespace = name.substring(0, dot);
        ModuleMetadata metadata = moduleMetadataMap.getModulesByGoogNamespace().get(subNamespace);
        if (metadata == null || !metadata.isLegacyGoogModule()) {
            dot = name.lastIndexOf('.', dot - 1);
            continue;
        }
        String rest = dot == name.length() ? "" : name.substring(dot);
        ResolveBehaviorNameResult result = resolveBehaviorName(GOOG_MODULE_EXPORTS + rest, metadata);
        // rewriting unconditionally after the PolymerPass.
        return result.equals(FAILED_RESOLVE_RESULT) ? null : result;
    }
    return null;
}
Also used : ModuleMetadata(com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata)

Example 5 with ModuleMetadata

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

the class GatherModuleMetadataTest method testDynamicImport.

@Test
public void testDynamicImport() {
    test(srcs(SourceFile.fromCode("imported.js", "export default function() {};"), SourceFile.fromCode("script.js", "import('./imported.js')")));
    ModuleMetadata m = metadataMap().getModulesByPath().get("script.js");
    assertThat(m.isNonProvideScript()).isTrue();
    assertThat(m.es6ImportSpecifiers()).containsExactly("./imported.js");
}
Also used : ModuleMetadata(com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata) Test(org.junit.Test)

Aggregations

ModuleMetadata (com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata)61 Test (org.junit.Test)47 Name (com.google.javascript.jscomp.GlobalNamespace.Name)12 Node (com.google.javascript.rhino.Node)5 GatherModuleMetadata (com.google.javascript.jscomp.GatherModuleMetadata)2 Module (com.google.javascript.jscomp.modules.Module)2 BasicErrorManager (com.google.javascript.jscomp.BasicErrorManager)1 CheckLevel (com.google.javascript.jscomp.CheckLevel)1 Compiler (com.google.javascript.jscomp.Compiler)1 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)1 JSError (com.google.javascript.jscomp.JSError)1 GlobalizedModuleName (com.google.javascript.jscomp.ModuleRenaming.GlobalizedModuleName)1 SourceFile (com.google.javascript.jscomp.SourceFile)1 ModuleLoader (com.google.javascript.jscomp.deps.ModuleLoader)1 Binding (com.google.javascript.jscomp.modules.Binding)1 ModuleMetadataMap (com.google.javascript.jscomp.modules.ModuleMetadataMap)1 Config (com.google.javascript.jscomp.parsing.Config)1 ParserRunner (com.google.javascript.jscomp.parsing.ParserRunner)1 Comment (com.google.javascript.jscomp.parsing.parser.trees.Comment)1 InputId (com.google.javascript.rhino.InputId)1