Skip to content

Commit 082f20f

Browse files
committed
refactor(compiler): simplify logic
1 parent 2cd68b5 commit 082f20f

File tree

1 file changed

+20
-39
lines changed

1 file changed

+20
-39
lines changed

src/compiler.ts

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -81,35 +81,23 @@ function compileRouteMatch(
8181
deps?: any[],
8282
opts?: RouterCompilerOptions,
8383
): string {
84-
// Ignore trailing slash
85-
let str = `${opts?.matchAll ? `let r=[];` : ""}if(p[p.length-1]==='/')p=p.slice(0,-1)||'/';`;
86-
84+
let str = "";
8785
const staticNodes = new Set<Node>();
8886

8987
for (const key in router.static) {
9088
const node = router.static[key];
9189
if (node?.methods) {
9290
staticNodes.add(node);
93-
str += `if(p===${JSON.stringify(key.replace(/\/$/, "") || "/")}){${compileMethodMatch(node.methods, [], deps, -1, opts)[1]}}`;
91+
str += `if(p===${JSON.stringify(key.replace(/\/$/, "") || "/")}){${compileMethodMatch(node.methods, [], deps, -1, opts)}}`;
9492
}
9593
}
9694

97-
const [existsTail, tail] = compileNode(
98-
router.root,
99-
[],
100-
0,
101-
deps,
102-
false,
103-
staticNodes,
104-
opts,
105-
);
106-
const returnStmt = opts?.matchAll ? "return r;" : "";
107-
return (
108-
str +
109-
(existsTail
110-
? "let s=p.split('/'),l=s.length-1;" + tail + returnStmt
111-
: returnStmt)
112-
);
95+
const match = compileNode(router.root, [], 0, deps, false, staticNodes, opts);
96+
if (match) {
97+
str += "let s=p.split('/'),l=s.length-1;" + match;
98+
}
99+
100+
return `${opts?.matchAll ? `let r=[];` : ""}if(p[p.length-1]==='/')p=p.slice(0,-1)||'/';${str}${opts?.matchAll ? "return r;" : ""}`;
113101
}
114102

115103
function compileMethodMatch(
@@ -118,13 +106,11 @@ function compileMethodMatch(
118106
deps: any[] | undefined,
119107
currentIdx: number, // Set to -1 for non-param node
120108
opts?: RouterCompilerOptions,
121-
): [boolean, string] {
109+
): string {
122110
let str = "";
123-
let exists = false;
124111
for (const key in methods) {
125112
const data = methods[key];
126113
if (data && data?.length > 0) {
127-
exists = true;
128114
// Don't check for matchAll method handler
129115
if (key !== "") str += `if(m==='${key}')`;
130116

@@ -165,7 +151,7 @@ function compileMethodMatch(
165151
str += opts?.matchAll ? `r.unshift(${res}});` : `return ${res}};`;
166152
}
167153
}
168-
return [exists, str];
154+
return str;
169155
}
170156

171157
/**
@@ -179,27 +165,25 @@ function compileNode(
179165
isParamNode: boolean,
180166
staticNodes: Set<Node>,
181167
opts?: RouterCompilerOptions,
182-
): [boolean, string] {
168+
): string {
183169
let str = "";
184-
let exists = false;
185170

186171
if (node.methods && !staticNodes.has(node)) {
187-
const [existsChild, match] = compileMethodMatch(
172+
const match = compileMethodMatch(
188173
node.methods,
189174
params,
190175
deps,
191176
isParamNode ? startIdx : -1,
192177
opts,
193178
);
194-
if (existsChild) {
195-
exists = true;
179+
if (match) {
196180
str += `if(l===${startIdx}${isParamNode ? `||l===${startIdx - 1}` : ""}){${match}}`;
197181
}
198182
}
199183

200184
if (node.static) {
201185
for (const key in node.static) {
202-
const [existsChild, match] = compileNode(
186+
const match = compileNode(
203187
node.static[key],
204188
params,
205189
startIdx + 1,
@@ -208,15 +192,14 @@ function compileNode(
208192
staticNodes,
209193
opts,
210194
);
211-
if (existsChild) {
212-
exists = true;
195+
if (match) {
213196
str += `if(s[${startIdx + 1}]===${JSON.stringify(key)}){${match}}`;
214197
}
215198
}
216199
}
217200

218201
if (node.param) {
219-
const [existsChild, match] = compileNode(
202+
const match = compileNode(
220203
node.param,
221204
[...params, `s[${startIdx + 1}]`],
222205
startIdx + 1,
@@ -225,8 +208,7 @@ function compileNode(
225208
staticNodes,
226209
opts,
227210
);
228-
if (existsChild) {
229-
exists = true;
211+
if (match) {
230212
str += match;
231213
}
232214
}
@@ -238,21 +220,20 @@ function compileNode(
238220
}
239221

240222
if (wildcard.methods) {
241-
const [existsChild, match] = compileMethodMatch(
223+
const match = compileMethodMatch(
242224
wildcard.methods,
243225
[...params, `s.slice(${startIdx + 1}).join('/')`],
244226
deps,
245227
startIdx,
246228
opts,
247229
);
248-
if (existsChild) {
249-
exists = true;
230+
if (match) {
250231
str += match;
251232
}
252233
}
253234
}
254235

255-
return [exists, str];
236+
return str;
256237
}
257238

258239
/**

0 commit comments

Comments
 (0)