Repository navigation
[jsifier] Share (and fix) function-matching regexes - #28288
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a centralized regular expression, "arrowFuncRe", to identify arrow function signatures in JS library snippets and replaces inline regex matches with this centralized pattern. It also adds a test suite to verify various arrow function patterns. The review feedback recommends improving the "arrowFuncRe" regex to support identifiers containing "$" and to handle "async" arrow functions without spaces before the parameter list. Additionally, it is suggested to allow leading whitespace in the "function" keyword check to prevent false negatives.
6d8271a to
35445e5
Compare
35445e5 to
0126659
Compare
0126659 to
09a8160
Compare
| // Finds the index of closing parens from the opening parens at arr[i]. | ||
| // Used polymorphically for strings ('foo') and token arrays (['(', 'foo', ')']) as input. | ||
| $find_closing_parens_index: (arr, i, opening='(', closing=')') => { | ||
| $find_closing_parens_index: function(arr, i, opening='(', closing=')') { |
There was a problem hiding this comment.
Is there a reason this had to change, or was this just a drive-by of some kind?
There was a problem hiding this comment.
Basically, if we left this as an arrow function it would be beyond the power of the regex to solve (since there is an embedded closing brace inside a string, which confused the regex).
Changing it to use function works around it, and also matches the function just below.
If anyone tries to change it back we now have an assert that would catch it.
There was a problem hiding this comment.
There is a paragraph about this in the PR description.
I asked Gemini to find a way to make it work, and it restored to using acorn to actually parse the JS.
09a8160 to
5f59c07
Compare
Extract shared module-level `arrowFuncRe` and `regularFuncRe` regexes in
`src/jsifier.mjs` and use both for stringified function detection and
mangled name emission.
Previously:
- Both arrow regexes used `[^}]*`, failing on destructured parameters
(e.g. `({a}) => ...`), omitting single-arg (including `$` and `async`
as a parameter) and `async` arrow forms (including `async(x) => ...`),
and potentially misclassifying regular functions with default arrow
parameters.
- Stringified `function` detection (`/^function\b/`) did not allow
leading whitespace or `async`, and did not share the regular-function
name-replacement regex (`regularFuncRe`), which now anchors at `^`,
requires `(`, and preserves leading whitespace and `async`.
- Stringified function detection lacked a `!isConst` guard, risking
treating `__const` string literals like `x => ...` or `function(...)`
as functions.
Also assert that `regularFuncRe` matches in the regular-function
fallback, and convert `$find_closing_parens_index` in
`src/lib/libc_preprocessor.js` from an arrow function to a regular
`function` so its `closing=')'` default parameter doesn't trip
`arrowFuncRe`'s `[^)]*` parameter match.
Add `test_jslib_function_forms` in `test/test_jslib.py` covering these
cases.
5f59c07 to
a962057
Compare
Followup to #28286 Depends on #28288 Use object and parameter destructuring for multi-property reads and unused event parameters across `src/lib/` and `src/pthread_esm_startup.mjs`, using `const` for declarations so Closure Compiler lowers them cleanly without extra bindings. Update affected `test/codesize/` expectations. See: #28272
Extract shared module-level
arrowFuncReandregularFuncReregexes insrc/jsifier.mjsand use both for stringified function detection andmangled name emission.
Previously:
[^}]*, failing on destructured parameters(e.g.
({a}) => ...), omitting single-arg (including$andasyncas a parameter) and
asyncarrow forms (includingasync(x) => ...),and potentially misclassifying regular functions with default arrow
parameters.
functiondetection (/^function\b/) did not allowleading whitespace or
async, and did not share the regular-functionname-replacement regex (
regularFuncRe), which now anchors at^,requires
(, and preserves leading whitespace andasync.!isConstguard, riskingtreating
__conststring literals likex => ...orfunction(...)as functions.
Also assert that
regularFuncRematches in the regular-functionfallback, and convert
$find_closing_parens_indexinsrc/lib/libc_preprocessor.jsfrom an arrow function to a regularfunctionso itsclosing=')'default parameter doesn't triparrowFuncRe's[^)]*parameter match.Add
test_jslib_function_formsintest/test_jslib.pycovering thesecases.