(sourceFile: tsmorph.SourceFile)
| 320 | } |
| 321 | |
| 322 | async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> { |
| 323 | // keep original text for comparison |
| 324 | const originalText = sourceFile.getFullText(); |
| 325 | |
| 326 | const newImports: ImportState = { |
| 327 | core: new Set(), |
| 328 | runtime: new Set(), |
| 329 | compat: new Set(), |
| 330 | httpError: 0, |
| 331 | }; |
| 332 | |
| 333 | const text = sourceFile.getFullText() |
| 334 | .replaceAll("/** @jsx h */\n", "") |
| 335 | .replaceAll("/** @jsxFrag Fragment */\n", "") |
| 336 | .replaceAll('/// <reference no-default-lib="true" />\n', "") |
| 337 | .replaceAll('/// <reference lib="dom" />\n', "") |
| 338 | .replaceAll('/// <reference lib="dom.iterable" />\n', "") |
| 339 | .replaceAll('/// <reference lib="dom.asynciterable" />\n', "") |
| 340 | .replaceAll('/// <reference lib="deno.ns" />\n', ""); |
| 341 | sourceFile.replaceWithText(text); |
| 342 | |
| 343 | if ( |
| 344 | sourceFile.getFilePath().includes("/routes/") && |
| 345 | !sourceFile.getDirectoryPath().includes("/(_") |
| 346 | ) { |
| 347 | for (const [name, decl] of sourceFile.getExportedDeclarations()) { |
| 348 | if (name === "handler") { |
| 349 | const node = decl[0]; |
| 350 | if (node.isKind(SyntaxKind.VariableDeclaration)) { |
| 351 | const init = node.getInitializer(); |
| 352 | if ( |
| 353 | init !== undefined && |
| 354 | init.isKind(SyntaxKind.ObjectLiteralExpression) |
| 355 | ) { |
| 356 | for (const property of init.getProperties()) { |
| 357 | if (property.isKind(SyntaxKind.MethodDeclaration)) { |
| 358 | const name = property.getName(); |
| 359 | if ( |
| 360 | name === "GET" || name === "POST" || name === "PATCH" || |
| 361 | name === "PUT" || name === "DELETE" |
| 362 | ) { |
| 363 | const body = property.getBody(); |
| 364 | if (body !== undefined) { |
| 365 | const stmts = body.getDescendantStatements(); |
| 366 | rewriteCtxMethods(newImports, stmts); |
| 367 | } |
| 368 | |
| 369 | maybePrependReqVar(property, newImports, true); |
| 370 | } |
| 371 | } else if (property.isKind(SyntaxKind.PropertyAssignment)) { |
| 372 | const init = property.getInitializer(); |
| 373 | if ( |
| 374 | init !== undefined && |
| 375 | (init.isKind(SyntaxKind.ArrowFunction) || |
| 376 | init.isKind(SyntaxKind.FunctionExpression)) |
| 377 | ) { |
| 378 | const body = init.getBody(); |
| 379 | if (body !== undefined) { |
no test coverage detected