(source: string)
| 218 | |
| 219 | // fallow-ignore-next-line complexity |
| 220 | export function stripJsComments(source: string): string { |
| 221 | let out = ""; |
| 222 | let i = 0; |
| 223 | let quote: "'" | '"' | "`" | null = null; |
| 224 | let escaped = false; |
| 225 | |
| 226 | while (i < source.length) { |
| 227 | const ch = source[i] ?? ""; |
| 228 | const next = source[i + 1] ?? ""; |
| 229 | |
| 230 | if (quote) { |
| 231 | out += ch; |
| 232 | if (escaped) { |
| 233 | escaped = false; |
| 234 | } else if (ch === "\\") { |
| 235 | escaped = true; |
| 236 | } else if (ch === quote) { |
| 237 | quote = null; |
| 238 | } |
| 239 | i += 1; |
| 240 | continue; |
| 241 | } |
| 242 | |
| 243 | if (ch === "'" || ch === '"' || ch === "`") { |
| 244 | quote = ch; |
| 245 | out += ch; |
| 246 | i += 1; |
| 247 | continue; |
| 248 | } |
| 249 | |
| 250 | if (ch === "/" && next === "/") { |
| 251 | out += " "; |
| 252 | i += 2; |
| 253 | while (i < source.length && source[i] !== "\n" && source[i] !== "\r") { |
| 254 | out += " "; |
| 255 | i += 1; |
| 256 | } |
| 257 | continue; |
| 258 | } |
| 259 | |
| 260 | if (ch === "/" && next === "*") { |
| 261 | out += " "; |
| 262 | i += 2; |
| 263 | while (i < source.length) { |
| 264 | const blockCh = source[i] ?? ""; |
| 265 | const blockNext = source[i + 1] ?? ""; |
| 266 | if (blockCh === "*" && blockNext === "/") { |
| 267 | out += " "; |
| 268 | i += 2; |
| 269 | break; |
| 270 | } |
| 271 | out += blockCh === "\n" || blockCh === "\r" ? blockCh : " "; |
| 272 | i += 1; |
| 273 | } |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | out += ch; |
no outgoing calls
no test coverage detected