@zenstackhq/language 3.0.0-beta.1 → 3.0.0-beta.10

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;
@@ -1225,7 +1233,7 @@ function getFieldReference(expr) {
1225
1233
  }
1226
1234
  __name(getFieldReference, "getFieldReference");
1227
1235
  function isCheckInvocation(node) {
1228
- return isInvocationExpr(node) && node.function.ref?.name === "check" && isFromStdlib(node.function.ref);
1236
+ return isInvocationExpr(node) && node.function.ref?.name === "check";
1229
1237
  }
1230
1238
  __name(isCheckInvocation, "isCheckInvocation");
1231
1239
  function resolveTransitiveImports(documents, model) {
@@ -1302,10 +1310,10 @@ function getAuthDecl(decls) {
1302
1310
  return authModel;
1303
1311
  }
1304
1312
  __name(getAuthDecl, "getAuthDecl");
1305
- function isFutureInvocation(node) {
1306
- return isInvocationExpr(node) && node.function.ref?.name === "future" && isFromStdlib(node.function.ref);
1313
+ function isBeforeInvocation(node) {
1314
+ return isInvocationExpr(node) && node.function.ref?.name === "before";
1307
1315
  }
1308
- __name(isFutureInvocation, "isFutureInvocation");
1316
+ __name(isBeforeInvocation, "isBeforeInvocation");
1309
1317
  function isCollectionPredicate(node) {
1310
1318
  return isBinaryExpr(node) && [
1311
1319
  "?",
@@ -1360,12 +1368,14 @@ function getAllFields(decl, includeIgnored = false, seen = /* @__PURE__ */ new S
1360
1368
  seen.add(decl);
1361
1369
  const fields = [];
1362
1370
  for (const mixin of decl.mixins) {
1363
- invariant(mixin.ref, `Mixin ${mixin.$refText} is not resolved`);
1364
- fields.push(...getAllFields(mixin.ref, includeIgnored, seen));
1371
+ if (mixin.ref) {
1372
+ fields.push(...getAllFields(mixin.ref, includeIgnored, seen));
1373
+ }
1365
1374
  }
1366
1375
  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));
1376
+ if (decl.baseModel.ref) {
1377
+ fields.push(...getAllFields(decl.baseModel.ref, includeIgnored, seen));
1378
+ }
1369
1379
  }
1370
1380
  fields.push(...decl.fields.filter((f) => includeIgnored || !hasAttribute(f, "@ignore")));
1371
1381
  return fields;
@@ -1378,12 +1388,14 @@ function getAllAttributes(decl, seen = /* @__PURE__ */ new Set()) {
1378
1388
  seen.add(decl);
1379
1389
  const attributes = [];
1380
1390
  for (const mixin of decl.mixins) {
1381
- invariant(mixin.ref, `Mixin ${mixin.$refText} is not resolved`);
1382
- attributes.push(...getAllAttributes(mixin.ref, seen));
1391
+ if (mixin.ref) {
1392
+ attributes.push(...getAllAttributes(mixin.ref, seen));
1393
+ }
1383
1394
  }
1384
1395
  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));
1396
+ if (decl.baseModel.ref) {
1397
+ attributes.push(...getAllAttributes(decl.baseModel.ref, seen));
1398
+ }
1387
1399
  }
1388
1400
  attributes.push(...decl.attributes);
1389
1401
  return attributes;
@@ -1398,6 +1410,71 @@ function getDocument(node) {
1398
1410
  return result;
1399
1411
  }
1400
1412
  __name(getDocument, "getDocument");
1413
+ function getPluginDocuments(model, schemaPath) {
1414
+ const result = [];
1415
+ for (const decl of model.declarations.filter(isPlugin)) {
1416
+ const providerField = decl.fields.find((f) => f.name === "provider");
1417
+ if (!providerField) {
1418
+ continue;
1419
+ }
1420
+ const provider = getLiteral(providerField.value);
1421
+ if (!provider) {
1422
+ continue;
1423
+ }
1424
+ let pluginModelFile;
1425
+ let providerPath = path.resolve(path.dirname(schemaPath), provider);
1426
+ if (fs.existsSync(providerPath)) {
1427
+ if (fs.statSync(providerPath).isDirectory()) {
1428
+ providerPath = path.join(providerPath, "index.js");
1429
+ }
1430
+ pluginModelFile = path.resolve(path.dirname(providerPath), PLUGIN_MODULE_NAME);
1431
+ if (!fs.existsSync(pluginModelFile)) {
1432
+ pluginModelFile = findUp([
1433
+ PLUGIN_MODULE_NAME
1434
+ ], path.dirname(providerPath));
1435
+ }
1436
+ }
1437
+ if (!pluginModelFile) {
1438
+ if (typeof import.meta.resolve === "function") {
1439
+ try {
1440
+ const resolvedUrl = import.meta.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
1441
+ pluginModelFile = fileURLToPath(resolvedUrl);
1442
+ } catch {
1443
+ }
1444
+ }
1445
+ }
1446
+ if (!pluginModelFile) {
1447
+ try {
1448
+ const require2 = createRequire(pathToFileURL(schemaPath));
1449
+ pluginModelFile = require2.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
1450
+ } catch {
1451
+ }
1452
+ }
1453
+ if (pluginModelFile && fs.existsSync(pluginModelFile)) {
1454
+ result.push(pluginModelFile);
1455
+ }
1456
+ }
1457
+ return result;
1458
+ }
1459
+ __name(getPluginDocuments, "getPluginDocuments");
1460
+ function findUp(names, cwd = process.cwd(), multiple = false, result = []) {
1461
+ if (!names.some((name) => !!name)) {
1462
+ return void 0;
1463
+ }
1464
+ const target = names.find((name) => fs.existsSync(path.join(cwd, name)));
1465
+ if (multiple === false && target) {
1466
+ return path.join(cwd, target);
1467
+ }
1468
+ if (target) {
1469
+ result.push(path.join(cwd, target));
1470
+ }
1471
+ const up = path.resolve(cwd, "..");
1472
+ if (up === cwd) {
1473
+ return multiple && result.length > 0 ? result : void 0;
1474
+ }
1475
+ return findUp(names, up, multiple, result);
1476
+ }
1477
+ __name(findUp, "findUp");
1401
1478
  function findRootNode(node) {
1402
1479
  while (node.$container) {
1403
1480
  node = node.$container;
@@ -1427,20 +1504,20 @@ export {
1427
1504
  getModelIdFields,
1428
1505
  getModelUniqueFields,
1429
1506
  getObjectLiteral,
1507
+ getPluginDocuments,
1430
1508
  getRecursiveBases,
1431
1509
  getStringLiteral,
1432
1510
  getUniqueFields,
1433
1511
  hasAttribute,
1434
1512
  isAuthInvocation,
1435
1513
  isAuthOrAuthMemberAccess,
1514
+ isBeforeInvocation,
1436
1515
  isCheckInvocation,
1437
1516
  isCollectionPredicate,
1438
1517
  isDataFieldReference,
1439
1518
  isDelegateModel,
1440
1519
  isEnumFieldReference,
1441
1520
  isFromStdlib,
1442
- isFutureExpr,
1443
- isFutureInvocation,
1444
1521
  isMemberContainer,
1445
1522
  isRelationshipField,
1446
1523
  mapBuiltinTypeToExpressionType,