@zenstackhq/language 3.0.0-beta.3 → 3.0.0-beta.31

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
@@ -2,13 +2,15 @@ var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
4
  // src/utils.ts
5
- import { invariant } from "@zenstackhq/common-helpers";
6
5
  import { AstUtils, URI } from "langium";
7
6
  import fs from "fs";
7
+ import { createRequire } from "module";
8
8
  import path from "path";
9
+ import { fileURLToPath, pathToFileURL } from "url";
9
10
 
10
11
  // src/constants.ts
11
12
  var STD_LIB_MODULE_NAME = "stdlib.zmodel";
13
+ var PLUGIN_MODULE_NAME = "plugin.zmodel";
12
14
 
13
15
  // src/generated/ast.ts
14
16
  import * as langium from "langium";
@@ -101,6 +103,10 @@ function isObjectExpr(item) {
101
103
  }
102
104
  __name(isObjectExpr, "isObjectExpr");
103
105
  var Plugin = "Plugin";
106
+ function isPlugin(item) {
107
+ return reflection.isInstance(item, Plugin);
108
+ }
109
+ __name(isPlugin, "isPlugin");
104
110
  var PluginField = "PluginField";
105
111
  var Procedure = "Procedure";
106
112
  var ProcedureParam = "ProcedureParam";
@@ -1044,10 +1050,6 @@ function isRelationshipField(field) {
1044
1050
  return isDataModel(field.type.reference?.ref);
1045
1051
  }
1046
1052
  __name(isRelationshipField, "isRelationshipField");
1047
- function isFutureExpr(node) {
1048
- return isInvocationExpr(node) && node.function.ref?.name === "future" && isFromStdlib(node.function.ref);
1049
- }
1050
- __name(isFutureExpr, "isFutureExpr");
1051
1053
  function isDelegateModel(node) {
1052
1054
  return isDataModel(node) && hasAttribute(node, "@@delegate");
1053
1055
  }
@@ -1065,8 +1067,14 @@ function getRecursiveBases(decl, includeDelegate = true, seen = /* @__PURE__ */
1065
1067
  return result;
1066
1068
  }
1067
1069
  seen.add(decl);
1068
- decl.mixins.forEach((mixin) => {
1069
- const baseDecl = mixin.ref;
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);
1070
1078
  if (baseDecl) {
1071
1079
  if (!includeDelegate && isDelegateModel(baseDecl)) {
1072
1080
  return;
@@ -1189,6 +1197,10 @@ function getArray(expr) {
1189
1197
  return isArrayExpr(expr) || isConfigArrayExpr(expr) ? expr.items : void 0;
1190
1198
  }
1191
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");
1192
1204
  function getAttributeArgLiteral(attr, name) {
1193
1205
  for (const arg of attr.args) {
1194
1206
  if (arg.$resolvedParam?.name === name) {
@@ -1225,7 +1237,7 @@ function getFieldReference(expr) {
1225
1237
  }
1226
1238
  __name(getFieldReference, "getFieldReference");
1227
1239
  function isCheckInvocation(node) {
1228
- return isInvocationExpr(node) && node.function.ref?.name === "check" && isFromStdlib(node.function.ref);
1240
+ return isInvocationExpr(node) && node.function.ref?.name === "check";
1229
1241
  }
1230
1242
  __name(isCheckInvocation, "isCheckInvocation");
1231
1243
  function resolveTransitiveImports(documents, model) {
@@ -1295,17 +1307,17 @@ function getAllDeclarationsIncludingImports(documents, model) {
1295
1307
  }
1296
1308
  __name(getAllDeclarationsIncludingImports, "getAllDeclarationsIncludingImports");
1297
1309
  function getAuthDecl(decls) {
1298
- let authModel = decls.find((m) => hasAttribute(m, "@@auth"));
1310
+ let authModel = decls.find((d) => hasAttribute(d, "@@auth"));
1299
1311
  if (!authModel) {
1300
- authModel = decls.find((m) => m.name === "User");
1312
+ authModel = decls.find((d) => d.name === "User");
1301
1313
  }
1302
1314
  return authModel;
1303
1315
  }
1304
1316
  __name(getAuthDecl, "getAuthDecl");
1305
- function isFutureInvocation(node) {
1306
- return isInvocationExpr(node) && node.function.ref?.name === "future" && isFromStdlib(node.function.ref);
1317
+ function isBeforeInvocation(node) {
1318
+ return isInvocationExpr(node) && node.function.ref?.name === "before";
1307
1319
  }
1308
- __name(isFutureInvocation, "isFutureInvocation");
1320
+ __name(isBeforeInvocation, "isBeforeInvocation");
1309
1321
  function isCollectionPredicate(node) {
1310
1322
  return isBinaryExpr(node) && [
1311
1323
  "?",
@@ -1360,12 +1372,14 @@ function getAllFields(decl, includeIgnored = false, seen = /* @__PURE__ */ new S
1360
1372
  seen.add(decl);
1361
1373
  const fields = [];
1362
1374
  for (const mixin of decl.mixins) {
1363
- invariant(mixin.ref, `Mixin ${mixin.$refText} is not resolved`);
1364
- fields.push(...getAllFields(mixin.ref, includeIgnored, seen));
1375
+ if (mixin.ref) {
1376
+ fields.push(...getAllFields(mixin.ref, includeIgnored, seen));
1377
+ }
1365
1378
  }
1366
1379
  if (isDataModel(decl) && decl.baseModel) {
1367
- invariant(decl.baseModel.ref, `Base model ${decl.baseModel.$refText} is not resolved`);
1368
- fields.push(...getAllFields(decl.baseModel.ref, includeIgnored, seen));
1380
+ if (decl.baseModel.ref) {
1381
+ fields.push(...getAllFields(decl.baseModel.ref, includeIgnored, seen));
1382
+ }
1369
1383
  }
1370
1384
  fields.push(...decl.fields.filter((f) => includeIgnored || !hasAttribute(f, "@ignore")));
1371
1385
  return fields;
@@ -1378,17 +1392,29 @@ function getAllAttributes(decl, seen = /* @__PURE__ */ new Set()) {
1378
1392
  seen.add(decl);
1379
1393
  const attributes = [];
1380
1394
  for (const mixin of decl.mixins) {
1381
- invariant(mixin.ref, `Mixin ${mixin.$refText} is not resolved`);
1382
- attributes.push(...getAllAttributes(mixin.ref, seen));
1395
+ if (mixin.ref) {
1396
+ attributes.push(...getAllAttributes(mixin.ref, seen));
1397
+ }
1383
1398
  }
1384
1399
  if (isDataModel(decl) && decl.baseModel) {
1385
- invariant(decl.baseModel.ref, `Base model ${decl.baseModel.$refText} is not resolved`);
1386
- attributes.push(...getAllAttributes(decl.baseModel.ref, seen));
1400
+ if (decl.baseModel.ref) {
1401
+ const attrs = getAllAttributes(decl.baseModel.ref, seen).filter((attr) => !isNonInheritableAttribute(attr));
1402
+ attributes.push(...attrs);
1403
+ }
1387
1404
  }
1388
1405
  attributes.push(...decl.attributes);
1389
1406
  return attributes;
1390
1407
  }
1391
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");
1392
1418
  function getDocument(node) {
1393
1419
  const rootNode = findRootNode(node);
1394
1420
  const result = rootNode.$document;
@@ -1398,6 +1424,71 @@ function getDocument(node) {
1398
1424
  return result;
1399
1425
  }
1400
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");
1401
1492
  function findRootNode(node) {
1402
1493
  while (node.$container) {
1403
1494
  node = node.$container;
@@ -1415,6 +1506,7 @@ export {
1415
1506
  getAllLoadedAndReachableDataModelsAndTypeDefs,
1416
1507
  getAllLoadedDataModelsAndTypeDefs,
1417
1508
  getAttribute,
1509
+ getAttributeArg,
1418
1510
  getAttributeArgLiteral,
1419
1511
  getAuthDecl,
1420
1512
  getContainingDataModel,
@@ -1427,20 +1519,20 @@ export {
1427
1519
  getModelIdFields,
1428
1520
  getModelUniqueFields,
1429
1521
  getObjectLiteral,
1522
+ getPluginDocuments,
1430
1523
  getRecursiveBases,
1431
1524
  getStringLiteral,
1432
1525
  getUniqueFields,
1433
1526
  hasAttribute,
1434
1527
  isAuthInvocation,
1435
1528
  isAuthOrAuthMemberAccess,
1529
+ isBeforeInvocation,
1436
1530
  isCheckInvocation,
1437
1531
  isCollectionPredicate,
1438
1532
  isDataFieldReference,
1439
1533
  isDelegateModel,
1440
1534
  isEnumFieldReference,
1441
1535
  isFromStdlib,
1442
- isFutureExpr,
1443
- isFutureInvocation,
1444
1536
  isMemberContainer,
1445
1537
  isRelationshipField,
1446
1538
  mapBuiltinTypeToExpressionType,