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

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.d.cts CHANGED
@@ -80,7 +80,7 @@ declare function createZModelServices(): {
80
80
  declare class DocumentLoadError extends Error {
81
81
  constructor(message: string);
82
82
  }
83
- declare function loadDocument(fileName: string, pluginModelFiles?: string[]): Promise<{
83
+ declare function loadDocument(fileName: string, additionalModelFiles?: string[]): Promise<{
84
84
  success: true;
85
85
  model: Model;
86
86
  warnings: string[];
package/dist/index.d.ts CHANGED
@@ -80,7 +80,7 @@ declare function createZModelServices(): {
80
80
  declare class DocumentLoadError extends Error {
81
81
  constructor(message: string);
82
82
  }
83
- declare function loadDocument(fileName: string, pluginModelFiles?: string[]): Promise<{
83
+ declare function loadDocument(fileName: string, additionalModelFiles?: string[]): Promise<{
84
84
  success: true;
85
85
  model: Model;
86
86
  warnings: string[];
package/dist/index.js CHANGED
@@ -5208,8 +5208,14 @@ function getRecursiveBases(decl, includeDelegate = true, seen = /* @__PURE__ */
5208
5208
  return result;
5209
5209
  }
5210
5210
  seen.add(decl);
5211
- decl.mixins.forEach((mixin) => {
5212
- const baseDecl = decl.$container.declarations.find((d) => isTypeDef(d) && d.name === mixin.$refText);
5211
+ const bases = [
5212
+ ...decl.mixins,
5213
+ ...isDataModel(decl) && decl.baseModel ? [
5214
+ decl.baseModel
5215
+ ] : []
5216
+ ];
5217
+ bases.forEach((base) => {
5218
+ const baseDecl = decl.$container.declarations.find((d) => isTypeDef(d) || isDataModel(d) && d.name === base.$refText);
5213
5219
  if (baseDecl) {
5214
5220
  if (!includeDelegate && isDelegateModel(baseDecl)) {
5215
5221
  return;
@@ -5852,6 +5858,10 @@ function assignableToAttributeParam(arg, param, attr) {
5852
5858
  let dstIsArray = param.type.array;
5853
5859
  if (dstType === "ContextType") {
5854
5860
  if (isDataField(attr.$container)) {
5861
+ const dstIsTypedJson = hasAttribute(attr.$container, "@json");
5862
+ if (dstIsTypedJson && attr.decl.ref?.name === "@default") {
5863
+ return argResolvedType.decl === "String";
5864
+ }
5855
5865
  dstIsArray = attr.$container.type.array;
5856
5866
  }
5857
5867
  }
@@ -5943,6 +5953,9 @@ function isValidAttributeTarget(attrDecl, targetDecl) {
5943
5953
  case "TypeDefField":
5944
5954
  allowed = allowed || isTypeDef(targetDecl.type.reference?.ref);
5945
5955
  break;
5956
+ case "ListField":
5957
+ allowed = allowed || !isDataModel(targetDecl.type.reference?.ref) && targetDecl.type.array;
5958
+ break;
5946
5959
  default:
5947
5960
  break;
5948
5961
  }
@@ -6721,7 +6734,7 @@ var FunctionInvocationValidator = class {
6721
6734
  }
6722
6735
  curr = curr.$container;
6723
6736
  }
6724
- const exprContext = match(containerAttribute?.decl.$refText).with("@default", () => ExpressionContext.DefaultValue).with(P.union("@@allow", "@@deny", "@allow", "@deny"), () => ExpressionContext.AccessPolicy).with("@@validate", () => ExpressionContext.ValidationRule).with("@@index", () => ExpressionContext.Index).otherwise(() => void 0);
6737
+ const exprContext = this.getExpressionContext(containerAttribute);
6725
6738
  const funcAllowedContext = getFunctionExpressionContext(funcDecl);
6726
6739
  if (exprContext && !funcAllowedContext.includes(exprContext)) {
6727
6740
  accept("error", `function "${funcDecl.name}" is not allowed in the current context: ${exprContext}`, {
@@ -6753,6 +6766,18 @@ var FunctionInvocationValidator = class {
6753
6766
  checker.value.call(this, expr, accept);
6754
6767
  }
6755
6768
  }
6769
+ getExpressionContext(containerAttribute) {
6770
+ if (!containerAttribute) {
6771
+ return void 0;
6772
+ }
6773
+ if (this.isValidationAttribute(containerAttribute)) {
6774
+ return ExpressionContext.ValidationRule;
6775
+ }
6776
+ return match(containerAttribute?.decl.$refText).with("@default", () => ExpressionContext.DefaultValue).with(P.union("@@allow", "@@deny", "@allow", "@deny"), () => ExpressionContext.AccessPolicy).with("@@index", () => ExpressionContext.Index).otherwise(() => void 0);
6777
+ }
6778
+ isValidationAttribute(attr) {
6779
+ return !!attr.decl.ref?.attributes.some((attr2) => attr2.decl.$refText === "@@@validation");
6780
+ }
6756
6781
  validateArgs(funcDecl, args, accept) {
6757
6782
  let success = true;
6758
6783
  for (let i = 0; i < funcDecl.params.length; i++) {
@@ -6813,6 +6838,43 @@ var FunctionInvocationValidator = class {
6813
6838
  }
6814
6839
  return true;
6815
6840
  }
6841
+ _checkLength(expr, accept) {
6842
+ const msg = "argument must be a string or list field";
6843
+ const fieldArg = expr.args[0].value;
6844
+ if (!isDataFieldReference(fieldArg)) {
6845
+ accept("error", msg, {
6846
+ node: expr.args[0]
6847
+ });
6848
+ return;
6849
+ }
6850
+ if (isDataModel(fieldArg.$resolvedType?.decl)) {
6851
+ accept("error", msg, {
6852
+ node: expr.args[0]
6853
+ });
6854
+ return;
6855
+ }
6856
+ if (!fieldArg.$resolvedType?.array && fieldArg.$resolvedType?.decl !== "String") {
6857
+ accept("error", msg, {
6858
+ node: expr.args[0]
6859
+ });
6860
+ }
6861
+ }
6862
+ _checkRegex(expr, accept) {
6863
+ const regex = expr.args[1]?.value;
6864
+ if (!isStringLiteral(regex)) {
6865
+ accept("error", "second argument must be a string literal", {
6866
+ node: expr.args[1]
6867
+ });
6868
+ return;
6869
+ }
6870
+ try {
6871
+ new RegExp(regex.value);
6872
+ } catch (e) {
6873
+ accept("error", "invalid regular expression: " + e.message, {
6874
+ node: expr.args[1]
6875
+ });
6876
+ }
6877
+ }
6816
6878
  // TODO: move this to policy plugin
6817
6879
  _checkCheck(expr, accept) {
6818
6880
  let valid = true;
@@ -6885,6 +6947,24 @@ var FunctionInvocationValidator = class {
6885
6947
  }
6886
6948
  }
6887
6949
  };
6950
+ _ts_decorate2([
6951
+ func("length"),
6952
+ _ts_metadata2("design:type", Function),
6953
+ _ts_metadata2("design:paramtypes", [
6954
+ typeof InvocationExpr === "undefined" ? Object : InvocationExpr,
6955
+ typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
6956
+ ]),
6957
+ _ts_metadata2("design:returntype", void 0)
6958
+ ], FunctionInvocationValidator.prototype, "_checkLength", null);
6959
+ _ts_decorate2([
6960
+ func("regex"),
6961
+ _ts_metadata2("design:type", Function),
6962
+ _ts_metadata2("design:paramtypes", [
6963
+ typeof InvocationExpr === "undefined" ? Object : InvocationExpr,
6964
+ typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
6965
+ ]),
6966
+ _ts_metadata2("design:returntype", void 0)
6967
+ ], FunctionInvocationValidator.prototype, "_checkRegex", null);
6888
6968
  _ts_decorate2([
6889
6969
  func("check"),
6890
6970
  _ts_metadata2("design:type", Function),
@@ -7435,7 +7515,7 @@ var ZModelScopeComputation = class extends DefaultScopeComputation {
7435
7515
  }
7436
7516
  processNode(node, document, scopes) {
7437
7517
  super.processNode(node, document, scopes);
7438
- if (isDataModel(node)) {
7518
+ if (isDataModel(node) || isTypeDef(node)) {
7439
7519
  const bases = getRecursiveBases(node);
7440
7520
  for (const base of bases) {
7441
7521
  for (const field of base.fields) {
@@ -7736,7 +7816,7 @@ var DocumentLoadError = class extends Error {
7736
7816
  super(message);
7737
7817
  }
7738
7818
  };
7739
- async function loadDocument(fileName, pluginModelFiles = []) {
7819
+ async function loadDocument(fileName, additionalModelFiles = []) {
7740
7820
  const { ZModelLanguage: services } = createZModelServices();
7741
7821
  const extensions = services.LanguageMetaData.fileExtensions;
7742
7822
  if (!extensions.includes(path3.extname(fileName))) {
@@ -7759,7 +7839,7 @@ async function loadDocument(fileName, pluginModelFiles = []) {
7759
7839
  }
7760
7840
  const _dirname = typeof __dirname !== "undefined" ? __dirname : path3.dirname(fileURLToPath2(import.meta.url));
7761
7841
  const stdLib = await services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI3.file(path3.resolve(path3.join(_dirname, "../res", STD_LIB_MODULE_NAME))));
7762
- const pluginDocs = await Promise.all(pluginModelFiles.map((file) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI3.file(path3.resolve(file)))));
7842
+ const pluginDocs = await Promise.all(additionalModelFiles.map((file) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI3.file(path3.resolve(file)))));
7763
7843
  const langiumDocuments = services.shared.workspace.LangiumDocuments;
7764
7844
  const document = await langiumDocuments.getOrCreateDocument(URI3.file(path3.resolve(fileName)));
7765
7845
  const importedURIs = await loadImports(document, langiumDocuments);