Repository navigation
fix: recognize regex literals before a ternary colon - #5552
Mangesh Raut (mangeshraut712) wants to merge 1 commit into
Conversation
|
@microsoft-github-policy-service agree |
|
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
392aede to
9c72980
Compare
|
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: The highlighting is the minor part. Once the first The fix. One character: Tests. The same test block is added to
The VS Code PR check shows |
Summary
Fixes #2512.
The TypeScript Monarch grammar only treated
/.../flagsas a regular expression when the closing flags were followed by.,;,,,),],}, or end of line. In a conditional such as: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, includingc ? a / b / d : eandn / 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 : efalls into that case; spaced division does not.Tests
npm run test:grammars— 87 passed, including new TypeScript and JavaScript cases for:regexptokens (without the change the first quote is astringtoken):, including agflag:that must stay divisionCLA
I agree to the Microsoft Contributor License Agreement at https://cla.opensource.microsoft.com/.