Repository navigation
Conversation
In replMode, an input that looks like an object literal is parsed inside
parentheses, so `{ a: 1 }` is an expression. Only get_parse_result() did
this, so only transformSync() and scan() had it. transform() and
scanImports() build their own source. transform() parsed `{ a: 1 }` as a
block with a label, and both methods threw a syntax error for
`{a: 1, b: 2}`.
get_parse_result() also wrapped the input of a data loader. In replMode,
transformSync('{"a":1}', "json") threw `Unexpected (`, and the text
loader returned "({hi})".
repl_mode_source() now decides for all four methods. It wraps only for a
JavaScript-like loader. The wrap itself is wrap_likely_object_literal(),
which the CLI REPL also calls in place of its own copy.
|
Status: ready for review. The design changed since the first version: the parser now decides between an object literal and a block, and no method parses a copy of the input in parentheses. How to reproduce on bun 1.4.3: const t = new Bun.Transpiler({ loader: "ts", replMode: true });
t.transformSync("{ let x = 1; x }"); // throws: Expected "}" but found "x"
await t.transform("{ let x = 1; x }"); // a block
t.transformSync("{a: 1, b: 2}"); // the value wrapper around { a: 1, b: 2 }
await t.transform("{a: 1, b: 2}"); // throws: Expected ";" but found ":"
t.scanImports("{a: 1, b: require('y')}"); // throws the same errorWith this PR the four methods read each input the same way: a block is a block and an object literal is an object literal.
One question is open for a maintainer (in the Notes of the description): is one never-taken test of |
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline finding, I also checked the two arena-lifetime paths the wrap newly creates: in TransformTask::run the wrapped bytes die with the per-call arena before the rejection is built on the JS thread, but Location::init_or_null_impl (src/ast/lib.rs) stores line_text as Cow::Owned and file borrows the static stdin name, so the deferred log.to_js reads no arena memory; in scan_imports, named_imports_to_js consumes the import records before arena drops. Neither is affected.
Extended reasoning...
The change factors the replMode object-literal wrap in src/runtime/api/JSTranspiler.rs into repl_mode_source/wrap_likely_object_literal and applies it to transform(), transformSync(), scan(), and scanImports(), with the CLI REPL in src/runtime/cli/repl.rs sharing the helper and allocating in the parse arena; no security-sensitive surface is touched. One inline finding (error positions shifted by the injected paren for transform/scanImports) is already posted, so this note only records the lifetime checks that were ruled out.
…spiler-async-repl-object-literal
With repl_mode, an input that starts with `{` and does not end with `;`
is read as one expression by a parser of its own. If the input is not one
expression, or the visit pass rejects it, another parser reads it as
statements. Bun.Transpiler and bun repl no longer parse a copy of the
input inside parentheses. A block is not a syntax error in transformSync()
and scan(), an object literal is not a block in transform() and
scanImports(), and an error position is a position in the input.
bun repl asks for the expression reading only. It still evaluates a block
as written.
|
Updated 2:51 AM PT - Oct 14th, 2026
✅ @robobun, your commit 002e3d7ec3137a1fca59a0098abadb1bb81d9b6d passed in 🧪 To try this PR locally: bunx bun-pr 43770That installs a local version of the PR into your bun-43770 --bun |
The reading of a block as one expression is dropped, so the parser does not continue after the first token that is not the expected one.
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline finding, I also checked error attribution when both readings fail in src/js_parser/parse/parse_entry.rs (parse_repl_expression_or_statements / scan_imports_of_repl_expression_or_statements): the reported log now carries positions in the caller's own input with no (-wrapper shift, and the statements-first rule is the deliberate, documented choice, so it is not a regression. expected_is_fatal is reset on every exit of read_repl_expression, and core::mem::take(&mut p.options) is backed by the existing Options: Default impl.
Extended reasoning...
The change moves the REPL object-literal-vs-block decision from two textual (+code+) wrappers (CLI REPL and Bun.Transpiler) into the parser via a new ReplLeadingBrace option, a scratch-log two-reading strategy in _parse and _scan_imports, and a lexer expected_is_fatal flag; it touches no security-sensitive surface. One confirmed finding is posted inline; this note records only the additional concerns examined and ruled out this run.
|
Closing as stale: this PR conflicts with main, and its last commit is from 2026-10-14. The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR. |
Problem
replMode: true,transformSync()andscan()parse(+ input +)if the input starts with{and does not end with;. A block fails:transformSync("{ let x = 1; x }")throwsExpected "}" but found "x".transform()andscanImports()parse the input as written (src/runtime/api/JSTranspiler.rs). An object literal fails:transform("{a: 1, b: 2}")throwsExpected ";" but found ":".Fix
Parser::parse_repl_expression_or_statements(src/js_parser/parse/parse_entry.rs) reads such an input as one expression. If that fails, a second parser reads statements. The copies are deleted.{ a: }fails at 1:6, not 1:7.bun replasks for the expression only and still evaluates a block as written.test/js/bun/transpiler/repl-transform.test.ts(bun 1.4.3 fails 55 of 106),test/js/bun/repl/repl.test.ts. Self-reviewed: 13 concerns raised, 13 addressed.Background
replModeis aBun.Transpileroption for REPL input. Node's REPL evaluates(input)first, so that{ a: 1 }is an object, then the input as written.JSTranspiler.rs: it keeps the copy and the shifted positions. No user reported this.Downsides
repl_modetest inParser::_parse(+2 instructions). The binary grows by 10.5 KB.replMode, a block costs two more parser setups: +22 allocations pertransform()call, +3 µs perscanImports()call.{a: 1} // noteis an object literal (Node prints 1).transformSync("{a:1}) + (2")now fails.Notes
Repro (columns:
transformSync,transform,scan,scanImports, withnew Bun.Transpiler({ loader: "ts", replMode: true })):1,500 generated inputs that start with a brace (a seeded generator: blocks, object literals, and 16 suffixes such as
;,// note,.a,\nrender()). 1,019 of them are valid as statements or as one expression.transformSynctransformscanscanImportstransform()returns the same code astransformSync(), or both refuse: 564 of 1,500 on 1.4.3, 1,500 with this PR.scanImports()takes, because it has no visit pass (1 on 1.4.3).transform()where the input is statements, and the code oftransformSync("(" + input + "\n)")where it is one expression.Measured cost (release builds for linux-x64 of main 177da66 and of this PR at fc508ca)
replModeoff:Parser::_parsegets 2 instructions in front of the top-level statement list (cmpb $0x0, <repl_mode>,jne), and none after it. This is from the disassembly (llvm-objdump) of both builds, for_parse::<false>and_parse::<true>. The build machine has noperfcounters and novalgrind, so there is no instruction count of a whole run. The frame of_parse::<false>is 10,008 B (was 9,992 B). The frame of_parse::<true>stays at 9,584 B..text80,719,717 B to 80,730,213 B (+10,496 B,size -A). The stripped binary grows by 8,192 B. The new code is cold:read_repl_leading_brace(2 copies of 1,525 B),read_repl_expression(4 copies of 825 B), and +658 B and +1,431 B in the two copies of_parse.gdb, the difference between 1,500 and 500 calls):transformSynctransformscanscanImportsreplModeoff,const a: number = 1replModeon,const a: number = 1replModeon,{a: 1, b: 2}replModeon,{ foo() }replModeon: 1 for an input that does not start with{, 2 for an object literal, 3 for a block. The first entry only starts the others. The expression reading of a block stops at the first token that is not the expected one (Lexer::expected_is_fatal), so a block does not pay a full parse pass.{ let x = 1; x }:scanImports()8.4 µs to 11.6 µs (noise 1.1%).transform()131 µs to 144 µs (noise 4.4%).{a: 1, b: 2}:transformSync()13.7 µs to 13.9 µs (noise 0.9%).transform()+3.5% (noise 1.1%),scanImports()+0.8% (noise 2.9%). An object literal of 181 KB:transformSync()-2.4% (noise 3.0%).Which errors, if the input is not statements either
{a: 1, b: }isExpected ";" but found ":"at 1:9.{a: () => { const c = 1; c = 2 }, b: 2}isCannot assign to "c" because it is a constant.{ a: 1, b: 2,isExpected identifier but found end of file. A REPL that reads input line by line can look forend of fileto ask for the next line.dannote/just-bun(lib/console.ts) does that withtransformSync(). A run of 28 object literals typed line by line reaches the last line in 25 on 1.4.3 and in 28 with this PR. For 16 blocks the numbers are 2 and 14.transformSync()and 42 intransform().Differences from the REPL of Node.js v26.3.0 (
lib/repl.jsL458-L462 and L550-L561){a: 1} // note: Node prints 1, because the comment takes the). Here it is an object literal in the four methods and inbun repl.bun repl1.4.3 prints 1.{ await x; y }: Node rejects it. Here it is a block, as intransform()andscanImports()on 1.4.3._=inputis valid, Node reports the error of(input). Here the rule above decides.bun repl{ let x = 1; x }prints 1,{ function f() {} }thentypeof fprints"function"), with two changes:{ a: 1 } // noteprints the object (was 1), and{a:1}) + (2is a SyntaxError (was"[object Object]2").bun repl -escripts, 1 changes:{a: 1} // noteexits with code 0 (was 1).What the four methods still do not share
scanImports()has no visit pass. It keeps the expression reading of{}\n/require('a')/g.x = 1and returns therequire-call.scanImports()builds its own parser options. For example it rejectsclass A { accessor x = 1 }, with and withoutreplMode.Open question for a maintainer:
Parser::_parsegets one test ofrepl_modein front of the top-level statement list. There are three places for the decision._parse(+2 instructions per parsed file, never taken withoutreplMode). The parser already testsrepl_modeonce per file inP::to_ast(src/js_parser/p.rs).(+ input +)and add a second parse at the three sites inJSTranspiler.rsand inrepl.rs. The copy and the shifted positions stay.constparameter on_parseand_scan_imports: no test at run time, and a second copy of both functions (about 59 KB for_parse, about 10 KB for_scan_imports).Out of scope, reported separately
bun repl -eand-pexit with code 1 for a block that ran.Bun.Transpiler.scan()lists noimport()when dead code elimination is off, whichreplModeturns off.{input in parentheses with no second attempt.bun replgives a statement no completion value, so a block cannot go through the REPL transform yet. That is whybun replasks for the expression reading only.Open PRs that touch the same parse sites in
JSTranspiler.rs: #40405, #43771, #30540, #37601, #42433, #42430. This PR returnsTransformTask::runto the text of main and removes lines fromget_parse_result.[human-review] gate passed · iteration 0 · 3 files touched
fails on main (without fix)
passes on PR (with fix)
diff hotspot
gate history · 1 passed · 0 rejected · iteration 0
evidence per changed file