From 44cdfeb68630dc2fc8bb99650fead73c40748ee4 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Tue, 31 Jul 2018 15:44:30 -0400 Subject: [PATCH] Prepare for module.link(id, setters) replacing module.watch. Thanks to these commits in the Reify project, there's a new runtime module system method: module.link(id, setters), which replaces the previous (more cumbersome) module.watch(require(id), setters). This is more than a cosmetic change, since it will allow creating module Entry objects before evaluating modules, which will help improve spec compliance around import cycles and hoisted function declarations. It's also shorter than the module.watch style, which is always nice. However, since require(id) no longer appears in the generated code, we can't just rely on findImportedModuleIdentifiers looking for require function calls, so the scanner now needs to look for module.link(id, ...) calls as well. --- tools/isobuild/js-analyze.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/tools/isobuild/js-analyze.js b/tools/isobuild/js-analyze.js index 2c35ed06e1..88d13490d4 100644 --- a/tools/isobuild/js-analyze.js +++ b/tools/isobuild/js-analyze.js @@ -6,6 +6,11 @@ import Visitor from "reify/lib/visitor.js"; import { findPossibleIndexes } from "reify/lib/utils.js"; const hasOwn = Object.prototype.hasOwnProperty; +const objToStr = Object.prototype.toString + +function isRegExp(value) { + return value && objToStr.call(value) === "[object RegExp]"; +} var AST_CACHE = new LRU({ max: Math.pow(2, 12), @@ -58,6 +63,7 @@ export function findImportedModuleIdentifiers(source, hash) { "import", "export", "dynamicImport", + "link", ]); if (possibleIndexes.length === 0) { @@ -150,10 +156,12 @@ const importedIdentifierVisitor = new (class extends Visitor { this.addIdentifier(firstArg.value, "import", true); } else if (node.callee.type === "MemberExpression" && - isIdWithName(node.callee.object, "module")) { + // The Reify compiler sometimes renames references to the + // CommonJS module object for hygienic purposes, but it + // always does so by appending additional numbers. + isIdWithName(node.callee.object, /^module\d*$/)) { const propertyName = - isPropertyWithName(node.callee.property, "import") || - isPropertyWithName(node.callee.property, "importSync") || + isPropertyWithName(node.callee.property, "link") || isPropertyWithName(node.callee.property, "dynamicImport"); if (propertyName) { @@ -193,9 +201,20 @@ const importedIdentifierVisitor = new (class extends Visitor { }); function isIdWithName(node, name) { - return node && - node.type === "Identifier" && - node.name === name; + if (! node || + node.type !== "Identifier") { + return false; + } + + if (typeof name === "string") { + return node.name === name; + } + + if (isRegExp(name)) { + return name.test(node.name); + } + + return false; } function isStringLiteral(node) {