This repository was archived by the owner on Mar 1, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathutils.ts
More file actions
711 lines (630 loc) · 22.3 KB
/
utils.ts
File metadata and controls
711 lines (630 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
import { AstUtils, URI, type AstNode, type LangiumDocument, type LangiumDocuments, type Reference } from 'langium';
import fs from 'node:fs';
import { createRequire } from 'node:module';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { PLUGIN_MODULE_NAME, STD_LIB_MODULE_NAME, type ExpressionContext } from './constants';
import {
InternalAttribute,
isArrayExpr,
isBinaryExpr,
isConfigArrayExpr,
isDataField,
isDataModel,
isEnumField,
isExpression,
isInvocationExpr,
isLiteralExpr,
isMemberAccessExpr,
isModel,
isObjectExpr,
isPlugin,
isReferenceExpr,
isStringLiteral,
isTypeDef,
type Attribute,
type AttributeParam,
type BinaryExpr,
type BuiltinType,
type ConfigExpr,
type DataField,
type DataFieldAttribute,
type DataModel,
type DataModelAttribute,
type Enum,
type EnumField,
type Expression,
type ExpressionType,
type FunctionDecl,
type Model,
type ModelImport,
type ReferenceExpr,
type TypeDef,
} from './generated/ast';
export type AttributeTarget =
| DataModel
| TypeDef
| DataField
| Enum
| EnumField
| FunctionDecl
| Attribute
| AttributeParam;
export function hasAttribute(decl: AttributeTarget, name: string) {
return !!getAttribute(decl, name);
}
export function getAttribute(decl: AttributeTarget, name: string) {
return (decl.attributes as (DataModelAttribute | DataFieldAttribute)[]).find((attr) => attr.decl.$refText === name);
}
export function isFromStdlib(node: AstNode) {
const model = AstUtils.getContainerOfType(node, isModel);
return !!model && !!model.$document && model.$document.uri.path.endsWith(STD_LIB_MODULE_NAME);
}
export function isAuthInvocation(node: AstNode) {
return isInvocationExpr(node) && node.function.ref?.name === 'auth' && isFromStdlib(node.function.ref);
}
/**
* Try getting string value from a potential string literal expression
*/
export function getStringLiteral(node: AstNode | undefined): string | undefined {
return isStringLiteral(node) ? node.value : undefined;
}
const isoDateTimeRegex = /^\d{4}(-\d\d(-\d\d(T\d\d:\d\d(:\d\d)?(\.\d+)?(([+-]\d\d:\d\d)|Z)?)?)?)?$/i;
/**
* Determines if the given sourceType is assignable to a destination of destType
*/
export function typeAssignable(destType: ExpressionType, sourceType: ExpressionType, sourceExpr?: Expression): boolean {
// implicit conversion from ISO datetime string to datetime
if (destType === 'DateTime' && sourceType === 'String' && sourceExpr && isStringLiteral(sourceExpr)) {
const literal = getStringLiteral(sourceExpr);
if (literal && isoDateTimeRegex.test(literal)) {
// implicitly convert to DateTime
sourceType = 'DateTime';
}
}
switch (destType) {
case 'Any':
return true;
case 'Float':
return sourceType === 'Any' || sourceType === 'Int' || sourceType === 'Float';
default:
return sourceType === 'Any' || sourceType === destType;
}
}
/**
* Maps a ZModel builtin type to expression type
*/
export function mapBuiltinTypeToExpressionType(type: BuiltinType | ExpressionType): ExpressionType {
switch (type) {
case 'Any':
case 'Boolean':
case 'String':
case 'DateTime':
case 'Int':
case 'Float':
case 'Null':
case 'Object':
case 'Unsupported':
case 'Void':
case 'Undefined':
return type;
case 'BigInt':
return 'Int';
case 'Decimal':
return 'Float';
case 'Json':
case 'Bytes':
return 'Any';
}
}
export function isAuthOrAuthMemberAccess(expr: Expression): boolean {
return isAuthInvocation(expr) || (isMemberAccessExpr(expr) && isAuthOrAuthMemberAccess(expr.operand));
}
export function isEnumFieldReference(node: AstNode): node is ReferenceExpr {
return isReferenceExpr(node) && isEnumField(node.target.ref);
}
export function isDataFieldReference(node: AstNode): node is ReferenceExpr {
return isReferenceExpr(node) && isDataField(node.target.ref);
}
/**
* Returns if the given field is a relation field.
*/
export function isRelationshipField(field: DataField) {
return isDataModel(field.type.reference?.ref);
}
/**
* Returns if the given field is a computed field.
*/
export function isComputedField(field: DataField) {
return hasAttribute(field, '@computed');
}
/**
* Returns if the given field is a virtual field.
*/
export function isVirtualField(field: DataField) {
return hasAttribute(field, '@virtual');
}
export function isDelegateModel(node: AstNode) {
return isDataModel(node) && hasAttribute(node, '@@delegate');
}
export function resolved<T extends AstNode>(ref: Reference<T>): T {
if (!ref.ref) {
throw new Error(`Reference not resolved: ${ref.$refText}`);
}
return ref.ref;
}
export function getRecursiveBases(
decl: DataModel | TypeDef,
includeDelegate = true,
documents?: LangiumDocuments,
seen = new Set<DataModel | TypeDef>(),
): (TypeDef | DataModel)[] {
const result: (TypeDef | DataModel)[] = [];
if (seen.has(decl)) {
return result;
}
seen.add(decl);
const bases = [...decl.mixins, ...(isDataModel(decl) && decl.baseModel ? [decl.baseModel] : [])];
bases.forEach((base) => {
let baseDecl: TypeDef | DataModel | undefined;
if (base.ref && (isTypeDef(base.ref) || isDataModel(base.ref))) {
// base is already resolved
baseDecl = base.ref;
} else {
// otherwise, search by name, in all imported documents if provided
const declarations = documents
? getAllDeclarationsIncludingImports(documents, decl.$container)
: decl.$container.declarations;
baseDecl = declarations.find(
(d): d is TypeDef | DataModel => (isTypeDef(d) || isDataModel(d)) && d.name === base.$refText,
);
}
if (baseDecl) {
if (!includeDelegate && isDelegateModel(baseDecl)) {
return;
}
result.push(baseDecl);
result.push(...getRecursiveBases(baseDecl, includeDelegate, documents, seen));
}
});
return result;
}
/**
* Gets `@@id` fields declared at the data model level (including search in base models)
*/
export function getModelIdFields(model: DataModel) {
const modelsToCheck = [model, ...getRecursiveBases(model)];
for (const modelToCheck of modelsToCheck) {
const allAttributes = getAllAttributes(modelToCheck);
const idAttr = allAttributes.find((attr) => attr.decl.$refText === '@@id');
if (!idAttr) {
continue;
}
const fieldsArg = idAttr.args.find((a) => a.$resolvedParam?.name === 'fields');
if (!fieldsArg || !isArrayExpr(fieldsArg.value)) {
continue;
}
return fieldsArg.value.items
.filter((item): item is ReferenceExpr => isReferenceExpr(item))
.map((item) => resolved(item.target) as DataField);
}
return [];
}
/**
* Gets `@@unique` fields declared at the data model level (including search in base models)
*/
export function getModelUniqueFields(model: DataModel) {
const modelsToCheck = [model, ...getRecursiveBases(model)];
for (const modelToCheck of modelsToCheck) {
const allAttributes = getAllAttributes(modelToCheck);
const uniqueAttr = allAttributes.find((attr) => attr.decl.$refText === '@@unique');
if (!uniqueAttr) {
continue;
}
const fieldsArg = uniqueAttr.args.find((a) => a.$resolvedParam?.name === 'fields');
if (!fieldsArg || !isArrayExpr(fieldsArg.value)) {
continue;
}
return fieldsArg.value.items
.filter((item): item is ReferenceExpr => isReferenceExpr(item))
.map((item) => resolved(item.target) as DataField);
}
return [];
}
/**
* Gets lists of unique fields declared at the data model level
*
* TODO: merge this with {@link getModelUniqueFields}
*/
export function getUniqueFields(model: DataModel) {
const uniqueAttrs = model.attributes.filter(
(attr) => attr.decl.ref?.name === '@@unique' || attr.decl.ref?.name === '@@id',
);
return uniqueAttrs.map((uniqueAttr) => {
const fieldsArg = uniqueAttr.args.find((a) => a.$resolvedParam?.name === 'fields');
if (!fieldsArg || !isArrayExpr(fieldsArg.value)) {
return [];
}
return fieldsArg.value.items
.filter((item): item is ReferenceExpr => isReferenceExpr(item))
.map((item) => resolved(item.target) as DataField);
});
}
export function findUpAst(node: AstNode, predicate: (node: AstNode) => boolean): AstNode | undefined {
let curr: AstNode | undefined = node;
while (curr) {
if (predicate(curr)) {
return curr;
}
curr = curr.$container;
}
return undefined;
}
export function getLiteral<T extends string | number | boolean | any = any>(
expr: Expression | ConfigExpr | undefined,
): T | undefined {
switch (expr?.$type) {
case 'ObjectExpr':
return getObjectLiteral<T>(expr);
case 'StringLiteral':
case 'BooleanLiteral':
return expr.value as T;
case 'NumberLiteral':
return parseFloat(expr.value) as T;
default:
return undefined;
}
}
export function getObjectLiteral<T>(expr: Expression | ConfigExpr | undefined): T | undefined {
if (!expr || !isObjectExpr(expr)) {
return undefined;
}
const result: Record<string, unknown> = {};
for (const field of expr.fields) {
let fieldValue: unknown;
if (isLiteralExpr(field.value)) {
fieldValue = getLiteral(field.value);
} else if (isArrayExpr(field.value)) {
fieldValue = getLiteralArray(field.value);
} else if (isObjectExpr(field.value)) {
fieldValue = getObjectLiteral(field.value);
}
if (fieldValue === undefined) {
return undefined;
} else {
result[field.name] = fieldValue;
}
}
return result as T;
}
export function getLiteralArray<T extends string | number | boolean | any = any>(
expr: Expression | ConfigExpr | undefined,
): T[] | undefined {
const arr = getArray(expr);
if (!arr) {
return undefined;
}
return arr.map((item) => isExpression(item) && getLiteral<T>(item)).filter((v): v is T => v !== undefined);
}
function getArray(expr: Expression | ConfigExpr | undefined) {
return isArrayExpr(expr) || isConfigArrayExpr(expr) ? expr.items : undefined;
}
export function getAttributeArg(
attr: DataModelAttribute | DataFieldAttribute | InternalAttribute,
name: string,
): Expression | undefined {
return attr.args.find((arg) => arg.$resolvedParam?.name === name)?.value;
}
export function getAttributeArgLiteral<T extends string | number | boolean>(
attr: DataModelAttribute | DataFieldAttribute | InternalAttribute,
name: string,
): T | undefined {
for (const arg of attr.args) {
if (arg.$resolvedParam?.name === name) {
return getLiteral<T>(arg.value);
}
}
return undefined;
}
export function getFunctionExpressionContext(funcDecl: FunctionDecl) {
const funcAllowedContext: ExpressionContext[] = [];
const funcAttr = funcDecl.attributes.find((attr) => attr.decl.$refText === '@@@expressionContext');
if (funcAttr) {
const contextArg = funcAttr.args[0]?.value;
if (isArrayExpr(contextArg)) {
contextArg.items.forEach((item) => {
if (isEnumFieldReference(item)) {
funcAllowedContext.push(item.target.$refText as ExpressionContext);
}
});
}
}
return funcAllowedContext;
}
export function getFieldReference(expr: Expression): DataField | undefined {
if (isReferenceExpr(expr) && isDataField(expr.target.ref)) {
return expr.target.ref;
} else if (isMemberAccessExpr(expr) && isDataField(expr.member.ref)) {
return expr.member.ref;
} else {
return undefined;
}
}
// TODO: move to policy plugin
export function isCheckInvocation(node: AstNode) {
return isInvocationExpr(node) && node.function.ref?.name === 'check';
}
export function resolveTransitiveImports(documents: LangiumDocuments, model: Model) {
return resolveTransitiveImportsInternal(documents, model);
}
function resolveTransitiveImportsInternal(
documents: LangiumDocuments,
model: Model,
initialModel = model,
visited: Set<string> = new Set(),
models: Set<Model> = new Set(),
) {
const doc = AstUtils.getDocument(model);
const initialDoc = AstUtils.getDocument(initialModel);
if (initialDoc.uri.fsPath.toLowerCase() !== doc.uri.fsPath.toLowerCase()) {
models.add(model);
}
const normalizedPath = doc.uri.fsPath.toLowerCase();
if (!visited.has(normalizedPath)) {
visited.add(normalizedPath);
for (const imp of model.imports) {
const importedModel = resolveImport(documents, imp);
if (importedModel) {
resolveTransitiveImportsInternal(documents, importedModel, initialModel, visited, models);
}
}
}
return Array.from(models);
}
export function resolveImport(documents: LangiumDocuments, imp: ModelImport) {
const resolvedUri = resolveImportUri(imp);
try {
if (resolvedUri) {
let resolvedDocument = documents.getDocument(resolvedUri);
if (!resolvedDocument) {
const content = fs.readFileSync(resolvedUri.fsPath, 'utf-8');
resolvedDocument = documents.createDocument(resolvedUri, content);
}
const node = resolvedDocument.parseResult.value;
if (isModel(node)) {
return node;
}
}
} catch {
// NOOP
}
return undefined;
}
export function resolveImportUri(imp: ModelImport) {
if (!imp.path) {
return undefined;
}
const doc = AstUtils.getDocument(imp);
const dir = path.dirname(doc.uri.fsPath);
const importPath = imp.path.endsWith('.zmodel') ? imp.path : `${imp.path}.zmodel`;
return URI.file(path.resolve(dir, importPath));
}
/**
* Gets data models and type defs in the ZModel schema.
*/
export function getDataModelAndTypeDefs(model: Model, includeIgnored = false) {
const r = model.declarations.filter((d): d is DataModel | TypeDef => isDataModel(d) || isTypeDef(d));
if (includeIgnored) {
return r;
} else {
return r.filter((model) => !hasAttribute(model, '@@ignore'));
}
}
export function getAllDeclarationsIncludingImports(documents: LangiumDocuments, model: Model) {
const imports = resolveTransitiveImports(documents, model);
return model.declarations.concat(...imports.map((imp) => imp.declarations));
}
export function getAuthDecl(decls: (DataModel | TypeDef)[]) {
let authModel = decls.find((d) => hasAttribute(d, '@@auth'));
if (!authModel) {
authModel = decls.find((d) => d.name === 'User');
}
return authModel;
}
// TODO: move to policy plugin
export function isBeforeInvocation(node: AstNode) {
return isInvocationExpr(node) && node.function.ref?.name === 'before';
}
export function isCollectionPredicate(node: AstNode): node is BinaryExpr {
return isBinaryExpr(node) && ['?', '!', '^'].includes(node.operator);
}
export function getAllLoadedDataModelsAndTypeDefs(langiumDocuments: LangiumDocuments) {
return langiumDocuments.all
.map((doc) => doc.parseResult.value as Model)
.flatMap((model) => model.declarations.filter((d): d is DataModel | TypeDef => isDataModel(d) || isTypeDef(d)))
.toArray();
}
export function getAllDataModelsIncludingImports(documents: LangiumDocuments, model: Model) {
return getAllDeclarationsIncludingImports(documents, model).filter(isDataModel);
}
export function getAllLoadedAndReachableDataModelsAndTypeDefs(
langiumDocuments: LangiumDocuments,
fromModel?: DataModel,
) {
// get all data models from loaded documents
const allDataModels = getAllLoadedDataModelsAndTypeDefs(langiumDocuments);
if (fromModel) {
// merge data models transitively reached from the current model
const model = AstUtils.getContainerOfType(fromModel, isModel);
if (model) {
const transitiveDataModels = getAllDataModelsIncludingImports(langiumDocuments, model);
transitiveDataModels.forEach((dm) => {
if (!allDataModels.includes(dm)) {
allDataModels.push(dm);
}
});
}
}
return allDataModels;
}
export function getContainingDataModel(node: AstNode): DataModel | undefined {
let curr: AstNode | undefined = node.$container;
while (curr) {
if (isDataModel(curr)) {
return curr;
}
curr = curr.$container;
}
return undefined;
}
export function isMemberContainer(node: unknown): node is DataModel | TypeDef {
return isDataModel(node) || isTypeDef(node);
}
export function getAllFields(
decl: DataModel | TypeDef,
includeIgnored = false,
seen: Set<DataModel | TypeDef> = new Set(),
): DataField[] {
if (seen.has(decl)) {
return [];
}
seen.add(decl);
const fields: DataField[] = [];
for (const mixin of decl.mixins) {
if (mixin.ref) {
fields.push(...getAllFields(mixin.ref, includeIgnored, seen));
}
}
if (isDataModel(decl) && decl.baseModel) {
if (decl.baseModel.ref) {
fields.push(...getAllFields(decl.baseModel.ref, includeIgnored, seen));
}
}
fields.push(...decl.fields.filter((f) => includeIgnored || !hasAttribute(f, '@ignore')));
return fields;
}
export function getAllAttributes(
decl: DataModel | TypeDef,
seen: Set<DataModel | TypeDef> = new Set(),
): DataModelAttribute[] {
if (seen.has(decl)) {
return [];
}
seen.add(decl);
const attributes: DataModelAttribute[] = [];
for (const mixin of decl.mixins) {
if (mixin.ref) {
attributes.push(...getAllAttributes(mixin.ref, seen));
}
}
if (isDataModel(decl) && decl.baseModel) {
if (decl.baseModel.ref) {
const attrs = getAllAttributes(decl.baseModel.ref, seen).filter((attr) => !isNonInheritableAttribute(attr));
attributes.push(...attrs);
}
}
attributes.push(...decl.attributes);
return attributes;
}
function isNonInheritableAttribute(attr: DataModelAttribute) {
const attrName = attr.decl.ref?.name ?? attr.decl.$refText;
return ['@@map', '@@unique', '@@index'].includes(attrName);
}
/**
* Retrieve the document in which the given AST node is contained. A reference to the document is
* usually held by the root node of the AST.
*
* @throws an error if the node is not contained in a document.
*/
export function getDocument<T extends AstNode = AstNode>(node: AstNode): LangiumDocument<T> {
const rootNode = findRootNode(node);
const result = rootNode.$document;
if (!result) {
throw new Error('AST node has no document.');
}
return result as LangiumDocument<T>;
}
export function getPluginDocuments(model: Model, schemaPath: string): string[] {
// traverse plugins and collect "plugin.zmodel" documents
const result: string[] = [];
for (const decl of model.declarations.filter(isPlugin)) {
const providerField = decl.fields.find((f) => f.name === 'provider');
if (!providerField) {
continue;
}
const provider = getLiteral<string>(providerField.value);
if (!provider) {
continue;
}
let pluginModelFile: string | undefined;
// first try to treat provider as a path
let providerPath = path.resolve(path.dirname(schemaPath), provider);
if (fs.existsSync(providerPath)) {
if (fs.statSync(providerPath).isDirectory()) {
providerPath = path.join(providerPath, 'index.js');
}
// try plugin.zmodel next to the provider file
pluginModelFile = path.resolve(path.dirname(providerPath), PLUGIN_MODULE_NAME);
if (!fs.existsSync(pluginModelFile)) {
// try to find upwards
pluginModelFile = findUp([PLUGIN_MODULE_NAME], path.dirname(providerPath));
}
}
if (!pluginModelFile) {
if (typeof import.meta.resolve === 'function') {
try {
// try loading as a ESM module
const resolvedUrl = import.meta.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
pluginModelFile = fileURLToPath(resolvedUrl);
} catch {
// noop
}
}
}
if (!pluginModelFile) {
// try loading as a CJS module
try {
const require = createRequire(pathToFileURL(schemaPath));
pluginModelFile = require.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
} catch {
// noop
}
}
if (pluginModelFile && fs.existsSync(pluginModelFile)) {
result.push(pluginModelFile);
}
}
return result;
}
type FindUpResult<Multiple extends boolean> = Multiple extends true ? string[] | undefined : string | undefined;
function findUp<Multiple extends boolean = false>(
names: string[],
cwd: string = process.cwd(),
multiple: Multiple = false as Multiple,
result: string[] = [],
): FindUpResult<Multiple> {
if (!names.some((name) => !!name)) {
return undefined;
}
const target = names.find((name) => fs.existsSync(path.join(cwd, name)));
if (multiple === false && target) {
return path.join(cwd, target) as FindUpResult<Multiple>;
}
if (target) {
result.push(path.join(cwd, target));
}
const up = path.resolve(cwd, '..');
if (up === cwd) {
return (multiple && result.length > 0 ? result : undefined) as FindUpResult<Multiple>;
}
return findUp(names, up, multiple, result);
}
/**
* Returns the root node of the given AST node by following the `$container` references.
*/
export function findRootNode(node: AstNode): AstNode {
while (node.$container) {
node = node.$container;
}
return node;
}