Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support generating function comments #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/authors/mysql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SELECT * FROM authors
ORDER BY name;

/* name: CreateAuthor :exec */
/* Create a new author. */
INSERT INTO authors (
name, bio
) VALUES (
Expand Down
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
1 change: 1 addition & 0 deletions examples/authors/sqlite/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SELECT * FROM authors
ORDER BY name;

-- name: CreateAuthor :exec
-- Create a new author.
INSERT INTO authors (
name, bio
) VALUES (
Expand Down
3 changes: 3 additions & 0 deletions examples/bun-mysql2/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export interface CreateAuthorArgs {
bio: string | null;
}

/**
* Create a new author.
*/
export async function createAuthor(client: Client, args: CreateAuthorArgs): Promise<void> {
await client.query({
sql: createAuthorQuery,
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
3 changes: 3 additions & 0 deletions examples/node-better-sqlite3/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export interface CreateAuthorArgs {
bio: any | null;
}

/**
* Create a new author.
*/
export async function createAuthor(database: Database, args: CreateAuthorArgs): Promise<void> {
const stmt = database.prepare(createAuthorQuery);
await stmt.run(args.name, args.bio);
Expand Down
3 changes: 3 additions & 0 deletions examples/node-mysql2/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export interface CreateAuthorArgs {
bio: string | null;
}

/**
* Create a new author.
*/
export async function createAuthor(client: Client, args: CreateAuthorArgs): Promise<void> {
await client.query({
sql: createAuthorQuery,
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