fix(update): rewrite ctx.renderNotFound() (#3126)
Fixes https://github.com/denoland/fresh/issues/3026
This commit is contained in:
@@ -80,7 +80,7 @@
|
|||||||
"tailwindcss": "npm:tailwindcss@^4.1.10",
|
"tailwindcss": "npm:tailwindcss@^4.1.10",
|
||||||
"postcss": "npm:postcss@8.5.6",
|
"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",
|
"@std/front-matter": "jsr:@std/front-matter@^1.0.5",
|
||||||
"github-slugger": "npm:github-slugger@^2.0.0",
|
"github-slugger": "npm:github-slugger@^2.0.0",
|
||||||
|
|||||||
@@ -78,7 +78,7 @@
|
|||||||
"npm:preact@^10.26.9": "10.26.9",
|
"npm:preact@^10.26.9": "10.26.9",
|
||||||
"npm:prismjs@^1.29.0": "1.30.0",
|
"npm:prismjs@^1.29.0": "1.30.0",
|
||||||
"npm:tailwindcss@^4.1.10": "4.1.11",
|
"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": {
|
"jsr": {
|
||||||
"@astral/astral@0.5.3": {
|
"@astral/astral@0.5.3": {
|
||||||
@@ -723,6 +723,15 @@
|
|||||||
"os": ["win32"],
|
"os": ["win32"],
|
||||||
"cpu": ["x64"]
|
"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": {
|
"@isaacs/fs-minipass@4.0.1": {
|
||||||
"integrity": "sha512-wgm9Ehl2jpeqP3zw/7mo3kRHFp5MEDhqAdwy1fTGkHAwnkGOVsgpvQhL8B5n1qlb01jV3n/bI0ZfZp5lWA1k4w==",
|
"integrity": "sha512-wgm9Ehl2jpeqP3zw/7mo3kRHFp5MEDhqAdwy1fTGkHAwnkGOVsgpvQhL8B5n1qlb01jV3n/bI0ZfZp5lWA1k4w==",
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
@@ -905,8 +914,8 @@
|
|||||||
"tailwindcss"
|
"tailwindcss"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"@ts-morph/common@0.26.1": {
|
"@ts-morph/common@0.27.0": {
|
||||||
"integrity": "sha512-Sn28TGl/4cFpcM+jwsH1wLncYq3FtN/BIpem+HOygfBWPT5pAeS5dB4VFVzV8FbnOKHpDLZmvAl4AjPEev5idA==",
|
"integrity": "sha512-Wf29UqxWDpc+i61k3oIOzcUfQt79PIT9y/MWfAGlrkjg6lBC1hwDECLXPVJAhWjiGbfBCxZd65F/LIZF3+jeJQ==",
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"fast-glob",
|
"fast-glob",
|
||||||
"minimatch",
|
"minimatch",
|
||||||
@@ -925,18 +934,9 @@
|
|||||||
"undici-types"
|
"undici-types"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"balanced-match@1.0.2": {
|
|
||||||
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="
|
|
||||||
},
|
|
||||||
"boolbase@1.0.0": {
|
"boolbase@1.0.0": {
|
||||||
"integrity": "sha512-JZOSA7Mo9sNGB8+UjSgzdLtokWAky1zbztM3WRLCbZ70/3cTANmQmOdR7y2g+J0e2WXywy1yS468tY+IruqEww=="
|
"integrity": "sha512-JZOSA7Mo9sNGB8+UjSgzdLtokWAky1zbztM3WRLCbZ70/3cTANmQmOdR7y2g+J0e2WXywy1yS468tY+IruqEww=="
|
||||||
},
|
},
|
||||||
"brace-expansion@2.0.2": {
|
|
||||||
"integrity": "sha512-Jt0vHyM+jmUBqojB7E1NIYadt0vI0Qxjxd2TErW94wDz+E2LAm5vKMXXwg6ZZBTHPuUlDgQHKXvjGBdfcF1ZDQ==",
|
|
||||||
"dependencies": [
|
|
||||||
"balanced-match"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"braces@3.0.3": {
|
"braces@3.0.3": {
|
||||||
"integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==",
|
"integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==",
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
@@ -1209,10 +1209,10 @@
|
|||||||
"picomatch"
|
"picomatch"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"minimatch@9.0.5": {
|
"minimatch@10.0.3": {
|
||||||
"integrity": "sha512-G6T0ZX48xgozx7587koeX9Ys2NYy6Gmv//P89sEte9V9whIapMNF4idKxnW2QtCcLiTWlb/wfCabAtAFWhhBow==",
|
"integrity": "sha512-IPZ167aShDZZUMdRk66cyQAW3qr0WzbHkPdMYa8bzZhlHhO3jALbKdxcaak7W9FfT2rZNpQuUu4Od7ILEpXSaw==",
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"brace-expansion"
|
"@isaacs/brace-expansion"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"minipass@7.1.2": {
|
"minipass@7.1.2": {
|
||||||
@@ -1305,8 +1305,8 @@
|
|||||||
"is-number"
|
"is-number"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"ts-morph@25.0.1": {
|
"ts-morph@26.0.0": {
|
||||||
"integrity": "sha512-QJEiTdnz1YjrB3JFhd626gX4rKHDLSjSVMvGGG4v7ONc3RBwa0Eei98G9AT9uNFDMtV54JyuXsFeC+OH0n6bXQ==",
|
"integrity": "sha512-ztMO++owQnz8c/gIENcM9XfCEzgoGphTv+nKpYNM1bgsdOVC/jRZuEBf6N+mLLDNg68Kl+GgUZfOySaRiG1/Ug==",
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"@ts-morph/common",
|
"@ts-morph/common",
|
||||||
"code-block-writer"
|
"code-block-writer"
|
||||||
@@ -1426,7 +1426,7 @@
|
|||||||
"npm:preact@^10.26.9",
|
"npm:preact@^10.26.9",
|
||||||
"npm:prismjs@^1.29.0",
|
"npm:prismjs@^1.29.0",
|
||||||
"npm:tailwindcss@^4.1.10",
|
"npm:tailwindcss@^4.1.10",
|
||||||
"npm:ts-morph@^25.0.1"
|
"npm:ts-morph@26"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-20
@@ -67,6 +67,7 @@ export interface ImportState {
|
|||||||
core: Set<string>;
|
core: Set<string>;
|
||||||
runtime: Set<string>;
|
runtime: Set<string>;
|
||||||
compat: Set<string>;
|
compat: Set<string>;
|
||||||
|
httpError: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const compat = new Set([
|
const compat = new Set([
|
||||||
@@ -267,6 +268,7 @@ async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> {
|
|||||||
core: new Set(),
|
core: new Set(),
|
||||||
runtime: new Set(),
|
runtime: new Set(),
|
||||||
compat: new Set(),
|
compat: new Set(),
|
||||||
|
httpError: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
const text = sourceFile.getFullText()
|
const text = sourceFile.getFullText()
|
||||||
@@ -302,7 +304,7 @@ async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> {
|
|||||||
const body = property.getBody();
|
const body = property.getBody();
|
||||||
if (body !== undefined) {
|
if (body !== undefined) {
|
||||||
const stmts = body.getDescendantStatements();
|
const stmts = body.getDescendantStatements();
|
||||||
rewriteCtxMethods(stmts);
|
rewriteCtxMethods(newImports, stmts);
|
||||||
}
|
}
|
||||||
|
|
||||||
maybePrependReqVar(property, newImports, true);
|
maybePrependReqVar(property, newImports, true);
|
||||||
@@ -317,7 +319,7 @@ async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> {
|
|||||||
const body = init.getBody();
|
const body = init.getBody();
|
||||||
if (body !== undefined) {
|
if (body !== undefined) {
|
||||||
const stmts = body.getDescendantStatements();
|
const stmts = body.getDescendantStatements();
|
||||||
rewriteCtxMethods(stmts);
|
rewriteCtxMethods(newImports, stmts);
|
||||||
}
|
}
|
||||||
|
|
||||||
maybePrependReqVar(init, newImports, true);
|
maybePrependReqVar(init, newImports, true);
|
||||||
@@ -329,7 +331,7 @@ async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> {
|
|||||||
const body = node.getBody();
|
const body = node.getBody();
|
||||||
if (body !== undefined) {
|
if (body !== undefined) {
|
||||||
const stmts = body.getDescendantStatements();
|
const stmts = body.getDescendantStatements();
|
||||||
rewriteCtxMethods(stmts);
|
rewriteCtxMethods(newImports, stmts);
|
||||||
}
|
}
|
||||||
|
|
||||||
maybePrependReqVar(node, newImports, false);
|
maybePrependReqVar(node, newImports, false);
|
||||||
@@ -354,7 +356,7 @@ async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> {
|
|||||||
const body = first.getBody();
|
const body = first.getBody();
|
||||||
if (body !== undefined) {
|
if (body !== undefined) {
|
||||||
const stmts = body.getDescendantStatements();
|
const stmts = body.getDescendantStatements();
|
||||||
rewriteCtxMethods(stmts);
|
rewriteCtxMethods(newImports, stmts);
|
||||||
}
|
}
|
||||||
|
|
||||||
maybePrependReqVar(first, newImports, false);
|
maybePrependReqVar(first, newImports, false);
|
||||||
@@ -366,7 +368,7 @@ async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> {
|
|||||||
const body = caller.getBody();
|
const body = caller.getBody();
|
||||||
if (body !== undefined) {
|
if (body !== undefined) {
|
||||||
const stmts = body.getDescendantStatements();
|
const stmts = body.getDescendantStatements();
|
||||||
rewriteCtxMethods(stmts);
|
rewriteCtxMethods(newImports, stmts);
|
||||||
}
|
}
|
||||||
|
|
||||||
maybePrependReqVar(caller, newImports, false);
|
maybePrependReqVar(caller, newImports, false);
|
||||||
@@ -441,6 +443,12 @@ async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> {
|
|||||||
namedImports: Array.from(newImports.compat),
|
namedImports: Array.from(newImports.compat),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (newImports.httpError > 0) {
|
||||||
|
sourceFile.addImportDeclaration({
|
||||||
|
moduleSpecifier: "fresh",
|
||||||
|
namedImports: ["HttpError"],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
await sourceFile.save();
|
await sourceFile.save();
|
||||||
await format(sourceFile.getFilePath());
|
await format(sourceFile.getFilePath());
|
||||||
@@ -563,17 +571,35 @@ function maybePrependReqVar(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function rewriteCtxMethods(
|
function rewriteCtxMethods(
|
||||||
|
importState: ImportState,
|
||||||
nodes: (tsmorph.Node<tsmorph.ts.Node>)[],
|
nodes: (tsmorph.Node<tsmorph.ts.Node>)[],
|
||||||
) {
|
) {
|
||||||
for (let i = 0; i < nodes.length; i++) {
|
for (let i = 0; i < nodes.length; i++) {
|
||||||
const node = nodes[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)) {
|
if (node.isKind(SyntaxKind.PropertyAccessExpression)) {
|
||||||
rewriteCtxMemberName(node);
|
rewriteCtxMemberName(node);
|
||||||
} else if (node.isKind(SyntaxKind.ReturnStatement)) {
|
} else if (node.isKind(SyntaxKind.ReturnStatement)) {
|
||||||
const expr = node.getExpression();
|
if (node.getText() === "return ctx.renderNotFound();") {
|
||||||
if (expr !== undefined) {
|
importState.httpError++;
|
||||||
rewriteCtxMethods([expr]);
|
node.replaceWithText(`throw new HttpError(404)`);
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
const expr = node.getExpression();
|
||||||
|
if (expr !== undefined) {
|
||||||
|
rewriteCtxMethods(importState, [expr]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (node.isKind(SyntaxKind.VariableStatement)) {
|
} else if (node.isKind(SyntaxKind.VariableStatement)) {
|
||||||
const decls = node.getDeclarations();
|
const decls = node.getDeclarations();
|
||||||
@@ -581,7 +607,7 @@ function rewriteCtxMethods(
|
|||||||
const decl = decls[i];
|
const decl = decls[i];
|
||||||
const init = decl.getInitializer();
|
const init = decl.getInitializer();
|
||||||
if (init !== undefined) {
|
if (init !== undefined) {
|
||||||
rewriteCtxMethods([init]);
|
rewriteCtxMethods(importState, [init]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (
|
} else if (
|
||||||
@@ -590,16 +616,16 @@ function rewriteCtxMethods(
|
|||||||
node.isKind(SyntaxKind.CallExpression)
|
node.isKind(SyntaxKind.CallExpression)
|
||||||
) {
|
) {
|
||||||
const expr = node.getExpression();
|
const expr = node.getExpression();
|
||||||
rewriteCtxMethods([expr]);
|
rewriteCtxMethods(importState, [expr]);
|
||||||
} else if (node.isKind(SyntaxKind.BinaryExpression)) {
|
} else if (node.isKind(SyntaxKind.BinaryExpression)) {
|
||||||
rewriteCtxMethods([node.getLeft()]);
|
rewriteCtxMethods(importState, [node.getLeft()]);
|
||||||
rewriteCtxMethods([node.getRight()]);
|
rewriteCtxMethods(importState, [node.getRight()]);
|
||||||
} else if (
|
} else if (
|
||||||
!node.isKind(SyntaxKind.ExpressionStatement) &&
|
!node.isKind(SyntaxKind.ExpressionStatement) &&
|
||||||
node.getKindName().endsWith("Statement")
|
node.getKindName().endsWith("Statement")
|
||||||
) {
|
) {
|
||||||
const inner = node.getDescendantStatements();
|
const inner = node.getDescendantStatements();
|
||||||
rewriteCtxMethods(inner);
|
rewriteCtxMethods(importState, inner);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -609,19 +635,12 @@ function rewriteCtxMemberName(
|
|||||||
) {
|
) {
|
||||||
const children = node.getChildren();
|
const children = node.getChildren();
|
||||||
if (children.length === 0) return;
|
if (children.length === 0) return;
|
||||||
const last = children[children.length - 1];
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
node.getExpression().getText() === "ctx" &&
|
node.getExpression().getText() === "ctx" &&
|
||||||
node.getName() === "remoteAddr"
|
node.getName() === "remoteAddr"
|
||||||
) {
|
) {
|
||||||
node.getExpression().replaceWithText("ctx.info.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)) {
|
} else if (children[0].isKind(SyntaxKind.PropertyAccessExpression)) {
|
||||||
rewriteCtxMemberName(children[0]);
|
rewriteCtxMemberName(children[0]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -542,16 +542,17 @@ Deno.test(
|
|||||||
const files = await readFiles(dir);
|
const files = await readFiles(dir);
|
||||||
expect(files["/routes/index.tsx"])
|
expect(files["/routes/index.tsx"])
|
||||||
.toEqual(`import { FreshContext } from "fresh";
|
.toEqual(`import { FreshContext } from "fresh";
|
||||||
|
import { HttpError } from "fresh";
|
||||||
|
|
||||||
export default async function Index(ctx: FreshContext) {
|
export default async function Index(ctx: FreshContext) {
|
||||||
const req = ctx.req;
|
const req = ctx.req;
|
||||||
|
|
||||||
if (true) {
|
if (true) {
|
||||||
return ctx.throw(404);
|
throw new HttpError(404);
|
||||||
}
|
}
|
||||||
if ("foo" === "foo" as any) {
|
if ("foo" === "foo" as any) {
|
||||||
ctx.throw(404);
|
throw new HttpError(404);
|
||||||
return ctx.throw(404);
|
throw new HttpError(404);
|
||||||
}
|
}
|
||||||
return new Response(req.url);
|
return new Response(req.url);
|
||||||
}`);
|
}`);
|
||||||
@@ -559,7 +560,7 @@ export default async function Index(ctx: FreshContext) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
Deno.test.ignore(
|
Deno.test.ignore(
|
||||||
"update - 1.x ctx.renderNotFound() -> ctx.throw()",
|
"update - 1.x ctx.renderNotFound() -> throw new HttpError(404)",
|
||||||
async () => {
|
async () => {
|
||||||
await using _tmp = await withTmpDir();
|
await using _tmp = await withTmpDir();
|
||||||
const dir = _tmp.dir;
|
const dir = _tmp.dir;
|
||||||
|
|||||||
Reference in New Issue
Block a user