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
+1 -1
View File
@@ -80,7 +80,7 @@
"tailwindcss": "npm:tailwindcss@^4.1.10",
"postcss": "npm:postcss@8.5.6",
"ts-morph": "npm:ts-morph@^25.0.1",
"ts-morph": "npm:ts-morph@^26.0.0",
"@std/front-matter": "jsr:@std/front-matter@^1.0.5",
"github-slugger": "npm:github-slugger@^2.0.0",
Generated
+18 -18
View File
@@ -78,7 +78,7 @@
"npm:preact@^10.26.9": "10.26.9",
"npm:prismjs@^1.29.0": "1.30.0",
"npm:tailwindcss@^4.1.10": "4.1.11",
"npm:ts-morph@^25.0.1": "25.0.1"
"npm:ts-morph@26": "26.0.0"
},
"jsr": {
"@astral/astral@0.5.3": {
@@ -723,6 +723,15 @@
"os": ["win32"],
"cpu": ["x64"]
},
"@isaacs/balanced-match@4.0.1": {
"integrity": "sha512-yzMTt9lEb8Gv7zRioUilSglI0c0smZ9k5D65677DLWLtWJaXIS3CqcGyUFByYKlnUj6TkjLVs54fBl6+TiGQDQ=="
},
"@isaacs/brace-expansion@5.0.0": {
"integrity": "sha512-ZT55BDLV0yv0RBm2czMiZ+SqCGO7AvmOM3G/w2xhVPH+te0aKgFjmBvGlL1dH+ql2tgGO3MVrbb3jCKyvpgnxA==",
"dependencies": [
"@isaacs/balanced-match"
]
},
"@isaacs/fs-minipass@4.0.1": {
"integrity": "sha512-wgm9Ehl2jpeqP3zw/7mo3kRHFp5MEDhqAdwy1fTGkHAwnkGOVsgpvQhL8B5n1qlb01jV3n/bI0ZfZp5lWA1k4w==",
"dependencies": [
@@ -905,8 +914,8 @@
"tailwindcss"
]
},
"@ts-morph/common@0.26.1": {
"integrity": "sha512-Sn28TGl/4cFpcM+jwsH1wLncYq3FtN/BIpem+HOygfBWPT5pAeS5dB4VFVzV8FbnOKHpDLZmvAl4AjPEev5idA==",
"@ts-morph/common@0.27.0": {
"integrity": "sha512-Wf29UqxWDpc+i61k3oIOzcUfQt79PIT9y/MWfAGlrkjg6lBC1hwDECLXPVJAhWjiGbfBCxZd65F/LIZF3+jeJQ==",
"dependencies": [
"fast-glob",
"minimatch",
@@ -925,18 +934,9 @@
"undici-types"
]
},
"balanced-match@1.0.2": {
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="
},
"boolbase@1.0.0": {
"integrity": "sha512-JZOSA7Mo9sNGB8+UjSgzdLtokWAky1zbztM3WRLCbZ70/3cTANmQmOdR7y2g+J0e2WXywy1yS468tY+IruqEww=="
},
"brace-expansion@2.0.2": {
"integrity": "sha512-Jt0vHyM+jmUBqojB7E1NIYadt0vI0Qxjxd2TErW94wDz+E2LAm5vKMXXwg6ZZBTHPuUlDgQHKXvjGBdfcF1ZDQ==",
"dependencies": [
"balanced-match"
]
},
"braces@3.0.3": {
"integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==",
"dependencies": [
@@ -1209,10 +1209,10 @@
"picomatch"
]
},
"minimatch@9.0.5": {
"integrity": "sha512-G6T0ZX48xgozx7587koeX9Ys2NYy6Gmv//P89sEte9V9whIapMNF4idKxnW2QtCcLiTWlb/wfCabAtAFWhhBow==",
"minimatch@10.0.3": {
"integrity": "sha512-IPZ167aShDZZUMdRk66cyQAW3qr0WzbHkPdMYa8bzZhlHhO3jALbKdxcaak7W9FfT2rZNpQuUu4Od7ILEpXSaw==",
"dependencies": [
"brace-expansion"
"@isaacs/brace-expansion"
]
},
"minipass@7.1.2": {
@@ -1305,8 +1305,8 @@
"is-number"
]
},
"ts-morph@25.0.1": {
"integrity": "sha512-QJEiTdnz1YjrB3JFhd626gX4rKHDLSjSVMvGGG4v7ONc3RBwa0Eei98G9AT9uNFDMtV54JyuXsFeC+OH0n6bXQ==",
"ts-morph@26.0.0": {
"integrity": "sha512-ztMO++owQnz8c/gIENcM9XfCEzgoGphTv+nKpYNM1bgsdOVC/jRZuEBf6N+mLLDNg68Kl+GgUZfOySaRiG1/Ug==",
"dependencies": [
"@ts-morph/common",
"code-block-writer"
@@ -1426,7 +1426,7 @@
"npm:preact@^10.26.9",
"npm:prismjs@^1.29.0",
"npm:tailwindcss@^4.1.10",
"npm:ts-morph@^25.0.1"
"npm:ts-morph@26"
]
}
}
+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;