@ts-safeql/plugin-kysely 0.0.0-stage → 0.2.1
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/LICENSE +21 -0
- package/dist/index.cjs +873 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.mjs +864 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +63 -3
- package/README.md +0 -3
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,864 @@
|
|
|
1
|
+
import { ast, definePlugin } from '@ts-safeql/plugin-utils';
|
|
2
|
+
import ts from 'typescript';
|
|
3
|
+
import { sql, Kysely, PostgresQueryCompiler, PostgresIntrospector, DummyDriver, PostgresAdapter, CamelCasePlugin, ParseJSONResultsPlugin, DeduplicateJoinsPlugin, WithSchemaPlugin, HandleEmptyInListsPlugin } from 'kysely';
|
|
4
|
+
import { createRequire } from 'module';
|
|
5
|
+
import fs from 'fs';
|
|
6
|
+
import path from 'path';
|
|
7
|
+
|
|
8
|
+
const builderPluginClasses = /* @__PURE__ */ new Map([
|
|
9
|
+
["CamelCasePlugin", CamelCasePlugin],
|
|
10
|
+
["ParseJSONResultsPlugin", ParseJSONResultsPlugin],
|
|
11
|
+
["DeduplicateJoinsPlugin", DeduplicateJoinsPlugin],
|
|
12
|
+
["WithSchemaPlugin", WithSchemaPlugin],
|
|
13
|
+
["HandleEmptyInListsPlugin", HandleEmptyInListsPlugin]
|
|
14
|
+
]);
|
|
15
|
+
const sqlHelperNames = new Set(Object.keys(sql));
|
|
16
|
+
const forbiddenMemberNames = /* @__PURE__ */ new Set([
|
|
17
|
+
"constructor",
|
|
18
|
+
"prototype",
|
|
19
|
+
"__proto__",
|
|
20
|
+
"__defineGetter__",
|
|
21
|
+
"__defineSetter__",
|
|
22
|
+
"__lookupGetter__",
|
|
23
|
+
"__lookupSetter__",
|
|
24
|
+
"call",
|
|
25
|
+
"apply",
|
|
26
|
+
"bind",
|
|
27
|
+
"toString",
|
|
28
|
+
"toLocaleString",
|
|
29
|
+
"valueOf",
|
|
30
|
+
"hasOwnProperty",
|
|
31
|
+
"isPrototypeOf",
|
|
32
|
+
"propertyIsEnumerable"
|
|
33
|
+
]);
|
|
34
|
+
const codeEvaluatingConstructors = /* @__PURE__ */ new Set([
|
|
35
|
+
Function,
|
|
36
|
+
Object.getPrototypeOf(async () => {
|
|
37
|
+
}).constructor,
|
|
38
|
+
Object.getPrototypeOf(function* () {
|
|
39
|
+
}).constructor,
|
|
40
|
+
Object.getPrototypeOf(async function* () {
|
|
41
|
+
}).constructor
|
|
42
|
+
]);
|
|
43
|
+
const intrinsicPrototypes = /* @__PURE__ */ new Set([
|
|
44
|
+
Object.prototype,
|
|
45
|
+
Function.prototype,
|
|
46
|
+
Array.prototype,
|
|
47
|
+
Promise.prototype
|
|
48
|
+
]);
|
|
49
|
+
function createCompilerDatabase() {
|
|
50
|
+
return new Kysely({
|
|
51
|
+
dialect: {
|
|
52
|
+
createAdapter: () => new PostgresAdapter(),
|
|
53
|
+
createDriver: () => new DummyDriver(),
|
|
54
|
+
createIntrospector: (db) => new PostgresIntrospector(db),
|
|
55
|
+
createQueryCompiler: () => new PostgresQueryCompiler()
|
|
56
|
+
},
|
|
57
|
+
plugins: []
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
function readMember(receiver, name) {
|
|
61
|
+
if (!isInheritedBuilderMember(receiver, name)) {
|
|
62
|
+
return ast.UNRESOLVED;
|
|
63
|
+
}
|
|
64
|
+
return guarded(() => Reflect.get(receiver, name));
|
|
65
|
+
}
|
|
66
|
+
function callMethod(receiver, name, args) {
|
|
67
|
+
if (!isInheritedBuilderMember(receiver, name)) {
|
|
68
|
+
return ast.UNRESOLVED;
|
|
69
|
+
}
|
|
70
|
+
return guarded(() => {
|
|
71
|
+
const method = Reflect.get(receiver, name);
|
|
72
|
+
return isCallable(method) ? Reflect.apply(method, receiver, args) : ast.UNRESOLVED;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
function callSqlTag(strings, values) {
|
|
76
|
+
const template = Object.assign([...strings], { raw: [...strings] });
|
|
77
|
+
return guarded(() => sql(template, ...values));
|
|
78
|
+
}
|
|
79
|
+
function callSqlHelper(name, args) {
|
|
80
|
+
if (!sqlHelperNames.has(name)) {
|
|
81
|
+
return ast.UNRESOLVED;
|
|
82
|
+
}
|
|
83
|
+
return guarded(() => {
|
|
84
|
+
const helper = Reflect.get(sql, name);
|
|
85
|
+
return isCallable(helper) ? Reflect.apply(helper, sql, args) : ast.UNRESOLVED;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function constructPlugin(name, args) {
|
|
89
|
+
const PluginClass = builderPluginClasses.get(name);
|
|
90
|
+
if (PluginClass === void 0) {
|
|
91
|
+
return ast.UNRESOLVED;
|
|
92
|
+
}
|
|
93
|
+
return guarded(() => Reflect.construct(PluginClass, args));
|
|
94
|
+
}
|
|
95
|
+
function getCompiledSql(compiled) {
|
|
96
|
+
return isObject(compiled) && "sql" in compiled && typeof compiled.sql === "string" ? compiled.sql : void 0;
|
|
97
|
+
}
|
|
98
|
+
function guarded(run) {
|
|
99
|
+
try {
|
|
100
|
+
const value = run();
|
|
101
|
+
return isObject(value) ? value : ast.UNRESOLVED;
|
|
102
|
+
} catch {
|
|
103
|
+
return ast.UNRESOLVED;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function isCallable(value) {
|
|
107
|
+
return typeof value === "function" && !codeEvaluatingConstructors.has(value);
|
|
108
|
+
}
|
|
109
|
+
function isObject(value) {
|
|
110
|
+
return typeof value === "object" && value !== null;
|
|
111
|
+
}
|
|
112
|
+
function isInheritedBuilderMember(receiver, name) {
|
|
113
|
+
if (!isObject(receiver) || forbiddenMemberNames.has(name) || Object.hasOwn(receiver, name)) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
const owner = findPrototypeDeclaring(receiver, name);
|
|
117
|
+
return owner !== void 0 && !intrinsicPrototypes.has(owner);
|
|
118
|
+
}
|
|
119
|
+
function findPrototypeDeclaring(receiver, name) {
|
|
120
|
+
let prototype = Object.getPrototypeOf(receiver);
|
|
121
|
+
while (prototype !== null) {
|
|
122
|
+
if (Object.hasOwn(prototype, name)) {
|
|
123
|
+
return prototype;
|
|
124
|
+
}
|
|
125
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
126
|
+
}
|
|
127
|
+
return void 0;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const BOOLEAN = { kind: "type", value: "boolean" };
|
|
131
|
+
function createBuilderTypeCheck(fragments) {
|
|
132
|
+
return (ctx) => {
|
|
133
|
+
const reports = fragments.map((fragment) => checkFragment(fragment, ctx)).filter((report) => report !== void 0);
|
|
134
|
+
return reports.length > 0 ? reports : void 0;
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function checkFragment(fragment, ctx) {
|
|
138
|
+
const tag = ctx.parser.tsNodeToESTreeNodeMap.get(fragment.tag);
|
|
139
|
+
if (tag === void 0 || tag.type !== "TaggedTemplateExpression") {
|
|
140
|
+
return void 0;
|
|
141
|
+
}
|
|
142
|
+
const typeParameter = tag.typeArguments;
|
|
143
|
+
const annotation = typeParameter?.params[0];
|
|
144
|
+
if (typeParameter === void 0 || annotation === void 0) {
|
|
145
|
+
return void 0;
|
|
146
|
+
}
|
|
147
|
+
const actual = actualTypeOf(fragment, ctx.output);
|
|
148
|
+
const expected = expectedTypeOf(fragment, annotation, ctx);
|
|
149
|
+
if (actual === null || expected === null || isEqualIgnoringQuoteStyle(expected, actual, ctx)) {
|
|
150
|
+
return void 0;
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
kind: "incorrect-type-annotation",
|
|
154
|
+
typeParameter,
|
|
155
|
+
expected: ctx.getComparableString(expected),
|
|
156
|
+
actual: ctx.getComparableString(actual)
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function actualTypeOf(fragment, output) {
|
|
160
|
+
if (fragment.kind === "condition") {
|
|
161
|
+
return BOOLEAN;
|
|
162
|
+
}
|
|
163
|
+
if (output?.kind !== "object") {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
return output.value.find(([key]) => key === fragment.alias)?.[1] ?? null;
|
|
167
|
+
}
|
|
168
|
+
function expectedTypeOf(fragment, annotation, ctx) {
|
|
169
|
+
if (fragment.kind === "condition" && isBooleanAnnotation(annotation, ctx)) {
|
|
170
|
+
return BOOLEAN;
|
|
171
|
+
}
|
|
172
|
+
return ctx.resolveExpectedType(annotation);
|
|
173
|
+
}
|
|
174
|
+
function isBooleanAnnotation(annotation, ctx) {
|
|
175
|
+
const tsNode = ctx.parser.esTreeNodeToTSNodeMap.get(annotation);
|
|
176
|
+
return isBooleanType(ctx.checker.getTypeAtLocation(tsNode));
|
|
177
|
+
}
|
|
178
|
+
function isBooleanType(type) {
|
|
179
|
+
if ((type.flags & (ts.TypeFlags.Boolean | ts.TypeFlags.BooleanLiteral)) !== 0) {
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
return type.isUnionOrIntersection() && type.types.some(isBooleanType);
|
|
183
|
+
}
|
|
184
|
+
function isEqualIgnoringQuoteStyle(expected, actual, ctx) {
|
|
185
|
+
const canonical = (target) => ctx.getComparableString(target).replace(/'/g, '"');
|
|
186
|
+
return canonical(expected) === canonical(actual);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const MIGRATION_FILE = /\.(?:m|c)?[jt]s$/;
|
|
190
|
+
const DECLARATION_FILE = /\.d\.(?:m|c)?ts$/;
|
|
191
|
+
class TsxFileMigrationProvider {
|
|
192
|
+
constructor(migrationFolder) {
|
|
193
|
+
this.migrationFolder = migrationFolder;
|
|
194
|
+
}
|
|
195
|
+
async getMigrations() {
|
|
196
|
+
const folder = path.resolve(this.migrationFolder);
|
|
197
|
+
if (!fs.existsSync(folder)) {
|
|
198
|
+
return {};
|
|
199
|
+
}
|
|
200
|
+
const files = fs.readdirSync(folder).filter((file) => MIGRATION_FILE.test(file) && !DECLARATION_FILE.test(file)).sort();
|
|
201
|
+
const migrations = {};
|
|
202
|
+
for (const file of files) {
|
|
203
|
+
const fullPath = path.join(folder, file);
|
|
204
|
+
const name = file.replace(MIGRATION_FILE, "");
|
|
205
|
+
if (name in migrations) {
|
|
206
|
+
throw new Error(
|
|
207
|
+
`Duplicate Kysely migration name "${name}" (e.g. two files differing only by extension). Migration names must be unique.`
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
const mod = loadModule(fullPath);
|
|
211
|
+
migrations[name] = { up: mod.up, down: mod.down };
|
|
212
|
+
}
|
|
213
|
+
return migrations;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function loadModule(absolutePath) {
|
|
217
|
+
const tsx = loadTsx();
|
|
218
|
+
const mod = tsx.require(absolutePath, import.meta.url);
|
|
219
|
+
const resolved = mod.default ?? mod;
|
|
220
|
+
if (typeof resolved !== "object" || resolved === null || typeof resolved.up !== "function" || resolved.down !== void 0 && typeof resolved.down !== "function") {
|
|
221
|
+
throw new Error(
|
|
222
|
+
`Kysely migration "${absolutePath}" must export an \`up\` function (\`down\` is optional).`
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
return resolved;
|
|
226
|
+
}
|
|
227
|
+
function loadTsx() {
|
|
228
|
+
try {
|
|
229
|
+
return createRequire(import.meta.url)("tsx/cjs/api");
|
|
230
|
+
} catch (cause) {
|
|
231
|
+
throw new Error(
|
|
232
|
+
'SafeQL could not load "tsx", which is required to run Kysely TypeScript migrations. Install it in your project (e.g. `npm i -D tsx`).',
|
|
233
|
+
{ cause }
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
async function migrate({ databaseUrl, migrationsDir }) {
|
|
238
|
+
const { Kysely, PostgresDialect } = await loadKysely();
|
|
239
|
+
const { Migrator } = await loadKyselyMigration();
|
|
240
|
+
const { Pool } = await loadPg();
|
|
241
|
+
const pool = new Pool({ connectionString: databaseUrl });
|
|
242
|
+
try {
|
|
243
|
+
const db = new Kysely({ dialect: new PostgresDialect({ pool }) });
|
|
244
|
+
const migrator = new Migrator({
|
|
245
|
+
db,
|
|
246
|
+
provider: new TsxFileMigrationProvider(migrationsDir)
|
|
247
|
+
});
|
|
248
|
+
const { error, results } = await migrator.migrateToLatest();
|
|
249
|
+
if (error !== void 0) {
|
|
250
|
+
const failed = results?.find((result) => result.status === "Error");
|
|
251
|
+
const where = failed ? ` (in "${failed.migrationName}")` : "";
|
|
252
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
253
|
+
throw new Error(`Kysely migration failed${where}: ${reason}`);
|
|
254
|
+
}
|
|
255
|
+
} finally {
|
|
256
|
+
await pool.end();
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
async function loadKysely() {
|
|
260
|
+
try {
|
|
261
|
+
return await import('kysely');
|
|
262
|
+
} catch (cause) {
|
|
263
|
+
throw new Error(
|
|
264
|
+
'SafeQL could not load "kysely". Install it in your project to run Kysely migrations.',
|
|
265
|
+
{ cause }
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
async function loadKyselyMigration(importModule = (specifier) => import(specifier)) {
|
|
270
|
+
for (const specifier of ["kysely/migration", "kysely"]) {
|
|
271
|
+
const mod = await importModule(specifier).catch(() => void 0);
|
|
272
|
+
if (isKyselyMigrationModule(mod)) return mod;
|
|
273
|
+
}
|
|
274
|
+
throw new Error(
|
|
275
|
+
'SafeQL could not load the Kysely migrator. Install "kysely" in your project to run Kysely migrations.'
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
function isKyselyMigrationModule(mod) {
|
|
279
|
+
return typeof mod === "object" && mod !== null && "Migrator" in mod && typeof mod.Migrator === "function";
|
|
280
|
+
}
|
|
281
|
+
async function loadPg() {
|
|
282
|
+
try {
|
|
283
|
+
return await import('pg');
|
|
284
|
+
} catch (cause) {
|
|
285
|
+
throw new Error(
|
|
286
|
+
'SafeQL could not load "pg". Install it in your project to run Kysely migrations.',
|
|
287
|
+
{ cause }
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const terminalBuilderMethods = /* @__PURE__ */ new Set([
|
|
293
|
+
"execute",
|
|
294
|
+
"executeTakeFirst",
|
|
295
|
+
"executeTakeFirstOrThrow",
|
|
296
|
+
"compile",
|
|
297
|
+
"stream"
|
|
298
|
+
]);
|
|
299
|
+
const kyselyBuilderTypeNames = /* @__PURE__ */ new Set([
|
|
300
|
+
"QueryCreator",
|
|
301
|
+
"Kysely",
|
|
302
|
+
"Transaction",
|
|
303
|
+
"ControlledTransaction"
|
|
304
|
+
]);
|
|
305
|
+
function isKyselyBuilderRoot(node, checker) {
|
|
306
|
+
if (!ts.isIdentifier(node) && !ts.isPropertyAccessExpression(node) && !ts.isElementAccessExpression(node) && node.kind !== ts.SyntaxKind.ThisKeyword) {
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
return typeDerivesFromKyselyBuilder(
|
|
310
|
+
checker.getApparentType(checker.getTypeAtLocation(node)),
|
|
311
|
+
checker
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
function typeDerivesFromKyselyBuilder(type, checker) {
|
|
315
|
+
const seen = /* @__PURE__ */ new Set();
|
|
316
|
+
const visit = (current) => {
|
|
317
|
+
if (seen.has(current)) return false;
|
|
318
|
+
seen.add(current);
|
|
319
|
+
if (current.isUnionOrIntersection()) {
|
|
320
|
+
return current.types.some(visit);
|
|
321
|
+
}
|
|
322
|
+
if (symbolIsKyselyBuilder(current.getSymbol() ?? current.aliasSymbol, checker)) {
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
const bases = current.isClassOrInterface() ? checker.getBaseTypes(current) : [];
|
|
326
|
+
return bases.some(visit);
|
|
327
|
+
};
|
|
328
|
+
return visit(type);
|
|
329
|
+
}
|
|
330
|
+
function symbolIsKyselyBuilder(symbol, checker) {
|
|
331
|
+
if (symbol === void 0 || !kyselyBuilderTypeNames.has(symbol.getName())) {
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
return ast.isSymbolImportedFrom({ checker, symbol, moduleName: "kysely" });
|
|
335
|
+
}
|
|
336
|
+
const whereLikeMethods = /* @__PURE__ */ new Set(["where", "having", "andWhere", "orWhere", "and", "or", "on"]);
|
|
337
|
+
const singleBoundParamOperators = /* @__PURE__ */ new Set([
|
|
338
|
+
"=",
|
|
339
|
+
"==",
|
|
340
|
+
"!=",
|
|
341
|
+
"<>",
|
|
342
|
+
"<",
|
|
343
|
+
"<=",
|
|
344
|
+
">",
|
|
345
|
+
">=",
|
|
346
|
+
"like",
|
|
347
|
+
"not like",
|
|
348
|
+
"ilike",
|
|
349
|
+
"not ilike",
|
|
350
|
+
"~",
|
|
351
|
+
"~*",
|
|
352
|
+
"!~",
|
|
353
|
+
"!~*",
|
|
354
|
+
"@>",
|
|
355
|
+
"<@"
|
|
356
|
+
]);
|
|
357
|
+
const BOUND_PARAM_STUB = 1;
|
|
358
|
+
const fragmentMarkerMethods = /* @__PURE__ */ new Set(["as", "toOperationNode"]);
|
|
359
|
+
const DEBUG_BUILDER_ENV_VAR = "SAFEQL_DEBUG_KYSELY_BUILDER";
|
|
360
|
+
const plugin = definePlugin({
|
|
361
|
+
name: "kysely",
|
|
362
|
+
package: "@ts-safeql/plugin-kysely",
|
|
363
|
+
setup(config = {}) {
|
|
364
|
+
const plugin = {
|
|
365
|
+
onTarget,
|
|
366
|
+
onExpression,
|
|
367
|
+
migrate
|
|
368
|
+
};
|
|
369
|
+
if (config.builder === true) {
|
|
370
|
+
return {
|
|
371
|
+
...plugin,
|
|
372
|
+
queryNodeKinds: [
|
|
373
|
+
{ kind: "CallExpression", callee: { property: { nameIn: [...terminalBuilderMethods] } } }
|
|
374
|
+
],
|
|
375
|
+
resolveQuery: (context) => resolveBuilderQuery(context)
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
return plugin;
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
function resolveBuilderQuery(context) {
|
|
382
|
+
const tsNode = context.tsNode;
|
|
383
|
+
if (!ts.isCallExpression(tsNode) || !isTerminalBuilderCall(tsNode)) {
|
|
384
|
+
return "skip";
|
|
385
|
+
}
|
|
386
|
+
const sourceFile = tsNode.getSourceFile();
|
|
387
|
+
const state = { hasEmbeddedSql: false, fragments: [], typedFragments: [] };
|
|
388
|
+
const builder = evaluateBuilderChain(
|
|
389
|
+
tsNode.expression.expression,
|
|
390
|
+
context.checker,
|
|
391
|
+
sourceFile,
|
|
392
|
+
state
|
|
393
|
+
);
|
|
394
|
+
if (builder === ast.UNRESOLVED) {
|
|
395
|
+
return skipBuilderQuery(
|
|
396
|
+
tsNode,
|
|
397
|
+
sourceFile,
|
|
398
|
+
"dynamic builder shape (a value drives the SQL text)"
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
if (!state.hasEmbeddedSql) {
|
|
402
|
+
return skipBuilderQuery(tsNode, sourceFile, "no embedded raw sql to validate");
|
|
403
|
+
}
|
|
404
|
+
const resultSql = getCompiledSql(callMethod(builder, "compile", []));
|
|
405
|
+
if (!resultSql) {
|
|
406
|
+
return skipBuilderQuery(tsNode, sourceFile, "kysely could not compile the chain");
|
|
407
|
+
}
|
|
408
|
+
builderStats.validated += 1;
|
|
409
|
+
logBuilderDebug(`validated (${builderStats.validated} total): ${truncate(resultSql)}`);
|
|
410
|
+
return {
|
|
411
|
+
kind: "sql",
|
|
412
|
+
text: resultSql,
|
|
413
|
+
sourcemaps: buildBuilderSourcemaps({ state, resultSql, tsNode, sourceFile }),
|
|
414
|
+
...state.typedFragments.length > 0 ? { typeCheck: createBuilderTypeCheck(state.typedFragments) } : {}
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
function buildBuilderSourcemaps(params) {
|
|
418
|
+
const { state, resultSql, tsNode, sourceFile } = params;
|
|
419
|
+
const nodeStart = tsNode.getStart(sourceFile);
|
|
420
|
+
const sourceText = sourceFile.text;
|
|
421
|
+
const wholeChain = { start: nodeStart, end: tsNode.getEnd() };
|
|
422
|
+
const target = state.fragments.length === 1 ? state.fragments[0] : wholeChain;
|
|
423
|
+
const text = sourceText.slice(target.start, target.end);
|
|
424
|
+
return [
|
|
425
|
+
{
|
|
426
|
+
generated: { start: 0, end: resultSql.length, text: resultSql },
|
|
427
|
+
original: { start: target.start - nodeStart, end: target.end - nodeStart, text },
|
|
428
|
+
offset: 0
|
|
429
|
+
}
|
|
430
|
+
];
|
|
431
|
+
}
|
|
432
|
+
const builderStats = { validated: 0, skipped: 0 };
|
|
433
|
+
function skipBuilderQuery(node, sourceFile, reason) {
|
|
434
|
+
builderStats.skipped += 1;
|
|
435
|
+
logBuilderDebug(
|
|
436
|
+
`skipped (${builderStats.skipped} total): ${reason} \u2014 ${truncate(node.getText(sourceFile))}`
|
|
437
|
+
);
|
|
438
|
+
return "skip";
|
|
439
|
+
}
|
|
440
|
+
function logBuilderDebug(message) {
|
|
441
|
+
if (process.env[DEBUG_BUILDER_ENV_VAR]) {
|
|
442
|
+
console.error(`[safeql:kysely:builder] ${message}`);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
function truncate(text, max = 120) {
|
|
446
|
+
const flat = text.replace(/\s+/g, " ").trim();
|
|
447
|
+
return flat.length > max ? `${flat.slice(0, max)}\u2026` : flat;
|
|
448
|
+
}
|
|
449
|
+
function onTarget({
|
|
450
|
+
node,
|
|
451
|
+
context
|
|
452
|
+
}) {
|
|
453
|
+
const tsNode = context.parser.esTreeNodeToTSNodeMap.get(node);
|
|
454
|
+
if (!tsNode || !ts.isTaggedTemplateExpression(tsNode)) {
|
|
455
|
+
return void 0;
|
|
456
|
+
}
|
|
457
|
+
if (!isKyselyTag(tsNode.tag, context.checker)) {
|
|
458
|
+
return void 0;
|
|
459
|
+
}
|
|
460
|
+
if (isFragmentUsage(tsNode)) {
|
|
461
|
+
return false;
|
|
462
|
+
}
|
|
463
|
+
return {};
|
|
464
|
+
}
|
|
465
|
+
function onExpression({
|
|
466
|
+
context
|
|
467
|
+
}) {
|
|
468
|
+
return buildExpressionSQL(context.tsNode, context.checker);
|
|
469
|
+
}
|
|
470
|
+
function isTerminalBuilderCall(node) {
|
|
471
|
+
if (!ts.isPropertyAccessExpression(node.expression)) {
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
return terminalBuilderMethods.has(node.expression.name.text);
|
|
475
|
+
}
|
|
476
|
+
function asMethodCall(node) {
|
|
477
|
+
if (!ts.isCallExpression(node)) {
|
|
478
|
+
return void 0;
|
|
479
|
+
}
|
|
480
|
+
const callee = ast.unwrap({ node: node.expression });
|
|
481
|
+
if (!ts.isPropertyAccessExpression(callee)) {
|
|
482
|
+
return void 0;
|
|
483
|
+
}
|
|
484
|
+
return { receiver: callee.expression, name: callee.name.text, call: node };
|
|
485
|
+
}
|
|
486
|
+
function evaluateBuilderChain(node, checker, sourceFile, state) {
|
|
487
|
+
const unwrapped = ast.unwrap({ node });
|
|
488
|
+
if (!ts.isCallExpression(unwrapped) && isKyselyBuilderRoot(unwrapped, checker)) {
|
|
489
|
+
return createCompilerDatabase();
|
|
490
|
+
}
|
|
491
|
+
if (ts.isNewExpression(unwrapped)) {
|
|
492
|
+
return evaluatePluginConstruction(unwrapped, checker);
|
|
493
|
+
}
|
|
494
|
+
if (ts.isPropertyAccessExpression(unwrapped)) {
|
|
495
|
+
const receiver2 = evaluateBuilderChain(unwrapped.expression, checker, sourceFile, state);
|
|
496
|
+
return receiver2 === ast.UNRESOLVED ? receiver2 : readMember(receiver2, unwrapped.name.text);
|
|
497
|
+
}
|
|
498
|
+
const methodCall = asMethodCall(unwrapped);
|
|
499
|
+
if (methodCall === void 0) {
|
|
500
|
+
return ast.UNRESOLVED;
|
|
501
|
+
}
|
|
502
|
+
const receiver = evaluateBuilderChain(methodCall.receiver, checker, sourceFile, state);
|
|
503
|
+
if (receiver === ast.UNRESOLVED) {
|
|
504
|
+
return receiver;
|
|
505
|
+
}
|
|
506
|
+
const args = evaluateCallArguments(methodCall.call, checker, sourceFile, state);
|
|
507
|
+
return args === ast.UNRESOLVED ? args : callMethod(receiver, methodCall.name, args);
|
|
508
|
+
}
|
|
509
|
+
function evaluatePluginConstruction(node, checker) {
|
|
510
|
+
if (!ts.isIdentifier(node.expression)) {
|
|
511
|
+
return ast.UNRESOLVED;
|
|
512
|
+
}
|
|
513
|
+
const args = evaluateEach(
|
|
514
|
+
node.arguments ?? [],
|
|
515
|
+
(arg) => ast.getStaticValue({ node: arg, checker })
|
|
516
|
+
);
|
|
517
|
+
return args === ast.UNRESOLVED ? args : constructPlugin(node.expression.text, args);
|
|
518
|
+
}
|
|
519
|
+
function evaluateCallArguments(call, checker, sourceFile, state) {
|
|
520
|
+
if (isWhereLikeComparison(call, checker)) {
|
|
521
|
+
return evaluateWhereLikeComparisonArguments(call.arguments, checker);
|
|
522
|
+
}
|
|
523
|
+
return evaluateEach(
|
|
524
|
+
call.arguments,
|
|
525
|
+
(arg) => evaluateArgumentValue(arg, checker, sourceFile, state)
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
function evaluateEach(nodes, evaluate) {
|
|
529
|
+
const values = [];
|
|
530
|
+
for (const node of nodes) {
|
|
531
|
+
const value = ts.isSpreadElement(node) ? ast.UNRESOLVED : evaluate(node);
|
|
532
|
+
if (value === ast.UNRESOLVED) {
|
|
533
|
+
return ast.UNRESOLVED;
|
|
534
|
+
}
|
|
535
|
+
values.push(value);
|
|
536
|
+
}
|
|
537
|
+
return values;
|
|
538
|
+
}
|
|
539
|
+
function isWhereLikeComparison(call, checker) {
|
|
540
|
+
const methodName = getMethodName(call);
|
|
541
|
+
return methodName !== void 0 && whereLikeMethods.has(methodName) && call.arguments.length === 3 && !isKyselySqlExpression(call.arguments[0], checker);
|
|
542
|
+
}
|
|
543
|
+
function evaluateWhereLikeComparisonArguments(args, checker) {
|
|
544
|
+
const [columnArg, operatorArg, valueArg] = args;
|
|
545
|
+
const column = ast.getStaticValue({ node: columnArg, checker });
|
|
546
|
+
const operator = ast.getStaticValue({ node: operatorArg, checker });
|
|
547
|
+
if (typeof column !== "string" || typeof operator !== "string") {
|
|
548
|
+
return ast.UNRESOLVED;
|
|
549
|
+
}
|
|
550
|
+
const value = evaluateComparisonValue(valueArg, operator, checker);
|
|
551
|
+
return value === ast.UNRESOLVED ? value : [column, operator, value];
|
|
552
|
+
}
|
|
553
|
+
function evaluateComparisonValue(valueArg, operator, checker) {
|
|
554
|
+
const value = ast.getStaticValue({ node: valueArg, checker });
|
|
555
|
+
if (value !== ast.UNRESOLVED) {
|
|
556
|
+
return value;
|
|
557
|
+
}
|
|
558
|
+
return singleBoundParamOperators.has(operator) ? BOUND_PARAM_STUB : ast.UNRESOLVED;
|
|
559
|
+
}
|
|
560
|
+
function evaluateArgumentValue(arg, checker, sourceFile, state) {
|
|
561
|
+
const unwrapped = ast.unwrap({ node: arg });
|
|
562
|
+
if (ts.isFunctionLike(unwrapped) || ts.isSpreadElement(unwrapped)) {
|
|
563
|
+
return ast.UNRESOLVED;
|
|
564
|
+
}
|
|
565
|
+
if (containsKyselySqlFragment(unwrapped, checker)) {
|
|
566
|
+
return evaluateSqlExpressionChain(unwrapped, checker, sourceFile, state);
|
|
567
|
+
}
|
|
568
|
+
if (ts.isArrayLiteralExpression(unwrapped)) {
|
|
569
|
+
return evaluateEach(
|
|
570
|
+
unwrapped.elements,
|
|
571
|
+
(element) => evaluateArgumentValue(element, checker, sourceFile, state)
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
if (ts.isNewExpression(unwrapped)) {
|
|
575
|
+
return evaluatePluginConstruction(unwrapped, checker);
|
|
576
|
+
}
|
|
577
|
+
return ast.getStaticValue({ node: unwrapped, checker });
|
|
578
|
+
}
|
|
579
|
+
function isKyselySqlExpression(node, checker) {
|
|
580
|
+
return containsKyselySqlFragment(ast.unwrap({ node }), checker);
|
|
581
|
+
}
|
|
582
|
+
function containsKyselySqlFragment(node, checker) {
|
|
583
|
+
const u = ast.unwrap({ node });
|
|
584
|
+
if (isKyselySqlTaggedTemplate(u, checker)) return true;
|
|
585
|
+
if (ts.isCallExpression(u)) return containsKyselySqlFragment(u.expression, checker);
|
|
586
|
+
if (ts.isPropertyAccessExpression(u)) return containsKyselySqlFragment(u.expression, checker);
|
|
587
|
+
return false;
|
|
588
|
+
}
|
|
589
|
+
function isKyselySqlTaggedTemplate(node, checker) {
|
|
590
|
+
return ts.isTaggedTemplateExpression(node) && isKyselyTag(node.tag, checker);
|
|
591
|
+
}
|
|
592
|
+
function evaluateSqlExpressionChain(node, checker, sourceFile, state) {
|
|
593
|
+
const u = ast.unwrap({ node });
|
|
594
|
+
if (isKyselySqlTaggedTemplate(u, checker)) {
|
|
595
|
+
state.hasEmbeddedSql = true;
|
|
596
|
+
state.fragments.push({ start: u.getStart(sourceFile), end: u.getEnd() });
|
|
597
|
+
const typedFragment = classifyTypedFragment(u, checker);
|
|
598
|
+
if (typedFragment !== void 0) {
|
|
599
|
+
state.typedFragments.push(typedFragment);
|
|
600
|
+
}
|
|
601
|
+
return evaluateEmbeddedSqlFragment(u.template, checker);
|
|
602
|
+
}
|
|
603
|
+
if (ts.isPropertyAccessExpression(u)) {
|
|
604
|
+
const receiver2 = evaluateSqlExpressionChain(u.expression, checker, sourceFile, state);
|
|
605
|
+
return receiver2 === ast.UNRESOLVED ? receiver2 : readMember(receiver2, u.name.text);
|
|
606
|
+
}
|
|
607
|
+
const methodCall = asMethodCall(u);
|
|
608
|
+
if (methodCall === void 0) {
|
|
609
|
+
return ast.UNRESOLVED;
|
|
610
|
+
}
|
|
611
|
+
const receiver = evaluateSqlExpressionChain(methodCall.receiver, checker, sourceFile, state);
|
|
612
|
+
if (receiver === ast.UNRESOLVED) {
|
|
613
|
+
return receiver;
|
|
614
|
+
}
|
|
615
|
+
const args = evaluateEach(
|
|
616
|
+
methodCall.call.arguments,
|
|
617
|
+
(arg) => evaluateArgumentValue(arg, checker, sourceFile, state)
|
|
618
|
+
);
|
|
619
|
+
return args === ast.UNRESOLVED ? args : callMethod(receiver, methodCall.name, args);
|
|
620
|
+
}
|
|
621
|
+
function evaluateEmbeddedSqlFragment(template, checker, identifiersBeingResolved = /* @__PURE__ */ new Set()) {
|
|
622
|
+
if (ts.isNoSubstitutionTemplateLiteral(template)) {
|
|
623
|
+
return callSqlTag([template.text], []);
|
|
624
|
+
}
|
|
625
|
+
const spans = template.templateSpans;
|
|
626
|
+
const values = evaluateEach(
|
|
627
|
+
spans.map((span) => span.expression),
|
|
628
|
+
(expression) => evaluateInterpolation(expression, checker, identifiersBeingResolved)
|
|
629
|
+
);
|
|
630
|
+
if (values === ast.UNRESOLVED) {
|
|
631
|
+
return values;
|
|
632
|
+
}
|
|
633
|
+
return callSqlTag([template.head.text, ...spans.map((span) => span.literal.text)], values);
|
|
634
|
+
}
|
|
635
|
+
function evaluateInterpolation(expression, checker, identifiersBeingResolved) {
|
|
636
|
+
const u = ast.unwrap({ node: expression });
|
|
637
|
+
if (isKyselySqlTaggedTemplate(u, checker)) {
|
|
638
|
+
return evaluateEmbeddedSqlFragment(u.template, checker, identifiersBeingResolved);
|
|
639
|
+
}
|
|
640
|
+
if (isKyselyHelperCall(u, checker)) {
|
|
641
|
+
return evaluateSqlHelperCall(u, checker);
|
|
642
|
+
}
|
|
643
|
+
if (ts.isIdentifier(u)) {
|
|
644
|
+
return evaluateIdentifierInterpolation(u, checker, identifiersBeingResolved);
|
|
645
|
+
}
|
|
646
|
+
return BOUND_PARAM_STUB;
|
|
647
|
+
}
|
|
648
|
+
function evaluateIdentifierInterpolation(identifier, checker, identifiersBeingResolved) {
|
|
649
|
+
const symbol = checker.getSymbolAtLocation(identifier);
|
|
650
|
+
const initializer = ast.findInitializer({ identifier, checker });
|
|
651
|
+
if (symbol === void 0 || initializer === void 0 || identifiersBeingResolved.has(symbol)) {
|
|
652
|
+
return BOUND_PARAM_STUB;
|
|
653
|
+
}
|
|
654
|
+
return evaluateInterpolation(initializer, checker, new Set(identifiersBeingResolved).add(symbol));
|
|
655
|
+
}
|
|
656
|
+
function evaluateSqlHelperCall(call, checker) {
|
|
657
|
+
const args = evaluateEach(call.arguments, (arg) => evaluateSqlHelperArgument(arg, checker));
|
|
658
|
+
return args === ast.UNRESOLVED ? args : callSqlHelper(call.expression.name.text, args);
|
|
659
|
+
}
|
|
660
|
+
function evaluateSqlHelperArgument(arg, checker) {
|
|
661
|
+
const u = ast.unwrap({ node: arg });
|
|
662
|
+
if (isKyselySqlTaggedTemplate(u, checker)) {
|
|
663
|
+
return evaluateEmbeddedSqlFragment(u.template, checker);
|
|
664
|
+
}
|
|
665
|
+
if (isKyselyHelperCall(u, checker)) {
|
|
666
|
+
return evaluateSqlHelperCall(u, checker);
|
|
667
|
+
}
|
|
668
|
+
if (ts.isArrayLiteralExpression(u)) {
|
|
669
|
+
return evaluateEach(u.elements, (element) => evaluateSqlHelperArgument(element, checker));
|
|
670
|
+
}
|
|
671
|
+
return ast.getStaticValue({ node: u, checker });
|
|
672
|
+
}
|
|
673
|
+
function buildExpressionSQL(node, checker) {
|
|
674
|
+
const expression = ast.unwrap({ node });
|
|
675
|
+
if (ts.isTaggedTemplateExpression(expression) && isKyselyTag(expression.tag, checker)) {
|
|
676
|
+
return buildTemplateSQL(expression.template, checker);
|
|
677
|
+
}
|
|
678
|
+
if (ts.isIdentifier(expression)) {
|
|
679
|
+
const initializer = ast.findInitializer({ identifier: expression, checker });
|
|
680
|
+
return initializer ? buildExpressionSQL(initializer, checker) : void 0;
|
|
681
|
+
}
|
|
682
|
+
if (!ts.isCallExpression(expression) || !isKyselyHelperCall(expression, checker)) {
|
|
683
|
+
return void 0;
|
|
684
|
+
}
|
|
685
|
+
return buildHelperSQL(expression, checker);
|
|
686
|
+
}
|
|
687
|
+
function buildHelperSQL(call, checker) {
|
|
688
|
+
const method = getMethodName(call);
|
|
689
|
+
const args = call.arguments;
|
|
690
|
+
switch (method) {
|
|
691
|
+
case "val":
|
|
692
|
+
case "value":
|
|
693
|
+
return "$N";
|
|
694
|
+
case "ref":
|
|
695
|
+
case "table": {
|
|
696
|
+
const value = ast.getStaticValue({ node: args[0], checker });
|
|
697
|
+
return typeof value === "string" ? quoteIdentifierPath(value) : false;
|
|
698
|
+
}
|
|
699
|
+
case "id": {
|
|
700
|
+
const parts = [];
|
|
701
|
+
for (const arg of args) {
|
|
702
|
+
const value = ast.getStaticValue({ node: arg, checker });
|
|
703
|
+
if (typeof value !== "string") return false;
|
|
704
|
+
parts.push(value);
|
|
705
|
+
}
|
|
706
|
+
return parts.length > 0 ? parts.map(quoteIdentifier).join(".") : false;
|
|
707
|
+
}
|
|
708
|
+
case "lit":
|
|
709
|
+
case "literal": {
|
|
710
|
+
const value = ast.getStaticValue({ node: args[0], checker });
|
|
711
|
+
return formatLiteral(value);
|
|
712
|
+
}
|
|
713
|
+
case "raw": {
|
|
714
|
+
const value = ast.getStaticValue({ node: args[0], checker });
|
|
715
|
+
return typeof value === "string" ? value : false;
|
|
716
|
+
}
|
|
717
|
+
case "join":
|
|
718
|
+
return buildJoinSQL(call, checker);
|
|
719
|
+
default:
|
|
720
|
+
return void 0;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
function buildJoinSQL(call, checker) {
|
|
724
|
+
const values = ast.getStaticValue({ node: call.arguments[0], checker });
|
|
725
|
+
if (!Array.isArray(values)) return false;
|
|
726
|
+
const separator = getJoinSeparator(call.arguments[1], checker);
|
|
727
|
+
if (separator === false) return false;
|
|
728
|
+
return values.map(() => "$N").join(separator);
|
|
729
|
+
}
|
|
730
|
+
function getJoinSeparator(arg, checker) {
|
|
731
|
+
if (arg === void 0) return ", ";
|
|
732
|
+
const expression = ast.unwrap({ node: arg });
|
|
733
|
+
if (ts.isTaggedTemplateExpression(expression) && isKyselyTag(expression.tag, checker)) {
|
|
734
|
+
const text = buildTemplateSQL(expression.template, checker);
|
|
735
|
+
return typeof text === "string" ? text : false;
|
|
736
|
+
}
|
|
737
|
+
return false;
|
|
738
|
+
}
|
|
739
|
+
function buildTemplateSQL(template, checker) {
|
|
740
|
+
if (ts.isNoSubstitutionTemplateLiteral(template)) {
|
|
741
|
+
return template.text;
|
|
742
|
+
}
|
|
743
|
+
let sql = template.head.text;
|
|
744
|
+
for (const span of template.templateSpans) {
|
|
745
|
+
const resolved = buildExpressionSQL(span.expression, checker);
|
|
746
|
+
if (resolved === false) return false;
|
|
747
|
+
sql += typeof resolved === "string" ? resolved : "$N";
|
|
748
|
+
sql += span.literal.text;
|
|
749
|
+
}
|
|
750
|
+
return sql;
|
|
751
|
+
}
|
|
752
|
+
function classifyTypedFragment(node, checker) {
|
|
753
|
+
if (node.typeArguments === void 0 || node.typeArguments.length === 0) {
|
|
754
|
+
return void 0;
|
|
755
|
+
}
|
|
756
|
+
const alias = getSelectionAlias(node, checker);
|
|
757
|
+
if (alias !== void 0) {
|
|
758
|
+
return { kind: "column", alias, tag: node };
|
|
759
|
+
}
|
|
760
|
+
if (isConditionFragment(node)) {
|
|
761
|
+
return { kind: "condition", tag: node };
|
|
762
|
+
}
|
|
763
|
+
return void 0;
|
|
764
|
+
}
|
|
765
|
+
function climbTransparentWrappers(node) {
|
|
766
|
+
let current = node;
|
|
767
|
+
while (current.parent && (ts.isParenthesizedExpression(current.parent) || ts.isAsExpression(current.parent) || ts.isNonNullExpression(current.parent)) && current.parent.expression === current) {
|
|
768
|
+
current = current.parent;
|
|
769
|
+
}
|
|
770
|
+
return current;
|
|
771
|
+
}
|
|
772
|
+
function isConditionFragment(node) {
|
|
773
|
+
let current = climbTransparentWrappers(node);
|
|
774
|
+
let parent = current.parent;
|
|
775
|
+
while (parent && ts.isPropertyAccessExpression(parent) && parent.expression === current) {
|
|
776
|
+
current = parent;
|
|
777
|
+
parent = parent.parent;
|
|
778
|
+
}
|
|
779
|
+
if (!parent || !ts.isCallExpression(parent)) {
|
|
780
|
+
return false;
|
|
781
|
+
}
|
|
782
|
+
const isSoleArgument = parent.arguments.length === 1 && (parent.arguments[0] === current || ast.unwrap({ node: parent.arguments[0] }) === node);
|
|
783
|
+
if (!isSoleArgument) {
|
|
784
|
+
return false;
|
|
785
|
+
}
|
|
786
|
+
const callee = parent.expression;
|
|
787
|
+
if (!ts.isPropertyAccessExpression(callee)) {
|
|
788
|
+
return false;
|
|
789
|
+
}
|
|
790
|
+
return whereLikeMethods.has(callee.name.text);
|
|
791
|
+
}
|
|
792
|
+
function getSelectionAlias(node, checker) {
|
|
793
|
+
const fragment = climbTransparentWrappers(node);
|
|
794
|
+
const asAccess = fragment.parent;
|
|
795
|
+
if (!asAccess || !ts.isPropertyAccessExpression(asAccess) || asAccess.expression !== fragment || asAccess.name.text !== "as") {
|
|
796
|
+
return void 0;
|
|
797
|
+
}
|
|
798
|
+
const asCall = asAccess.parent;
|
|
799
|
+
if (!asCall || !ts.isCallExpression(asCall) || asCall.expression !== asAccess) {
|
|
800
|
+
return void 0;
|
|
801
|
+
}
|
|
802
|
+
const aliasArg = asCall.arguments[0];
|
|
803
|
+
if (aliasArg === void 0) {
|
|
804
|
+
return void 0;
|
|
805
|
+
}
|
|
806
|
+
const alias = ast.getStaticValue({ node: aliasArg, checker });
|
|
807
|
+
return typeof alias === "string" ? alias : void 0;
|
|
808
|
+
}
|
|
809
|
+
function isFragmentUsage(tsNode) {
|
|
810
|
+
if (tsNode.parent && ts.isTemplateSpan(tsNode.parent)) {
|
|
811
|
+
return true;
|
|
812
|
+
}
|
|
813
|
+
let current = tsNode;
|
|
814
|
+
let parent = current.parent;
|
|
815
|
+
while (parent) {
|
|
816
|
+
if (ts.isPropertyAccessExpression(parent) && parent.expression === current) {
|
|
817
|
+
if (fragmentMarkerMethods.has(parent.name.text)) return true;
|
|
818
|
+
current = parent;
|
|
819
|
+
parent = current.parent;
|
|
820
|
+
continue;
|
|
821
|
+
}
|
|
822
|
+
if ((ts.isCallExpression(parent) || ts.isAwaitExpression(parent) || ts.isParenthesizedExpression(parent) || ts.isAsExpression(parent) || ts.isNonNullExpression(parent)) && getInnerExpression(parent) === current) {
|
|
823
|
+
current = parent;
|
|
824
|
+
parent = current.parent;
|
|
825
|
+
continue;
|
|
826
|
+
}
|
|
827
|
+
break;
|
|
828
|
+
}
|
|
829
|
+
return parent !== void 0 && ts.isCallExpression(parent) && parent.arguments.some((arg) => arg === current);
|
|
830
|
+
}
|
|
831
|
+
function getInnerExpression(node) {
|
|
832
|
+
if (ts.isCallExpression(node)) return node.expression;
|
|
833
|
+
if (ts.isAwaitExpression(node) || ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isNonNullExpression(node)) {
|
|
834
|
+
return node.expression;
|
|
835
|
+
}
|
|
836
|
+
return void 0;
|
|
837
|
+
}
|
|
838
|
+
function isKyselyTag(node, checker) {
|
|
839
|
+
return ast.isImportedFrom({ node, checker, moduleName: "kysely" });
|
|
840
|
+
}
|
|
841
|
+
function isKyselyHelperCall(node, checker) {
|
|
842
|
+
return ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && isKyselyTag(node.expression, checker);
|
|
843
|
+
}
|
|
844
|
+
function getMethodName(call) {
|
|
845
|
+
return ts.isPropertyAccessExpression(call.expression) ? call.expression.name.text : void 0;
|
|
846
|
+
}
|
|
847
|
+
function quoteIdentifier(value) {
|
|
848
|
+
return `"${value.replace(/"/g, '""')}"`;
|
|
849
|
+
}
|
|
850
|
+
function quoteIdentifierPath(value) {
|
|
851
|
+
return value.split(".").map(quoteIdentifier).join(".");
|
|
852
|
+
}
|
|
853
|
+
function formatLiteral(value) {
|
|
854
|
+
if (value === ast.UNRESOLVED) return false;
|
|
855
|
+
if (typeof value === "string") return `'${value.replace(/'/g, "''")}'`;
|
|
856
|
+
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
|
|
857
|
+
return String(value);
|
|
858
|
+
}
|
|
859
|
+
if (value === null) return "null";
|
|
860
|
+
return false;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
export { plugin as default };
|
|
864
|
+
//# sourceMappingURL=index.mjs.map
|