@zenstackhq/language 3.0.0-beta.7 → 3.0.0-beta.8

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/index.cjs CHANGED
@@ -5223,10 +5223,6 @@ function isRelationshipField(field) {
5223
5223
  return isDataModel(field.type.reference?.ref);
5224
5224
  }
5225
5225
  __name(isRelationshipField, "isRelationshipField");
5226
- function isFutureExpr(node) {
5227
- return isInvocationExpr(node) && node.function.ref?.name === "future" && isFromStdlib(node.function.ref);
5228
- }
5229
- __name(isFutureExpr, "isFutureExpr");
5230
5226
  function isDelegateModel(node) {
5231
5227
  return isDataModel(node) && hasAttribute(node, "@@delegate");
5232
5228
  }
@@ -5462,10 +5458,10 @@ function getAuthDecl(decls) {
5462
5458
  return authModel;
5463
5459
  }
5464
5460
  __name(getAuthDecl, "getAuthDecl");
5465
- function isFutureInvocation(node) {
5466
- return isInvocationExpr(node) && node.function.ref?.name === "future" && isFromStdlib(node.function.ref);
5461
+ function isBeforeInvocation(node) {
5462
+ return isInvocationExpr(node) && node.function.ref?.name === "before" && isFromStdlib(node.function.ref);
5467
5463
  }
5468
- __name(isFutureInvocation, "isFutureInvocation");
5464
+ __name(isBeforeInvocation, "isBeforeInvocation");
5469
5465
  function isCollectionPredicate(node) {
5470
5466
  return isBinaryExpr(node) && [
5471
5467
  "?",
@@ -5697,12 +5693,21 @@ var AttributeApplicationValidator = class {
5697
5693
  "create",
5698
5694
  "read",
5699
5695
  "update",
5696
+ "post-update",
5700
5697
  "delete",
5701
5698
  "all"
5702
5699
  ], attr, accept);
5703
5700
  if ((kind === "create" || kind === "all") && attr.args[1]?.value) {
5704
5701
  this.rejectNonOwnedRelationInExpression(attr.args[1].value, accept);
5705
5702
  }
5703
+ if (kind !== "post-update" && attr.args[1]?.value) {
5704
+ const beforeCall = import_langium3.AstUtils.streamAst(attr.args[1]?.value).find(isBeforeInvocation);
5705
+ if (beforeCall) {
5706
+ accept("error", `"before()" is only allowed in "post-update" policy rules`, {
5707
+ node: beforeCall
5708
+ });
5709
+ }
5710
+ }
5706
5711
  }
5707
5712
  rejectNonOwnedRelationInExpression(expr, accept) {
5708
5713
  const contextModel = import_langium3.AstUtils.getContainerOfType(expr, isDataModel);
@@ -5757,8 +5762,8 @@ var AttributeApplicationValidator = class {
5757
5762
  "all"
5758
5763
  ], attr, accept);
5759
5764
  const expr = attr.args[1]?.value;
5760
- if (expr && import_langium3.AstUtils.streamAst(expr).some((node) => isFutureExpr(node))) {
5761
- accept("error", `"future()" is not allowed in field-level policy rules`, {
5765
+ if (expr && import_langium3.AstUtils.streamAst(expr).some((node) => isBeforeInvocation(node))) {
5766
+ accept("error", `"before()" is not allowed in field-level policy rules`, {
5762
5767
  node: expr
5763
5768
  });
5764
5769
  }
@@ -6519,11 +6524,21 @@ var ExpressionValidator = class {
6519
6524
  }
6520
6525
  }
6521
6526
  switch (expr.$type) {
6527
+ case "MemberAccessExpr":
6528
+ this.validateMemberAccessExpr(expr, accept);
6529
+ break;
6522
6530
  case "BinaryExpr":
6523
6531
  this.validateBinaryExpr(expr, accept);
6524
6532
  break;
6525
6533
  }
6526
6534
  }
6535
+ validateMemberAccessExpr(expr, accept) {
6536
+ if (isBeforeInvocation(expr.operand) && isDataModel(expr.$resolvedType?.decl)) {
6537
+ accept("error", "relation fields cannot be accessed from `before()`", {
6538
+ node: expr
6539
+ });
6540
+ }
6541
+ }
6527
6542
  validateBinaryExpr(expr, accept) {
6528
6543
  switch (expr.operator) {
6529
6544
  case "in": {
@@ -7046,18 +7061,26 @@ var ZModelDocumentBuilder = class extends import_langium7.DefaultDocumentBuilder
7046
7061
  static {
7047
7062
  __name(this, "ZModelDocumentBuilder");
7048
7063
  }
7049
- buildDocuments(documents, options, cancelToken) {
7050
- return super.buildDocuments(documents, {
7051
- ...options,
7052
- validation: (
7053
- // force overriding validation options
7054
- options.validation === false || options.validation === void 0 ? options.validation : {
7055
- stopAfterLexingErrors: true,
7056
- stopAfterParsingErrors: true,
7057
- stopAfterLinkingErrors: true
7058
- }
7059
- )
7060
- }, cancelToken);
7064
+ constructor(services) {
7065
+ super(services);
7066
+ let validationOptions = this.updateBuildOptions.validation;
7067
+ const stopFlags = {
7068
+ stopAfterLinkingErrors: true,
7069
+ stopAfterLexingErrors: true,
7070
+ stopAfterParsingErrors: true
7071
+ };
7072
+ if (validationOptions === true) {
7073
+ validationOptions = stopFlags;
7074
+ } else if (typeof validationOptions === "object") {
7075
+ validationOptions = {
7076
+ ...validationOptions,
7077
+ ...stopFlags
7078
+ };
7079
+ }
7080
+ this.updateBuildOptions = {
7081
+ ...this.updateBuildOptions,
7082
+ validation: validationOptions
7083
+ };
7061
7084
  }
7062
7085
  };
7063
7086
 
@@ -7084,21 +7107,19 @@ var ZModelLinker = class extends import_langium8.DefaultLinker {
7084
7107
  }
7085
7108
  document.state = import_langium8.DocumentState.Linked;
7086
7109
  }
7087
- linkReference(container, property, document, extraScopes) {
7088
- if (this.resolveFromScopeProviders(container, property, document, extraScopes)) {
7110
+ linkReference(refInfo, document, extraScopes) {
7111
+ const defaultRef = refInfo.reference;
7112
+ if (defaultRef._ref) {
7113
+ return;
7114
+ }
7115
+ if (this.resolveFromScopeProviders(refInfo.reference, document, extraScopes)) {
7089
7116
  return;
7090
7117
  }
7091
- const reference = container[property];
7092
- this.doLink({
7093
- reference,
7094
- container,
7095
- property
7096
- }, document);
7118
+ this.doLink(refInfo, document);
7097
7119
  }
7098
7120
  //#endregion
7099
7121
  //#region Expression type resolving
7100
- resolveFromScopeProviders(node, property, document, providers) {
7101
- const reference = node[property];
7122
+ resolveFromScopeProviders(reference, document, providers) {
7102
7123
  for (const provider of providers) {
7103
7124
  const target = provider(reference.$refText);
7104
7125
  if (target) {
@@ -7227,7 +7248,11 @@ var ZModelLinker = class extends import_langium8.DefaultLinker {
7227
7248
  }
7228
7249
  }
7229
7250
  resolveInvocation(node, document, extraScopes) {
7230
- this.linkReference(node, "function", document, extraScopes);
7251
+ this.linkReference({
7252
+ reference: node.function,
7253
+ container: node,
7254
+ property: "function"
7255
+ }, document, extraScopes);
7231
7256
  node.args.forEach((arg) => this.resolve(arg, document, extraScopes));
7232
7257
  if (node.function.ref) {
7233
7258
  const funcDecl = node.function.ref;
@@ -7240,7 +7265,7 @@ var ZModelLinker = class extends import_langium8.DefaultLinker {
7240
7265
  nullable: true
7241
7266
  };
7242
7267
  }
7243
- } else if (isFutureExpr(node)) {
7268
+ } else if (isBeforeInvocation(node)) {
7244
7269
  node.$resolvedType = {
7245
7270
  decl: getContainingDataModel(node)
7246
7271
  };
@@ -7304,7 +7329,7 @@ var ZModelLinker = class extends import_langium8.DefaultLinker {
7304
7329
  if (isArrayExpr(node.value)) {
7305
7330
  node.value.items.forEach((item) => {
7306
7331
  if (isReferenceExpr(item)) {
7307
- const resolved2 = this.resolveFromScopeProviders(item, "target", document, [
7332
+ const resolved2 = this.resolveFromScopeProviders(item.target, document, [
7308
7333
  scopeProvider
7309
7334
  ]);
7310
7335
  if (resolved2) {
@@ -7318,7 +7343,7 @@ var ZModelLinker = class extends import_langium8.DefaultLinker {
7318
7343
  this.resolveToBuiltinTypeOrDecl(node.value, node.value.items[0].$resolvedType.decl, true);
7319
7344
  }
7320
7345
  } else if (isReferenceExpr(node.value)) {
7321
- const resolved2 = this.resolveFromScopeProviders(node.value, "target", document, [
7346
+ const resolved2 = this.resolveFromScopeProviders(node.value.target, document, [
7322
7347
  scopeProvider
7323
7348
  ]);
7324
7349
  if (resolved2) {
@@ -7370,13 +7395,9 @@ var ZModelLinker = class extends import_langium8.DefaultLinker {
7370
7395
  this.resolveDefault(node, document, scopes);
7371
7396
  }
7372
7397
  resolveDefault(node, document, extraScopes) {
7373
- for (const [property, value] of Object.entries(node)) {
7374
- if (!property.startsWith("$")) {
7375
- if ((0, import_langium8.isReference)(value)) {
7376
- this.linkReference(node, property, document, extraScopes);
7377
- }
7378
- }
7379
- }
7398
+ import_langium8.AstUtils.streamReferences(node).forEach((ref) => {
7399
+ this.linkReference(ref, document, extraScopes);
7400
+ });
7380
7401
  for (const child of import_langium8.AstUtils.streamContents(node)) {
7381
7402
  this.resolve(child, document, extraScopes);
7382
7403
  }
@@ -7517,7 +7538,7 @@ var ZModelScopeProvider = class extends import_langium9.DefaultScopeProvider {
7517
7538
  if (isAuthInvocation(operand)) {
7518
7539
  return this.createScopeForAuth(node, globalScope);
7519
7540
  }
7520
- if (isFutureInvocation(operand)) {
7541
+ if (isBeforeInvocation(operand)) {
7521
7542
  return this.createScopeForContainingModel(node, globalScope);
7522
7543
  }
7523
7544
  return import_langium9.EMPTY_SCOPE;
@@ -7786,7 +7807,11 @@ async function loadDocument(fileName, pluginModelFiles = []) {
7786
7807
  document,
7787
7808
  ...importedDocuments
7788
7809
  ], {
7789
- validation: true
7810
+ validation: {
7811
+ stopAfterLexingErrors: true,
7812
+ stopAfterParsingErrors: true,
7813
+ stopAfterLinkingErrors: true
7814
+ }
7790
7815
  });
7791
7816
  const diagnostics = langiumDocuments.all.flatMap((doc) => (doc.diagnostics ?? []).map((diag) => ({
7792
7817
  doc,