Fix the escape hatch counter for a TypeScript migration

Review the AI's codeEasy12 minMicrosoft style

Your task

Your team let an AI agent convert a JavaScript codebase to TypeScript, and tsc now passes with zero errors. A clean compile is easy to buy with escape hatches, so you want to count the lines that use one.

An assistant wrote count_escape_hatch_lines(lines). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • lines: the lines of the converted source, as strings.
  • Return the number of lines that hold at least one escape hatch.

Rules the tests check:

  • The markers are : any, as any, @ts-ignore and @ts-nocheck. Match them exactly: case matters, and : any has exactly one space.
  • A match counts only when the character right after it is not an ASCII letter, a digit, _ or $. The end of the line also ends a match. So : anything, as anyOther, @ts-ignored and @ts-ignore_all do not count.
  • Only the character after a match is checked, never the one before, so has any counts.
  • A line counts once, however many markers it holds.
  • Nothing is parsed: a marker inside a string or a comment still counts.

Examples

count_escape_hatch_lines(lines: list[str]) -> int
Example 1
Inputcount_escape_hatch_lines(["function f(x: any) {", " return x;", "}"])
Output1
Example 2
Inputcount_escape_hatch_lines(["const v = raw as any;", "let s: anything = init();"])
Output1
Example 3
Inputcount_escape_hatch_lines(["const a: any = b as any;", "// @ts-ignore", "const n: number = 1;"])
Output2