fix(update): rewrite ctx.renderNotFound() (#3126)

Fixes https://github.com/denoland/fresh/issues/3026
This commit is contained in:
Marvin Hagemeister
2025-07-24 16:10:08 +02:00
committed by GitHub
parent 5903684d02
commit 211af4e3c8
4 changed files with 63 additions and 43 deletions
+39 -20
View File
@@ -67,6 +67,7 @@ export interface ImportState {
core: Set<string>;
runtime: Set<string>;
compat: Set<string>;
httpError: number;
}
const compat = new Set([
@@ -267,6 +268,7 @@ async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> {
core: new Set(),
runtime: new Set(),
compat: new Set(),
httpError: 0,
};
const text = sourceFile.getFullText()
@@ -302,7 +304,7 @@ async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> {
const body = property.getBody();
if (body !== undefined) {
const stmts = body.getDescendantStatements();
rewriteCtxMethods(stmts);
rewriteCtxMethods(newImports, stmts);
}
maybePrependReqVar(property, newImports, true);
@@ -317,7 +319,7 @@ async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> {
const body = init.getBody();
if (body !== undefined) {
const stmts = body.getDescendantStatements();
rewriteCtxMethods(stmts);
rewriteCtxMethods(newImports, stmts);
}
maybePrependReqVar(init, newImports, true);
@@ -329,7 +331,7 @@ async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> {
const body = node.getBody();
if (body !== undefined) {
const stmts = body.getDescendantStatements();
rewriteCtxMethods(stmts);
rewriteCtxMethods(newImports, stmts);
}
maybePrependReqVar(node, newImports, false);
@@ -354,7 +356,7 @@ async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> {
const body = first.getBody();
if (body !== undefined) {
const stmts = body.getDescendantStatements();
rewriteCtxMethods(stmts);
rewriteCtxMethods(newImports, stmts);
}
maybePrependReqVar(first, newImports, false);
@@ -366,7 +368,7 @@ async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> {
const body = caller.getBody();
if (body !== undefined) {
const stmts = body.getDescendantStatements();
rewriteCtxMethods(stmts);
rewriteCtxMethods(newImports, stmts);
}
maybePrependReqVar(caller, newImports, false);
@@ -441,6 +443,12 @@ async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> {
namedImports: Array.from(newImports.compat),
});
}
if (newImports.httpError > 0) {
sourceFile.addImportDeclaration({
moduleSpecifier: "fresh",
namedImports: ["HttpError"],
});
}
await sourceFile.save();
await format(sourceFile.getFilePath());
@@ -563,17 +571,35 @@ function maybePrependReqVar(
}
function rewriteCtxMethods(
importState: ImportState,
nodes: (tsmorph.Node<tsmorph.ts.Node>)[],
) {
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
if (node.wasForgotten()) continue;
if (
node.isKind(SyntaxKind.ExpressionStatement) &&
node.getText() === "ctx.renderNotFound();"
) {
importState.httpError++;
node.replaceWithText("throw new HttpError(404);");
continue;
}
if (node.isKind(SyntaxKind.PropertyAccessExpression)) {
rewriteCtxMemberName(node);
} else if (node.isKind(SyntaxKind.ReturnStatement)) {
const expr = node.getExpression();
if (expr !== undefined) {
rewriteCtxMethods([expr]);
if (node.getText() === "return ctx.renderNotFound();") {
importState.httpError++;
node.replaceWithText(`throw new HttpError(404)`);
continue;
} else {
const expr = node.getExpression();
if (expr !== undefined) {
rewriteCtxMethods(importState, [expr]);
}
}
} else if (node.isKind(SyntaxKind.VariableStatement)) {
const decls = node.getDeclarations();
@@ -581,7 +607,7 @@ function rewriteCtxMethods(
const decl = decls[i];
const init = decl.getInitializer();
if (init !== undefined) {
rewriteCtxMethods([init]);
rewriteCtxMethods(importState, [init]);
}
}
} else if (
@@ -590,16 +616,16 @@ function rewriteCtxMethods(
node.isKind(SyntaxKind.CallExpression)
) {
const expr = node.getExpression();
rewriteCtxMethods([expr]);
rewriteCtxMethods(importState, [expr]);
} else if (node.isKind(SyntaxKind.BinaryExpression)) {
rewriteCtxMethods([node.getLeft()]);
rewriteCtxMethods([node.getRight()]);
rewriteCtxMethods(importState, [node.getLeft()]);
rewriteCtxMethods(importState, [node.getRight()]);
} else if (
!node.isKind(SyntaxKind.ExpressionStatement) &&
node.getKindName().endsWith("Statement")
) {
const inner = node.getDescendantStatements();
rewriteCtxMethods(inner);
rewriteCtxMethods(importState, inner);
}
}
}
@@ -609,19 +635,12 @@ function rewriteCtxMemberName(
) {
const children = node.getChildren();
if (children.length === 0) return;
const last = children[children.length - 1];
if (
node.getExpression().getText() === "ctx" &&
node.getName() === "remoteAddr"
) {
node.getExpression().replaceWithText("ctx.info.remoteAddr");
} else if (last.getText() === "renderNotFound") {
last.replaceWithText("throw");
const caller = node.getParentIfKind(SyntaxKind.CallExpression);
if (caller !== undefined) {
caller.addArgument("404");
}
} else if (children[0].isKind(SyntaxKind.PropertyAccessExpression)) {
rewriteCtxMemberName(children[0]);
}
+5 -4
View File
@@ -542,16 +542,17 @@ Deno.test(
const files = await readFiles(dir);
expect(files["/routes/index.tsx"])
.toEqual(`import { FreshContext } from "fresh";
import { HttpError } from "fresh";
export default async function Index(ctx: FreshContext) {
const req = ctx.req;
if (true) {
return ctx.throw(404);
throw new HttpError(404);
}
if ("foo" === "foo" as any) {
ctx.throw(404);
return ctx.throw(404);
throw new HttpError(404);
throw new HttpError(404);
}
return new Response(req.url);
}`);
@@ -559,7 +560,7 @@ export default async function Index(ctx: FreshContext) {
);
Deno.test.ignore(
"update - 1.x ctx.renderNotFound() -> ctx.throw()",
"update - 1.x ctx.renderNotFound() -> throw new HttpError(404)",
async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;