Repository navigation
transpiler: preserve non-ASCII source text at runtime #33868
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 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2632d5d
transpiler: preserve non-ASCII source text at runtime
robobun 73188c4
[autofix.ci] apply automated fixes
autofix-ci[bot] 7345f95
ref_counted_resolved_source: transcode non-ASCII printer output
robobun 3bbc8ae
address review: already_bundled path, stale comment, describe.concurrent
robobun a1c2cd2
test: drop exact-empty stderr assertions
robobun cec390e
bunshell.test: update 'escape unicode' expectation
robobun 67571f1
ci: retrigger
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
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
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
106 changes: 106 additions & 0 deletions
106
test/js/bun/transpiler/transpiler-non-ascii-source-text.test.ts
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,106 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| // ES2026 §20.2.3.5: Function.prototype.toString must return the source text | ||
| // the host has for the function. The runtime transpiler used to rewrite every | ||
| // non-ASCII codepoint to an escape sequence, which leaked into toString(), | ||
| // RegExp#source, and tagged-template .raw. | ||
|
|
||
| const fixture = ` | ||
| function f() { return "café"; } | ||
| class Café { méth(x) { return \`é\${x}é\`; } } | ||
| const rx = () => /π+/u.test("ππ"); | ||
| const tag = (a) => a.raw[0]; | ||
| const raw = () => tag\`你好𐃘\\\\\`; | ||
| const dyn = new Function('return "café";'); | ||
|
|
||
| console.log(JSON.stringify({ | ||
| value: f(), | ||
| fn: f.toString(), | ||
| cls: Café.toString(), | ||
| rx: rx.toString(), | ||
| raw: raw(), | ||
| dyn: dyn.toString(), | ||
| emoji: (() => "👍").toString(), | ||
| })); | ||
| process.exit(0); | ||
| `; | ||
|
|
||
| async function run(cmd: string[], cwd: string) { | ||
| await using proc = Bun.spawn({ | ||
| cmd, | ||
| env: bunEnv, | ||
| cwd, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| return { stdout, stderr, exitCode }; | ||
| } | ||
|
|
||
| describe("runtime transpiler preserves non-ASCII source text", () => { | ||
| for (const [label, args] of [ | ||
| [".mjs", ["entry.mjs"]], | ||
| [".cjs", ["entry.cjs"]], | ||
| [".ts", ["entry.ts"]], | ||
| [".mjs --hot", ["--hot", "entry.mjs"]], | ||
| ] as const) { | ||
| test(label, async () => { | ||
| const ext = args[args.length - 1].split(".").pop()!; | ||
| using dir = tempDir("non-ascii-src", { [`entry.${ext}`]: fixture }); | ||
| const { stdout, stderr, exitCode } = await run([bunExe(), ...args], String(dir)); | ||
| expect(stderr).toBe(""); | ||
|
Check warning on line 52 in test/js/bun/transpiler/transpiler-non-ascii-source-text.test.ts
|
||
|
robobun marked this conversation as resolved.
Resolved
|
||
| const out = JSON.parse(stdout); | ||
|
|
||
| // Evaluated value — must be the real string, not mojibake | ||
| expect(out.value).toBe("café"); | ||
|
|
||
| // Function.prototype.toString — string literal preserved verbatim | ||
| expect(out.fn).toContain('"café"'); | ||
| expect(out.fn).not.toContain("\\xE9"); | ||
| expect(out.fn).not.toContain("\\u"); | ||
|
|
||
| // Class source — identifier, method name, template literal all preserved | ||
| expect(out.cls).toContain("Café"); | ||
| expect(out.cls).toContain("méth"); | ||
| expect(out.cls).toContain("`é${x}é`"); | ||
| expect(out.cls).not.toContain("\\u{e9}"); | ||
| expect(out.cls).not.toContain("\\xE9"); | ||
|
|
||
| // RegExp literal inside a function body | ||
| expect(out.rx).toContain("/π+/u"); | ||
| expect(out.rx).not.toContain("\\u03C0"); | ||
|
|
||
| // Tagged template .raw — non-ASCII plus a raw backslash preserved | ||
| expect(out.raw).toBe("你好𐃘\\\\"); | ||
|
|
||
| // Astral codepoint in a string literal | ||
| expect(out.emoji).toContain('"👍"'); | ||
|
|
||
| // Control: new Function bodies never pass through the file transpiler | ||
| expect(out.dyn).toContain('"café"'); | ||
|
|
||
| expect(exitCode).toBe(0); | ||
| }); | ||
| } | ||
|
|
||
| test("RegExp.prototype.source", async () => { | ||
| using dir = tempDir("non-ascii-rx", { | ||
| "entry.mjs": `console.log(/π+/u.source);`, | ||
| }); | ||
| const { stdout, stderr, exitCode } = await run([bunExe(), "entry.mjs"], String(dir)); | ||
| expect(stderr).toBe(""); | ||
| expect(stdout).toBe("π+\n"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test("String.raw with non-ASCII", async () => { | ||
| using dir = tempDir("non-ascii-raw", { | ||
| "entry.mjs": `console.log(String.raw\`你好\\n𐃘\`);`, | ||
| }); | ||
| const { stdout, stderr, exitCode } = await run([bunExe(), "entry.mjs"], String(dir)); | ||
| expect(stderr).toBe(""); | ||
| expect(stdout).toBe("你好\\n𐃘\n"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
| }); | ||
|
robobun marked this conversation as resolved.
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
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.