Fix the escape hatch counter for a TypeScript migration
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-ignoreand@ts-nocheck. Match them exactly: case matters, and: anyhas 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-ignoredand@ts-ignore_alldo not count. - Only the character after a match is checked, never the one before, so
has anycounts. - 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]) -> intExample 1
Input
count_escape_hatch_lines(["function f(x: any) {", " return x;", "}"])Output
1Example 2
Input
count_escape_hatch_lines(["const v = raw as any;", "let s: anything = init();"])Output
1Example 3
Input
count_escape_hatch_lines(["const a: any = b as any;", "// @ts-ignore", "const n: number = 1;"])Output
2