@zenstackhq/language 3.8.0-beta.3 → 3.8.1

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
@@ -4008,7 +4008,6 @@ var AttributeApplicationValidator = class {
4008
4008
  if (require_ast.isDataField(targetDecl) && !isValidAttributeTarget(decl, targetDecl)) accept("error", `attribute "${decl.name}" cannot be used on this type of field`, { node: attr });
4009
4009
  this.checkDeprecation(attr, accept);
4010
4010
  this.checkDuplicatedAttributes(attr, accept, contextDataModel);
4011
- this.checkOnceInModel(attr, accept);
4012
4011
  const filledParams = /* @__PURE__ */ new Set();
4013
4012
  for (const arg of attr.args) {
4014
4013
  let paramDecl;
@@ -4054,15 +4053,6 @@ var AttributeApplicationValidator = class {
4054
4053
  if (!attrDecl?.attributes.some((a) => a.decl.ref?.name === "@@@once")) return;
4055
4054
  if ((contextDataModel ? require_utils.getAllAttributes(contextDataModel) : attr.$container.attributes).filter((a) => a.decl.ref === attrDecl && a !== attr).length > 0) accept("error", `Attribute "${attrDecl.name}" can only be applied once`, { node: attr });
4056
4055
  }
4057
- checkOnceInModel(attr, accept) {
4058
- const attrDecl = attr.decl.ref;
4059
- if (!attrDecl?.attributes.some((a) => a.decl.ref?.name === "@@@onceInModel")) return;
4060
- const field = attr.$container;
4061
- if (!require_ast.isDataField(field)) return;
4062
- const dataModel = require_utils.getContainingDataModel(attr);
4063
- if (!dataModel) return;
4064
- if (require_utils.getAllFields(dataModel).filter((f) => f.attributes.some((a) => a.decl.ref === attrDecl)).length > 1) accept("error", `Attribute "${attrDecl.name}" can only be applied to one field per model`, { node: attr });
4065
- }
4066
4056
  _checkModelLevelPolicy(attr, accept) {
4067
4057
  const kind = require_utils.getStringLiteral(attr.args[0]?.value);
4068
4058
  if (!kind) {
@@ -4409,6 +4399,7 @@ var DataModelValidator = class {
4409
4399
  validateDuplicatedDeclarations(dm, require_utils.getAllFields(dm), accept);
4410
4400
  this.validateAttributes(dm, accept);
4411
4401
  this.validateFields(dm, accept);
4402
+ this.validateOnceInModelAttributes(dm, accept);
4412
4403
  if (dm.mixins.length > 0) this.validateMixins(dm, accept);
4413
4404
  this.validateInherits(dm, accept);
4414
4405
  this.validateDelegateMap(dm, accept);
@@ -4457,6 +4448,24 @@ var DataModelValidator = class {
4457
4448
  validateAttributes(dm, accept) {
4458
4449
  require_utils.getAllAttributes(dm).forEach((attr) => validateAttributeApplication(attr, accept, dm));
4459
4450
  }
4451
+ validateOnceInModelAttributes(dm, accept) {
4452
+ const occurrences = /* @__PURE__ */ new Map();
4453
+ for (const field of require_utils.getAllFields(dm)) for (const attr of field.attributes) {
4454
+ const decl = attr.decl.ref;
4455
+ if (decl && require_utils.hasAttribute(decl, "@@@onceInModel")) {
4456
+ const list = occurrences.get(decl) ?? [];
4457
+ list.push(attr);
4458
+ occurrences.set(decl, list);
4459
+ }
4460
+ }
4461
+ for (const [decl, attrs] of occurrences) {
4462
+ if (attrs.length <= 1) continue;
4463
+ const message = `Attribute "${decl.name}" can only be applied to one field per model`;
4464
+ const local = attrs.filter((attr) => dm.fields.includes(attr.$container));
4465
+ if (local.length > 0) local.forEach((attr) => accept("error", message, { node: attr }));
4466
+ else accept("error", message, { node: dm });
4467
+ }
4468
+ }
4460
4469
  parseRelation(field, accept) {
4461
4470
  const relAttr = field.attributes.find((attr) => attr.decl.ref?.name === "@relation");
4462
4471
  let name;