Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

test(package): import() is decoded only after V8 calls it code [P3]

Corey Olson0037673bf3d4
diff --git a/test/package.test.js b/test/package.test.js
index 59c1e572..79adeb32 100644
--- a/test/package.test.js
+++ b/test/package.test.js
@@ -46,8 +46,8 @@ function look(sources) {
return JSON.parse(child.stdout);
}
-/** Every place the word import appears, in code or not. */
-const IMPORT = /(?<![\w$.#])import(?![\w$])/g;
+/** Every place the whole word import appears, in code or not; Unicode letters count as part of a word. */
+const IMPORT = /(?<![\p{ID_Continue}$\u200c\u200d#])import(?![\p{ID_Continue}$\u200c\u200d])/gu;
/** Whitespace and comments, which may sit between import, its parenthesis and its argument. A line comment ends at any line terminator. */
const GAP = /^(?:\s|\/\/[^\n\r\u2028\u2029]*|\/\*[\s\S]*?\*\/)*/;
@@ -56,44 +56,67 @@ const GAP = /^(?:\s|\/\/[^\n\r\u2028\u2029]*|\/\*[\s\S]*?\*\/)*/;
const COMPUTED = '(computed)';
/**
- * What an import(...) call at `at` loads: its argument when that is one plain string,
- * decoded, or COMPUTED for anything else. Null when the word is not followed by a parenthesis: a
- * declaration, which V8 lists, or import.meta.
+ * The text from `at` on, past whitespace and comments.
*
* @param {string} source
* @param {number} at
- * @returns {string | null}
+ * @returns {string}
+ */
+function pastGap(source, at) {
+ const rest = source.slice(at);
+ return rest.slice(GAP.exec(rest)[0].length);
+}
+
+/**
+ * What an import(...) call at `at` loads: its argument when that is one plain string, decoded, or
+ * COMPUTED for anything else, a string whose escapes do not decode included. Only called on words
+ * V8 has confirmed are import calls in code.
+ *
+ * @param {string} source
+ * @param {number} at
+ * @returns {string}
*/
function dynamicAt(source, at) {
- const skip = (text) => text.slice(GAP.exec(text)[0].length);
- const call = skip(source.slice(at + 'import'.length));
- if (call[0] !== '(') return null;
- const argument = skip(call.slice(1));
+ const argument = pastGap(pastGap(source, at + 'import'.length), 1);
const literal = /^(['"])(?:\\[\s\S]|(?!\1)[^\\\n\r])*\1/.exec(argument);
if (!literal) return COMPUTED;
- const after = skip(argument.slice(literal[0].length))[0];
- return after === ')' || after === ',' ? new Function(`return ${literal[0]};`)() : COMPUTED;
+ const after = pastGap(argument, literal[0].length)[0];
+ if (after !== ')' && after !== ',') return COMPUTED;
+ try {
+ return new Function(`return ${literal[0]};`)();
+ } catch {
+ return COMPUTED;
+ }
}
/**
* Every module each source imports. V8 lists the static ones: import and export-from statements and
- * side-effect imports. For import() calls V8 decides what is code: a NUL put before the word keeps
- * the module compiling when the word sits in a string, template, comment or regular expression,
- * and breaks it when the word is code. A call's argument must be one plain string, or the scan
- * reports it as COMPUTED, which no rule allows.
+ * side-effect imports. For each import followed by a parenthesis, V8 also decides two things: a NUL
+ * put before the word breaks the module only when the word is code, not text in a string, template,
+ * comment or regular expression; and putting 1 in its place still compiles only when the word is a
+ * call, not a property name after a dot. Only then is the argument read, and an argument that is not
+ * one plain string is reported as COMPUTED, which no rule allows. A method named import, in a class
+ * or object, still reads as a call: the scan errs toward a false alarm, never a miss.
*
* @param {string[]} sources - Each one a module that compiles.
* @returns {string[][]}
*/
function importsIn(sources) {
- const calls = sources.map((source) => [...source.matchAll(IMPORT)].map((match) => match.index).filter((at) => dynamicAt(source, at) !== null));
- const marked = sources.flatMap((source, index) => calls[index].map((at) => `${source.slice(0, at)}\u0000${source.slice(at)}`));
- const looks = look([...sources, ...marked]);
+ const calls = sources.map((source) => [...source.matchAll(IMPORT)].map((match) => match.index).filter((at) => pastGap(source, at + 'import'.length)[0] === '('));
+ const variants = sources.flatMap((source, index) =>
+ calls[index].flatMap((at) => [`${source.slice(0, at)}\u0000${source.slice(at)}`, `${source.slice(0, at)}1${source.slice(at + 'import'.length)}`]),
+ );
+ const looks = look([...sources, ...variants]);
let next = sources.length;
return sources.map((source, index) => {
assert.ok(looks[index].compiles, `does not compile as a module: ${source.slice(0, 80)}`);
- const dynamic = calls[index].filter(() => !looks[next++].compiles).map((at) => dynamicAt(source, at));
- return [...looks[index].imports, ...dynamic];
+ const dynamic = calls[index].filter(() => {
+ const isCode = !looks[next].compiles;
+ const isCall = looks[next + 1].compiles;
+ next += 2;
+ return isCode && isCall;
+ });
+ return [...looks[index].imports, ...dynamic.map((at) => dynamicAt(source, at))];
});
}
@@ -149,6 +172,12 @@ test('the import scan finds a package however it is imported, and nothing that i
'const pad = await import("left" + "-pad");': ['(computed)'],
'const pad = await import("left\\u002dpad");': ['left-pad'],
"const pad = await import(/* why */ 'left-pad' /* still */, { with: {} });": ['left-pad'],
+ '// from import("\\u{110000}")': [],
+ '// from import("\\xZZ")': [],
+ 'const caféimport = (x) => x; caféimport("left-pad");': [],
+ 'const obj = { import: (x) => x }; obj . import("left-pad"); obj?.import("left-pad");': [],
+ '// Load it.\nimport("left-pad").then(() => {});': ['left-pad'],
+ 'const all = [...await import("left-pad")];': ['left-pad'],
'function later() { return import("left-pad"); }': ['left-pad'],
"function later() { return \"import('left-pad')\"; }": [],
};