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

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,12 +1392,14 @@ 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
+ attributes.push(...getAllAttributes(decl.baseModel.ref, seen));
1402
+ }
1387
1403
  }
1388
1404
  attributes.push(...decl.attributes);
1389
1405
  return attributes;
@@ -1398,6 +1414,71 @@ function getDocument(node) {
1398
1414
  return result;
1399
1415
  }
1400
1416
  __name(getDocument, "getDocument");
1417
+ function getPluginDocuments(model, schemaPath) {
1418
+ const result = [];
1419
+ for (const decl of model.declarations.filter(isPlugin)) {
1420
+ const providerField = decl.fields.find((f) => f.name === "provider");
1421
+ if (!providerField) {
1422
+ continue;
1423
+ }
1424
+ const provider = getLiteral(providerField.value);
1425
+ if (!provider) {
1426
+ continue;
1427
+ }
1428
+ let pluginModelFile;
1429
+ let providerPath = path.resolve(path.dirname(schemaPath), provider);
1430
+ if (fs.existsSync(providerPath)) {
1431
+ if (fs.statSync(providerPath).isDirectory()) {
1432
+ providerPath = path.join(providerPath, "index.js");
1433
+ }
1434
+ pluginModelFile = path.resolve(path.dirname(providerPath), PLUGIN_MODULE_NAME);
1435
+ if (!fs.existsSync(pluginModelFile)) {
1436
+ pluginModelFile = findUp([
1437
+ PLUGIN_MODULE_NAME
1438
+ ], path.dirname(providerPath));
1439
+ }
1440
+ }
1441
+ if (!pluginModelFile) {
1442
+ if (typeof import.meta.resolve === "function") {
1443
+ try {
1444
+ const resolvedUrl = import.meta.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
1445
+ pluginModelFile = fileURLToPath(resolvedUrl);
1446
+ } catch {
1447
+ }
1448
+ }
1449
+ }
1450
+ if (!pluginModelFile) {
1451
+ try {
1452
+ const require2 = createRequire(pathToFileURL(schemaPath));
1453
+ pluginModelFile = require2.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
1454
+ } catch {
1455
+ }
1456
+ }
1457
+ if (pluginModelFile && fs.existsSync(pluginModelFile)) {
1458
+ result.push(pluginModelFile);
1459
+ }
1460
+ }
1461
+ return result;
1462
+ }
1463
+ __name(getPluginDocuments, "getPluginDocuments");
1464
+ function findUp(names, cwd = process.cwd(), multiple = false, result = []) {
1465
+ if (!names.some((name) => !!name)) {
1466
+ return void 0;
1467
+ }
1468
+ const target = names.find((name) => fs.existsSync(path.join(cwd, name)));
1469
+ if (multiple === false && target) {
1470
+ return path.join(cwd, target);
1471
+ }
1472
+ if (target) {
1473
+ result.push(path.join(cwd, target));
1474
+ }
1475
+ const up = path.resolve(cwd, "..");
1476
+ if (up === cwd) {
1477
+ return multiple && result.length > 0 ? result : void 0;
1478
+ }
1479
+ return findUp(names, up, multiple, result);
1480
+ }
1481
+ __name(findUp, "findUp");
1401
1482
  function findRootNode(node) {
1402
1483
  while (node.$container) {
1403
1484
  node = node.$container;
@@ -1415,6 +1496,7 @@ export {
1415
1496
  getAllLoadedAndReachableDataModelsAndTypeDefs,
1416
1497
  getAllLoadedDataModelsAndTypeDefs,
1417
1498
  getAttribute,
1499
+ getAttributeArg,
1418
1500
  getAttributeArgLiteral,
1419
1501
  getAuthDecl,
1420
1502
  getContainingDataModel,
@@ -1427,20 +1509,20 @@ export {
1427
1509
  getModelIdFields,
1428
1510
  getModelUniqueFields,
1429
1511
  getObjectLiteral,
1512
+ getPluginDocuments,
1430
1513
  getRecursiveBases,
1431
1514
  getStringLiteral,
1432
1515
  getUniqueFields,
1433
1516
  hasAttribute,
1434
1517
  isAuthInvocation,
1435
1518
  isAuthOrAuthMemberAccess,
1519
+ isBeforeInvocation,
1436
1520
  isCheckInvocation,
1437
1521
  isCollectionPredicate,
1438
1522
  isDataFieldReference,
1439
1523
  isDelegateModel,
1440
1524
  isEnumFieldReference,
1441
1525
  isFromStdlib,
1442
- isFutureExpr,
1443
- isFutureInvocation,
1444
1526
  isMemberContainer,
1445
1527
  isRelationshipField,
1446
1528
  mapBuiltinTypeToExpressionType,