Skip to content

Hoist top-level function declarations out of the wombat block - #370

Merged
benoit74 merged 3 commits into
mainfrom
benoit74/hoist-top-level-functions
Oct 29, 2026
Merged

benoit74 merged 3 commits into
mainfrom
benoit74/hoist-top-level-functions

Conversation

@benoit74

@benoit74 benoit74 commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Backport of webrecorder/wabac.js#380 (released in wabac.js 2.27.x, fixes webrecorder/wabac.js#379) into our port of jsrewriter.ts, as a follow-up of #329 which is not released yet.

Changes:

  • top-level function declarations (including async, generator and async generator ones) are now carried across the wombat block like const and class, and re-declared as var after it, as they are on the global scope; this fixes pages calling, from one script, a function declared at the top level of another one
  • top-level class declarations are re-declared as let instead of const, since a class binding can be reassigned
  • the carrier is renamed from self.___WB_const_x to self.___WB_hoist_x, to stay in line with wabac.js
  • a nameless function () {} / class {} is a function / class expression for tree-sitter, never a declaration, so no name guard is needed (unlike wabac.js if (expr.id.name) check with acorn)

Tests ported from wabac.js test/rewriteJS.ts (combined fixture with function E / async function* F, expected strings verbatim), plus dedicated tests for functions of every kind, class reassignment and nameless function expressions.

Also records, in src/zimscraperlib/rewriting/js.py docstring and rules/rules.yaml header, that both jsrewriter.ts and fuzzymatcher.ts are now in sync with wabac.js 2.27.4 (fuzzymatcher.ts has not changed since previous sync point).

Backport of webrecorder/wabac.js#380 (fixes webrecorder/wabac.js#379):
- carry top-level function declarations across the block, re-declared
  as var as they are on the global scope
- re-declare classes as let instead of const, their binding can be
  reassigned
- rename the carrier from self.___WB_const_x to self.___WB_hoist_x
@codecov

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (2ff8d1e) to head (7dc0159).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #370   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           42        42           
  Lines         2863      2868    +5     
  Branches       430       432    +2     
=========================================
+ Hits          2863      2868    +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

tree-sitter never yields a nameless class or function declaration: a
nameless function or class parses as an expression.
@benoit74 benoit74 self-assigned this Oct 29, 2026
@benoit74 benoit74 added this to the 5.5.0 milestone Oct 29, 2026
@benoit74
benoit74 merged commit acce16f into main Oct 29, 2026
9 of 10 checks passed
@benoit74
benoit74 deleted the benoit74/hoist-top-level-functions branch October 29, 2026 09:50
@benoit74 benoit74 mentioned this pull request Oct 29, 2026
17 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hoist function names as well

1 participant