@zenstackhq/language 3.0.0-alpha.14 → 3.0.0-alpha.16

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.js CHANGED
@@ -8,45 +8,12 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
8
8
  });
9
9
 
10
10
  // src/index.ts
11
- import { URI as URI3 } from "langium";
11
+ import { isAstNode, URI as URI3 } from "langium";
12
12
  import { NodeFileSystem } from "langium/node";
13
13
  import fs3 from "fs";
14
14
  import path3 from "path";
15
15
  import { fileURLToPath as fileURLToPath2 } from "url";
16
16
 
17
- // src/constants.ts
18
- var SUPPORTED_PROVIDERS = [
19
- "sqlite",
20
- "postgresql"
21
- ];
22
- var SCALAR_TYPES = [
23
- "String",
24
- "Int",
25
- "Float",
26
- "Decimal",
27
- "BigInt",
28
- "Boolean",
29
- "Bytes",
30
- "DateTime"
31
- ];
32
- var STD_LIB_MODULE_NAME = "stdlib.zmodel";
33
- var PLUGIN_MODULE_NAME = "plugin.zmodel";
34
- var IssueCodes = /* @__PURE__ */ function(IssueCodes2) {
35
- IssueCodes2["MissingOppositeRelation"] = "miss-opposite-relation";
36
- return IssueCodes2;
37
- }({});
38
- var ExpressionContext = /* @__PURE__ */ function(ExpressionContext2) {
39
- ExpressionContext2["DefaultValue"] = "DefaultValue";
40
- ExpressionContext2["AccessPolicy"] = "AccessPolicy";
41
- ExpressionContext2["ValidationRule"] = "ValidationRule";
42
- ExpressionContext2["Index"] = "Index";
43
- return ExpressionContext2;
44
- }({});
45
-
46
- // src/module.ts
47
- import { inject } from "langium";
48
- import { createDefaultModule, createDefaultSharedModule } from "langium/lsp";
49
-
50
17
  // src/generated/ast.ts
51
18
  import * as langium from "langium";
52
19
  var AbstractDeclaration = "AbstractDeclaration";
@@ -1045,6 +1012,39 @@ var ZModelAstReflection = class extends langium.AbstractAstReflection {
1045
1012
  };
1046
1013
  var reflection = new ZModelAstReflection();
1047
1014
 
1015
+ // src/constants.ts
1016
+ var SUPPORTED_PROVIDERS = [
1017
+ "sqlite",
1018
+ "postgresql"
1019
+ ];
1020
+ var SCALAR_TYPES = [
1021
+ "String",
1022
+ "Int",
1023
+ "Float",
1024
+ "Decimal",
1025
+ "BigInt",
1026
+ "Boolean",
1027
+ "Bytes",
1028
+ "DateTime"
1029
+ ];
1030
+ var STD_LIB_MODULE_NAME = "stdlib.zmodel";
1031
+ var PLUGIN_MODULE_NAME = "plugin.zmodel";
1032
+ var IssueCodes = /* @__PURE__ */ function(IssueCodes2) {
1033
+ IssueCodes2["MissingOppositeRelation"] = "miss-opposite-relation";
1034
+ return IssueCodes2;
1035
+ }({});
1036
+ var ExpressionContext = /* @__PURE__ */ function(ExpressionContext2) {
1037
+ ExpressionContext2["DefaultValue"] = "DefaultValue";
1038
+ ExpressionContext2["AccessPolicy"] = "AccessPolicy";
1039
+ ExpressionContext2["ValidationRule"] = "ValidationRule";
1040
+ ExpressionContext2["Index"] = "Index";
1041
+ return ExpressionContext2;
1042
+ }({});
1043
+
1044
+ // src/module.ts
1045
+ import { inject } from "langium";
1046
+ import { createDefaultModule, createDefaultSharedModule } from "langium/lsp";
1047
+
1048
1048
  // src/generated/grammar.ts
1049
1049
  import { loadGrammarFromJson } from "langium";
1050
1050
  var loadedZModelGrammar;
@@ -5400,34 +5400,24 @@ function resolveImport(documents, imp) {
5400
5400
  }
5401
5401
  __name(resolveImport, "resolveImport");
5402
5402
  function resolveImportUri(imp) {
5403
- if (!imp.path) return void 0;
5404
- if (!imp.path.endsWith(".zmodel")) {
5405
- imp.path += ".zmodel";
5406
- }
5407
- if (!imp.path.startsWith(".") && // Respect relative paths
5408
- !path.isAbsolute(imp.path)) {
5409
- const contextPath = imp.$container.$document ? path.dirname(imp.$container.$document.uri.fsPath) : process.cwd();
5410
- imp.path = findNodeModulesFile(imp.path, contextPath) ?? imp.path;
5403
+ if (!imp.path) {
5404
+ return void 0;
5411
5405
  }
5412
5406
  const doc = AstUtils.getDocument(imp);
5413
5407
  const dir = path.dirname(doc.uri.fsPath);
5414
- return URI.file(path.resolve(dir, imp.path));
5408
+ const importPath = imp.path.endsWith(".zmodel") ? imp.path : `${imp.path}.zmodel`;
5409
+ return URI.file(path.resolve(dir, importPath));
5415
5410
  }
5416
5411
  __name(resolveImportUri, "resolveImportUri");
5417
- function findNodeModulesFile(name, cwd = process.cwd()) {
5418
- if (!name) return void 0;
5419
- try {
5420
- const resolvedPath = __require.resolve(name, {
5421
- paths: [
5422
- cwd
5423
- ]
5424
- });
5425
- return resolvedPath;
5426
- } catch {
5427
- return void 0;
5412
+ function getDataModelAndTypeDefs(model, includeIgnored = false) {
5413
+ const r = model.declarations.filter((d) => isDataModel(d) || isTypeDef(d));
5414
+ if (includeIgnored) {
5415
+ return r;
5416
+ } else {
5417
+ return r.filter((model2) => !hasAttribute(model2, "@@ignore"));
5428
5418
  }
5429
5419
  }
5430
- __name(findNodeModulesFile, "findNodeModulesFile");
5420
+ __name(getDataModelAndTypeDefs, "getDataModelAndTypeDefs");
5431
5421
  function getAllDeclarationsIncludingImports(documents, model) {
5432
5422
  const imports = resolveTransitiveImports(documents, model);
5433
5423
  return model.declarations.concat(...imports.map((imp) => imp.declarations));
@@ -5528,6 +5518,22 @@ function getAllAttributes(decl, seen = /* @__PURE__ */ new Set()) {
5528
5518
  return attributes;
5529
5519
  }
5530
5520
  __name(getAllAttributes, "getAllAttributes");
5521
+ function getDocument(node) {
5522
+ const rootNode = findRootNode(node);
5523
+ const result = rootNode.$document;
5524
+ if (!result) {
5525
+ throw new Error("AST node has no document.");
5526
+ }
5527
+ return result;
5528
+ }
5529
+ __name(getDocument, "getDocument");
5530
+ function findRootNode(node) {
5531
+ while (node.$container) {
5532
+ node = node.$container;
5533
+ }
5534
+ return node;
5535
+ }
5536
+ __name(findRootNode, "findRootNode");
5531
5537
 
5532
5538
  // src/validators/attribute-application-validator.ts
5533
5539
  function _ts_decorate(decorators, target, key, desc) {
@@ -6884,8 +6890,8 @@ var SchemaValidator = class {
6884
6890
  validateImports(model, accept) {
6885
6891
  model.imports.forEach((imp) => {
6886
6892
  const importedModel = resolveImport(this.documents, imp);
6887
- const importPath = imp.path.endsWith(".zmodel") ? imp.path : `${imp.path}.zmodel`;
6888
6893
  if (!importedModel) {
6894
+ const importPath = imp.path.endsWith(".zmodel") ? imp.path : `${imp.path}.zmodel`;
6889
6895
  accept("error", `Cannot find model file ${importPath}`, {
6890
6896
  node: imp
6891
6897
  });
@@ -7693,10 +7699,16 @@ async function loadDocument(fileName, pluginModelFiles = []) {
7693
7699
  const pluginDocs = await Promise.all(pluginModelFiles.map((file) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI3.file(path3.resolve(file)))));
7694
7700
  const langiumDocuments = services.shared.workspace.LangiumDocuments;
7695
7701
  const document = await langiumDocuments.getOrCreateDocument(URI3.file(path3.resolve(fileName)));
7702
+ const importedURIs = await loadImports(document, langiumDocuments);
7703
+ const importedDocuments = [];
7704
+ for (const uri of importedURIs) {
7705
+ importedDocuments.push(await langiumDocuments.getOrCreateDocument(uri));
7706
+ }
7696
7707
  await services.shared.workspace.DocumentBuilder.build([
7697
7708
  stdLib,
7698
7709
  ...pluginDocs,
7699
- document
7710
+ document,
7711
+ ...importedDocuments
7700
7712
  ], {
7701
7713
  validation: true
7702
7714
  });
@@ -7723,6 +7735,20 @@ async function loadDocument(fileName, pluginModelFiles = []) {
7723
7735
  warnings
7724
7736
  };
7725
7737
  }
7738
+ const model = document.parseResult.value;
7739
+ const imported = mergeImportsDeclarations(langiumDocuments, model);
7740
+ imported.forEach((model2) => {
7741
+ langiumDocuments.deleteDocument(model2.$document.uri);
7742
+ services.shared.workspace.IndexManager.remove(model2.$document.uri);
7743
+ });
7744
+ const additionalErrors = validationAfterImportMerge(model);
7745
+ if (additionalErrors.length > 0) {
7746
+ return {
7747
+ success: false,
7748
+ errors: additionalErrors,
7749
+ warnings
7750
+ };
7751
+ }
7726
7752
  return {
7727
7753
  success: true,
7728
7754
  model: document.parseResult.value,
@@ -7730,6 +7756,64 @@ async function loadDocument(fileName, pluginModelFiles = []) {
7730
7756
  };
7731
7757
  }
7732
7758
  __name(loadDocument, "loadDocument");
7759
+ async function loadImports(document, documents, uris = /* @__PURE__ */ new Set()) {
7760
+ const uriString = document.uri.toString();
7761
+ if (!uris.has(uriString)) {
7762
+ uris.add(uriString);
7763
+ const model = document.parseResult.value;
7764
+ for (const imp of model.imports) {
7765
+ const importedModel = resolveImport(documents, imp);
7766
+ if (importedModel) {
7767
+ const importedDoc = getDocument(importedModel);
7768
+ await loadImports(importedDoc, documents, uris);
7769
+ }
7770
+ }
7771
+ }
7772
+ return Array.from(uris).filter((x) => uriString != x).map((e) => URI3.parse(e));
7773
+ }
7774
+ __name(loadImports, "loadImports");
7775
+ function mergeImportsDeclarations(documents, model) {
7776
+ const importedModels = resolveTransitiveImports(documents, model);
7777
+ const importedDeclarations = importedModels.flatMap((m) => m.declarations);
7778
+ model.declarations.push(...importedDeclarations);
7779
+ model.imports = [];
7780
+ linkContentToContainer(model);
7781
+ return importedModels;
7782
+ }
7783
+ __name(mergeImportsDeclarations, "mergeImportsDeclarations");
7784
+ function linkContentToContainer(node) {
7785
+ for (const [name, value] of Object.entries(node)) {
7786
+ if (!name.startsWith("$")) {
7787
+ if (Array.isArray(value)) {
7788
+ value.forEach((item, index) => {
7789
+ if (isAstNode(item)) {
7790
+ item.$container = node;
7791
+ item.$containerProperty = name;
7792
+ item.$containerIndex = index;
7793
+ }
7794
+ });
7795
+ } else if (isAstNode(value)) {
7796
+ value.$container = node;
7797
+ value.$containerProperty = name;
7798
+ }
7799
+ }
7800
+ }
7801
+ }
7802
+ __name(linkContentToContainer, "linkContentToContainer");
7803
+ function validationAfterImportMerge(model) {
7804
+ const errors = [];
7805
+ const dataSources = model.declarations.filter((d) => isDataSource(d));
7806
+ if (dataSources.length > 1) {
7807
+ errors.push("Validation error: Multiple datasource declarations are not allowed");
7808
+ }
7809
+ const decls = getDataModelAndTypeDefs(model, true);
7810
+ const authDecls = decls.filter((d) => hasAttribute(d, "@@auth"));
7811
+ if (authDecls.length > 1) {
7812
+ errors.push("Validation error: Multiple `@@auth` declarations are not allowed");
7813
+ }
7814
+ return errors;
7815
+ }
7816
+ __name(validationAfterImportMerge, "validationAfterImportMerge");
7733
7817
  export {
7734
7818
  DocumentLoadError,
7735
7819
  ZModelLanguageMetaData,