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): imports are read from code, not comments or strings [P3]

Corey Olsond081874d3f14
diff --git a/test/package.test.js b/test/package.test.js
index 4ac8546e..2d8bfcba 100644
--- a/test/package.test.js
+++ b/test/package.test.js
@@ -10,19 +10,113 @@ import { test } from 'node:test';
const ROOT = resolve(import.meta.dirname, '..');
const PACKAGE = JSON.parse(readFileSync(join(ROOT, 'package.json'), 'utf8'));
+/** Words after which a slash starts a regular expression rather than a division. */
+const BEFORE_REGEX = new Set(['return', 'typeof', 'instanceof', 'in', 'of', 'new', 'delete', 'void', 'throw', 'case', 'do', 'else', 'yield', 'await']);
+
+/**
+ * The source with every comment, and the inside of every string, template and regular expression,
+ * blanked character for character: line breaks and quotes stay, and a string's inside becomes x's.
+ * So an import found in the masked text is code, and sits where the source has it.
+ *
+ * @param {string} source
+ * @returns {string}
+ */
+function maskLiterals(source) {
+ const out = source.split('');
+ const fill = (from, to, char) => {
+ for (let index = from; index < to; index += 1) if (out[index] !== '\n') out[index] = char;
+ };
+ const templates = [];
+ let depth = 0;
+ let previous = '';
+ let at = source.startsWith('#!') ? source.indexOf('\n') : 0;
+ if (at === -1) at = source.length;
+ fill(0, at, ' ');
+ const readTemplate = () => {
+ const start = at;
+ while (at < source.length) {
+ if (source[at] === '\\') at += 2;
+ else if (source[at] === '`') break;
+ else if (source[at] === '$' && source[at + 1] === '{') break;
+ else at += 1;
+ }
+ fill(start, at, ' ');
+ if (source[at] === '$') {
+ templates.push(depth);
+ depth += 1;
+ previous = '{';
+ at += 2;
+ } else {
+ previous = 'x';
+ at += 1;
+ }
+ };
+ while (at < source.length) {
+ const char = source[at];
+ const next = source[at + 1];
+ if (char === '/' && (next === '/' || next === '*')) {
+ const end = next === '/' ? source.indexOf('\n', at) : source.indexOf('*/', at + 2) + 2;
+ const stop = end < at ? source.length : end;
+ fill(at, stop, ' ');
+ at = stop;
+ } else if (char === "'" || char === '"') {
+ let end = at + 1;
+ while (end < source.length && source[end] !== char && source[end] !== '\n') end += source[end] === '\\' ? 2 : 1;
+ fill(at + 1, end, 'x');
+ at = end + 1;
+ previous = 'x';
+ } else if (char === '`') {
+ at += 1;
+ readTemplate();
+ } else if (char === '/' && (previous === '' || /[(,=:[!&|?{};+\-*%<>~^]/.test(previous) || BEFORE_REGEX.has(previous))) {
+ let end = at + 1;
+ let inClass = false;
+ while (end < source.length && source[end] !== '\n' && (inClass || source[end] !== '/')) {
+ if (source[end] === '[') inClass = true;
+ if (source[end] === ']') inClass = false;
+ end += source[end] === '\\' ? 2 : 1;
+ }
+ fill(at + 1, end, ' ');
+ at = end + 1;
+ previous = 'x';
+ } else if (char === '}' && templates.at(-1) === depth - 1) {
+ templates.pop();
+ depth -= 1;
+ at += 1;
+ readTemplate();
+ } else if (/[\w$]/.test(char)) {
+ const word = /^[\w$]+/.exec(source.slice(at, at + 64))[0];
+ previous = word;
+ at += word.length;
+ } else {
+ if (char === '{') depth += 1;
+ if (char === '}') depth -= 1;
+ if (!/\s/.test(char)) previous = char;
+ at += 1;
+ }
+ }
+ return out.join('');
+}
+
/**
- * Every module a source file imports: import and export-from statements, braces across lines
- * included, side-effect imports, and dynamic import(), with either quote. A string or comment that
- * merely contains the word from is not an import.
+ * Every module a source file imports: import and export-from statements, braces and line breaks
+ * included, side-effect imports, and dynamic import(), with either quote. Text inside a comment, a
+ * string, a template or a regular expression is not code, so it is never read as an import.
*
* @param {string} source
* @returns {string[]}
*/
function importsIn(source) {
- const statements = /^[ \t]*(?:import|export)\b[^'"`;]*?\bfrom[ \t]*(['"])([^'"]+)\1/gm;
- const sideEffects = /^[ \t]*import[ \t]*(['"])([^'"]+)\1/gm;
- const dynamic = /\bimport\(\s*(['"])([^'"]+)\1\s*\)/g;
- return [statements, sideEffects, dynamic].flatMap((pattern) => [...source.matchAll(pattern)].map((match) => match[2]));
+ const masked = maskLiterals(source);
+ const patterns = [
+ /^[ \t]*(?:import|export)\b[^;'"`]*?\bfrom\s*(['"])([^'"]*)\1/dgm,
+ /^[ \t]*import\s*(['"])([^'"]*)\1/dgm,
+ /\bimport\s*\(\s*(['"])([^'"]*)\1\s*\)/dg,
+ ];
+ return patterns
+ .flatMap((pattern) => [...masked.matchAll(pattern)])
+ .sort((a, b) => a.index - b.index)
+ .map((match) => source.slice(...match.indices[2]));
}
test('no dependencies of any kind, and Node 22.13 or newer [P3]', () => {
@@ -42,8 +136,16 @@ test('the import scan finds a package however it is imported, and nothing that i
'import "left-pad";': ['left-pad'],
"const pad = await import('left-pad');": ['left-pad'],
'const pad = await import( "left-pad" );': ['left-pad'],
+ 'import pad\n from "left-pad";': ['left-pad'],
+ "const pad = await import ('left-pad');": ['left-pad'],
+ "const half = total / 2; const pad = await import('left-pad');": ['left-pad'],
+ "const page = `${'a'} b`; const pad = await import('left-pad');": ['left-pad'],
"const rule = 'the move starts from'; const next = 'x';": [],
"// this rule comes from 'left-pad'": [],
+ "/*\nimport fake from 'left-pad';\n*/": [],
+ "const page = `\nimport fake from 'left-pad';\n`;": [],
+ "const code = \"await import('left-pad')\";": [],
+ "const quote = /'/g; const text = \"import fake from 'left-pad'\";": [],
"export const FROM = 'from';": [],
};
for (const [source, expected] of Object.entries(forms)) assert.deepEqual(importsIn(source), expected, source);