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();
}
}
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();
}
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);
}
}
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;
}
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");
}
Aggregations