type Error = unknown let logError = console.error // for testing (spy/mock) export function setErrorLogger( logger: (message: string, data?: any) => void, ): void { logError = logger } export function disableErrorLogging(): void { logError = () => { /* do not output to console */ } } export function enableErrorLogging(): void { logError = console.error } export function stripComments(text: string): string { const buffer: string[] = [] let in_string = false let string_char = '' let string_escaped = false let in_inline_comment = false let in_block_comment = false let saw_star = false let in_html_comment = false let saw_hyphen = 0 for (const char of text) { // handle string content if (in_string) { // handle escaped sequence payload if (string_escaped) { string_escaped = false buffer.push(char) continue } // handle start of escape sequence if (char === '\\') { string_escaped = true buffer.push(char) continue } // handle end of string if (char === string_char) { in_string = false buffer.push(char) continue } // otherwise take the content of string buffer.push(char) continue } // handle inline comment content if (in_inline_comment) { // handle end of inline comment if (char === '\n') { in_inline_comment = false continue } // otherwise ignore the content of comment continue } const buffer_length = buffer.length const last_char = buffer_length === 0 ? '' : buffer[buffer_length - 1] // handle block comment content if (in_block_comment) { // handle end of block comment if (char === '*') { saw_star = true continue } if (saw_star && char === '/') { in_block_comment = false continue } // otherwise ignore the content of comment saw_star = false continue } // handle html comment content if (in_html_comment) { // handle end of html comment if (char === '-') { saw_hyphen++ continue } if (saw_hyphen >= 2 && char === '>') { in_html_comment = false continue } // otherwise ignore the content of comment saw_hyphen = 0 continue } // handle start of inline comment if (last_char === '/' && char === '/') { buffer.pop() in_inline_comment = true continue } // handle start of block comment if (last_char === '/' && char === '*') { buffer.pop() in_block_comment = true saw_star = false continue } // handle start of string if (char === '"' || char === "'" || char === '`') { in_string = true string_char = char string_escaped = false buffer.push(char) continue } // handle start of html comment "