diff --git a/bend2/main.ts b/bend2/main.ts index 063b9733b..6fd7998bf 100755 --- a/bend2/main.ts +++ b/bend2/main.ts @@ -333,8 +333,15 @@ async function cli_checkup(file: string): Promise { const base = await book_read(BASE); let bad = false; for (const raw of fs.readFileSync(file, "utf8").split("\n")) { + const line = raw.trim(); + if (line === "" || line.startsWith("#")) { + continue; + } + if (!/^import(\s|$)/.test(line)) { + break; + } const m = /^import\s+(\S+)\s+as\s+[A-Za-z_][A-Za-z0-9_]*\s*(?:#.*)?$/ - .exec(raw.trim()); + .exec(line); if (m === null) { continue; } @@ -343,7 +350,8 @@ async function cli_checkup(file: string): Promise { cli_say(1, "--- " + m[1] + " ---\n"); let code = 1; try { - const own = /^import Base\s*(?:#.*)?$/m.test(fs.readFileSync(at, "utf8")); + const own = /^(?:[\t\r ]*(?:(?:#|import )[^\n]*)?\n)*[\t ]*import Base\s/ + .test(fs.readFileSync(at, "utf8")); code = book_run(await book_read(at, own ? base : undefined), [at]); } catch (e) { cli_say(2, book_err(e) + "\n"); diff --git a/gates/ping.ts b/gates/ping.ts index 9575607d5..6d9b62840 100644 --- a/gates/ping.ts +++ b/gates/ping.ts @@ -293,6 +293,26 @@ try { check("guide, base and a program run through the executable", guide.out.startsWith("# Bend") && base.out.startsWith("type Map") && sum5.code === 0 && sum5.out === "5n\n"); + const header_source = path.join(TMP, "literal-checkup.bend"); + fs.writeFileSync(header_source, + "# a header\nimport Base\n\nimport ./sum.bend as Sum\n" + + "def text() -> String:\n \"body text\n" + + "import ./absent.bend as Hidden\nmore text\"\n"); + const header_check = await bend([header_source, "--check-only"], + { BEND_NO_TELEMETRY: "1" }); + const header_run = await bend([header_source, "--checkup"], + { BEND_NO_TELEMETRY: "1" }); + check("valid body literal text does not add an independent import", + header_check.code === 0 && header_run.code === 0 + && header_run.out.endsWith("5n\n")); + const no_base = path.join(TMP, "no-base.bend"); + fs.writeFileSync(no_base, + "def main() -> String:\n \"body\nimport Base\n\"\n"); + fs.writeFileSync(header_source, "import ./no-base.bend as NoBase\n"); + const base_run = await bend([header_source, "--checkup"], + { BEND_NO_TELEMETRY: "1" }); + check("--checkup gives Base only to a module that imports it", + base_run.code === 1); fs.writeFileSync(path.join(TMP, "page.html"), "\n"); fs.writeFileSync(path.join(TMP, "page.ts"), "import \"./none.ts\";\n");