diff --git a/examples/authors/postgresql/query.sql b/examples/authors/postgresql/query.sql index 971b8f9..f9572d4 100644 --- a/examples/authors/postgresql/query.sql +++ b/examples/authors/postgresql/query.sql @@ -7,6 +7,9 @@ SELECT * FROM authors ORDER BY name; -- name: CreateAuthor :one +-- Create a new author. +-- This is the second line.*/ +--/This is the third line. INSERT INTO authors ( name, bio ) VALUES ( diff --git a/examples/bun-pg/src/db/query_sql.ts b/examples/bun-pg/src/db/query_sql.ts index 2e8db52..1a78b3b 100644 --- a/examples/bun-pg/src/db/query_sql.ts +++ b/examples/bun-pg/src/db/query_sql.ts @@ -81,6 +81,11 @@ export interface CreateAuthorRow { bio: string | null; } +/** + * Create a new author. + * This is the second line.*\/ + *\/This is the third line. + */ export async function createAuthor(client: Client, args: CreateAuthorArgs): Promise { const result = await client.query({ text: createAuthorQuery, diff --git a/examples/bun-postgres/src/db/query_sql.ts b/examples/bun-postgres/src/db/query_sql.ts index 8e15f3b..a13edd5 100644 --- a/examples/bun-postgres/src/db/query_sql.ts +++ b/examples/bun-postgres/src/db/query_sql.ts @@ -69,6 +69,11 @@ export interface CreateAuthorRow { bio: string | null; } +/** + * Create a new author. + * This is the second line.*\/ + *\/This is the third line. + */ export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise { const rows = await sql.unsafe(createAuthorQuery, [args.name, args.bio]).values(); if (rows.length !== 1) { diff --git a/examples/node-pg/src/db/query_sql.ts b/examples/node-pg/src/db/query_sql.ts index 2e8db52..1a78b3b 100644 --- a/examples/node-pg/src/db/query_sql.ts +++ b/examples/node-pg/src/db/query_sql.ts @@ -81,6 +81,11 @@ export interface CreateAuthorRow { bio: string | null; } +/** + * Create a new author. + * This is the second line.*\/ + *\/This is the third line. + */ export async function createAuthor(client: Client, args: CreateAuthorArgs): Promise { const result = await client.query({ text: createAuthorQuery, diff --git a/examples/node-postgres/src/db/query_sql.ts b/examples/node-postgres/src/db/query_sql.ts index 8e15f3b..a13edd5 100644 --- a/examples/node-postgres/src/db/query_sql.ts +++ b/examples/node-postgres/src/db/query_sql.ts @@ -69,6 +69,11 @@ export interface CreateAuthorRow { bio: string | null; } +/** + * Create a new author. + * This is the second line.*\/ + *\/This is the third line. + */ export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise { const rows = await sql.unsafe(createAuthorQuery, [args.name, args.bio]).values(); if (rows.length !== 1) { diff --git a/src/app.ts b/src/app.ts index e3a077c..44a0bf5 100644 --- a/src/app.ts +++ b/src/app.ts @@ -4,8 +4,8 @@ // @ts-expect-error import { readFileSync, writeFileSync, STDIO } from "javy/fs"; import { + addSyntheticLeadingComment, EmitHint, - FunctionDeclaration, NewLineKind, TypeNode, ScriptKind, @@ -161,38 +161,50 @@ ${query.text}` switch (query.cmd) { case ":exec": { nodes.push( - driver.execDecl(lowerName, textName, argIface, query.params) + withComment( + driver.execDecl(lowerName, textName, argIface, query.params), + query.comments + ) ); break; } case ":execlastid": { nodes.push( - driver.execlastidDecl(lowerName, textName, argIface, query.params) + withComment( + driver.execlastidDecl(lowerName, textName, argIface, query.params), + query.comments + ) ); break; } case ":one": { nodes.push( - driver.oneDecl( - lowerName, - textName, - argIface, - returnIface ?? "void", - query.params, - query.columns + withComment( + driver.oneDecl( + lowerName, + textName, + argIface, + returnIface ?? "void", + query.params, + query.columns + ), + query.comments ) ); break; } case ":many": { nodes.push( - driver.manyDecl( - lowerName, - textName, - argIface, - returnIface ?? "void", - query.params, - query.columns + withComment( + driver.manyDecl( + lowerName, + textName, + argIface, + returnIface ?? "void", + query.params, + query.columns + ), + query.comments, ) ); break; @@ -279,6 +291,31 @@ function rowDecl( ); } +function withComment(node: Node, comments: string[]): Node { + if (comments.length === 0) return node; + const multilineCommentTerminator = /\*\//g; + addSyntheticLeadingComment( + node, + SyntaxKind.MultiLineCommentTrivia, + ( + "*\n" + + comments.map((line) => { + if (line.startsWith("/")) { + // Escape leading `*/` + line = `\\${line}`; + } + // Escape `*/` in the middle + line = line.replace(multilineCommentTerminator, "*\\/"); + + return ` *${line}`; + }).join("\n") + + "\n " + ), + true, + ); + return node; +} + function printNode(nodes: Node[]): string { // https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API#creating-and-printing-a-typescript-ast const resultFile = createSourceFile(