Skip to content

Commit 78bca9d

Browse files
committed
feat: Support generating function comments
1 parent 54496e9 commit 78bca9d

File tree

6 files changed

+77
-17
lines changed

6 files changed

+77
-17
lines changed

Diff for: examples/authors/postgresql/query.sql

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ SELECT * FROM authors
77
ORDER BY name;
88

99
-- name: CreateAuthor :one
10+
-- Create a new author.
11+
-- This is the second line.*/
12+
--/This is the third line.
1013
INSERT INTO authors (
1114
name, bio
1215
) VALUES (

Diff for: examples/bun-pg/src/db/query_sql.ts

+5
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ export interface CreateAuthorRow {
8181
bio: string | null;
8282
}
8383

84+
/**
85+
* Create a new author.
86+
* This is the second line.*\/
87+
*\/This is the third line.
88+
*/
8489
export async function createAuthor(client: Client, args: CreateAuthorArgs): Promise<CreateAuthorRow | null> {
8590
const result = await client.query({
8691
text: createAuthorQuery,

Diff for: examples/bun-postgres/src/db/query_sql.ts

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ export interface CreateAuthorRow {
6969
bio: string | null;
7070
}
7171

72+
/**
73+
* Create a new author.
74+
* This is the second line.*\/
75+
*\/This is the third line.
76+
*/
7277
export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise<CreateAuthorRow | null> {
7378
const rows = await sql.unsafe(createAuthorQuery, [args.name, args.bio]).values();
7479
if (rows.length !== 1) {

Diff for: examples/node-pg/src/db/query_sql.ts

+5
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ export interface CreateAuthorRow {
8181
bio: string | null;
8282
}
8383

84+
/**
85+
* Create a new author.
86+
* This is the second line.*\/
87+
*\/This is the third line.
88+
*/
8489
export async function createAuthor(client: Client, args: CreateAuthorArgs): Promise<CreateAuthorRow | null> {
8590
const result = await client.query({
8691
text: createAuthorQuery,

Diff for: examples/node-postgres/src/db/query_sql.ts

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ export interface CreateAuthorRow {
6969
bio: string | null;
7070
}
7171

72+
/**
73+
* Create a new author.
74+
* This is the second line.*\/
75+
*\/This is the third line.
76+
*/
7277
export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise<CreateAuthorRow | null> {
7378
const rows = await sql.unsafe(createAuthorQuery, [args.name, args.bio]).values();
7479
if (rows.length !== 1) {

Diff for: src/app.ts

+54-17
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// @ts-expect-error
55
import { readFileSync, writeFileSync, STDIO } from "javy/fs";
66
import {
7+
addSyntheticLeadingComment,
78
EmitHint,
8-
FunctionDeclaration,
99
NewLineKind,
1010
TypeNode,
1111
ScriptKind,
@@ -161,38 +161,50 @@ ${query.text}`
161161
switch (query.cmd) {
162162
case ":exec": {
163163
nodes.push(
164-
driver.execDecl(lowerName, textName, argIface, query.params)
164+
withComment(
165+
driver.execDecl(lowerName, textName, argIface, query.params),
166+
query.comments
167+
)
165168
);
166169
break;
167170
}
168171
case ":execlastid": {
169172
nodes.push(
170-
driver.execlastidDecl(lowerName, textName, argIface, query.params)
173+
withComment(
174+
driver.execlastidDecl(lowerName, textName, argIface, query.params),
175+
query.comments
176+
)
171177
);
172178
break;
173179
}
174180
case ":one": {
175181
nodes.push(
176-
driver.oneDecl(
177-
lowerName,
178-
textName,
179-
argIface,
180-
returnIface ?? "void",
181-
query.params,
182-
query.columns
182+
withComment(
183+
driver.oneDecl(
184+
lowerName,
185+
textName,
186+
argIface,
187+
returnIface ?? "void",
188+
query.params,
189+
query.columns
190+
),
191+
query.comments
183192
)
184193
);
185194
break;
186195
}
187196
case ":many": {
188197
nodes.push(
189-
driver.manyDecl(
190-
lowerName,
191-
textName,
192-
argIface,
193-
returnIface ?? "void",
194-
query.params,
195-
query.columns
198+
withComment(
199+
driver.manyDecl(
200+
lowerName,
201+
textName,
202+
argIface,
203+
returnIface ?? "void",
204+
query.params,
205+
query.columns
206+
),
207+
query.comments,
196208
)
197209
);
198210
break;
@@ -279,6 +291,31 @@ function rowDecl(
279291
);
280292
}
281293

294+
function withComment(node: Node, comments: string[]): Node {
295+
if (comments.length === 0) return node;
296+
const multilineCommentTerminator = /\*\//g;
297+
addSyntheticLeadingComment(
298+
node,
299+
SyntaxKind.MultiLineCommentTrivia,
300+
(
301+
"*\n" +
302+
comments.map((line) => {
303+
if (line.startsWith("/")) {
304+
// Escape leading `*/`
305+
line = `\\${line}`;
306+
}
307+
// Escape `*/` in the middle
308+
line = line.replace(multilineCommentTerminator, "*\\/");
309+
310+
return ` *${line}`;
311+
}).join("\n") +
312+
"\n "
313+
),
314+
true,
315+
);
316+
return node;
317+
}
318+
282319
function printNode(nodes: Node[]): string {
283320
// https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API#creating-and-printing-a-typescript-ast
284321
const resultFile = createSourceFile(

0 commit comments

Comments
 (0)