Skip to content

fix: recognize regex literals before a ternary colon - #5552

Open
Mangesh Raut (mangeshraut712) wants to merge 1 commit into
microsoft:mainfrom
mangeshraut712:cursor/monaco-ts-regex-ternary-dffc
Open

Mangesh Raut (mangeshraut712) wants to merge 1 commit into
microsoft:mainfrom
mangeshraut712:cursor/monaco-ts-regex-ternary-dffc

Conversation

@mangeshraut712

Copy link
Copy Markdown

Summary

Fixes #2512.

The TypeScript Monarch grammar only treated /.../flags as a regular expression when the closing flags were followed by ., ;, ,, ), ], }, or end of line. In a conditional such as:

const x = true ? /^([^"]*)"/ : /^([^']*)'/

the first regex is followed by :, so the slash was tokenized as division. The " inside the pattern then opened a string, which broke bracket matching for the rest of the line. JavaScript reuses this tokenizer, so the same rule applies there.

: is now an accepted terminator. Division that is not a terminated regex is unchanged, including c ? a / b / d : e and n / 2 : x.

The existing heuristic still treats a slash pair as a regex when a flag character sits directly against the closing slash before a terminator (the same rule that already accepts /foo/g;). a/b/d : e falls into that case; spaced division does not.

Tests

npm run test:grammars — 87 passed, including new TypeScript and JavaScript cases for:

  • the reported ternary, with both quotes kept inside regexp tokens (without the change the first quote is a string token)
  • regexes on both sides of :, including a g flag
  • division followed by : that must stay division

CLA

I agree to the Microsoft Contributor License Agreement at https://cla.opensource.microsoft.com/.

@mangeshraut712

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@mangeshraut712

Copy link
Copy Markdown
Author

CI and the CLA check are passing. When you have time, could collaborators review this PR? The PR check currently indicates that three collaborator approvals are required. Thank you!

The TypeScript Monarch grammar only started a regex when the closing
flags were followed by `.`, `;`, `,`, `)`, `]`, `}`, or end of line.
In `cond ? /re/ : alt` the first regex is followed by `:`, so it was
tokenized as division and a quote inside it opened a string. JavaScript
reuses this tokenizer.

Fixes microsoft#2512
@mangeshraut712
Mangesh Raut (mangeshraut712) force-pushed the cursor/monaco-ts-regex-ternary-dffc branch from 392aede to 9c72980 Compare November 29, 2026 16:30
@mangeshraut712

Copy link
Copy Markdown
Author

Hi Henning Dieterichs (@hediet) — would you mind taking a look at this one when you have a moment?

The bug. In the JS/TS Monarch definition, the "unambiguous regex" rule only opens a regex when the literal is followed by something that can legitimately come after one: . ; , ) ] } or end of line. A regex in the first branch of a ternary is followed by :, which is not in that list, so the rule declines and the / falls through to the division operator.

The highlighting is the minor part. Once the first / is misread, a quote inside the regex is treated as opening a string, and everything after it on the line is tokenized as string contents. In this line the " in the first regex opens a string, so the second regex and the rest of the line are colored as string text:

const x = true ? /^([^"]*)"/ : /^([^\']*)\'/;

The fix. One character: : is added to that lookahead's follow set in typescript.ts, plus a comment. No other token sequence changes:

- /\/(?=([^\\\/]|\\.)+\/([dgimsuy]*)(\s*)(\.|;|,|\)|\]|\}|$))/,
+ /\/(?=([^\\\/]|\\.)+\/([dgimsuy]*)(\s*)(\.|;|,|\)|\]|\}|:|$))/,

Tests. The same test block is added to typescript.test.ts and javascript.test.ts (111 lines each), asserting the exact token stream.

  • Regex in the first ternary branch: true ? /a/ : /b/, true ? /foo/g : bar, and the quoted-pattern line above.

  • Division is unchanged: c ? a / b / d : e and n / 2 : x still tokenize as division, because a division is not a terminated regex literal.

  • Existing follow-set behavior is unchanged: { key: 123 }, [1,2,3], foo(123);, {a:{b:[]}}, x = "[{()}]".

  • npm run test:grammars: 88 passed, 0 failed

  • prettier --check on the three touched files: clean

  • Rebased onto current main as a single commit; the patch is byte-identical to the one CI and the CLA check already passed

The VS Code PR check shows Awaiting approvals (0/3), so this is waiting only on review. Happy to adjust the approach or test layout if you would prefer it done differently. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TS regex missed by Monarch grammar

1 participant