zitejs 0.10.0-start.2 → 0.10.0-start.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/auth/index.d.ts +2 -2
- package/dist/cjs/auth/index.js +6 -0
- package/dist/cjs/auth/index.test.js +30 -0
- package/dist/cjs/auth/previewClaim.d.ts +7 -0
- package/dist/cjs/auth/previewClaim.js +16 -0
- package/dist/cjs/backend/index.d.ts +121 -3
- package/dist/cjs/backend/index.test.js +58 -0
- package/dist/cjs/bundle/index.d.ts +8 -0
- package/dist/cjs/bundle/index.js +9 -8
- package/dist/cjs/check/index.d.ts +45 -1
- package/dist/cjs/check/index.js +198 -109
- package/dist/cjs/check/start/problem.d.ts +11 -0
- package/dist/cjs/check/start/problem.js +5 -0
- package/dist/cjs/check/start/routeTree.d.ts +9 -0
- package/dist/cjs/check/start/routeTree.js +111 -0
- package/dist/cjs/check/start/sourceRules.d.ts +8 -0
- package/dist/cjs/check/start/sourceRules.js +1110 -0
- package/dist/cjs/check/start/tsgo.d.ts +2 -0
- package/dist/cjs/check/start/tsgo.js +35 -0
- package/dist/cjs/cli.js +1 -1
- package/dist/cjs/client/index.d.ts +24 -0
- package/dist/cjs/dev/index.js +14 -4
- package/dist/cjs/integrations/rules.d.ts +13 -0
- package/dist/cjs/integrations/rules.js +29 -0
- package/dist/cjs/integrations/rules.test.js +1 -0
- package/dist/cjs/internal/appMode.d.ts +3 -0
- package/dist/cjs/internal/appMode.js +15 -0
- package/dist/cjs/runtime/index.d.ts +13 -13
- package/dist/cjs/start/entries.d.ts +10 -4
- package/dist/cjs/start/entries.js +23 -6
- package/dist/cjs/steps/index.d.ts +78 -0
- package/dist/cjs/steps/index.js +507 -0
- package/dist/cjs/steps/index.test.d.ts +1 -0
- package/dist/cjs/steps/index.test.js +635 -0
- package/dist/cjs/sync/index.js +2 -1
- package/dist/cjs/sync/lib.d.ts +10 -3
- package/dist/cjs/sync/lib.js +35 -21
- package/dist/cjs/sync/lib.test.js +106 -36
- package/dist/cjs/types/fields.d.ts +8 -1
- package/dist/cjs/types/fields.js +7 -1
- package/dist/cjs/types/tables.d.ts +16 -0
- package/dist/cjs/upload/index.js +12 -2
- package/dist/cjs/upload/index.test.js +39 -0
- package/dist/cjs/vite/index.js +8 -3
- package/dist/cjs/vite/index.test.js +10 -2
- package/dist/esm/auth/index.d.ts +2 -2
- package/dist/esm/auth/index.js +6 -0
- package/dist/esm/auth/index.test.js +30 -0
- package/dist/esm/auth/previewClaim.d.ts +7 -0
- package/dist/esm/auth/previewClaim.js +12 -0
- package/dist/esm/backend/index.d.ts +121 -3
- package/dist/esm/backend/index.test.js +58 -0
- package/dist/esm/bundle/index.d.ts +8 -0
- package/dist/esm/bundle/index.js +8 -8
- package/dist/esm/check/index.d.ts +45 -1
- package/dist/esm/check/index.js +198 -113
- package/dist/esm/check/start/problem.d.ts +11 -0
- package/dist/esm/check/start/problem.js +1 -0
- package/dist/esm/check/start/routeTree.d.ts +9 -0
- package/dist/esm/check/start/routeTree.js +70 -0
- package/dist/esm/check/start/sourceRules.d.ts +8 -0
- package/dist/esm/check/start/sourceRules.js +1107 -0
- package/dist/esm/check/start/tsgo.d.ts +2 -0
- package/dist/esm/check/start/tsgo.js +29 -0
- package/dist/esm/cli.js +1 -1
- package/dist/esm/client/index.d.ts +24 -0
- package/dist/esm/dev/index.js +16 -6
- package/dist/esm/integrations/rules.d.ts +13 -0
- package/dist/esm/integrations/rules.js +29 -0
- package/dist/esm/integrations/rules.test.js +1 -0
- package/dist/esm/internal/appMode.d.ts +3 -0
- package/dist/esm/internal/appMode.js +10 -0
- package/dist/esm/runtime/index.d.ts +13 -13
- package/dist/esm/start/aiUsage.d.ts +23 -0
- package/dist/esm/start/aiUsage.js +146 -0
- package/dist/esm/start/airtable/arrayFields.d.ts +10 -0
- package/dist/esm/start/airtable/arrayFields.js +30 -0
- package/dist/esm/start/airtable/dispatch.d.ts +54 -0
- package/dist/esm/start/airtable/dispatch.js +483 -0
- package/dist/esm/start/airtable/formulas.d.ts +70 -0
- package/dist/esm/start/airtable/formulas.js +258 -0
- package/dist/esm/start/airtable/index.d.ts +8 -0
- package/dist/esm/start/airtable/index.js +54 -0
- package/dist/esm/start/authHooks.d.ts +7 -0
- package/dist/esm/start/authHooks.js +10 -0
- package/dist/esm/start/authHooksModule.d.ts +1 -0
- package/dist/esm/start/authHooksModule.js +3 -0
- package/dist/esm/start/clientErrorMessage.test.d.ts +1 -0
- package/dist/esm/start/clientErrorMessage.test.js +105 -0
- package/dist/esm/start/credentials.d.ts +2 -0
- package/dist/esm/start/credentials.js +2 -0
- package/dist/esm/start/data/baseRunner.d.ts +92 -0
- package/dist/esm/start/data/baseRunner.js +83 -0
- package/dist/esm/start/data/dispatch.d.ts +16 -0
- package/dist/esm/start/data/dispatch.js +198 -0
- package/dist/esm/start/data/index.d.ts +15 -0
- package/dist/esm/start/data/index.js +58 -0
- package/dist/esm/start/data/sqlNameMappings.d.ts +58 -0
- package/dist/esm/start/data/sqlNameMappings.js +92 -0
- package/dist/esm/start/data/transformations.d.ts +97 -0
- package/dist/esm/start/data/transformations.js +262 -0
- package/dist/esm/start/data/types.d.ts +91 -0
- package/dist/esm/start/data/types.js +14 -0
- package/dist/esm/start/describe.d.ts +35 -5
- package/dist/esm/start/describe.js +74 -22
- package/dist/esm/start/entries.d.ts +10 -4
- package/dist/esm/start/entries.js +21 -5
- package/dist/esm/start/errors.d.ts +63 -0
- package/dist/esm/start/errors.js +146 -0
- package/dist/esm/start/errors.test.d.ts +1 -0
- package/dist/esm/start/errors.test.js +23 -0
- package/dist/esm/start/factories.test-d.d.ts +1 -0
- package/dist/esm/start/factories.test-d.js +20 -0
- package/dist/esm/start/index.d.ts +32 -27
- package/dist/esm/start/index.js +77 -47
- package/dist/esm/start/instance.d.ts +4 -4
- package/dist/esm/start/instance.js +54 -11
- package/dist/esm/start/outbound.d.ts +11 -0
- package/dist/esm/start/outbound.js +398 -0
- package/dist/esm/start/previewClaim.d.ts +7 -0
- package/dist/esm/start/previewClaim.js +23 -0
- package/dist/esm/start/problems.js +1 -1
- package/dist/esm/start/quiet.d.ts +2 -0
- package/dist/esm/start/quiet.js +27 -0
- package/dist/esm/start/redactSecrets.d.ts +15 -0
- package/dist/esm/start/redactSecrets.js +154 -0
- package/dist/esm/start/redactSecrets.test.d.ts +1 -0
- package/dist/esm/start/redactSecrets.test.js +151 -0
- package/dist/esm/start/registration.d.ts +13 -0
- package/dist/esm/start/registration.js +21 -0
- package/dist/esm/start/registrationHash.d.ts +22 -0
- package/dist/esm/start/registrationHash.js +470 -0
- package/dist/esm/start/router.d.ts +18 -4
- package/dist/esm/start/router.js +134 -11
- package/dist/esm/start/runLog.d.ts +7 -4
- package/dist/esm/start/runLog.js +64 -6
- package/dist/esm/start/scope.d.ts +2 -2
- package/dist/esm/start/server.d.ts +40 -36
- package/dist/esm/start/server.js +320 -48
- package/dist/esm/start/steps.d.ts +28 -0
- package/dist/esm/start/steps.js +97 -0
- package/dist/esm/start/types.d.ts +178 -5
- package/dist/esm/start/version.d.ts +1 -0
- package/dist/esm/start/version.js +2 -0
- package/dist/esm/start/vite.js +88 -11
- package/dist/esm/start/worker.d.ts +6 -0
- package/dist/esm/start/worker.js +12 -0
- package/dist/esm/start/workflow.d.ts +42 -12
- package/dist/esm/start/workflow.js +3 -3
- package/dist/esm/start/workflow.test-d.js +41 -8
- package/dist/esm/start/workflowModules.js +1 -1
- package/dist/esm/start/workflowRun.d.ts +21 -8
- package/dist/esm/start/workflowRun.js +29 -8
- package/dist/esm/steps/index.d.ts +78 -0
- package/dist/esm/steps/index.js +502 -0
- package/dist/esm/steps/index.test.d.ts +1 -0
- package/dist/esm/steps/index.test.js +633 -0
- package/dist/esm/sync/index.js +2 -1
- package/dist/esm/sync/lib.d.ts +10 -3
- package/dist/esm/sync/lib.js +35 -21
- package/dist/esm/sync/lib.test.js +106 -36
- package/dist/esm/types/fields.d.ts +8 -1
- package/dist/esm/types/fields.js +6 -0
- package/dist/esm/types/tables.d.ts +16 -0
- package/dist/esm/upload/index.js +12 -2
- package/dist/esm/upload/index.test.js +39 -0
- package/dist/esm/vite/index.js +8 -3
- package/dist/esm/vite/index.test.js +10 -2
- package/package.json +37 -16
- package/dist/cjs/start/problems.d.ts +0 -19
- package/dist/cjs/start/problems.js +0 -11
|
@@ -0,0 +1,1107 @@
|
|
|
1
|
+
// Source hints for a Start app: line-level findings read from the code. The
|
|
2
|
+
// platform's build describes the app and enforces access; these only say early,
|
|
3
|
+
// and only where the code shows it for sure, what that build would refuse. A
|
|
4
|
+
// file that doesn't parse is skipped; the typecheck reports it.
|
|
5
|
+
import { parse } from "@babel/parser";
|
|
6
|
+
import { ENTRY_EXTENSIONS } from "./routeTree.js";
|
|
7
|
+
const isNode = (value) => typeof value === "object" && value !== null && typeof value.type === "string";
|
|
8
|
+
const SKIP_KEYS = new Set(["loc", "start", "end", "extra", "leadingComments", "trailingComments", "innerComments", "range"]);
|
|
9
|
+
const walk = (node, visit, ancestors = []) => {
|
|
10
|
+
visit(node, ancestors);
|
|
11
|
+
ancestors.push(node);
|
|
12
|
+
for (const key of Object.keys(node)) {
|
|
13
|
+
if (SKIP_KEYS.has(key))
|
|
14
|
+
continue;
|
|
15
|
+
const value = node[key];
|
|
16
|
+
if (Array.isArray(value)) {
|
|
17
|
+
for (const child of value)
|
|
18
|
+
if (isNode(child))
|
|
19
|
+
walk(child, visit, ancestors);
|
|
20
|
+
}
|
|
21
|
+
else if (isNode(value)) {
|
|
22
|
+
walk(value, visit, ancestors);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
ancestors.pop();
|
|
26
|
+
};
|
|
27
|
+
const FUNCTION_TYPES = new Set([
|
|
28
|
+
"FunctionDeclaration",
|
|
29
|
+
"FunctionExpression",
|
|
30
|
+
"ArrowFunctionExpression",
|
|
31
|
+
"ObjectMethod",
|
|
32
|
+
"ClassMethod",
|
|
33
|
+
"ClassPrivateMethod",
|
|
34
|
+
]);
|
|
35
|
+
const LOOP_TYPES = new Set(["ForStatement", "ForInStatement", "ForOfStatement", "WhileStatement", "DoWhileStatement"]);
|
|
36
|
+
const atModuleScope = (ancestors) => !ancestors.some((a) => FUNCTION_TYPES.has(a.type));
|
|
37
|
+
const lineOf = (node) => node.loc?.start.line;
|
|
38
|
+
const nameOf = (node) => {
|
|
39
|
+
if (!isNode(node))
|
|
40
|
+
return undefined;
|
|
41
|
+
if (node.type === "Identifier")
|
|
42
|
+
return node.name;
|
|
43
|
+
if (node.type === "StringLiteral")
|
|
44
|
+
return node.value;
|
|
45
|
+
return undefined;
|
|
46
|
+
};
|
|
47
|
+
const propertyNamed = (object, name) => object.properties.find((p) => (p.type === "ObjectProperty" || p.type === "ObjectMethod") && !p.computed && nameOf(p.key) === name);
|
|
48
|
+
const unwrap = (node) => node.type === "TSNonNullExpression" || node.type === "TSAsExpression" || node.type === "TSSatisfiesExpression"
|
|
49
|
+
? unwrap(node.expression)
|
|
50
|
+
: node;
|
|
51
|
+
/** `a.b(…).c(…)`: the calls from the outside in, the member names, and the root identifier. */
|
|
52
|
+
const chainOf = (start) => {
|
|
53
|
+
const calls = [];
|
|
54
|
+
const members = [];
|
|
55
|
+
let current = start;
|
|
56
|
+
while (current) {
|
|
57
|
+
current = unwrap(current);
|
|
58
|
+
if (current.type === "CallExpression") {
|
|
59
|
+
calls.push(current);
|
|
60
|
+
current = current.callee;
|
|
61
|
+
}
|
|
62
|
+
else if (current.type === "MemberExpression" && !current.computed) {
|
|
63
|
+
const member = nameOf(current.property);
|
|
64
|
+
if (member === undefined)
|
|
65
|
+
return { calls, members };
|
|
66
|
+
members.push(member);
|
|
67
|
+
current = current.object;
|
|
68
|
+
}
|
|
69
|
+
else if (current.type === "Identifier") {
|
|
70
|
+
return { root: current.name, calls, members };
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
return { calls, members };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return { calls, members };
|
|
77
|
+
};
|
|
78
|
+
const isHandlerCall = (node) => node.type === "CallExpression" && isNode(node.callee) && node.callee.type === "MemberExpression" && nameOf(node.callee.property) === "handler";
|
|
79
|
+
const BUILDER_MEMBERS = new Set(["handler", "validator", "inputValidator", "middleware"]);
|
|
80
|
+
/** The call that hands the factory its options: `signedInFn({ … })`, not `.validator(…)`. */
|
|
81
|
+
const optionsCall = (calls) => calls.find((call) => {
|
|
82
|
+
const callee = call.callee;
|
|
83
|
+
return !(callee.type === "MemberExpression" && BUILDER_MEMBERS.has(nameOf(callee.property) ?? ""));
|
|
84
|
+
});
|
|
85
|
+
const hasMethodOption = (calls) => {
|
|
86
|
+
const call = optionsCall(calls);
|
|
87
|
+
const options = call && call.arguments[0];
|
|
88
|
+
return !!options && options.type === "ObjectExpression" && !!propertyNamed(options, "method");
|
|
89
|
+
};
|
|
90
|
+
const isTopLevelDeclarator = (ancestors) => {
|
|
91
|
+
const [declarator, declaration, outer, program] = ancestors.slice(-4).reverse();
|
|
92
|
+
if (declarator?.type !== "VariableDeclarator" || declaration?.type !== "VariableDeclaration")
|
|
93
|
+
return false;
|
|
94
|
+
return outer?.type === "Program" || (outer?.type === "ExportNamedDeclaration" && program?.type === "Program");
|
|
95
|
+
};
|
|
96
|
+
// TypeScript nodes that hold runtime code; under any other TS node a name is a type.
|
|
97
|
+
const VALUE_TS_NODES = new Set([
|
|
98
|
+
"TSAsExpression",
|
|
99
|
+
"TSSatisfiesExpression",
|
|
100
|
+
"TSNonNullExpression",
|
|
101
|
+
"TSInstantiationExpression",
|
|
102
|
+
"TSTypeAssertion",
|
|
103
|
+
"TSEnumDeclaration",
|
|
104
|
+
"TSEnumBody",
|
|
105
|
+
"TSEnumMember",
|
|
106
|
+
"TSModuleDeclaration",
|
|
107
|
+
"TSModuleBlock",
|
|
108
|
+
"TSExportAssignment",
|
|
109
|
+
"TSParameterProperty",
|
|
110
|
+
]);
|
|
111
|
+
const isPropertyName = (node, parent) => !!parent &&
|
|
112
|
+
!parent.computed &&
|
|
113
|
+
(parent.key === node ||
|
|
114
|
+
(parent.property === node && ["MemberExpression", "OptionalMemberExpression", "JSXMemberExpression"].includes(parent.type)) ||
|
|
115
|
+
(parent.type === "ExportSpecifier" && parent.exported === node) ||
|
|
116
|
+
parent.type === "JSXAttribute");
|
|
117
|
+
// `export type { T }` and `export { type T }`, which the build erases with the import.
|
|
118
|
+
const isTypeExport = (ancestors) => {
|
|
119
|
+
const [specifier, declaration] = [ancestors.at(-1), ancestors.at(-2)];
|
|
120
|
+
return specifier?.type === "ExportSpecifier" && (specifier.exportKind === "type" || declaration?.exportKind === "type");
|
|
121
|
+
};
|
|
122
|
+
/** Each name the file reads as a value, outside its imports. */
|
|
123
|
+
const forEachValueName = (program, visit) => {
|
|
124
|
+
for (const statement of program.body) {
|
|
125
|
+
if (statement.type === "ImportDeclaration")
|
|
126
|
+
continue;
|
|
127
|
+
walk(statement, (node, ancestors) => {
|
|
128
|
+
if (node.type !== "Identifier" && node.type !== "JSXIdentifier")
|
|
129
|
+
return;
|
|
130
|
+
if (isPropertyName(node, ancestors.at(-1)) || isTypeExport(ancestors))
|
|
131
|
+
return;
|
|
132
|
+
if (ancestors.some((a) => a.type.startsWith("TS") && !VALUE_TS_NODES.has(a.type)))
|
|
133
|
+
return;
|
|
134
|
+
visit(node, ancestors);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
const valueNames = (program) => {
|
|
139
|
+
const names = new Set();
|
|
140
|
+
forEachValueName(program, (node) => names.add(node.name));
|
|
141
|
+
return names;
|
|
142
|
+
};
|
|
143
|
+
/** What a file reads off a namespace import, `ns.name` or `const { name } = ns`; "all" when it uses it any other way. */
|
|
144
|
+
const namespaceReads = (file, namespace) => {
|
|
145
|
+
const names = [];
|
|
146
|
+
let all = false;
|
|
147
|
+
forEachValueName(file.program, (node, ancestors) => {
|
|
148
|
+
if (node.name !== namespace)
|
|
149
|
+
return;
|
|
150
|
+
const parent = ancestors.at(-1);
|
|
151
|
+
const pattern = parent?.type === "VariableDeclarator" && parent.init === node ? parent.id : undefined;
|
|
152
|
+
if (parent && ["MemberExpression", "OptionalMemberExpression", "JSXMemberExpression"].includes(parent.type) && parent.object === node && !parent.computed) {
|
|
153
|
+
names.push(parent.property.name);
|
|
154
|
+
}
|
|
155
|
+
else if (pattern?.type === "ObjectPattern") {
|
|
156
|
+
for (const property of pattern.properties) {
|
|
157
|
+
const key = property.type === "ObjectProperty" && !property.computed ? nameOf(property.key) : undefined;
|
|
158
|
+
if (key)
|
|
159
|
+
names.push(key);
|
|
160
|
+
else
|
|
161
|
+
all = true;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
all = true;
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
return all ? "all" : names;
|
|
169
|
+
};
|
|
170
|
+
const parseFile = (path, code) => {
|
|
171
|
+
try {
|
|
172
|
+
const ast = parse(code, {
|
|
173
|
+
sourceType: "module",
|
|
174
|
+
plugins: path.endsWith("x") ? ["typescript", "jsx"] : ["typescript"],
|
|
175
|
+
errorRecovery: true,
|
|
176
|
+
});
|
|
177
|
+
const imports = new Map();
|
|
178
|
+
for (const statement of ast.program.body) {
|
|
179
|
+
if (statement.type !== "ImportDeclaration")
|
|
180
|
+
continue;
|
|
181
|
+
const source = statement.source.value;
|
|
182
|
+
for (const specifier of statement.specifiers) {
|
|
183
|
+
const local = nameOf(specifier.local);
|
|
184
|
+
if (!local)
|
|
185
|
+
continue;
|
|
186
|
+
const imported = specifier.type === "ImportSpecifier"
|
|
187
|
+
? (nameOf(specifier.imported) ?? local)
|
|
188
|
+
: specifier.type === "ImportNamespaceSpecifier"
|
|
189
|
+
? "*"
|
|
190
|
+
: "default";
|
|
191
|
+
imports.set(local, { source, imported });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return { path, program: ast.program, imports, values: valueNames(ast.program) };
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
return undefined;
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
const FACTORIES = ["signedInFn", "publicFn", "createServerFn"];
|
|
201
|
+
/** The names a file declares at the top level, or re-exports with `export … from`. */
|
|
202
|
+
const declaredNames = (file) => {
|
|
203
|
+
const names = new Set();
|
|
204
|
+
for (const statement of file.program.body) {
|
|
205
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
206
|
+
if (declaration?.type === "VariableDeclaration") {
|
|
207
|
+
for (const declarator of declaration.declarations)
|
|
208
|
+
names.add(nameOf(declarator.id) ?? "");
|
|
209
|
+
}
|
|
210
|
+
if (statement.type === "ExportNamedDeclaration" && statement.source) {
|
|
211
|
+
for (const specifier of statement.specifiers)
|
|
212
|
+
names.add(nameOf(specifier.exported) ?? "");
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return names;
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* The names a server function can start from, each with the factory it comes
|
|
219
|
+
* from: zitejs's, TanStack's raw one, and the app's own built from them
|
|
220
|
+
* (`export const managerFn = signedInFn.middleware([role("manager")])`). A name
|
|
221
|
+
* is the file's own, or the one its import names in the app file declaring it;
|
|
222
|
+
* only a name no app file is known to declare (a factory, a name behind
|
|
223
|
+
* `export *`) is looked up app-wide.
|
|
224
|
+
*/
|
|
225
|
+
const factoryNames = (files, paths) => {
|
|
226
|
+
const byName = new Map(FACTORIES.map((factory) => [factory, { factory, method: false }]));
|
|
227
|
+
const declared = new Map(files.map((file) => [file.path, declaredNames(file)]));
|
|
228
|
+
const byFile = new Map(files.map((file) => [file.path, new Map()]));
|
|
229
|
+
const exportedBy = (from, source, name) => {
|
|
230
|
+
const target = resolveImport(from, source, paths);
|
|
231
|
+
return target && declared.get(target).has(name) ? byFile.get(target).get(name) : byName.get(name);
|
|
232
|
+
};
|
|
233
|
+
const builderOf = (file, name) => {
|
|
234
|
+
if (!name)
|
|
235
|
+
return undefined;
|
|
236
|
+
if (declared.get(file.path).has(name))
|
|
237
|
+
return byFile.get(file.path).get(name);
|
|
238
|
+
const imported = file.imports.get(name);
|
|
239
|
+
return imported ? exportedBy(file.path, imported.source, imported.imported) : byName.get(name);
|
|
240
|
+
};
|
|
241
|
+
const add = (file, name, builder) => {
|
|
242
|
+
byFile.get(file.path).set(name, builder);
|
|
243
|
+
if (!byName.has(name))
|
|
244
|
+
byName.set(name, builder);
|
|
245
|
+
};
|
|
246
|
+
for (let changed = true; changed;) {
|
|
247
|
+
changed = false;
|
|
248
|
+
for (const file of files) {
|
|
249
|
+
const own = byFile.get(file.path);
|
|
250
|
+
for (const statement of file.program.body) {
|
|
251
|
+
if (statement.type === "ExportNamedDeclaration" && statement.source) {
|
|
252
|
+
for (const specifier of statement.specifiers) {
|
|
253
|
+
const exported = nameOf(specifier.exported);
|
|
254
|
+
if (!exported || own.has(exported))
|
|
255
|
+
continue;
|
|
256
|
+
const builder = exportedBy(file.path, statement.source.value, nameOf(specifier.local) ?? "");
|
|
257
|
+
if (builder) {
|
|
258
|
+
add(file, exported, builder);
|
|
259
|
+
changed = true;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
264
|
+
if (declaration?.type !== "VariableDeclaration")
|
|
265
|
+
continue;
|
|
266
|
+
for (const declarator of declaration.declarations) {
|
|
267
|
+
const name = nameOf(declarator.id);
|
|
268
|
+
const init = declarator.init;
|
|
269
|
+
if (!name || !init || own.has(name) || isHandlerCall(unwrap(init)))
|
|
270
|
+
continue;
|
|
271
|
+
const { root, calls } = chainOf(init);
|
|
272
|
+
const parent = builderOf(file, root);
|
|
273
|
+
if (parent) {
|
|
274
|
+
add(file, name, { factory: parent.factory, method: parent.method || hasMethodOption(calls) });
|
|
275
|
+
changed = true;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return builderOf;
|
|
282
|
+
};
|
|
283
|
+
/** A file's server functions: its top-level consts that are a factory's `.handler(…)`. */
|
|
284
|
+
const serverFnNames = (file, factories) => {
|
|
285
|
+
const names = new Set();
|
|
286
|
+
for (const statement of file.program.body) {
|
|
287
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
288
|
+
if (declaration?.type !== "VariableDeclaration")
|
|
289
|
+
continue;
|
|
290
|
+
for (const declarator of declaration.declarations) {
|
|
291
|
+
const name = nameOf(declarator.id);
|
|
292
|
+
const init = declarator.init ? unwrap(declarator.init) : undefined;
|
|
293
|
+
const root = init && isHandlerCall(init) ? chainOf(init).root : undefined;
|
|
294
|
+
if (name && factories(file, root))
|
|
295
|
+
names.add(name);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return names;
|
|
299
|
+
};
|
|
300
|
+
/** The server functions a module exports, by exported name, followed through its re-exports. */
|
|
301
|
+
const exportedServerFns = (files, paths, serverFns) => {
|
|
302
|
+
const byPath = new Map(files.map((file) => [file.path, file]));
|
|
303
|
+
const found = new Map();
|
|
304
|
+
const exportsOf = (path) => {
|
|
305
|
+
const known = found.get(path);
|
|
306
|
+
if (known)
|
|
307
|
+
return known;
|
|
308
|
+
const names = new Set();
|
|
309
|
+
found.set(path, names);
|
|
310
|
+
const file = byPath.get(path);
|
|
311
|
+
const local = serverFns.get(path);
|
|
312
|
+
const exportsAt = (source) => {
|
|
313
|
+
const target = resolveImport(path, source, paths);
|
|
314
|
+
return target ? exportsOf(target) : new Set();
|
|
315
|
+
};
|
|
316
|
+
for (const statement of file.program.body) {
|
|
317
|
+
if (statement.exportKind === "type")
|
|
318
|
+
continue;
|
|
319
|
+
if (statement.type === "ExportAllDeclaration") {
|
|
320
|
+
for (const name of exportsAt(statement.source.value))
|
|
321
|
+
if (name !== "default")
|
|
322
|
+
names.add(name);
|
|
323
|
+
}
|
|
324
|
+
if (statement.type !== "ExportNamedDeclaration")
|
|
325
|
+
continue;
|
|
326
|
+
const declaration = statement.declaration;
|
|
327
|
+
if (declaration?.type === "VariableDeclaration") {
|
|
328
|
+
for (const declarator of declaration.declarations) {
|
|
329
|
+
const name = nameOf(declarator.id);
|
|
330
|
+
if (name && local.has(name))
|
|
331
|
+
names.add(name);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
const reexported = statement.source ? exportsAt(statement.source.value) : undefined;
|
|
335
|
+
for (const specifier of statement.specifiers) {
|
|
336
|
+
if (specifier.type !== "ExportSpecifier" || specifier.exportKind === "type")
|
|
337
|
+
continue;
|
|
338
|
+
const name = nameOf(specifier.local) ?? "";
|
|
339
|
+
const imported = file.imports.get(name);
|
|
340
|
+
const serverFn = reexported
|
|
341
|
+
? reexported.has(name)
|
|
342
|
+
: local.has(name) || (!!imported && exportsAt(imported.source).has(imported.imported));
|
|
343
|
+
if (serverFn)
|
|
344
|
+
names.add(nameOf(specifier.exported) ?? name);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return names;
|
|
348
|
+
};
|
|
349
|
+
return exportsOf;
|
|
350
|
+
};
|
|
351
|
+
const OWNER_INPUTS = ["userId", "ownerId"];
|
|
352
|
+
/** An owner id a function's validator takes as input: `.validator(z.object({ userId: … }))`. */
|
|
353
|
+
const ownerInput = (calls) => {
|
|
354
|
+
for (const call of calls) {
|
|
355
|
+
const callee = call.callee;
|
|
356
|
+
if (callee.type !== "MemberExpression" || !["validator", "inputValidator"].includes(nameOf(callee.property) ?? ""))
|
|
357
|
+
continue;
|
|
358
|
+
const schema = call.arguments[0];
|
|
359
|
+
if (schema?.type !== "CallExpression" || nameOf(schema.callee.property) !== "object")
|
|
360
|
+
continue;
|
|
361
|
+
const shape = schema.arguments[0];
|
|
362
|
+
const owner = shape?.type === "ObjectExpression" ? OWNER_INPUTS.find((key) => propertyNamed(shape, key)) : undefined;
|
|
363
|
+
if (owner)
|
|
364
|
+
return owner;
|
|
365
|
+
}
|
|
366
|
+
return undefined;
|
|
367
|
+
};
|
|
368
|
+
const callsCreateWorkflow = (program) => {
|
|
369
|
+
let found = false;
|
|
370
|
+
walk(program, (node) => {
|
|
371
|
+
if (node.type === "CallExpression" && nameOf(node.callee) === "createAutomation")
|
|
372
|
+
found = true;
|
|
373
|
+
});
|
|
374
|
+
return found;
|
|
375
|
+
};
|
|
376
|
+
const isServerReachable = (path, callsWorkflow) => /\.(functions|server)\.tsx?$/.test(path) || path.startsWith("src/routes/api/") || path === "src/start.ts" || callsWorkflow;
|
|
377
|
+
/**
|
|
378
|
+
* Whether an imported name survives to the build's resolver. In a TypeScript
|
|
379
|
+
* file, the transform drops a name the file reads only as a type, or not at all,
|
|
380
|
+
* written with `type` or not; a .js or .jsx file keeps every import.
|
|
381
|
+
*/
|
|
382
|
+
const keepsImport = (file, local) => !/\.[cm]?tsx?$/.test(file.path) || file.values.has(local);
|
|
383
|
+
/** The names an import brings in as values. */
|
|
384
|
+
const valueImports = (file, statement) => {
|
|
385
|
+
if (statement.importKind === "type")
|
|
386
|
+
return [];
|
|
387
|
+
const specifiers = statement.specifiers.filter((s) => s.importKind !== "type" && keepsImport(file, nameOf(s.local) ?? ""));
|
|
388
|
+
if (statement.specifiers.length === 0)
|
|
389
|
+
return "all";
|
|
390
|
+
if (specifiers.some((s) => s.type !== "ImportSpecifier"))
|
|
391
|
+
return "all";
|
|
392
|
+
return specifiers.map((s) => nameOf(s.imported) ?? "");
|
|
393
|
+
};
|
|
394
|
+
/** The names an import reads as values, by the names its module exports them under; "all" when it can't tell. */
|
|
395
|
+
const importedReads = (file, statement) => {
|
|
396
|
+
if (statement.importKind === "type")
|
|
397
|
+
return [];
|
|
398
|
+
const names = [];
|
|
399
|
+
for (const specifier of statement.specifiers) {
|
|
400
|
+
const local = nameOf(specifier.local) ?? "";
|
|
401
|
+
if (specifier.importKind === "type" || !file.values.has(local))
|
|
402
|
+
continue;
|
|
403
|
+
if (specifier.type === "ImportSpecifier")
|
|
404
|
+
names.push(nameOf(specifier.imported) ?? local);
|
|
405
|
+
if (specifier.type === "ImportNamespaceSpecifier") {
|
|
406
|
+
const reads = namespaceReads(file, local);
|
|
407
|
+
if (reads === "all")
|
|
408
|
+
return "all";
|
|
409
|
+
names.push(...reads);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return names;
|
|
413
|
+
};
|
|
414
|
+
/** The names an `export … from` passes on as values; undefined for any other statement. */
|
|
415
|
+
const valueReexports = (statement) => {
|
|
416
|
+
if ((statement.type !== "ExportNamedDeclaration" && statement.type !== "ExportAllDeclaration") || !statement.source)
|
|
417
|
+
return undefined;
|
|
418
|
+
if (statement.exportKind === "type")
|
|
419
|
+
return [];
|
|
420
|
+
if (statement.type === "ExportAllDeclaration")
|
|
421
|
+
return "all";
|
|
422
|
+
return statement.specifiers.filter((s) => s.exportKind !== "type").map((s) => nameOf(s.exported) ?? "");
|
|
423
|
+
};
|
|
424
|
+
const MUTATING_METHODS = new Set(["set", "add", "push", "unshift", "splice", "delete", "clear"]);
|
|
425
|
+
const isContainer = (init) => !!init &&
|
|
426
|
+
((init.type === "NewExpression" && ["Map", "Set"].includes(nameOf(init.callee) ?? "")) ||
|
|
427
|
+
init.type === "ArrayExpression" ||
|
|
428
|
+
init.type === "ObjectExpression");
|
|
429
|
+
const callsAny = (node, matches) => {
|
|
430
|
+
let found = false;
|
|
431
|
+
walk(node, (n) => {
|
|
432
|
+
if (n.type === "CallExpression" && isNode(n.callee) && matches(n.callee))
|
|
433
|
+
found = true;
|
|
434
|
+
});
|
|
435
|
+
return found;
|
|
436
|
+
};
|
|
437
|
+
const ROOT_LAYOUTS = new Set(["src/routes/__root.tsx", "src/routes/_app.tsx", "src/routes/_app/route.tsx"]);
|
|
438
|
+
const CLASSIC_IMPORTS = {
|
|
439
|
+
"zitejs/backend": "server code is a signedInFn/publicFn server function from zitejs/start",
|
|
440
|
+
"zitejs/api": "pages call server functions directly, from a loader or an event handler",
|
|
441
|
+
"react-router-dom": "routing is file routes, with Link and useNavigate from @tanstack/react-router",
|
|
442
|
+
"react-router": "routing is file routes, with Link and useNavigate from @tanstack/react-router",
|
|
443
|
+
};
|
|
444
|
+
const importsModule = (source, module) => source === module || source.startsWith(`${module}/`);
|
|
445
|
+
const containsTypeofWindow = (node) => {
|
|
446
|
+
let found = false;
|
|
447
|
+
walk(node, (n) => {
|
|
448
|
+
if (n.type === "UnaryExpression" && n.operator === "typeof" && nameOf(n.argument) === "window")
|
|
449
|
+
found = true;
|
|
450
|
+
});
|
|
451
|
+
return found;
|
|
452
|
+
};
|
|
453
|
+
const GUARD_TYPES = new Set(["IfStatement", "ConditionalExpression", "LogicalExpression"]);
|
|
454
|
+
const guardedByTypeofWindow = (ancestors) => ancestors.some((a) => GUARD_TYPES.has(a.type) && containsTypeofWindow((a.test ?? a.left)));
|
|
455
|
+
const isRelativePath = (node) => (node?.type === "StringLiteral" && node.value.startsWith("/")) ||
|
|
456
|
+
(node?.type === "TemplateLiteral" && (node.quasis[0]?.value?.cooked ?? "").startsWith("/"));
|
|
457
|
+
/** A top-level `const` in the file, so `{ server: serverOptions }` reads as written. */
|
|
458
|
+
const resolveConst = (file, node) => {
|
|
459
|
+
if (!node)
|
|
460
|
+
return undefined;
|
|
461
|
+
const value = unwrap(node);
|
|
462
|
+
if (value.type !== "Identifier")
|
|
463
|
+
return value;
|
|
464
|
+
for (const statement of file.program.body) {
|
|
465
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
466
|
+
if (declaration?.type !== "VariableDeclaration" || declaration.kind !== "const")
|
|
467
|
+
continue;
|
|
468
|
+
const declarator = declaration.declarations.find((d) => nameOf(d.id) === value.name);
|
|
469
|
+
if (declarator?.init)
|
|
470
|
+
return unwrap(declarator.init);
|
|
471
|
+
}
|
|
472
|
+
return undefined;
|
|
473
|
+
};
|
|
474
|
+
/** An object's property, read through a top-level const; undefined when absent, null when there but unreadable. */
|
|
475
|
+
const readProperty = (file, object, name) => {
|
|
476
|
+
if (object.properties.some((p) => p.type === "SpreadElement"))
|
|
477
|
+
return null;
|
|
478
|
+
const property = propertyNamed(object, name);
|
|
479
|
+
if (!property)
|
|
480
|
+
return undefined;
|
|
481
|
+
return (property.type === "ObjectMethod" ? property : resolveConst(file, property.value)) ?? null;
|
|
482
|
+
};
|
|
483
|
+
// The platform's, as describe's rule had them: the host, TanStack and Cloudflare answer these before the app does.
|
|
484
|
+
const RESERVED_PATHS = [
|
|
485
|
+
/^\/_zite/,
|
|
486
|
+
/^\/__zite/,
|
|
487
|
+
/^\/_serverFn(\/|$)/,
|
|
488
|
+
/^\/cdn-cgi(\/|$)/,
|
|
489
|
+
/^\/auth(\/|$)/,
|
|
490
|
+
/^\/api\/auth(\/|$)/,
|
|
491
|
+
/^\/manifest\.webmanifest$/,
|
|
492
|
+
];
|
|
493
|
+
/** What one middleware grants, as zitejs/start names it: `role()` signs in. Undefined when it isn't one of those. */
|
|
494
|
+
const accessOf = (file, node) => {
|
|
495
|
+
const value = unwrap(node);
|
|
496
|
+
const fromZitejs = (local) => {
|
|
497
|
+
const imported = local ? file.imports.get(local) : undefined;
|
|
498
|
+
return imported?.source === "zitejs/start" ? imported.imported : undefined;
|
|
499
|
+
};
|
|
500
|
+
const name = value.type === "Identifier"
|
|
501
|
+
? fromZitejs(value.name)
|
|
502
|
+
: value.type === "MemberExpression"
|
|
503
|
+
? fromZitejs(nameOf(value.object))
|
|
504
|
+
: value.type === "CallExpression"
|
|
505
|
+
? fromZitejs(nameOf(value.callee))
|
|
506
|
+
: undefined;
|
|
507
|
+
if (name === "role")
|
|
508
|
+
return value.type === "CallExpression" ? "signedIn" : undefined;
|
|
509
|
+
if (value.type === "MemberExpression")
|
|
510
|
+
return name === "webhook" ? "webhook" : undefined;
|
|
511
|
+
return name === "signedIn" || name === "anyone" || name === "webhook" ? name : undefined;
|
|
512
|
+
};
|
|
513
|
+
/** A middleware list's access, and whether anything in it is the app's own, which only the build can read. */
|
|
514
|
+
const middlewareAccess = (file, node) => {
|
|
515
|
+
if (node === undefined)
|
|
516
|
+
return { access: [], unread: false };
|
|
517
|
+
const list = node && resolveConst(file, node);
|
|
518
|
+
if (list?.type !== "ArrayExpression")
|
|
519
|
+
return { access: [], unread: true };
|
|
520
|
+
const access = [];
|
|
521
|
+
let unread = false;
|
|
522
|
+
for (const element of list.elements) {
|
|
523
|
+
const granted = element && element.type !== "SpreadElement" ? accessOf(file, element) : undefined;
|
|
524
|
+
if (granted)
|
|
525
|
+
access.push(granted);
|
|
526
|
+
else
|
|
527
|
+
unread = true;
|
|
528
|
+
}
|
|
529
|
+
return { access, unread };
|
|
530
|
+
};
|
|
531
|
+
/** `createFileRoute('/id')({ … })` in each route file; the root route as id "". */
|
|
532
|
+
const fileRoutes = (files) => files.flatMap((file) => {
|
|
533
|
+
if (!file.path.startsWith("src/routes/"))
|
|
534
|
+
return [];
|
|
535
|
+
const found = [];
|
|
536
|
+
walk(file.program, (node) => {
|
|
537
|
+
if (node.type !== "CallExpression" || !isNode(node.callee) || node.callee.type !== "CallExpression")
|
|
538
|
+
return;
|
|
539
|
+
const factory = nameOf(node.callee.callee);
|
|
540
|
+
const literal = node.callee.arguments[0];
|
|
541
|
+
const id = factory === "createFileRoute" && literal?.type === "StringLiteral"
|
|
542
|
+
? literal.value
|
|
543
|
+
: factory === "createRootRoute" || factory === "createRootRouteWithContext"
|
|
544
|
+
? ""
|
|
545
|
+
: undefined;
|
|
546
|
+
if (id === undefined)
|
|
547
|
+
return;
|
|
548
|
+
const options = resolveConst(file, node.arguments[0]);
|
|
549
|
+
found.push({ file, id, node, ...(options?.type === "ObjectExpression" && { options }) });
|
|
550
|
+
});
|
|
551
|
+
return found;
|
|
552
|
+
});
|
|
553
|
+
const segments = (id) => id.split("/").filter(Boolean);
|
|
554
|
+
// An index route ('/api/') sits beside its siblings, as a child of the route at its path ('/api').
|
|
555
|
+
const isAncestor = (ancestor, id) => {
|
|
556
|
+
if (ancestor.endsWith("/"))
|
|
557
|
+
return false;
|
|
558
|
+
const outer = segments(ancestor);
|
|
559
|
+
const inner = id.endsWith("/") ? [...segments(id), ""] : segments(id);
|
|
560
|
+
return outer.length < inner.length && outer.every((segment, i) => segment === inner[i]);
|
|
561
|
+
};
|
|
562
|
+
/** As TanStack shows a route's path: no pathless layouts or groups. */
|
|
563
|
+
const fullPathOf = (id) => {
|
|
564
|
+
const path = `/${segments(id).filter((s) => !s.startsWith("_") && !/^\(.*\)$/.test(s)).join("/")}`;
|
|
565
|
+
return id.endsWith("/") && path !== "/" ? `${path}/` : path;
|
|
566
|
+
};
|
|
567
|
+
/** A server route's methods, each with its own middleware; null when the handlers can't be read from source. */
|
|
568
|
+
const routeMethods = (file, handlers) => {
|
|
569
|
+
let object = handlers;
|
|
570
|
+
if (FUNCTION_TYPES.has(handlers.type)) {
|
|
571
|
+
// `handlers: ({ createHandlers }) => createHandlers({ … })`
|
|
572
|
+
const body = handlers.body;
|
|
573
|
+
const call = body.type === "CallExpression" ? body : undefined;
|
|
574
|
+
object = call && nameOf(call.callee) === "createHandlers" ? resolveConst(file, call.arguments[0]) : undefined;
|
|
575
|
+
}
|
|
576
|
+
if (object?.type !== "ObjectExpression")
|
|
577
|
+
return null;
|
|
578
|
+
const methods = [];
|
|
579
|
+
for (const property of object.properties) {
|
|
580
|
+
const method = property.type === "SpreadElement" ? undefined : nameOf(property.key);
|
|
581
|
+
if (!method)
|
|
582
|
+
return null;
|
|
583
|
+
const value = property.type === "ObjectMethod" ? property : resolveConst(file, property.value);
|
|
584
|
+
if (!value)
|
|
585
|
+
return null;
|
|
586
|
+
methods.push({ method, middleware: value.type === "ObjectExpression" ? readProperty(file, value, "middleware") : undefined });
|
|
587
|
+
}
|
|
588
|
+
return methods;
|
|
589
|
+
};
|
|
590
|
+
/** What the build's describe refuses or warns about a route, where the source shows it for sure. */
|
|
591
|
+
const routeProblems = (files, internal) => {
|
|
592
|
+
const problems = [];
|
|
593
|
+
const routes = fileRoutes(files);
|
|
594
|
+
const report = (rule, severity, route, message) => problems.push({ rule, severity, file: route.file.path, ...(lineOf(route.node) && { line: lineOf(route.node) }), message });
|
|
595
|
+
for (const route of routes) {
|
|
596
|
+
const path = fullPathOf(route.id);
|
|
597
|
+
if (route.id && RESERVED_PATHS.some((reserved) => reserved.test(path))) {
|
|
598
|
+
report("reserved-path", "error", route, `${path} is the platform's; give the route another path`);
|
|
599
|
+
}
|
|
600
|
+
const server = route.options && readProperty(route.file, route.options, "server");
|
|
601
|
+
if (!server || server.type !== "ObjectExpression")
|
|
602
|
+
continue;
|
|
603
|
+
const handlers = readProperty(route.file, server, "handlers");
|
|
604
|
+
if (!handlers)
|
|
605
|
+
continue;
|
|
606
|
+
const methods = routeMethods(route.file, handlers);
|
|
607
|
+
if (!methods)
|
|
608
|
+
continue;
|
|
609
|
+
const subject = `route ${path}`;
|
|
610
|
+
const staticData = readProperty(route.file, route.options, "staticData");
|
|
611
|
+
const description = staticData?.type === "ObjectExpression" ? readProperty(route.file, staticData, "description") : staticData;
|
|
612
|
+
if (description === undefined) {
|
|
613
|
+
report("description-missing", "warning", route, `${subject} has no description; add staticData: { description } for the editor`);
|
|
614
|
+
}
|
|
615
|
+
// Middleware applies from the root down: every route this one sits under, then its own, then the method's.
|
|
616
|
+
const inherited = routes
|
|
617
|
+
.filter((other) => other !== route && (other.id === "" || isAncestor(other.id, route.id)))
|
|
618
|
+
.map((other) => {
|
|
619
|
+
const otherServer = other.options ? readProperty(other.file, other.options, "server") : undefined;
|
|
620
|
+
if (otherServer === undefined)
|
|
621
|
+
return { access: [], unread: !other.options };
|
|
622
|
+
if (otherServer?.type !== "ObjectExpression")
|
|
623
|
+
return { access: [], unread: true };
|
|
624
|
+
return middlewareAccess(other.file, readProperty(other.file, otherServer, "middleware"));
|
|
625
|
+
});
|
|
626
|
+
const own = middlewareAccess(route.file, readProperty(route.file, server, "middleware"));
|
|
627
|
+
const publicMethods = [];
|
|
628
|
+
for (const { method, middleware } of methods) {
|
|
629
|
+
const chain = [...inherited, own, middlewareAccess(route.file, middleware)];
|
|
630
|
+
const unread = chain.some((link) => link.unread);
|
|
631
|
+
const access = [...new Set(chain.flatMap((link) => link.access))];
|
|
632
|
+
if (access.length === 0) {
|
|
633
|
+
if (!unread)
|
|
634
|
+
report("access-missing", "error", route, `${subject} ${method}: no signedIn, anyone or webhook middleware in its chain`);
|
|
635
|
+
}
|
|
636
|
+
else if (access.length > 1) {
|
|
637
|
+
report("access-conflict", "error", route, `${subject} ${method}: ${access.join(" + ")}`);
|
|
638
|
+
}
|
|
639
|
+
else if (access[0] === "anyone" && !unread) {
|
|
640
|
+
publicMethods.push(method);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
if (internal && publicMethods.length > 0) {
|
|
644
|
+
report("public-in-internal", "error", route, `${subject} lets anyone call ${publicMethods.join(", ")}, and this internal app signs everyone in; use signedIn`);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
return problems;
|
|
648
|
+
};
|
|
649
|
+
/** The workflows an app exports, by id, as the build loads them: every module that calls createAutomation. */
|
|
650
|
+
const workflowProblems = (files, kind) => {
|
|
651
|
+
const problems = [];
|
|
652
|
+
const exported = [];
|
|
653
|
+
for (const file of files) {
|
|
654
|
+
if (!callsCreateWorkflow(file.program))
|
|
655
|
+
continue;
|
|
656
|
+
const createCall = (init) => {
|
|
657
|
+
if (!init)
|
|
658
|
+
return undefined;
|
|
659
|
+
const { root, calls } = chainOf(init);
|
|
660
|
+
const innermost = calls.at(-1);
|
|
661
|
+
return root && file.imports.get(root)?.imported === "createAutomation" && innermost && nameOf(innermost.callee) === root
|
|
662
|
+
? innermost
|
|
663
|
+
: undefined;
|
|
664
|
+
};
|
|
665
|
+
const workflow = (id, node, call) => {
|
|
666
|
+
exported.push({ id, file: file.path, node });
|
|
667
|
+
const config = resolveConst(file, call.arguments[0]);
|
|
668
|
+
const trigger = config?.type === "ObjectExpression" ? readProperty(file, config, "trigger") : undefined;
|
|
669
|
+
if (trigger?.type !== "ObjectExpression")
|
|
670
|
+
return;
|
|
671
|
+
if (["table", "form"].some((key) => propertyNamed(trigger, key))) {
|
|
672
|
+
problems.push({
|
|
673
|
+
rule: "trigger-unsupported",
|
|
674
|
+
severity: "error",
|
|
675
|
+
file: file.path,
|
|
676
|
+
...(lineOf(node) && { line: lineOf(node) }),
|
|
677
|
+
message: `automation ${id}: a Start automation can't take a database or form trigger yet`,
|
|
678
|
+
});
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
if (["every", "events", "subscribe"].some((key) => readProperty(file, trigger, key) !== undefined))
|
|
682
|
+
return;
|
|
683
|
+
problems.push({
|
|
684
|
+
rule: "workflow-trigger-unregistered",
|
|
685
|
+
severity: "error",
|
|
686
|
+
file: file.path,
|
|
687
|
+
...(lineOf(node) && { line: lineOf(node) }),
|
|
688
|
+
message: `automation ${id}: a provider trigger needs \`subscribe\` to register it with the provider`,
|
|
689
|
+
});
|
|
690
|
+
};
|
|
691
|
+
const locals = new Map();
|
|
692
|
+
for (const statement of file.program.body) {
|
|
693
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
694
|
+
if (declaration?.type === "VariableDeclaration") {
|
|
695
|
+
for (const declarator of declaration.declarations) {
|
|
696
|
+
const name = nameOf(declarator.id);
|
|
697
|
+
const call = createCall(declarator.init);
|
|
698
|
+
if (!name || !call)
|
|
699
|
+
continue;
|
|
700
|
+
if (statement.type === "ExportNamedDeclaration")
|
|
701
|
+
workflow(name, declarator, call);
|
|
702
|
+
else
|
|
703
|
+
locals.set(name, { node: declarator, call });
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
if (statement.type === "ExportNamedDeclaration" && !statement.source) {
|
|
707
|
+
for (const specifier of statement.specifiers) {
|
|
708
|
+
const local = locals.get(nameOf(specifier.local) ?? "");
|
|
709
|
+
const id = nameOf(specifier.exported);
|
|
710
|
+
if (local && id)
|
|
711
|
+
workflow(id, specifier, local.call);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
if (statement.type === "ExportDefaultDeclaration") {
|
|
715
|
+
const declared = statement.declaration;
|
|
716
|
+
const call = createCall(declared) ?? (declared.type === "Identifier" ? locals.get(declared.name)?.call : undefined);
|
|
717
|
+
if (call) {
|
|
718
|
+
problems.push({
|
|
719
|
+
rule: "workflow-default-export",
|
|
720
|
+
severity: "error",
|
|
721
|
+
file: file.path,
|
|
722
|
+
...(lineOf(statement) && { line: lineOf(statement) }),
|
|
723
|
+
message: "automation default: export it by name, which becomes its id",
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
for (const { id, file, node } of exported) {
|
|
730
|
+
const elsewhere = exported.find((other) => other.id === id && other.file !== file);
|
|
731
|
+
if (elsewhere) {
|
|
732
|
+
problems.push({
|
|
733
|
+
rule: "duplicate-workflow",
|
|
734
|
+
severity: "error",
|
|
735
|
+
file,
|
|
736
|
+
...(lineOf(node) && { line: lineOf(node) }),
|
|
737
|
+
message: `automation ${id}: ${id} is also exported from ${elsewhere.file}`,
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
// A standalone automation is one workflow with one trigger, as classic's is; an app may hold several.
|
|
742
|
+
const webhookImports = files.flatMap((file) => file.program.body
|
|
743
|
+
.filter((statement) => statement.type === "ImportDeclaration" &&
|
|
744
|
+
statement.source.value === "zitejs/start" &&
|
|
745
|
+
statement.importKind !== "type" &&
|
|
746
|
+
statement.specifiers.some((specifier) => nameOf(specifier.imported) === "webhook"))
|
|
747
|
+
.map((statement) => ({ file: file.path, node: statement })));
|
|
748
|
+
const triggers = [
|
|
749
|
+
...exported.map(({ id, file, node }) => ({ label: id, file, node })),
|
|
750
|
+
...webhookImports.map(({ file, node }) => ({ label: `the webhook in ${file}`, file, node })),
|
|
751
|
+
];
|
|
752
|
+
if (kind === "automation" && triggers.length > 1) {
|
|
753
|
+
const second = triggers[1];
|
|
754
|
+
problems.push({
|
|
755
|
+
rule: "one-trigger",
|
|
756
|
+
severity: "error",
|
|
757
|
+
file: second.file,
|
|
758
|
+
...(lineOf(second.node) && { line: lineOf(second.node) }),
|
|
759
|
+
message: `an automation has one trigger, and this one has ${triggers.length} (${triggers.map(({ label }) => label).join(", ")}); a second trigger is a second automation`,
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
return problems;
|
|
763
|
+
};
|
|
764
|
+
// What the build keeps out of the browser (zite()'s importProtection), by the specifier an app writes.
|
|
765
|
+
const SERVER_ONLY_IMPORTS = [/^zitejs\/db$/, /^zitejs\/integrations(\/|$)/, /^zitejs\/email$/, /(^|\/)\.zite\/db(\.ts)?$/, /(^|\/)\.zite\/integrations(\/|$)/];
|
|
766
|
+
const isServerOnlyFile = (path) => /\.server(\.[^/]*)?$/.test(path.split("/").pop() ?? "");
|
|
767
|
+
const SOURCE_EXTENSIONS = ["", ".ts", ".tsx", ".js", ".jsx", ".mts", ".mjs", "/index.ts", "/index.tsx", "/index.js"];
|
|
768
|
+
// As TypeScript and Vite read a .js specifier: the TypeScript file it compiles from.
|
|
769
|
+
const TS_FOR_JS = { ".js": [".ts", ".tsx"], ".jsx": [".tsx"], ".mjs": [".mts"], ".cjs": [".cts"] };
|
|
770
|
+
/** The app file an import names, by relative path or the `~/` and `@/` aliases for src/. */
|
|
771
|
+
const resolveImport = (from, source, paths) => {
|
|
772
|
+
const base = /^[~@]\//.test(source)
|
|
773
|
+
? `src/${source.slice(2)}`
|
|
774
|
+
: source.startsWith(".")
|
|
775
|
+
? posixJoin(from.split("/").slice(0, -1).join("/"), source)
|
|
776
|
+
: undefined;
|
|
777
|
+
if (base === undefined)
|
|
778
|
+
return undefined;
|
|
779
|
+
const js = /\.[cm]?jsx?$/.exec(base)?.[0];
|
|
780
|
+
const typescript = js ? (TS_FOR_JS[js] ?? []).map((extension) => base.slice(0, -js.length) + extension) : [];
|
|
781
|
+
return [...SOURCE_EXTENSIONS.map((extension) => base + extension), ...typescript].find((path) => paths.has(path));
|
|
782
|
+
};
|
|
783
|
+
const posixJoin = (dir, relative) => {
|
|
784
|
+
const parts = dir ? dir.split("/") : [];
|
|
785
|
+
for (const part of relative.split("/")) {
|
|
786
|
+
if (part === "..")
|
|
787
|
+
parts.pop();
|
|
788
|
+
else if (part !== "." && part !== "")
|
|
789
|
+
parts.push(part);
|
|
790
|
+
}
|
|
791
|
+
return parts.join("/");
|
|
792
|
+
};
|
|
793
|
+
/** Whether a file defines server code the build compiles out of the browser's copy, which only the build can follow. */
|
|
794
|
+
const definesServerCode = (file, factories) => {
|
|
795
|
+
let found = false;
|
|
796
|
+
walk(file.program, (node) => {
|
|
797
|
+
if (found)
|
|
798
|
+
return;
|
|
799
|
+
if (node.type === "CallExpression" && isHandlerCall(node)) {
|
|
800
|
+
const { root } = chainOf(node);
|
|
801
|
+
found = !!factories(file, root);
|
|
802
|
+
}
|
|
803
|
+
if ((node.type === "ObjectProperty" || node.type === "ObjectMethod") && nameOf(node.key) === "server")
|
|
804
|
+
found = true;
|
|
805
|
+
});
|
|
806
|
+
return found;
|
|
807
|
+
};
|
|
808
|
+
/**
|
|
809
|
+
* Page code that imports server-only modules, which the build refuses: followed
|
|
810
|
+
* from the routes through plain modules' imports and re-exports, never into a
|
|
811
|
+
* *.functions.* file, whose handlers the browser never gets, nor into or out of
|
|
812
|
+
* a file that defines server code itself.
|
|
813
|
+
*/
|
|
814
|
+
const serverImportProblems = (files, factories, routerPath) => {
|
|
815
|
+
const byPath = new Map(files.map((file) => [file.path, file]));
|
|
816
|
+
const paths = new Set(byPath.keys());
|
|
817
|
+
const problems = [];
|
|
818
|
+
const pageRoots = files.filter((file) => file.path === routerPath || (file.path.startsWith("src/routes/") && !file.path.startsWith("src/routes/api/")));
|
|
819
|
+
const seen = new Set();
|
|
820
|
+
const visit = (file) => {
|
|
821
|
+
if (seen.has(file.path))
|
|
822
|
+
return;
|
|
823
|
+
seen.add(file.path);
|
|
824
|
+
if (definesServerCode(file, factories))
|
|
825
|
+
return;
|
|
826
|
+
for (const statement of file.program.body) {
|
|
827
|
+
const values = statement.type === "ImportDeclaration" ? valueImports(file, statement) : valueReexports(statement);
|
|
828
|
+
if (values === undefined || (values !== "all" && values.length === 0))
|
|
829
|
+
continue;
|
|
830
|
+
const source = statement.source.value;
|
|
831
|
+
const target = resolveImport(file.path, source, paths);
|
|
832
|
+
if (SERVER_ONLY_IMPORTS.some((pattern) => pattern.test(source)) || isServerOnlyFile(target ?? source)) {
|
|
833
|
+
problems.push({
|
|
834
|
+
rule: "server-import",
|
|
835
|
+
severity: "error",
|
|
836
|
+
file: file.path,
|
|
837
|
+
...(lineOf(statement) && { line: lineOf(statement) }),
|
|
838
|
+
message: `${source} runs only on the server, so page code can't import it; call a server function that uses it instead`,
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
else if (target && !/\.functions(\.[^/]*)?$/.test(target)) {
|
|
842
|
+
visit(byPath.get(target));
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
};
|
|
846
|
+
pageRoots.forEach(visit);
|
|
847
|
+
return problems;
|
|
848
|
+
};
|
|
849
|
+
const callsCreateZiteRouter = (file) => callsAny(file.program, (callee) => {
|
|
850
|
+
if (callee.type === "Identifier")
|
|
851
|
+
return (file.imports.get(callee.name)?.imported ?? callee.name) === "createZiteRouter";
|
|
852
|
+
const namespace = callee.type === "MemberExpression" && !callee.computed && nameOf(callee.property) === "createZiteRouter"
|
|
853
|
+
? file.imports.get(nameOf(callee.object) ?? "")
|
|
854
|
+
: undefined;
|
|
855
|
+
return namespace?.imported === "*" && namespace.source === "zitejs/start";
|
|
856
|
+
});
|
|
857
|
+
/** Every source hint for one Start app, given its files by app-relative path. */
|
|
858
|
+
export function sourceProblems(sources, { internal = false, kind } = {}) {
|
|
859
|
+
const files = Object.entries(sources)
|
|
860
|
+
.filter(([path]) => /\.[cm]?[jt]sx?$/.test(path) && !path.endsWith("routeTree.gen.ts") && !path.endsWith(".d.ts"))
|
|
861
|
+
.flatMap(([path, code]) => parseFile(path, code) ?? []);
|
|
862
|
+
const problems = [];
|
|
863
|
+
const report = (rule, severity, file, node, message) => problems.push({ rule, severity, file, ...(node && lineOf(node) && { line: lineOf(node) }), message });
|
|
864
|
+
const serverFnFiles = new Map();
|
|
865
|
+
const paths = new Set(files.map((file) => file.path));
|
|
866
|
+
const factories = factoryNames(files, paths);
|
|
867
|
+
const serverFns = new Map(files.map((file) => [file.path, serverFnNames(file, factories)]));
|
|
868
|
+
const serverFnExports = exportedServerFns(files, paths, serverFns);
|
|
869
|
+
// As Start finds it. One that doesn't parse is the typecheck's to report.
|
|
870
|
+
const routerPath = ENTRY_EXTENSIONS.map((extension) => `src/router${extension}`).find((path) => sources[path] !== undefined);
|
|
871
|
+
const router = files.find((file) => file.path === routerPath);
|
|
872
|
+
if (!routerPath || (router && !callsCreateZiteRouter(router))) {
|
|
873
|
+
problems.push({
|
|
874
|
+
rule: "router-file",
|
|
875
|
+
severity: "error",
|
|
876
|
+
file: routerPath ?? "src/router.tsx",
|
|
877
|
+
message: `export getRouter from ${routerPath ?? "src/router.tsx"}, built with createZiteRouter from zitejs/start, which gives pages the signed-in user`,
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
for (const file of files) {
|
|
881
|
+
const { path, program } = file;
|
|
882
|
+
const callsWorkflow = callsCreateWorkflow(program);
|
|
883
|
+
const serverReachable = isServerReachable(path, callsWorkflow);
|
|
884
|
+
const localServerFns = serverFns.get(path);
|
|
885
|
+
const moduleContainers = new Map();
|
|
886
|
+
for (const statement of program.body) {
|
|
887
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
888
|
+
if (declaration?.type !== "VariableDeclaration")
|
|
889
|
+
continue;
|
|
890
|
+
for (const declarator of declaration.declarations) {
|
|
891
|
+
const name = nameOf(declarator.id);
|
|
892
|
+
const init = declarator.init ? unwrap(declarator.init) : undefined;
|
|
893
|
+
if (!name || !init)
|
|
894
|
+
continue;
|
|
895
|
+
if (serverReachable && declaration.kind === "const" && isContainer(init))
|
|
896
|
+
moduleContainers.set(name, false);
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
for (const directive of program.directives ?? []) {
|
|
900
|
+
if (directive.value.value === "use server") {
|
|
901
|
+
report("use-server", "error", path, directive, "'use server' is Next.js; server code is a signedInFn/publicFn server function");
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
for (const statement of program.body) {
|
|
905
|
+
if (statement.type === "ImportDeclaration") {
|
|
906
|
+
const source = statement.source.value;
|
|
907
|
+
const values = valueImports(file, statement);
|
|
908
|
+
for (const [module, instead] of Object.entries(CLASSIC_IMPORTS)) {
|
|
909
|
+
const refused = values === "all" || values.length > 0;
|
|
910
|
+
if (importsModule(source, module) && refused) {
|
|
911
|
+
report("classic-construct", "error", path, statement, `${module} is classic's; in a Start app, ${instead}`);
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
if (source === "cloudflare:workers") {
|
|
915
|
+
report("cloudflare-env", "error", path, statement, "read the app's environment from process.env, not cloudflare:workers");
|
|
916
|
+
}
|
|
917
|
+
const typeOnly = values !== "all" && values.length === 0;
|
|
918
|
+
// Loading a module is fine without a request; only calling one of its server functions isn't.
|
|
919
|
+
const target = callsWorkflow ? resolveImport(path, source, paths) : undefined;
|
|
920
|
+
const unresolvedFunctions = !target && /\.functions(\.[jt]sx?)?$/.test(source);
|
|
921
|
+
if (callsWorkflow && !typeOnly && (importsModule(source, "@tanstack/react-start/server") || unresolvedFunctions)) {
|
|
922
|
+
report("workflow-imports", "error", path, statement, `an automation runs without a page request, so it can't use ${source}; move what it needs into a *.server.ts helper`);
|
|
923
|
+
}
|
|
924
|
+
const targetFns = target ? serverFnExports(target) : new Set();
|
|
925
|
+
const reads = targetFns.size > 0 ? importedReads(file, statement) : [];
|
|
926
|
+
const called = reads === "all" ? [...targetFns] : [...new Set(reads)].filter((name) => targetFns.has(name));
|
|
927
|
+
if (called.length > 0) {
|
|
928
|
+
report("workflow-imports", "error", path, statement, `an automation runs without a page request, so it can't call ${called.join(", ")} from ${source}; move what it needs into a *.server.ts helper`);
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
const exported = statement.type === "ExportNamedDeclaration" ? statement.declaration : undefined;
|
|
932
|
+
const exportedNames = exported?.type === "FunctionDeclaration"
|
|
933
|
+
? [nameOf(exported.id)]
|
|
934
|
+
: exported?.type === "VariableDeclaration"
|
|
935
|
+
? exported.declarations.map((d) => nameOf(d.id))
|
|
936
|
+
: [];
|
|
937
|
+
if (exportedNames.includes("getServerSideProps")) {
|
|
938
|
+
report("next-construct", "error", path, statement, "getServerSideProps is Next.js; a route's loader fetches its data");
|
|
939
|
+
}
|
|
940
|
+
if (serverReachable && statement.type === "VariableDeclaration" && statement.kind === "let") {
|
|
941
|
+
report("module-state", "warning", path, statement, "a module-level let in server code is shared by every request and every user; keep per-request state inside the handler");
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
walk(program, (node, ancestors) => {
|
|
945
|
+
const parent = ancestors.at(-1);
|
|
946
|
+
if (node.type === "CallExpression" && isHandlerCall(node)) {
|
|
947
|
+
const { root, calls } = chainOf(node);
|
|
948
|
+
const builder = factories(file, root);
|
|
949
|
+
if (builder) {
|
|
950
|
+
const declarator = parent?.type === "VariableDeclarator" ? parent : undefined;
|
|
951
|
+
const name = nameOf(declarator?.id);
|
|
952
|
+
if (!declarator || !isTopLevelDeclarator(ancestors)) {
|
|
953
|
+
report("server-fn-not-top-level", "error", path, node, "assign each server function to its own top-level const: one in an object, a function or a helper's return value isn't compiled");
|
|
954
|
+
}
|
|
955
|
+
else if (name) {
|
|
956
|
+
serverFnFiles.set(name, [...(serverFnFiles.get(name) ?? []), path]);
|
|
957
|
+
}
|
|
958
|
+
const { factory, method } = builder;
|
|
959
|
+
if (!method && !hasMethodOption(calls)) {
|
|
960
|
+
report("method-missing", "error", path, node, `call ${root} with { method: 'GET' | 'POST', description } before .handler()`);
|
|
961
|
+
}
|
|
962
|
+
const subject = `function ${name ?? "this function"}`;
|
|
963
|
+
if (factory === "createServerFn") {
|
|
964
|
+
report("raw-server-fn", "error", path, node, `${subject}: define it with signedInFn or publicFn from zitejs/start`);
|
|
965
|
+
}
|
|
966
|
+
else if (factory === "publicFn" && internal) {
|
|
967
|
+
report("public-in-internal", "error", path, node, `${subject} is a publicFn, and this internal app signs everyone in; make it a signedInFn`);
|
|
968
|
+
}
|
|
969
|
+
else if (factory === "signedInFn") {
|
|
970
|
+
const owner = ownerInput(calls);
|
|
971
|
+
if (owner) {
|
|
972
|
+
report("owner-from-input", "warning", path, node, `${subject} takes ${owner} as input, which any caller can set; use context.user.id`);
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
if (file.imports.get(root)?.imported === "publicFn" || root === "publicFn") {
|
|
976
|
+
const handler = node.arguments[0];
|
|
977
|
+
if (handler && FUNCTION_TYPES.has(handler.type)) {
|
|
978
|
+
walk(handler, (inner, innerAncestors) => {
|
|
979
|
+
const returned = inner.type === "ReturnStatement"
|
|
980
|
+
? inner.argument
|
|
981
|
+
: inner === handler && handler.type === "ArrowFunctionExpression" && handler.body.type !== "BlockStatement"
|
|
982
|
+
? handler.body
|
|
983
|
+
: undefined;
|
|
984
|
+
if (!returned || innerAncestors.slice(1).some((a) => FUNCTION_TYPES.has(a.type)))
|
|
985
|
+
return;
|
|
986
|
+
const call = unwrap(returned.type === "AwaitExpression" ? returned.argument : returned);
|
|
987
|
+
if (call.type !== "CallExpression")
|
|
988
|
+
return;
|
|
989
|
+
const chain = chainOf(call);
|
|
990
|
+
const method = chain.members[0];
|
|
991
|
+
if (chain.root !== "zite" || (method !== "findAll" && method !== "findOne"))
|
|
992
|
+
return;
|
|
993
|
+
const options = call.arguments[0];
|
|
994
|
+
if (options?.type === "ObjectExpression" && propertyNamed(options, "fields"))
|
|
995
|
+
return;
|
|
996
|
+
report("public-rows", "warning", path, inner, `anyone can call ${declarator ? nameOf(declarator.id) : "this function"}, and it returns whole records; pass fields to return only what the page shows`);
|
|
997
|
+
});
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
if (moduleContainers.size > 0 && !atModuleScope(ancestors)) {
|
|
1003
|
+
const target = node.type === "CallExpression" && isNode(node.callee) && node.callee.type === "MemberExpression" &&
|
|
1004
|
+
MUTATING_METHODS.has(nameOf(node.callee.property) ?? "")
|
|
1005
|
+
? nameOf(node.callee.object)
|
|
1006
|
+
: node.type === "AssignmentExpression" && isNode(node.left) && node.left.type === "MemberExpression"
|
|
1007
|
+
? nameOf(node.left.object)
|
|
1008
|
+
: undefined;
|
|
1009
|
+
if (target && moduleContainers.get(target) === false) {
|
|
1010
|
+
moduleContainers.set(target, true);
|
|
1011
|
+
report("module-state", "warning", path, node, `${target} is module-level, so what a handler writes to it is shared by every request and every user; use the database or keep it inside the handler`);
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
if (node.type === "MemberExpression" && nameOf(node.property) === "inputValidator" && !node.computed) {
|
|
1015
|
+
report("input-validator", "error", path, node, "inputValidator was removed in TanStack Start 1.0; call .validator(…)");
|
|
1016
|
+
}
|
|
1017
|
+
if (node.type === "CallExpression" && nameOf(node.callee) === "createEndpoint") {
|
|
1018
|
+
report("classic-construct", "error", path, node, "createEndpoint is classic's; in a Start app, server code is a signedInFn/publicFn server function");
|
|
1019
|
+
}
|
|
1020
|
+
if (node.type === "CallExpression" && nameOf(node.callee) === "redirect") {
|
|
1021
|
+
const argument = node.arguments[0];
|
|
1022
|
+
const options = argument?.type === "ObjectExpression" ? argument : undefined;
|
|
1023
|
+
const href = options && propertyNamed(options, "href");
|
|
1024
|
+
const value = href && href.value;
|
|
1025
|
+
const toLogin = !!value && (nameOf(value) === "loginHref" || callsAny(value, (callee) => nameOf(callee) === "loginHref"));
|
|
1026
|
+
const reload = options && propertyNamed(options, "reloadDocument");
|
|
1027
|
+
const reloads = !!reload && isNode(reload.value) && reload.value.type === "BooleanLiteral" && reload.value.value === true;
|
|
1028
|
+
if (toLogin && !reloads) {
|
|
1029
|
+
report("login-reload", "error", path, node, "the sign-in page isn't one of the app's routes; redirect to it with reloadDocument: true");
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
if (node.type === "NewExpression" &&
|
|
1033
|
+
nameOf(node.callee) === "QueryClient" &&
|
|
1034
|
+
atModuleScope(ancestors)) {
|
|
1035
|
+
report("module-query-client", "error", path, node, "a module-level QueryClient caches one visitor's data for the next; createZiteRouter makes one per request as context.queryClient");
|
|
1036
|
+
}
|
|
1037
|
+
if (atModuleScope(ancestors)) {
|
|
1038
|
+
const envRead = node.type === "MemberExpression" &&
|
|
1039
|
+
isNode(node.object) &&
|
|
1040
|
+
node.object.type === "MemberExpression" &&
|
|
1041
|
+
nameOf(node.object.object) === "process" &&
|
|
1042
|
+
nameOf(node.object.property) === "env" &&
|
|
1043
|
+
nameOf(node.property) !== "NODE_ENV";
|
|
1044
|
+
if (envRead) {
|
|
1045
|
+
report("module-env", "error", path, node, "process.env is the request's, so read it inside a handler or loader, not when the module loads");
|
|
1046
|
+
}
|
|
1047
|
+
if (node.type === "CallExpression" && isNode(node.callee) && node.callee.type === "MemberExpression" &&
|
|
1048
|
+
nameOf(node.callee.object) === "Date" && nameOf(node.callee.property) === "now") {
|
|
1049
|
+
report("module-env", "error", path, node, "Date.now() when the module loads is the server's start time; call it where the value is used");
|
|
1050
|
+
}
|
|
1051
|
+
const isWindowReference = node.type === "Identifier" &&
|
|
1052
|
+
node.name === "window" &&
|
|
1053
|
+
!(parent?.type === "UnaryExpression" && parent.operator === "typeof") &&
|
|
1054
|
+
!(parent?.type === "MemberExpression" && parent.property === node && !parent.computed) &&
|
|
1055
|
+
!(parent?.type === "ObjectProperty" && parent.key === node) &&
|
|
1056
|
+
!parent?.type.startsWith("TS") &&
|
|
1057
|
+
!guardedByTypeofWindow(ancestors);
|
|
1058
|
+
if (isWindowReference) {
|
|
1059
|
+
// Guards come in too many shapes to tell for sure, so only a warning.
|
|
1060
|
+
report("module-env", "warning", path, node, "window doesn't exist while the server renders; use it in an effect or event handler");
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
if (node.type === "AwaitExpression" &&
|
|
1064
|
+
isNode(node.argument) &&
|
|
1065
|
+
chainOf(node.argument).root === "zite") {
|
|
1066
|
+
const fn = [...ancestors].reverse().findIndex((a) => FUNCTION_TYPES.has(a.type));
|
|
1067
|
+
const inner = fn === -1 ? ancestors : ancestors.slice(ancestors.length - fn);
|
|
1068
|
+
if (inner.some((a) => LOOP_TYPES.has(a.type))) {
|
|
1069
|
+
report("await-in-loop", "warning", path, node, "each await zite.* in a loop is a round trip; batch it with one findAll, bulkCreate or bulkUpdate");
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
if ((node.type === "ObjectProperty" || node.type === "ObjectMethod") &&
|
|
1073
|
+
(nameOf(node.key) === "loader" || nameOf(node.key) === "beforeLoad")) {
|
|
1074
|
+
const body = (node.type === "ObjectMethod" ? node : node.value);
|
|
1075
|
+
const isLayout = ROOT_LAYOUTS.has(path) && nameOf(node.key) === "beforeLoad";
|
|
1076
|
+
const ssr = parent?.type === "ObjectExpression" ? propertyNamed(parent, "ssr")?.value : undefined;
|
|
1077
|
+
const browserOnly = isNode(ssr) && ssr.type === "BooleanLiteral" && ssr.value === false;
|
|
1078
|
+
walk(body, (inner) => {
|
|
1079
|
+
if (inner.type !== "CallExpression")
|
|
1080
|
+
return;
|
|
1081
|
+
const callee = nameOf(inner.callee);
|
|
1082
|
+
if (!browserOnly && callee === "fetch" && isRelativePath(inner.arguments[0])) {
|
|
1083
|
+
report("relative-fetch", "error", path, inner, "a loader also runs on the server, where a relative URL has no host; call a server function instead");
|
|
1084
|
+
}
|
|
1085
|
+
const source = callee ? file.imports.get(callee)?.source : undefined;
|
|
1086
|
+
const serverFn = !!callee && (localServerFns.has(callee) || (!!source && /\.functions(\.[jt]sx?)?$/.test(source)));
|
|
1087
|
+
if (isLayout && serverFn) {
|
|
1088
|
+
report("layout-server-fn", "warning", path, inner, `${callee} would run on every navigation from the root layout; the signed-in user is already context.user`);
|
|
1089
|
+
}
|
|
1090
|
+
});
|
|
1091
|
+
}
|
|
1092
|
+
});
|
|
1093
|
+
}
|
|
1094
|
+
problems.push(...routeProblems(files, internal), ...workflowProblems(files, kind), ...serverImportProblems(files, factories, routerPath));
|
|
1095
|
+
for (const [name, paths] of serverFnFiles) {
|
|
1096
|
+
const distinct = [...new Set(paths)];
|
|
1097
|
+
if (distinct.length > 1) {
|
|
1098
|
+
problems.push({
|
|
1099
|
+
rule: "duplicate-server-fn",
|
|
1100
|
+
severity: "error",
|
|
1101
|
+
file: distinct[1],
|
|
1102
|
+
message: `${name} is also a server function in ${distinct[0]}; a server function's name is its id, so each is unique in the app`,
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
return problems;
|
|
1107
|
+
}
|