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.
This commit is contained in:
Ben Newman
2018-07-31 15:44:30 -04:00
parent 8a4a62a79b
commit 44cdfeb686

View File

@@ -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) {