@zenstackhq/language 3.0.0-beta.8 → 3.0.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";
@@ -1060,8 +1067,14 @@ function getRecursiveBases(decl, includeDelegate = true, seen = /* @__PURE__ */
1060
1067
  return result;
1061
1068
  }
1062
1069
  seen.add(decl);
1063
- decl.mixins.forEach((mixin) => {
1064
- const baseDecl = decl.$container.declarations.find((d) => isTypeDef(d) && d.name === mixin.$refText);
1070
+ const bases = [
1071
+ ...decl.mixins,
1072
+ ...isDataModel(decl) && decl.baseModel ? [
1073
+ decl.baseModel
1074
+ ] : []
1075
+ ];
1076
+ bases.forEach((base) => {
1077
+ const baseDecl = decl.$container.declarations.find((d) => (isTypeDef(d) || isDataModel(d)) && d.name === base.$refText);
1065
1078
  if (baseDecl) {
1066
1079
  if (!includeDelegate && isDelegateModel(baseDecl)) {
1067
1080
  return;
@@ -1184,6 +1197,10 @@ function getArray(expr) {
1184
1197
  return isArrayExpr(expr) || isConfigArrayExpr(expr) ? expr.items : void 0;
1185
1198
  }
1186
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");
1187
1204
  function getAttributeArgLiteral(attr, name) {
1188
1205
  for (const arg of attr.args) {
1189
1206
  if (arg.$resolvedParam?.name === name) {
@@ -1290,15 +1307,15 @@ function getAllDeclarationsIncludingImports(documents, model) {
1290
1307
  }
1291
1308
  __name(getAllDeclarationsIncludingImports, "getAllDeclarationsIncludingImports");
1292
1309
  function getAuthDecl(decls) {
1293
- let authModel = decls.find((m) => hasAttribute(m, "@@auth"));
1310
+ let authModel = decls.find((d) => hasAttribute(d, "@@auth"));
1294
1311
  if (!authModel) {
1295
- authModel = decls.find((m) => m.name === "User");
1312
+ authModel = decls.find((d) => d.name === "User");
1296
1313
  }
1297
1314
  return authModel;
1298
1315
  }
1299
1316
  __name(getAuthDecl, "getAuthDecl");
1300
1317
  function isBeforeInvocation(node) {
1301
- return isInvocationExpr(node) && node.function.ref?.name === "before" && isFromStdlib(node.function.ref);
1318
+ return isInvocationExpr(node) && node.function.ref?.name === "before";
1302
1319
  }
1303
1320
  __name(isBeforeInvocation, "isBeforeInvocation");
1304
1321
  function isCollectionPredicate(node) {
@@ -1381,13 +1398,23 @@ function getAllAttributes(decl, seen = /* @__PURE__ */ new Set()) {
1381
1398
  }
1382
1399
  if (isDataModel(decl) && decl.baseModel) {
1383
1400
  if (decl.baseModel.ref) {
1384
- attributes.push(...getAllAttributes(decl.baseModel.ref, seen));
1401
+ const attrs = getAllAttributes(decl.baseModel.ref, seen).filter((attr) => !isNonInheritableAttribute(attr));
1402
+ attributes.push(...attrs);
1385
1403
  }
1386
1404
  }
1387
1405
  attributes.push(...decl.attributes);
1388
1406
  return attributes;
1389
1407
  }
1390
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");
1391
1418
  function getDocument(node) {
1392
1419
  const rootNode = findRootNode(node);
1393
1420
  const result = rootNode.$document;
@@ -1397,6 +1424,71 @@ function getDocument(node) {
1397
1424
  return result;
1398
1425
  }
1399
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");
1400
1492
  function findRootNode(node) {
1401
1493
  while (node.$container) {
1402
1494
  node = node.$container;
@@ -1414,6 +1506,7 @@ export {
1414
1506
  getAllLoadedAndReachableDataModelsAndTypeDefs,
1415
1507
  getAllLoadedDataModelsAndTypeDefs,
1416
1508
  getAttribute,
1509
+ getAttributeArg,
1417
1510
  getAttributeArgLiteral,
1418
1511
  getAuthDecl,
1419
1512
  getContainingDataModel,
@@ -1426,6 +1519,7 @@ export {
1426
1519
  getModelIdFields,
1427
1520
  getModelUniqueFields,
1428
1521
  getObjectLiteral,
1522
+ getPluginDocuments,
1429
1523
  getRecursiveBases,
1430
1524
  getStringLiteral,
1431
1525
  getUniqueFields,