Deviopath › TypeScript

Code Bracket Validator

Hard · Parsing

Write checkBrackets(code) that checks (), [] and {} in a piece of code, scanning left to right.

- Ignore everything inside string literals that start with " or ' and end at the next matching quote. Inside a string, a backslash \ escapes the next character.

- Ignore line comments: from // to the end of the line.

Return { valid: true }, or the FIRST problem as { valid: false, index, reason }:

- "unexpected": a closing bracket with nothing open (index of that bracket).

- "mismatch": a closing bracket of the wrong type (index of that closing bracket).

- "unclosed": the code ends inside a string (index of its opening quote), or with open brackets (index of the most recently opened one).

Next: Run an Async Task Graph