@zenstackhq/language 3.0.0-beta.2 → 3.0.0-beta.20

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/utils.cjs CHANGED
@@ -52,20 +52,20 @@ __export(utils_exports, {
52
52
  getModelIdFields: () => getModelIdFields,
53
53
  getModelUniqueFields: () => getModelUniqueFields,
54
54
  getObjectLiteral: () => getObjectLiteral,
55
+ getPluginDocuments: () => getPluginDocuments,
55
56
  getRecursiveBases: () => getRecursiveBases,
56
57
  getStringLiteral: () => getStringLiteral,
57
58
  getUniqueFields: () => getUniqueFields,
58
59
  hasAttribute: () => hasAttribute,
59
60
  isAuthInvocation: () => isAuthInvocation,
60
61
  isAuthOrAuthMemberAccess: () => isAuthOrAuthMemberAccess,
62
+ isBeforeInvocation: () => isBeforeInvocation,
61
63
  isCheckInvocation: () => isCheckInvocation,
62
64
  isCollectionPredicate: () => isCollectionPredicate,
63
65
  isDataFieldReference: () => isDataFieldReference,
64
66
  isDelegateModel: () => isDelegateModel,
65
67
  isEnumFieldReference: () => isEnumFieldReference,
66
68
  isFromStdlib: () => isFromStdlib,
67
- isFutureExpr: () => isFutureExpr,
68
- isFutureInvocation: () => isFutureInvocation,
69
69
  isMemberContainer: () => isMemberContainer,
70
70
  isRelationshipField: () => isRelationshipField,
71
71
  mapBuiltinTypeToExpressionType: () => mapBuiltinTypeToExpressionType,
@@ -76,13 +76,15 @@ __export(utils_exports, {
76
76
  typeAssignable: () => typeAssignable
77
77
  });
78
78
  module.exports = __toCommonJS(utils_exports);
79
- var import_common_helpers = require("@zenstackhq/common-helpers");
80
79
  var import_langium = require("langium");
81
80
  var import_node_fs = __toESM(require("fs"), 1);
82
- var import_path = __toESM(require("path"), 1);
81
+ var import_node_module = require("module");
82
+ var import_node_path = __toESM(require("path"), 1);
83
+ var import_node_url = require("url");
83
84
 
84
85
  // src/constants.ts
85
86
  var STD_LIB_MODULE_NAME = "stdlib.zmodel";
87
+ var PLUGIN_MODULE_NAME = "plugin.zmodel";
86
88
 
87
89
  // src/generated/ast.ts
88
90
  var langium = __toESM(require("langium"), 1);
@@ -175,6 +177,10 @@ function isObjectExpr(item) {
175
177
  }
176
178
  __name(isObjectExpr, "isObjectExpr");
177
179
  var Plugin = "Plugin";
180
+ function isPlugin(item) {
181
+ return reflection.isInstance(item, Plugin);
182
+ }
183
+ __name(isPlugin, "isPlugin");
178
184
  var PluginField = "PluginField";
179
185
  var Procedure = "Procedure";
180
186
  var ProcedureParam = "ProcedureParam";
@@ -1039,6 +1045,7 @@ var ZModelAstReflection = class extends langium.AbstractAstReflection {
1039
1045
  var reflection = new ZModelAstReflection();
1040
1046
 
1041
1047
  // src/utils.ts
1048
+ var import_meta = {};
1042
1049
  function hasAttribute(decl, name) {
1043
1050
  return !!getAttribute(decl, name);
1044
1051
  }
@@ -1118,10 +1125,6 @@ function isRelationshipField(field) {
1118
1125
  return isDataModel(field.type.reference?.ref);
1119
1126
  }
1120
1127
  __name(isRelationshipField, "isRelationshipField");
1121
- function isFutureExpr(node) {
1122
- return isInvocationExpr(node) && node.function.ref?.name === "future" && isFromStdlib(node.function.ref);
1123
- }
1124
- __name(isFutureExpr, "isFutureExpr");
1125
1128
  function isDelegateModel(node) {
1126
1129
  return isDataModel(node) && hasAttribute(node, "@@delegate");
1127
1130
  }
@@ -1139,8 +1142,14 @@ function getRecursiveBases(decl, includeDelegate = true, seen = /* @__PURE__ */
1139
1142
  return result;
1140
1143
  }
1141
1144
  seen.add(decl);
1142
- decl.mixins.forEach((mixin) => {
1143
- const baseDecl = mixin.ref;
1145
+ const bases = [
1146
+ ...decl.mixins,
1147
+ ...isDataModel(decl) && decl.baseModel ? [
1148
+ decl.baseModel
1149
+ ] : []
1150
+ ];
1151
+ bases.forEach((base) => {
1152
+ const baseDecl = decl.$container.declarations.find((d) => isTypeDef(d) || isDataModel(d) && d.name === base.$refText);
1144
1153
  if (baseDecl) {
1145
1154
  if (!includeDelegate && isDelegateModel(baseDecl)) {
1146
1155
  return;
@@ -1299,7 +1308,7 @@ function getFieldReference(expr) {
1299
1308
  }
1300
1309
  __name(getFieldReference, "getFieldReference");
1301
1310
  function isCheckInvocation(node) {
1302
- return isInvocationExpr(node) && node.function.ref?.name === "check" && isFromStdlib(node.function.ref);
1311
+ return isInvocationExpr(node) && node.function.ref?.name === "check";
1303
1312
  }
1304
1313
  __name(isCheckInvocation, "isCheckInvocation");
1305
1314
  function resolveTransitiveImports(documents, model) {
@@ -1349,9 +1358,9 @@ function resolveImportUri(imp) {
1349
1358
  return void 0;
1350
1359
  }
1351
1360
  const doc = import_langium.AstUtils.getDocument(imp);
1352
- const dir = import_path.default.dirname(doc.uri.fsPath);
1361
+ const dir = import_node_path.default.dirname(doc.uri.fsPath);
1353
1362
  const importPath = imp.path.endsWith(".zmodel") ? imp.path : `${imp.path}.zmodel`;
1354
- return import_langium.URI.file(import_path.default.resolve(dir, importPath));
1363
+ return import_langium.URI.file(import_node_path.default.resolve(dir, importPath));
1355
1364
  }
1356
1365
  __name(resolveImportUri, "resolveImportUri");
1357
1366
  function getDataModelAndTypeDefs(model, includeIgnored = false) {
@@ -1369,17 +1378,17 @@ function getAllDeclarationsIncludingImports(documents, model) {
1369
1378
  }
1370
1379
  __name(getAllDeclarationsIncludingImports, "getAllDeclarationsIncludingImports");
1371
1380
  function getAuthDecl(decls) {
1372
- let authModel = decls.find((m) => hasAttribute(m, "@@auth"));
1381
+ let authModel = decls.find((d) => hasAttribute(d, "@@auth"));
1373
1382
  if (!authModel) {
1374
- authModel = decls.find((m) => m.name === "User");
1383
+ authModel = decls.find((d) => d.name === "User");
1375
1384
  }
1376
1385
  return authModel;
1377
1386
  }
1378
1387
  __name(getAuthDecl, "getAuthDecl");
1379
- function isFutureInvocation(node) {
1380
- return isInvocationExpr(node) && node.function.ref?.name === "future" && isFromStdlib(node.function.ref);
1388
+ function isBeforeInvocation(node) {
1389
+ return isInvocationExpr(node) && node.function.ref?.name === "before";
1381
1390
  }
1382
- __name(isFutureInvocation, "isFutureInvocation");
1391
+ __name(isBeforeInvocation, "isBeforeInvocation");
1383
1392
  function isCollectionPredicate(node) {
1384
1393
  return isBinaryExpr(node) && [
1385
1394
  "?",
@@ -1434,12 +1443,14 @@ function getAllFields(decl, includeIgnored = false, seen = /* @__PURE__ */ new S
1434
1443
  seen.add(decl);
1435
1444
  const fields = [];
1436
1445
  for (const mixin of decl.mixins) {
1437
- (0, import_common_helpers.invariant)(mixin.ref, `Mixin ${mixin.$refText} is not resolved`);
1438
- fields.push(...getAllFields(mixin.ref, includeIgnored, seen));
1446
+ if (mixin.ref) {
1447
+ fields.push(...getAllFields(mixin.ref, includeIgnored, seen));
1448
+ }
1439
1449
  }
1440
1450
  if (isDataModel(decl) && decl.baseModel) {
1441
- (0, import_common_helpers.invariant)(decl.baseModel.ref, `Base model ${decl.baseModel.$refText} is not resolved`);
1442
- fields.push(...getAllFields(decl.baseModel.ref, includeIgnored, seen));
1451
+ if (decl.baseModel.ref) {
1452
+ fields.push(...getAllFields(decl.baseModel.ref, includeIgnored, seen));
1453
+ }
1443
1454
  }
1444
1455
  fields.push(...decl.fields.filter((f) => includeIgnored || !hasAttribute(f, "@ignore")));
1445
1456
  return fields;
@@ -1452,12 +1463,14 @@ function getAllAttributes(decl, seen = /* @__PURE__ */ new Set()) {
1452
1463
  seen.add(decl);
1453
1464
  const attributes = [];
1454
1465
  for (const mixin of decl.mixins) {
1455
- (0, import_common_helpers.invariant)(mixin.ref, `Mixin ${mixin.$refText} is not resolved`);
1456
- attributes.push(...getAllAttributes(mixin.ref, seen));
1466
+ if (mixin.ref) {
1467
+ attributes.push(...getAllAttributes(mixin.ref, seen));
1468
+ }
1457
1469
  }
1458
1470
  if (isDataModel(decl) && decl.baseModel) {
1459
- (0, import_common_helpers.invariant)(decl.baseModel.ref, `Base model ${decl.baseModel.$refText} is not resolved`);
1460
- attributes.push(...getAllAttributes(decl.baseModel.ref, seen));
1471
+ if (decl.baseModel.ref) {
1472
+ attributes.push(...getAllAttributes(decl.baseModel.ref, seen));
1473
+ }
1461
1474
  }
1462
1475
  attributes.push(...decl.attributes);
1463
1476
  return attributes;
@@ -1472,6 +1485,71 @@ function getDocument(node) {
1472
1485
  return result;
1473
1486
  }
1474
1487
  __name(getDocument, "getDocument");
1488
+ function getPluginDocuments(model, schemaPath) {
1489
+ const result = [];
1490
+ for (const decl of model.declarations.filter(isPlugin)) {
1491
+ const providerField = decl.fields.find((f) => f.name === "provider");
1492
+ if (!providerField) {
1493
+ continue;
1494
+ }
1495
+ const provider = getLiteral(providerField.value);
1496
+ if (!provider) {
1497
+ continue;
1498
+ }
1499
+ let pluginModelFile;
1500
+ let providerPath = import_node_path.default.resolve(import_node_path.default.dirname(schemaPath), provider);
1501
+ if (import_node_fs.default.existsSync(providerPath)) {
1502
+ if (import_node_fs.default.statSync(providerPath).isDirectory()) {
1503
+ providerPath = import_node_path.default.join(providerPath, "index.js");
1504
+ }
1505
+ pluginModelFile = import_node_path.default.resolve(import_node_path.default.dirname(providerPath), PLUGIN_MODULE_NAME);
1506
+ if (!import_node_fs.default.existsSync(pluginModelFile)) {
1507
+ pluginModelFile = findUp([
1508
+ PLUGIN_MODULE_NAME
1509
+ ], import_node_path.default.dirname(providerPath));
1510
+ }
1511
+ }
1512
+ if (!pluginModelFile) {
1513
+ if (typeof import_meta.resolve === "function") {
1514
+ try {
1515
+ const resolvedUrl = import_meta.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
1516
+ pluginModelFile = (0, import_node_url.fileURLToPath)(resolvedUrl);
1517
+ } catch {
1518
+ }
1519
+ }
1520
+ }
1521
+ if (!pluginModelFile) {
1522
+ try {
1523
+ const require2 = (0, import_node_module.createRequire)((0, import_node_url.pathToFileURL)(schemaPath));
1524
+ pluginModelFile = require2.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
1525
+ } catch {
1526
+ }
1527
+ }
1528
+ if (pluginModelFile && import_node_fs.default.existsSync(pluginModelFile)) {
1529
+ result.push(pluginModelFile);
1530
+ }
1531
+ }
1532
+ return result;
1533
+ }
1534
+ __name(getPluginDocuments, "getPluginDocuments");
1535
+ function findUp(names, cwd = process.cwd(), multiple = false, result = []) {
1536
+ if (!names.some((name) => !!name)) {
1537
+ return void 0;
1538
+ }
1539
+ const target = names.find((name) => import_node_fs.default.existsSync(import_node_path.default.join(cwd, name)));
1540
+ if (multiple === false && target) {
1541
+ return import_node_path.default.join(cwd, target);
1542
+ }
1543
+ if (target) {
1544
+ result.push(import_node_path.default.join(cwd, target));
1545
+ }
1546
+ const up = import_node_path.default.resolve(cwd, "..");
1547
+ if (up === cwd) {
1548
+ return multiple && result.length > 0 ? result : void 0;
1549
+ }
1550
+ return findUp(names, up, multiple, result);
1551
+ }
1552
+ __name(findUp, "findUp");
1475
1553
  function findRootNode(node) {
1476
1554
  while (node.$container) {
1477
1555
  node = node.$container;
@@ -1502,20 +1580,20 @@ __name(findRootNode, "findRootNode");
1502
1580
  getModelIdFields,
1503
1581
  getModelUniqueFields,
1504
1582
  getObjectLiteral,
1583
+ getPluginDocuments,
1505
1584
  getRecursiveBases,
1506
1585
  getStringLiteral,
1507
1586
  getUniqueFields,
1508
1587
  hasAttribute,
1509
1588
  isAuthInvocation,
1510
1589
  isAuthOrAuthMemberAccess,
1590
+ isBeforeInvocation,
1511
1591
  isCheckInvocation,
1512
1592
  isCollectionPredicate,
1513
1593
  isDataFieldReference,
1514
1594
  isDelegateModel,
1515
1595
  isEnumFieldReference,
1516
1596
  isFromStdlib,
1517
- isFutureExpr,
1518
- isFutureInvocation,
1519
1597
  isMemberContainer,
1520
1598
  isRelationshipField,
1521
1599
  mapBuiltinTypeToExpressionType,