Milestone
Description
When rewriting JS code, wabac uses a JS parser (acorn) to parse JS and correctly detect all JS variables.
For now, our Python equivalent in python-scraperlib is not correctly rewriting these globals in all situations due to the fact that we miss a JS parser and code equivalent to wabac.JS
This is wabac code for which we miss an equivalent in Python scraperlib:
I don't know yet if we can include a JS parser (equivalent to acorn) in the scraperlib or do things differently.
Activity
epheterson commentedon Sep 7, 2026
Concrete case, with a workaround we're shipping in Zimi. nerdfonts.com/cheat-sheet defines
const glyphs = {...}in an inline script and reads it from cheat-sheet.js. The block wrapper makesglyphsblock-scoped, so the second script throwsglyphs is not definedwith the whole table present in the ZIM. Workaround: before the block's closing brace, for eachconst/let/classname declared at the block's top level, emittry{self.NAME=NAME;}catch(e){}, which is whatvarwould have done. A ~40-line brace-depth scanner that skips strings, templates and comments finds the names. Happy to open a PR if that shape is acceptable.benoit74 commentedon Sep 7, 2026
@epheterson thank you very much for your interest in this issue and your valuable input (especially the concrete case which will help for sure).
I have two questions:
const,let,classnames declared at the block's top level? Don't you need a JS parser for that?We would definitely prefer to stay close to what wabac.js is doing for maintenance simplicity, but are definitely open to analyze alternatives especially if they are simpler / more maintainable
epheterson commentedon Sep 7, 2026
Thanks — two straight answers.
Detection: no parser. A ~40-line scanner tracks
{}/()/[]depth and skips string, template and comment bodies, soconst/let/classat depth 0 are the top level. Regex literals are the one construct it doesn't model, and a brace inside one can only make it miss a declaration, never invent one. Names wombat shadows (window,self,location, …) are excluded, sinceself.location = locationwould navigate.vs wabac.js: I hadn't read
parseGlobalsclosely enough before writing — it's the better design. It parses with acorn and recreates real bindings:letis stripped and pre-declared outside the block,const/classare stashed onselfand re-declared after it. Mine just assignsself.NAME = NAME, which is whatvarwould have done and is enough for the nerdfonts case, but a trueconstbinding is more faithful (your comment aboutconst Xvsself.Xchecks is right).So the useful PR is probably a port of
parseGlobalsusing a Python JS parser (esprima-python or similar), with the scanner as the fallback when parsing fails — wabac itself falls back with a warning. Happy to do that if you'd take it; otherwise I'll keep the scanner as Zimi's install-time patch until upstream lands.epheterson commentedon Sep 7, 2026
Claude code is taking the lead here 😅 Hit this bug while validating new features in my project and wanted to share the finding upstream instead of maintaining a fork. Not sure on the specifics but we can iterate until it's a well understood, proper fix and happy to make or review the PR when we figure out how to address it.
benoit74 commentedon Sep 7, 2026
The "Python JS parser" endeavor was indeed what I had in mind when opening the issue. I feel like it is the correct way to tackle it and maintain good parity with wabac.js codebase. I'm glad Claude is aligned with my conclusions 🤣
I would definitely appreciate a PR on this issue, and if you do so please try to get as close as reasonable to wabac.js implementation (e.g. reuse same variables names when possible, etc ...) and also port all unit tests from wabac.js exercising this codebranch into your PR.
And I'm totally OK when Claude code takes the lead (I enjoy it actually on my own duty) if there is still a pilot responsible for decisions and final discussions ^^