Repository navigation
parser: preserve comments inside function parameter lists #36705
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
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d9bda24
parser: preserve comments inside function parameter lists
robobun 421a7bf
[autofix.ci] apply automated fixes
autofix-ci[bot] 8e58894
trim comments per comment-cop
robobun 3afb3d4
single-line comments
robobun 5fb70a2
scope preserve_all_comments_before to arg gaps only
robobun 402da9c
don't capture comments inside TS this-param/decorators; re-snapshot b…
robobun 3a58b80
drain leading comments before parse_type_script_decorators
robobun 3dc1be7
ci: retrigger
robobun 3ff12cd
test: use test.concurrent for independent subprocess tests
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| // https://github.com/oven-sh/bun/issues/13451 | ||
| // | ||
| // Karma's `di` package (and AngularJS $injector) parse Function.prototype.toString() | ||
| // to extract DI tokens from /* comment */-annotated parameter names, e.g. | ||
| // | ||
| // exports.create = function (/* config */ config, /* config.proxies */ proxies) { ... } | ||
| // | ||
| // Bun's runtime transpiler was discarding those comments, so `di` saw `proxies` | ||
| // instead of `config.proxies` and failed with "No provider for "proxies"!". | ||
|
|
||
| const FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m; | ||
| const FN_ARG = /\/\*([^\*]*)\*\//m; | ||
|
|
||
| function diParse(src: string): string[] { | ||
| const match = src.match(FN_ARGS); | ||
| if (!match || !match[1]) return []; | ||
| return match[1].split(",").map(arg => { | ||
| const m = arg.match(FN_ARG); | ||
| return m ? m[1].trim() : arg.trim(); | ||
| }); | ||
| } | ||
|
|
||
| test.concurrent("inline /* */ comments in function parameter lists survive the runtime transpiler", async () => { | ||
| using dir = tempDir("issue-13451", { | ||
| "mod.cjs": ` | ||
| exports.anonExpr = function (/* a */ x, /* b.c */ y, /* d */ z) { | ||
| return [x, y, z]; | ||
| }; | ||
|
|
||
| function decl(/* one */ p, /* two */ q) { | ||
| return [p, q]; | ||
| } | ||
| exports.decl = decl; | ||
|
|
||
| exports.obj = { | ||
| method(/* inner */ v) { | ||
| return v; | ||
| }, | ||
| }; | ||
|
|
||
| exports.karma = function (/* config */ config, /* config.proxies */ proxies, /* emitter */ emitter) { | ||
| return [config, proxies, emitter]; | ||
| }; | ||
|
|
||
| exports.lineComment = function ( | ||
| // token | ||
| value | ||
| ) { | ||
| return value; | ||
| }; | ||
|
|
||
| exports.noMigrateDefault = function (a = /* x */ 1, b) { | ||
| return [a, b]; | ||
| }; | ||
|
|
||
| exports.noMigrateTrailing = function (a /* trail */, b) { | ||
| return [a, b]; | ||
| }; | ||
|
|
||
| exports.noLeakIntoBody = function (a = () => { return 1 /* mid */ + 2; }, b) { | ||
| return [a, b]; | ||
| }; | ||
| `, | ||
| "main.cjs": ` | ||
| const m = require("./mod.cjs"); | ||
| console.log(JSON.stringify({ | ||
| anonExpr: m.anonExpr.toString(), | ||
| decl: m.decl.toString(), | ||
| method: m.obj.method.toString(), | ||
| karma: m.karma.toString(), | ||
| lineComment: m.lineComment.toString(), | ||
| noMigrateDefault: m.noMigrateDefault.toString(), | ||
| noMigrateTrailing: m.noMigrateTrailing.toString(), | ||
| noLeakIntoBody: m.noLeakIntoBody.toString(), | ||
| })); | ||
| `, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "main.cjs"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stderr).toBe(""); | ||
|
|
||
| const results = JSON.parse(stdout); | ||
|
|
||
| // anonymous function expression | ||
| expect(results.anonExpr).toContain("/* a */"); | ||
| expect(results.anonExpr).toContain("/* b.c */"); | ||
| expect(results.anonExpr).toContain("/* d */"); | ||
| expect(diParse(results.anonExpr)).toEqual(["a", "b.c", "d"]); | ||
|
|
||
| // named function declaration | ||
| expect(results.decl).toContain("/* one */"); | ||
| expect(results.decl).toContain("/* two */"); | ||
| expect(diParse(results.decl)).toEqual(["one", "two"]); | ||
|
|
||
| // object method shorthand | ||
| expect(results.method).toContain("/* inner */"); | ||
|
|
||
| // karma's exact pattern from middleware/proxy.js | ||
| expect(results.karma).toContain("/* config */"); | ||
| expect(results.karma).toContain("/* config.proxies */"); | ||
| expect(results.karma).toContain("/* emitter */"); | ||
| expect(diParse(results.karma)).toEqual(["config", "config.proxies", "emitter"]); | ||
|
|
||
| // line comment converted to block (otherwise it would swallow the arg list) | ||
| expect(results.lineComment).not.toMatch(/\/\/[^\n]*\)/); | ||
| expect(results.lineComment).toContain("token"); | ||
|
|
||
| // Comments inside a default value / after a binding must not migrate to the | ||
| // next argument (that would change the DI token), nor surface as statements | ||
| // inside a nested body. | ||
| expect(results.noMigrateDefault).not.toContain("/* x */"); | ||
| expect(diParse(results.noMigrateDefault)).toEqual(["a = 1", "b"]); | ||
| expect(results.noMigrateTrailing).not.toContain("/* trail */"); | ||
| expect(diParse(results.noMigrateTrailing)).toEqual(["a", "b"]); | ||
| expect(results.noLeakIntoBody).not.toContain("/* mid */"); | ||
|
|
||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test.concurrent("comments inside erased TS this-params/decorators are not relocated onto real args", async () => { | ||
| using dir = tempDir("issue-13451-ts", { | ||
| "in.ts": ` | ||
| export function f1(this: /* Self */ number, x: number) { return x; } | ||
| export function f2(/* ctx */ this: number, x: number) { return x; } | ||
| declare const dec: any; | ||
| export class C { | ||
| constructor(@dec(/* opts */) x: number) {} | ||
| } | ||
| `, | ||
| }); | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "build", "--target=bun", "in.ts"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stderr).toBe(""); | ||
| expect(stdout).not.toContain("/* Self */"); | ||
| expect(stdout).not.toContain("/* ctx */"); | ||
| expect(stdout).not.toContain("/* opts */"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test.concurrent("legal comments in parameter lists survive minification", async () => { | ||
| using dir = tempDir("issue-13451-legal", { | ||
| "in.js": ` | ||
| export const f = function(/*! @license MIT */ x) { return x; }; | ||
| export const g = function(/*! @preserve */) { return 1; }; | ||
| `, | ||
| }); | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "build", "--minify", "in.js"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stderr).toBe(""); | ||
| expect(stdout).toContain("/*! @license MIT */"); | ||
| expect(stdout).toContain("/*! @preserve */"); | ||
| 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.