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