@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.js CHANGED
@@ -4,10 +4,13 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
4
4
  // src/utils.ts
5
5
  import { AstUtils, URI } from "langium";
6
6
  import fs from "fs";
7
+ import { createRequire } from "module";
7
8
  import path from "path";
9
+ import { fileURLToPath, pathToFileURL } from "url";
8
10
 
9
11
  // src/constants.ts
10
12
  var STD_LIB_MODULE_NAME = "stdlib.zmodel";
13
+ var PLUGIN_MODULE_NAME = "plugin.zmodel";
11
14
 
12
15
  // src/generated/ast.ts
13
16
  import * as langium from "langium";
@@ -100,6 +103,10 @@ function isObjectExpr(item) {
100
103
  }
101
104
  __name(isObjectExpr, "isObjectExpr");
102
105
  var Plugin = "Plugin";
106
+ function isPlugin(item) {
107
+ return reflection.isInstance(item, Plugin);
108
+ }
109
+ __name(isPlugin, "isPlugin");
103
110
  var PluginField = "PluginField";
104
111
  var Procedure = "Procedure";
105
112
  var ProcedureParam = "ProcedureParam";
@@ -1067,7 +1074,7 @@ function getRecursiveBases(decl, includeDelegate = true, seen = /* @__PURE__ */
1067
1074
  ] : []
1068
1075
  ];
1069
1076
  bases.forEach((base) => {
1070
- const baseDecl = decl.$container.declarations.find((d) => isTypeDef(d) || isDataModel(d) && d.name === base.$refText);
1077
+ const baseDecl = decl.$container.declarations.find((d) => (isTypeDef(d) || isDataModel(d)) && d.name === base.$refText);
1071
1078
  if (baseDecl) {
1072
1079
  if (!includeDelegate && isDelegateModel(baseDecl)) {
1073
1080
  return;
@@ -1190,6 +1197,10 @@ function getArray(expr) {
1190
1197
  return isArrayExpr(expr) || isConfigArrayExpr(expr) ? expr.items : void 0;
1191
1198
  }
1192
1199
  __name(getArray, "getArray");
1200
+ function getAttributeArg(attr, name) {
1201
+ return attr.args.find((arg) => arg.$resolvedParam?.name === name)?.value;
1202
+ }
1203
+ __name(getAttributeArg, "getAttributeArg");
1193
1204
  function getAttributeArgLiteral(attr, name) {
1194
1205
  for (const arg of attr.args) {
1195
1206
  if (arg.$resolvedParam?.name === name) {
@@ -1296,15 +1307,15 @@ function getAllDeclarationsIncludingImports(documents, model) {
1296
1307
  }
1297
1308
  __name(getAllDeclarationsIncludingImports, "getAllDeclarationsIncludingImports");
1298
1309
  function getAuthDecl(decls) {
1299
- let authModel = decls.find((m) => hasAttribute(m, "@@auth"));
1310
+ let authModel = decls.find((d) => hasAttribute(d, "@@auth"));
1300
1311
  if (!authModel) {
1301
- authModel = decls.find((m) => m.name === "User");
1312
+ authModel = decls.find((d) => d.name === "User");
1302
1313
  }
1303
1314
  return authModel;
1304
1315
  }
1305
1316
  __name(getAuthDecl, "getAuthDecl");
1306
1317
  function isBeforeInvocation(node) {
1307
- return isInvocationExpr(node) && node.function.ref?.name === "before" && isFromStdlib(node.function.ref);
1318
+ return isInvocationExpr(node) && node.function.ref?.name === "before";
1308
1319
  }
1309
1320
  __name(isBeforeInvocation, "isBeforeInvocation");
1310
1321
  function isCollectionPredicate(node) {
@@ -1387,13 +1398,23 @@ function getAllAttributes(decl, seen = /* @__PURE__ */ new Set()) {
1387
1398
  }
1388
1399
  if (isDataModel(decl) && decl.baseModel) {
1389
1400
  if (decl.baseModel.ref) {
1390
- attributes.push(...getAllAttributes(decl.baseModel.ref, seen));
1401
+ const attrs = getAllAttributes(decl.baseModel.ref, seen).filter((attr) => !isNonInheritableAttribute(attr));
1402
+ attributes.push(...attrs);
1391
1403
  }
1392
1404
  }
1393
1405
  attributes.push(...decl.attributes);
1394
1406
  return attributes;
1395
1407
  }
1396
1408
  __name(getAllAttributes, "getAllAttributes");
1409
+ function isNonInheritableAttribute(attr) {
1410
+ const attrName = attr.decl.ref?.name ?? attr.decl.$refText;
1411
+ return [
1412
+ "@@map",
1413
+ "@@unique",
1414
+ "@@index"
1415
+ ].includes(attrName);
1416
+ }
1417
+ __name(isNonInheritableAttribute, "isNonInheritableAttribute");
1397
1418
  function getDocument(node) {
1398
1419
  const rootNode = findRootNode(node);
1399
1420
  const result = rootNode.$document;
@@ -1403,6 +1424,71 @@ function getDocument(node) {
1403
1424
  return result;
1404
1425
  }
1405
1426
  __name(getDocument, "getDocument");
1427
+ function getPluginDocuments(model, schemaPath) {
1428
+ const result = [];
1429
+ for (const decl of model.declarations.filter(isPlugin)) {
1430
+ const providerField = decl.fields.find((f) => f.name === "provider");
1431
+ if (!providerField) {
1432
+ continue;
1433
+ }
1434
+ const provider = getLiteral(providerField.value);
1435
+ if (!provider) {
1436
+ continue;
1437
+ }
1438
+ let pluginModelFile;
1439
+ let providerPath = path.resolve(path.dirname(schemaPath), provider);
1440
+ if (fs.existsSync(providerPath)) {
1441
+ if (fs.statSync(providerPath).isDirectory()) {
1442
+ providerPath = path.join(providerPath, "index.js");
1443
+ }
1444
+ pluginModelFile = path.resolve(path.dirname(providerPath), PLUGIN_MODULE_NAME);
1445
+ if (!fs.existsSync(pluginModelFile)) {
1446
+ pluginModelFile = findUp([
1447
+ PLUGIN_MODULE_NAME
1448
+ ], path.dirname(providerPath));
1449
+ }
1450
+ }
1451
+ if (!pluginModelFile) {
1452
+ if (typeof import.meta.resolve === "function") {
1453
+ try {
1454
+ const resolvedUrl = import.meta.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
1455
+ pluginModelFile = fileURLToPath(resolvedUrl);
1456
+ } catch {
1457
+ }
1458
+ }
1459
+ }
1460
+ if (!pluginModelFile) {
1461
+ try {
1462
+ const require2 = createRequire(pathToFileURL(schemaPath));
1463
+ pluginModelFile = require2.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
1464
+ } catch {
1465
+ }
1466
+ }
1467
+ if (pluginModelFile && fs.existsSync(pluginModelFile)) {
1468
+ result.push(pluginModelFile);
1469
+ }
1470
+ }
1471
+ return result;
1472
+ }
1473
+ __name(getPluginDocuments, "getPluginDocuments");
1474
+ function findUp(names, cwd = process.cwd(), multiple = false, result = []) {
1475
+ if (!names.some((name) => !!name)) {
1476
+ return void 0;
1477
+ }
1478
+ const target = names.find((name) => fs.existsSync(path.join(cwd, name)));
1479
+ if (multiple === false && target) {
1480
+ return path.join(cwd, target);
1481
+ }
1482
+ if (target) {
1483
+ result.push(path.join(cwd, target));
1484
+ }
1485
+ const up = path.resolve(cwd, "..");
1486
+ if (up === cwd) {
1487
+ return multiple && result.length > 0 ? result : void 0;
1488
+ }
1489
+ return findUp(names, up, multiple, result);
1490
+ }
1491
+ __name(findUp, "findUp");
1406
1492
  function findRootNode(node) {
1407
1493
  while (node.$container) {
1408
1494
  node = node.$container;
@@ -1420,6 +1506,7 @@ export {
1420
1506
  getAllLoadedAndReachableDataModelsAndTypeDefs,
1421
1507
  getAllLoadedDataModelsAndTypeDefs,
1422
1508
  getAttribute,
1509
+ getAttributeArg,
1423
1510
  getAttributeArgLiteral,
1424
1511
  getAuthDecl,
1425
1512
  getContainingDataModel,
@@ -1432,6 +1519,7 @@ export {
1432
1519
  getModelIdFields,
1433
1520
  getModelUniqueFields,
1434
1521
  getObjectLiteral,
1522
+ getPluginDocuments,
1435
1523
  getRecursiveBases,
1436
1524
  getStringLiteral,
1437
1525
  getUniqueFields,