Repository navigation
Commit 23ea19c
authored
fix(openapi-inference): strip tsconfig comments and trailing commas in linear time (#2502)
## What
`stripJsonComments` in `packages/openapi-inference/src/project.ts`
removed trailing commas with the lookahead
`,(?=\s*(?:\/\/[^\n]*\s*|\/\*[\s\S]*?\*\/\s*)*[\]}])`. When the next
real token after a comma isn't `]` or `}`, the lookahead fails and V8
tries every way to split the following run of comments between the
`(...)*` iterations, which takes exponential time.
`readReferencePaths` runs this on a solution tsconfig (the documented
`"files": []` with `references` setup) and on every tsconfig it
references. So a referenced tsconfig with a `////` banner after
`"strict": true,` hangs project loading: 40 slashes took 1.3 s, 46 took
23.8 s, and an 80-column banner never finishes. Back-to-back block
comments do the same.
## Fix
Replace the regex with a scanner that reads the text once from start to
end:
- strings are copied as they are, escapes included
- `//` and `/* */` comments are dropped
- it remembers the last comma it saw and drops it when the next token,
past whitespace and comments, is `]` or `}`; anything else in between
keeps it
The work now grows with the file's length, whatever the input.
## Tests
In `project.test.ts`:
- A solution tsconfig with 40 back-to-back block comments, referencing
an app tsconfig with an 80-column `/` banner and 40 lines of block
comments. With the old regex this hangs (killed by a 60 s timeout).
- A reference path `"./*/../a,b//c"` (contains `/*`, a comma and `//`)
followed by trailing commas before line and block comments, to check
that strings are kept and trailing commas are still dropped.
For reviewers: without the fix the banner test hangs rather than
failing, since Vitest can't time out a test that blocks synchronously.
`pnpm check` passes. `pnpm exec vitest run` passes except
`openapi-generator.test.ts` > "names the nearest tsconfig when it does
not include the file, and why", which also fails on `main`.1 parent 0bb8c10 commit 23ea19c
2 files changed
Lines changed: 92 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
108 | 160 | | |
109 | 161 | | |
110 | 162 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
306 | 306 | | |
307 | 307 | | |
308 | 308 | | |
| 309 | + | |
| 310 | + | |
309 | 311 | | |
310 | 312 | | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
315 | 351 | | |
0 commit comments