|
4 | 4 | // @ts-expect-error |
5 | 5 | import { readFileSync, writeFileSync, STDIO } from "javy/fs"; |
6 | 6 | import { |
| 7 | + addSyntheticLeadingComment, |
7 | 8 | EmitHint, |
8 | | - FunctionDeclaration, |
9 | 9 | NewLineKind, |
10 | 10 | TypeNode, |
11 | 11 | ScriptKind, |
@@ -161,38 +161,50 @@ ${query.text}` |
161 | 161 | switch (query.cmd) { |
162 | 162 | case ":exec": { |
163 | 163 | nodes.push( |
164 | | - driver.execDecl(lowerName, textName, argIface, query.params) |
| 164 | + withComment( |
| 165 | + driver.execDecl(lowerName, textName, argIface, query.params), |
| 166 | + query.comments |
| 167 | + ) |
165 | 168 | ); |
166 | 169 | break; |
167 | 170 | } |
168 | 171 | case ":execlastid": { |
169 | 172 | nodes.push( |
170 | | - driver.execlastidDecl(lowerName, textName, argIface, query.params) |
| 173 | + withComment( |
| 174 | + driver.execlastidDecl(lowerName, textName, argIface, query.params), |
| 175 | + query.comments |
| 176 | + ) |
171 | 177 | ); |
172 | 178 | break; |
173 | 179 | } |
174 | 180 | case ":one": { |
175 | 181 | 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 |
183 | 192 | ) |
184 | 193 | ); |
185 | 194 | break; |
186 | 195 | } |
187 | 196 | case ":many": { |
188 | 197 | 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, |
196 | 208 | ) |
197 | 209 | ); |
198 | 210 | break; |
@@ -279,6 +291,31 @@ function rowDecl( |
279 | 291 | ); |
280 | 292 | } |
281 | 293 |
|
| 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 | + |
282 | 319 | function printNode(nodes: Node[]): string { |
283 | 320 | // https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API#creating-and-printing-a-typescript-ast |
284 | 321 | const resultFile = createSourceFile( |
|
0 commit comments