-
-
Notifications
You must be signed in to change notification settings - Fork 132
fix(cjs-wrap): function-nested class named export lost (ajv 'undefined is not a constructor') #5325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| #!/bin/bash | ||
| # Regression (fix/ajv-not-constructor): a compiled CJS package whose class is | ||
| # kept inside the cjs_wrap IIFE (#5251 hoist guard — the class body references | ||
| # the injected `exports`) must still surface its `exports.X = X` named export to | ||
| # `require('./code').X`. The HIR lowering for a function-nested `class X {}` now | ||
| # defines a scope-local binding shadowing the outer same-named re-export const, | ||
| # matching how a nested `function X(){}` already behaves. | ||
| # | ||
| # Pre-fix: `require('./code').Name` was `undefined` (the in-IIFE `exports.Name = | ||
| # Name` resolved `Name` to the circular module-scope `const Name = _cjs.Name`), | ||
| # so ajv's `new code_1.Name(...)` threw "undefined is not a constructor". | ||
| # The function variant in the identical shape always worked — this guards both. | ||
|
|
||
| set -e | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" | ||
| PERRY="$SCRIPT_DIR/../target/release/perry" | ||
| [ ! -f "$PERRY" ] && PERRY="$SCRIPT_DIR/../target/debug/perry" | ||
| if [ ! -f "$PERRY" ]; then | ||
| echo "SKIP: perry binary not found (build with cargo build --release)" | ||
| exit 0 | ||
| fi | ||
| if ! command -v cc >/dev/null 2>&1; then | ||
| echo "SKIP: cc not available" | ||
| exit 0 | ||
| fi | ||
|
|
||
| TMPDIR=$(mktemp -d) | ||
| trap "rm -rf $TMPDIR" EXIT | ||
|
|
||
| PKG="$TMPDIR/node_modules/pk" | ||
| mkdir -p "$PKG" | ||
|
|
||
| # code.js — the kept-in-IIFE shape: `class Name` whose ctor reads the injected | ||
| # `exports`, then `exports.Name = Name`. The #5251 guard keeps the class inside | ||
| # the IIFE; the named export must still reach the module's external exports. | ||
| cat > "$PKG/code.js" << 'EOF' | ||
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.Name = void 0; | ||
| exports.IDENTIFIER = /^[a-z]+$/i; | ||
| class Name { constructor(s){ if(!exports.IDENTIFIER.test(s)) throw new Error("bad"); this.str = s; } } | ||
| exports.Name = Name; | ||
| EOF | ||
|
|
||
| # func.js — the FUNCTION variant of the identical shape (the asymmetry to guard | ||
| # against re-breaking): a function kept in the IIFE exported the same way. | ||
| cat > "$PKG/func.js" << 'EOF' | ||
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.Make = void 0; | ||
| exports.PREFIX = "v"; | ||
| function Make(s){ this.tag = exports.PREFIX + s; } | ||
| exports.Make = Make; | ||
| EOF | ||
|
|
||
| # scope.js — cross-module consumer that constructs the kept-in-IIFE class at | ||
| # module-init time (mirrors ajv's scope.js `new code_1.Name("const")`). | ||
| cat > "$PKG/scope.js" << 'EOF' | ||
| "use strict"; | ||
| const code_1 = require("./code"); | ||
| const func_1 = require("./func"); | ||
| exports.varKinds = { const: new code_1.Name("const") }; | ||
| exports.made = new func_1.Make("x"); | ||
| EOF | ||
|
|
||
| cat > "$PKG/index.js" << 'EOF' | ||
| "use strict"; | ||
| const code_1 = require("./code"); | ||
| const func_1 = require("./func"); | ||
| const scope_1 = require("./scope"); | ||
| module.exports = function(){ | ||
| return "Name=" + typeof code_1.Name | ||
| + " Make=" + typeof func_1.Make | ||
| + " kind=" + String(scope_1.varKinds.const) | ||
| + " made=" + scope_1.made.tag; | ||
| }; | ||
| EOF | ||
|
|
||
| cat > "$PKG/package.json" << 'EOF' | ||
| { "name":"pk","version":"1.0.0","main":"index.js" } | ||
| EOF | ||
|
|
||
| cat > "$TMPDIR/package.json" << 'EOF' | ||
| { "type":"module","private":true,"perry":{"compilePackages":["*"],"allow":{"compilePackages":["*"]}} } | ||
| EOF | ||
|
|
||
| cat > "$TMPDIR/main.ts" << 'EOF' | ||
| import pk from "pk"; | ||
| console.log(pk()); | ||
| EOF | ||
|
|
||
| cd "$TMPDIR" | ||
| COMPILE_OUTPUT=$(PERRY_NO_AUTO_OPTIMIZE=1 "$PERRY" main.ts -o test_bin --no-cache 2>&1) || { | ||
| echo "FAIL: compile error" | ||
| echo "$COMPILE_OUTPUT" | tail -20 | ||
| exit 1 | ||
| } | ||
|
|
||
| RUN_OUTPUT=$(./test_bin 2>&1) | ||
| # Matches `node main.ts`: the class & function both export as functions, and the | ||
| # cross-module `new code_1.Name(...)` / `new func_1.Make(...)` succeed. | ||
| EXPECTED="Name=function Make=function kind=[object Object] made=vx" | ||
|
|
||
| if [ "$RUN_OUTPUT" = "$EXPECTED" ]; then | ||
| echo "PASS" | ||
| exit 0 | ||
| fi | ||
|
|
||
| echo "FAIL: kept-in-IIFE class named export not visible on require()" | ||
| echo "Expected: $EXPECTED" | ||
| echo "Got: $RUN_OUTPUT" | ||
| exit 1 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard runtime execution so failures still print diagnostics.
At Line 100,
set -ecan terminate the script before theExpected/Gotfailure output runs. Add explicit handling around./test_binso runtime regressions remain debuggable.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents