@@ -227,47 +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.
232- // Without a fix, scanSync throws: "Bad control character in string literal"
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.
233233 const sql = `CREATE FUNCTION test() RETURNS void AS $$
234234BEGIN
235235 RAISE NOTICE 'hello';
236236END;
237237$$ LANGUAGE plpgsql` ;
238-
238+
239239 const result = query . scanSync ( sql ) ;
240240 assert . equal ( typeof result , "object" ) ;
241241 assert . ok ( Array . isArray ( result . tokens ) ) ;
242242 assert . ok ( result . tokens . length > 0 ) ;
243-
244- // Find the dollar-quoted string token
243+
244+ // The dollar-quoted body spans multiple lines
245245 const dollarToken = result . tokens . find ( t => t . text . includes ( 'BEGIN' ) ) ;
246246 assert . ok ( dollarToken , "should have a token containing the function body" ) ;
247- assert . ok ( dollarToken . text . includes ( '\n' ) , "token text should contain newlines" ) ;
247+ assert . ok ( dollarToken . text . includes ( '\n' ) , "token text should preserve newlines" ) ;
248248 } ) ;
249249
250- it ( "should handle multi-line tokens with tabs" , ( ) => {
251- const sql = "SELECT $$line1\n\tindented\nline3$$" ;
252-
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+
253256 const result = query . scanSync ( sql ) ;
254257 assert . equal ( typeof result , "object" ) ;
255258 assert . ok ( Array . isArray ( result . tokens ) ) ;
256-
259+
257260 const dollarToken = result . tokens . find ( t => t . text . includes ( 'indented' ) ) ;
258261 assert . ok ( dollarToken , "should have a token containing the tabbed content" ) ;
259262 } ) ;
260263
261- it ( "should handle multi-line SQL comments" , ( ) => {
262- const sql = "SELECT 1; /* multi\nline\ncomment */ SELECT 2" ;
263-
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+
264270 const result = query . scanSync ( sql ) ;
265271 assert . equal ( typeof result , "object" ) ;
266272 assert . ok ( Array . isArray ( result . tokens ) ) ;
267-
273+
268274 const commentToken = result . tokens . find ( t => t . tokenName === "C_COMMENT" ) ;
269275 assert . ok ( commentToken , "should have a C_COMMENT token" ) ;
270- assert . ok ( commentToken . text . includes ( '\n' ) , "comment text should contain newlines" ) ;
276+ assert . ok ( commentToken . text . includes ( '\n' ) , "comment text should preserve newlines" ) ;
271277 } ) ;
272278 } ) ;
273279} ) ;
0 commit comments