fix: treat private class methods as function boundaries in TLA detection - #307
Conversation
|
I only noticed now that PR #300 fix it another way. Sorry for this duplicate, you can close this PR if you think the other fix is better |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #307 +/- ##
==========================================
- Coverage 87.23% 86.47% -0.77%
==========================================
Files 23 23
Lines 7929 7917 -12
Branches 1214 1213 -1
==========================================
- Hits 6917 6846 -71
- Misses 1005 1063 +58
- Partials 7 8 +1
🚀 New features to boost your workflow:
|
2f6ea0b to
8d8bcf8
Compare
| // node-opcua >= 2.185 ships ESM and relies on `import.meta.dirname`, | ||
| // which esbuild empties when emitting CJS | ||
| '--define:import.meta.dirname=__dirname', | ||
| '--define:import.meta.filename=__filename', |
There was a problem hiding this comment.
tests on ubuntu-latest 22.x and 24.x were failing without it : https://github.com/yao-pkg/pkg/actions/runs/36423145365
I've removed it anyway since you pinned node-opcua to 2.181.0 in tests.
I directly edited my commit
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The generalized Babel function-boundary check correctly addresses the regression and has focused unit coverage.
Review effort: Balanced
Findings: None
What changed in this PR
Fixes false top-level-await detection for private class methods.
Changes:
- Uses Babel’s function alias for all function boundaries.
- Adds regression coverage for private async methods.
- Defines
import.metapaths in the node-opcua bundle test.
| File | Description |
|---|---|
lib/esm-transformer.ts |
Uses isFunction() during await detection. |
test/unit/esm-transformer.test.ts |
Tests private methods containing await expressions. |
test/test-80-compression-node-opcua/main.js |
Supplies CJS-compatible import.meta definitions. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
f96c783 to
e7491b8
Compare
`detectESMFeatures` did not stop at `ClassPrivateMethod` when climbing from an `await` / `for await`, so `await` inside `async #method()` was reported as top-level await. Combined with exports, the module was left untransformed, bytecode generation failed and the file was skipped from the executable (e.g. node-opcua >= 2.184). Use Babel's `isFunction()` alias, which covers every function-like node. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
e7491b8 to
ffa3726
Compare
Problem
detectESMFeaturesclimbs up from eachAwaitExpression/for awaitand stops at function boundaries. The check listsFunctionDeclaration,FunctionExpression,ArrowFunctionExpression,ObjectMethodandClassMethod, but notClassPrivateMethod. Soawaitinsideasync #method()is reported as top-level await.If the module also has
exportstatements, the transformer logs "has both top-level await and export statements" and returns the source untransformed. Bytecode generation then fails and the file is skipped from the executable. The binary crashes at runtime, e.g.ERR_MODULE_NOT_FOUNDfrom a sibling import.Real-world trigger:
node-opcua≥ 2.184 (node-opcua-chunkmanager,node-opcua-server,node-opcua-pki,node-opcua-certificate-manager, …) uses private async methods, so any app bundling it gets a broken executable with pkg 6.22.0.Fix
Replace the list of checks with
parent.isFunction(). Babel'sFunctionalias covers all function-like nodes, includingClassPrivateMethod, so no future function kind can slip through.Tests
await/for awaitinside private methods of an exported class. It fails before the change and passes after.yarn test:unit(258 pass) andyarn lintare clean.node26-macos-arm64): the warnings are gone and the executable starts.🤖 Generated with Claude Code