Skip to content

Commit

Permalink
feat: Support generating function comments
Browse files Browse the repository at this point in the history
  • Loading branch information
uki00a committed Aug 12, 2024
1 parent 54496e9 commit 78bca9d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 17 deletions.
3 changes: 3 additions & 0 deletions examples/authors/postgresql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
5 changes: 5 additions & 0 deletions examples/bun-pg/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateAuthorRow | null> {
const result = await client.query({
text: createAuthorQuery,
Expand Down
5 changes: 5 additions & 0 deletions examples/bun-postgres/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateAuthorRow | null> {
const rows = await sql.unsafe(createAuthorQuery, [args.name, args.bio]).values();
if (rows.length !== 1) {
Expand Down
5 changes: 5 additions & 0 deletions examples/node-pg/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateAuthorRow | null> {
const result = await client.query({
text: createAuthorQuery,
Expand Down
5 changes: 5 additions & 0 deletions examples/node-postgres/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateAuthorRow | null> {
const rows = await sql.unsafe(createAuthorQuery, [args.name, args.bio]).values();
if (rows.length !== 1) {
Expand Down
71 changes: 54 additions & 17 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// @ts-expect-error
import { readFileSync, writeFileSync, STDIO } from "javy/fs";
import {
addSyntheticLeadingComment,
EmitHint,
FunctionDeclaration,
NewLineKind,
TypeNode,
ScriptKind,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 78bca9d

Please sign in to comment.