@zenstackhq/language 3.0.0-beta.9 → 3.1.0

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
@@ -40,6 +40,7 @@ __export(utils_exports, {
40
40
  getAllLoadedAndReachableDataModelsAndTypeDefs: () => getAllLoadedAndReachableDataModelsAndTypeDefs,
41
41
  getAllLoadedDataModelsAndTypeDefs: () => getAllLoadedDataModelsAndTypeDefs,
42
42
  getAttribute: () => getAttribute,
43
+ getAttributeArg: () => getAttributeArg,
43
44
  getAttributeArgLiteral: () => getAttributeArgLiteral,
44
45
  getAuthDecl: () => getAuthDecl,
45
46
  getContainingDataModel: () => getContainingDataModel,
@@ -52,6 +53,7 @@ __export(utils_exports, {
52
53
  getModelIdFields: () => getModelIdFields,
53
54
  getModelUniqueFields: () => getModelUniqueFields,
54
55
  getObjectLiteral: () => getObjectLiteral,
56
+ getPluginDocuments: () => getPluginDocuments,
55
57
  getRecursiveBases: () => getRecursiveBases,
56
58
  getStringLiteral: () => getStringLiteral,
57
59
  getUniqueFields: () => getUniqueFields,
@@ -77,10 +79,13 @@ __export(utils_exports, {
77
79
  module.exports = __toCommonJS(utils_exports);
78
80
  var import_langium = require("langium");
79
81
  var import_node_fs = __toESM(require("fs"), 1);
80
- var import_path = __toESM(require("path"), 1);
82
+ var import_node_module = require("module");
83
+ var import_node_path = __toESM(require("path"), 1);
84
+ var import_node_url = require("url");
81
85
 
82
86
  // src/constants.ts
83
87
  var STD_LIB_MODULE_NAME = "stdlib.zmodel";
88
+ var PLUGIN_MODULE_NAME = "plugin.zmodel";
84
89
 
85
90
  // src/generated/ast.ts
86
91
  var langium = __toESM(require("langium"), 1);
@@ -173,6 +178,10 @@ function isObjectExpr(item) {
173
178
  }
174
179
  __name(isObjectExpr, "isObjectExpr");
175
180
  var Plugin = "Plugin";
181
+ function isPlugin(item) {
182
+ return reflection.isInstance(item, Plugin);
183
+ }
184
+ __name(isPlugin, "isPlugin");
176
185
  var PluginField = "PluginField";
177
186
  var Procedure = "Procedure";
178
187
  var ProcedureParam = "ProcedureParam";
@@ -1037,6 +1046,7 @@ var ZModelAstReflection = class extends langium.AbstractAstReflection {
1037
1046
  var reflection = new ZModelAstReflection();
1038
1047
 
1039
1048
  // src/utils.ts
1049
+ var import_meta = {};
1040
1050
  function hasAttribute(decl, name) {
1041
1051
  return !!getAttribute(decl, name);
1042
1052
  }
@@ -1140,7 +1150,7 @@ function getRecursiveBases(decl, includeDelegate = true, seen = /* @__PURE__ */
1140
1150
  ] : []
1141
1151
  ];
1142
1152
  bases.forEach((base) => {
1143
- const baseDecl = decl.$container.declarations.find((d) => isTypeDef(d) || isDataModel(d) && d.name === base.$refText);
1153
+ const baseDecl = decl.$container.declarations.find((d) => (isTypeDef(d) || isDataModel(d)) && d.name === base.$refText);
1144
1154
  if (baseDecl) {
1145
1155
  if (!includeDelegate && isDelegateModel(baseDecl)) {
1146
1156
  return;
@@ -1263,6 +1273,10 @@ function getArray(expr) {
1263
1273
  return isArrayExpr(expr) || isConfigArrayExpr(expr) ? expr.items : void 0;
1264
1274
  }
1265
1275
  __name(getArray, "getArray");
1276
+ function getAttributeArg(attr, name) {
1277
+ return attr.args.find((arg) => arg.$resolvedParam?.name === name)?.value;
1278
+ }
1279
+ __name(getAttributeArg, "getAttributeArg");
1266
1280
  function getAttributeArgLiteral(attr, name) {
1267
1281
  for (const arg of attr.args) {
1268
1282
  if (arg.$resolvedParam?.name === name) {
@@ -1349,9 +1363,9 @@ function resolveImportUri(imp) {
1349
1363
  return void 0;
1350
1364
  }
1351
1365
  const doc = import_langium.AstUtils.getDocument(imp);
1352
- const dir = import_path.default.dirname(doc.uri.fsPath);
1366
+ const dir = import_node_path.default.dirname(doc.uri.fsPath);
1353
1367
  const importPath = imp.path.endsWith(".zmodel") ? imp.path : `${imp.path}.zmodel`;
1354
- return import_langium.URI.file(import_path.default.resolve(dir, importPath));
1368
+ return import_langium.URI.file(import_node_path.default.resolve(dir, importPath));
1355
1369
  }
1356
1370
  __name(resolveImportUri, "resolveImportUri");
1357
1371
  function getDataModelAndTypeDefs(model, includeIgnored = false) {
@@ -1369,15 +1383,15 @@ function getAllDeclarationsIncludingImports(documents, model) {
1369
1383
  }
1370
1384
  __name(getAllDeclarationsIncludingImports, "getAllDeclarationsIncludingImports");
1371
1385
  function getAuthDecl(decls) {
1372
- let authModel = decls.find((m) => hasAttribute(m, "@@auth"));
1386
+ let authModel = decls.find((d) => hasAttribute(d, "@@auth"));
1373
1387
  if (!authModel) {
1374
- authModel = decls.find((m) => m.name === "User");
1388
+ authModel = decls.find((d) => d.name === "User");
1375
1389
  }
1376
1390
  return authModel;
1377
1391
  }
1378
1392
  __name(getAuthDecl, "getAuthDecl");
1379
1393
  function isBeforeInvocation(node) {
1380
- return isInvocationExpr(node) && node.function.ref?.name === "before" && isFromStdlib(node.function.ref);
1394
+ return isInvocationExpr(node) && node.function.ref?.name === "before";
1381
1395
  }
1382
1396
  __name(isBeforeInvocation, "isBeforeInvocation");
1383
1397
  function isCollectionPredicate(node) {
@@ -1460,13 +1474,23 @@ function getAllAttributes(decl, seen = /* @__PURE__ */ new Set()) {
1460
1474
  }
1461
1475
  if (isDataModel(decl) && decl.baseModel) {
1462
1476
  if (decl.baseModel.ref) {
1463
- attributes.push(...getAllAttributes(decl.baseModel.ref, seen));
1477
+ const attrs = getAllAttributes(decl.baseModel.ref, seen).filter((attr) => !isNonInheritableAttribute(attr));
1478
+ attributes.push(...attrs);
1464
1479
  }
1465
1480
  }
1466
1481
  attributes.push(...decl.attributes);
1467
1482
  return attributes;
1468
1483
  }
1469
1484
  __name(getAllAttributes, "getAllAttributes");
1485
+ function isNonInheritableAttribute(attr) {
1486
+ const attrName = attr.decl.ref?.name ?? attr.decl.$refText;
1487
+ return [
1488
+ "@@map",
1489
+ "@@unique",
1490
+ "@@index"
1491
+ ].includes(attrName);
1492
+ }
1493
+ __name(isNonInheritableAttribute, "isNonInheritableAttribute");
1470
1494
  function getDocument(node) {
1471
1495
  const rootNode = findRootNode(node);
1472
1496
  const result = rootNode.$document;
@@ -1476,6 +1500,71 @@ function getDocument(node) {
1476
1500
  return result;
1477
1501
  }
1478
1502
  __name(getDocument, "getDocument");
1503
+ function getPluginDocuments(model, schemaPath) {
1504
+ const result = [];
1505
+ for (const decl of model.declarations.filter(isPlugin)) {
1506
+ const providerField = decl.fields.find((f) => f.name === "provider");
1507
+ if (!providerField) {
1508
+ continue;
1509
+ }
1510
+ const provider = getLiteral(providerField.value);
1511
+ if (!provider) {
1512
+ continue;
1513
+ }
1514
+ let pluginModelFile;
1515
+ let providerPath = import_node_path.default.resolve(import_node_path.default.dirname(schemaPath), provider);
1516
+ if (import_node_fs.default.existsSync(providerPath)) {
1517
+ if (import_node_fs.default.statSync(providerPath).isDirectory()) {
1518
+ providerPath = import_node_path.default.join(providerPath, "index.js");
1519
+ }
1520
+ pluginModelFile = import_node_path.default.resolve(import_node_path.default.dirname(providerPath), PLUGIN_MODULE_NAME);
1521
+ if (!import_node_fs.default.existsSync(pluginModelFile)) {
1522
+ pluginModelFile = findUp([
1523
+ PLUGIN_MODULE_NAME
1524
+ ], import_node_path.default.dirname(providerPath));
1525
+ }
1526
+ }
1527
+ if (!pluginModelFile) {
1528
+ if (typeof import_meta.resolve === "function") {
1529
+ try {
1530
+ const resolvedUrl = import_meta.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
1531
+ pluginModelFile = (0, import_node_url.fileURLToPath)(resolvedUrl);
1532
+ } catch {
1533
+ }
1534
+ }
1535
+ }
1536
+ if (!pluginModelFile) {
1537
+ try {
1538
+ const require2 = (0, import_node_module.createRequire)((0, import_node_url.pathToFileURL)(schemaPath));
1539
+ pluginModelFile = require2.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
1540
+ } catch {
1541
+ }
1542
+ }
1543
+ if (pluginModelFile && import_node_fs.default.existsSync(pluginModelFile)) {
1544
+ result.push(pluginModelFile);
1545
+ }
1546
+ }
1547
+ return result;
1548
+ }
1549
+ __name(getPluginDocuments, "getPluginDocuments");
1550
+ function findUp(names, cwd = process.cwd(), multiple = false, result = []) {
1551
+ if (!names.some((name) => !!name)) {
1552
+ return void 0;
1553
+ }
1554
+ const target = names.find((name) => import_node_fs.default.existsSync(import_node_path.default.join(cwd, name)));
1555
+ if (multiple === false && target) {
1556
+ return import_node_path.default.join(cwd, target);
1557
+ }
1558
+ if (target) {
1559
+ result.push(import_node_path.default.join(cwd, target));
1560
+ }
1561
+ const up = import_node_path.default.resolve(cwd, "..");
1562
+ if (up === cwd) {
1563
+ return multiple && result.length > 0 ? result : void 0;
1564
+ }
1565
+ return findUp(names, up, multiple, result);
1566
+ }
1567
+ __name(findUp, "findUp");
1479
1568
  function findRootNode(node) {
1480
1569
  while (node.$container) {
1481
1570
  node = node.$container;
@@ -1494,6 +1583,7 @@ __name(findRootNode, "findRootNode");
1494
1583
  getAllLoadedAndReachableDataModelsAndTypeDefs,
1495
1584
  getAllLoadedDataModelsAndTypeDefs,
1496
1585
  getAttribute,
1586
+ getAttributeArg,
1497
1587
  getAttributeArgLiteral,
1498
1588
  getAuthDecl,
1499
1589
  getContainingDataModel,
@@ -1506,6 +1596,7 @@ __name(findRootNode, "findRootNode");
1506
1596
  getModelIdFields,
1507
1597
  getModelUniqueFields,
1508
1598
  getObjectLiteral,
1599
+ getPluginDocuments,
1509
1600
  getRecursiveBases,
1510
1601
  getStringLiteral,
1511
1602
  getUniqueFields,