-
Notifications
You must be signed in to change notification settings - Fork 5k
transpiler: preserve function-body "use strict" in CJS #29539
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
Closed
Closed
Changes from all commits
Commits
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
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
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,23 @@ | ||
| exports.isES5 = (function () { | ||
| "use strict"; | ||
| return this === undefined; | ||
| })(); | ||
|
|
||
| exports.typeofThis = (function () { | ||
| "use strict"; | ||
| return typeof this; | ||
| }).call("hello"); | ||
|
|
||
| exports.mode = (function () { | ||
| "use strict"; | ||
| // Unique sentinel so an earlier test can't poison the result by | ||
| // leaking a reachable global named `undeclared`. | ||
| delete globalThis.__issue29533_sentinel__; | ||
| try { | ||
| __issue29533_sentinel__ = 1; | ||
| delete globalThis.__issue29533_sentinel__; | ||
| return "sloppy"; | ||
| } catch (e) { | ||
| return "strict"; | ||
| } | ||
| })(); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
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,4 @@ | ||
| "use strict"; | ||
| exports.typeofThis = (function () { | ||
| return typeof this; | ||
| }).call("hello"); |
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,132 @@ | ||
| // https://github.com/oven-sh/bun/issues/29533 | ||
|
|
||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| test("function-body 'use strict' is preserved in CJS", () => { | ||
| const m = require("./29533-fn.fixture.cjs"); | ||
| expect(m.isES5).toBe(true); | ||
| expect(m.typeofThis).toBe("string"); | ||
| expect(m.mode).toBe("strict"); | ||
| }); | ||
|
|
||
| test("module-level 'use strict' still enforces strict mode in CJS", () => { | ||
| // The CJS wrapper re-emits module-level "use strict". | ||
| const m = require("./29533-module.fixture.cjs"); | ||
| expect(m.typeofThis).toBe("string"); | ||
| }); | ||
|
claude[bot] marked this conversation as resolved.
|
||
|
|
||
| // Subprocesses inherit the parent's bun — under `bun bd` (ASAN debug) that | ||
| // binary prints a JSC warning to stderr unconditionally. Assert stderr has | ||
| // no "error:" rather than toBe("") so the warning doesn't fail the test. | ||
| // (panics/ASSERTION FAILED already produce a non-zero exit code, which the | ||
| // expect(exitCode).toBe(0) at each test callsite catches.) | ||
| function expectNoStderrErrors(stderr: string) { | ||
| expect(stderr).not.toMatch(/^error:/im); | ||
| } | ||
|
robobun marked this conversation as resolved.
|
||
|
|
||
| test.concurrent("function-body 'use strict' preserved in .js with package type=commonjs", async () => { | ||
| // Same body shape as the fixture, but a .js file in a directory whose | ||
| // package.json declares "commonjs" — exercises the CJS classifier path for | ||
| // the extension the original reproduction hit (bluebird's index.js). | ||
| using dir = tempDir("issue-29533-js", { | ||
| "package.json": JSON.stringify({ type: "commonjs" }), | ||
| "entry.js": ` | ||
| var isES5 = (function () { | ||
| "use strict"; | ||
| return this === undefined; | ||
| })(); | ||
| var mode = (function () { | ||
| "use strict"; | ||
| try { | ||
| __issue29533_spawn_sentinel__ = 1; | ||
| return "sloppy"; | ||
| } catch (e) { | ||
| return "strict"; | ||
| } | ||
| })(); | ||
| console.log(isES5 + " " + mode); | ||
| `, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "run", "entry.js"], | ||
| cwd: String(dir), | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expectNoStderrErrors(stderr); | ||
| expect(stdout.trim()).toBe("true strict"); | ||
| expect(exitCode).toBe(0); | ||
|
robobun marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| test.concurrent("non-strict module-level directive doesn't suppress CJS 'use strict' re-emission", async () => { | ||
| // Regression guard: the CJS wrapper in P.zig skips re-emitting "use strict" | ||
| // when the module already starts with one. That check must look at the | ||
| // directive VALUE, not just the tag — otherwise a module that starts with | ||
| // e.g. "use client" (or any other custom directive) followed by "use | ||
| // strict" would run sloppy. | ||
| using dir = tempDir("issue-29533-use-client", { | ||
| "entry.cjs": ` | ||
| "use client"; | ||
| "use strict"; | ||
| var isStrict = (function () { | ||
| return this === undefined; | ||
| }).call(undefined); | ||
| module.exports.isStrict = isStrict; | ||
| console.log(isStrict); | ||
| `, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "run", "entry.cjs"], | ||
| cwd: String(dir), | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expectNoStderrErrors(stderr); | ||
| expect(stdout.trim()).toBe("true"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test.concurrent("block-scope string literals are not treated as directives", async () => { | ||
| // ES2015 §14.1.1 restricts the directive prologue to the leading | ||
| // ExpressionStatements of a FunctionBody or Script/Module. Block-scope | ||
| // string expressions (dead code) must remain ordinary expressions so | ||
| // DCE can still drop them under minify_syntax. Bundle with --minify and | ||
| // confirm the block-scope string is gone. | ||
| using dir = tempDir("issue-29533-block-dir", { | ||
| "entry.js": ` | ||
| function foo() { | ||
| if (true) { | ||
| "__block_scope_should_be_dropped__"; | ||
| console.log("hello"); | ||
| } | ||
| } | ||
| foo(); | ||
| `, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "build", "--minify", "--target=node", "entry.js"], | ||
| cwd: String(dir), | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expectNoStderrErrors(stderr); | ||
| expect(stdout).not.toContain("__block_scope_should_be_dropped__"); | ||
| expect(stdout).toContain('"hello"'); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.