Skip to content
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8bd0c41
repl: recognize regex literals and comments in continuation detection
robobun Aug 10, 2026
7c6ebe5
repl: do not let a trailing escape carry a regex scan past the newline
robobun Aug 10, 2026
6e4c4a6
repl: tighten regex scan comment
robobun Aug 10, 2026
9a67d3a
repl: refine regex-vs-division heuristic for conditionals, properties…
robobun Aug 10, 2026
cf1ddd5
repl: condense scanner comments to single lines
robobun Aug 10, 2026
0064fa8
[autofix.ci] apply automated fixes
autofix-ci[bot] Aug 10, 2026
ef1f3ae
repl: treat postfix inc/dec and object literals in groups as division…
robobun Aug 10, 2026
072a487
repl: treat JSX closing tags, non-null assertions, and nested object …
robobun Aug 10, 2026
a6316d1
repl: classify braces as blocks or object literals for division context
robobun Aug 10, 2026
c926c3e
repl: condense brace classification comment
robobun Aug 10, 2026
b2da29e
[autofix.ci] apply automated fixes
autofix-ci[bot] Aug 10, 2026
256107a
repl: handle chained non-null assertions before division
robobun Aug 10, 2026
7123713
repl: a block closing inside parens or brackets stays in expression c…
robobun Aug 10, 2026
f8eb6c8
repl: treat private identifiers as member access in division context
robobun Aug 10, 2026
12ed2f5
repl: a > closing a JSX tag ends an expression
robobun Aug 10, 2026
166b697
ci: retrigger
robobun Aug 10, 2026
c1181e4
repl: handle object literal non-null assertions, spread objects, and …
robobun Aug 10, 2026
d913a8c
repl: allow same-line whitespace before postfix bang and JSX tag-end
robobun Aug 10, 2026
b77a864
repl: keyword-named members before a bang, string closers, and compou…
robobun Aug 11, 2026
573013e
repl: condense guard comments
robobun Aug 11, 2026
c550e55
Merge branch 'main' into farm/10b8d6c1/repl-regex-literals
alii Aug 11, 2026
bfdcb01
repl: nested generic closers and as/satisfies type positions are divi…
robobun Aug 11, 2026
f1dacfe
repl: use a banner-proof needle in the nested generic test
robobun Aug 11, 2026
dcc61e2
repl: multi-argument generic closers are division context
robobun Aug 11, 2026
202d8af
repl: condense generic closer comment
robobun Aug 11, 2026
adf53a8
repl: void after as or satisfies is a type, not the void operator
robobun Aug 11, 2026
3a95083
repl: generic closers with inner type arguments are division context
robobun Aug 11, 2026
dbac44f
repl: carry type position across unions and into non-null assertions
robobun Aug 11, 2026
c4b94fc
repl: intersection types in generic arguments are division context
robobun Aug 11, 2026
707e561
Merge remote-tracking branch 'origin/main' into farm/10b8d6c1/repl-re…
robobun Oct 19, 2026
fb553e4
repl: test comment and regex shapes fed by a pipe and typed on a term…
robobun Oct 19, 2026
19c1072
repl: use is_word_byte in the continuation scan so non-ASCII identifi…
robobun Oct 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 162 additions & 26 deletions src/runtime/cli/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2492,61 +2492,197 @@
}

fn is_incomplete_code(code: &[u8]) -> bool {
const fn is_word_char(ch: u8) -> bool {
ch.is_ascii_alphanumeric() || ch == b'_' || ch == b'$'
}
Comment thread
robobun marked this conversation as resolved.
Resolved

/// Keywords after which a `/` starts a regex literal rather than division.
const REGEX_KEYWORDS: &[&[u8]] = &[
b"return",
b"typeof",
b"instanceof",
b"in",
b"of",
b"new",
b"delete",
b"void",
b"throw",
b"case",
b"do",
b"else",
b"yield",
b"await",
];

let mut brace_count: i32 = 0;
let mut bracket_count: i32 = 0;
let mut paren_count: i32 = 0;
let mut in_string: u8 = 0;
let mut in_template = false;
let mut in_block_comment = false;
let mut escaped = false;

for &ch in code {
if escaped {
escaped = false;
// Regex-vs-division context: last significant byte, and the identifier ending at it.
let mut prev: u8 = 0;
let mut prev_idx = 0usize;
let mut word_start = 0usize;
let mut word_end = 0usize;
// `obj.in` is a property access, not the `in` keyword.
let mut word_after_dot = false;
// Parens opened by `if`/`for`/`while`/`with`; a `/` after such a `)` is a regex.
let mut paren_stack: Vec<bool> = Vec::new();
let mut last_paren_conditional = false;

let mut i = 0usize;
while i < code.len() {
let ch = code[i];

if in_block_comment {
if ch == b'*' && code.get(i + 1) == Some(&b'/') {
in_block_comment = false;
i += 2;
} else {
i += 1;
}
continue;
}

if in_string != 0 || in_template {
if escaped {
escaped = false;
} else if ch == b'\\' {
escaped = true;
} else if in_string != 0 && ch == in_string {
in_string = 0;
} else if in_template && ch == b'`' {
in_template = false;
}
i += 1;
continue;
}

if ch == b'\\' {
escaped = true;
if ch.is_ascii_whitespace() {
i += 1;
continue;
}

// Handle strings
if in_string == 0 && !in_template {
if ch == b'"' || ch == b'\'' {
in_string = ch;
continue;
if ch == b'/' {
match code.get(i + 1) {
Some(b'/') => {
while i < code.len() && code[i] != b'\n' {
i += 1;
}
continue;
}
Some(b'*') => {
in_block_comment = true;
i += 2;
continue;
}
_ => {}
Comment thread
robobun marked this conversation as resolved.
Resolved
}
if ch == b'`' {
in_template = true;
// A `/` after a token that cannot end an expression starts a regex.
let after_keyword = is_word_char(prev)
&& !word_after_dot
&& REGEX_KEYWORDS.contains(&&code[word_start..word_end]);
Comment thread
robobun marked this conversation as resolved.
Resolved
// A doubled `+`/`-` is postfix inc/dec, which ends an expression.
let postfix_incdec =
matches!(prev, b'+' | b'-') && prev_idx > 0 && code[prev_idx - 1] == prev;
// `}` ends a statement block at top level, but an object literal inside `()`/`[]`.
let after_block = prev == b'}' && paren_count <= 0 && bracket_count <= 0;
let starts_regex = prev == 0
|| after_keyword
|| after_block
|| (prev == b')' && last_paren_conditional)
|| (!postfix_incdec
&& matches!(
prev,
b'(' | b'['
| b'{'
| b','
| b';'
| b':'
| b'='
| b'!'
| b'&'
| b'|'
| b'?'
| b'+'
| b'-'
| b'*'
| b'/'
| b'%'
| b'<'
| b'>'
| b'^'
| b'~'
Comment thread
robobun marked this conversation as resolved.
Resolved
));

Check failure on line 2619 in src/runtime/cli/repl.rs

View check run for this annotation

Claude / Claude Code Review

starts_regex heuristic: four remaining misfires swallow closing delimiters (JSX </, TS x!, {} inside braces, obj.if())

Four more inputs that evaluated pre-PR now leave the REPL stuck at `...` because `starts_regex` fires and the regex-body scanner swallows the real closing `)`/`]`/`}`. The load-bearing one is **JSX closing tags** — the REPL uses `Loader::Tsx`, so `console.log(<div>hi</div>)` / `(<a></a>)` are supported input, but `prev == b'<'` matches the punctuation list and `</` enters regex mode, eating `div>)`. The other three are the same sibling-gate pattern as the ef1f3ae fixes: `f(total! / count)` (post
Comment thread
robobun marked this conversation as resolved.
Resolved
if starts_regex {
// Skip the regex body; `/` in a `[...]` class doesn't end it, a newline does.
i += 1;
let mut in_class = false;
while i < code.len() && code[i] != b'\n' {
match code[i] {
// A trailing escape must not consume the line break.
b'\\' if code.get(i + 1).is_some_and(|&next| next != b'\n') => i += 1,
b'[' => in_class = true,
b']' => in_class = false,
b'/' if !in_class => break,
_ => {}
}
i += 1;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// A regex ends an expression; `)` (non-conditional) encodes that.
prev = b')';
last_paren_conditional = false;
i += 1;
continue;
}
} else if in_string != 0 && ch == in_string {
in_string = 0;
continue;
} else if in_template && ch == b'`' {
in_template = false;
continue;
}

// Skip content inside strings
if in_string != 0 || in_template {
continue;
if is_word_char(ch) {
if word_end != i {
word_start = i;
word_after_dot = prev == b'.';
Comment thread
robobun marked this conversation as resolved.
Resolved
}
word_end = i + 1;
}

// Count brackets
match ch {
b'"' | b'\'' => in_string = ch,
b'`' => in_template = true,
b'{' => brace_count += 1,
b'}' => brace_count -= 1,
b'[' => bracket_count += 1,
b']' => bracket_count -= 1,
b'(' => paren_count += 1,
b')' => paren_count -= 1,
b'(' => {
paren_count += 1;
paren_stack.push(
is_word_char(prev)
&& matches!(
&code[word_start..word_end],
b"if" | b"for" | b"while" | b"with"
),
);
}
b')' => {
paren_count -= 1;
last_paren_conditional = paren_stack.pop().unwrap_or(false);
}
_ => {}
}
prev = ch;
prev_idx = i;
i += 1;
}

// Incomplete if any unclosed delimiters or unclosed strings
in_string != 0 || in_template || brace_count > 0 || bracket_count > 0 || paren_count > 0
// Incomplete if any unclosed delimiters, strings, or block comments
in_string != 0
|| in_template
|| in_block_comment
|| brace_count > 0
|| bracket_count > 0
|| paren_count > 0
}

use crate::api::js_transpiler::is_likely_object_literal;
Expand Down
112 changes: 112 additions & 0 deletions test/js/bun/repl/repl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,118 @@ describe.concurrent("Bun REPL", () => {
});
});

// https://github.com/oven-sh/bun/issues/37326
describe("regex literals and comments in continuation detection", () => {
test("regex containing a double quote evaluates on one line", async () => {
const { stdout, exitCode } = await runRepl(['x = /hello "world/;', "x.source", ".exit"]);
expect(stripAnsi(stdout)).toContain('hello "world');
expect(exitCode).toBe(0);
});

test("regex containing a backtick evaluates on one line", async () => {
const { stdout, exitCode } = await runRepl(["x = /hello `world/;", "x.source", ".exit"]);
expect(stripAnsi(stdout)).toContain("hello `world");
expect(exitCode).toBe(0);
});

test("slash inside a regex character class does not end the regex", async () => {
const { stdout, exitCode } = await runRepl(['/[/"]/.test("/")', ".exit"]);
expect(stripAnsi(stdout)).toContain("true");
expect(exitCode).toBe(0);
});
Comment thread
robobun marked this conversation as resolved.
Resolved

test("escaped slash inside a regex does not end the regex", async () => {
const { stdout, exitCode } = await runRepl(['/a\\/b/.test("a/b")', ".exit"]);
expect(stripAnsi(stdout)).toContain("true");
expect(exitCode).toBe(0);
});

test("unbalanced brace and paren inside a regex evaluate on one line", async () => {
const { stdout, exitCode } = await runRepl(["x = /a{b\\(/;", "x", ".exit"]);
expect(stripAnsi(stdout)).toContain("/a{b\\(/");
expect(exitCode).toBe(0);
});

test("backslash before a newline does not continue a regex onto the next line", async () => {
// `/a\` at end of line is an unterminated regex; the `2]` on the next
// line must still balance the `[`, not be swallowed as regex body.
const { stdout, stderr, exitCode } = await runRepl(["x = [1, /a\\", "2]", "40 + 2", ".exit"]);
expect(stripAnsi(stdout + stderr)).toContain("SyntaxError");
expect(stripAnsi(stdout)).toContain("42");
expect(exitCode).toBe(0);
});

test("invalid regex reports a syntax error instead of waiting for more input", async () => {
const { stdout, stderr, exitCode } = await runRepl(["x = /hello (world/;", "1 + 1", ".exit"]);
expect(stripAnsi(stdout + stderr)).toContain("SyntaxError");
// REPL recovered and evaluated the next line
expect(stripAnsi(stdout)).toContain("2");
expect(exitCode).toBe(0);
});

test("regex after the return keyword", async () => {
const { stdout, exitCode } = await runRepl(['function f() { return /ab"c/; }', "f()", ".exit"]);
expect(stripAnsi(stdout)).toContain('/ab"c/');
expect(exitCode).toBe(0);
});

test("division is not mistaken for a regex", async () => {
const { stdout, exitCode } = await runRepl(["x = 10 / 2 / 1; x", ".exit"]);
expect(stripAnsi(stdout)).toContain("5");
expect(exitCode).toBe(0);
});

test("division after postfix increment and decrement", async () => {
const inc = await runRepl(["i = 84; q = (i++ / 2)", "q", ".exit"]);
expect(stripAnsi(inc.stdout)).toContain("42");
expect(inc.exitCode).toBe(0);

const dec = await runRepl(["i = 86; q = (i-- / 2)", "q", ".exit"]);
expect(stripAnsi(dec.stdout)).toContain("43");
expect(dec.exitCode).toBe(0);
});
Comment thread
robobun marked this conversation as resolved.
Resolved

test("division after an object literal inside brackets", async () => {
const { stdout, exitCode } = await runRepl(["q = [{} / 1]", "q", ".exit"]);
expect(stripAnsi(stdout)).toContain("NaN");
expect(exitCode).toBe(0);
});

test("regex after a conditional's closing paren", async () => {
const { stdout, exitCode } = await runRepl(['if (true) x = /"/; x', ".exit"]);
const output = stripAnsi(stdout);
expect(output).toContain('/"/');
expect(output).not.toContain("SyntaxError");
expect(exitCode).toBe(0);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
});

test("regex as the right operand of a division", async () => {
const { stdout, exitCode } = await runRepl(['10 / /[(]/.test("(")', ".exit"]);
expect(stripAnsi(stdout)).toContain("10");
expect(exitCode).toBe(0);
});

test("a property named like a keyword does not start a regex", async () => {
// `x.in / {` must open a continuation, not be read as `x.in` followed
// by a regex starting at `/ {`.
const { stdout, exitCode } = await runRepl(["x = { in: 84 }; x.in / {", "valueOf: () => 2 }", ".exit"]);
expect(stripAnsi(stdout)).toContain("42");
expect(exitCode).toBe(0);
});

test("unbalanced bracket in a line comment is ignored", async () => {
const { stdout, exitCode } = await runRepl(["1 + 1 // unbalanced (", ".exit"]);
expect(stripAnsi(stdout)).toContain("2");
expect(exitCode).toBe(0);
});

test("unterminated block comment continues to the next line", async () => {
const { stdout, exitCode } = await runRepl(["40 + 2 /* comment", "*/", ".exit"]);
expect(stripAnsi(stdout)).toContain("42");
expect(exitCode).toBe(0);
});
});

describe("async evaluation", () => {
test("await expressions", async () => {
const { stdout, exitCode } = await runRepl(["await Promise.resolve(42)", ".exit"]);
Expand Down