-
Notifications
You must be signed in to change notification settings - Fork 4.7k
js_parser: sanitize auto-generated default export name for digit-named modules #31403
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
+70
−4
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0f04b58
js_parser: sanitize auto-generated default export name
robobun 6b87f02
test: harden issue 31401 regression test for Windows
robobun 0bb7514
test: use absolute file paths in issue 31401 regression test
robobun 52a84cd
[autofix.ci] apply automated fixes
autofix-ci[bot] b103c2a
test: use normalizeBunSnapshot for issue 31401 stdout assertions
robobun a9a963b
ci: retrigger
robobun d47b834
js_parser: fix fmt_identifier to sanitize leading non-identifier-star…
robobun 9beb90a
Merge remote-tracking branch 'origin/main' into farm/1629c71d/fix-dig…
robobun 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,73 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, normalizeBunSnapshot, tempDir } from "harness"; | ||
| import { join } from "node:path"; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // https://github.com/oven-sh/bun/issues/31401 | ||
| // | ||
| // An anonymous `export default function () {}` gets an auto-generated name | ||
| // derived from the module filename (`<name>_default`). When the filename | ||
| // starts with a digit (e.g. `1.ts`), the transpile / `bun run` path (which | ||
| // does not run the renamer) emitted `function 1_default()` — an invalid | ||
| // identifier that JSC's lexer rejected with | ||
| // "No identifiers allowed directly after numeric literal". | ||
| // The generated name must be sanitized up-front (→ `_1_default`). | ||
| describe.concurrent("issue 31401: anonymous default export from digit-named module", () => { | ||
| test("run a digit-named module with an anonymous default function", async () => { | ||
| using dir = tempDir("issue-31401-run", { | ||
| "1.ts": `export default function () {}\nconsole.log("ok");\n`, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), join(String(dir), "1.ts")], | ||
| env: bunEnv, | ||
| stderr: "pipe", | ||
| stdout: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect(stderr).not.toContain("No identifiers allowed directly after numeric literal"); | ||
| expect(normalizeBunSnapshot(stdout)).toMatchInlineSnapshot(`"ok"`); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test("import a digit-named module with an anonymous default function", async () => { | ||
| using dir = tempDir("issue-31401-import", { | ||
| "9mod.ts": `export default function () {}\n`, | ||
| "index.ts": `import f from "./9mod.ts";\nconsole.log(typeof f);\n`, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), join(String(dir), "index.ts")], | ||
| env: bunEnv, | ||
| stderr: "pipe", | ||
| stdout: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect(stderr).not.toContain("No identifiers allowed directly after numeric literal"); | ||
| expect(normalizeBunSnapshot(stdout)).toMatchInlineSnapshot(`"function"`); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test("transpile-only output uses a valid identifier for the generated default name", async () => { | ||
| using dir = tempDir("issue-31401-transpile", { | ||
| "1.ts": `export default function () {}\n`, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "build", "--no-bundle", join(String(dir), "1.ts")], | ||
| env: bunEnv, | ||
| stderr: "pipe", | ||
| stdout: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect(stderr).not.toContain("No identifiers allowed directly after numeric literal"); | ||
| // Must be a valid identifier: the leading digit gets an underscore prefix. | ||
| expect(normalizeBunSnapshot(stdout)).toContain("export default function _1_default() {}"); | ||
| 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.