@@ -227,46 +227,53 @@ describe("Query Scanning", () => {
227227 } ) ;
228228
229229 it ( "should handle multi-line dollar-quoted strings without JSON errors" , ( ) => {
230- // This tests that the JSON serialization properly escapes control
231- // characters (newlines, tabs) inside token text fields.
230+ // Without the fix, scanSync throws:
231+ // "Bad control character in string literal"
232+ // because build_scan_json() doesn't escape \n in the token text.
232233 const sql = `CREATE FUNCTION test() RETURNS void AS $$
233234BEGIN
234235 RAISE NOTICE 'hello';
235236END;
236237$$ LANGUAGE plpgsql` ;
237-
238+
238239 const result = query . scanSync ( sql ) ;
239240 assert . equal ( typeof result , "object" ) ;
240241 assert . ok ( Array . isArray ( result . tokens ) ) ;
241242 assert . ok ( result . tokens . length > 0 ) ;
242-
243- // Find the dollar-quoted string token
243+
244+ // The dollar-quoted body spans multiple lines
244245 const dollarToken = result . tokens . find ( t => t . text . includes ( 'BEGIN' ) ) ;
245246 assert . ok ( dollarToken , "should have a token containing the function body" ) ;
246- assert . ok ( dollarToken . text . includes ( '\n' ) , "token text should contain newlines" ) ;
247+ assert . ok ( dollarToken . text . includes ( '\n' ) , "token text should preserve newlines" ) ;
247248 } ) ;
248249
249- it ( "should handle multi-line tokens with tabs" , ( ) => {
250- const sql = "SELECT $$line1\n\tindented\nline3$$" ;
251-
250+ it ( "should handle dollar-quoted tokens with tabs" , ( ) => {
251+ // Tab characters also break JSON.parse when unescaped.
252+ const sql = `SELECT $$line1
253+ indented
254+ line3$$` ;
255+
252256 const result = query . scanSync ( sql ) ;
253257 assert . equal ( typeof result , "object" ) ;
254258 assert . ok ( Array . isArray ( result . tokens ) ) ;
255-
259+
256260 const dollarToken = result . tokens . find ( t => t . text . includes ( 'indented' ) ) ;
257261 assert . ok ( dollarToken , "should have a token containing the tabbed content" ) ;
258262 } ) ;
259263
260- it ( "should handle multi-line SQL comments" , ( ) => {
261- const sql = "SELECT 1; /* multi\nline\ncomment */ SELECT 2" ;
262-
264+ it ( "should handle multi-line block comments" , ( ) => {
265+ // C-style block comments spanning multiple lines hit the same bug.
266+ const sql = `SELECT 1; /* multi
267+ line
268+ comment */ SELECT 2` ;
269+
263270 const result = query . scanSync ( sql ) ;
264271 assert . equal ( typeof result , "object" ) ;
265272 assert . ok ( Array . isArray ( result . tokens ) ) ;
266-
273+
267274 const commentToken = result . tokens . find ( t => t . tokenName === "C_COMMENT" ) ;
268275 assert . ok ( commentToken , "should have a C_COMMENT token" ) ;
269- assert . ok ( commentToken . text . includes ( '\n' ) , "comment text should contain newlines" ) ;
276+ assert . ok ( commentToken . text . includes ( '\n' ) , "comment text should preserve newlines" ) ;
270277 } ) ;
271278 } ) ;
272279} ) ;
0 commit comments