@nifrajs/cli 5.17.0 → 5.18.0
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/CHANGELOG.md +50 -0
- package/dist/check-diagnostics.d.ts +7 -0
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +3 -0
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan-sql.d.ts +95 -0
- package/dist/check-scan-sql.d.ts.map +1 -0
- package/dist/check-scan-sql.js +1190 -0
- package/dist/check-scan-sql.js.map +1 -0
- package/dist/check-scan.d.ts +5 -93
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +20 -1207
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +9 -0
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts +2 -6
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +249 -80
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts +3 -0
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +18 -4
- package/dist/command-catalog.js.map +1 -1
- package/dist/frontend-guidance.js +1 -1
- package/dist/frontend-guidance.js.map +1 -1
- package/dist/internal/a11y-element.d.ts +33 -0
- package/dist/internal/a11y-element.d.ts.map +1 -0
- package/dist/internal/a11y-element.js +124 -0
- package/dist/internal/a11y-element.js.map +1 -0
- package/dist/internal/a11y-jsx.d.ts +12 -0
- package/dist/internal/a11y-jsx.d.ts.map +1 -0
- package/dist/internal/a11y-jsx.js +42 -0
- package/dist/internal/a11y-jsx.js.map +1 -0
- package/dist/internal/a11y-template.d.ts +11 -0
- package/dist/internal/a11y-template.d.ts.map +1 -0
- package/dist/internal/a11y-template.js +56 -0
- package/dist/internal/a11y-template.js.map +1 -0
- package/dist/internal/aria-data.d.ts +23 -0
- package/dist/internal/aria-data.d.ts.map +1 -0
- package/dist/internal/aria-data.js +220 -0
- package/dist/internal/aria-data.js.map +1 -0
- package/dist/internal/edit-distance.d.ts +3 -0
- package/dist/internal/edit-distance.d.ts.map +1 -0
- package/dist/internal/edit-distance.js +12 -0
- package/dist/internal/edit-distance.js.map +1 -0
- package/dist/internal/imported-as.d.ts +10 -0
- package/dist/internal/imported-as.d.ts.map +1 -0
- package/dist/internal/imported-as.js +23 -0
- package/dist/internal/imported-as.js.map +1 -0
- package/dist/internal/source-facts.d.ts +3 -0
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +2 -0
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/internal/template-markup.d.ts +58 -0
- package/dist/internal/template-markup.d.ts.map +1 -0
- package/dist/internal/template-markup.js +221 -0
- package/dist/internal/template-markup.js.map +1 -0
- package/dist/internal/typescript-import.d.ts.map +1 -1
- package/dist/internal/typescript-import.js +11 -0
- package/dist/internal/typescript-import.js.map +1 -1
- package/dist/mcp-exec.d.ts.map +1 -1
- package/dist/mcp-exec.js +37 -23
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.js +1 -1
- package/dist/mcp-io.d.ts.map +1 -1
- package/dist/mcp-io.js +5 -3
- package/dist/mcp-io.js.map +1 -1
- package/dist/mcp-root.js +1 -1
- package/dist/mcp-root.js.map +1 -1
- package/dist/mcp-ws.d.ts.map +1 -1
- package/dist/mcp-ws.js +3 -2
- package/dist/mcp-ws.js.map +1 -1
- package/dist/migrate-layout-config.d.ts +63 -0
- package/dist/migrate-layout-config.d.ts.map +1 -0
- package/dist/migrate-layout-config.js +507 -0
- package/dist/migrate-layout-config.js.map +1 -0
- package/dist/migrate-layout-split.d.ts +46 -0
- package/dist/migrate-layout-split.d.ts.map +1 -0
- package/dist/migrate-layout-split.js +687 -0
- package/dist/migrate-layout-split.js.map +1 -0
- package/dist/migrate-layout.d.ts +4 -24
- package/dist/migrate-layout.d.ts.map +1 -1
- package/dist/migrate-layout.js +8 -1183
- package/dist/migrate-layout.js.map +1 -1
- package/dist/output-schemas.d.ts.map +1 -1
- package/dist/output-schemas.js +78 -10
- package/dist/output-schemas.js.map +1 -1
- package/dist/recipes/5.18.0.d.ts +9 -0
- package/dist/recipes/5.18.0.d.ts.map +1 -0
- package/dist/recipes/5.18.0.js +56 -0
- package/dist/recipes/5.18.0.js.map +1 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +2 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/review.d.ts.map +1 -1
- package/dist/review.js +6 -0
- package/dist/review.js.map +1 -1
- package/dist/rules/a11y.d.ts +5 -0
- package/dist/rules/a11y.d.ts.map +1 -0
- package/dist/rules/a11y.js +78 -0
- package/dist/rules/a11y.js.map +1 -0
- package/dist/rules/codes.d.ts +6 -0
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +6 -0
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/comment-markers.js +1 -1
- package/dist/rules/comment-markers.js.map +1 -1
- package/dist/rules/explanations.d.ts.map +1 -1
- package/dist/rules/explanations.js +30 -0
- package/dist/rules/explanations.js.map +1 -1
- package/dist/rules/host-exposure.d.ts +23 -0
- package/dist/rules/host-exposure.d.ts.map +1 -0
- package/dist/rules/host-exposure.js +380 -0
- package/dist/rules/host-exposure.js.map +1 -0
- package/dist/rules/internal-links.d.ts.map +1 -1
- package/dist/rules/internal-links.js +16 -141
- package/dist/rules/internal-links.js.map +1 -1
- package/dist/rules/islands.d.ts +54 -0
- package/dist/rules/islands.d.ts.map +1 -1
- package/dist/rules/islands.js +577 -80
- package/dist/rules/islands.js.map +1 -1
- package/dist/rules/security.d.ts +1 -0
- package/dist/rules/security.d.ts.map +1 -1
- package/dist/rules/security.js +34 -7
- package/dist/rules/security.js.map +1 -1
- package/dist/runtime-differences.js +3 -3
- package/dist/runtime-differences.js.map +1 -1
- package/dist/scaffold.d.ts +1 -1
- package/dist/scaffold.d.ts.map +1 -1
- package/dist/scaffold.js +2 -2
- package/dist/scaffold.js.map +1 -1
- package/dist/source-rewrites.d.ts.map +1 -1
- package/dist/source-rewrites.js +2 -19
- package/dist/source-rewrites.js.map +1 -1
- package/docs/examples.json +4 -4
- package/docs/llms-full.txt +422 -42
- package/docs/types.json +466 -56
- package/package.json +12 -12
- package/src/check-diagnostics.ts +11 -0
- package/src/check-scan-sql.ts +1746 -0
- package/src/check-scan.ts +34 -1754
- package/src/check.ts +10 -1
- package/src/cli.ts +258 -98
- package/src/command-catalog.ts +21 -4
- package/src/frontend-guidance.ts +1 -1
- package/src/internal/a11y-element.ts +227 -0
- package/src/internal/a11y-jsx.ts +54 -0
- package/src/internal/a11y-template.ts +74 -0
- package/src/internal/aria-data.ts +242 -0
- package/src/internal/edit-distance.ts +16 -0
- package/src/internal/imported-as.ts +30 -0
- package/src/internal/source-facts.ts +4 -0
- package/src/internal/template-markup.ts +270 -0
- package/src/internal/typescript-import.ts +15 -0
- package/src/mcp-exec.ts +43 -22
- package/src/mcp-http.ts +1 -1
- package/src/mcp-io.ts +5 -3
- package/src/mcp-root.ts +1 -1
- package/src/mcp-ws.ts +3 -2
- package/src/migrate-layout-config.ts +615 -0
- package/src/migrate-layout-split.ts +870 -0
- package/src/migrate-layout.ts +37 -1468
- package/src/output-schemas.ts +85 -17
- package/src/recipes/5.18.0.ts +63 -0
- package/src/recipes/index.ts +2 -0
- package/src/review.ts +6 -0
- package/src/rules/a11y.ts +119 -0
- package/src/rules/codes.ts +6 -0
- package/src/rules/comment-markers.ts +2 -2
- package/src/rules/explanations.ts +30 -0
- package/src/rules/host-exposure.ts +531 -0
- package/src/rules/internal-links.ts +21 -154
- package/src/rules/islands.ts +698 -86
- package/src/rules/security.ts +48 -7
- package/src/runtime-differences.ts +3 -3
- package/src/scaffold.ts +3 -3
- package/src/source-rewrites.ts +2 -15
|
@@ -0,0 +1,1190 @@
|
|
|
1
|
+
import { readFileSync, realpathSync } from "node:fs";
|
|
2
|
+
import { dirname, isAbsolute, join, resolve, sep } from "node:path";
|
|
3
|
+
import { bySite, IGNORED_DIR, isRelativeSpecifier, walkSource, } from "./check-scan.js";
|
|
4
|
+
import { realpathOrSelf } from "./internal/realpath-or-self.js";
|
|
5
|
+
import { scriptKindOf } from "./internal/script-kind.js";
|
|
6
|
+
import { loadProjectTypeScript, } from "./internal/typescript-import.js";
|
|
7
|
+
import { commentBlockExpectation } from "./rules/comment-markers.js";
|
|
8
|
+
const SQL_METHODS = new Set([
|
|
9
|
+
"query",
|
|
10
|
+
"queryRaw",
|
|
11
|
+
"prepare",
|
|
12
|
+
"exec",
|
|
13
|
+
"execute",
|
|
14
|
+
"run",
|
|
15
|
+
"unsafe",
|
|
16
|
+
"$queryRawUnsafe",
|
|
17
|
+
"$executeRawUnsafe",
|
|
18
|
+
"raw",
|
|
19
|
+
]);
|
|
20
|
+
const SQL_KEYWORD = /\b(select|insert\s+into|update|delete\s+from|drop\s+table|create\s+table|alter\s+table|truncate|from|where|values|set|join|union)\b/i;
|
|
21
|
+
const ALWAYS_UNSAFE = new Set(["unsafe", "$queryRawUnsafe", "$executeRawUnsafe", "raw"]);
|
|
22
|
+
const DRIVER_READ_METHODS = new Set([
|
|
23
|
+
"all",
|
|
24
|
+
"get",
|
|
25
|
+
"each",
|
|
26
|
+
"any",
|
|
27
|
+
"one",
|
|
28
|
+
"none",
|
|
29
|
+
"many",
|
|
30
|
+
"oneOrNone",
|
|
31
|
+
"manyOrNone",
|
|
32
|
+
"result",
|
|
33
|
+
"multi",
|
|
34
|
+
"multiResult",
|
|
35
|
+
]);
|
|
36
|
+
const STATEMENT_START = /^\s*\(?\s*(?:select|insert|update|delete|with|replace|merge|create|drop|alter|truncate|pragma)\s/i;
|
|
37
|
+
function sqlModuleScope(ts, id, source) {
|
|
38
|
+
const consts = new Map();
|
|
39
|
+
const imports = new Map();
|
|
40
|
+
const localExports = new Map();
|
|
41
|
+
const reExports = new Map();
|
|
42
|
+
const starExports = [];
|
|
43
|
+
for (const statement of source.statements) {
|
|
44
|
+
if (ts.isVariableStatement(statement)) {
|
|
45
|
+
if ((statement.declarationList.flags & ts.NodeFlags.Const) === 0)
|
|
46
|
+
continue;
|
|
47
|
+
const exported = statement.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword) === true;
|
|
48
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
49
|
+
if (!ts.isIdentifier(declaration.name) || declaration.initializer === undefined)
|
|
50
|
+
continue;
|
|
51
|
+
consts.set(declaration.name.text, declaration.initializer);
|
|
52
|
+
if (exported)
|
|
53
|
+
localExports.set(declaration.name.text, declaration.name.text);
|
|
54
|
+
}
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (ts.isImportDeclaration(statement)) {
|
|
58
|
+
const clause = statement.importClause;
|
|
59
|
+
if (clause === undefined || clause.isTypeOnly)
|
|
60
|
+
continue;
|
|
61
|
+
if (!ts.isStringLiteral(statement.moduleSpecifier))
|
|
62
|
+
continue;
|
|
63
|
+
const bindings = clause.namedBindings;
|
|
64
|
+
if (bindings === undefined || !ts.isNamedImports(bindings))
|
|
65
|
+
continue;
|
|
66
|
+
for (const element of bindings.elements) {
|
|
67
|
+
if (element.isTypeOnly)
|
|
68
|
+
continue;
|
|
69
|
+
imports.set(element.name.text, {
|
|
70
|
+
specifier: statement.moduleSpecifier.text,
|
|
71
|
+
exported: element.propertyName?.text ?? element.name.text,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (!ts.isExportDeclaration(statement) || statement.isTypeOnly)
|
|
77
|
+
continue;
|
|
78
|
+
const from = statement.moduleSpecifier;
|
|
79
|
+
if (from !== undefined && !ts.isStringLiteral(from))
|
|
80
|
+
continue;
|
|
81
|
+
const clause = statement.exportClause;
|
|
82
|
+
if (clause === undefined) {
|
|
83
|
+
if (from !== undefined)
|
|
84
|
+
starExports.push(from.text);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (!ts.isNamedExports(clause))
|
|
88
|
+
continue;
|
|
89
|
+
for (const element of clause.elements) {
|
|
90
|
+
if (element.isTypeOnly)
|
|
91
|
+
continue;
|
|
92
|
+
const local = element.propertyName?.text ?? element.name.text;
|
|
93
|
+
if (from === undefined)
|
|
94
|
+
localExports.set(element.name.text, local);
|
|
95
|
+
else
|
|
96
|
+
reExports.set(element.name.text, { specifier: from.text, exported: local });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return { id, consts, imports, localExports, reExports, starExports };
|
|
100
|
+
}
|
|
101
|
+
export const createSqlImports = (load) => ({
|
|
102
|
+
load,
|
|
103
|
+
scopes: new Map(),
|
|
104
|
+
});
|
|
105
|
+
function parseSqlSource(ts, file, content) {
|
|
106
|
+
const kind = scriptKindOf(ts, file);
|
|
107
|
+
let source;
|
|
108
|
+
try {
|
|
109
|
+
source = ts.createSourceFile(file, content, ts.ScriptTarget.Latest, true, kind);
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
const parseDiagnostics = source
|
|
115
|
+
.parseDiagnostics;
|
|
116
|
+
return parseDiagnostics !== undefined && parseDiagnostics.length > 0 ? undefined : source;
|
|
117
|
+
}
|
|
118
|
+
const SQL_IMPORT_HOPS = 3;
|
|
119
|
+
function loadSqlModuleScope(ts, imports, fromModule, specifier) {
|
|
120
|
+
const loaded = imports.load(fromModule, specifier);
|
|
121
|
+
if (loaded === undefined)
|
|
122
|
+
return undefined;
|
|
123
|
+
if (imports.scopes.has(loaded.id))
|
|
124
|
+
return imports.scopes.get(loaded.id);
|
|
125
|
+
const source = parseSqlSource(ts, loaded.id, loaded.content);
|
|
126
|
+
const scope = source === undefined ? undefined : sqlModuleScope(ts, loaded.id, source);
|
|
127
|
+
imports.scopes.set(loaded.id, scope);
|
|
128
|
+
return scope;
|
|
129
|
+
}
|
|
130
|
+
function resolveExportedConst(ts, imports, scope, name, hops, seen) {
|
|
131
|
+
if (hops > SQL_IMPORT_HOPS)
|
|
132
|
+
return undefined;
|
|
133
|
+
const key = `${scope.id}#${name}`;
|
|
134
|
+
if (seen.has(key))
|
|
135
|
+
return undefined;
|
|
136
|
+
const nextSeen = new Set(seen).add(key);
|
|
137
|
+
const follow = (edge) => {
|
|
138
|
+
const target = loadSqlModuleScope(ts, imports, scope.id, edge.specifier);
|
|
139
|
+
return target === undefined
|
|
140
|
+
? undefined
|
|
141
|
+
: resolveExportedConst(ts, imports, target, edge.exported, hops + 1, nextSeen);
|
|
142
|
+
};
|
|
143
|
+
const local = scope.localExports.get(name);
|
|
144
|
+
if (local !== undefined) {
|
|
145
|
+
const initializer = scope.consts.get(local);
|
|
146
|
+
if (initializer !== undefined)
|
|
147
|
+
return { expression: initializer, scope };
|
|
148
|
+
const via = scope.imports.get(local);
|
|
149
|
+
return via === undefined ? undefined : follow(via);
|
|
150
|
+
}
|
|
151
|
+
const reExported = scope.reExports.get(name);
|
|
152
|
+
if (reExported !== undefined)
|
|
153
|
+
return follow(reExported);
|
|
154
|
+
let found;
|
|
155
|
+
for (const specifier of scope.starExports) {
|
|
156
|
+
const hit = follow({ specifier, exported: name });
|
|
157
|
+
if (hit === undefined)
|
|
158
|
+
continue;
|
|
159
|
+
if (found !== undefined)
|
|
160
|
+
return undefined;
|
|
161
|
+
found = hit;
|
|
162
|
+
}
|
|
163
|
+
return found;
|
|
164
|
+
}
|
|
165
|
+
function isShadowedAt(ts, at, name) {
|
|
166
|
+
let scope;
|
|
167
|
+
for (let node = at.parent; node !== undefined; node = node.parent) {
|
|
168
|
+
if (ts.isSourceFile(node))
|
|
169
|
+
break;
|
|
170
|
+
if (ts.isFunctionLike(node) || ts.isBlock(node))
|
|
171
|
+
scope = node;
|
|
172
|
+
}
|
|
173
|
+
if (scope === undefined)
|
|
174
|
+
return false;
|
|
175
|
+
const bindsName = (binding) => ts.isIdentifier(binding)
|
|
176
|
+
? binding.text === name
|
|
177
|
+
: binding.elements.some((element) => !ts.isOmittedExpression(element) &&
|
|
178
|
+
(ts.isIdentifier(element.name) ? element.name.text === name : bindsName(element.name)));
|
|
179
|
+
let found = false;
|
|
180
|
+
const scan = (node) => {
|
|
181
|
+
if (found)
|
|
182
|
+
return;
|
|
183
|
+
if ((ts.isVariableDeclaration(node) || ts.isParameter(node)) && bindsName(node.name)) {
|
|
184
|
+
found = true;
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (ts.isFunctionDeclaration(node) && node.name?.text === name) {
|
|
188
|
+
found = true;
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
ts.forEachChild(node, scan);
|
|
192
|
+
};
|
|
193
|
+
scan(scope);
|
|
194
|
+
return found;
|
|
195
|
+
}
|
|
196
|
+
const CONST_RESOLUTION_DEPTH = 5;
|
|
197
|
+
function resolveStaticSqlText(ts, node, scope, depth) {
|
|
198
|
+
if (depth > CONST_RESOLUTION_DEPTH)
|
|
199
|
+
return undefined;
|
|
200
|
+
if (ts.isParenthesizedExpression(node)) {
|
|
201
|
+
return resolveStaticSqlText(ts, node.expression, scope, depth);
|
|
202
|
+
}
|
|
203
|
+
if (ts.isAsExpression(node) || ts.isSatisfiesExpression(node)) {
|
|
204
|
+
return resolveStaticSqlText(ts, node.expression, scope, depth);
|
|
205
|
+
}
|
|
206
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
|
|
207
|
+
return node.text;
|
|
208
|
+
if (ts.isNumericLiteral(node))
|
|
209
|
+
return node.text;
|
|
210
|
+
if (ts.isTemplateExpression(node)) {
|
|
211
|
+
let text = node.head.text;
|
|
212
|
+
for (const span of node.templateSpans) {
|
|
213
|
+
const part = placeholderNumber(ts, text, span.expression, depth) ??
|
|
214
|
+
resolveStaticSqlText(ts, span.expression, scope, depth + 1);
|
|
215
|
+
if (part === undefined)
|
|
216
|
+
return undefined;
|
|
217
|
+
text += part + span.literal.text;
|
|
218
|
+
}
|
|
219
|
+
return text;
|
|
220
|
+
}
|
|
221
|
+
if (ts.isConditionalExpression(node)) {
|
|
222
|
+
const whenTrue = resolveStaticSqlText(ts, node.whenTrue, scope, depth + 1);
|
|
223
|
+
const whenFalse = resolveStaticSqlText(ts, node.whenFalse, scope, depth + 1);
|
|
224
|
+
if (whenTrue === undefined || whenFalse === undefined)
|
|
225
|
+
return undefined;
|
|
226
|
+
return `${whenTrue} ${whenFalse}`;
|
|
227
|
+
}
|
|
228
|
+
if (ts.isCallExpression(node))
|
|
229
|
+
return resolveStaticJoin(ts, node, scope, depth);
|
|
230
|
+
if (ts.isIdentifier(node)) {
|
|
231
|
+
const local = localConstInitializer(ts, node);
|
|
232
|
+
if (local === "shadow")
|
|
233
|
+
return undefined;
|
|
234
|
+
if (local !== undefined)
|
|
235
|
+
return resolveStaticSqlText(ts, local, scope, depth + 1);
|
|
236
|
+
const initializer = scope.module.consts.get(node.text);
|
|
237
|
+
const edge = initializer === undefined ? scope.module.imports.get(node.text) : undefined;
|
|
238
|
+
if (initializer === undefined && edge === undefined)
|
|
239
|
+
return undefined;
|
|
240
|
+
if (isShadowedAt(ts, node, node.text))
|
|
241
|
+
return undefined;
|
|
242
|
+
if (initializer !== undefined)
|
|
243
|
+
return resolveStaticSqlText(ts, initializer, scope, depth + 1);
|
|
244
|
+
if (edge === undefined || scope.imports === undefined)
|
|
245
|
+
return undefined;
|
|
246
|
+
const target = loadSqlModuleScope(ts, scope.imports, scope.module.id, edge.specifier);
|
|
247
|
+
if (target === undefined)
|
|
248
|
+
return undefined;
|
|
249
|
+
const exported = resolveExportedConst(ts, scope.imports, target, edge.exported, 1, new Set());
|
|
250
|
+
if (exported === undefined)
|
|
251
|
+
return undefined;
|
|
252
|
+
return resolveStaticSqlText(ts, exported.expression, { module: exported.scope, imports: scope.imports }, depth + 1);
|
|
253
|
+
}
|
|
254
|
+
return undefined;
|
|
255
|
+
}
|
|
256
|
+
function localConstInitializer(ts, node) {
|
|
257
|
+
const source = node.getSourceFile();
|
|
258
|
+
for (let scope = node.parent; scope !== undefined; scope = scope.parent) {
|
|
259
|
+
if (ts.isSourceFile(scope))
|
|
260
|
+
return undefined;
|
|
261
|
+
const binding = directBinding(ts, source, scope, node.text);
|
|
262
|
+
if (binding === "shadow")
|
|
263
|
+
return "shadow";
|
|
264
|
+
if (binding !== undefined)
|
|
265
|
+
return binding.constant ? binding.initializer : "shadow";
|
|
266
|
+
}
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
const unwrapExpression = (ts, node) => {
|
|
270
|
+
let current = node;
|
|
271
|
+
while (ts.isParenthesizedExpression(current) ||
|
|
272
|
+
ts.isAsExpression(current) ||
|
|
273
|
+
ts.isSatisfiesExpression(current)) {
|
|
274
|
+
current = current.expression;
|
|
275
|
+
}
|
|
276
|
+
return current;
|
|
277
|
+
};
|
|
278
|
+
function frozenArrayLiteral(ts, node) {
|
|
279
|
+
const call = unwrapExpression(ts, node);
|
|
280
|
+
if (!ts.isCallExpression(call) || call.arguments.length !== 1)
|
|
281
|
+
return undefined;
|
|
282
|
+
const callee = call.expression;
|
|
283
|
+
if (!ts.isPropertyAccessExpression(callee) ||
|
|
284
|
+
callee.name.text !== "freeze" ||
|
|
285
|
+
!ts.isIdentifier(callee.expression) ||
|
|
286
|
+
callee.expression.text !== "Object" ||
|
|
287
|
+
isShadowedAt(ts, callee.expression, "Object")) {
|
|
288
|
+
return undefined;
|
|
289
|
+
}
|
|
290
|
+
const array = unwrapExpression(ts, call.arguments[0]);
|
|
291
|
+
return ts.isArrayLiteralExpression(array) ? array : undefined;
|
|
292
|
+
}
|
|
293
|
+
const ARRAY_READS = new Set(["join", "includes", "indexOf"]);
|
|
294
|
+
function onlyReadsArray(ts, source, name, declaration, pushed) {
|
|
295
|
+
let ok = true;
|
|
296
|
+
const visit = (node) => {
|
|
297
|
+
if (!ok)
|
|
298
|
+
return;
|
|
299
|
+
if (ts.isIdentifier(node) && node.text === name && node !== declaration) {
|
|
300
|
+
const access = node.parent;
|
|
301
|
+
const call = ts.isPropertyAccessExpression(access) &&
|
|
302
|
+
access.expression === node &&
|
|
303
|
+
ts.isCallExpression(access.parent) &&
|
|
304
|
+
access.parent.expression === access
|
|
305
|
+
? access.parent
|
|
306
|
+
: undefined;
|
|
307
|
+
if (call !== undefined &&
|
|
308
|
+
ts.isPropertyAccessExpression(access) &&
|
|
309
|
+
access.name.text === "push") {
|
|
310
|
+
if (call.arguments.some((argument) => ts.isSpreadElement(argument)))
|
|
311
|
+
ok = false;
|
|
312
|
+
else
|
|
313
|
+
pushed.push(...call.arguments);
|
|
314
|
+
for (const argument of call.arguments)
|
|
315
|
+
visit(argument);
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
const read = ts.isPropertyAccessExpression(access) &&
|
|
319
|
+
access.expression === node &&
|
|
320
|
+
(access.name.text === "length"
|
|
321
|
+
? !isWrittenTo(ts, access)
|
|
322
|
+
: ARRAY_READS.has(access.name.text) && call !== undefined);
|
|
323
|
+
if (!read)
|
|
324
|
+
ok = false;
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
ts.forEachChild(node, visit);
|
|
328
|
+
};
|
|
329
|
+
visit(source);
|
|
330
|
+
return ok;
|
|
331
|
+
}
|
|
332
|
+
function isWrittenTo(ts, node) {
|
|
333
|
+
const parent = node.parent;
|
|
334
|
+
if (ts.isBinaryExpression(parent) &&
|
|
335
|
+
parent.left === node &&
|
|
336
|
+
parent.operatorToken.kind >= ts.SyntaxKind.FirstAssignment &&
|
|
337
|
+
parent.operatorToken.kind <= ts.SyntaxKind.LastAssignment) {
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
if (ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent)) {
|
|
341
|
+
return (parent.operator === ts.SyntaxKind.PlusPlusToken ||
|
|
342
|
+
parent.operator === ts.SyntaxKind.MinusMinusToken);
|
|
343
|
+
}
|
|
344
|
+
return ts.isDeleteExpression(parent);
|
|
345
|
+
}
|
|
346
|
+
function staticArrayItems(ts, receiver, scope, depth) {
|
|
347
|
+
const node = unwrapExpression(ts, receiver);
|
|
348
|
+
let array = ts.isArrayLiteralExpression(node) ? node : frozenArrayLiteral(ts, node);
|
|
349
|
+
let itemScope = scope;
|
|
350
|
+
const pushed = [];
|
|
351
|
+
if (array === undefined && ts.isIdentifier(node)) {
|
|
352
|
+
const local = localConstInitializer(ts, node);
|
|
353
|
+
if (local === "shadow")
|
|
354
|
+
return undefined;
|
|
355
|
+
let initializer = local;
|
|
356
|
+
let exported = false;
|
|
357
|
+
if (initializer === undefined && !isShadowedAt(ts, node, node.text)) {
|
|
358
|
+
initializer = scope.module.consts.get(node.text);
|
|
359
|
+
exported = [...scope.module.localExports.values()].includes(node.text);
|
|
360
|
+
const edge = initializer === undefined ? scope.module.imports.get(node.text) : undefined;
|
|
361
|
+
if (edge !== undefined && scope.imports !== undefined) {
|
|
362
|
+
const target = loadSqlModuleScope(ts, scope.imports, scope.module.id, edge.specifier);
|
|
363
|
+
const found = target === undefined
|
|
364
|
+
? undefined
|
|
365
|
+
: resolveExportedConst(ts, scope.imports, target, edge.exported, 1, new Set());
|
|
366
|
+
if (found === undefined)
|
|
367
|
+
return undefined;
|
|
368
|
+
array = frozenArrayLiteral(ts, found.expression);
|
|
369
|
+
itemScope = { module: found.scope, imports: scope.imports };
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (array === undefined && initializer !== undefined) {
|
|
373
|
+
array = frozenArrayLiteral(ts, initializer);
|
|
374
|
+
const literal = unwrapExpression(ts, initializer);
|
|
375
|
+
if (array === undefined &&
|
|
376
|
+
!exported &&
|
|
377
|
+
ts.isArrayLiteralExpression(literal) &&
|
|
378
|
+
ts.isVariableDeclaration(initializer.parent) &&
|
|
379
|
+
onlyReadsArray(ts, node.getSourceFile(), node.text, initializer.parent.name, pushed)) {
|
|
380
|
+
array = literal;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (array === undefined)
|
|
385
|
+
return undefined;
|
|
386
|
+
const items = [];
|
|
387
|
+
for (const element of [...array.elements, ...pushed]) {
|
|
388
|
+
if (ts.isSpreadElement(element) || ts.isOmittedExpression(element))
|
|
389
|
+
return undefined;
|
|
390
|
+
const text = resolveStaticSqlText(ts, element, itemScope, depth + 1);
|
|
391
|
+
if (text === undefined)
|
|
392
|
+
return undefined;
|
|
393
|
+
items.push(text);
|
|
394
|
+
}
|
|
395
|
+
return items;
|
|
396
|
+
}
|
|
397
|
+
function resolveStaticJoin(ts, call, scope, depth) {
|
|
398
|
+
const callee = call.expression;
|
|
399
|
+
if (!ts.isPropertyAccessExpression(callee) ||
|
|
400
|
+
callee.questionDotToken !== undefined ||
|
|
401
|
+
callee.name.text !== "join" ||
|
|
402
|
+
call.arguments.length > 1) {
|
|
403
|
+
return undefined;
|
|
404
|
+
}
|
|
405
|
+
const separatorArg = call.arguments[0];
|
|
406
|
+
const separator = separatorArg === undefined
|
|
407
|
+
? ","
|
|
408
|
+
: ts.isStringLiteral(separatorArg) || ts.isNoSubstitutionTemplateLiteral(separatorArg)
|
|
409
|
+
? separatorArg.text
|
|
410
|
+
: undefined;
|
|
411
|
+
if (separator === undefined)
|
|
412
|
+
return undefined;
|
|
413
|
+
const items = staticArrayItems(ts, callee.expression, scope, depth + 1) ??
|
|
414
|
+
mappedItem(ts, callee.expression, scope, depth + 1);
|
|
415
|
+
return items?.join(separator);
|
|
416
|
+
}
|
|
417
|
+
function mappedItem(ts, receiver, scope, depth) {
|
|
418
|
+
const call = unwrapExpression(ts, receiver);
|
|
419
|
+
if (!ts.isCallExpression(call) || !ts.isPropertyAccessExpression(call.expression))
|
|
420
|
+
return undefined;
|
|
421
|
+
const method = call.expression.name.text;
|
|
422
|
+
const argument = indexCallbackArgument(ts, call);
|
|
423
|
+
if (argument === undefined || !(method === "map" || method === "flatMap" || argument === 1))
|
|
424
|
+
return undefined;
|
|
425
|
+
const fn = call.arguments[argument];
|
|
426
|
+
if (fn === undefined || !(ts.isArrowFunction(fn) || ts.isFunctionExpression(fn)))
|
|
427
|
+
return undefined;
|
|
428
|
+
const only = ts.isBlock(fn.body) && fn.body.statements.length === 1 ? fn.body.statements[0] : undefined;
|
|
429
|
+
const body = ts.isBlock(fn.body)
|
|
430
|
+
? only !== undefined && ts.isReturnStatement(only)
|
|
431
|
+
? only.expression
|
|
432
|
+
: undefined
|
|
433
|
+
: fn.body;
|
|
434
|
+
const text = body === undefined ? undefined : resolveStaticSqlText(ts, body, scope, depth + 1);
|
|
435
|
+
return text === undefined ? undefined : [text];
|
|
436
|
+
}
|
|
437
|
+
const INDEX_PARAMETER = new Map([
|
|
438
|
+
["map", 1],
|
|
439
|
+
["flatMap", 1],
|
|
440
|
+
["forEach", 1],
|
|
441
|
+
["filter", 1],
|
|
442
|
+
["some", 1],
|
|
443
|
+
["every", 1],
|
|
444
|
+
["find", 1],
|
|
445
|
+
["findIndex", 1],
|
|
446
|
+
["reduce", 2],
|
|
447
|
+
["reduceRight", 2],
|
|
448
|
+
]);
|
|
449
|
+
function indexCallbackArgument(ts, call) {
|
|
450
|
+
const callee = call.expression;
|
|
451
|
+
if (!ts.isPropertyAccessExpression(callee) || callee.questionDotToken !== undefined)
|
|
452
|
+
return undefined;
|
|
453
|
+
if (INDEX_PARAMETER.has(callee.name.text))
|
|
454
|
+
return 0;
|
|
455
|
+
return callee.name.text === "from" &&
|
|
456
|
+
ts.isIdentifier(callee.expression) &&
|
|
457
|
+
callee.expression.text === "Array" &&
|
|
458
|
+
!isShadowedAt(ts, callee.expression, "Array")
|
|
459
|
+
? 1
|
|
460
|
+
: undefined;
|
|
461
|
+
}
|
|
462
|
+
function placeholderNumber(ts, before, node, depth) {
|
|
463
|
+
return before.endsWith("$") && isNumericExpression(ts, node, depth) ? "1" : undefined;
|
|
464
|
+
}
|
|
465
|
+
const NUMERIC_OPERATORS = (ts) => new Set([
|
|
466
|
+
ts.SyntaxKind.MinusToken,
|
|
467
|
+
ts.SyntaxKind.AsteriskToken,
|
|
468
|
+
ts.SyntaxKind.AsteriskAsteriskToken,
|
|
469
|
+
ts.SyntaxKind.SlashToken,
|
|
470
|
+
ts.SyntaxKind.PercentToken,
|
|
471
|
+
ts.SyntaxKind.AmpersandToken,
|
|
472
|
+
ts.SyntaxKind.BarToken,
|
|
473
|
+
ts.SyntaxKind.CaretToken,
|
|
474
|
+
ts.SyntaxKind.LessThanLessThanToken,
|
|
475
|
+
ts.SyntaxKind.GreaterThanGreaterThanToken,
|
|
476
|
+
ts.SyntaxKind.GreaterThanGreaterThanGreaterThanToken,
|
|
477
|
+
]);
|
|
478
|
+
const NUMBER_FUNCTIONS = new Set(["Number", "parseInt", "parseFloat"]);
|
|
479
|
+
function isNumericExpression(ts, node, depth) {
|
|
480
|
+
if (depth > CONST_RESOLUTION_DEPTH)
|
|
481
|
+
return false;
|
|
482
|
+
const expression = unwrapExpression(ts, node);
|
|
483
|
+
if (ts.isNumericLiteral(expression))
|
|
484
|
+
return true;
|
|
485
|
+
if (ts.isPrefixUnaryExpression(expression))
|
|
486
|
+
return expression.operator !== ts.SyntaxKind.ExclamationToken;
|
|
487
|
+
if (ts.isPostfixUnaryExpression(expression))
|
|
488
|
+
return true;
|
|
489
|
+
if (ts.isBinaryExpression(expression)) {
|
|
490
|
+
const operator = expression.operatorToken.kind;
|
|
491
|
+
if (operator === ts.SyntaxKind.PlusToken) {
|
|
492
|
+
return (isNumericExpression(ts, expression.left, depth + 1) &&
|
|
493
|
+
isNumericExpression(ts, expression.right, depth + 1));
|
|
494
|
+
}
|
|
495
|
+
return NUMERIC_OPERATORS(ts).has(operator);
|
|
496
|
+
}
|
|
497
|
+
if (ts.isIdentifier(expression))
|
|
498
|
+
return isNumericName(ts, expression, depth);
|
|
499
|
+
if (ts.isCallExpression(expression)) {
|
|
500
|
+
const callee = expression.expression;
|
|
501
|
+
if (ts.isIdentifier(callee))
|
|
502
|
+
return NUMBER_FUNCTIONS.has(callee.text) && !isShadowedAt(ts, callee, callee.text);
|
|
503
|
+
if (!ts.isPropertyAccessExpression(callee) || callee.questionDotToken !== undefined)
|
|
504
|
+
return false;
|
|
505
|
+
if (callee.name.text === "push")
|
|
506
|
+
return true;
|
|
507
|
+
return (ts.isIdentifier(callee.expression) &&
|
|
508
|
+
callee.expression.text === "Math" &&
|
|
509
|
+
!isShadowedAt(ts, callee.expression, "Math"));
|
|
510
|
+
}
|
|
511
|
+
if (ts.isPropertyAccessExpression(expression) &&
|
|
512
|
+
expression.name.text === "length" &&
|
|
513
|
+
ts.isIdentifier(expression.expression)) {
|
|
514
|
+
const binding = nameBinding(ts, expression.expression);
|
|
515
|
+
return (binding.kind === "value" &&
|
|
516
|
+
ts.isArrayLiteralExpression(unwrapExpression(ts, binding.initializer)));
|
|
517
|
+
}
|
|
518
|
+
return false;
|
|
519
|
+
}
|
|
520
|
+
function nameBinding(ts, node) {
|
|
521
|
+
const source = node.getSourceFile();
|
|
522
|
+
for (let scope = node.parent; scope !== undefined; scope = scope.parent) {
|
|
523
|
+
if (ts.isFunctionLike(scope)) {
|
|
524
|
+
const position = scope.parameters.findIndex((p) => bindingNameCovers(ts, p.name, node.text));
|
|
525
|
+
if (position !== -1) {
|
|
526
|
+
return ts.isIdentifier(scope.parameters[position]?.name ?? node)
|
|
527
|
+
? { kind: "parameter", fn: scope, position }
|
|
528
|
+
: { kind: "unknown" };
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
const binding = directBinding(ts, source, scope, node.text);
|
|
532
|
+
if (binding === "shadow") {
|
|
533
|
+
const list = ts.isForStatement(scope) ? scope.initializer : undefined;
|
|
534
|
+
const declaration = list !== undefined && ts.isVariableDeclarationList(list)
|
|
535
|
+
? list.declarations.find((d) => ts.isIdentifier(d.name) && d.name.text === node.text)
|
|
536
|
+
: undefined;
|
|
537
|
+
return declaration?.initializer === undefined
|
|
538
|
+
? { kind: "unknown" }
|
|
539
|
+
: { kind: "counter", initializer: declaration.initializer };
|
|
540
|
+
}
|
|
541
|
+
if (binding !== undefined)
|
|
542
|
+
return { kind: "value", initializer: binding.initializer };
|
|
543
|
+
if (ts.isSourceFile(scope))
|
|
544
|
+
break;
|
|
545
|
+
}
|
|
546
|
+
return { kind: "unknown" };
|
|
547
|
+
}
|
|
548
|
+
function isNumericName(ts, node, depth) {
|
|
549
|
+
const source = node.getSourceFile();
|
|
550
|
+
const binding = nameBinding(ts, node);
|
|
551
|
+
if (binding.kind === "value")
|
|
552
|
+
return isNumericExpression(ts, binding.initializer, depth + 1);
|
|
553
|
+
if (binding.kind === "counter") {
|
|
554
|
+
return (isNumericExpression(ts, binding.initializer, depth + 1) &&
|
|
555
|
+
onlyNumericWrites(ts, source, node.text, depth));
|
|
556
|
+
}
|
|
557
|
+
if (binding.kind !== "parameter")
|
|
558
|
+
return false;
|
|
559
|
+
const call = binding.fn.parent;
|
|
560
|
+
if (!ts.isCallExpression(call) || !ts.isPropertyAccessExpression(call.expression))
|
|
561
|
+
return false;
|
|
562
|
+
const argument = indexCallbackArgument(ts, call);
|
|
563
|
+
const position = argument === 1 ? 1 : INDEX_PARAMETER.get(call.expression.name.text);
|
|
564
|
+
return (argument !== undefined &&
|
|
565
|
+
call.arguments[argument] === binding.fn &&
|
|
566
|
+
binding.position === position &&
|
|
567
|
+
!isReassignedInFile(ts, source, node.text));
|
|
568
|
+
}
|
|
569
|
+
const NUMERIC_ASSIGNMENTS = (ts) => new Set([
|
|
570
|
+
ts.SyntaxKind.MinusEqualsToken,
|
|
571
|
+
ts.SyntaxKind.AsteriskEqualsToken,
|
|
572
|
+
ts.SyntaxKind.AsteriskAsteriskEqualsToken,
|
|
573
|
+
ts.SyntaxKind.SlashEqualsToken,
|
|
574
|
+
ts.SyntaxKind.PercentEqualsToken,
|
|
575
|
+
ts.SyntaxKind.AmpersandEqualsToken,
|
|
576
|
+
ts.SyntaxKind.BarEqualsToken,
|
|
577
|
+
ts.SyntaxKind.CaretEqualsToken,
|
|
578
|
+
ts.SyntaxKind.LessThanLessThanEqualsToken,
|
|
579
|
+
ts.SyntaxKind.GreaterThanGreaterThanEqualsToken,
|
|
580
|
+
ts.SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken,
|
|
581
|
+
]);
|
|
582
|
+
function onlyNumericWrites(ts, source, name, depth) {
|
|
583
|
+
let ok = true;
|
|
584
|
+
const visit = (node) => {
|
|
585
|
+
if (!ok)
|
|
586
|
+
return;
|
|
587
|
+
if (ts.isIdentifier(node) && node.text === name && isWrittenTo(ts, node)) {
|
|
588
|
+
const parent = node.parent;
|
|
589
|
+
if (ts.isBinaryExpression(parent)) {
|
|
590
|
+
const operator = parent.operatorToken.kind;
|
|
591
|
+
ok =
|
|
592
|
+
operator === ts.SyntaxKind.EqualsToken || operator === ts.SyntaxKind.PlusEqualsToken
|
|
593
|
+
? isNumericExpression(ts, parent.right, depth + 1)
|
|
594
|
+
: NUMERIC_ASSIGNMENTS(ts).has(operator);
|
|
595
|
+
}
|
|
596
|
+
else
|
|
597
|
+
ok = !ts.isDeleteExpression(parent);
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
ts.forEachChild(node, visit);
|
|
601
|
+
};
|
|
602
|
+
visit(source);
|
|
603
|
+
return ok;
|
|
604
|
+
}
|
|
605
|
+
function templateShape(ts, template, scope) {
|
|
606
|
+
let staticText = template.head.text;
|
|
607
|
+
let dynamic = false;
|
|
608
|
+
for (const span of template.templateSpans) {
|
|
609
|
+
const resolved = scope === undefined
|
|
610
|
+
? undefined
|
|
611
|
+
: (placeholderNumber(ts, staticText, span.expression, 0) ??
|
|
612
|
+
resolveStaticSqlText(ts, span.expression, scope, 0));
|
|
613
|
+
if (resolved === undefined)
|
|
614
|
+
dynamic = true;
|
|
615
|
+
else
|
|
616
|
+
staticText += resolved;
|
|
617
|
+
staticText += span.literal.text;
|
|
618
|
+
}
|
|
619
|
+
return { staticText, dynamic };
|
|
620
|
+
}
|
|
621
|
+
function isReassignedInFile(ts, source, name) {
|
|
622
|
+
let found = false;
|
|
623
|
+
const visit = (node) => {
|
|
624
|
+
if (found)
|
|
625
|
+
return;
|
|
626
|
+
if (ts.isBinaryExpression(node) &&
|
|
627
|
+
node.operatorToken.kind >= ts.SyntaxKind.FirstAssignment &&
|
|
628
|
+
node.operatorToken.kind <= ts.SyntaxKind.LastAssignment &&
|
|
629
|
+
ts.isIdentifier(node.left) &&
|
|
630
|
+
node.left.text === name) {
|
|
631
|
+
found = true;
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
if ((ts.isPrefixUnaryExpression(node) || ts.isPostfixUnaryExpression(node)) &&
|
|
635
|
+
(node.operator === ts.SyntaxKind.PlusPlusToken ||
|
|
636
|
+
node.operator === ts.SyntaxKind.MinusMinusToken) &&
|
|
637
|
+
ts.isIdentifier(node.operand) &&
|
|
638
|
+
node.operand.text === name) {
|
|
639
|
+
found = true;
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
ts.forEachChild(node, visit);
|
|
643
|
+
};
|
|
644
|
+
visit(source);
|
|
645
|
+
return found;
|
|
646
|
+
}
|
|
647
|
+
function bindingNameCovers(ts, binding, name) {
|
|
648
|
+
return ts.isIdentifier(binding)
|
|
649
|
+
? binding.text === name
|
|
650
|
+
: binding.elements.some((element) => !ts.isOmittedExpression(element) &&
|
|
651
|
+
(ts.isIdentifier(element.name)
|
|
652
|
+
? element.name.text === name
|
|
653
|
+
: bindingNameCovers(ts, element.name, name)));
|
|
654
|
+
}
|
|
655
|
+
function directBinding(ts, source, scope, name) {
|
|
656
|
+
if (ts.isFunctionLike(scope)) {
|
|
657
|
+
for (const parameter of scope.parameters) {
|
|
658
|
+
if (bindingNameCovers(ts, parameter.name, name))
|
|
659
|
+
return "shadow";
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
if (ts.isCatchClause(scope)) {
|
|
663
|
+
const declared = scope.variableDeclaration;
|
|
664
|
+
if (declared !== undefined && bindingNameCovers(ts, declared.name, name))
|
|
665
|
+
return "shadow";
|
|
666
|
+
}
|
|
667
|
+
for (const statement of scopeStatements(ts, scope) ?? []) {
|
|
668
|
+
if ((ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement)) &&
|
|
669
|
+
statement.name?.text === name) {
|
|
670
|
+
return "shadow";
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
for (const list of scopeDeclarationLists(ts, scope)) {
|
|
674
|
+
for (const declaration of list.declarations) {
|
|
675
|
+
if (!bindingNameCovers(ts, declaration.name, name))
|
|
676
|
+
continue;
|
|
677
|
+
if (!ts.isIdentifier(declaration.name) || declaration.initializer === undefined)
|
|
678
|
+
return "shadow";
|
|
679
|
+
const isConst = (list.flags & ts.NodeFlags.Const) !== 0;
|
|
680
|
+
const isLet = (list.flags & ts.NodeFlags.Let) !== 0;
|
|
681
|
+
if (isConst)
|
|
682
|
+
return { initializer: declaration.initializer, constant: true };
|
|
683
|
+
if (isLet && !isReassignedInFile(ts, source, name))
|
|
684
|
+
return { initializer: declaration.initializer, constant: false };
|
|
685
|
+
return "shadow";
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
return undefined;
|
|
689
|
+
}
|
|
690
|
+
function scopeStatements(ts, scope) {
|
|
691
|
+
return ts.isSourceFile(scope) ||
|
|
692
|
+
ts.isBlock(scope) ||
|
|
693
|
+
ts.isModuleBlock(scope) ||
|
|
694
|
+
ts.isCaseClause(scope) ||
|
|
695
|
+
ts.isDefaultClause(scope)
|
|
696
|
+
? scope.statements
|
|
697
|
+
: undefined;
|
|
698
|
+
}
|
|
699
|
+
function scopeDeclarationLists(ts, scope) {
|
|
700
|
+
const lists = [];
|
|
701
|
+
for (const statement of scopeStatements(ts, scope) ?? []) {
|
|
702
|
+
if (ts.isVariableStatement(statement))
|
|
703
|
+
lists.push(statement.declarationList);
|
|
704
|
+
}
|
|
705
|
+
if ((ts.isForStatement(scope) || ts.isForInStatement(scope) || ts.isForOfStatement(scope)) &&
|
|
706
|
+
scope.initializer !== undefined &&
|
|
707
|
+
ts.isVariableDeclarationList(scope.initializer)) {
|
|
708
|
+
lists.push(scope.initializer);
|
|
709
|
+
}
|
|
710
|
+
return lists;
|
|
711
|
+
}
|
|
712
|
+
function mutableDeclaration(ts, scope, name) {
|
|
713
|
+
for (const list of scopeDeclarationLists(ts, scope)) {
|
|
714
|
+
if ((list.flags & ts.NodeFlags.Const) !== 0)
|
|
715
|
+
continue;
|
|
716
|
+
for (const declaration of list.declarations) {
|
|
717
|
+
if (ts.isIdentifier(declaration.name) && declaration.name.text === name)
|
|
718
|
+
return declaration;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
return undefined;
|
|
722
|
+
}
|
|
723
|
+
function variableWrites(ts, node) {
|
|
724
|
+
const source = node.getSourceFile();
|
|
725
|
+
const name = node.text;
|
|
726
|
+
for (let scope = node.parent; scope !== undefined; scope = scope.parent) {
|
|
727
|
+
const binding = directBinding(ts, source, scope, name);
|
|
728
|
+
if (binding === "shadow") {
|
|
729
|
+
const declaration = mutableDeclaration(ts, scope, name);
|
|
730
|
+
if (declaration === undefined)
|
|
731
|
+
return undefined;
|
|
732
|
+
let owner = declaration;
|
|
733
|
+
while (!ts.isFunctionLike(owner) && !ts.isSourceFile(owner))
|
|
734
|
+
owner = owner.parent;
|
|
735
|
+
const writes = writesTo(ts, owner, name);
|
|
736
|
+
return {
|
|
737
|
+
key: declaration,
|
|
738
|
+
writes: declaration.initializer === undefined
|
|
739
|
+
? writes
|
|
740
|
+
: [{ value: declaration.initializer, appends: false }, ...writes],
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
if (binding !== undefined) {
|
|
744
|
+
return { key: binding.initializer, writes: [{ value: binding.initializer, appends: false }] };
|
|
745
|
+
}
|
|
746
|
+
if (ts.isSourceFile(scope))
|
|
747
|
+
break;
|
|
748
|
+
}
|
|
749
|
+
return undefined;
|
|
750
|
+
}
|
|
751
|
+
function writesTo(ts, root, name) {
|
|
752
|
+
const writes = [];
|
|
753
|
+
const visit = (node) => {
|
|
754
|
+
if (ts.isIdentifier(node) && node.text === name) {
|
|
755
|
+
const parent = node.parent;
|
|
756
|
+
if (ts.isBinaryExpression(parent) && parent.left === node) {
|
|
757
|
+
const operator = parent.operatorToken.kind;
|
|
758
|
+
if (operator === ts.SyntaxKind.PlusEqualsToken) {
|
|
759
|
+
writes.push({ value: parent.right, appends: true });
|
|
760
|
+
}
|
|
761
|
+
else if (operator === ts.SyntaxKind.EqualsToken ||
|
|
762
|
+
operator === ts.SyntaxKind.QuestionQuestionEqualsToken ||
|
|
763
|
+
operator === ts.SyntaxKind.BarBarEqualsToken ||
|
|
764
|
+
operator === ts.SyntaxKind.AmpersandAmpersandEqualsToken) {
|
|
765
|
+
writes.push({ value: parent.right, appends: false });
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
ts.forEachChild(node, visit);
|
|
770
|
+
};
|
|
771
|
+
visit(root);
|
|
772
|
+
return writes;
|
|
773
|
+
}
|
|
774
|
+
function mentionsName(ts, node, name) {
|
|
775
|
+
if (ts.isPropertyAccessExpression(node))
|
|
776
|
+
return mentionsName(ts, node.expression, name);
|
|
777
|
+
if (ts.isPropertyAssignment(node)) {
|
|
778
|
+
return (mentionsName(ts, node.initializer, name) ||
|
|
779
|
+
(ts.isComputedPropertyName(node.name) && mentionsName(ts, node.name.expression, name)));
|
|
780
|
+
}
|
|
781
|
+
if (ts.isIdentifier(node))
|
|
782
|
+
return node.text === name;
|
|
783
|
+
return ts.forEachChild(node, (child) => mentionsName(ts, child, name) || undefined) === true;
|
|
784
|
+
}
|
|
785
|
+
const UNKNOWN_SQL = {
|
|
786
|
+
staticText: "",
|
|
787
|
+
dynamic: true,
|
|
788
|
+
concatenated: false,
|
|
789
|
+
literal: false,
|
|
790
|
+
parameterizedTag: false,
|
|
791
|
+
};
|
|
792
|
+
function textShape(text) {
|
|
793
|
+
return {
|
|
794
|
+
staticText: text,
|
|
795
|
+
dynamic: false,
|
|
796
|
+
concatenated: false,
|
|
797
|
+
literal: true,
|
|
798
|
+
parameterizedTag: false,
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
function joinShapes(shapes) {
|
|
802
|
+
return {
|
|
803
|
+
staticText: shapes.map((shape) => shape.staticText).join(""),
|
|
804
|
+
dynamic: shapes.some((shape) => shape.dynamic || shape.parameterizedTag),
|
|
805
|
+
concatenated: true,
|
|
806
|
+
literal: false,
|
|
807
|
+
parameterizedTag: false,
|
|
808
|
+
};
|
|
809
|
+
}
|
|
810
|
+
function sqlRisk(shape) {
|
|
811
|
+
const dynamic = !shape.dynamic
|
|
812
|
+
? 0
|
|
813
|
+
: SQL_KEYWORD.test(shape.staticText) || STATEMENT_START.test(shape.staticText)
|
|
814
|
+
? 3
|
|
815
|
+
: shape.concatenated
|
|
816
|
+
? 2
|
|
817
|
+
: 1;
|
|
818
|
+
return dynamic * 2 + (shape.parameterizedTag ? 0 : 1);
|
|
819
|
+
}
|
|
820
|
+
function writtenShape(ts, name, writes, scope, trail) {
|
|
821
|
+
const replacing = [];
|
|
822
|
+
const building = [];
|
|
823
|
+
for (const write of writes) {
|
|
824
|
+
const shape = sqlExpressionShape(ts, write.value, scope, trail);
|
|
825
|
+
if (write.appends || mentionsName(ts, write.value, name))
|
|
826
|
+
building.push(shape);
|
|
827
|
+
else
|
|
828
|
+
replacing.push(shape);
|
|
829
|
+
}
|
|
830
|
+
const candidates = building.length === 0 ? replacing : [...replacing, joinShapes([...replacing, ...building])];
|
|
831
|
+
let worst;
|
|
832
|
+
for (const candidate of candidates) {
|
|
833
|
+
if (worst === undefined || sqlRisk(candidate) > sqlRisk(worst))
|
|
834
|
+
worst = candidate;
|
|
835
|
+
}
|
|
836
|
+
return worst ?? UNKNOWN_SQL;
|
|
837
|
+
}
|
|
838
|
+
const TEXT_METHODS = new Set([
|
|
839
|
+
"toString",
|
|
840
|
+
"valueOf",
|
|
841
|
+
"trim",
|
|
842
|
+
"trimStart",
|
|
843
|
+
"trimEnd",
|
|
844
|
+
"toLowerCase",
|
|
845
|
+
"toUpperCase",
|
|
846
|
+
]);
|
|
847
|
+
function passedThrough(ts, call) {
|
|
848
|
+
const callee = call.expression;
|
|
849
|
+
if (ts.isIdentifier(callee) && callee.text === "String" && call.arguments.length === 1) {
|
|
850
|
+
return call.arguments[0];
|
|
851
|
+
}
|
|
852
|
+
if (ts.isPropertyAccessExpression(callee) &&
|
|
853
|
+
TEXT_METHODS.has(callee.name.text) &&
|
|
854
|
+
call.arguments.length === 0) {
|
|
855
|
+
return callee.expression;
|
|
856
|
+
}
|
|
857
|
+
return undefined;
|
|
858
|
+
}
|
|
859
|
+
function joinedParts(ts, call) {
|
|
860
|
+
const callee = call.expression;
|
|
861
|
+
if (!ts.isPropertyAccessExpression(callee))
|
|
862
|
+
return undefined;
|
|
863
|
+
if (callee.name.text === "concat")
|
|
864
|
+
return [callee.expression, ...call.arguments];
|
|
865
|
+
const items = unwrapExpression(ts, callee.expression);
|
|
866
|
+
if (callee.name.text !== "join" || !ts.isArrayLiteralExpression(items))
|
|
867
|
+
return undefined;
|
|
868
|
+
const separator = call.arguments[0];
|
|
869
|
+
const glue = separator === undefined ? "," : ts.isStringLiteralLike(separator) ? separator.text : separator;
|
|
870
|
+
const parts = [];
|
|
871
|
+
items.elements.forEach((element, index) => {
|
|
872
|
+
if (index > 0)
|
|
873
|
+
parts.push(glue);
|
|
874
|
+
if (ts.isSpreadElement(element))
|
|
875
|
+
parts.push(element.expression);
|
|
876
|
+
else if (!ts.isOmittedExpression(element))
|
|
877
|
+
parts.push(element);
|
|
878
|
+
});
|
|
879
|
+
return parts;
|
|
880
|
+
}
|
|
881
|
+
function definesSqlFunction(ts, tag) {
|
|
882
|
+
for (let scope = tag.parent; scope !== undefined; scope = scope.parent) {
|
|
883
|
+
if (ts.isFunctionLike(scope) &&
|
|
884
|
+
scope.parameters.some((parameter) => bindingNameCovers(ts, parameter.name, tag.text))) {
|
|
885
|
+
return false;
|
|
886
|
+
}
|
|
887
|
+
for (const statement of scopeStatements(ts, scope) ?? []) {
|
|
888
|
+
if (ts.isFunctionDeclaration(statement) && statement.name?.text === tag.text)
|
|
889
|
+
return true;
|
|
890
|
+
}
|
|
891
|
+
for (const list of scopeDeclarationLists(ts, scope)) {
|
|
892
|
+
for (const declaration of list.declarations) {
|
|
893
|
+
if (!ts.isIdentifier(declaration.name) || declaration.name.text !== tag.text)
|
|
894
|
+
continue;
|
|
895
|
+
const value = declaration.initializer === undefined
|
|
896
|
+
? undefined
|
|
897
|
+
: unwrapExpression(ts, declaration.initializer);
|
|
898
|
+
return value !== undefined && (ts.isArrowFunction(value) || ts.isFunctionExpression(value));
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
return false;
|
|
903
|
+
}
|
|
904
|
+
function sqlExpressionShape(ts, expression, scope, trail = []) {
|
|
905
|
+
let node = unwrapExpression(ts, expression);
|
|
906
|
+
while (ts.isNonNullExpression(node))
|
|
907
|
+
node = unwrapExpression(ts, node.expression);
|
|
908
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
|
|
909
|
+
return {
|
|
910
|
+
staticText: node.text,
|
|
911
|
+
dynamic: false,
|
|
912
|
+
concatenated: false,
|
|
913
|
+
literal: true,
|
|
914
|
+
parameterizedTag: false,
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
if (ts.isTaggedTemplateExpression(node)) {
|
|
918
|
+
const tag = node.tag;
|
|
919
|
+
const parameterizedTag = (ts.isIdentifier(tag) && tag.text === "sql" && !definesSqlFunction(ts, tag)) ||
|
|
920
|
+
(ts.isPropertyAccessExpression(tag) && tag.getText() === "Prisma.sql");
|
|
921
|
+
const template = node.template;
|
|
922
|
+
const templated = ts.isTemplateExpression(template)
|
|
923
|
+
? templateShape(ts, template, parameterizedTag ? undefined : scope)
|
|
924
|
+
: { staticText: template.text, dynamic: false };
|
|
925
|
+
const staticText = templated.staticText;
|
|
926
|
+
return {
|
|
927
|
+
staticText,
|
|
928
|
+
dynamic: templated.dynamic && !parameterizedTag,
|
|
929
|
+
concatenated: false,
|
|
930
|
+
literal: !ts.isTemplateExpression(template),
|
|
931
|
+
parameterizedTag,
|
|
932
|
+
};
|
|
933
|
+
}
|
|
934
|
+
if (ts.isTemplateExpression(node)) {
|
|
935
|
+
const templated = templateShape(ts, node, scope);
|
|
936
|
+
return {
|
|
937
|
+
staticText: templated.staticText,
|
|
938
|
+
dynamic: templated.dynamic,
|
|
939
|
+
concatenated: [node.head, ...node.templateSpans.map((span) => span.literal)].every((part) => part.text.trim() === ""),
|
|
940
|
+
literal: false,
|
|
941
|
+
parameterizedTag: false,
|
|
942
|
+
};
|
|
943
|
+
}
|
|
944
|
+
if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.PlusToken) {
|
|
945
|
+
const left = sqlExpressionShape(ts, node.left, scope, trail);
|
|
946
|
+
const right = sqlExpressionShape(ts, node.right, scope, trail);
|
|
947
|
+
return {
|
|
948
|
+
staticText: left.staticText + right.staticText,
|
|
949
|
+
dynamic: left.dynamic || right.dynamic || left.parameterizedTag || right.parameterizedTag,
|
|
950
|
+
concatenated: true,
|
|
951
|
+
literal: left.literal && right.literal,
|
|
952
|
+
parameterizedTag: false,
|
|
953
|
+
};
|
|
954
|
+
}
|
|
955
|
+
if (scope !== undefined) {
|
|
956
|
+
const resolved = resolveStaticSqlText(ts, node, scope, 0);
|
|
957
|
+
if (resolved !== undefined) {
|
|
958
|
+
return {
|
|
959
|
+
staticText: resolved,
|
|
960
|
+
dynamic: false,
|
|
961
|
+
concatenated: false,
|
|
962
|
+
literal: false,
|
|
963
|
+
parameterizedTag: false,
|
|
964
|
+
};
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
if (ts.isCallExpression(node)) {
|
|
968
|
+
const inner = passedThrough(ts, node);
|
|
969
|
+
if (inner !== undefined)
|
|
970
|
+
return { ...sqlExpressionShape(ts, inner, scope, trail), literal: false };
|
|
971
|
+
const parts = joinedParts(ts, node);
|
|
972
|
+
if (parts !== undefined) {
|
|
973
|
+
return joinShapes(parts.map((part) => typeof part === "string" ? textShape(part) : sqlExpressionShape(ts, part, scope, trail)));
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
if (ts.isIdentifier(node)) {
|
|
977
|
+
const read = variableWrites(ts, node);
|
|
978
|
+
if (read !== undefined && trail.includes(read.key))
|
|
979
|
+
return { ...textShape(""), literal: false };
|
|
980
|
+
if (read !== undefined && trail.length <= CONST_RESOLUTION_DEPTH) {
|
|
981
|
+
const written = writtenShape(ts, node.text, read.writes, scope, [...trail, read.key]);
|
|
982
|
+
return { ...written, literal: false };
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
return UNKNOWN_SQL;
|
|
986
|
+
}
|
|
987
|
+
function sqlMethod(ts, call) {
|
|
988
|
+
const sink = (method, receiver) => SQL_METHODS.has(method) || DRIVER_READ_METHODS.has(method)
|
|
989
|
+
? { method, receiver: receiver?.getText() }
|
|
990
|
+
: undefined;
|
|
991
|
+
const callee = call.expression;
|
|
992
|
+
if (ts.isPropertyAccessExpression(callee) &&
|
|
993
|
+
callee.name.text === "raw" &&
|
|
994
|
+
ts.isIdentifier(callee.expression) &&
|
|
995
|
+
callee.expression.text === "$state") {
|
|
996
|
+
const source = callee.getSourceFile();
|
|
997
|
+
const imported = source.statements.some((statement) => {
|
|
998
|
+
if (ts.isImportEqualsDeclaration(statement))
|
|
999
|
+
return statement.name.text === "$state";
|
|
1000
|
+
if (!ts.isImportDeclaration(statement))
|
|
1001
|
+
return false;
|
|
1002
|
+
const clause = statement.importClause;
|
|
1003
|
+
const bindings = clause?.namedBindings;
|
|
1004
|
+
return (clause?.name?.text === "$state" ||
|
|
1005
|
+
(bindings !== undefined &&
|
|
1006
|
+
(ts.isNamespaceImport(bindings)
|
|
1007
|
+
? bindings.name.text === "$state"
|
|
1008
|
+
: bindings.elements.some((binding) => binding.name.text === "$state"))));
|
|
1009
|
+
});
|
|
1010
|
+
let bound = imported || isShadowedAt(ts, callee.expression, "$state");
|
|
1011
|
+
for (let scope = callee.parent; scope !== undefined && !bound; scope = scope.parent) {
|
|
1012
|
+
bound = directBinding(ts, source, scope, "$state") !== undefined;
|
|
1013
|
+
}
|
|
1014
|
+
if (/\.svelte(?:\.[jt]s)?$/.test(source.fileName) && !bound)
|
|
1015
|
+
return undefined;
|
|
1016
|
+
}
|
|
1017
|
+
if (ts.isIdentifier(callee))
|
|
1018
|
+
return sink(callee.text, undefined);
|
|
1019
|
+
if (ts.isPropertyAccessExpression(callee))
|
|
1020
|
+
return sink(callee.name.text, callee.expression);
|
|
1021
|
+
if (ts.isElementAccessExpression(callee) && ts.isStringLiteralLike(callee.argumentExpression)) {
|
|
1022
|
+
return sink(callee.argumentExpression.text, callee.expression);
|
|
1023
|
+
}
|
|
1024
|
+
return undefined;
|
|
1025
|
+
}
|
|
1026
|
+
const SQL_RECEIVER = /(?:^|\.)(?:db|sql|prisma|drizzle|database|conn|connection|client|tx)$/i;
|
|
1027
|
+
function isAssembledSql(sink, shape) {
|
|
1028
|
+
if (!shape.dynamic)
|
|
1029
|
+
return false;
|
|
1030
|
+
if (sink.receiver === undefined || DRIVER_READ_METHODS.has(sink.method)) {
|
|
1031
|
+
return STATEMENT_START.test(shape.staticText);
|
|
1032
|
+
}
|
|
1033
|
+
return (SQL_KEYWORD.test(shape.staticText) || (shape.concatenated && SQL_RECEIVER.test(sink.receiver)));
|
|
1034
|
+
}
|
|
1035
|
+
function statementArgument(ts, argument) {
|
|
1036
|
+
const node = unwrapExpression(ts, argument);
|
|
1037
|
+
if (!ts.isObjectLiteralExpression(node))
|
|
1038
|
+
return argument;
|
|
1039
|
+
for (const property of node.properties) {
|
|
1040
|
+
if (ts.isShorthandPropertyAssignment(property) && STATEMENT_KEYS.has(property.name.text)) {
|
|
1041
|
+
return property.name;
|
|
1042
|
+
}
|
|
1043
|
+
if (ts.isPropertyAssignment(property) &&
|
|
1044
|
+
(ts.isIdentifier(property.name) || ts.isStringLiteral(property.name)) &&
|
|
1045
|
+
STATEMENT_KEYS.has(property.name.text)) {
|
|
1046
|
+
return property.initializer;
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
return undefined;
|
|
1050
|
+
}
|
|
1051
|
+
const STATEMENT_KEYS = new Set(["text", "sql"]);
|
|
1052
|
+
function statementHelpers(ts, source) {
|
|
1053
|
+
const helpers = new Map();
|
|
1054
|
+
const visit = (node) => {
|
|
1055
|
+
const sink = ts.isCallExpression(node) ? sqlMethod(ts, node) : undefined;
|
|
1056
|
+
const passed = sink !== undefined && ts.isCallExpression(node) ? node.arguments[0] : undefined;
|
|
1057
|
+
const argument = passed === undefined ? undefined : statementArgument(ts, passed);
|
|
1058
|
+
const statement = argument === undefined ? undefined : unwrapExpression(ts, argument);
|
|
1059
|
+
if (sink !== undefined && statement !== undefined && ts.isIdentifier(statement)) {
|
|
1060
|
+
const binding = nameBinding(ts, statement);
|
|
1061
|
+
const name = binding.kind === "parameter" ? functionName(ts, binding.fn) : undefined;
|
|
1062
|
+
if (binding.kind === "parameter" && name !== undefined && !helpers.has(name)) {
|
|
1063
|
+
helpers.set(name, { position: binding.position, sink });
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
ts.forEachChild(node, visit);
|
|
1067
|
+
};
|
|
1068
|
+
visit(source);
|
|
1069
|
+
return helpers;
|
|
1070
|
+
}
|
|
1071
|
+
function functionName(ts, fn) {
|
|
1072
|
+
if (ts.isFunctionDeclaration(fn))
|
|
1073
|
+
return fn.name?.text;
|
|
1074
|
+
if ((ts.isArrowFunction(fn) || ts.isFunctionExpression(fn)) &&
|
|
1075
|
+
ts.isVariableDeclaration(fn.parent) &&
|
|
1076
|
+
ts.isIdentifier(fn.parent.name)) {
|
|
1077
|
+
return fn.parent.name.text;
|
|
1078
|
+
}
|
|
1079
|
+
return undefined;
|
|
1080
|
+
}
|
|
1081
|
+
function literalsBlanked(ts, source, content) {
|
|
1082
|
+
const out = content.split("");
|
|
1083
|
+
const visit = (node) => {
|
|
1084
|
+
if (ts.isStringLiteralLike(node) ||
|
|
1085
|
+
ts.isTemplateHead(node) ||
|
|
1086
|
+
ts.isTemplateMiddle(node) ||
|
|
1087
|
+
ts.isTemplateTail(node) ||
|
|
1088
|
+
ts.isRegularExpressionLiteral(node) ||
|
|
1089
|
+
ts.isJsxText(node)) {
|
|
1090
|
+
for (let i = node.getStart(source); i < node.end; i++)
|
|
1091
|
+
if (out[i] !== "\n")
|
|
1092
|
+
out[i] = " ";
|
|
1093
|
+
return;
|
|
1094
|
+
}
|
|
1095
|
+
ts.forEachChild(node, visit);
|
|
1096
|
+
};
|
|
1097
|
+
visit(source);
|
|
1098
|
+
return out.join("");
|
|
1099
|
+
}
|
|
1100
|
+
const SQL_DYNAMIC_EXPECTATION = ["NF-C006", "sql-dynamic"];
|
|
1101
|
+
export function scanInterpolatedSql(file, content, ts, imports) {
|
|
1102
|
+
return scanSqlSites(file, content, ts, imports).filter((site) => site.expected === undefined);
|
|
1103
|
+
}
|
|
1104
|
+
export function scanSqlSites(file, content, ts, imports, parse = (path, text) => parseSqlSource(ts, path, text)) {
|
|
1105
|
+
const out = [];
|
|
1106
|
+
const lines = content.split("\n");
|
|
1107
|
+
const source = parse(file, content);
|
|
1108
|
+
if (source === undefined)
|
|
1109
|
+
return out;
|
|
1110
|
+
const scope = { module: sqlModuleScope(ts, file, source), imports };
|
|
1111
|
+
const helpers = statementHelpers(ts, source);
|
|
1112
|
+
let markerLines;
|
|
1113
|
+
const visit = (node) => {
|
|
1114
|
+
if (ts.isCallExpression(node)) {
|
|
1115
|
+
const helper = ts.isIdentifier(node.expression)
|
|
1116
|
+
? helpers.get(node.expression.text)
|
|
1117
|
+
: undefined;
|
|
1118
|
+
const sink = helper?.sink ?? sqlMethod(ts, node);
|
|
1119
|
+
const passed = node.arguments[helper?.position ?? 0];
|
|
1120
|
+
const argument = passed === undefined ? undefined : statementArgument(ts, passed);
|
|
1121
|
+
if (sink !== undefined && argument !== undefined) {
|
|
1122
|
+
const shape = sqlExpressionShape(ts, argument, scope);
|
|
1123
|
+
const unsafeEscape = helper === undefined &&
|
|
1124
|
+
sink.receiver !== undefined &&
|
|
1125
|
+
ALWAYS_UNSAFE.has(sink.method) &&
|
|
1126
|
+
!shape.literal &&
|
|
1127
|
+
!shape.parameterizedTag;
|
|
1128
|
+
if (unsafeEscape || isAssembledSql(sink, shape)) {
|
|
1129
|
+
const index = node.getStart(source);
|
|
1130
|
+
const line = source.getLineAndCharacterOfPosition(index).line + 1;
|
|
1131
|
+
markerLines ??= literalsBlanked(ts, source, content).split("\n");
|
|
1132
|
+
const reason = commentBlockExpectation(markerLines, line, SQL_DYNAMIC_EXPECTATION)?.reason;
|
|
1133
|
+
const site = { file, line, snippet: (lines[line - 1] ?? "").trim() };
|
|
1134
|
+
out.push(reason === undefined ? site : { ...site, expected: reason });
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
ts.forEachChild(node, visit);
|
|
1139
|
+
};
|
|
1140
|
+
visit(source);
|
|
1141
|
+
return out;
|
|
1142
|
+
}
|
|
1143
|
+
export async function scanProjectSql(cwd) {
|
|
1144
|
+
const sourceFiles = [];
|
|
1145
|
+
await walkSource(cwd, (file, content) => sourceFiles.push({ file, content }));
|
|
1146
|
+
const contents = new Map(sourceFiles.map(({ file, content }) => [file, content]));
|
|
1147
|
+
const sourceIndex = {
|
|
1148
|
+
files: sourceFiles.map(({ file }) => file),
|
|
1149
|
+
read: (file) => contents.get(file),
|
|
1150
|
+
};
|
|
1151
|
+
const loaded = await loadProjectTypeScript(cwd, undefined, sourceIndex);
|
|
1152
|
+
if (loaded.compiler === undefined)
|
|
1153
|
+
return undefined;
|
|
1154
|
+
const out = [];
|
|
1155
|
+
const imports = createProjectSqlImports(cwd);
|
|
1156
|
+
try {
|
|
1157
|
+
for (const { file, content } of sourceFiles) {
|
|
1158
|
+
out.push(...scanInterpolatedSql(file, content, loaded.compiler, imports));
|
|
1159
|
+
}
|
|
1160
|
+
return out.sort(bySite);
|
|
1161
|
+
}
|
|
1162
|
+
finally {
|
|
1163
|
+
await loaded.session?.close();
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
const SQL_DECLARATION_FILE = /\.d\.[cm]?ts$/;
|
|
1167
|
+
const SQL_SOURCE_FILE = /\.[cm]?tsx?$/;
|
|
1168
|
+
export function createProjectSqlImports(cwd) {
|
|
1169
|
+
const root = realpathOrSelf(resolve(cwd));
|
|
1170
|
+
return createSqlImports((fromModule, specifier) => {
|
|
1171
|
+
if (!isRelativeSpecifier(specifier))
|
|
1172
|
+
return undefined;
|
|
1173
|
+
try {
|
|
1174
|
+
const fromAbs = isAbsolute(fromModule) ? fromModule : join(root, fromModule);
|
|
1175
|
+
const resolved = realpathSync(Bun.resolveSync(specifier, dirname(fromAbs)));
|
|
1176
|
+
if (!resolved.startsWith(root + sep))
|
|
1177
|
+
return undefined;
|
|
1178
|
+
const rel = resolved.slice(root.length + 1);
|
|
1179
|
+
if (IGNORED_DIR.test(rel))
|
|
1180
|
+
return undefined;
|
|
1181
|
+
if (SQL_DECLARATION_FILE.test(resolved) || !SQL_SOURCE_FILE.test(resolved))
|
|
1182
|
+
return undefined;
|
|
1183
|
+
return { id: resolved, content: readFileSync(resolved, "utf8") };
|
|
1184
|
+
}
|
|
1185
|
+
catch {
|
|
1186
|
+
return undefined;
|
|
1187
|
+
}
|
|
1188
|
+
});
|
|
1189
|
+
}
|
|
1190
|
+
//# sourceMappingURL=check-scan-sql.js.map
|