@zenstackhq/language 3.0.0-beta.2 → 3.0.0-beta.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1644 -294
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +84 -20
- package/dist/index.d.ts +84 -20
- package/dist/index.js +1595 -248
- package/dist/index.js.map +1 -1
- package/dist/utils.cjs +112 -28
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +7 -6
- package/dist/utils.d.ts +7 -6
- package/dist/utils.js +105 -23
- package/dist/utils.js.map +1 -1
- package/package.json +11 -7
- package/res/stdlib.zmodel +49 -77
package/dist/index.js
CHANGED
|
@@ -7,16 +7,19 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
7
7
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
-
// src/
|
|
11
|
-
import { isAstNode, URI as
|
|
12
|
-
import
|
|
13
|
-
import
|
|
14
|
-
import
|
|
15
|
-
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
10
|
+
// src/document.ts
|
|
11
|
+
import { isAstNode, URI as URI4 } from "langium";
|
|
12
|
+
import fs4 from "fs";
|
|
13
|
+
import path4 from "path";
|
|
14
|
+
import { fileURLToPath as fileURLToPath4 } from "url";
|
|
16
15
|
|
|
17
16
|
// src/generated/ast.ts
|
|
18
17
|
import * as langium from "langium";
|
|
19
18
|
var AbstractDeclaration = "AbstractDeclaration";
|
|
19
|
+
function isAbstractDeclaration(item) {
|
|
20
|
+
return reflection.isInstance(item, AbstractDeclaration);
|
|
21
|
+
}
|
|
22
|
+
__name(isAbstractDeclaration, "isAbstractDeclaration");
|
|
20
23
|
var ConfigExpr = "ConfigExpr";
|
|
21
24
|
var Expression = "Expression";
|
|
22
25
|
function isExpression(item) {
|
|
@@ -43,6 +46,10 @@ function isAttribute(item) {
|
|
|
43
46
|
}
|
|
44
47
|
__name(isAttribute, "isAttribute");
|
|
45
48
|
var AttributeArg = "AttributeArg";
|
|
49
|
+
function isAttributeArg(item) {
|
|
50
|
+
return reflection.isInstance(item, AttributeArg);
|
|
51
|
+
}
|
|
52
|
+
__name(isAttributeArg, "isAttributeArg");
|
|
46
53
|
var AttributeParam = "AttributeParam";
|
|
47
54
|
var AttributeParamType = "AttributeParamType";
|
|
48
55
|
var BinaryExpr = "BinaryExpr";
|
|
@@ -61,6 +68,10 @@ function isConfigArrayExpr(item) {
|
|
|
61
68
|
}
|
|
62
69
|
__name(isConfigArrayExpr, "isConfigArrayExpr");
|
|
63
70
|
var ConfigField = "ConfigField";
|
|
71
|
+
function isConfigField(item) {
|
|
72
|
+
return reflection.isInstance(item, ConfigField);
|
|
73
|
+
}
|
|
74
|
+
__name(isConfigField, "isConfigField");
|
|
64
75
|
var ConfigInvocationArg = "ConfigInvocationArg";
|
|
65
76
|
var ConfigInvocationExpr = "ConfigInvocationExpr";
|
|
66
77
|
var DataField = "DataField";
|
|
@@ -105,10 +116,22 @@ function isEnumField(item) {
|
|
|
105
116
|
__name(isEnumField, "isEnumField");
|
|
106
117
|
var FieldInitializer = "FieldInitializer";
|
|
107
118
|
var FunctionDecl = "FunctionDecl";
|
|
119
|
+
function isFunctionDecl(item) {
|
|
120
|
+
return reflection.isInstance(item, FunctionDecl);
|
|
121
|
+
}
|
|
122
|
+
__name(isFunctionDecl, "isFunctionDecl");
|
|
108
123
|
var FunctionParam = "FunctionParam";
|
|
109
124
|
var FunctionParamType = "FunctionParamType";
|
|
110
125
|
var GeneratorDecl = "GeneratorDecl";
|
|
126
|
+
function isGeneratorDecl(item) {
|
|
127
|
+
return reflection.isInstance(item, GeneratorDecl);
|
|
128
|
+
}
|
|
129
|
+
__name(isGeneratorDecl, "isGeneratorDecl");
|
|
111
130
|
var InternalAttribute = "InternalAttribute";
|
|
131
|
+
function isInternalAttribute(item) {
|
|
132
|
+
return reflection.isInstance(item, InternalAttribute);
|
|
133
|
+
}
|
|
134
|
+
__name(isInternalAttribute, "isInternalAttribute");
|
|
112
135
|
var InvocationExpr = "InvocationExpr";
|
|
113
136
|
function isInvocationExpr(item) {
|
|
114
137
|
return reflection.isInstance(item, InvocationExpr);
|
|
@@ -125,6 +148,10 @@ function isModel(item) {
|
|
|
125
148
|
}
|
|
126
149
|
__name(isModel, "isModel");
|
|
127
150
|
var ModelImport = "ModelImport";
|
|
151
|
+
function isModelImport(item) {
|
|
152
|
+
return reflection.isInstance(item, ModelImport);
|
|
153
|
+
}
|
|
154
|
+
__name(isModelImport, "isModelImport");
|
|
128
155
|
var NullExpr = "NullExpr";
|
|
129
156
|
function isNullExpr(item) {
|
|
130
157
|
return reflection.isInstance(item, NullExpr);
|
|
@@ -146,6 +173,10 @@ function isPlugin(item) {
|
|
|
146
173
|
}
|
|
147
174
|
__name(isPlugin, "isPlugin");
|
|
148
175
|
var PluginField = "PluginField";
|
|
176
|
+
function isPluginField(item) {
|
|
177
|
+
return reflection.isInstance(item, PluginField);
|
|
178
|
+
}
|
|
179
|
+
__name(isPluginField, "isPluginField");
|
|
149
180
|
var Procedure = "Procedure";
|
|
150
181
|
var ProcedureParam = "ProcedureParam";
|
|
151
182
|
var ReferenceArg = "ReferenceArg";
|
|
@@ -1012,6 +1043,26 @@ var ZModelAstReflection = class extends langium.AbstractAstReflection {
|
|
|
1012
1043
|
};
|
|
1013
1044
|
var reflection = new ZModelAstReflection();
|
|
1014
1045
|
|
|
1046
|
+
// src/ast.ts
|
|
1047
|
+
var BinaryExprOperatorPriority = {
|
|
1048
|
+
//LogicalExpr
|
|
1049
|
+
"||": 1,
|
|
1050
|
+
"&&": 1,
|
|
1051
|
+
//EqualityExpr
|
|
1052
|
+
"==": 2,
|
|
1053
|
+
"!=": 2,
|
|
1054
|
+
//ComparisonExpr
|
|
1055
|
+
">": 3,
|
|
1056
|
+
"<": 3,
|
|
1057
|
+
">=": 3,
|
|
1058
|
+
"<=": 3,
|
|
1059
|
+
in: 4,
|
|
1060
|
+
//CollectionPredicateExpr
|
|
1061
|
+
"^": 5,
|
|
1062
|
+
"?": 5,
|
|
1063
|
+
"!": 5
|
|
1064
|
+
};
|
|
1065
|
+
|
|
1015
1066
|
// src/constants.ts
|
|
1016
1067
|
var SUPPORTED_PROVIDERS = [
|
|
1017
1068
|
"sqlite",
|
|
@@ -1042,8 +1093,11 @@ var ExpressionContext = /* @__PURE__ */ function(ExpressionContext2) {
|
|
|
1042
1093
|
}({});
|
|
1043
1094
|
|
|
1044
1095
|
// src/module.ts
|
|
1045
|
-
import { inject } from "langium";
|
|
1096
|
+
import { DocumentState as DocumentState2, inject, URI as URI3 } from "langium";
|
|
1046
1097
|
import { createDefaultModule, createDefaultSharedModule } from "langium/lsp";
|
|
1098
|
+
import { NodeFileSystem } from "langium/node";
|
|
1099
|
+
import path3 from "path";
|
|
1100
|
+
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
1047
1101
|
|
|
1048
1102
|
// src/generated/grammar.ts
|
|
1049
1103
|
import { loadGrammarFromJson } from "langium";
|
|
@@ -5104,15 +5158,12 @@ var ZModelGeneratedModule = {
|
|
|
5104
5158
|
parser: {}
|
|
5105
5159
|
};
|
|
5106
5160
|
|
|
5107
|
-
// src/validators/attribute-application-validator.ts
|
|
5108
|
-
import { AstUtils as AstUtils2 } from "langium";
|
|
5109
|
-
import pluralize from "pluralize";
|
|
5110
|
-
|
|
5111
5161
|
// src/utils.ts
|
|
5112
|
-
import { invariant } from "@zenstackhq/common-helpers";
|
|
5113
5162
|
import { AstUtils, URI } from "langium";
|
|
5114
5163
|
import fs from "fs";
|
|
5164
|
+
import { createRequire } from "module";
|
|
5115
5165
|
import path from "path";
|
|
5166
|
+
import { fileURLToPath, pathToFileURL } from "url";
|
|
5116
5167
|
function hasAttribute(decl, name) {
|
|
5117
5168
|
return !!getAttribute(decl, name);
|
|
5118
5169
|
}
|
|
@@ -5192,10 +5243,6 @@ function isRelationshipField(field) {
|
|
|
5192
5243
|
return isDataModel(field.type.reference?.ref);
|
|
5193
5244
|
}
|
|
5194
5245
|
__name(isRelationshipField, "isRelationshipField");
|
|
5195
|
-
function isFutureExpr(node) {
|
|
5196
|
-
return isInvocationExpr(node) && node.function.ref?.name === "future" && isFromStdlib(node.function.ref);
|
|
5197
|
-
}
|
|
5198
|
-
__name(isFutureExpr, "isFutureExpr");
|
|
5199
5246
|
function isDelegateModel(node) {
|
|
5200
5247
|
return isDataModel(node) && hasAttribute(node, "@@delegate");
|
|
5201
5248
|
}
|
|
@@ -5213,8 +5260,14 @@ function getRecursiveBases(decl, includeDelegate = true, seen = /* @__PURE__ */
|
|
|
5213
5260
|
return result;
|
|
5214
5261
|
}
|
|
5215
5262
|
seen.add(decl);
|
|
5216
|
-
|
|
5217
|
-
|
|
5263
|
+
const bases = [
|
|
5264
|
+
...decl.mixins,
|
|
5265
|
+
...isDataModel(decl) && decl.baseModel ? [
|
|
5266
|
+
decl.baseModel
|
|
5267
|
+
] : []
|
|
5268
|
+
];
|
|
5269
|
+
bases.forEach((base) => {
|
|
5270
|
+
const baseDecl = decl.$container.declarations.find((d) => (isTypeDef(d) || isDataModel(d)) && d.name === base.$refText);
|
|
5218
5271
|
if (baseDecl) {
|
|
5219
5272
|
if (!includeDelegate && isDelegateModel(baseDecl)) {
|
|
5220
5273
|
return;
|
|
@@ -5337,6 +5390,10 @@ function getArray(expr) {
|
|
|
5337
5390
|
return isArrayExpr(expr) || isConfigArrayExpr(expr) ? expr.items : void 0;
|
|
5338
5391
|
}
|
|
5339
5392
|
__name(getArray, "getArray");
|
|
5393
|
+
function getAttributeArg(attr, name) {
|
|
5394
|
+
return attr.args.find((arg) => arg.$resolvedParam?.name === name)?.value;
|
|
5395
|
+
}
|
|
5396
|
+
__name(getAttributeArg, "getAttributeArg");
|
|
5340
5397
|
function getFunctionExpressionContext(funcDecl) {
|
|
5341
5398
|
const funcAllowedContext = [];
|
|
5342
5399
|
const funcAttr = funcDecl.attributes.find((attr) => attr.decl.$refText === "@@@expressionContext");
|
|
@@ -5354,7 +5411,7 @@ function getFunctionExpressionContext(funcDecl) {
|
|
|
5354
5411
|
}
|
|
5355
5412
|
__name(getFunctionExpressionContext, "getFunctionExpressionContext");
|
|
5356
5413
|
function isCheckInvocation(node) {
|
|
5357
|
-
return isInvocationExpr(node) && node.function.ref?.name === "check"
|
|
5414
|
+
return isInvocationExpr(node) && node.function.ref?.name === "check";
|
|
5358
5415
|
}
|
|
5359
5416
|
__name(isCheckInvocation, "isCheckInvocation");
|
|
5360
5417
|
function resolveTransitiveImports(documents, model) {
|
|
@@ -5424,17 +5481,17 @@ function getAllDeclarationsIncludingImports(documents, model) {
|
|
|
5424
5481
|
}
|
|
5425
5482
|
__name(getAllDeclarationsIncludingImports, "getAllDeclarationsIncludingImports");
|
|
5426
5483
|
function getAuthDecl(decls) {
|
|
5427
|
-
let authModel = decls.find((
|
|
5484
|
+
let authModel = decls.find((d) => hasAttribute(d, "@@auth"));
|
|
5428
5485
|
if (!authModel) {
|
|
5429
|
-
authModel = decls.find((
|
|
5486
|
+
authModel = decls.find((d) => d.name === "User");
|
|
5430
5487
|
}
|
|
5431
5488
|
return authModel;
|
|
5432
5489
|
}
|
|
5433
5490
|
__name(getAuthDecl, "getAuthDecl");
|
|
5434
|
-
function
|
|
5435
|
-
return isInvocationExpr(node) && node.function.ref?.name === "
|
|
5491
|
+
function isBeforeInvocation(node) {
|
|
5492
|
+
return isInvocationExpr(node) && node.function.ref?.name === "before";
|
|
5436
5493
|
}
|
|
5437
|
-
__name(
|
|
5494
|
+
__name(isBeforeInvocation, "isBeforeInvocation");
|
|
5438
5495
|
function isCollectionPredicate(node) {
|
|
5439
5496
|
return isBinaryExpr(node) && [
|
|
5440
5497
|
"?",
|
|
@@ -5489,12 +5546,14 @@ function getAllFields(decl, includeIgnored = false, seen = /* @__PURE__ */ new S
|
|
|
5489
5546
|
seen.add(decl);
|
|
5490
5547
|
const fields = [];
|
|
5491
5548
|
for (const mixin of decl.mixins) {
|
|
5492
|
-
|
|
5493
|
-
|
|
5549
|
+
if (mixin.ref) {
|
|
5550
|
+
fields.push(...getAllFields(mixin.ref, includeIgnored, seen));
|
|
5551
|
+
}
|
|
5494
5552
|
}
|
|
5495
5553
|
if (isDataModel(decl) && decl.baseModel) {
|
|
5496
|
-
|
|
5497
|
-
|
|
5554
|
+
if (decl.baseModel.ref) {
|
|
5555
|
+
fields.push(...getAllFields(decl.baseModel.ref, includeIgnored, seen));
|
|
5556
|
+
}
|
|
5498
5557
|
}
|
|
5499
5558
|
fields.push(...decl.fields.filter((f) => includeIgnored || !hasAttribute(f, "@ignore")));
|
|
5500
5559
|
return fields;
|
|
@@ -5507,12 +5566,14 @@ function getAllAttributes(decl, seen = /* @__PURE__ */ new Set()) {
|
|
|
5507
5566
|
seen.add(decl);
|
|
5508
5567
|
const attributes = [];
|
|
5509
5568
|
for (const mixin of decl.mixins) {
|
|
5510
|
-
|
|
5511
|
-
|
|
5569
|
+
if (mixin.ref) {
|
|
5570
|
+
attributes.push(...getAllAttributes(mixin.ref, seen));
|
|
5571
|
+
}
|
|
5512
5572
|
}
|
|
5513
5573
|
if (isDataModel(decl) && decl.baseModel) {
|
|
5514
|
-
|
|
5515
|
-
|
|
5574
|
+
if (decl.baseModel.ref) {
|
|
5575
|
+
attributes.push(...getAllAttributes(decl.baseModel.ref, seen));
|
|
5576
|
+
}
|
|
5516
5577
|
}
|
|
5517
5578
|
attributes.push(...decl.attributes);
|
|
5518
5579
|
return attributes;
|
|
@@ -5527,6 +5588,71 @@ function getDocument(node) {
|
|
|
5527
5588
|
return result;
|
|
5528
5589
|
}
|
|
5529
5590
|
__name(getDocument, "getDocument");
|
|
5591
|
+
function getPluginDocuments(model, schemaPath) {
|
|
5592
|
+
const result = [];
|
|
5593
|
+
for (const decl of model.declarations.filter(isPlugin)) {
|
|
5594
|
+
const providerField = decl.fields.find((f) => f.name === "provider");
|
|
5595
|
+
if (!providerField) {
|
|
5596
|
+
continue;
|
|
5597
|
+
}
|
|
5598
|
+
const provider = getLiteral(providerField.value);
|
|
5599
|
+
if (!provider) {
|
|
5600
|
+
continue;
|
|
5601
|
+
}
|
|
5602
|
+
let pluginModelFile;
|
|
5603
|
+
let providerPath = path.resolve(path.dirname(schemaPath), provider);
|
|
5604
|
+
if (fs.existsSync(providerPath)) {
|
|
5605
|
+
if (fs.statSync(providerPath).isDirectory()) {
|
|
5606
|
+
providerPath = path.join(providerPath, "index.js");
|
|
5607
|
+
}
|
|
5608
|
+
pluginModelFile = path.resolve(path.dirname(providerPath), PLUGIN_MODULE_NAME);
|
|
5609
|
+
if (!fs.existsSync(pluginModelFile)) {
|
|
5610
|
+
pluginModelFile = findUp([
|
|
5611
|
+
PLUGIN_MODULE_NAME
|
|
5612
|
+
], path.dirname(providerPath));
|
|
5613
|
+
}
|
|
5614
|
+
}
|
|
5615
|
+
if (!pluginModelFile) {
|
|
5616
|
+
if (typeof import.meta.resolve === "function") {
|
|
5617
|
+
try {
|
|
5618
|
+
const resolvedUrl = import.meta.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
|
|
5619
|
+
pluginModelFile = fileURLToPath(resolvedUrl);
|
|
5620
|
+
} catch {
|
|
5621
|
+
}
|
|
5622
|
+
}
|
|
5623
|
+
}
|
|
5624
|
+
if (!pluginModelFile) {
|
|
5625
|
+
try {
|
|
5626
|
+
const require2 = createRequire(pathToFileURL(schemaPath));
|
|
5627
|
+
pluginModelFile = require2.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
|
|
5628
|
+
} catch {
|
|
5629
|
+
}
|
|
5630
|
+
}
|
|
5631
|
+
if (pluginModelFile && fs.existsSync(pluginModelFile)) {
|
|
5632
|
+
result.push(pluginModelFile);
|
|
5633
|
+
}
|
|
5634
|
+
}
|
|
5635
|
+
return result;
|
|
5636
|
+
}
|
|
5637
|
+
__name(getPluginDocuments, "getPluginDocuments");
|
|
5638
|
+
function findUp(names, cwd = process.cwd(), multiple = false, result = []) {
|
|
5639
|
+
if (!names.some((name) => !!name)) {
|
|
5640
|
+
return void 0;
|
|
5641
|
+
}
|
|
5642
|
+
const target = names.find((name) => fs.existsSync(path.join(cwd, name)));
|
|
5643
|
+
if (multiple === false && target) {
|
|
5644
|
+
return path.join(cwd, target);
|
|
5645
|
+
}
|
|
5646
|
+
if (target) {
|
|
5647
|
+
result.push(path.join(cwd, target));
|
|
5648
|
+
}
|
|
5649
|
+
const up = path.resolve(cwd, "..");
|
|
5650
|
+
if (up === cwd) {
|
|
5651
|
+
return multiple && result.length > 0 ? result : void 0;
|
|
5652
|
+
}
|
|
5653
|
+
return findUp(names, up, multiple, result);
|
|
5654
|
+
}
|
|
5655
|
+
__name(findUp, "findUp");
|
|
5530
5656
|
function findRootNode(node) {
|
|
5531
5657
|
while (node.$container) {
|
|
5532
5658
|
node = node.$container;
|
|
@@ -5536,6 +5662,9 @@ function findRootNode(node) {
|
|
|
5536
5662
|
__name(findRootNode, "findRootNode");
|
|
5537
5663
|
|
|
5538
5664
|
// src/validators/attribute-application-validator.ts
|
|
5665
|
+
import { invariant } from "@zenstackhq/common-helpers";
|
|
5666
|
+
import { AstUtils as AstUtils2 } from "langium";
|
|
5667
|
+
import pluralize from "pluralize";
|
|
5539
5668
|
function _ts_decorate(decorators, target, key, desc) {
|
|
5540
5669
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
5541
5670
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -5649,6 +5778,7 @@ var AttributeApplicationValidator = class {
|
|
|
5649
5778
|
});
|
|
5650
5779
|
}
|
|
5651
5780
|
}
|
|
5781
|
+
// TODO: design a way to let plugin register validation
|
|
5652
5782
|
_checkModelLevelPolicy(attr, accept) {
|
|
5653
5783
|
const kind = getStringLiteral(attr.args[0]?.value);
|
|
5654
5784
|
if (!kind) {
|
|
@@ -5661,11 +5791,61 @@ var AttributeApplicationValidator = class {
|
|
|
5661
5791
|
"create",
|
|
5662
5792
|
"read",
|
|
5663
5793
|
"update",
|
|
5794
|
+
"post-update",
|
|
5664
5795
|
"delete",
|
|
5665
5796
|
"all"
|
|
5666
5797
|
], attr, accept);
|
|
5667
|
-
|
|
5798
|
+
if ((kind === "create" || kind === "all") && attr.args[1]?.value) {
|
|
5799
|
+
this.rejectNonOwnedRelationInExpression(attr.args[1].value, accept);
|
|
5800
|
+
}
|
|
5801
|
+
if (kind !== "post-update" && attr.args[1]?.value) {
|
|
5802
|
+
const beforeCall = AstUtils2.streamAst(attr.args[1]?.value).find(isBeforeInvocation);
|
|
5803
|
+
if (beforeCall) {
|
|
5804
|
+
accept("error", `"before()" is only allowed in "post-update" policy rules`, {
|
|
5805
|
+
node: beforeCall
|
|
5806
|
+
});
|
|
5807
|
+
}
|
|
5808
|
+
}
|
|
5668
5809
|
}
|
|
5810
|
+
rejectNonOwnedRelationInExpression(expr, accept) {
|
|
5811
|
+
const contextModel = AstUtils2.getContainerOfType(expr, isDataModel);
|
|
5812
|
+
if (!contextModel) {
|
|
5813
|
+
return;
|
|
5814
|
+
}
|
|
5815
|
+
if (AstUtils2.streamAst(expr).some((node) => {
|
|
5816
|
+
if (!isDataFieldReference(node)) {
|
|
5817
|
+
return false;
|
|
5818
|
+
}
|
|
5819
|
+
if (node.target.ref?.$container !== contextModel) {
|
|
5820
|
+
return false;
|
|
5821
|
+
}
|
|
5822
|
+
const field = node.target.ref;
|
|
5823
|
+
if (!isRelationshipField(field)) {
|
|
5824
|
+
return false;
|
|
5825
|
+
}
|
|
5826
|
+
if (isAuthOrAuthMemberAccess(node)) {
|
|
5827
|
+
return false;
|
|
5828
|
+
}
|
|
5829
|
+
const startNode = isCollectionPredicate(node.$container) && node.$container.left === node ? node.$container : node;
|
|
5830
|
+
const collectionPredicate = AstUtils2.getContainerOfType(startNode.$container, isCollectionPredicate);
|
|
5831
|
+
if (collectionPredicate && isAuthOrAuthMemberAccess(collectionPredicate.left)) {
|
|
5832
|
+
return false;
|
|
5833
|
+
}
|
|
5834
|
+
const relationAttr = field.attributes.find((attr) => attr.decl.ref?.name === "@relation");
|
|
5835
|
+
if (!relationAttr) {
|
|
5836
|
+
return true;
|
|
5837
|
+
}
|
|
5838
|
+
if (!relationAttr.args.some((arg) => arg.name === "fields")) {
|
|
5839
|
+
return true;
|
|
5840
|
+
}
|
|
5841
|
+
return false;
|
|
5842
|
+
})) {
|
|
5843
|
+
accept("error", `non-owned relation fields are not allowed in "create" rules`, {
|
|
5844
|
+
node: expr
|
|
5845
|
+
});
|
|
5846
|
+
}
|
|
5847
|
+
}
|
|
5848
|
+
// TODO: design a way to let plugin register validation
|
|
5669
5849
|
_checkFieldLevelPolicy(attr, accept) {
|
|
5670
5850
|
const kind = getStringLiteral(attr.args[0]?.value);
|
|
5671
5851
|
if (!kind) {
|
|
@@ -5680,8 +5860,8 @@ var AttributeApplicationValidator = class {
|
|
|
5680
5860
|
"all"
|
|
5681
5861
|
], attr, accept);
|
|
5682
5862
|
const expr = attr.args[1]?.value;
|
|
5683
|
-
if (expr && AstUtils2.streamAst(expr).some((node) =>
|
|
5684
|
-
accept("error", `"
|
|
5863
|
+
if (expr && AstUtils2.streamAst(expr).some((node) => isBeforeInvocation(node))) {
|
|
5864
|
+
accept("error", `"before()" is not allowed in field-level policy rules`, {
|
|
5685
5865
|
node: expr
|
|
5686
5866
|
});
|
|
5687
5867
|
}
|
|
@@ -5693,7 +5873,6 @@ var AttributeApplicationValidator = class {
|
|
|
5693
5873
|
});
|
|
5694
5874
|
}
|
|
5695
5875
|
}
|
|
5696
|
-
this.rejectEncryptedFields(attr, accept);
|
|
5697
5876
|
}
|
|
5698
5877
|
_checkValidate(attr, accept) {
|
|
5699
5878
|
const condition = attr.args[0]?.value;
|
|
@@ -5703,8 +5882,9 @@ var AttributeApplicationValidator = class {
|
|
|
5703
5882
|
});
|
|
5704
5883
|
}
|
|
5705
5884
|
}
|
|
5706
|
-
|
|
5707
|
-
const fields = attr
|
|
5885
|
+
_checkConstraint(attr, accept) {
|
|
5886
|
+
const fields = getAttributeArg(attr, "fields");
|
|
5887
|
+
const attrName = attr.decl.ref?.name;
|
|
5708
5888
|
if (!fields) {
|
|
5709
5889
|
accept("error", `expects an array of field references`, {
|
|
5710
5890
|
node: attr.args[0]
|
|
@@ -5713,7 +5893,7 @@ var AttributeApplicationValidator = class {
|
|
|
5713
5893
|
}
|
|
5714
5894
|
if (isArrayExpr(fields)) {
|
|
5715
5895
|
if (fields.items.length === 0) {
|
|
5716
|
-
accept("error",
|
|
5896
|
+
accept("error", `\`${attrName}\` expects at least one field reference`, {
|
|
5717
5897
|
node: fields
|
|
5718
5898
|
});
|
|
5719
5899
|
return;
|
|
@@ -5732,7 +5912,7 @@ var AttributeApplicationValidator = class {
|
|
|
5732
5912
|
return;
|
|
5733
5913
|
}
|
|
5734
5914
|
if (item.target.ref.$container !== attr.$container && isDelegateModel(item.target.ref.$container)) {
|
|
5735
|
-
accept("error", `Cannot use fields inherited from a polymorphic base model in
|
|
5915
|
+
accept("error", `Cannot use fields inherited from a polymorphic base model in \`${attrName}\``, {
|
|
5736
5916
|
node: item
|
|
5737
5917
|
});
|
|
5738
5918
|
}
|
|
@@ -5743,14 +5923,23 @@ var AttributeApplicationValidator = class {
|
|
|
5743
5923
|
});
|
|
5744
5924
|
}
|
|
5745
5925
|
}
|
|
5746
|
-
|
|
5747
|
-
|
|
5748
|
-
|
|
5749
|
-
|
|
5750
|
-
|
|
5926
|
+
_checkSchema(attr, accept) {
|
|
5927
|
+
const schemaName = getStringLiteral(attr.args[0]?.value);
|
|
5928
|
+
invariant(schemaName, `@@schema expects a string literal`);
|
|
5929
|
+
const zmodel = AstUtils2.getContainerOfType(attr, isModel);
|
|
5930
|
+
const datasource = zmodel.declarations.find(isDataSource);
|
|
5931
|
+
if (datasource) {
|
|
5932
|
+
let found = false;
|
|
5933
|
+
const schemas = datasource.fields.find((f) => f.name === "schemas");
|
|
5934
|
+
if (schemas && isConfigArrayExpr(schemas.value)) {
|
|
5935
|
+
found = schemas.value.items.some((item) => isLiteralExpr(item) && item.value === schemaName);
|
|
5936
|
+
}
|
|
5937
|
+
if (!found) {
|
|
5938
|
+
accept("error", `Schema "${schemaName}" is not defined in the datasource`, {
|
|
5939
|
+
node: attr
|
|
5751
5940
|
});
|
|
5752
5941
|
}
|
|
5753
|
-
}
|
|
5942
|
+
}
|
|
5754
5943
|
}
|
|
5755
5944
|
validatePolicyKinds(kind, candidates, attr, accept) {
|
|
5756
5945
|
const items = kind.split(",").map((x) => x.trim());
|
|
@@ -5794,15 +5983,25 @@ _ts_decorate([
|
|
|
5794
5983
|
_ts_metadata("design:returntype", void 0)
|
|
5795
5984
|
], AttributeApplicationValidator.prototype, "_checkValidate", null);
|
|
5796
5985
|
_ts_decorate([
|
|
5797
|
-
check("@@unique"),
|
|
5798
5986
|
check("@@id"),
|
|
5987
|
+
check("@@index"),
|
|
5988
|
+
check("@@unique"),
|
|
5989
|
+
_ts_metadata("design:type", Function),
|
|
5990
|
+
_ts_metadata("design:paramtypes", [
|
|
5991
|
+
typeof AttributeApplication === "undefined" ? Object : AttributeApplication,
|
|
5992
|
+
typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
|
|
5993
|
+
]),
|
|
5994
|
+
_ts_metadata("design:returntype", void 0)
|
|
5995
|
+
], AttributeApplicationValidator.prototype, "_checkConstraint", null);
|
|
5996
|
+
_ts_decorate([
|
|
5997
|
+
check("@@schema"),
|
|
5799
5998
|
_ts_metadata("design:type", Function),
|
|
5800
5999
|
_ts_metadata("design:paramtypes", [
|
|
5801
6000
|
typeof AttributeApplication === "undefined" ? Object : AttributeApplication,
|
|
5802
6001
|
typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
|
|
5803
6002
|
]),
|
|
5804
6003
|
_ts_metadata("design:returntype", void 0)
|
|
5805
|
-
], AttributeApplicationValidator.prototype, "
|
|
6004
|
+
], AttributeApplicationValidator.prototype, "_checkSchema", null);
|
|
5806
6005
|
function assignableToAttributeParam(arg, param, attr) {
|
|
5807
6006
|
const argResolvedType = arg.$resolvedType;
|
|
5808
6007
|
if (!argResolvedType) {
|
|
@@ -5812,6 +6011,10 @@ function assignableToAttributeParam(arg, param, attr) {
|
|
|
5812
6011
|
let dstIsArray = param.type.array;
|
|
5813
6012
|
if (dstType === "ContextType") {
|
|
5814
6013
|
if (isDataField(attr.$container)) {
|
|
6014
|
+
const dstIsTypedJson = hasAttribute(attr.$container, "@json");
|
|
6015
|
+
if (dstIsTypedJson && attr.decl.ref?.name === "@default") {
|
|
6016
|
+
return argResolvedType.decl === "String";
|
|
6017
|
+
}
|
|
5815
6018
|
dstIsArray = attr.$container.type.array;
|
|
5816
6019
|
}
|
|
5817
6020
|
}
|
|
@@ -5903,6 +6106,9 @@ function isValidAttributeTarget(attrDecl, targetDecl) {
|
|
|
5903
6106
|
case "TypeDefField":
|
|
5904
6107
|
allowed = allowed || isTypeDef(targetDecl.type.reference?.ref);
|
|
5905
6108
|
break;
|
|
6109
|
+
case "ListField":
|
|
6110
|
+
allowed = allowed || !isDataModel(targetDecl.type.reference?.ref) && targetDecl.type.array;
|
|
6111
|
+
break;
|
|
5906
6112
|
default:
|
|
5907
6113
|
break;
|
|
5908
6114
|
}
|
|
@@ -6353,7 +6559,6 @@ var DataSourceValidator = class {
|
|
|
6353
6559
|
validateDuplicatedDeclarations(ds, ds.fields, accept);
|
|
6354
6560
|
this.validateProvider(ds, accept);
|
|
6355
6561
|
this.validateUrl(ds, accept);
|
|
6356
|
-
this.validateRelationMode(ds, accept);
|
|
6357
6562
|
}
|
|
6358
6563
|
validateProvider(ds, accept) {
|
|
6359
6564
|
const provider = ds.fields.find((f) => f.name === "provider");
|
|
@@ -6363,16 +6568,52 @@ var DataSourceValidator = class {
|
|
|
6363
6568
|
});
|
|
6364
6569
|
return;
|
|
6365
6570
|
}
|
|
6366
|
-
const
|
|
6367
|
-
if (!
|
|
6571
|
+
const providerValue = getStringLiteral(provider.value);
|
|
6572
|
+
if (!providerValue) {
|
|
6368
6573
|
accept("error", '"provider" must be set to a string literal', {
|
|
6369
6574
|
node: provider.value
|
|
6370
6575
|
});
|
|
6371
|
-
} else if (!SUPPORTED_PROVIDERS.includes(
|
|
6372
|
-
accept("error", `Provider "${
|
|
6576
|
+
} else if (!SUPPORTED_PROVIDERS.includes(providerValue)) {
|
|
6577
|
+
accept("error", `Provider "${providerValue}" is not supported. Choose from ${SUPPORTED_PROVIDERS.map((p) => '"' + p + '"').join(" | ")}.`, {
|
|
6373
6578
|
node: provider.value
|
|
6374
6579
|
});
|
|
6375
6580
|
}
|
|
6581
|
+
const defaultSchemaField = ds.fields.find((f) => f.name === "defaultSchema");
|
|
6582
|
+
let defaultSchemaValue;
|
|
6583
|
+
if (defaultSchemaField) {
|
|
6584
|
+
if (providerValue !== "postgresql") {
|
|
6585
|
+
accept("error", '"defaultSchema" is only supported for "postgresql" provider', {
|
|
6586
|
+
node: defaultSchemaField
|
|
6587
|
+
});
|
|
6588
|
+
}
|
|
6589
|
+
defaultSchemaValue = getStringLiteral(defaultSchemaField.value);
|
|
6590
|
+
if (!defaultSchemaValue) {
|
|
6591
|
+
accept("error", '"defaultSchema" must be a string literal', {
|
|
6592
|
+
node: defaultSchemaField.value
|
|
6593
|
+
});
|
|
6594
|
+
}
|
|
6595
|
+
}
|
|
6596
|
+
const schemasField = ds.fields.find((f) => f.name === "schemas");
|
|
6597
|
+
if (schemasField) {
|
|
6598
|
+
if (providerValue !== "postgresql") {
|
|
6599
|
+
accept("error", '"schemas" is only supported for "postgresql" provider', {
|
|
6600
|
+
node: schemasField
|
|
6601
|
+
});
|
|
6602
|
+
}
|
|
6603
|
+
const schemasValue = schemasField.value;
|
|
6604
|
+
if (!isConfigArrayExpr(schemasValue) || !schemasValue.items.every((e) => isLiteralExpr(e) && typeof getStringLiteral(e) === "string")) {
|
|
6605
|
+
accept("error", '"schemas" must be an array of string literals', {
|
|
6606
|
+
node: schemasField
|
|
6607
|
+
});
|
|
6608
|
+
} else if (
|
|
6609
|
+
// validate `defaultSchema` is included in `schemas`
|
|
6610
|
+
defaultSchemaValue && !schemasValue.items.some((e) => getStringLiteral(e) === defaultSchemaValue)
|
|
6611
|
+
) {
|
|
6612
|
+
accept("error", `"${defaultSchemaValue}" must be included in the "schemas" array`, {
|
|
6613
|
+
node: schemasField
|
|
6614
|
+
});
|
|
6615
|
+
}
|
|
6616
|
+
}
|
|
6376
6617
|
}
|
|
6377
6618
|
validateUrl(ds, accept) {
|
|
6378
6619
|
const urlField = ds.fields.find((f) => f.name === "url");
|
|
@@ -6386,20 +6627,6 @@ var DataSourceValidator = class {
|
|
|
6386
6627
|
});
|
|
6387
6628
|
}
|
|
6388
6629
|
}
|
|
6389
|
-
validateRelationMode(ds, accept) {
|
|
6390
|
-
const field = ds.fields.find((f) => f.name === "relationMode");
|
|
6391
|
-
if (field) {
|
|
6392
|
-
const val = getStringLiteral(field.value);
|
|
6393
|
-
if (!val || ![
|
|
6394
|
-
"foreignKeys",
|
|
6395
|
-
"prisma"
|
|
6396
|
-
].includes(val)) {
|
|
6397
|
-
accept("error", '"relationMode" must be set to "foreignKeys" or "prisma"', {
|
|
6398
|
-
node: field.value
|
|
6399
|
-
});
|
|
6400
|
-
}
|
|
6401
|
-
}
|
|
6402
|
-
}
|
|
6403
6630
|
};
|
|
6404
6631
|
|
|
6405
6632
|
// src/validators/enum-validator.ts
|
|
@@ -6452,11 +6679,21 @@ var ExpressionValidator = class {
|
|
|
6452
6679
|
}
|
|
6453
6680
|
}
|
|
6454
6681
|
switch (expr.$type) {
|
|
6682
|
+
case "MemberAccessExpr":
|
|
6683
|
+
this.validateMemberAccessExpr(expr, accept);
|
|
6684
|
+
break;
|
|
6455
6685
|
case "BinaryExpr":
|
|
6456
6686
|
this.validateBinaryExpr(expr, accept);
|
|
6457
6687
|
break;
|
|
6458
6688
|
}
|
|
6459
6689
|
}
|
|
6690
|
+
validateMemberAccessExpr(expr, accept) {
|
|
6691
|
+
if (isBeforeInvocation(expr.operand) && isDataModel(expr.$resolvedType?.decl)) {
|
|
6692
|
+
accept("error", "relation fields cannot be accessed from `before()`", {
|
|
6693
|
+
node: expr
|
|
6694
|
+
});
|
|
6695
|
+
}
|
|
6696
|
+
}
|
|
6460
6697
|
validateBinaryExpr(expr, accept) {
|
|
6461
6698
|
switch (expr.operator) {
|
|
6462
6699
|
case "in": {
|
|
@@ -6509,23 +6746,25 @@ var ExpressionValidator = class {
|
|
|
6509
6746
|
"Any"
|
|
6510
6747
|
];
|
|
6511
6748
|
}
|
|
6512
|
-
|
|
6749
|
+
const leftResolvedDecl = expr.left.$resolvedType?.decl;
|
|
6750
|
+
const rightResolvedDecl = expr.right.$resolvedType?.decl;
|
|
6751
|
+
if (leftResolvedDecl && (typeof leftResolvedDecl !== "string" || !supportedShapes.includes(leftResolvedDecl))) {
|
|
6513
6752
|
accept("error", `invalid operand type for "${expr.operator}" operator`, {
|
|
6514
6753
|
node: expr.left
|
|
6515
6754
|
});
|
|
6516
6755
|
return;
|
|
6517
6756
|
}
|
|
6518
|
-
if (typeof
|
|
6757
|
+
if (rightResolvedDecl && (typeof rightResolvedDecl !== "string" || !supportedShapes.includes(rightResolvedDecl))) {
|
|
6519
6758
|
accept("error", `invalid operand type for "${expr.operator}" operator`, {
|
|
6520
6759
|
node: expr.right
|
|
6521
6760
|
});
|
|
6522
6761
|
return;
|
|
6523
6762
|
}
|
|
6524
|
-
if (
|
|
6763
|
+
if (leftResolvedDecl === "DateTime" && rightResolvedDecl && rightResolvedDecl !== "DateTime") {
|
|
6525
6764
|
accept("error", "incompatible operand types", {
|
|
6526
6765
|
node: expr
|
|
6527
6766
|
});
|
|
6528
|
-
} else if (
|
|
6767
|
+
} else if (rightResolvedDecl === "DateTime" && leftResolvedDecl && leftResolvedDecl !== "DateTime") {
|
|
6529
6768
|
accept("error", "incompatible operand types", {
|
|
6530
6769
|
node: expr
|
|
6531
6770
|
});
|
|
@@ -6565,11 +6804,11 @@ var ExpressionValidator = class {
|
|
|
6565
6804
|
});
|
|
6566
6805
|
}
|
|
6567
6806
|
if (isDataFieldReference(expr.left) && (isThisExpr(expr.right) || isDataFieldReference(expr.right))) {
|
|
6568
|
-
accept("error", "comparison between
|
|
6807
|
+
accept("error", "comparison between models is not supported", {
|
|
6569
6808
|
node: expr
|
|
6570
6809
|
});
|
|
6571
6810
|
} else if (isDataFieldReference(expr.right) && (isThisExpr(expr.left) || isDataFieldReference(expr.left))) {
|
|
6572
|
-
accept("error", "comparison between
|
|
6811
|
+
accept("error", "comparison between models is not supported", {
|
|
6573
6812
|
node: expr
|
|
6574
6813
|
});
|
|
6575
6814
|
}
|
|
@@ -6659,41 +6898,39 @@ var FunctionInvocationValidator = class {
|
|
|
6659
6898
|
if (!this.validateArgs(funcDecl, expr.args, accept)) {
|
|
6660
6899
|
return;
|
|
6661
6900
|
}
|
|
6662
|
-
|
|
6663
|
-
|
|
6664
|
-
|
|
6665
|
-
|
|
6666
|
-
|
|
6667
|
-
|
|
6668
|
-
break;
|
|
6669
|
-
}
|
|
6670
|
-
curr = curr.$container;
|
|
6901
|
+
let curr = expr.$container;
|
|
6902
|
+
let containerAttribute;
|
|
6903
|
+
while (curr) {
|
|
6904
|
+
if (isDataModelAttribute(curr) || isDataFieldAttribute(curr)) {
|
|
6905
|
+
containerAttribute = curr;
|
|
6906
|
+
break;
|
|
6671
6907
|
}
|
|
6672
|
-
|
|
6673
|
-
|
|
6674
|
-
|
|
6675
|
-
|
|
6676
|
-
|
|
6908
|
+
curr = curr.$container;
|
|
6909
|
+
}
|
|
6910
|
+
const exprContext = this.getExpressionContext(containerAttribute);
|
|
6911
|
+
const funcAllowedContext = getFunctionExpressionContext(funcDecl);
|
|
6912
|
+
if (exprContext && !funcAllowedContext.includes(exprContext)) {
|
|
6913
|
+
accept("error", `function "${funcDecl.name}" is not allowed in the current context: ${exprContext}`, {
|
|
6914
|
+
node: expr
|
|
6915
|
+
});
|
|
6916
|
+
return;
|
|
6917
|
+
}
|
|
6918
|
+
const allCasing = [
|
|
6919
|
+
"original",
|
|
6920
|
+
"upper",
|
|
6921
|
+
"lower",
|
|
6922
|
+
"capitalize",
|
|
6923
|
+
"uncapitalize"
|
|
6924
|
+
];
|
|
6925
|
+
if ([
|
|
6926
|
+
"currentModel",
|
|
6927
|
+
"currentOperation"
|
|
6928
|
+
].includes(funcDecl.name)) {
|
|
6929
|
+
const arg = getLiteral(expr.args[0]?.value);
|
|
6930
|
+
if (arg && !allCasing.includes(arg)) {
|
|
6931
|
+
accept("error", `argument must be one of: ${allCasing.map((c) => '"' + c + '"').join(", ")}`, {
|
|
6932
|
+
node: expr.args[0]
|
|
6677
6933
|
});
|
|
6678
|
-
return;
|
|
6679
|
-
}
|
|
6680
|
-
const allCasing = [
|
|
6681
|
-
"original",
|
|
6682
|
-
"upper",
|
|
6683
|
-
"lower",
|
|
6684
|
-
"capitalize",
|
|
6685
|
-
"uncapitalize"
|
|
6686
|
-
];
|
|
6687
|
-
if ([
|
|
6688
|
-
"currentModel",
|
|
6689
|
-
"currentOperation"
|
|
6690
|
-
].includes(funcDecl.name)) {
|
|
6691
|
-
const arg = getLiteral(expr.args[0]?.value);
|
|
6692
|
-
if (arg && !allCasing.includes(arg)) {
|
|
6693
|
-
accept("error", `argument must be one of: ${allCasing.map((c) => '"' + c + '"').join(", ")}`, {
|
|
6694
|
-
node: expr.args[0]
|
|
6695
|
-
});
|
|
6696
|
-
}
|
|
6697
6934
|
}
|
|
6698
6935
|
}
|
|
6699
6936
|
const checker = invocationCheckers.get(expr.function.$refText);
|
|
@@ -6701,6 +6938,18 @@ var FunctionInvocationValidator = class {
|
|
|
6701
6938
|
checker.value.call(this, expr, accept);
|
|
6702
6939
|
}
|
|
6703
6940
|
}
|
|
6941
|
+
getExpressionContext(containerAttribute) {
|
|
6942
|
+
if (!containerAttribute) {
|
|
6943
|
+
return void 0;
|
|
6944
|
+
}
|
|
6945
|
+
if (this.isValidationAttribute(containerAttribute)) {
|
|
6946
|
+
return ExpressionContext.ValidationRule;
|
|
6947
|
+
}
|
|
6948
|
+
return match(containerAttribute?.decl.$refText).with("@default", () => ExpressionContext.DefaultValue).with(P.union("@@allow", "@@deny", "@allow", "@deny"), () => ExpressionContext.AccessPolicy).with("@@index", () => ExpressionContext.Index).otherwise(() => void 0);
|
|
6949
|
+
}
|
|
6950
|
+
isValidationAttribute(attr) {
|
|
6951
|
+
return !!attr.decl.ref?.attributes.some((attr2) => attr2.decl.$refText === "@@@validation");
|
|
6952
|
+
}
|
|
6704
6953
|
validateArgs(funcDecl, args, accept) {
|
|
6705
6954
|
let success = true;
|
|
6706
6955
|
for (let i = 0; i < funcDecl.params.length; i++) {
|
|
@@ -6761,6 +7010,44 @@ var FunctionInvocationValidator = class {
|
|
|
6761
7010
|
}
|
|
6762
7011
|
return true;
|
|
6763
7012
|
}
|
|
7013
|
+
_checkLength(expr, accept) {
|
|
7014
|
+
const msg = "argument must be a string or list field";
|
|
7015
|
+
const fieldArg = expr.args[0].value;
|
|
7016
|
+
if (!isDataFieldReference(fieldArg)) {
|
|
7017
|
+
accept("error", msg, {
|
|
7018
|
+
node: expr.args[0]
|
|
7019
|
+
});
|
|
7020
|
+
return;
|
|
7021
|
+
}
|
|
7022
|
+
if (isDataModel(fieldArg.$resolvedType?.decl)) {
|
|
7023
|
+
accept("error", msg, {
|
|
7024
|
+
node: expr.args[0]
|
|
7025
|
+
});
|
|
7026
|
+
return;
|
|
7027
|
+
}
|
|
7028
|
+
if (!fieldArg.$resolvedType?.array && fieldArg.$resolvedType?.decl !== "String") {
|
|
7029
|
+
accept("error", msg, {
|
|
7030
|
+
node: expr.args[0]
|
|
7031
|
+
});
|
|
7032
|
+
}
|
|
7033
|
+
}
|
|
7034
|
+
_checkRegex(expr, accept) {
|
|
7035
|
+
const regex = expr.args[1]?.value;
|
|
7036
|
+
if (!isStringLiteral(regex)) {
|
|
7037
|
+
accept("error", "second argument must be a string literal", {
|
|
7038
|
+
node: expr.args[1]
|
|
7039
|
+
});
|
|
7040
|
+
return;
|
|
7041
|
+
}
|
|
7042
|
+
try {
|
|
7043
|
+
new RegExp(regex.value);
|
|
7044
|
+
} catch (e) {
|
|
7045
|
+
accept("error", "invalid regular expression: " + e.message, {
|
|
7046
|
+
node: expr.args[1]
|
|
7047
|
+
});
|
|
7048
|
+
}
|
|
7049
|
+
}
|
|
7050
|
+
// TODO: move this to policy plugin
|
|
6764
7051
|
_checkCheck(expr, accept) {
|
|
6765
7052
|
let valid = true;
|
|
6766
7053
|
const fieldArg = expr.args[0].value;
|
|
@@ -6832,6 +7119,24 @@ var FunctionInvocationValidator = class {
|
|
|
6832
7119
|
}
|
|
6833
7120
|
}
|
|
6834
7121
|
};
|
|
7122
|
+
_ts_decorate2([
|
|
7123
|
+
func("length"),
|
|
7124
|
+
_ts_metadata2("design:type", Function),
|
|
7125
|
+
_ts_metadata2("design:paramtypes", [
|
|
7126
|
+
typeof InvocationExpr === "undefined" ? Object : InvocationExpr,
|
|
7127
|
+
typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
|
|
7128
|
+
]),
|
|
7129
|
+
_ts_metadata2("design:returntype", void 0)
|
|
7130
|
+
], FunctionInvocationValidator.prototype, "_checkLength", null);
|
|
7131
|
+
_ts_decorate2([
|
|
7132
|
+
func("regex"),
|
|
7133
|
+
_ts_metadata2("design:type", Function),
|
|
7134
|
+
_ts_metadata2("design:paramtypes", [
|
|
7135
|
+
typeof InvocationExpr === "undefined" ? Object : InvocationExpr,
|
|
7136
|
+
typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
|
|
7137
|
+
]),
|
|
7138
|
+
_ts_metadata2("design:returntype", void 0)
|
|
7139
|
+
], FunctionInvocationValidator.prototype, "_checkRegex", null);
|
|
6835
7140
|
_ts_decorate2([
|
|
6836
7141
|
func("check"),
|
|
6837
7142
|
_ts_metadata2("design:type", Function),
|
|
@@ -6941,50 +7246,1039 @@ var ZModelValidator = class {
|
|
|
6941
7246
|
constructor(services) {
|
|
6942
7247
|
this.services = services;
|
|
6943
7248
|
}
|
|
6944
|
-
shouldCheck(node) {
|
|
6945
|
-
let doc;
|
|
6946
|
-
let currNode = node;
|
|
6947
|
-
while (currNode) {
|
|
6948
|
-
if (currNode.$document) {
|
|
6949
|
-
doc = currNode.$document;
|
|
6950
|
-
break;
|
|
6951
|
-
}
|
|
6952
|
-
currNode = currNode.$container;
|
|
6953
|
-
}
|
|
6954
|
-
return doc?.parseResult.lexerErrors.length === 0 && doc?.parseResult.parserErrors.length === 0;
|
|
6955
|
-
}
|
|
6956
7249
|
checkModel(node, accept) {
|
|
6957
|
-
|
|
7250
|
+
new SchemaValidator(this.services.shared.workspace.LangiumDocuments).validate(node, accept);
|
|
6958
7251
|
}
|
|
6959
7252
|
checkDataSource(node, accept) {
|
|
6960
|
-
|
|
7253
|
+
new DataSourceValidator().validate(node, accept);
|
|
6961
7254
|
}
|
|
6962
7255
|
checkDataModel(node, accept) {
|
|
6963
|
-
|
|
7256
|
+
new DataModelValidator().validate(node, accept);
|
|
6964
7257
|
}
|
|
6965
7258
|
checkTypeDef(node, accept) {
|
|
6966
|
-
|
|
7259
|
+
new TypeDefValidator().validate(node, accept);
|
|
6967
7260
|
}
|
|
6968
7261
|
checkEnum(node, accept) {
|
|
6969
|
-
|
|
7262
|
+
new EnumValidator().validate(node, accept);
|
|
6970
7263
|
}
|
|
6971
7264
|
checkAttribute(node, accept) {
|
|
6972
|
-
|
|
7265
|
+
new AttributeValidator().validate(node, accept);
|
|
6973
7266
|
}
|
|
6974
7267
|
checkExpression(node, accept) {
|
|
6975
|
-
|
|
7268
|
+
new ExpressionValidator().validate(node, accept);
|
|
6976
7269
|
}
|
|
6977
7270
|
checkFunctionInvocation(node, accept) {
|
|
6978
|
-
|
|
7271
|
+
new FunctionInvocationValidator().validate(node, accept);
|
|
6979
7272
|
}
|
|
6980
7273
|
checkFunctionDecl(node, accept) {
|
|
6981
|
-
|
|
7274
|
+
new FunctionDeclValidator().validate(node, accept);
|
|
6982
7275
|
}
|
|
6983
7276
|
};
|
|
6984
7277
|
|
|
6985
|
-
// src/zmodel-
|
|
6986
|
-
import {
|
|
7278
|
+
// src/zmodel-comment-provider.ts
|
|
7279
|
+
import { DefaultCommentProvider } from "langium";
|
|
6987
7280
|
import { match as match2 } from "ts-pattern";
|
|
7281
|
+
var ZModelCommentProvider = class extends DefaultCommentProvider {
|
|
7282
|
+
static {
|
|
7283
|
+
__name(this, "ZModelCommentProvider");
|
|
7284
|
+
}
|
|
7285
|
+
getComment(node) {
|
|
7286
|
+
let comment = super.getComment(node);
|
|
7287
|
+
if (!comment) {
|
|
7288
|
+
comment = match2(node).when(isDataModel, (d) => `/**
|
|
7289
|
+
* Model *${d.name}*
|
|
7290
|
+
*/`).when(isTypeDef, (d) => `/**
|
|
7291
|
+
* Type *${d.name}*
|
|
7292
|
+
*/`).when(isEnum, (e) => `/**
|
|
7293
|
+
* Enum *${e.name}*
|
|
7294
|
+
*/`).when(isEnumField, (f) => `/**
|
|
7295
|
+
* Value of enum *${f.$container?.name}*
|
|
7296
|
+
*/`).when(isDataField, (f) => `/**
|
|
7297
|
+
* Field of *${f.$container?.name}*
|
|
7298
|
+
*/`).when(isFunctionDecl, (f) => `/**
|
|
7299
|
+
* Function *${f.name}*
|
|
7300
|
+
*/`).otherwise(() => "");
|
|
7301
|
+
}
|
|
7302
|
+
return comment;
|
|
7303
|
+
}
|
|
7304
|
+
};
|
|
7305
|
+
|
|
7306
|
+
// src/zmodel-completion-provider.ts
|
|
7307
|
+
import { DefaultCompletionProvider } from "langium/lsp";
|
|
7308
|
+
import fs2 from "fs";
|
|
7309
|
+
import { P as P2, match as match3 } from "ts-pattern";
|
|
7310
|
+
import { CompletionItemKind } from "vscode-languageserver";
|
|
7311
|
+
|
|
7312
|
+
// src/zmodel-code-generator.ts
|
|
7313
|
+
function _ts_decorate3(decorators, target, key, desc) {
|
|
7314
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
7315
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
7316
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
7317
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7318
|
+
}
|
|
7319
|
+
__name(_ts_decorate3, "_ts_decorate");
|
|
7320
|
+
function _ts_metadata3(k, v) {
|
|
7321
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
7322
|
+
}
|
|
7323
|
+
__name(_ts_metadata3, "_ts_metadata");
|
|
7324
|
+
var generationHandlers = /* @__PURE__ */ new Map();
|
|
7325
|
+
function gen(name) {
|
|
7326
|
+
return function(_target, _propertyKey, descriptor) {
|
|
7327
|
+
if (!generationHandlers.get(name)) {
|
|
7328
|
+
generationHandlers.set(name, descriptor);
|
|
7329
|
+
}
|
|
7330
|
+
return descriptor;
|
|
7331
|
+
};
|
|
7332
|
+
}
|
|
7333
|
+
__name(gen, "gen");
|
|
7334
|
+
var ZModelCodeGenerator = class {
|
|
7335
|
+
static {
|
|
7336
|
+
__name(this, "ZModelCodeGenerator");
|
|
7337
|
+
}
|
|
7338
|
+
options;
|
|
7339
|
+
constructor(options) {
|
|
7340
|
+
this.options = {
|
|
7341
|
+
binaryExprNumberOfSpaces: options?.binaryExprNumberOfSpaces ?? 1,
|
|
7342
|
+
unaryExprNumberOfSpaces: options?.unaryExprNumberOfSpaces ?? 0,
|
|
7343
|
+
indent: options?.indent ?? 4,
|
|
7344
|
+
quote: options?.quote ?? "single"
|
|
7345
|
+
};
|
|
7346
|
+
}
|
|
7347
|
+
/**
|
|
7348
|
+
* Generates ZModel source code from AST.
|
|
7349
|
+
*/
|
|
7350
|
+
generate(ast) {
|
|
7351
|
+
const handler = generationHandlers.get(ast.$type);
|
|
7352
|
+
if (!handler) {
|
|
7353
|
+
throw new Error(`No generation handler found for ${ast.$type}`);
|
|
7354
|
+
}
|
|
7355
|
+
return handler.value.call(this, ast);
|
|
7356
|
+
}
|
|
7357
|
+
_generateModel(ast) {
|
|
7358
|
+
return ast.declarations.map((d) => this.generate(d)).join("\n\n");
|
|
7359
|
+
}
|
|
7360
|
+
_generateDataSource(ast) {
|
|
7361
|
+
return `datasource ${ast.name} {
|
|
7362
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
|
|
7363
|
+
}`;
|
|
7364
|
+
}
|
|
7365
|
+
_generateEnum(ast) {
|
|
7366
|
+
return `enum ${ast.name} {
|
|
7367
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
|
|
7368
|
+
}`;
|
|
7369
|
+
}
|
|
7370
|
+
_generateEnumField(ast) {
|
|
7371
|
+
return `${ast.name}${ast.attributes.length > 0 ? " " + ast.attributes.map((x) => this.generate(x)).join(" ") : ""}`;
|
|
7372
|
+
}
|
|
7373
|
+
_generateGenerator(ast) {
|
|
7374
|
+
return `generator ${ast.name} {
|
|
7375
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
|
|
7376
|
+
}`;
|
|
7377
|
+
}
|
|
7378
|
+
_generateConfigField(ast) {
|
|
7379
|
+
return `${ast.name} = ${this.generate(ast.value)}`;
|
|
7380
|
+
}
|
|
7381
|
+
_generateConfigArrayExpr(ast) {
|
|
7382
|
+
return `[${ast.items.map((x) => this.generate(x)).join(", ")}]`;
|
|
7383
|
+
}
|
|
7384
|
+
_generateConfigInvocationExpr(ast) {
|
|
7385
|
+
if (ast.args.length === 0) {
|
|
7386
|
+
return ast.name;
|
|
7387
|
+
} else {
|
|
7388
|
+
return `${ast.name}(${ast.args.map((x) => (x.name ? x.name + ": " : "") + this.generate(x.value)).join(", ")})`;
|
|
7389
|
+
}
|
|
7390
|
+
}
|
|
7391
|
+
_generatePlugin(ast) {
|
|
7392
|
+
return `plugin ${ast.name} {
|
|
7393
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
|
|
7394
|
+
}`;
|
|
7395
|
+
}
|
|
7396
|
+
_generatePluginField(ast) {
|
|
7397
|
+
return `${ast.name} = ${this.generate(ast.value)}`;
|
|
7398
|
+
}
|
|
7399
|
+
_generateDataModel(ast) {
|
|
7400
|
+
return `${ast.isView ? "view" : "model"} ${ast.name}${ast.mixins.length > 0 ? " mixes " + ast.mixins.map((x) => x.ref?.name).join(", ") : ""} {
|
|
7401
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}${ast.attributes.length > 0 ? "\n\n" + ast.attributes.map((x) => this.indent + this.generate(x)).join("\n") : ""}
|
|
7402
|
+
}`;
|
|
7403
|
+
}
|
|
7404
|
+
_generateDataField(ast) {
|
|
7405
|
+
return `${ast.name} ${this.fieldType(ast.type)}${ast.attributes.length > 0 ? " " + ast.attributes.map((x) => this.generate(x)).join(" ") : ""}`;
|
|
7406
|
+
}
|
|
7407
|
+
fieldType(type) {
|
|
7408
|
+
const baseType = type.type ? type.type : type.$type == "DataFieldType" && type.unsupported ? "Unsupported(" + this.generate(type.unsupported.value) + ")" : type.reference?.$refText;
|
|
7409
|
+
return `${baseType}${type.array ? "[]" : ""}${type.optional ? "?" : ""}`;
|
|
7410
|
+
}
|
|
7411
|
+
_generateDataModelAttribute(ast) {
|
|
7412
|
+
return this.attribute(ast);
|
|
7413
|
+
}
|
|
7414
|
+
_generateDataFieldAttribute(ast) {
|
|
7415
|
+
return this.attribute(ast);
|
|
7416
|
+
}
|
|
7417
|
+
attribute(ast) {
|
|
7418
|
+
const args = ast.args.length ? `(${ast.args.map((x) => this.generate(x)).join(", ")})` : "";
|
|
7419
|
+
return `${resolved(ast.decl).name}${args}`;
|
|
7420
|
+
}
|
|
7421
|
+
_generateAttributeArg(ast) {
|
|
7422
|
+
if (ast.name) {
|
|
7423
|
+
return `${ast.name}: ${this.generate(ast.value)}`;
|
|
7424
|
+
} else {
|
|
7425
|
+
return this.generate(ast.value);
|
|
7426
|
+
}
|
|
7427
|
+
}
|
|
7428
|
+
_generateObjectExpr(ast) {
|
|
7429
|
+
return `{ ${ast.fields.map((field) => this.objectField(field)).join(", ")} }`;
|
|
7430
|
+
}
|
|
7431
|
+
objectField(field) {
|
|
7432
|
+
return `${field.name}: ${this.generate(field.value)}`;
|
|
7433
|
+
}
|
|
7434
|
+
_generateArrayExpr(ast) {
|
|
7435
|
+
return `[${ast.items.map((item) => this.generate(item)).join(", ")}]`;
|
|
7436
|
+
}
|
|
7437
|
+
_generateLiteralExpr(ast) {
|
|
7438
|
+
return this.options.quote === "single" ? `'${ast.value}'` : `"${ast.value}"`;
|
|
7439
|
+
}
|
|
7440
|
+
_generateNumberLiteral(ast) {
|
|
7441
|
+
return ast.value.toString();
|
|
7442
|
+
}
|
|
7443
|
+
_generateBooleanLiteral(ast) {
|
|
7444
|
+
return ast.value.toString();
|
|
7445
|
+
}
|
|
7446
|
+
_generateUnaryExpr(ast) {
|
|
7447
|
+
return `${ast.operator}${this.unaryExprSpace}${this.generate(ast.operand)}`;
|
|
7448
|
+
}
|
|
7449
|
+
_generateBinaryExpr(ast) {
|
|
7450
|
+
const operator = ast.operator;
|
|
7451
|
+
const isCollectionPredicate2 = this.isCollectionPredicateOperator(operator);
|
|
7452
|
+
const rightExpr = this.generate(ast.right);
|
|
7453
|
+
const { left: isLeftParenthesis, right: isRightParenthesis } = this.isParenthesesNeededForBinaryExpr(ast);
|
|
7454
|
+
return `${isLeftParenthesis ? "(" : ""}${this.generate(ast.left)}${isLeftParenthesis ? ")" : ""}${isCollectionPredicate2 ? "" : this.binaryExprSpace}${operator}${isCollectionPredicate2 ? "" : this.binaryExprSpace}${isRightParenthesis ? "(" : ""}${isCollectionPredicate2 ? `[${rightExpr}]` : rightExpr}${isRightParenthesis ? ")" : ""}`;
|
|
7455
|
+
}
|
|
7456
|
+
_generateReferenceExpr(ast) {
|
|
7457
|
+
const args = ast.args.length ? `(${ast.args.map((x) => this.generate(x)).join(", ")})` : "";
|
|
7458
|
+
return `${ast.target.ref?.name}${args}`;
|
|
7459
|
+
}
|
|
7460
|
+
_generateReferenceArg(ast) {
|
|
7461
|
+
return `${ast.name}:${this.generate(ast.value)}`;
|
|
7462
|
+
}
|
|
7463
|
+
_generateMemberExpr(ast) {
|
|
7464
|
+
return `${this.generate(ast.operand)}.${ast.member.ref?.name}`;
|
|
7465
|
+
}
|
|
7466
|
+
_generateInvocationExpr(ast) {
|
|
7467
|
+
return `${ast.function.ref?.name}(${ast.args.map((x) => this.argument(x)).join(", ")})`;
|
|
7468
|
+
}
|
|
7469
|
+
_generateNullExpr() {
|
|
7470
|
+
return "null";
|
|
7471
|
+
}
|
|
7472
|
+
_generateThisExpr() {
|
|
7473
|
+
return "this";
|
|
7474
|
+
}
|
|
7475
|
+
_generateAttribute(ast) {
|
|
7476
|
+
return `attribute ${ast.name}(${ast.params.map((x) => this.generate(x)).join(", ")})`;
|
|
7477
|
+
}
|
|
7478
|
+
_generateAttributeParam(ast) {
|
|
7479
|
+
return `${ast.default ? "_ " : ""}${ast.name}: ${this.generate(ast.type)}`;
|
|
7480
|
+
}
|
|
7481
|
+
_generateAttributeParamType(ast) {
|
|
7482
|
+
return `${ast.type ?? ast.reference?.$refText}${ast.array ? "[]" : ""}${ast.optional ? "?" : ""}`;
|
|
7483
|
+
}
|
|
7484
|
+
_generateFunctionDecl(ast) {
|
|
7485
|
+
return `function ${ast.name}(${ast.params.map((x) => this.generate(x)).join(", ")}) ${ast.returnType ? ": " + this.generate(ast.returnType) : ""} {}`;
|
|
7486
|
+
}
|
|
7487
|
+
_generateFunctionParam(ast) {
|
|
7488
|
+
return `${ast.name}: ${this.generate(ast.type)}`;
|
|
7489
|
+
}
|
|
7490
|
+
_generateFunctionParamType(ast) {
|
|
7491
|
+
return `${ast.type ?? ast.reference?.$refText}${ast.array ? "[]" : ""}`;
|
|
7492
|
+
}
|
|
7493
|
+
_generateTypeDef(ast) {
|
|
7494
|
+
return `type ${ast.name} {
|
|
7495
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}${ast.attributes.length > 0 ? "\n\n" + ast.attributes.map((x) => this.indent + this.generate(x)).join("\n") : ""}
|
|
7496
|
+
}`;
|
|
7497
|
+
}
|
|
7498
|
+
argument(ast) {
|
|
7499
|
+
return this.generate(ast.value);
|
|
7500
|
+
}
|
|
7501
|
+
get binaryExprSpace() {
|
|
7502
|
+
return " ".repeat(this.options.binaryExprNumberOfSpaces);
|
|
7503
|
+
}
|
|
7504
|
+
get unaryExprSpace() {
|
|
7505
|
+
return " ".repeat(this.options.unaryExprNumberOfSpaces);
|
|
7506
|
+
}
|
|
7507
|
+
get indent() {
|
|
7508
|
+
return " ".repeat(this.options.indent);
|
|
7509
|
+
}
|
|
7510
|
+
isParenthesesNeededForBinaryExpr(ast) {
|
|
7511
|
+
const result = {
|
|
7512
|
+
left: false,
|
|
7513
|
+
right: false
|
|
7514
|
+
};
|
|
7515
|
+
const operator = ast.operator;
|
|
7516
|
+
const isCollectionPredicate2 = this.isCollectionPredicateOperator(operator);
|
|
7517
|
+
const currentPriority = BinaryExprOperatorPriority[operator];
|
|
7518
|
+
if (ast.left.$type === BinaryExpr && BinaryExprOperatorPriority[ast.left["operator"]] < currentPriority) {
|
|
7519
|
+
result.left = true;
|
|
7520
|
+
}
|
|
7521
|
+
if (!isCollectionPredicate2 && ast.right.$type === BinaryExpr && BinaryExprOperatorPriority[ast.right["operator"]] <= currentPriority) {
|
|
7522
|
+
result.right = true;
|
|
7523
|
+
}
|
|
7524
|
+
return result;
|
|
7525
|
+
}
|
|
7526
|
+
isCollectionPredicateOperator(op) {
|
|
7527
|
+
return [
|
|
7528
|
+
"?",
|
|
7529
|
+
"!",
|
|
7530
|
+
"^"
|
|
7531
|
+
].includes(op);
|
|
7532
|
+
}
|
|
7533
|
+
};
|
|
7534
|
+
_ts_decorate3([
|
|
7535
|
+
gen(Model),
|
|
7536
|
+
_ts_metadata3("design:type", Function),
|
|
7537
|
+
_ts_metadata3("design:paramtypes", [
|
|
7538
|
+
typeof Model === "undefined" ? Object : Model
|
|
7539
|
+
]),
|
|
7540
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7541
|
+
], ZModelCodeGenerator.prototype, "_generateModel", null);
|
|
7542
|
+
_ts_decorate3([
|
|
7543
|
+
gen(DataSource),
|
|
7544
|
+
_ts_metadata3("design:type", Function),
|
|
7545
|
+
_ts_metadata3("design:paramtypes", [
|
|
7546
|
+
typeof DataSource === "undefined" ? Object : DataSource
|
|
7547
|
+
]),
|
|
7548
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7549
|
+
], ZModelCodeGenerator.prototype, "_generateDataSource", null);
|
|
7550
|
+
_ts_decorate3([
|
|
7551
|
+
gen(Enum),
|
|
7552
|
+
_ts_metadata3("design:type", Function),
|
|
7553
|
+
_ts_metadata3("design:paramtypes", [
|
|
7554
|
+
typeof Enum === "undefined" ? Object : Enum
|
|
7555
|
+
]),
|
|
7556
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7557
|
+
], ZModelCodeGenerator.prototype, "_generateEnum", null);
|
|
7558
|
+
_ts_decorate3([
|
|
7559
|
+
gen(EnumField),
|
|
7560
|
+
_ts_metadata3("design:type", Function),
|
|
7561
|
+
_ts_metadata3("design:paramtypes", [
|
|
7562
|
+
typeof EnumField === "undefined" ? Object : EnumField
|
|
7563
|
+
]),
|
|
7564
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7565
|
+
], ZModelCodeGenerator.prototype, "_generateEnumField", null);
|
|
7566
|
+
_ts_decorate3([
|
|
7567
|
+
gen(GeneratorDecl),
|
|
7568
|
+
_ts_metadata3("design:type", Function),
|
|
7569
|
+
_ts_metadata3("design:paramtypes", [
|
|
7570
|
+
typeof GeneratorDecl === "undefined" ? Object : GeneratorDecl
|
|
7571
|
+
]),
|
|
7572
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7573
|
+
], ZModelCodeGenerator.prototype, "_generateGenerator", null);
|
|
7574
|
+
_ts_decorate3([
|
|
7575
|
+
gen(ConfigField),
|
|
7576
|
+
_ts_metadata3("design:type", Function),
|
|
7577
|
+
_ts_metadata3("design:paramtypes", [
|
|
7578
|
+
typeof ConfigField === "undefined" ? Object : ConfigField
|
|
7579
|
+
]),
|
|
7580
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7581
|
+
], ZModelCodeGenerator.prototype, "_generateConfigField", null);
|
|
7582
|
+
_ts_decorate3([
|
|
7583
|
+
gen(ConfigArrayExpr),
|
|
7584
|
+
_ts_metadata3("design:type", Function),
|
|
7585
|
+
_ts_metadata3("design:paramtypes", [
|
|
7586
|
+
typeof ConfigArrayExpr === "undefined" ? Object : ConfigArrayExpr
|
|
7587
|
+
]),
|
|
7588
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7589
|
+
], ZModelCodeGenerator.prototype, "_generateConfigArrayExpr", null);
|
|
7590
|
+
_ts_decorate3([
|
|
7591
|
+
gen(ConfigInvocationExpr),
|
|
7592
|
+
_ts_metadata3("design:type", Function),
|
|
7593
|
+
_ts_metadata3("design:paramtypes", [
|
|
7594
|
+
typeof ConfigInvocationExpr === "undefined" ? Object : ConfigInvocationExpr
|
|
7595
|
+
]),
|
|
7596
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7597
|
+
], ZModelCodeGenerator.prototype, "_generateConfigInvocationExpr", null);
|
|
7598
|
+
_ts_decorate3([
|
|
7599
|
+
gen(Plugin),
|
|
7600
|
+
_ts_metadata3("design:type", Function),
|
|
7601
|
+
_ts_metadata3("design:paramtypes", [
|
|
7602
|
+
typeof Plugin === "undefined" ? Object : Plugin
|
|
7603
|
+
]),
|
|
7604
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7605
|
+
], ZModelCodeGenerator.prototype, "_generatePlugin", null);
|
|
7606
|
+
_ts_decorate3([
|
|
7607
|
+
gen(PluginField),
|
|
7608
|
+
_ts_metadata3("design:type", Function),
|
|
7609
|
+
_ts_metadata3("design:paramtypes", [
|
|
7610
|
+
typeof PluginField === "undefined" ? Object : PluginField
|
|
7611
|
+
]),
|
|
7612
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7613
|
+
], ZModelCodeGenerator.prototype, "_generatePluginField", null);
|
|
7614
|
+
_ts_decorate3([
|
|
7615
|
+
gen(DataModel),
|
|
7616
|
+
_ts_metadata3("design:type", Function),
|
|
7617
|
+
_ts_metadata3("design:paramtypes", [
|
|
7618
|
+
typeof DataModel === "undefined" ? Object : DataModel
|
|
7619
|
+
]),
|
|
7620
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7621
|
+
], ZModelCodeGenerator.prototype, "_generateDataModel", null);
|
|
7622
|
+
_ts_decorate3([
|
|
7623
|
+
gen(DataField),
|
|
7624
|
+
_ts_metadata3("design:type", Function),
|
|
7625
|
+
_ts_metadata3("design:paramtypes", [
|
|
7626
|
+
typeof DataField === "undefined" ? Object : DataField
|
|
7627
|
+
]),
|
|
7628
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7629
|
+
], ZModelCodeGenerator.prototype, "_generateDataField", null);
|
|
7630
|
+
_ts_decorate3([
|
|
7631
|
+
gen(DataModelAttribute),
|
|
7632
|
+
_ts_metadata3("design:type", Function),
|
|
7633
|
+
_ts_metadata3("design:paramtypes", [
|
|
7634
|
+
typeof DataModelAttribute === "undefined" ? Object : DataModelAttribute
|
|
7635
|
+
]),
|
|
7636
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7637
|
+
], ZModelCodeGenerator.prototype, "_generateDataModelAttribute", null);
|
|
7638
|
+
_ts_decorate3([
|
|
7639
|
+
gen(DataFieldAttribute),
|
|
7640
|
+
_ts_metadata3("design:type", Function),
|
|
7641
|
+
_ts_metadata3("design:paramtypes", [
|
|
7642
|
+
typeof DataFieldAttribute === "undefined" ? Object : DataFieldAttribute
|
|
7643
|
+
]),
|
|
7644
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7645
|
+
], ZModelCodeGenerator.prototype, "_generateDataFieldAttribute", null);
|
|
7646
|
+
_ts_decorate3([
|
|
7647
|
+
gen(AttributeArg),
|
|
7648
|
+
_ts_metadata3("design:type", Function),
|
|
7649
|
+
_ts_metadata3("design:paramtypes", [
|
|
7650
|
+
typeof AttributeArg === "undefined" ? Object : AttributeArg
|
|
7651
|
+
]),
|
|
7652
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7653
|
+
], ZModelCodeGenerator.prototype, "_generateAttributeArg", null);
|
|
7654
|
+
_ts_decorate3([
|
|
7655
|
+
gen(ObjectExpr),
|
|
7656
|
+
_ts_metadata3("design:type", Function),
|
|
7657
|
+
_ts_metadata3("design:paramtypes", [
|
|
7658
|
+
typeof ObjectExpr === "undefined" ? Object : ObjectExpr
|
|
7659
|
+
]),
|
|
7660
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7661
|
+
], ZModelCodeGenerator.prototype, "_generateObjectExpr", null);
|
|
7662
|
+
_ts_decorate3([
|
|
7663
|
+
gen(ArrayExpr),
|
|
7664
|
+
_ts_metadata3("design:type", Function),
|
|
7665
|
+
_ts_metadata3("design:paramtypes", [
|
|
7666
|
+
typeof ArrayExpr === "undefined" ? Object : ArrayExpr
|
|
7667
|
+
]),
|
|
7668
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7669
|
+
], ZModelCodeGenerator.prototype, "_generateArrayExpr", null);
|
|
7670
|
+
_ts_decorate3([
|
|
7671
|
+
gen(StringLiteral),
|
|
7672
|
+
_ts_metadata3("design:type", Function),
|
|
7673
|
+
_ts_metadata3("design:paramtypes", [
|
|
7674
|
+
typeof LiteralExpr === "undefined" ? Object : LiteralExpr
|
|
7675
|
+
]),
|
|
7676
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7677
|
+
], ZModelCodeGenerator.prototype, "_generateLiteralExpr", null);
|
|
7678
|
+
_ts_decorate3([
|
|
7679
|
+
gen(NumberLiteral),
|
|
7680
|
+
_ts_metadata3("design:type", Function),
|
|
7681
|
+
_ts_metadata3("design:paramtypes", [
|
|
7682
|
+
typeof NumberLiteral === "undefined" ? Object : NumberLiteral
|
|
7683
|
+
]),
|
|
7684
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7685
|
+
], ZModelCodeGenerator.prototype, "_generateNumberLiteral", null);
|
|
7686
|
+
_ts_decorate3([
|
|
7687
|
+
gen(BooleanLiteral),
|
|
7688
|
+
_ts_metadata3("design:type", Function),
|
|
7689
|
+
_ts_metadata3("design:paramtypes", [
|
|
7690
|
+
typeof BooleanLiteral === "undefined" ? Object : BooleanLiteral
|
|
7691
|
+
]),
|
|
7692
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7693
|
+
], ZModelCodeGenerator.prototype, "_generateBooleanLiteral", null);
|
|
7694
|
+
_ts_decorate3([
|
|
7695
|
+
gen(UnaryExpr),
|
|
7696
|
+
_ts_metadata3("design:type", Function),
|
|
7697
|
+
_ts_metadata3("design:paramtypes", [
|
|
7698
|
+
typeof UnaryExpr === "undefined" ? Object : UnaryExpr
|
|
7699
|
+
]),
|
|
7700
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7701
|
+
], ZModelCodeGenerator.prototype, "_generateUnaryExpr", null);
|
|
7702
|
+
_ts_decorate3([
|
|
7703
|
+
gen(BinaryExpr),
|
|
7704
|
+
_ts_metadata3("design:type", Function),
|
|
7705
|
+
_ts_metadata3("design:paramtypes", [
|
|
7706
|
+
typeof BinaryExpr === "undefined" ? Object : BinaryExpr
|
|
7707
|
+
]),
|
|
7708
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7709
|
+
], ZModelCodeGenerator.prototype, "_generateBinaryExpr", null);
|
|
7710
|
+
_ts_decorate3([
|
|
7711
|
+
gen(ReferenceExpr),
|
|
7712
|
+
_ts_metadata3("design:type", Function),
|
|
7713
|
+
_ts_metadata3("design:paramtypes", [
|
|
7714
|
+
typeof ReferenceExpr === "undefined" ? Object : ReferenceExpr
|
|
7715
|
+
]),
|
|
7716
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7717
|
+
], ZModelCodeGenerator.prototype, "_generateReferenceExpr", null);
|
|
7718
|
+
_ts_decorate3([
|
|
7719
|
+
gen(ReferenceArg),
|
|
7720
|
+
_ts_metadata3("design:type", Function),
|
|
7721
|
+
_ts_metadata3("design:paramtypes", [
|
|
7722
|
+
typeof ReferenceArg === "undefined" ? Object : ReferenceArg
|
|
7723
|
+
]),
|
|
7724
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7725
|
+
], ZModelCodeGenerator.prototype, "_generateReferenceArg", null);
|
|
7726
|
+
_ts_decorate3([
|
|
7727
|
+
gen(MemberAccessExpr),
|
|
7728
|
+
_ts_metadata3("design:type", Function),
|
|
7729
|
+
_ts_metadata3("design:paramtypes", [
|
|
7730
|
+
typeof MemberAccessExpr === "undefined" ? Object : MemberAccessExpr
|
|
7731
|
+
]),
|
|
7732
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7733
|
+
], ZModelCodeGenerator.prototype, "_generateMemberExpr", null);
|
|
7734
|
+
_ts_decorate3([
|
|
7735
|
+
gen(InvocationExpr),
|
|
7736
|
+
_ts_metadata3("design:type", Function),
|
|
7737
|
+
_ts_metadata3("design:paramtypes", [
|
|
7738
|
+
typeof InvocationExpr === "undefined" ? Object : InvocationExpr
|
|
7739
|
+
]),
|
|
7740
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7741
|
+
], ZModelCodeGenerator.prototype, "_generateInvocationExpr", null);
|
|
7742
|
+
_ts_decorate3([
|
|
7743
|
+
gen(NullExpr),
|
|
7744
|
+
_ts_metadata3("design:type", Function),
|
|
7745
|
+
_ts_metadata3("design:paramtypes", []),
|
|
7746
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7747
|
+
], ZModelCodeGenerator.prototype, "_generateNullExpr", null);
|
|
7748
|
+
_ts_decorate3([
|
|
7749
|
+
gen(ThisExpr),
|
|
7750
|
+
_ts_metadata3("design:type", Function),
|
|
7751
|
+
_ts_metadata3("design:paramtypes", []),
|
|
7752
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7753
|
+
], ZModelCodeGenerator.prototype, "_generateThisExpr", null);
|
|
7754
|
+
_ts_decorate3([
|
|
7755
|
+
gen(Attribute),
|
|
7756
|
+
_ts_metadata3("design:type", Function),
|
|
7757
|
+
_ts_metadata3("design:paramtypes", [
|
|
7758
|
+
typeof Attribute === "undefined" ? Object : Attribute
|
|
7759
|
+
]),
|
|
7760
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7761
|
+
], ZModelCodeGenerator.prototype, "_generateAttribute", null);
|
|
7762
|
+
_ts_decorate3([
|
|
7763
|
+
gen(AttributeParam),
|
|
7764
|
+
_ts_metadata3("design:type", Function),
|
|
7765
|
+
_ts_metadata3("design:paramtypes", [
|
|
7766
|
+
typeof AttributeParam === "undefined" ? Object : AttributeParam
|
|
7767
|
+
]),
|
|
7768
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7769
|
+
], ZModelCodeGenerator.prototype, "_generateAttributeParam", null);
|
|
7770
|
+
_ts_decorate3([
|
|
7771
|
+
gen(AttributeParamType),
|
|
7772
|
+
_ts_metadata3("design:type", Function),
|
|
7773
|
+
_ts_metadata3("design:paramtypes", [
|
|
7774
|
+
typeof AttributeParamType === "undefined" ? Object : AttributeParamType
|
|
7775
|
+
]),
|
|
7776
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7777
|
+
], ZModelCodeGenerator.prototype, "_generateAttributeParamType", null);
|
|
7778
|
+
_ts_decorate3([
|
|
7779
|
+
gen(FunctionDecl),
|
|
7780
|
+
_ts_metadata3("design:type", Function),
|
|
7781
|
+
_ts_metadata3("design:paramtypes", [
|
|
7782
|
+
typeof FunctionDecl === "undefined" ? Object : FunctionDecl
|
|
7783
|
+
]),
|
|
7784
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7785
|
+
], ZModelCodeGenerator.prototype, "_generateFunctionDecl", null);
|
|
7786
|
+
_ts_decorate3([
|
|
7787
|
+
gen(FunctionParam),
|
|
7788
|
+
_ts_metadata3("design:type", Function),
|
|
7789
|
+
_ts_metadata3("design:paramtypes", [
|
|
7790
|
+
typeof FunctionParam === "undefined" ? Object : FunctionParam
|
|
7791
|
+
]),
|
|
7792
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7793
|
+
], ZModelCodeGenerator.prototype, "_generateFunctionParam", null);
|
|
7794
|
+
_ts_decorate3([
|
|
7795
|
+
gen(FunctionParamType),
|
|
7796
|
+
_ts_metadata3("design:type", Function),
|
|
7797
|
+
_ts_metadata3("design:paramtypes", [
|
|
7798
|
+
typeof FunctionParamType === "undefined" ? Object : FunctionParamType
|
|
7799
|
+
]),
|
|
7800
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7801
|
+
], ZModelCodeGenerator.prototype, "_generateFunctionParamType", null);
|
|
7802
|
+
_ts_decorate3([
|
|
7803
|
+
gen(TypeDef),
|
|
7804
|
+
_ts_metadata3("design:type", Function),
|
|
7805
|
+
_ts_metadata3("design:paramtypes", [
|
|
7806
|
+
typeof TypeDef === "undefined" ? Object : TypeDef
|
|
7807
|
+
]),
|
|
7808
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7809
|
+
], ZModelCodeGenerator.prototype, "_generateTypeDef", null);
|
|
7810
|
+
|
|
7811
|
+
// src/zmodel-completion-provider.ts
|
|
7812
|
+
var ZModelCompletionProvider = class extends DefaultCompletionProvider {
|
|
7813
|
+
static {
|
|
7814
|
+
__name(this, "ZModelCompletionProvider");
|
|
7815
|
+
}
|
|
7816
|
+
services;
|
|
7817
|
+
constructor(services) {
|
|
7818
|
+
super(services), this.services = services;
|
|
7819
|
+
}
|
|
7820
|
+
completionOptions = {
|
|
7821
|
+
triggerCharacters: [
|
|
7822
|
+
"@",
|
|
7823
|
+
"(",
|
|
7824
|
+
",",
|
|
7825
|
+
"."
|
|
7826
|
+
]
|
|
7827
|
+
};
|
|
7828
|
+
async getCompletion(document, params) {
|
|
7829
|
+
try {
|
|
7830
|
+
return await super.getCompletion(document, params);
|
|
7831
|
+
} catch (e) {
|
|
7832
|
+
console.error("Completion error:", e.message);
|
|
7833
|
+
return void 0;
|
|
7834
|
+
}
|
|
7835
|
+
}
|
|
7836
|
+
completionFor(context, next, acceptor) {
|
|
7837
|
+
if (isDataModelAttribute(context.node) || isDataFieldAttribute(context.node)) {
|
|
7838
|
+
const completions = this.getCompletionFromHint(context.node);
|
|
7839
|
+
if (completions) {
|
|
7840
|
+
completions.forEach((c) => acceptor(context, c));
|
|
7841
|
+
return;
|
|
7842
|
+
}
|
|
7843
|
+
}
|
|
7844
|
+
return super.completionFor(context, next, acceptor);
|
|
7845
|
+
}
|
|
7846
|
+
getCompletionFromHint(contextNode) {
|
|
7847
|
+
const unfilledParams = this.getUnfilledAttributeParams(contextNode);
|
|
7848
|
+
const nextParam = unfilledParams[0];
|
|
7849
|
+
if (!nextParam) {
|
|
7850
|
+
return void 0;
|
|
7851
|
+
}
|
|
7852
|
+
const hintAttr = getAttribute(nextParam, "@@@completionHint");
|
|
7853
|
+
if (hintAttr) {
|
|
7854
|
+
const hint = hintAttr.args[0];
|
|
7855
|
+
if (hint?.value) {
|
|
7856
|
+
if (isArrayExpr(hint.value)) {
|
|
7857
|
+
return hint.value.items.map((item) => {
|
|
7858
|
+
return {
|
|
7859
|
+
label: `${item.value}`,
|
|
7860
|
+
kind: CompletionItemKind.Value,
|
|
7861
|
+
detail: "Parameter",
|
|
7862
|
+
sortText: "0"
|
|
7863
|
+
};
|
|
7864
|
+
});
|
|
7865
|
+
}
|
|
7866
|
+
}
|
|
7867
|
+
}
|
|
7868
|
+
return void 0;
|
|
7869
|
+
}
|
|
7870
|
+
// TODO: this doesn't work when the file contains parse errors
|
|
7871
|
+
getUnfilledAttributeParams(contextNode) {
|
|
7872
|
+
try {
|
|
7873
|
+
const params = contextNode.decl.ref?.params;
|
|
7874
|
+
if (params) {
|
|
7875
|
+
const args = contextNode.args;
|
|
7876
|
+
let unfilledParams = [
|
|
7877
|
+
...params
|
|
7878
|
+
];
|
|
7879
|
+
args.forEach((arg) => {
|
|
7880
|
+
if (arg.name) {
|
|
7881
|
+
unfilledParams = unfilledParams.filter((p) => p.name !== arg.name);
|
|
7882
|
+
} else {
|
|
7883
|
+
unfilledParams.shift();
|
|
7884
|
+
}
|
|
7885
|
+
});
|
|
7886
|
+
return unfilledParams;
|
|
7887
|
+
}
|
|
7888
|
+
} catch {
|
|
7889
|
+
}
|
|
7890
|
+
return [];
|
|
7891
|
+
}
|
|
7892
|
+
completionForCrossReference(context, crossRef, acceptor) {
|
|
7893
|
+
if (crossRef.property === "member" && !isMemberAccessExpr(context.node)) {
|
|
7894
|
+
return;
|
|
7895
|
+
}
|
|
7896
|
+
const customAcceptor = /* @__PURE__ */ __name((context2, item) => {
|
|
7897
|
+
if (item.insertText?.startsWith("@@@") || item.label?.startsWith("@@@")) {
|
|
7898
|
+
return;
|
|
7899
|
+
}
|
|
7900
|
+
if ("nodeDescription" in item) {
|
|
7901
|
+
const node = this.getAstNode(item.nodeDescription);
|
|
7902
|
+
if (!node) {
|
|
7903
|
+
return;
|
|
7904
|
+
}
|
|
7905
|
+
if ((isEnum(node) || isEnumField(node)) && isFromStdlib(node)) {
|
|
7906
|
+
return;
|
|
7907
|
+
}
|
|
7908
|
+
if ((isDataModelAttribute(context2.node) || isDataFieldAttribute(context2.node)) && !this.filterAttributeApplicationCompletion(context2.node, node)) {
|
|
7909
|
+
return;
|
|
7910
|
+
}
|
|
7911
|
+
}
|
|
7912
|
+
acceptor(context2, item);
|
|
7913
|
+
}, "customAcceptor");
|
|
7914
|
+
return super.completionForCrossReference(context, crossRef, customAcceptor);
|
|
7915
|
+
}
|
|
7916
|
+
completionForKeyword(context, keyword, acceptor) {
|
|
7917
|
+
const customAcceptor = /* @__PURE__ */ __name((context2, item) => {
|
|
7918
|
+
if (!this.filterKeywordForContext(context2, keyword.value)) {
|
|
7919
|
+
return;
|
|
7920
|
+
}
|
|
7921
|
+
acceptor(context2, item);
|
|
7922
|
+
}, "customAcceptor");
|
|
7923
|
+
return super.completionForKeyword(context, keyword, customAcceptor);
|
|
7924
|
+
}
|
|
7925
|
+
filterKeywordForContext(context, keyword) {
|
|
7926
|
+
if (isInvocationExpr(context.node)) {
|
|
7927
|
+
return [
|
|
7928
|
+
"true",
|
|
7929
|
+
"false",
|
|
7930
|
+
"null",
|
|
7931
|
+
"this"
|
|
7932
|
+
].includes(keyword);
|
|
7933
|
+
} else if (isDataModelAttribute(context.node) || isDataFieldAttribute(context.node)) {
|
|
7934
|
+
const exprContext = this.getAttributeContextType(context.node);
|
|
7935
|
+
if (exprContext === "DefaultValue") {
|
|
7936
|
+
return [
|
|
7937
|
+
"true",
|
|
7938
|
+
"false",
|
|
7939
|
+
"null"
|
|
7940
|
+
].includes(keyword);
|
|
7941
|
+
} else {
|
|
7942
|
+
return [
|
|
7943
|
+
"true",
|
|
7944
|
+
"false",
|
|
7945
|
+
"null",
|
|
7946
|
+
"this"
|
|
7947
|
+
].includes(keyword);
|
|
7948
|
+
}
|
|
7949
|
+
} else {
|
|
7950
|
+
return true;
|
|
7951
|
+
}
|
|
7952
|
+
}
|
|
7953
|
+
filterAttributeApplicationCompletion(contextNode, node) {
|
|
7954
|
+
const attrContextType = this.getAttributeContextType(contextNode);
|
|
7955
|
+
if (isFunctionDecl(node) && attrContextType) {
|
|
7956
|
+
const funcExprContextAttr = getAttribute(node, "@@@expressionContext");
|
|
7957
|
+
if (funcExprContextAttr && funcExprContextAttr.args[0]) {
|
|
7958
|
+
const arg = funcExprContextAttr.args[0];
|
|
7959
|
+
if (isArrayExpr(arg.value)) {
|
|
7960
|
+
return arg.value.items.some((item) => isEnumFieldReference(item) && item.target.$refText === attrContextType);
|
|
7961
|
+
}
|
|
7962
|
+
}
|
|
7963
|
+
return false;
|
|
7964
|
+
}
|
|
7965
|
+
if (isDataField(node)) {
|
|
7966
|
+
return attrContextType !== "DefaultValue";
|
|
7967
|
+
}
|
|
7968
|
+
return true;
|
|
7969
|
+
}
|
|
7970
|
+
getAttributeContextType(node) {
|
|
7971
|
+
return match3(node.decl.$refText).with("@default", () => "DefaultValue").with(P2.union("@@allow", "@allow", "@@deny", "@deny"), () => "AccessPolicy").with("@@validate", () => "ValidationRule").otherwise(() => void 0);
|
|
7972
|
+
}
|
|
7973
|
+
createReferenceCompletionItem(nodeDescription) {
|
|
7974
|
+
const node = this.getAstNode(nodeDescription);
|
|
7975
|
+
const documentation = this.getNodeDocumentation(node);
|
|
7976
|
+
return match3(node).when(isDataModel, () => ({
|
|
7977
|
+
nodeDescription,
|
|
7978
|
+
kind: CompletionItemKind.Class,
|
|
7979
|
+
detail: "Model",
|
|
7980
|
+
sortText: "1",
|
|
7981
|
+
documentation
|
|
7982
|
+
})).when(isTypeDef, () => ({
|
|
7983
|
+
nodeDescription,
|
|
7984
|
+
kind: CompletionItemKind.Class,
|
|
7985
|
+
detail: "Type",
|
|
7986
|
+
sortText: "1",
|
|
7987
|
+
documentation
|
|
7988
|
+
})).when(isDataField, () => ({
|
|
7989
|
+
nodeDescription,
|
|
7990
|
+
kind: CompletionItemKind.Field,
|
|
7991
|
+
detail: "Field",
|
|
7992
|
+
sortText: "0",
|
|
7993
|
+
documentation
|
|
7994
|
+
})).when(isEnum, () => ({
|
|
7995
|
+
nodeDescription,
|
|
7996
|
+
kind: CompletionItemKind.Class,
|
|
7997
|
+
detail: "Enum",
|
|
7998
|
+
sortText: "1",
|
|
7999
|
+
documentation
|
|
8000
|
+
})).when(isEnumField, (d) => {
|
|
8001
|
+
const container = d.$container;
|
|
8002
|
+
return {
|
|
8003
|
+
nodeDescription,
|
|
8004
|
+
kind: CompletionItemKind.Enum,
|
|
8005
|
+
detail: `Value of enum "${container.name}"`,
|
|
8006
|
+
sortText: "1",
|
|
8007
|
+
documentation
|
|
8008
|
+
};
|
|
8009
|
+
}).when(isFunctionDecl, () => ({
|
|
8010
|
+
nodeDescription,
|
|
8011
|
+
insertText: this.getFunctionInsertText(nodeDescription),
|
|
8012
|
+
kind: CompletionItemKind.Function,
|
|
8013
|
+
detail: "Function",
|
|
8014
|
+
sortText: "1",
|
|
8015
|
+
documentation
|
|
8016
|
+
})).when(isAttribute, () => ({
|
|
8017
|
+
nodeDescription,
|
|
8018
|
+
insertText: this.getAttributeInsertText(nodeDescription),
|
|
8019
|
+
kind: CompletionItemKind.Property,
|
|
8020
|
+
detail: "Attribute",
|
|
8021
|
+
sortText: "1",
|
|
8022
|
+
documentation
|
|
8023
|
+
})).otherwise(() => ({
|
|
8024
|
+
nodeDescription,
|
|
8025
|
+
kind: CompletionItemKind.Reference,
|
|
8026
|
+
detail: nodeDescription.type,
|
|
8027
|
+
sortText: "2",
|
|
8028
|
+
documentation
|
|
8029
|
+
}));
|
|
8030
|
+
}
|
|
8031
|
+
getFunctionInsertText(nodeDescription) {
|
|
8032
|
+
const node = this.getAstNode(nodeDescription);
|
|
8033
|
+
if (isFunctionDecl(node)) {
|
|
8034
|
+
if (node.params.some((p) => !p.optional)) {
|
|
8035
|
+
return nodeDescription.name;
|
|
8036
|
+
}
|
|
8037
|
+
}
|
|
8038
|
+
return `${nodeDescription.name}()`;
|
|
8039
|
+
}
|
|
8040
|
+
getAttributeInsertText(nodeDescription) {
|
|
8041
|
+
const node = this.getAstNode(nodeDescription);
|
|
8042
|
+
if (isAttribute(node)) {
|
|
8043
|
+
if (node.name === "@relation") {
|
|
8044
|
+
return `${nodeDescription.name}(fields: [], references: [])`;
|
|
8045
|
+
}
|
|
8046
|
+
}
|
|
8047
|
+
return nodeDescription.name;
|
|
8048
|
+
}
|
|
8049
|
+
getAstNode(nodeDescription) {
|
|
8050
|
+
let node = nodeDescription.node;
|
|
8051
|
+
if (!node) {
|
|
8052
|
+
const doc = this.getOrCreateDocumentSync(nodeDescription);
|
|
8053
|
+
if (!doc) {
|
|
8054
|
+
return void 0;
|
|
8055
|
+
}
|
|
8056
|
+
node = this.services.workspace.AstNodeLocator.getAstNode(doc.parseResult.value, nodeDescription.path);
|
|
8057
|
+
if (!node) {
|
|
8058
|
+
return void 0;
|
|
8059
|
+
}
|
|
8060
|
+
}
|
|
8061
|
+
return node;
|
|
8062
|
+
}
|
|
8063
|
+
getOrCreateDocumentSync(nodeDescription) {
|
|
8064
|
+
let doc = this.services.shared.workspace.LangiumDocuments.getDocument(nodeDescription.documentUri);
|
|
8065
|
+
if (!doc) {
|
|
8066
|
+
try {
|
|
8067
|
+
const content = fs2.readFileSync(nodeDescription.documentUri.fsPath, "utf-8");
|
|
8068
|
+
doc = this.services.shared.workspace.LangiumDocuments.createDocument(nodeDescription.documentUri, content);
|
|
8069
|
+
} catch {
|
|
8070
|
+
console.warn("Failed to read or create document:", nodeDescription.documentUri);
|
|
8071
|
+
return void 0;
|
|
8072
|
+
}
|
|
8073
|
+
}
|
|
8074
|
+
return doc;
|
|
8075
|
+
}
|
|
8076
|
+
getNodeDocumentation(node) {
|
|
8077
|
+
if (!node) {
|
|
8078
|
+
return void 0;
|
|
8079
|
+
}
|
|
8080
|
+
const md = this.commentsToMarkdown(node);
|
|
8081
|
+
return {
|
|
8082
|
+
kind: "markdown",
|
|
8083
|
+
value: md
|
|
8084
|
+
};
|
|
8085
|
+
}
|
|
8086
|
+
commentsToMarkdown(node) {
|
|
8087
|
+
const md = this.services.documentation.DocumentationProvider.getDocumentation(node) ?? "";
|
|
8088
|
+
const zModelGenerator = new ZModelCodeGenerator();
|
|
8089
|
+
const docs = [];
|
|
8090
|
+
try {
|
|
8091
|
+
match3(node).when(isAttribute, (attr) => {
|
|
8092
|
+
docs.push("```prisma", zModelGenerator.generate(attr), "```");
|
|
8093
|
+
}).when(isFunctionDecl, (func2) => {
|
|
8094
|
+
docs.push("```ts", zModelGenerator.generate(func2), "```");
|
|
8095
|
+
}).when(isDataModel, (model) => {
|
|
8096
|
+
docs.push("```prisma", `model ${model.name} { ... }`, "```");
|
|
8097
|
+
}).when(isEnum, (enumDecl) => {
|
|
8098
|
+
docs.push("```prisma", zModelGenerator.generate(enumDecl), "```");
|
|
8099
|
+
}).when(isDataField, (field) => {
|
|
8100
|
+
docs.push(`${field.name}: ${field.type.type ?? field.type.reference?.$refText}`);
|
|
8101
|
+
}).otherwise((ast) => {
|
|
8102
|
+
const name = ast.name;
|
|
8103
|
+
if (name) {
|
|
8104
|
+
docs.push(name);
|
|
8105
|
+
}
|
|
8106
|
+
});
|
|
8107
|
+
} catch {
|
|
8108
|
+
}
|
|
8109
|
+
if (md) {
|
|
8110
|
+
docs.push("___", md);
|
|
8111
|
+
}
|
|
8112
|
+
return docs.join("\n");
|
|
8113
|
+
}
|
|
8114
|
+
};
|
|
8115
|
+
|
|
8116
|
+
// src/zmodel-definition.ts
|
|
8117
|
+
import { DefaultDefinitionProvider } from "langium/lsp";
|
|
8118
|
+
import { LocationLink, Range } from "vscode-languageserver";
|
|
8119
|
+
var ZModelDefinitionProvider = class extends DefaultDefinitionProvider {
|
|
8120
|
+
static {
|
|
8121
|
+
__name(this, "ZModelDefinitionProvider");
|
|
8122
|
+
}
|
|
8123
|
+
documents;
|
|
8124
|
+
constructor(services) {
|
|
8125
|
+
super(services);
|
|
8126
|
+
this.documents = services.shared.workspace.LangiumDocuments;
|
|
8127
|
+
}
|
|
8128
|
+
collectLocationLinks(sourceCstNode, _params) {
|
|
8129
|
+
if (isModelImport(sourceCstNode.astNode)) {
|
|
8130
|
+
const importedModel = resolveImport(this.documents, sourceCstNode.astNode);
|
|
8131
|
+
if (importedModel?.$document) {
|
|
8132
|
+
const targetObject = importedModel;
|
|
8133
|
+
const selectionRange = this.nameProvider.getNameNode(targetObject)?.range ?? Range.create(0, 0, 0, 0);
|
|
8134
|
+
const previewRange = targetObject.$cstNode?.range ?? Range.create(0, 0, 0, 0);
|
|
8135
|
+
return [
|
|
8136
|
+
LocationLink.create(importedModel.$document.uri.toString(), previewRange, selectionRange, sourceCstNode.range)
|
|
8137
|
+
];
|
|
8138
|
+
}
|
|
8139
|
+
return void 0;
|
|
8140
|
+
}
|
|
8141
|
+
return super.collectLocationLinks(sourceCstNode, _params);
|
|
8142
|
+
}
|
|
8143
|
+
};
|
|
8144
|
+
|
|
8145
|
+
// src/zmodel-document-builder.ts
|
|
8146
|
+
import { DefaultDocumentBuilder } from "langium";
|
|
8147
|
+
var ZModelDocumentBuilder = class extends DefaultDocumentBuilder {
|
|
8148
|
+
static {
|
|
8149
|
+
__name(this, "ZModelDocumentBuilder");
|
|
8150
|
+
}
|
|
8151
|
+
constructor(services) {
|
|
8152
|
+
super(services);
|
|
8153
|
+
let validationOptions = this.updateBuildOptions.validation;
|
|
8154
|
+
const stopFlags = {
|
|
8155
|
+
stopAfterLinkingErrors: true,
|
|
8156
|
+
stopAfterLexingErrors: true,
|
|
8157
|
+
stopAfterParsingErrors: true
|
|
8158
|
+
};
|
|
8159
|
+
if (validationOptions === true) {
|
|
8160
|
+
validationOptions = stopFlags;
|
|
8161
|
+
} else if (typeof validationOptions === "object") {
|
|
8162
|
+
validationOptions = {
|
|
8163
|
+
...validationOptions,
|
|
8164
|
+
...stopFlags
|
|
8165
|
+
};
|
|
8166
|
+
}
|
|
8167
|
+
this.updateBuildOptions = {
|
|
8168
|
+
...this.updateBuildOptions,
|
|
8169
|
+
validation: validationOptions
|
|
8170
|
+
};
|
|
8171
|
+
}
|
|
8172
|
+
};
|
|
8173
|
+
|
|
8174
|
+
// src/zmodel-documentation-provider.ts
|
|
8175
|
+
import { JSDocDocumentationProvider } from "langium";
|
|
8176
|
+
var ZModelDocumentationProvider = class extends JSDocDocumentationProvider {
|
|
8177
|
+
static {
|
|
8178
|
+
__name(this, "ZModelDocumentationProvider");
|
|
8179
|
+
}
|
|
8180
|
+
getDocumentation(node) {
|
|
8181
|
+
if ("comments" in node && Array.isArray(node.comments) && node.comments.length > 0) {
|
|
8182
|
+
return node.comments.map((c) => c.replace(/^[/]*\s*/, "")).join("\n");
|
|
8183
|
+
}
|
|
8184
|
+
return super.getDocumentation(node);
|
|
8185
|
+
}
|
|
8186
|
+
};
|
|
8187
|
+
|
|
8188
|
+
// src/zmodel-formatter.ts
|
|
8189
|
+
import { AbstractFormatter, Formatting } from "langium/lsp";
|
|
8190
|
+
var ZModelFormatter = class extends AbstractFormatter {
|
|
8191
|
+
static {
|
|
8192
|
+
__name(this, "ZModelFormatter");
|
|
8193
|
+
}
|
|
8194
|
+
formatOptions;
|
|
8195
|
+
configurationProvider;
|
|
8196
|
+
constructor(services) {
|
|
8197
|
+
super();
|
|
8198
|
+
this.configurationProvider = services.shared.workspace.ConfigurationProvider;
|
|
8199
|
+
}
|
|
8200
|
+
format(node) {
|
|
8201
|
+
const formatter = this.getNodeFormatter(node);
|
|
8202
|
+
if (isDataField(node)) {
|
|
8203
|
+
if (isDataModel(node.$container) || isTypeDef(node.$container)) {
|
|
8204
|
+
const dataModel = node.$container;
|
|
8205
|
+
const compareFn = /* @__PURE__ */ __name((a, b) => b - a, "compareFn");
|
|
8206
|
+
const maxNameLength = dataModel.fields.map((x) => x.name.length).sort(compareFn)[0] ?? 0;
|
|
8207
|
+
const maxTypeLength = dataModel.fields.map(this.getFieldTypeLength).sort(compareFn)[0] ?? 0;
|
|
8208
|
+
formatter.property("type").prepend(Formatting.spaces(maxNameLength - node.name.length + 1));
|
|
8209
|
+
if (node.attributes.length > 0) {
|
|
8210
|
+
formatter.node(node.attributes[0]).prepend(Formatting.spaces(maxTypeLength - this.getFieldTypeLength(node) + 1));
|
|
8211
|
+
formatter.nodes(...node.attributes.slice(1)).prepend(Formatting.oneSpace());
|
|
8212
|
+
}
|
|
8213
|
+
} else {
|
|
8214
|
+
formatter.property("type").prepend(Formatting.oneSpace());
|
|
8215
|
+
if (node.attributes.length > 0) {
|
|
8216
|
+
formatter.properties("attributes").prepend(Formatting.oneSpace());
|
|
8217
|
+
}
|
|
8218
|
+
}
|
|
8219
|
+
} else if (isDataFieldAttribute(node)) {
|
|
8220
|
+
formatter.keyword("(").surround(Formatting.noSpace());
|
|
8221
|
+
formatter.keyword(")").prepend(Formatting.noSpace());
|
|
8222
|
+
formatter.keyword(",").append(Formatting.oneSpace());
|
|
8223
|
+
if (node.args.length > 1) {
|
|
8224
|
+
formatter.nodes(...node.args.slice(1)).prepend(Formatting.oneSpace());
|
|
8225
|
+
}
|
|
8226
|
+
} else if (isAttributeArg(node)) {
|
|
8227
|
+
formatter.keyword(":").prepend(Formatting.noSpace());
|
|
8228
|
+
formatter.keyword(":").append(Formatting.oneSpace());
|
|
8229
|
+
} else if (isAbstractDeclaration(node)) {
|
|
8230
|
+
const bracesOpen = formatter.keyword("{");
|
|
8231
|
+
const bracesClose = formatter.keyword("}");
|
|
8232
|
+
formatter.interior(bracesOpen, bracesClose).prepend(Formatting.indent({
|
|
8233
|
+
allowMore: true
|
|
8234
|
+
}));
|
|
8235
|
+
bracesOpen.prepend(Formatting.oneSpace());
|
|
8236
|
+
bracesClose.prepend(Formatting.newLine());
|
|
8237
|
+
} else if (isModel(node)) {
|
|
8238
|
+
const model = node;
|
|
8239
|
+
const nodes = formatter.nodes(...model.declarations);
|
|
8240
|
+
nodes.prepend(Formatting.noIndent());
|
|
8241
|
+
}
|
|
8242
|
+
}
|
|
8243
|
+
formatDocument(document, params) {
|
|
8244
|
+
this.formatOptions = params.options;
|
|
8245
|
+
this.configurationProvider.getConfiguration(ZModelLanguageMetaData.languageId, "format").then((config) => {
|
|
8246
|
+
if (config) {
|
|
8247
|
+
}
|
|
8248
|
+
});
|
|
8249
|
+
return super.formatDocument(document, params);
|
|
8250
|
+
}
|
|
8251
|
+
getFormatOptions() {
|
|
8252
|
+
return this.formatOptions;
|
|
8253
|
+
}
|
|
8254
|
+
getIndent() {
|
|
8255
|
+
return 1;
|
|
8256
|
+
}
|
|
8257
|
+
getFieldTypeLength(field) {
|
|
8258
|
+
let length;
|
|
8259
|
+
if (field.type.type) {
|
|
8260
|
+
length = field.type.type.length;
|
|
8261
|
+
} else if (field.type.reference) {
|
|
8262
|
+
length = field.type.reference.$refText.length;
|
|
8263
|
+
} else if (isDataField(field) && field.type.unsupported) {
|
|
8264
|
+
const name = `Unsupported("${field.type.unsupported.value.value}")`;
|
|
8265
|
+
length = name.length;
|
|
8266
|
+
} else {
|
|
8267
|
+
length = 1;
|
|
8268
|
+
}
|
|
8269
|
+
if (field.type.optional) {
|
|
8270
|
+
length += 1;
|
|
8271
|
+
}
|
|
8272
|
+
if (field.type.array) {
|
|
8273
|
+
length += 2;
|
|
8274
|
+
}
|
|
8275
|
+
return length;
|
|
8276
|
+
}
|
|
8277
|
+
};
|
|
8278
|
+
|
|
8279
|
+
// src/zmodel-linker.ts
|
|
8280
|
+
import { AstUtils as AstUtils6, Cancellation, DefaultLinker, DocumentState, interruptAndCheck } from "langium";
|
|
8281
|
+
import { match as match4 } from "ts-pattern";
|
|
6988
8282
|
var ZModelLinker = class extends DefaultLinker {
|
|
6989
8283
|
static {
|
|
6990
8284
|
__name(this, "ZModelLinker");
|
|
@@ -7005,21 +8299,19 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7005
8299
|
}
|
|
7006
8300
|
document.state = DocumentState.Linked;
|
|
7007
8301
|
}
|
|
7008
|
-
linkReference(
|
|
7009
|
-
|
|
8302
|
+
linkReference(refInfo, document, extraScopes) {
|
|
8303
|
+
const defaultRef = refInfo.reference;
|
|
8304
|
+
if (defaultRef._ref) {
|
|
8305
|
+
return;
|
|
8306
|
+
}
|
|
8307
|
+
if (this.resolveFromScopeProviders(refInfo.reference, document, extraScopes)) {
|
|
7010
8308
|
return;
|
|
7011
8309
|
}
|
|
7012
|
-
|
|
7013
|
-
this.doLink({
|
|
7014
|
-
reference,
|
|
7015
|
-
container,
|
|
7016
|
-
property
|
|
7017
|
-
}, document);
|
|
8310
|
+
this.doLink(refInfo, document);
|
|
7018
8311
|
}
|
|
7019
8312
|
//#endregion
|
|
7020
8313
|
//#region Expression type resolving
|
|
7021
|
-
resolveFromScopeProviders(
|
|
7022
|
-
const reference = node[property];
|
|
8314
|
+
resolveFromScopeProviders(reference, document, providers) {
|
|
7023
8315
|
for (const provider of providers) {
|
|
7024
8316
|
const target = provider(reference.$refText);
|
|
7025
8317
|
if (target) {
|
|
@@ -7148,7 +8440,11 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7148
8440
|
}
|
|
7149
8441
|
}
|
|
7150
8442
|
resolveInvocation(node, document, extraScopes) {
|
|
7151
|
-
this.linkReference(
|
|
8443
|
+
this.linkReference({
|
|
8444
|
+
reference: node.function,
|
|
8445
|
+
container: node,
|
|
8446
|
+
property: "function"
|
|
8447
|
+
}, document, extraScopes);
|
|
7152
8448
|
node.args.forEach((arg) => this.resolve(arg, document, extraScopes));
|
|
7153
8449
|
if (node.function.ref) {
|
|
7154
8450
|
const funcDecl = node.function.ref;
|
|
@@ -7161,7 +8457,7 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7161
8457
|
nullable: true
|
|
7162
8458
|
};
|
|
7163
8459
|
}
|
|
7164
|
-
} else if (
|
|
8460
|
+
} else if (isBeforeInvocation(node)) {
|
|
7165
8461
|
node.$resolvedType = {
|
|
7166
8462
|
decl: getContainingDataModel(node)
|
|
7167
8463
|
};
|
|
@@ -7171,7 +8467,7 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7171
8467
|
}
|
|
7172
8468
|
}
|
|
7173
8469
|
resolveLiteral(node) {
|
|
7174
|
-
const type =
|
|
8470
|
+
const type = match4(node).when(isStringLiteral, () => "String").when(isBooleanLiteral, () => "Boolean").when(isNumberLiteral, () => "Int").exhaustive();
|
|
7175
8471
|
if (type) {
|
|
7176
8472
|
this.resolveToBuiltinTypeOrDecl(node, type);
|
|
7177
8473
|
}
|
|
@@ -7225,7 +8521,7 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7225
8521
|
if (isArrayExpr(node.value)) {
|
|
7226
8522
|
node.value.items.forEach((item) => {
|
|
7227
8523
|
if (isReferenceExpr(item)) {
|
|
7228
|
-
const resolved2 = this.resolveFromScopeProviders(item
|
|
8524
|
+
const resolved2 = this.resolveFromScopeProviders(item.target, document, [
|
|
7229
8525
|
scopeProvider
|
|
7230
8526
|
]);
|
|
7231
8527
|
if (resolved2) {
|
|
@@ -7239,7 +8535,7 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7239
8535
|
this.resolveToBuiltinTypeOrDecl(node.value, node.value.items[0].$resolvedType.decl, true);
|
|
7240
8536
|
}
|
|
7241
8537
|
} else if (isReferenceExpr(node.value)) {
|
|
7242
|
-
const resolved2 = this.resolveFromScopeProviders(node.value
|
|
8538
|
+
const resolved2 = this.resolveFromScopeProviders(node.value.target, document, [
|
|
7243
8539
|
scopeProvider
|
|
7244
8540
|
]);
|
|
7245
8541
|
if (resolved2) {
|
|
@@ -7291,13 +8587,9 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7291
8587
|
this.resolveDefault(node, document, scopes);
|
|
7292
8588
|
}
|
|
7293
8589
|
resolveDefault(node, document, extraScopes) {
|
|
7294
|
-
|
|
7295
|
-
|
|
7296
|
-
|
|
7297
|
-
this.linkReference(node, property, document, extraScopes);
|
|
7298
|
-
}
|
|
7299
|
-
}
|
|
7300
|
-
}
|
|
8590
|
+
AstUtils6.streamReferences(node).forEach((ref) => {
|
|
8591
|
+
this.linkReference(ref, document, extraScopes);
|
|
8592
|
+
});
|
|
7301
8593
|
for (const child of AstUtils6.streamContents(node)) {
|
|
7302
8594
|
this.resolve(child, document, extraScopes);
|
|
7303
8595
|
}
|
|
@@ -7343,7 +8635,7 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7343
8635
|
|
|
7344
8636
|
// src/zmodel-scope.ts
|
|
7345
8637
|
import { AstUtils as AstUtils7, DefaultScopeComputation, DefaultScopeProvider, EMPTY_SCOPE, StreamScope, UriUtils, interruptAndCheck as interruptAndCheck2 } from "langium";
|
|
7346
|
-
import { match as
|
|
8638
|
+
import { match as match5 } from "ts-pattern";
|
|
7347
8639
|
var ZModelScopeComputation = class extends DefaultScopeComputation {
|
|
7348
8640
|
static {
|
|
7349
8641
|
__name(this, "ZModelScopeComputation");
|
|
@@ -7367,7 +8659,7 @@ var ZModelScopeComputation = class extends DefaultScopeComputation {
|
|
|
7367
8659
|
}
|
|
7368
8660
|
processNode(node, document, scopes) {
|
|
7369
8661
|
super.processNode(node, document, scopes);
|
|
7370
|
-
if (isDataModel(node)) {
|
|
8662
|
+
if (isDataModel(node) || isTypeDef(node)) {
|
|
7371
8663
|
const bases = getRecursiveBases(node);
|
|
7372
8664
|
for (const base of bases) {
|
|
7373
8665
|
for (const field of base.fields) {
|
|
@@ -7420,7 +8712,7 @@ var ZModelScopeProvider = class extends DefaultScopeProvider {
|
|
|
7420
8712
|
// isAuthOrAuthMemberAccess(node.operand) ||
|
|
7421
8713
|
!!AstUtils7.getContainerOfType(node, isTypeDef)
|
|
7422
8714
|
);
|
|
7423
|
-
return
|
|
8715
|
+
return match5(node.operand).when(isReferenceExpr, (operand) => {
|
|
7424
8716
|
const ref = operand.target.ref;
|
|
7425
8717
|
if (isDataField(ref)) {
|
|
7426
8718
|
return this.createScopeForContainer(ref.type.reference?.ref, globalScope, allowTypeDefScope);
|
|
@@ -7438,7 +8730,7 @@ var ZModelScopeProvider = class extends DefaultScopeProvider {
|
|
|
7438
8730
|
if (isAuthInvocation(operand)) {
|
|
7439
8731
|
return this.createScopeForAuth(node, globalScope);
|
|
7440
8732
|
}
|
|
7441
|
-
if (
|
|
8733
|
+
if (isBeforeInvocation(operand)) {
|
|
7442
8734
|
return this.createScopeForContainingModel(node, globalScope);
|
|
7443
8735
|
}
|
|
7444
8736
|
return EMPTY_SCOPE;
|
|
@@ -7449,7 +8741,7 @@ var ZModelScopeProvider = class extends DefaultScopeProvider {
|
|
|
7449
8741
|
const globalScope = this.getGlobalScope(referenceType, context);
|
|
7450
8742
|
const collection = collectionPredicate.left;
|
|
7451
8743
|
const allowTypeDefScope = false;
|
|
7452
|
-
return
|
|
8744
|
+
return match5(collection).when(isReferenceExpr, (expr) => {
|
|
7453
8745
|
const ref = expr.target.ref;
|
|
7454
8746
|
if (isDataField(ref)) {
|
|
7455
8747
|
return this.createScopeForContainer(ref.type.reference?.ref, globalScope, allowTypeDefScope);
|
|
@@ -7511,11 +8803,95 @@ function getCollectionPredicateContext(node) {
|
|
|
7511
8803
|
}
|
|
7512
8804
|
__name(getCollectionPredicateContext, "getCollectionPredicateContext");
|
|
7513
8805
|
|
|
8806
|
+
// src/zmodel-semantic.ts
|
|
8807
|
+
import { AbstractSemanticTokenProvider } from "langium/lsp";
|
|
8808
|
+
import { SemanticTokenTypes } from "vscode-languageserver";
|
|
8809
|
+
var ZModelSemanticTokenProvider = class extends AbstractSemanticTokenProvider {
|
|
8810
|
+
static {
|
|
8811
|
+
__name(this, "ZModelSemanticTokenProvider");
|
|
8812
|
+
}
|
|
8813
|
+
highlightElement(node, acceptor) {
|
|
8814
|
+
if (isDataModel(node)) {
|
|
8815
|
+
acceptor({
|
|
8816
|
+
node,
|
|
8817
|
+
property: "name",
|
|
8818
|
+
type: SemanticTokenTypes.type
|
|
8819
|
+
});
|
|
8820
|
+
acceptor({
|
|
8821
|
+
node,
|
|
8822
|
+
property: "mixins",
|
|
8823
|
+
type: SemanticTokenTypes.type
|
|
8824
|
+
});
|
|
8825
|
+
acceptor({
|
|
8826
|
+
node,
|
|
8827
|
+
property: "baseModel",
|
|
8828
|
+
type: SemanticTokenTypes.type
|
|
8829
|
+
});
|
|
8830
|
+
} else if (isDataSource(node) || isGeneratorDecl(node) || isPlugin(node) || isEnum(node) || isTypeDef(node)) {
|
|
8831
|
+
acceptor({
|
|
8832
|
+
node,
|
|
8833
|
+
property: "name",
|
|
8834
|
+
type: SemanticTokenTypes.type
|
|
8835
|
+
});
|
|
8836
|
+
} else if (isDataField(node) || isConfigField(node) || isAttributeArg(node) || isPluginField(node) || isEnumField(node)) {
|
|
8837
|
+
acceptor({
|
|
8838
|
+
node,
|
|
8839
|
+
property: "name",
|
|
8840
|
+
type: SemanticTokenTypes.variable
|
|
8841
|
+
});
|
|
8842
|
+
} else if (isDataFieldType(node)) {
|
|
8843
|
+
if (node.type) {
|
|
8844
|
+
acceptor({
|
|
8845
|
+
node,
|
|
8846
|
+
property: "type",
|
|
8847
|
+
type: SemanticTokenTypes.type
|
|
8848
|
+
});
|
|
8849
|
+
} else {
|
|
8850
|
+
acceptor({
|
|
8851
|
+
node,
|
|
8852
|
+
property: "reference",
|
|
8853
|
+
type: SemanticTokenTypes.macro
|
|
8854
|
+
});
|
|
8855
|
+
}
|
|
8856
|
+
} else if (isDataModelAttribute(node) || isDataFieldAttribute(node) || isInternalAttribute(node)) {
|
|
8857
|
+
acceptor({
|
|
8858
|
+
node,
|
|
8859
|
+
property: "decl",
|
|
8860
|
+
type: SemanticTokenTypes.function
|
|
8861
|
+
});
|
|
8862
|
+
} else if (isInvocationExpr(node)) {
|
|
8863
|
+
acceptor({
|
|
8864
|
+
node,
|
|
8865
|
+
property: "function",
|
|
8866
|
+
type: SemanticTokenTypes.function
|
|
8867
|
+
});
|
|
8868
|
+
} else if (isFunctionDecl(node) || isAttribute(node)) {
|
|
8869
|
+
acceptor({
|
|
8870
|
+
node,
|
|
8871
|
+
property: "name",
|
|
8872
|
+
type: SemanticTokenTypes.function
|
|
8873
|
+
});
|
|
8874
|
+
} else if (isReferenceExpr(node)) {
|
|
8875
|
+
acceptor({
|
|
8876
|
+
node,
|
|
8877
|
+
property: "target",
|
|
8878
|
+
type: SemanticTokenTypes.variable
|
|
8879
|
+
});
|
|
8880
|
+
} else if (isMemberAccessExpr(node)) {
|
|
8881
|
+
acceptor({
|
|
8882
|
+
node,
|
|
8883
|
+
property: "member",
|
|
8884
|
+
type: SemanticTokenTypes.property
|
|
8885
|
+
});
|
|
8886
|
+
}
|
|
8887
|
+
}
|
|
8888
|
+
};
|
|
8889
|
+
|
|
7514
8890
|
// src/zmodel-workspace-manager.ts
|
|
7515
|
-
import { DefaultWorkspaceManager, URI as URI2
|
|
7516
|
-
import
|
|
8891
|
+
import { DefaultWorkspaceManager, URI as URI2 } from "langium";
|
|
8892
|
+
import fs3 from "fs";
|
|
7517
8893
|
import path2 from "path";
|
|
7518
|
-
import { fileURLToPath } from "url";
|
|
8894
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
7519
8895
|
var ZModelWorkspaceManager = class extends DefaultWorkspaceManager {
|
|
7520
8896
|
static {
|
|
7521
8897
|
__name(this, "ZModelWorkspaceManager");
|
|
@@ -7539,7 +8915,7 @@ var ZModelWorkspaceManager = class extends DefaultWorkspaceManager {
|
|
|
7539
8915
|
});
|
|
7540
8916
|
const languagePackageDir = path2.dirname(languagePackagePath);
|
|
7541
8917
|
const candidateStdlibPath = path2.join(languagePackageDir, "res", STD_LIB_MODULE_NAME);
|
|
7542
|
-
if (
|
|
8918
|
+
if (fs3.existsSync(candidateStdlibPath)) {
|
|
7543
8919
|
installedStdlibPath = candidateStdlibPath;
|
|
7544
8920
|
console.log(`Found installed zenstack package stdlib at: ${installedStdlibPath}`);
|
|
7545
8921
|
break;
|
|
@@ -7551,73 +8927,12 @@ var ZModelWorkspaceManager = class extends DefaultWorkspaceManager {
|
|
|
7551
8927
|
if (installedStdlibPath) {
|
|
7552
8928
|
stdLibPath = installedStdlibPath;
|
|
7553
8929
|
} else {
|
|
7554
|
-
const _dirname = typeof __dirname !== "undefined" ? __dirname : path2.dirname(
|
|
8930
|
+
const _dirname = typeof __dirname !== "undefined" ? __dirname : path2.dirname(fileURLToPath2(import.meta.url));
|
|
7555
8931
|
stdLibPath = path2.join(_dirname, "../res", STD_LIB_MODULE_NAME);
|
|
7556
8932
|
console.log(`Using bundled stdlib in extension:`, stdLibPath);
|
|
7557
8933
|
}
|
|
7558
8934
|
const stdlib = await this.documentFactory.fromUri(URI2.file(stdLibPath));
|
|
7559
8935
|
collector(stdlib);
|
|
7560
|
-
const documents = this.langiumDocuments.all;
|
|
7561
|
-
const pluginModels = /* @__PURE__ */ new Set();
|
|
7562
|
-
documents.forEach((doc) => {
|
|
7563
|
-
const parsed = doc.parseResult.value;
|
|
7564
|
-
parsed.declarations.forEach((decl) => {
|
|
7565
|
-
if (isPlugin(decl)) {
|
|
7566
|
-
const providerField = decl.fields.find((f) => f.name === "provider");
|
|
7567
|
-
if (providerField) {
|
|
7568
|
-
const provider = getLiteral(providerField.value);
|
|
7569
|
-
if (provider) {
|
|
7570
|
-
pluginModels.add(provider);
|
|
7571
|
-
}
|
|
7572
|
-
}
|
|
7573
|
-
}
|
|
7574
|
-
});
|
|
7575
|
-
});
|
|
7576
|
-
if (pluginModels.size > 0) {
|
|
7577
|
-
console.log(`Used plugin modules: ${Array.from(pluginModels)}`);
|
|
7578
|
-
const pendingPluginModules = new Set(pluginModels);
|
|
7579
|
-
await Promise.all(folders.map((wf) => [
|
|
7580
|
-
wf,
|
|
7581
|
-
this.getRootFolder(wf)
|
|
7582
|
-
]).map(async (entry) => this.loadPluginModels(...entry, pendingPluginModules, collector)));
|
|
7583
|
-
}
|
|
7584
|
-
}
|
|
7585
|
-
async loadPluginModels(workspaceFolder, folderPath, pendingPluginModels, collector) {
|
|
7586
|
-
const content = (await this.fileSystemProvider.readDirectory(folderPath)).sort((a, b) => {
|
|
7587
|
-
if (a.isDirectory && b.isDirectory) {
|
|
7588
|
-
const aName = UriUtils2.basename(a.uri);
|
|
7589
|
-
if (aName === "node_modules") {
|
|
7590
|
-
return -1;
|
|
7591
|
-
} else {
|
|
7592
|
-
return 1;
|
|
7593
|
-
}
|
|
7594
|
-
} else {
|
|
7595
|
-
return 0;
|
|
7596
|
-
}
|
|
7597
|
-
});
|
|
7598
|
-
for (const entry of content) {
|
|
7599
|
-
if (entry.isDirectory) {
|
|
7600
|
-
const name = UriUtils2.basename(entry.uri);
|
|
7601
|
-
if (name === "node_modules") {
|
|
7602
|
-
for (const plugin of Array.from(pendingPluginModels)) {
|
|
7603
|
-
const path4 = UriUtils2.joinPath(entry.uri, plugin, PLUGIN_MODULE_NAME);
|
|
7604
|
-
try {
|
|
7605
|
-
await this.fileSystemProvider.readFile(path4);
|
|
7606
|
-
const document = await this.langiumDocuments.getOrCreateDocument(path4);
|
|
7607
|
-
collector(document);
|
|
7608
|
-
console.log(`Adding plugin document from ${path4.path}`);
|
|
7609
|
-
pendingPluginModels.delete(plugin);
|
|
7610
|
-
if (pendingPluginModels.size === 0) {
|
|
7611
|
-
return;
|
|
7612
|
-
}
|
|
7613
|
-
} catch {
|
|
7614
|
-
}
|
|
7615
|
-
}
|
|
7616
|
-
} else {
|
|
7617
|
-
await this.loadPluginModels(workspaceFolder, entry.uri, pendingPluginModels, collector);
|
|
7618
|
-
}
|
|
7619
|
-
}
|
|
7620
|
-
}
|
|
7621
8936
|
}
|
|
7622
8937
|
};
|
|
7623
8938
|
|
|
@@ -7630,14 +8945,25 @@ var ZModelLanguageModule = {
|
|
|
7630
8945
|
},
|
|
7631
8946
|
validation: {
|
|
7632
8947
|
ZModelValidator: /* @__PURE__ */ __name((services) => new ZModelValidator(services), "ZModelValidator")
|
|
8948
|
+
},
|
|
8949
|
+
lsp: {
|
|
8950
|
+
Formatter: /* @__PURE__ */ __name((services) => new ZModelFormatter(services), "Formatter"),
|
|
8951
|
+
DefinitionProvider: /* @__PURE__ */ __name((services) => new ZModelDefinitionProvider(services), "DefinitionProvider"),
|
|
8952
|
+
CompletionProvider: /* @__PURE__ */ __name((services) => new ZModelCompletionProvider(services), "CompletionProvider"),
|
|
8953
|
+
SemanticTokenProvider: /* @__PURE__ */ __name((services) => new ZModelSemanticTokenProvider(services), "SemanticTokenProvider")
|
|
8954
|
+
},
|
|
8955
|
+
documentation: {
|
|
8956
|
+
CommentProvider: /* @__PURE__ */ __name((services) => new ZModelCommentProvider(services), "CommentProvider"),
|
|
8957
|
+
DocumentationProvider: /* @__PURE__ */ __name((services) => new ZModelDocumentationProvider(services), "DocumentationProvider")
|
|
7633
8958
|
}
|
|
7634
8959
|
};
|
|
7635
8960
|
var ZModelSharedModule = {
|
|
7636
8961
|
workspace: {
|
|
8962
|
+
DocumentBuilder: /* @__PURE__ */ __name((services) => new ZModelDocumentBuilder(services), "DocumentBuilder"),
|
|
7637
8963
|
WorkspaceManager: /* @__PURE__ */ __name((services) => new ZModelWorkspaceManager(services), "WorkspaceManager")
|
|
7638
8964
|
}
|
|
7639
8965
|
};
|
|
7640
|
-
function createZModelLanguageServices(context) {
|
|
8966
|
+
function createZModelLanguageServices(context, logToConsole = false) {
|
|
7641
8967
|
const shared = inject(createDefaultSharedModule(context), ZModelGeneratedSharedModule, ZModelSharedModule);
|
|
7642
8968
|
const ZModelLanguage = inject(createDefaultModule({
|
|
7643
8969
|
shared
|
|
@@ -7647,30 +8973,47 @@ function createZModelLanguageServices(context) {
|
|
|
7647
8973
|
if (!context.connection) {
|
|
7648
8974
|
shared.workspace.ConfigurationProvider.initialized({});
|
|
7649
8975
|
}
|
|
8976
|
+
shared.workspace.DocumentBuilder.onBuildPhase(DocumentState2.Parsed, async (documents) => {
|
|
8977
|
+
for (const doc of documents) {
|
|
8978
|
+
if (doc.parseResult.lexerErrors.length > 0 || doc.parseResult.parserErrors.length > 0) {
|
|
8979
|
+
continue;
|
|
8980
|
+
}
|
|
8981
|
+
if (doc.uri.scheme !== "file") {
|
|
8982
|
+
continue;
|
|
8983
|
+
}
|
|
8984
|
+
const schemaPath = fileURLToPath3(doc.uri.toString());
|
|
8985
|
+
const pluginSchemas = getPluginDocuments(doc.parseResult.value, schemaPath);
|
|
8986
|
+
for (const plugin of pluginSchemas) {
|
|
8987
|
+
const pluginDocUri = URI3.file(path3.resolve(plugin));
|
|
8988
|
+
let pluginDoc = shared.workspace.LangiumDocuments.getDocument(pluginDocUri);
|
|
8989
|
+
if (!pluginDoc) {
|
|
8990
|
+
pluginDoc = await shared.workspace.LangiumDocuments.getOrCreateDocument(pluginDocUri);
|
|
8991
|
+
if (pluginDoc) {
|
|
8992
|
+
shared.workspace.IndexManager.updateContent(pluginDoc);
|
|
8993
|
+
if (logToConsole) {
|
|
8994
|
+
console.log(`Loaded plugin model: ${plugin}`);
|
|
8995
|
+
}
|
|
8996
|
+
}
|
|
8997
|
+
}
|
|
8998
|
+
}
|
|
8999
|
+
}
|
|
9000
|
+
});
|
|
7650
9001
|
return {
|
|
7651
9002
|
shared,
|
|
7652
9003
|
ZModelLanguage
|
|
7653
9004
|
};
|
|
7654
9005
|
}
|
|
7655
9006
|
__name(createZModelLanguageServices, "createZModelLanguageServices");
|
|
7656
|
-
|
|
7657
|
-
|
|
7658
|
-
function createZModelServices() {
|
|
7659
|
-
return createZModelLanguageServices(NodeFileSystem);
|
|
9007
|
+
function createZModelServices(logToConsole = false) {
|
|
9008
|
+
return createZModelLanguageServices(NodeFileSystem, logToConsole);
|
|
7660
9009
|
}
|
|
7661
9010
|
__name(createZModelServices, "createZModelServices");
|
|
7662
|
-
|
|
7663
|
-
|
|
7664
|
-
|
|
7665
|
-
}
|
|
7666
|
-
constructor(message) {
|
|
7667
|
-
super(message);
|
|
7668
|
-
}
|
|
7669
|
-
};
|
|
7670
|
-
async function loadDocument(fileName, pluginModelFiles = []) {
|
|
7671
|
-
const { ZModelLanguage: services } = createZModelServices();
|
|
9011
|
+
|
|
9012
|
+
// src/document.ts
|
|
9013
|
+
async function loadDocument(fileName, additionalModelFiles = []) {
|
|
9014
|
+
const { ZModelLanguage: services } = createZModelServices(false);
|
|
7672
9015
|
const extensions = services.LanguageMetaData.fileExtensions;
|
|
7673
|
-
if (!extensions.includes(
|
|
9016
|
+
if (!extensions.includes(path4.extname(fileName))) {
|
|
7674
9017
|
return {
|
|
7675
9018
|
success: false,
|
|
7676
9019
|
errors: [
|
|
@@ -7679,7 +9022,7 @@ async function loadDocument(fileName, pluginModelFiles = []) {
|
|
|
7679
9022
|
warnings: []
|
|
7680
9023
|
};
|
|
7681
9024
|
}
|
|
7682
|
-
if (!
|
|
9025
|
+
if (!fs4.existsSync(fileName)) {
|
|
7683
9026
|
return {
|
|
7684
9027
|
success: false,
|
|
7685
9028
|
errors: [
|
|
@@ -7688,23 +9031,27 @@ async function loadDocument(fileName, pluginModelFiles = []) {
|
|
|
7688
9031
|
warnings: []
|
|
7689
9032
|
};
|
|
7690
9033
|
}
|
|
7691
|
-
const _dirname = typeof __dirname !== "undefined" ? __dirname :
|
|
7692
|
-
const stdLib = await services.shared.workspace.LangiumDocuments.getOrCreateDocument(
|
|
7693
|
-
const pluginDocs = await Promise.all(pluginModelFiles.map((file) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI3.file(path3.resolve(file)))));
|
|
9034
|
+
const _dirname = typeof __dirname !== "undefined" ? __dirname : path4.dirname(fileURLToPath4(import.meta.url));
|
|
9035
|
+
const stdLib = await services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI4.file(path4.resolve(path4.join(_dirname, "../res", STD_LIB_MODULE_NAME))));
|
|
7694
9036
|
const langiumDocuments = services.shared.workspace.LangiumDocuments;
|
|
7695
|
-
const document = await langiumDocuments.getOrCreateDocument(
|
|
9037
|
+
const document = await langiumDocuments.getOrCreateDocument(URI4.file(path4.resolve(fileName)));
|
|
7696
9038
|
const importedURIs = await loadImports(document, langiumDocuments);
|
|
7697
9039
|
const importedDocuments = [];
|
|
7698
9040
|
for (const uri of importedURIs) {
|
|
7699
9041
|
importedDocuments.push(await langiumDocuments.getOrCreateDocument(uri));
|
|
7700
9042
|
}
|
|
9043
|
+
const additionalDocs = await Promise.all(additionalModelFiles.map((file) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI4.file(path4.resolve(file)))));
|
|
7701
9044
|
await services.shared.workspace.DocumentBuilder.build([
|
|
7702
9045
|
stdLib,
|
|
7703
|
-
...
|
|
9046
|
+
...additionalDocs,
|
|
7704
9047
|
document,
|
|
7705
9048
|
...importedDocuments
|
|
7706
9049
|
], {
|
|
7707
|
-
validation:
|
|
9050
|
+
validation: {
|
|
9051
|
+
stopAfterLexingErrors: true,
|
|
9052
|
+
stopAfterParsingErrors: true,
|
|
9053
|
+
stopAfterLinkingErrors: true
|
|
9054
|
+
}
|
|
7708
9055
|
});
|
|
7709
9056
|
const diagnostics = langiumDocuments.all.flatMap((doc) => (doc.diagnostics ?? []).map((diag) => ({
|
|
7710
9057
|
doc,
|
|
@@ -7714,7 +9061,7 @@ async function loadDocument(fileName, pluginModelFiles = []) {
|
|
|
7714
9061
|
const warnings = [];
|
|
7715
9062
|
if (diagnostics.length > 0) {
|
|
7716
9063
|
for (const { doc, diag } of diagnostics) {
|
|
7717
|
-
const message = `${
|
|
9064
|
+
const message = `${path4.relative(process.cwd(), doc.uri.fsPath)}:${diag.range.start.line + 1}:${diag.range.start.character + 1} - ${diag.message}`;
|
|
7718
9065
|
if (diag.severity === 1) {
|
|
7719
9066
|
errors.push(message);
|
|
7720
9067
|
} else {
|
|
@@ -7763,7 +9110,7 @@ async function loadImports(document, documents, uris = /* @__PURE__ */ new Set()
|
|
|
7763
9110
|
}
|
|
7764
9111
|
}
|
|
7765
9112
|
}
|
|
7766
|
-
return Array.from(uris).filter((x) => uriString != x).map((e) =>
|
|
9113
|
+
return Array.from(uris).filter((x) => uriString != x).map((e) => URI4.parse(e));
|
|
7767
9114
|
}
|
|
7768
9115
|
__name(loadImports, "loadImports");
|
|
7769
9116
|
function mergeImportsDeclarations(documents, model) {
|
|
@@ -7813,7 +9160,7 @@ function validationAfterImportMerge(model) {
|
|
|
7813
9160
|
}
|
|
7814
9161
|
__name(validationAfterImportMerge, "validationAfterImportMerge");
|
|
7815
9162
|
export {
|
|
7816
|
-
|
|
9163
|
+
ZModelCodeGenerator,
|
|
7817
9164
|
ZModelLanguageMetaData,
|
|
7818
9165
|
ZModelLanguageModule,
|
|
7819
9166
|
ZModelSharedModule,
|