import pathlib
import subprocess

js = pathlib.Path(__file__).with_name("_check.js").read_text(encoding="utf-8")
lines = js.splitlines()
lo, hi = 1, len(lines)

def ok(n):
    chunk = "\n".join(lines[:n]) + "\n})();\n"
    p = pathlib.Path(__file__).with_name("_part.js")
    p.write_text("(function(){\n" + chunk, encoding="utf-8")
    r = subprocess.run(["node", "--check", str(p)], capture_output=True, text=True)
    return r.returncode == 0, r.stderr.strip()[:200]

# find smallest n where adding close fails - actually find first line where syntax breaks when parsing full prefix+suffix
# simpler: find max n such that (function(){ lines[:n] })(); parses
best = 2
for n in range(100, len(lines), 200):
    good, err = ok(n)
    print(n, "ok" if good else err)
    if good:
        best = n

# refine
for n in range(best, len(lines), 50):
    good, _ = ok(n)
    if not good:
        print("breaks between", n - 50, n)
        break
else:
    print("all prefix ok?")
