forked from denoland/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaybePrependReqVar.ts
More file actions
105 lines (97 loc) · 3.13 KB
/
maybePrependReqVar.ts
File metadata and controls
105 lines (97 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import * as tsmorph from "ts-morph";
import { type ImportState, SyntaxKind } from "./update.ts";
export function maybePrependReqVar(
method:
| tsmorph.MethodDeclaration
| tsmorph.FunctionDeclaration
| tsmorph.FunctionExpression
| tsmorph.ArrowFunction,
newImports: ImportState,
hasInferredTypes: boolean,
) {
let hasRequestVar = false;
const params = method.getParameters();
if (params.length > 0) {
const paramName = params[0].getName();
// Add explicit types if the user did that
if (hasInferredTypes && params[0].getTypeNode()) {
hasInferredTypes = false;
}
hasRequestVar = params.length > 1 || paramName === "req";
if (hasRequestVar || paramName === "_req") {
if (hasRequestVar && params.length === 1) {
params[0].replaceWithText("ctx");
if (!hasInferredTypes) {
newImports.core.add("FreshContext");
params[0].setType("FreshContext");
}
} else {
params[0].remove();
// Use proper type
if (params.length > 1) {
const initType = params[1].getTypeNode()?.getText();
if (initType !== undefined && initType === "RouteContext") {
newImports.core.add("FreshContext");
params[1].setType("FreshContext");
}
}
}
}
const maybeObjBinding = params.length > 1
? params[1].getNameNode()
: undefined;
if (method.isKind(SyntaxKind.ArrowFunction)) {
const body = method.getBody();
if (body !== undefined && !body.isKind(SyntaxKind.Block)) {
// deno-lint-ignore no-console
console.warn(`Cannot transform arrow function`);
return;
}
}
if (
(maybeObjBinding === undefined ||
!maybeObjBinding.isKind(SyntaxKind.ObjectBindingPattern)) &&
hasRequestVar &&
!paramName.startsWith("_")
) {
method.insertVariableStatement(0, {
declarationKind: tsmorph.VariableDeclarationKind.Const,
declarations: [{
name: paramName,
initializer: "ctx.request",
}],
});
}
if (
maybeObjBinding !== undefined &&
maybeObjBinding.isKind(SyntaxKind.ObjectBindingPattern)
) {
const objBinding = maybeObjBinding as tsmorph.ObjectBindingPattern;
const bindings = objBinding.getElements();
if (bindings.length > 0) {
let needsRemoteAddr = false;
for (let i = 0; i < bindings.length; i++) {
const binding = bindings[i];
const name = binding.getName();
if (name === "remoteAddr") {
binding.replaceWithText("info");
needsRemoteAddr = true;
}
}
if (hasRequestVar && !paramName.startsWith("_")) {
const txt = maybeObjBinding.getFullText().slice(0, -2);
maybeObjBinding.replaceWithText(txt + ", req }");
}
if (needsRemoteAddr) {
method.insertVariableStatement(0, {
declarationKind: tsmorph.VariableDeclarationKind.Const,
declarations: [{
name: "remoteAddr",
initializer: "info.remoteAddr",
}],
});
}
}
}
}
}