From 0f6f89d32c984eb5d2240a24fe37e4dbee3584e1 Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Thu, 23 Nov 2017 11:53:30 -0800 Subject: [PATCH] Walk dep tree visit exports fix for `export {x, y as z} from 'module-identifier';` export format --- .../jscomp/FindModuleDependencies.java | 69 ++++++++++++------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/src/com/google/javascript/jscomp/FindModuleDependencies.java b/src/com/google/javascript/jscomp/FindModuleDependencies.java index cb66cbad9df..53705f3ab69 100644 --- a/src/com/google/javascript/jscomp/FindModuleDependencies.java +++ b/src/com/google/javascript/jscomp/FindModuleDependencies.java @@ -21,6 +21,7 @@ import com.google.javascript.jscomp.CompilerInput.ModuleType; import com.google.javascript.jscomp.Es6RewriteModules.FindGoogProvideOrGoogModule; import com.google.javascript.jscomp.deps.ModuleLoader; +import com.google.javascript.rhino.IR; import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Token; @@ -97,32 +98,16 @@ public void visit(NodeTraversal t, Node n, Node parent) { if (supportsEs6Modules && n.isExport()) { moduleType = ModuleType.ES6; + if (n.getBooleanProp(Node.EXPORT_DEFAULT)) { + // export default + } else if (n.hasTwoChildren()) { + // export * from 'moduleIdentifier'; + // export {x, y as z} from 'moduleIdentifier'; + addEs6ModuleImportToGraph(t, n); + } } else if (supportsEs6Modules && n.isImport()) { moduleType = ModuleType.ES6; - String moduleName; - String importName = n.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'. - // These are rewritten to plain namespace object accesses. - moduleName = importName.substring("goog:".length()); - } else { - ModuleLoader.ModulePath modulePath = - t.getInput() - .getPath() - .resolveJsModule(importName, n.getSourceFileName(), n.getLineno(), n.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); - } - moduleName = modulePath.toModuleName(); - } - if (moduleName.startsWith("goog.")) { - t.getInput().addOrderedRequire("goog"); - } - t.getInput().addOrderedRequire(moduleName); + addEs6ModuleImportToGraph(t, n); } else if (supportsCommonJsModules) { if (moduleType != ModuleType.GOOG && ProcessCommonJSModules.isCommonJsExport(t, n)) { moduleType = ModuleType.COMMONJS; @@ -167,6 +152,42 @@ public boolean convertToEs6Module(Node root) { return this.convertToEs6Module(root, false); } + /** + * Adds an es6 module from an import node (import or export statement) to the graph. + */ + private void addEs6ModuleImportToGraph(NodeTraversal t, Node n) { + String moduleName = getEs6ModuleNameFromImportNode(t, n); + if (moduleName.startsWith("goog.")) { + t.getInput().addOrderedRequire("goog"); + } + t.getInput().addOrderedRequire(moduleName); + } + + /** + * Get the module name from an import node (import or export statement). + */ + private String getEs6ModuleNameFromImportNode(NodeTraversal t, Node n) { + String importName = n.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'. + // These are rewritten to plain namespace object accesses. + return importName.substring("goog:".length()); + } else { + ModuleLoader.ModulePath modulePath = + t.getInput() + .getPath() + .resolveJsModule(importName, n.getSourceFileName(), n.getLineno(), n.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); + } + return modulePath.toModuleName(); + } + } + private boolean convertToEs6Module(Node root, boolean skipGoogProvideModuleCheck) { if (Es6RewriteModules.isEs6ModuleRoot(root)) { return true;