|
6 | 6 | * to identify SQL_COMMENT tokens with exact byte positions. |
7 | 7 | * Whitespace detection uses token gaps to find blank lines |
8 | 8 | * between statements/comments. |
9 | | - * |
10 | | - * Note: @libpg-query/parser has an upstream JSON serialization bug in |
11 | | - * _wasm_scan where literal control characters in token text are not |
12 | | - * escaped. We work around this by retrying with a patched JSON.parse |
13 | | - * that escapes control characters before parsing. |
14 | 9 | */ |
15 | 10 |
|
16 | 11 | import { scanSync, type ScanToken } from '@libpg-query/parser'; |
17 | 12 |
|
18 | | -/** |
19 | | - * Escape unescaped control characters inside JSON string values. |
20 | | - * The upstream _wasm_scan emits raw \n, \r, \t in token text fields, |
21 | | - * which breaks JSON.parse. This replaces them with their escape sequences. |
22 | | - */ |
23 | | -function fixScanJson(raw: string): string { |
24 | | - return raw.replace( |
25 | | - /"(?:[^"\\]|\\.)*"/g, |
26 | | - (match) => |
27 | | - match |
28 | | - .replace(/\t/g, '\\t') |
29 | | - .replace(/\n/g, '\\n') |
30 | | - .replace(/\r/g, '\\r') |
31 | | - ); |
32 | | -} |
33 | | - |
34 | | -/** |
35 | | - * Call scanSync with a workaround for the upstream JSON serialization bug. |
36 | | - * First tries the normal path; if JSON.parse throws, retries with a |
37 | | - * temporarily patched JSON.parse that escapes control characters. |
38 | | - * This is synchronous so there are no concurrency concerns. |
39 | | - */ |
40 | | -function safeScanSync(sql: string): { tokens: ScanToken[] } { |
41 | | - try { |
42 | | - return scanSync(sql); |
43 | | - } catch { |
44 | | - // Retry with patched JSON.parse to handle unescaped control chars |
45 | | - const origParse = JSON.parse; |
46 | | - try { |
47 | | - JSON.parse = ((text: string, reviver?: Parameters<typeof JSON.parse>[1]) => |
48 | | - origParse(fixScanJson(text), reviver)) as typeof JSON.parse; |
49 | | - return scanSync(sql); |
50 | | - } finally { |
51 | | - JSON.parse = origParse; |
52 | | - } |
53 | | - } |
54 | | -} |
55 | | - |
56 | 13 | /** Token type for -- line comments from PostgreSQL's lexer */ |
57 | 14 | const SQL_COMMENT = 275; |
58 | 15 |
|
@@ -105,7 +62,7 @@ export function scanComments(sql: string): ScannedElement[] { |
105 | 62 |
|
106 | 63 | let tokens: ScanToken[]; |
107 | 64 | try { |
108 | | - const scanResult = safeScanSync(sql); |
| 65 | + const scanResult = scanSync(sql); |
109 | 66 | tokens = scanResult.tokens; |
110 | 67 | } catch { |
111 | 68 | return []; |
|
0 commit comments