@dbml/parse 8.0.0-alpha.0 → 8.0.0-alpha.11

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.
Files changed (30) hide show
  1. package/dist/dbml-parse.js +22 -26
  2. package/dist/dbml-parse.mjs +499 -340
  3. package/dist/src/compiler/index.d.ts +12 -8
  4. package/dist/src/compiler/projectLayout/layout.d.ts +2 -6
  5. package/dist/src/compiler/queries/container/index.d.ts +5 -5
  6. package/dist/src/compiler/queries/files/reachableFiles.d.ts +1 -0
  7. package/dist/src/compiler/queries/files/usableMembers.d.ts +5 -3
  8. package/dist/src/compiler/queries/pipeline/index.d.ts +2 -2
  9. package/dist/src/compiler/queries/resolutionIndex.d.ts +2 -0
  10. package/dist/src/compiler/queries/transform/index.d.ts +3 -3
  11. package/dist/src/compiler/queries/transform/renameTable.d.ts +4 -6
  12. package/dist/src/compiler/queries/transform/syncDiagramView.d.ts +1 -1
  13. package/dist/src/core/global_modules/records/utils/data/values.d.ts +1 -1
  14. package/dist/src/core/global_modules/records/utils/index.d.ts +1 -0
  15. package/dist/src/core/local_modules/types.d.ts +1 -1
  16. package/dist/src/core/parser/parser.d.ts +2 -0
  17. package/dist/src/core/types/internable.d.ts +0 -12
  18. package/dist/src/core/types/module.d.ts +2 -1
  19. package/dist/src/core/types/symbol/index.d.ts +1 -1
  20. package/dist/src/index.d.ts +4 -4
  21. package/dist/src/services/index.d.ts +2 -2
  22. package/dist/src/services/monarch.d.ts +1 -1
  23. package/dist/src/services/suggestions/utils/useMerger.d.ts +2 -1
  24. package/dist/src/services/types.d.ts +1 -1
  25. package/package.json +4 -3
  26. /package/dist/src/core/global_modules/{program → records}/utils/constraints/fk.d.ts +0 -0
  27. /package/dist/src/core/global_modules/{program → records}/utils/constraints/helper.d.ts +0 -0
  28. /package/dist/src/core/global_modules/{program → records}/utils/constraints/index.d.ts +0 -0
  29. /package/dist/src/core/global_modules/{program → records}/utils/constraints/pk.d.ts +0 -0
  30. /package/dist/src/core/global_modules/{program → records}/utils/constraints/unique.d.ts +0 -0
@@ -1010,6 +1010,10 @@ function baseUniq(r, j, M) {
1010
1010
  return L;
1011
1011
  }
1012
1012
  var _baseUniq_default = baseUniq;
1013
+ function uniq(r) {
1014
+ return r && r.length ? _baseUniq_default(r) : [];
1015
+ }
1016
+ var uniq_default = uniq;
1013
1017
  function uniqBy(r, j) {
1014
1018
  return r && r.length ? _baseUniq_default(r, _baseIteratee_default(j, 2)) : [];
1015
1019
  }
@@ -1257,13 +1261,13 @@ var isAbsolute = function(r) {
1257
1261
  function resolveImportFilepath(r, j) {
1258
1262
  if (!Filepath.isRelative(j)) return;
1259
1263
  let M = Filepath.resolve(r.dirname, j);
1260
- return M.absolute.endsWith(".dbml") ? M : Filepath.from(M.absolute + DBML_EXT);
1264
+ return M.absolute.endsWith(".dbml") ? M : Filepath.from(M.absolute + ".dbml");
1261
1265
  }
1262
1266
  const KEYWORDS_OF_DEFAULT_SETTING = [
1263
1267
  "null",
1264
1268
  "true",
1265
1269
  "false"
1266
- ], DBML_EXT = ".dbml", NUMERIC_LITERAL_PREFIX = ["-", "+"], DEFAULT_SCHEMA_NAME = "public", DEFAULT_ENTRY = Filepath.from("/main.dbml");
1270
+ ], NUMERIC_LITERAL_PREFIX = ["-", "+"], DEFAULT_SCHEMA_NAME = "public", DEFAULT_ENTRY = Filepath.from("/main.dbml");
1267
1271
  let CompileErrorCode = /* @__PURE__ */ function(r) {
1268
1272
  return r[r.UNKNOWN_SYMBOL = 1e3] = "UNKNOWN_SYMBOL", r[r.UNEXPECTED_SYMBOL = 1001] = "UNEXPECTED_SYMBOL", r[r.UNEXPECTED_EOF = 1002] = "UNEXPECTED_EOF", r[r.UNEXPECTED_NEWLINE = 1003] = "UNEXPECTED_NEWLINE", r[r.UNKNOWN_TOKEN = 1004] = "UNKNOWN_TOKEN", r[r.UNEXPECTED_TOKEN = 1005] = "UNEXPECTED_TOKEN", r[r.UNEXPECTED_ELEMENT_DECLARATION = 1006] = "UNEXPECTED_ELEMENT_DECLARATION", r[r.MISSING_SPACES = 1007] = "MISSING_SPACES", r[r.UNKNOWN_PREFIX_OP = 1008] = "UNKNOWN_PREFIX_OP", r[r.INVALID_OPERAND = 1009] = "INVALID_OPERAND", r[r.EMPTY_ATTRIBUTE_NAME = 1010] = "EMPTY_ATTRIBUTE_NAME", r[r.INVALID_ESCAPE_SEQUENCE = 1011] = "INVALID_ESCAPE_SEQUENCE", r[r.INVALID_NAME = 3e3] = "INVALID_NAME", r[r.UNEXPECTED_NAME = 3001] = "UNEXPECTED_NAME", r[r.NAME_NOT_FOUND = 3002] = "NAME_NOT_FOUND", r[r.DUPLICATE_NAME = 3003] = "DUPLICATE_NAME", r[r.INVALID_ALIAS = 3004] = "INVALID_ALIAS", r[r.UNEXPECTED_ALIAS = 3005] = "UNEXPECTED_ALIAS", r[r.UNEXPECTED_SETTINGS = 3006] = "UNEXPECTED_SETTINGS", r[r.INVALID_SETTINGS = 3007] = "INVALID_SETTINGS", r[r.UNEXPECTED_SIMPLE_BODY = 3008] = "UNEXPECTED_SIMPLE_BODY", r[r.UNEXPECTED_COMPLEX_BODY = 3009] = "UNEXPECTED_COMPLEX_BODY", r[r.INVALID_TABLE_CONTEXT = 3010] = "INVALID_TABLE_CONTEXT", r[r.UNKNOWN_TABLE_SETTING = 3011] = "UNKNOWN_TABLE_SETTING", r[r.DUPLICATE_TABLE_SETTING = 3012] = "DUPLICATE_TABLE_SETTING", r[r.INVALID_TABLEGROUP_CONTEXT = 3013] = "INVALID_TABLEGROUP_CONTEXT", r[r.INVALID_TABLEGROUP_ELEMENT_NAME = 3014] = "INVALID_TABLEGROUP_ELEMENT_NAME", r[r.DUPLICATE_TABLEGROUP_ELEMENT_NAME = 3015] = "DUPLICATE_TABLEGROUP_ELEMENT_NAME", r[r.DUPLICATE_TABLEGROUP_FIELD_NAME = 3016] = "DUPLICATE_TABLEGROUP_FIELD_NAME", r[r.INVALID_TABLEGROUP_FIELD = 3017] = "INVALID_TABLEGROUP_FIELD", r[r.EMPTY_TABLE = 3018] = "EMPTY_TABLE", r[r.INVALID_COLUMN = 3019] = "INVALID_COLUMN", r[r.INVALID_COLUMN_NAME = 3020] = "INVALID_COLUMN_NAME", r[r.UNKNOWN_COLUMN_SETTING = 3021] = "UNKNOWN_COLUMN_SETTING", r[r.INVALID_COLUMN_TYPE = 3022] = "INVALID_COLUMN_TYPE", r[r.DUPLICATE_COLUMN_NAME = 3023] = "DUPLICATE_COLUMN_NAME", r[r.DUPLICATE_COLUMN_SETTING = 3024] = "DUPLICATE_COLUMN_SETTING", r[r.INVALID_COLUMN_SETTING_VALUE = 3025] = "INVALID_COLUMN_SETTING_VALUE", r[r.INVALID_ENUM_CONTEXT = 3026] = "INVALID_ENUM_CONTEXT", r[r.INVALID_ENUM_ELEMENT_NAME = 3027] = "INVALID_ENUM_ELEMENT_NAME", r[r.INVALID_ENUM_ELEMENT = 3028] = "INVALID_ENUM_ELEMENT", r[r.DUPLICATE_ENUM_ELEMENT_NAME = 3029] = "DUPLICATE_ENUM_ELEMENT_NAME", r[r.UNKNOWN_ENUM_ELEMENT_SETTING = 3030] = "UNKNOWN_ENUM_ELEMENT_SETTING", r[r.DUPLICATE_ENUM_ELEMENT_SETTING = 3031] = "DUPLICATE_ENUM_ELEMENT_SETTING", r[r.INVALID_ENUM_ELEMENT_SETTING = 3032] = "INVALID_ENUM_ELEMENT_SETTING", r[r.EMPTY_ENUM = 3033] = "EMPTY_ENUM", r[r.INVALID_REF_CONTEXT = 3034] = "INVALID_REF_CONTEXT", r[r.UNKNOWN_REF_SETTING = 3035] = "UNKNOWN_REF_SETTING", r[r.DUPLICATE_REF_SETTING = 3036] = "DUPLICATE_REF_SETTING", r[r.INVALID_REF_SETTING_VALUE = 3037] = "INVALID_REF_SETTING_VALUE", r[r.INVALID_REF_RELATIONSHIP = 3038] = "INVALID_REF_RELATIONSHIP", r[r.INVALID_REF_FIELD = 3039] = "INVALID_REF_FIELD", r[r.EMPTY_REF = 3040] = "EMPTY_REF", r[r.REF_REDEFINED = 3041] = "REF_REDEFINED", r[r.INVALID_NOTE_CONTEXT = 3042] = "INVALID_NOTE_CONTEXT", r[r.INVALID_NOTE = 3043] = "INVALID_NOTE", r[r.NOTE_REDEFINED = 3044] = "NOTE_REDEFINED", r[r.NOTE_CONTENT_REDEFINED = 3045] = "NOTE_CONTENT_REDEFINED", r[r.EMPTY_NOTE = 3046] = "EMPTY_NOTE", r[r.INVALID_INDEXES_CONTEXT = 3047] = "INVALID_INDEXES_CONTEXT", r[r.INVALID_INDEXES_FIELD = 3048] = "INVALID_INDEXES_FIELD", r[r.INVALID_INDEX = 3049] = "INVALID_INDEX", r[r.UNKNOWN_INDEX_SETTING = 3050] = "UNKNOWN_INDEX_SETTING", r[r.DUPLICATE_INDEX_SETTING = 3051] = "DUPLICATE_INDEX_SETTING", r[r.UNEXPECTED_INDEX_SETTING_VALUE = 3052] = "UNEXPECTED_INDEX_SETTING_VALUE", r[r.INVALID_INDEX_SETTING_VALUE = 3053] = "INVALID_INDEX_SETTING_VALUE", r[r.INVALID_PROJECT_CONTEXT = 3054] = "INVALID_PROJECT_CONTEXT", r[r.PROJECT_REDEFINED = 3055] = "PROJECT_REDEFINED", r[r.INVALID_PROJECT_FIELD = 3056] = "INVALID_PROJECT_FIELD", r[r.INVALID_CUSTOM_CONTEXT = 3057] = "INVALID_CUSTOM_CONTEXT", r[r.INVALID_CUSTOM_ELEMENT_VALUE = 3058] = "INVALID_CUSTOM_ELEMENT_VALUE", r[r.INVALID_ELEMENT_IN_SIMPLE_BODY = 3059] = "INVALID_ELEMENT_IN_SIMPLE_BODY", r[r.INVALID_TABLE_PARTIAL_CONTEXT = 3060] = "INVALID_TABLE_PARTIAL_CONTEXT", r[r.INVALID_TABLE_PARTIAL_ELEMENT_NAME = 3061] = "INVALID_TABLE_PARTIAL_ELEMENT_NAME", r[r.INVALID_TABLE_PARTIAL_SETTING_VALUE = 3062] = "INVALID_TABLE_PARTIAL_SETTING_VALUE", r[r.DUPLICATE_TABLE_PARTIAL_ELEMENT_NAME = 3063] = "DUPLICATE_TABLE_PARTIAL_ELEMENT_NAME", r[r.DUPLICATE_TABLE_PARTIAL_SETTING = 3064] = "DUPLICATE_TABLE_PARTIAL_SETTING", r[r.INVALID_TABLE_PARTIAL_INJECTION = 3065] = "INVALID_TABLE_PARTIAL_INJECTION", r[r.INVALID_TABLE_PARTIAL_INJECTION_OP = 3066] = "INVALID_TABLE_PARTIAL_INJECTION_OP", r[r.INVALID_TABLE_PARTIAL_INJECTION_NAME = 3067] = "INVALID_TABLE_PARTIAL_INJECTION_NAME", r[r.DUPLICATE_TABLE_PARTIAL_INJECTION_NAME = 3068] = "DUPLICATE_TABLE_PARTIAL_INJECTION_NAME", r[r.UNKNOWN_TABLE_PARTIAL_SETTING = 3069] = "UNKNOWN_TABLE_PARTIAL_SETTING", r[r.INVALID_TABLE_SETTING_VALUE = 3070] = "INVALID_TABLE_SETTING_VALUE", r[r.INVALID_CHECKS_CONTEXT = 3071] = "INVALID_CHECKS_CONTEXT", r[r.INVALID_CHECKS_FIELD = 3072] = "INVALID_CHECKS_FIELD", r[r.INVALID_CHECK = 3073] = "INVALID_CHECK", r[r.UNKNOWN_CHECK_SETTING = 3074] = "UNKNOWN_CHECK_SETTING", r[r.DUPLICATE_CHECK_SETTING = 3075] = "DUPLICATE_CHECK_SETTING", r[r.INVALID_CHECK_SETTING_VALUE = 3076] = "INVALID_CHECK_SETTING_VALUE", r[r.INVALID_RECORDS_CONTEXT = 3077] = "INVALID_RECORDS_CONTEXT", r[r.INVALID_RECORDS_NAME = 3078] = "INVALID_RECORDS_NAME", r[r.INVALID_RECORDS_FIELD = 3079] = "INVALID_RECORDS_FIELD", r[r.DUPLICATE_COLUMN_REFERENCES_IN_RECORDS = 3080] = "DUPLICATE_COLUMN_REFERENCES_IN_RECORDS", r[r.DUPLICATE_RECORDS_FOR_TABLE = 3081] = "DUPLICATE_RECORDS_FOR_TABLE", r[r.INVALID_USE_CONTEXT = 3082] = "INVALID_USE_CONTEXT", r[r.INVALID_USE_SPECIFIER_KIND = 3083] = "INVALID_USE_SPECIFIER_KIND", r[r.INVALID_USE_SPECIFIER_NAME = 3084] = "INVALID_USE_SPECIFIER_NAME", r[r.INVALID_DIAGRAMVIEW_CONTEXT = 3085] = "INVALID_DIAGRAMVIEW_CONTEXT", r[r.DUPLICATE_DIAGRAMVIEW_NAME = 3086] = "DUPLICATE_DIAGRAMVIEW_NAME", r[r.INVALID_DIAGRAMVIEW_FIELD = 3087] = "INVALID_DIAGRAMVIEW_FIELD", r[r.DUPLICATE_DIAGRAMVIEW_FIELD = 3088] = "DUPLICATE_DIAGRAMVIEW_FIELD", r[r.BINDING_ERROR = 4e3] = "BINDING_ERROR", r[r.NONEXISTENT_MODULE = 4001] = "NONEXISTENT_MODULE", r[r.UNSUPPORTED = 5e3] = "UNSUPPORTED", r[r.CIRCULAR_REF = 5001] = "CIRCULAR_REF", r[r.SAME_ENDPOINT = 5002] = "SAME_ENDPOINT", r[r.UNEQUAL_FIELDS_BINARY_REF = 5003] = "UNEQUAL_FIELDS_BINARY_REF", r[r.CONFLICTING_SETTING = 5004] = "CONFLICTING_SETTING", r[r.TABLE_REAPPEAR_IN_TABLEGROUP = 5005] = "TABLE_REAPPEAR_IN_TABLEGROUP", r;
1269
1273
  }({});
@@ -1310,6 +1314,9 @@ function isValidAlias(r) {
1310
1314
  function isSimpleName(r) {
1311
1315
  return r instanceof PrimaryExpressionNode && r.expression instanceof VariableNode;
1312
1316
  }
1317
+ function hasSimpleBody(r) {
1318
+ return !!r.bodyColon;
1319
+ }
1313
1320
  function isValidPartialInjection(r) {
1314
1321
  return r instanceof PrefixExpressionNode && r.op?.value === "~" && isExpressionAVariableNode(r.expression);
1315
1322
  }
@@ -1504,7 +1511,7 @@ function destructureIndexNode(r) {
1504
1511
  };
1505
1512
  }
1506
1513
  function extractQuotedStringToken(r) {
1507
- if (isExpressionAQuotedString(r)) return r?.expression instanceof VariableNode ? r?.expression?.variable?.value : r.expression.literal.value;
1514
+ if (isExpressionAQuotedString(r)) return r.expression instanceof VariableNode ? r?.expression?.variable?.value : r.expression.literal.value;
1508
1515
  }
1509
1516
  function extractNumericLiteral(r) {
1510
1517
  return r instanceof PrimaryExpressionNode && r.expression instanceof LiteralNode && r.expression.literal?.kind === SyntaxTokenKind.NUMERIC_LITERAL ? Number(r.expression.literal.value) : null;
@@ -2797,7 +2804,9 @@ var DiagramViewBinder = class {
2797
2804
  let [j] = partition_default(r.body, (r) => r instanceof FunctionApplicationNode);
2798
2805
  return j.flatMap((r) => !r.callee || isWildcardExpression(r.callee) ? [] : [r.callee, ...r.args].flatMap(scanNonListNodeForBinding).flatMap((r) => {
2799
2806
  let j = r.variables.pop();
2800
- return j ? this.compiler.nodeReferee(j).getErrors() : [];
2807
+ if (!j) return [];
2808
+ let M = r.variables.pop();
2809
+ return [...M ? this.compiler.nodeReferee(M).getErrors() : [], ...this.compiler.nodeReferee(j).getErrors()];
2801
2810
  }));
2802
2811
  }
2803
2812
  bindNoteReferences(r) {
@@ -3246,7 +3255,7 @@ const indexesModule$1 = {
3246
3255
  let z = isExpressionAVariableNode(F) ? F.expression.variable?.value ?? "" : "", B = P.lookupMembers(R, SymbolKind.Column, z);
3247
3256
  if (B) return Report.create(B);
3248
3257
  let V = isElementNode(L, ElementKind.TablePartial) ? "TablePartial" : "Table";
3249
- return new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Column '${z}' does not exist in ${V} 'public.${R.name}'`, F)]);
3258
+ return new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Column '${z}' does not exist in ${V} 'public.${R.name}'`, F)]);
3250
3259
  },
3251
3260
  bindNode(j, P) {
3252
3261
  return isElementNode(P, ElementKind.Indexes) ? Report.create(void 0, new IndexesBinder(j, P).bind()) : Report.create(PASS_THROUGH);
@@ -7350,11 +7359,18 @@ var ProgramInterpreter = class {
7350
7359
  let P = N.getValue();
7351
7360
  P && this.pushElement(M, P);
7352
7361
  }
7353
- let r = this.compiler.symbolMembers(this.programSymbol).getFiltered(UNHANDLED) ?? [];
7354
- for (let M of r) {
7355
- if (!(M instanceof SchemaSymbol)) continue;
7356
- let r = this.compiler.symbolMembers(M).getFiltered(UNHANDLED) ?? [];
7357
- for (let j of r) j instanceof UseSymbol && this.interpretUseSymbol(j);
7362
+ let r = /* @__PURE__ */ new Set(), M = (this.compiler.symbolMembers(this.programSymbol).getFiltered(UNHANDLED) ?? []).filter((r) => r instanceof SchemaSymbol);
7363
+ for (; M.length > 0;) {
7364
+ let N = M.shift(), P = this.compiler.symbolMembers(N).getFiltered(UNHANDLED) ?? [];
7365
+ for (let j of P) {
7366
+ if (j instanceof SchemaSymbol) {
7367
+ M.push(j);
7368
+ continue;
7369
+ }
7370
+ if (!(j instanceof UseSymbol)) continue;
7371
+ let N = j.originalSymbol.intern();
7372
+ r.has(N) || (r.add(N), this.interpretUseSymbol(j));
7373
+ }
7358
7374
  }
7359
7375
  }
7360
7376
  interpretUseSymbol(r) {
@@ -7558,24 +7574,16 @@ const programModule$1 = {
7558
7574
  },
7559
7575
  symbolMembers(N, P) {
7560
7576
  if (!P.isKind(SymbolKind.Program)) return Report.create(PASS_THROUGH);
7561
- let F = P.declaration;
7562
- if (!(F instanceof ProgramNode)) return Report.create([]);
7563
- let I = new Map([[DEFAULT_SCHEMA_NAME, N.symbolFactory.create(SchemaSymbol, { name: DEFAULT_SCHEMA_NAME }, P.filepath)]]);
7564
- for (let r of F.declarations) {
7565
- let M = N.nodeFullname(r).getFiltered(UNHANDLED) || [], F = M.length <= 1 ? DEFAULT_SCHEMA_NAME : M[0];
7566
- I.has(F) || I.set(F, N.symbolFactory.create(SchemaSymbol, { name: F }, P.filepath));
7567
- }
7577
+ if (!(P.declaration instanceof ProgramNode)) return Report.create([]);
7578
+ let F = /* @__PURE__ */ new Map();
7568
7579
  for (let r of N.reachableFiles(P.filepath)) {
7569
7580
  let { schemaMembers: j } = N.usableMembers(r).getValue();
7570
- for (let r of j) {
7571
- let { name: j } = r;
7572
- I.has(j) || I.set(j, N.symbolFactory.create(SchemaSymbol, { name: j }, P.filepath));
7573
- }
7581
+ for (let r of j) F.has(r.name) || F.set(r.name, r);
7574
7582
  }
7575
- let L = I.get(DEFAULT_SCHEMA_NAME);
7576
- if (!L) return Report.create([...I.values()]);
7577
- let R = N.symbolMembers(L).getFiltered(UNHANDLED)?.filter((r) => !r.isKind(SymbolKind.Schema));
7578
- return R ? Report.create([...I.values(), ...R]) : Report.create([...I.values()]);
7583
+ let I = F.get(DEFAULT_SCHEMA_NAME);
7584
+ if (!I) return Report.create([...F.values()]);
7585
+ let L = N.symbolMembers(I).getFiltered(UNHANDLED)?.filter((r) => !r.isKind(SymbolKind.Schema));
7586
+ return L ? Report.create([...F.values(), ...L]) : Report.create([...F.values()]);
7579
7587
  },
7580
7588
  bindNode(j, N) {
7581
7589
  return isProgramNode(N) ? new Binder(N, j).resolve() : Report.create(PASS_THROUGH);
@@ -7995,23 +8003,23 @@ function nodeRefereeOfRecordsName(r, j, N) {
7995
8003
  let P = extractVarNameFromPrimaryVariable(N) ?? "";
7996
8004
  if (!isAccessExpression(N.parentNode)) {
7997
8005
  let F = r.lookupMembers(j, [SymbolKind.Table, SymbolKind.Schema], P);
7998
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Table '${P}' does not exist in Schema 'public'`, N)]);
8006
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Table '${P}' does not exist in Schema 'public'`, N)]);
7999
8007
  }
8000
8008
  let F = nodeRefereeOfLeftExpression(r, N);
8001
8009
  if (!F) {
8002
8010
  let F = r.lookupMembers(j, [SymbolKind.Table, SymbolKind.Schema], P);
8003
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Table or schema '${P}' does not exist`, N)]);
8011
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Table or schema '${P}' does not exist`, N)]);
8004
8012
  }
8005
8013
  if (F.isKind(SymbolKind.Schema)) {
8006
8014
  let j = r.lookupMembers(F, [SymbolKind.Table, SymbolKind.Schema], P);
8007
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Table or schema '${P}' does not exist`, N)]);
8015
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Table or schema '${P}' does not exist`, N)]);
8008
8016
  }
8009
8017
  return new Report(void 0);
8010
8018
  }
8011
8019
  function nodeRefereeOfRecordsColumn(r, j, N) {
8012
8020
  if (!isExpressionAVariableNode(N)) return new Report(void 0);
8013
8021
  let P = extractVarNameFromPrimaryVariable(N) ?? "", F = r.lookupMembers(j, SymbolKind.Column, P);
8014
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Column '${P}' does not exist in Table 'public.${j.name}'`, N)]);
8022
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Column '${P}' does not exist in Table 'public.${j.name}'`, N)]);
8015
8023
  }
8016
8024
  function nodeRefereeOfEnumValue(r, j, N) {
8017
8025
  if (!isExpressionAVariableNode(N)) return new Report(void 0);
@@ -8021,7 +8029,7 @@ function nodeRefereeOfEnumValue(r, j, N) {
8021
8029
  if (F) {
8022
8030
  if (F.isKind(SymbolKind.Schema)) {
8023
8031
  let j = r.lookupMembers(F, [SymbolKind.Enum, SymbolKind.Schema], P);
8024
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum or schema '${P}' does not exist`, N)]);
8032
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum or schema '${P}' does not exist`, N)]);
8025
8033
  }
8026
8034
  if (F.isKind(SymbolKind.Enum)) {
8027
8035
  let j = r.lookupMembers(F, SymbolKind.EnumField, P);
@@ -8033,7 +8041,7 @@ function nodeRefereeOfEnumValue(r, j, N) {
8033
8041
  if (I.leftExpression === N) {
8034
8042
  if (isAccessExpression(I.parentNode) && I.parentNode.leftExpression === I) {
8035
8043
  let F = r.lookupMembers(j, SymbolKind.Schema, P);
8036
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Schema '${P}' does not exist in Schema 'public'`, N)]);
8044
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Schema '${P}' does not exist in Schema 'public'`, N)]);
8037
8045
  }
8038
8046
  let F = r.lookupMembers(j, SymbolKind.Enum, P);
8039
8047
  return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum '${P}' does not exist in Schema 'public'`, N)]);
@@ -8204,7 +8212,7 @@ function nodeRefereeOfRefEndpoint(r, N, P) {
8204
8212
  }
8205
8213
  const useUtils = { visibleName(r, M) {
8206
8214
  let N = M instanceof SyntaxNode ? M : M.useSpecifierDeclaration instanceof UseSpecifierNode ? M.useSpecifierDeclaration : M.declaration;
8207
- if (N) return r.nodeAlias(N).mapFiltered((r) => [r], UNHANDLED, void 0).getFiltered(UNHANDLED) || r.nodeFullname(N).getFiltered(UNHANDLED);
8215
+ if (N instanceof UseSpecifierNode && M instanceof NodeSymbol && N.getSymbolKind() !== M.kind && (N = M.declaration), N) return r.nodeAlias(N).mapFiltered((r) => [r], UNHANDLED, void 0).getFiltered(UNHANDLED) || r.nodeFullname(N).getFiltered(UNHANDLED);
8208
8216
  } }, useModule$1 = {
8209
8217
  nodeSymbol(N, P) {
8210
8218
  if (!isUseSpecifier(P) || !P.name) return Report.create(PASS_THROUGH);
@@ -8217,7 +8225,7 @@ const useUtils = { visibleName(r, M) {
8217
8225
  usedSymbol: I,
8218
8226
  kind: F,
8219
8227
  name: useUtils.visibleName(N, P)?.at(-1)
8220
- }, P.filepath), I === void 0 ? [new CompileError(CompileErrorCode.BINDING_ERROR, `Failed to resolve the import of ${F} '${(N.nodeFullname(P).getFiltered(UNHANDLED) || []).map(addDoubleQuoteIfNeeded).join(".")}'`, P)] : []);
8228
+ }, P.filepath), I === void 0 ? [new CompileError(CompileErrorCode.BINDING_ERROR, `Failed to resolve the import of ${F} '${(destructureComplexVariable(P.name) || []).map(addDoubleQuoteIfNeeded).join(".")}'`, P)] : []);
8221
8229
  },
8222
8230
  symbolMembers(N, P) {
8223
8231
  if (!(P instanceof UseSymbol)) return Report.create(PASS_THROUGH);
@@ -8235,14 +8243,14 @@ const useUtils = { visibleName(r, M) {
8235
8243
  let z = P.parentOfKind(UseDeclarationNode);
8236
8244
  if (z?.importPath?.value === void 0) return Report.create(void 0);
8237
8245
  let B = resolveImportFilepath(P.filepath, z.importPath.value);
8238
- if (!B) return Report.create(void 0);
8246
+ if (!B) return Report.create(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Import path must be relative, got '${z.importPath.value}'`, P)]);
8239
8247
  if (!N.layout.exists(B)) return Report.create(void 0, [new CompileError(CompileErrorCode.NONEXISTENT_MODULE, `${I} '${L?.join(".") ?? R}' does not exist in file ${B.toString()}. Does the file exist?`, P)]);
8240
8248
  let V = P.parentOfKind(InfixExpressionNode)?.leftExpression;
8241
8249
  if (V && V !== P) {
8242
8250
  let r = N.nodeReferee(V).getFiltered(UNHANDLED);
8243
8251
  if (!r) return Report.create(void 0);
8244
8252
  let F = N.lookupMembers(r, I, R);
8245
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Could not find ${I} '${R}'`, P)]);
8253
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Could not find ${I} '${R}'`, P)]);
8246
8254
  }
8247
8255
  let H = lookupMemberInFilepath(N, B, R, I);
8248
8256
  return H ? Report.create(H) : Report.create(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `${I} '${R}' does not exist in file ${B.toString()}`, P)]);
@@ -8252,7 +8260,7 @@ const useUtils = { visibleName(r, M) {
8252
8260
  let r = [];
8253
8261
  if (N.importPath?.value) {
8254
8262
  let M = resolveImportFilepath(N.filepath, N.importPath.value);
8255
- M && !j.layout.exists(M) && r.push(new CompileError(CompileErrorCode.NONEXISTENT_MODULE, `Failed to resolve the non-existent file '${N.importPath.value}'`, N.importPath));
8263
+ M ? j.layout.exists(M) || r.push(new CompileError(CompileErrorCode.NONEXISTENT_MODULE, `Failed to resolve the non-existent file '${N.importPath.value}'`, N.importPath)) : r.push(new CompileError(CompileErrorCode.BINDING_ERROR, `Import path must be relative, got '${N.importPath.value}'`, N.importPath));
8256
8264
  }
8257
8265
  if (N.specifiers) {
8258
8266
  let M = j.bindNode(N.specifiers);
@@ -8298,74 +8306,151 @@ function lookupMemberInFilepath(r, M, N, P, F = /* @__PURE__ */ new Set()) {
8298
8306
  }
8299
8307
  }
8300
8308
  function usableMembers(r) {
8301
- let N = [], P = /* @__PURE__ */ new Map(), F = [], I = [], L = [], R = [], z = r instanceof Filepath ? r : r.filepath, B = this.parseFile(z), { ast: V } = B.getValue();
8302
- for (let r of V.body) {
8303
- if (!(r instanceof UseDeclarationNode) || !r.specifiers || !r.importPath) continue;
8304
- let j = resolveImportFilepath(z, r.importPath.value);
8305
- j && (r.isReuse ? r.specifiers instanceof WildcardNode ? F.push({
8306
- importPath: j,
8307
- node: r.specifiers
8308
- }) : I.push(...r.specifiers.specifiers) : r.specifiers instanceof WildcardNode ? L.push({
8309
- importPath: j,
8310
- node: r.specifiers
8311
- }) : R.push(...r.specifiers.specifiers));
8312
- }
8313
- let H = [];
8314
8309
  if (r instanceof Filepath) {
8315
- let M = new Map([[DEFAULT_SCHEMA_NAME, this.symbolFactory.create(SchemaSymbol, { name: DEFAULT_SCHEMA_NAME }, r)]]);
8316
- for (let N of V.declarations) {
8317
- let P = this.nodeFullname(N).getFiltered(UNHANDLED) || [], F = P.length <= 1 ? DEFAULT_SCHEMA_NAME : P[0];
8318
- M.has(F) || M.set(F, this.symbolFactory.create(SchemaSymbol, { name: F }, r));
8319
- }
8320
- H.push(...M.values());
8321
- let P = H.find((r) => r.isPublicSchema());
8322
- if (P) {
8323
- let r = this.usableMembers(P).getFiltered(UNHANDLED);
8324
- r && N.push(...r.nonSchemaMembers);
8325
- }
8326
- } else {
8327
- let M = r;
8328
- for (let r of V.body) {
8329
- if (!(r instanceof ElementDeclarationNode)) continue;
8330
- let F = schemaMembership(this, M, r), I = this.nodeAlias(r).getFiltered(UNHANDLED), L = this.nodeSymbol(r).getFiltered(UNHANDLED);
8331
- if (I !== void 0 && M.isPublicSchema() && L && N.push(this.symbolFactory.create(AliasSymbol, {
8332
- kind: L.kind,
8333
- declaration: r,
8334
- aliasedSymbol: L,
8335
- name: I
8336
- }, M.filepath)), F.kind !== "none") if (F.kind === "direct") {
8337
- let M = this.nodeSymbol(r).getFiltered(UNHANDLED);
8338
- if (!M) continue;
8339
- N.push(M);
8340
- } else P.has(F.schemaName) || P.set(F.schemaName, this.symbolFactory.create(SchemaSymbol, {
8341
- name: F.schemaName,
8342
- parent: M
8343
- }, M.filepath));
8344
- }
8345
- H.push(...P.values());
8310
+ let { ast: M } = this.parseFile(r).getValue(), N = this.nodeSymbol(M).getFiltered(UNHANDLED);
8311
+ return this.usableMembers(N);
8346
8312
  }
8313
+ let N = r.filepath, P = collectImports(this, N), { nonSchemaMembers: F, schemaMembers: I } = r instanceof ProgramSymbol ? usableMembersForProgram(this, r, P) : usableMembersForSchema(this, r, P);
8347
8314
  return new Report({
8348
- nonSchemaMembers: N,
8349
- schemaMembers: H,
8315
+ nonSchemaMembers: F,
8316
+ schemaMembers: I,
8350
8317
  reuses: {
8351
- selective: I,
8352
- wildcard: F
8318
+ selective: P.selectiveReuses,
8319
+ wildcard: P.wildcardReuses
8353
8320
  },
8354
8321
  uses: {
8355
- selective: R,
8356
- wildcard: L
8322
+ selective: P.selectiveUses,
8323
+ wildcard: P.wildcardUses
8357
8324
  }
8358
- }, B.getErrors(), B.getWarnings());
8325
+ }, P.parseResult.getErrors(), P.parseResult.getWarnings());
8359
8326
  }
8360
8327
  function schemaMembership(r, M, N) {
8361
8328
  let P = M.qualifiedName, F;
8362
8329
  if (F = N instanceof UseSpecifierNode ? useUtils.visibleName(r, N) : r.nodeFullname(N).getFiltered(UNHANDLED), !F) return { kind: "none" };
8363
- let I = F.length <= 1 ? [DEFAULT_SCHEMA_NAME] : F.slice(0, -1);
8330
+ let I = N instanceof UseSpecifierNode && N.getSymbolKind() === SymbolKind.Schema ? F : F.length <= 1 ? [DEFAULT_SCHEMA_NAME] : F.slice(0, -1);
8364
8331
  return I.length < P.length || !P.every((r, j) => r === I[j]) ? { kind: "none" } : I.length === P.length ? { kind: "direct" } : {
8365
8332
  kind: "child",
8366
8333
  schemaName: I[P.length]
8367
8334
  };
8368
8335
  }
8336
+ function collectImports(r, j) {
8337
+ let M = r.parseFile(j), { ast: N } = M.getValue(), P = [], F = [], I = [], L = [];
8338
+ for (let r of N.body) {
8339
+ if (!(r instanceof UseDeclarationNode) || !r.specifiers || !r.importPath) continue;
8340
+ let M = resolveImportFilepath(j, r.importPath.value);
8341
+ M && (r.isReuse ? r.specifiers instanceof WildcardNode ? P.push({
8342
+ importPath: M,
8343
+ node: r.specifiers
8344
+ }) : F.push(...r.specifiers.specifiers) : r.specifiers instanceof WildcardNode ? I.push({
8345
+ importPath: M,
8346
+ node: r.specifiers
8347
+ }) : L.push(...r.specifiers.specifiers));
8348
+ }
8349
+ return {
8350
+ ast: N,
8351
+ parseResult: M,
8352
+ selectiveReuses: F,
8353
+ wildcardReuses: P,
8354
+ selectiveUses: L,
8355
+ wildcardUses: I
8356
+ };
8357
+ }
8358
+ function usableMembersForProgram(r, M, N) {
8359
+ let { ast: P, selectiveUses: F, selectiveReuses: I } = N, L = M.filepath, R = new Map([[DEFAULT_SCHEMA_NAME, r.symbolFactory.create(SchemaSymbol, { name: DEFAULT_SCHEMA_NAME }, L)]]);
8360
+ for (let M of P.declarations) {
8361
+ let N = r.nodeFullname(M).getFiltered(UNHANDLED) || [], P = N.length <= 1 ? DEFAULT_SCHEMA_NAME : N[0];
8362
+ R.has(P) || R.set(P, r.symbolFactory.create(SchemaSymbol, { name: P }, L));
8363
+ }
8364
+ registerProgramSchemas(r, [...F, ...I], R, L);
8365
+ for (let j of r.reachableFiles(L)) for (let M of collectTopLevelSchemaNames(r, j)) R.has(M) || R.set(M, r.symbolFactory.create(SchemaSymbol, { name: M }, L));
8366
+ let z = [...R.values()], B = [], V = z.find((r) => r.isPublicSchema());
8367
+ if (V) {
8368
+ let M = r.usableMembers(V).getFiltered(UNHANDLED);
8369
+ M && B.push(...M.nonSchemaMembers);
8370
+ }
8371
+ return {
8372
+ nonSchemaMembers: B,
8373
+ schemaMembers: z
8374
+ };
8375
+ }
8376
+ function usableMembersForSchema(r, M, N) {
8377
+ let { ast: P, selectiveUses: F, selectiveReuses: I } = N, L = [], R = /* @__PURE__ */ new Map();
8378
+ for (let N of P.body) {
8379
+ if (!(N instanceof ElementDeclarationNode)) continue;
8380
+ let P = schemaMembership(r, M, N), F = r.nodeAlias(N).getFiltered(UNHANDLED), I = r.nodeSymbol(N).getFiltered(UNHANDLED);
8381
+ if (F !== void 0 && M.isPublicSchema() && I && L.push(r.symbolFactory.create(AliasSymbol, {
8382
+ kind: I.kind,
8383
+ declaration: N,
8384
+ aliasedSymbol: I,
8385
+ name: F
8386
+ }, M.filepath)), P.kind !== "none") if (P.kind === "direct") {
8387
+ let M = r.nodeSymbol(N).getFiltered(UNHANDLED);
8388
+ if (!M) continue;
8389
+ L.push(M);
8390
+ } else R.has(P.schemaName) || R.set(P.schemaName, r.symbolFactory.create(SchemaSymbol, {
8391
+ name: P.schemaName,
8392
+ parent: M
8393
+ }, M.filepath));
8394
+ }
8395
+ registerSchemaChildren(r, [...F, ...I], M, R);
8396
+ let z = M.qualifiedName;
8397
+ for (let j of r.reachableFiles(M.filepath)) for (let N of collectChildSchemaNames(r, j, z)) R.has(N) || R.set(N, r.symbolFactory.create(SchemaSymbol, {
8398
+ name: N,
8399
+ parent: M
8400
+ }, M.filepath));
8401
+ return {
8402
+ nonSchemaMembers: L,
8403
+ schemaMembers: [...R.values()]
8404
+ };
8405
+ }
8406
+ function registerProgramSchemas(r, j, M, N) {
8407
+ for (let P of j) {
8408
+ if (P.getSymbolKind() !== SymbolKind.Schema) continue;
8409
+ let j = useUtils.visibleName(r, P)?.at(0);
8410
+ j !== void 0 && !M.has(j) && M.set(j, r.symbolFactory.create(SchemaSymbol, { name: j }, N));
8411
+ }
8412
+ }
8413
+ function registerSchemaChildren(r, j, M, N) {
8414
+ let P = M.qualifiedName;
8415
+ for (let F of j) {
8416
+ if (F.getSymbolKind() !== SymbolKind.Schema) continue;
8417
+ let j = useUtils.visibleName(r, F);
8418
+ if (!j || j.length <= P.length || !P.every((r, M) => r === j[M])) continue;
8419
+ let I = j[P.length];
8420
+ I && !N.has(I) && N.set(I, r.symbolFactory.create(SchemaSymbol, {
8421
+ name: I,
8422
+ parent: M
8423
+ }, M.filepath));
8424
+ }
8425
+ }
8426
+ function collectTopLevelSchemaNames(r, M) {
8427
+ let { ast: N } = r.parseFile(M).getValue(), P = [];
8428
+ for (let M of N.declarations) {
8429
+ let N = r.nodeFullname(M).getFiltered(UNHANDLED) || [], F = N.length <= 1 ? DEFAULT_SCHEMA_NAME : N[0];
8430
+ P.push(F);
8431
+ }
8432
+ let F = collectImports(r, M);
8433
+ for (let j of [...F.selectiveUses, ...F.selectiveReuses]) {
8434
+ if (j.getSymbolKind() !== SymbolKind.Schema) continue;
8435
+ let M = useUtils.visibleName(r, j)?.at(0);
8436
+ M !== void 0 && P.push(M);
8437
+ }
8438
+ return P;
8439
+ }
8440
+ function collectChildSchemaNames(r, M, N) {
8441
+ let { ast: P } = r.parseFile(M).getValue(), F = [];
8442
+ for (let M of P.declarations) {
8443
+ let P = r.nodeFullname(M).getFiltered(UNHANDLED) || [], I = P.length <= 1 ? [DEFAULT_SCHEMA_NAME] : P.slice(0, -1);
8444
+ I.length > N.length && N.every((r, j) => r === I[j]) && F.push(I[N.length]);
8445
+ }
8446
+ let I = collectImports(r, M);
8447
+ for (let j of [...I.selectiveUses, ...I.selectiveReuses]) {
8448
+ if (j.getSymbolKind() !== SymbolKind.Schema) continue;
8449
+ let M = useUtils.visibleName(r, j);
8450
+ !M || M.length <= N.length || N.every((r, j) => r === M[j]) && F.push(M[N.length]);
8451
+ }
8452
+ return F;
8453
+ }
8369
8454
  var TableBinder = class {
8370
8455
  constructor(r, j) {
8371
8456
  this.compiler = r, this.declarationNode = j;
@@ -8685,7 +8770,7 @@ function nodeRefereeOfPartialInjection(r, N, P) {
8685
8770
  }
8686
8771
  }
8687
8772
  let L = r.lookupMembers(N, SymbolKind.TablePartial, F);
8688
- return L ? Report.create(L) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `TablePartial '${F}' does not exist in Schema 'public'`, P)]);
8773
+ return L ? Report.create(L) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `TablePartial '${F}' does not exist in Schema 'public'`, P)]);
8689
8774
  }
8690
8775
  function nodeRefereeOfEnumType$1(r, j, N) {
8691
8776
  if (!isExpressionAVariableNode(N)) return new Report(void 0);
@@ -8695,13 +8780,13 @@ function nodeRefereeOfEnumType$1(r, j, N) {
8695
8780
  if (F) {
8696
8781
  if (F.isKind(SymbolKind.Schema)) {
8697
8782
  let j = isTerminalAccessFragment(N), I = r.lookupMembers(F, j ? SymbolKind.Enum : SymbolKind.Schema, P);
8698
- return I ? Report.create(I) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, j ? `Enum '${P}' does not exist in Schema 'public'` : `Schema '${P}' does not exist in Schema 'public'`, N)]);
8783
+ return I ? Report.create(I) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, j ? `Enum '${P}' does not exist in Schema 'public'` : `Schema '${P}' does not exist in Schema 'public'`, N)]);
8699
8784
  }
8700
8785
  return new Report(void 0);
8701
8786
  }
8702
8787
  if (N.parentNode.leftExpression === N) {
8703
8788
  let F = r.lookupMembers(j, SymbolKind.Schema, P);
8704
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Schema '${P}' does not exist in Schema 'public'`, N)]);
8789
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Schema '${P}' does not exist in Schema 'public'`, N)]);
8705
8790
  }
8706
8791
  return new Report(void 0);
8707
8792
  }
@@ -8748,11 +8833,11 @@ function nodeRefereeOfEnumDefault$1(r, j, N) {
8748
8833
  if (F) {
8749
8834
  if (F.isKind(SymbolKind.Schema)) {
8750
8835
  let j = r.lookupMembers(F, [SymbolKind.Enum, SymbolKind.Schema], P);
8751
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum or schema '${P}' does not exist`, N)]);
8836
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum or schema '${P}' does not exist`, N)]);
8752
8837
  }
8753
8838
  if (F.isKind(SymbolKind.Enum)) {
8754
8839
  let j = r.lookupMembers(F, SymbolKind.EnumField, P);
8755
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum field '${P}' does not exist in Enum 'public.${F.name}'`, N)]);
8840
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum field '${P}' does not exist in Enum 'public.${F.name}'`, N)]);
8756
8841
  }
8757
8842
  return new Report(void 0);
8758
8843
  }
@@ -8760,10 +8845,10 @@ function nodeRefereeOfEnumDefault$1(r, j, N) {
8760
8845
  if (I.leftExpression === N) {
8761
8846
  if (isAccessExpression(I.parentNode) && I.parentNode.leftExpression === I) {
8762
8847
  let F = r.lookupMembers(j, SymbolKind.Schema, P);
8763
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Schema '${P}' does not exist in Schema 'public'`, N)]);
8848
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Schema '${P}' does not exist in Schema 'public'`, N)]);
8764
8849
  }
8765
8850
  let F = r.lookupMembers(j, SymbolKind.Enum, P);
8766
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum '${P}' does not exist in Schema 'public'`, N)]);
8851
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum '${P}' does not exist in Schema 'public'`, N)]);
8767
8852
  }
8768
8853
  return new Report(void 0);
8769
8854
  }
@@ -9028,16 +9113,11 @@ function nodeRefereeOfTableGroupField(r, N, P) {
9028
9113
  let F = extractVarNameFromPrimaryVariable(P) ?? "";
9029
9114
  if (!isAccessExpression(P.parentNode)) {
9030
9115
  let I = r.symbolMembers(N).getFiltered(UNHANDLED);
9031
- if (I) for (let N of I) {
9032
- if (!(N instanceof SchemaSymbol)) continue;
9033
- let P = r.lookupMembers(N, SymbolKind.Table, F);
9034
- if (P) return Report.create(P);
9035
- if (!N.isPublicSchema()) {
9036
- let P = r.symbolMembers(N).getFiltered(UNHANDLED);
9037
- if (P) {
9038
- let N = P.find((M) => !M.isKind(SymbolKind.Table) || !M.declaration ? !1 : r.nodeAlias(M.declaration).getFiltered(UNHANDLED) === F);
9039
- if (N) return new Report(N);
9040
- }
9116
+ if (I) {
9117
+ let j = I.find((r) => r instanceof SchemaSymbol && r.isPublicSchema());
9118
+ if (j) {
9119
+ let N = r.lookupMembers(j, SymbolKind.Table, F);
9120
+ if (N) return Report.create(N);
9041
9121
  }
9042
9122
  }
9043
9123
  let L = r.lookupMembers(N, SymbolKind.Table, F);
@@ -9146,7 +9226,7 @@ var TablePartialBinder = class {
9146
9226
  this.tablePartial.headerColor = M ? extractColor(M.value) : void 0;
9147
9227
  let [N] = j[SettingName.Note] || [];
9148
9228
  return this.tablePartial.note = N && {
9149
- value: extractQuotedStringToken(N?.value) ? normalizeNote(extractQuotedStringToken(N?.value)) : "",
9229
+ value: normalizeNote(extractQuotedStringToken(N?.value)),
9150
9230
  token: getTokenPosition(N)
9151
9231
  }, [];
9152
9232
  }
@@ -9274,13 +9354,13 @@ function nodeRefereeOfEnumType(r, j, N) {
9274
9354
  if (F) {
9275
9355
  if (F.isKind(SymbolKind.Schema)) {
9276
9356
  let j = r.lookupMembers(F, [SymbolKind.Enum, SymbolKind.Schema], P);
9277
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum or schema '${P}' does not exist`, N)]);
9357
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum or schema '${P}' does not exist`, N)]);
9278
9358
  }
9279
9359
  return new Report(void 0);
9280
9360
  }
9281
9361
  if (N.parentNode.leftExpression === N) {
9282
9362
  let F = r.lookupMembers(j, SymbolKind.Schema, P);
9283
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Schema '${P}' does not exist in Schema 'public'`, N)]);
9363
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Schema '${P}' does not exist in Schema 'public'`, N)]);
9284
9364
  }
9285
9365
  return new Report(void 0);
9286
9366
  }
@@ -9293,21 +9373,21 @@ function nodeRefereeOfInlineRef(r, P, F) {
9293
9373
  let N = r.nodeSymbol(L).getFiltered(UNHANDLED);
9294
9374
  if (N) {
9295
9375
  let j = r.lookupMembers(N, SymbolKind.Column, I);
9296
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Column '${I}' does not exist in TablePartial '${N.name}'`, F)]);
9376
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Column '${I}' does not exist in TablePartial '${N.name}'`, F)]);
9297
9377
  }
9298
9378
  }
9299
9379
  let R = r.lookupMembers(P, SymbolKind.Column, I);
9300
- return R ? Report.create(R) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Column '${I}' does not exist`, F)]);
9380
+ return R ? Report.create(R) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Column '${I}' does not exist`, F)]);
9301
9381
  }
9302
9382
  let L = nodeRefereeOfLeftExpression(r, F);
9303
9383
  if (L) {
9304
9384
  if (L.isKind(SymbolKind.Schema)) {
9305
9385
  let j = r.lookupMembers(L, [SymbolKind.Table, SymbolKind.Schema], I);
9306
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Table or schema '${I}' does not exist`, F)]);
9386
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Table or schema '${I}' does not exist`, F)]);
9307
9387
  }
9308
9388
  if (L.isKind(SymbolKind.Table)) {
9309
9389
  let j = r.lookupMembers(L, SymbolKind.Column, I);
9310
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Column '${I}' does not exist in Table 'public.${L.name}'`, F)]);
9390
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Column '${I}' does not exist in Table 'public.${L.name}'`, F)]);
9311
9391
  }
9312
9392
  return new Report(void 0);
9313
9393
  }
@@ -9315,10 +9395,10 @@ function nodeRefereeOfInlineRef(r, P, F) {
9315
9395
  if (R.leftExpression === F) {
9316
9396
  if (isAccessExpression(R.parentNode) && R.parentNode.leftExpression === R) {
9317
9397
  let j = r.lookupMembers(P, SymbolKind.Schema, I);
9318
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Schema '${I}' does not exist in Schema 'public'`, F)]);
9398
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Schema '${I}' does not exist in Schema 'public'`, F)]);
9319
9399
  }
9320
9400
  let j = r.lookupMembers(P, SymbolKind.Table, I);
9321
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Table '${I}' does not exist in Schema 'public'`, F)]);
9401
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Table '${I}' does not exist in Schema 'public'`, F)]);
9322
9402
  }
9323
9403
  return new Report(void 0);
9324
9404
  }
@@ -9330,11 +9410,11 @@ function nodeRefereeOfEnumDefault(r, j, N) {
9330
9410
  if (F) {
9331
9411
  if (F.isKind(SymbolKind.Schema)) {
9332
9412
  let j = r.lookupMembers(F, [SymbolKind.Enum, SymbolKind.Schema], P);
9333
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum or schema '${P}' does not exist`, N)]);
9413
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum or schema '${P}' does not exist`, N)]);
9334
9414
  }
9335
9415
  if (F.isKind(SymbolKind.Enum)) {
9336
9416
  let j = r.lookupMembers(F, SymbolKind.EnumField, P);
9337
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum field '${P}' does not exist in Enum 'public.${F.name}'`, N)]);
9417
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum field '${P}' does not exist in Enum 'public.${F.name}'`, N)]);
9338
9418
  }
9339
9419
  return new Report(void 0);
9340
9420
  }
@@ -9342,10 +9422,10 @@ function nodeRefereeOfEnumDefault(r, j, N) {
9342
9422
  if (I.leftExpression === N) {
9343
9423
  if (isAccessExpression(I.parentNode) && I.parentNode.leftExpression === I) {
9344
9424
  let F = r.lookupMembers(j, SymbolKind.Schema, P);
9345
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Schema '${P}' does not exist in Schema 'public'`, N)]);
9425
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Schema '${P}' does not exist in Schema 'public'`, N)]);
9346
9426
  }
9347
9427
  let F = r.lookupMembers(j, SymbolKind.Enum, P);
9348
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum '${P}' does not exist in Schema 'public'`, N)]);
9428
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum '${P}' does not exist in Schema 'public'`, N)]);
9349
9429
  }
9350
9430
  return new Report(void 0);
9351
9431
  }
@@ -9358,26 +9438,38 @@ const schemaModule = { symbolMembers(N, P) {
9358
9438
  let M = handleMemberSelectiveUses(N, P, r, R);
9359
9439
  z.push(...M.getErrors());
9360
9440
  let F = M.getFiltered(UNHANDLED);
9361
- F && L.push(F);
9441
+ F && L.push(F), L.push(...mergeImportedSchema(N, P, r));
9362
9442
  }
9363
9443
  for (let { importPath: r, node: j } of I.reuses.wildcard) L.push(...handleMemberWildcardUses(N, P, r, j, R));
9364
9444
  for (let r of I.uses.selective) {
9365
9445
  let M = handleMemberSelectiveUses(N, P, r, R);
9366
9446
  z.push(...M.getErrors());
9367
9447
  let F = M.getFiltered(UNHANDLED);
9368
- F && L.push(F);
9448
+ F && L.push(F), L.push(...mergeImportedSchema(N, P, r));
9369
9449
  }
9370
9450
  for (let { importPath: r, node: j } of I.uses.wildcard) L.push(...handleMemberWildcardUses(N, P, r, j, R));
9371
9451
  L.push(...R.values());
9372
9452
  let B = [...L];
9373
- for (let r of L) r.isKind(SymbolKind.TableGroup) && B.push(...expandTableGroup(N, r));
9453
+ {
9454
+ let r = L.filter((r) => r.isKind(SymbolKind.TableGroup));
9455
+ if (!P.isPublicSchema()) {
9456
+ let M = N.usableMembers(P.filepath).getFiltered(UNHANDLED)?.schemaMembers.find((r) => r.isPublicSchema());
9457
+ if (M) {
9458
+ let P = N.symbolMembers(M).getFiltered(UNHANDLED);
9459
+ P && r.push(...P.filter((r) => r.isKind(SymbolKind.TableGroup)));
9460
+ }
9461
+ }
9462
+ for (let j of r) B.push(...expandTableGroup(N, P, j));
9463
+ }
9374
9464
  let V = /* @__PURE__ */ new Set(), H = B.filter((r) => {
9465
+ if (r instanceof UseSymbol && r.isKind(SymbolKind.Schema)) return !1;
9375
9466
  let j = `${r.originalSymbol.intern()}:${r.name ?? ""}`;
9376
9467
  return V.has(j) ? !1 : (V.add(j), !0);
9377
9468
  }), U = /* @__PURE__ */ new Map();
9378
9469
  for (let r of H) {
9379
9470
  let j = `${r.kind}:${r.name}`;
9380
9471
  if (U.get(j)) {
9472
+ if (r.isKind(SymbolKind.Schema)) continue;
9381
9473
  let j = r instanceof UseSymbol ? r.useSpecifierDeclaration ?? r.declaration : r.declaration instanceof ElementDeclarationNode && r.declaration.name ? r.declaration.name : r.declaration;
9382
9474
  j && r.name !== void 0 && z.push(getDuplicateSchemaMemberError(r.kind, r.name, F.join("."), j));
9383
9475
  } else U.set(j, r);
@@ -9406,6 +9498,43 @@ function handleMemberSelectiveUses(r, N, P, F) {
9406
9498
  parent: N
9407
9499
  }, N.filepath)), Report.create(void 0);
9408
9500
  }
9501
+ function mergeImportedSchema(r, M, N, P = /* @__PURE__ */ new Set()) {
9502
+ if (N.getSymbolKind() !== SymbolKind.Schema || !N.name) return [];
9503
+ let F = r.nodeReferee(N.name).getFiltered(UNHANDLED);
9504
+ if (!F || !F.isKind(SymbolKind.Schema)) return [];
9505
+ let I = useUtils.visibleName(r, N), L = M.qualifiedName;
9506
+ return !I || I.length !== L.length || !I.every((r, j) => r === L[j]) ? [] : mergeSchemaMembers(r, M, N, F, P);
9507
+ }
9508
+ function mergeSchemaMembers(r, M, N, P, F) {
9509
+ let I = P.intern();
9510
+ if (F.has(I)) return [];
9511
+ F.add(I);
9512
+ let L = r.usableMembers(P).getFiltered(UNHANDLED);
9513
+ if (!L) return [];
9514
+ let R = L.nonSchemaMembers.filter((r) => r.canBeImported).map((j) => r.symbolFactory.create(UseSymbol, {
9515
+ kind: j.kind,
9516
+ declaration: j.originalSymbol.declaration,
9517
+ usedSymbol: j.originalSymbol,
9518
+ useSpecifierDeclaration: N,
9519
+ name: j.name
9520
+ }, M.filepath));
9521
+ for (let j of [...R]) j.isKind(SymbolKind.TableGroup) && R.push(...expandTableGroup(r, M, j));
9522
+ for (let P of L.reuses.selective) if (R.push(...mergeImportedSchema(r, M, P, F)), schemaMembership(r, M, P).kind === "direct") {
9523
+ let F = r.nodeSymbol(P).getFiltered(UNHANDLED);
9524
+ F && F.canBeImported && R.push(r.symbolFactory.create(UseSymbol, {
9525
+ kind: F.kind,
9526
+ declaration: F.originalSymbol.declaration,
9527
+ usedSymbol: F.originalSymbol,
9528
+ useSpecifierDeclaration: N,
9529
+ name: F.name
9530
+ }, M.filepath));
9531
+ }
9532
+ for (let { importPath: j } of L.reuses.wildcard) {
9533
+ let P = findSchemaSymbolInFilepath(r, j, M.qualifiedName);
9534
+ P && R.push(...mergeSchemaMembers(r, M, N, P, F));
9535
+ }
9536
+ return R;
9537
+ }
9409
9538
  function handleMemberWildcardUses(r, M, N, P, F, I = /* @__PURE__ */ new Set()) {
9410
9539
  if (I.has(N)) return [];
9411
9540
  I.add(N);
@@ -9424,7 +9553,7 @@ function handleMemberWildcardUses(r, M, N, P, F, I = /* @__PURE__ */ new Set())
9424
9553
  let { reuses: { selective: B, wildcard: V } } = R;
9425
9554
  for (let N of B) {
9426
9555
  let P = handleMemberSelectiveUses(r, M, N, F).getFiltered(UNHANDLED);
9427
- P && z.push(P);
9556
+ P && z.push(P), z.push(...mergeImportedSchema(r, M, N));
9428
9557
  }
9429
9558
  for (let { importPath: j } of V) z.push(...handleMemberWildcardUses(r, M, j, P, F, I));
9430
9559
  return z;
@@ -9442,34 +9571,36 @@ function findSchemaSymbolInFilepath(r, M, N) {
9442
9571
  }
9443
9572
  return I;
9444
9573
  }
9445
- function expandTableGroup(r, M) {
9446
- if (!M.isKind(SymbolKind.TableGroup)) return [];
9447
- let N = M.originalSymbol, P = r.symbolMembers(N).getFiltered(UNHANDLED);
9448
- if (!P) return [];
9449
- let F = [];
9450
- for (let j of P) {
9574
+ function expandTableGroup(r, M, N) {
9575
+ if (!N.isKind(SymbolKind.TableGroup) || !(N instanceof UseSymbol)) return [];
9576
+ let P = N.originalSymbol, F = r.symbolMembers(P).getFiltered(UNHANDLED);
9577
+ if (!F) return [];
9578
+ let I = M.qualifiedName, L = [];
9579
+ for (let j of F) {
9451
9580
  if (!j.isKind(SymbolKind.TableGroupField) || !j.declaration) continue;
9452
- let N = j.declaration.callee;
9453
- if (!N) continue;
9454
- let P = destructureComplexVariable(N);
9581
+ let M = j.declaration.callee;
9582
+ if (!M) continue;
9583
+ let P = destructureComplexVariable(M);
9455
9584
  if (!P || P.length === 0) continue;
9456
- let I = j.declaration.filepath, L = lookupTableInFile(r, I, P);
9457
- L && M instanceof UseSymbol && F.push(r.symbolFactory.create(UseSymbol, {
9585
+ let F = P.length <= 1 ? [DEFAULT_SCHEMA_NAME] : P.slice(0, -1);
9586
+ if (F.length !== I.length || !I.every((r, j) => r === F[j])) continue;
9587
+ let R = j.declaration.filepath, z = lookupTableInFile(r, R, P);
9588
+ z && L.push(r.symbolFactory.create(UseSymbol, {
9458
9589
  kind: SymbolKind.Table,
9459
- declaration: L.declaration,
9460
- usedSymbol: L,
9590
+ declaration: z.declaration,
9591
+ usedSymbol: z,
9461
9592
  useSpecifierDeclaration: void 0,
9462
- name: L.name
9463
- }, M.filepath));
9593
+ name: z.name
9594
+ }, N.filepath));
9464
9595
  }
9465
- return F;
9596
+ return L;
9466
9597
  }
9467
9598
  function lookupTableInFile(r, M, N) {
9468
9599
  if (N.length === 1) {
9469
9600
  let P = r.usableMembers(M).getFiltered(UNHANDLED);
9470
9601
  return P ? P.nonSchemaMembers.find((r) => r.isKind(SymbolKind.Table) && r.name === N[0]) : void 0;
9471
9602
  }
9472
- let [P, F] = [N[0], N[N.length - 1]], I = r.usableMembers(M).getFiltered(UNHANDLED);
9603
+ let P = N[0], F = N[N.length - 1], I = r.usableMembers(M).getFiltered(UNHANDLED);
9473
9604
  if (!I) return;
9474
9605
  let L = I.schemaMembers.find((r) => r.name === P);
9475
9606
  if (!L) return;
@@ -9789,6 +9920,10 @@ var DiagramViewValidator = class {
9789
9920
  }
9790
9921
  validateSubBlock(r) {
9791
9922
  let j = [], M = [];
9923
+ if (hasSimpleBody(r)) return j.push(new CompileError(CompileErrorCode.UNEXPECTED_SIMPLE_BODY, `${r.type?.value} body must be a block`, r.bodyColon)), {
9924
+ errors: j,
9925
+ warnings: M
9926
+ };
9792
9927
  if (!r.body || !(r.body instanceof BlockExpressionNode)) return {
9793
9928
  errors: j,
9794
9929
  warnings: M
@@ -11023,7 +11158,7 @@ const modules = [
11023
11158
  nodeFullname(j, N) {
11024
11159
  if (!isUseSpecifier(N)) return Report.create(PASS_THROUGH);
11025
11160
  let P = destructureComplexVariable(N.name);
11026
- return !P || P.length === 0 ? Report.create(void 0, [new CompileError(CompileErrorCode.INVALID_NAME, "Use specifiers must have a valid name (simple name or schema-qualified name)", N)]) : N.isKind(ImportKind.TableGroup) && P.length > 1 ? Report.create(P, [new CompileError(CompileErrorCode.INVALID_USE_SPECIFIER_NAME, "A TableGroup name must be a simple name", N)]) : N.isKind(ImportKind.TablePartial) && P.length > 1 ? Report.create(P, [new CompileError(CompileErrorCode.INVALID_USE_SPECIFIER_NAME, "A TablePartial name must be a simple name", N)]) : N.isKind(ImportKind.Note) && P.length > 1 ? Report.create(P, [new CompileError(CompileErrorCode.INVALID_USE_SPECIFIER_NAME, "A Sticky note name must be a simple name", N)]) : (P.length === 1 && P.unshift(DEFAULT_SCHEMA_NAME), new Report(P));
11161
+ return !P || P.length === 0 ? Report.create(void 0, [new CompileError(CompileErrorCode.INVALID_NAME, "Use specifiers must have a valid name (simple name or schema-qualified name)", N)]) : N.isKind(ImportKind.TableGroup) && P.length > 1 ? Report.create(P, [new CompileError(CompileErrorCode.INVALID_USE_SPECIFIER_NAME, "A TableGroup name must be a simple name", N)]) : N.isKind(ImportKind.TablePartial) && P.length > 1 ? Report.create(P, [new CompileError(CompileErrorCode.INVALID_USE_SPECIFIER_NAME, "A TablePartial name must be a simple name", N)]) : N.isKind(ImportKind.Note) && P.length > 1 ? Report.create(P, [new CompileError(CompileErrorCode.INVALID_USE_SPECIFIER_NAME, "A Sticky note name must be a simple name", N)]) : (P.length === 1 && !N.isKind(ImportKind.Schema) && P.unshift(DEFAULT_SCHEMA_NAME), new Report(P));
11027
11162
  },
11028
11163
  nodeAlias(j, N) {
11029
11164
  return isUseSpecifier(N) ? N.alias ? isValidAlias(N.alias) ? new Report(extractVariableFromExpression(N.alias)) : new Report(void 0, [new CompileError(CompileErrorCode.INVALID_ALIAS, "Use aliases can only contain alphanumeric and underscore unless surrounded by double quotes", N.alias)]) : new Report(void 0) : Report.create(PASS_THROUGH);
@@ -11902,77 +12037,47 @@ function isOffsetWithinElementHeader(r, j) {
11902
12037
  let M = j.bodyColon?.start ?? j.body?.start;
11903
12038
  return M === void 0 ? r >= j.start && r <= j.end : r >= j.start && r < M;
11904
12039
  }
11905
- function scanExistingUses(r, j, M) {
11906
- let N = [], P = r.parseFile(j).getValue()?.ast;
11907
- if (!P) return N;
11908
- for (let r of P.uses) {
11909
- let j = r.importPath?.value ?? "";
11910
- if (!j) {
11911
- let N = r.useKeyword?.start ?? r.fullStart, P = M.lastIndexOf("\n", N - 1) + 1, F = M.indexOf("\n", N), I = M.slice(P, F === -1 ? void 0 : F).match(/from\s*(['"])(.*?)\1/);
11912
- I && (j = I[2]);
11913
- }
11914
- let P = [];
11915
- if (r.specifiers instanceof UseSpecifierListNode) {
11916
- let j = new Set([
11917
- "table",
11918
- "enum",
11919
- "tablepartial",
11920
- "tablegroup",
11921
- "note",
11922
- "schema",
11923
- "from"
11924
- ]);
11925
- for (let N of r.specifiers.specifiers) {
11926
- let r, F;
11927
- if (N.name) r = N.importKind?.value, F = extractVariableFromExpression(N.name), !F && N.name.start !== void 0 && N.name.end !== void 0 && (F = M.slice(N.name.start, N.name.end).trim());
11928
- else if (N.importKind) {
11929
- let r = N.importKind.value ?? void 0;
11930
- r && !j.has(r.toLowerCase()) && (F = r);
11931
- }
11932
- F && F.toLowerCase() !== "from" && P.push({
12040
+ function scanExistingUses(r, j) {
12041
+ let M = [], N = r.parseFile(j).getValue()?.ast;
12042
+ if (!N) return M;
12043
+ for (let r of N.uses) {
12044
+ let j = r.importPath?.value ?? "", N = [];
12045
+ if (r.specifiers instanceof UseSpecifierListNode) for (let j of r.specifiers.specifiers) {
12046
+ let r, M;
12047
+ if (j.name ? (r = j.importKind?.value, M = extractVariableFromExpression(j.name)) : j.importKind && (M = j.importKind.value ?? void 0), M) {
12048
+ let P = j.alias ? extractVariableFromExpression(j.alias) : void 0;
12049
+ N.push({
11933
12050
  kind: r,
11934
- name: F
12051
+ name: M,
12052
+ alias: P
11935
12053
  });
11936
12054
  }
11937
- } else r.specifiers instanceof WildcardNode && P.push({ name: "*" });
11938
- if (P.length === 0 && !(r.specifiers instanceof WildcardNode)) continue;
11939
- let F = r.fullEnd;
11940
- if (!r.importPath) {
11941
- let j = r.useKeyword?.start ?? r.fullStart, N = M.indexOf("\n", j);
11942
- F = N === -1 ? M.length : N;
11943
12055
  }
11944
- N.push({
12056
+ else r.specifiers instanceof WildcardNode && N.push({ name: "*" });
12057
+ N.length === 0 && !(r.specifiers instanceof WildcardNode) || M.push({
11945
12058
  startOffset: r.fullStart,
11946
- endOffset: F,
12059
+ endOffset: r.fullEnd,
11947
12060
  sourceFile: j,
11948
- specifiers: P,
12061
+ specifiers: N,
11949
12062
  node: r
11950
12063
  });
11951
12064
  }
11952
- return N;
12065
+ return M;
11953
12066
  }
11954
12067
  function mergeSymbolIntoUses(r, j, M, N, P, F) {
11955
- let I = scanExistingUses(r, P, F), L = normalizeSourcePath(N, P), R = I.findIndex((r) => r.sourceFile === L), z = `${M} ${j}`, B = detectLineEnding(F);
12068
+ let I = scanExistingUses(r, P), L = N.relativeTo(P.dirname).replace(/\.dbml$/, ""), R = I.findIndex((r) => r.sourceFile === L), z = `${M} ${j}`;
11956
12069
  if (R !== -1) {
11957
12070
  let r = I[R];
11958
12071
  return r.specifiers.some((r) => r.name === j) ? { topInsert: "" } : {
11959
- topInsert: buildUseStatement(uniqueInOrder([...r.specifiers.filter((r) => r.name !== "*").map((r) => `${r.kind ?? M} ${r.name}`), z]), L, B),
12072
+ topInsert: buildUseStatement(uniq_default([...r.specifiers.filter((r) => r.name !== "*").map((r) => `${r.kind ?? M} ${r.name}${r.alias ? ` as ${r.alias}` : ""}`), z]), L, "\n"),
11960
12073
  removeRange: expandToFullLines(F, r.startOffset, r.endOffset)
11961
12074
  };
11962
12075
  }
11963
- return { topInsert: buildUseStatement([z], L, B) };
12076
+ return { topInsert: buildUseStatement([z], L, "\n") };
11964
12077
  }
11965
12078
  function buildUseStatement(r, j, M) {
11966
12079
  return `use {${M}${r.map((r) => ` ${r}`).join(M)}${M}} from '${j}'${M}${M}`;
11967
12080
  }
11968
- function uniqueInOrder(r) {
11969
- let j = /* @__PURE__ */ new Set(), M = [];
11970
- for (let N of r) {
11971
- let r = N.trim();
11972
- j.has(r) || (j.add(r), M.push(N));
11973
- }
11974
- return M;
11975
- }
11976
12081
  function expandToFullLines(r, j, M) {
11977
12082
  let N = r.lastIndexOf("\n", j - 1) + 1, P = M;
11978
12083
  for (; P < r.length && (r[P] === " " || r[P] === " ");) P++;
@@ -11981,12 +12086,6 @@ function expandToFullLines(r, j, M) {
11981
12086
  endOffset: P
11982
12087
  };
11983
12088
  }
11984
- function detectLineEnding(r) {
11985
- return r.includes("\r\n") ? "\r\n" : "\n";
11986
- }
11987
- function normalizeSourcePath(r, j) {
11988
- return r.relativeTo(j.dirname).replace(/\.dbml$/, "");
11989
- }
11990
12089
  function pickCompletionItemKind(r) {
11991
12090
  switch (r) {
11992
12091
  case SymbolKind.Schema: return CompletionItemKind.Module;
@@ -12046,7 +12145,7 @@ function addSuggestAllSuggestion(r, j = ", ") {
12046
12145
  }
12047
12146
  function collectCrossFileSuggestions(r, M, N) {
12048
12147
  let P = [], F = /* @__PURE__ */ new Set(), I = r.layout.getSource(N) ?? "";
12049
- for (let L of r.layout.getEntryPoints()) {
12148
+ for (let L of r.layout.getEntrypoints()) {
12050
12149
  if (L.equals(N)) continue;
12051
12150
  let R = r.usableMembers(L).getFiltered(UNHANDLED);
12052
12151
  if (R) for (let j of [...R.nonSchemaMembers, ...R.schemaMembers]) {
@@ -12166,6 +12265,7 @@ function suggestUseCompletion(r, j, M, N, P) {
12166
12265
  }
12167
12266
  function suggestInUseDeclaration(r, j, M, N, P, F) {
12168
12267
  if (N.importPath && isOffsetWithinSpan(M, N.importPath)) return suggestUseFilepath(r, j, N.importPath, P, N);
12268
+ if (N.importPath && M >= N.importPath.end) return noSuggestions();
12169
12269
  if (N.fromKeyword && M > N.fromKeyword.end) return suggestUseFilepath(r, j, void 0, P, N);
12170
12270
  if (N.specifiers instanceof UseSpecifierListNode) {
12171
12271
  if (!N.specifiers.openBrace) {
@@ -12235,14 +12335,14 @@ function suggestUseSpecifierStart() {
12235
12335
  return { suggestions: [{
12236
12336
  label: "* from",
12237
12337
  detail: "import all",
12238
- insertText: "* from '${1:path}'",
12338
+ insertText: "* from '${1:./path}'",
12239
12339
  insertTextRules: CompletionItemInsertTextRule.InsertAsSnippet,
12240
12340
  kind: CompletionItemKind.Snippet,
12241
12341
  range: void 0
12242
12342
  }, {
12243
12343
  label: "{ } from",
12244
12344
  detail: "import named",
12245
- insertText: "{ ${1:table} ${2:name} } from '${3:path}'",
12345
+ insertText: "{ ${1:kind} ${2:name} } from '${3:./path}'",
12246
12346
  insertTextRules: CompletionItemInsertTextRule.InsertAsSnippet,
12247
12347
  kind: CompletionItemKind.Snippet,
12248
12348
  range: void 0
@@ -12259,7 +12359,7 @@ function suggestUseFilepath(r, j, M, N, P) {
12259
12359
  endColumn: r.endColumn - 1
12260
12360
  };
12261
12361
  }
12262
- let z = /* @__PURE__ */ new Map(), B = /* @__PURE__ */ new Map(), V = uniqBy_default(r.layout.getEntryPoints().flatMap((j) => [...r.reachableFiles(j)]), (r) => r.intern());
12362
+ let z = /* @__PURE__ */ new Map(), B = /* @__PURE__ */ new Map(), V = uniqBy_default(r.layout.getEntrypoints().flatMap((j) => [...r.reachableFiles(j)]), (r) => r.intern());
12263
12363
  for (let M of V) {
12264
12364
  if (M.equals(j)) continue;
12265
12365
  let N = M.relativeTo(F);
@@ -13115,7 +13215,7 @@ var dbmlMonarchTokensProvider = {
13115
13215
  }
13116
13216
  return [...N].sort().map(Filepath.from);
13117
13217
  }
13118
- getEntryPoints() {
13218
+ getEntrypoints() {
13119
13219
  return [...this.files.keys()].map(Filepath.from).sort((r, j) => r.absolute.localeCompare(j.absolute));
13120
13220
  }
13121
13221
  clone() {
@@ -13135,7 +13235,7 @@ function containerStack(r, j) {
13135
13235
  for (; R.length > 0;) {
13136
13236
  let M = !1, N = last_default(R);
13137
13237
  if (N instanceof FunctionApplicationNode) {
13138
- let P = this.layout.getSource(r) || "";
13238
+ let P = this.getSource(r) || "";
13139
13239
  for (let r = N.end; r < j; r += 1) P[r] === "\n" && (R.pop(), M = !0);
13140
13240
  } else N instanceof PrefixExpressionNode || N instanceof InfixExpressionNode ? this.container.token(r, j).token !== N.op && (R.pop(), M = !0) : N instanceof ListExpressionNode ? N.listCloseBracket && N.end <= j && (R.pop(), M = !0) : N instanceof TupleExpressionNode ? N.tupleCloseParen && N.end <= j && (R.pop(), M = !0) : N instanceof CommaExpressionNode ? N.end <= j && (R.pop(), M = !0) : N instanceof BlockExpressionNode ? N.blockCloseBrace && N.end <= j && (R.pop(), M = !0) : N instanceof IdentifierStreamNode || N.end < j && (R.pop(), M = !0);
13141
13241
  if (M) {
@@ -13190,33 +13290,41 @@ function containerScopeKind(r, j) {
13190
13290
  default: return ScopeKind.CUSTOM;
13191
13291
  }
13192
13292
  }
13193
- function canonicalName(r, N) {
13194
- let P = N.originalSymbol;
13195
- if (P.filepath.intern() === r.intern()) return Report.create(fullnameToCanonical(this, P));
13196
- let F = this.parseFile(r).getValue().ast, I = this.nodeSymbol(F).getFiltered(UNHANDLED);
13197
- if (!I) return Report.create(fullnameToCanonical(this, P));
13198
- let L = this.symbolMembers(I).getFiltered(UNHANDLED) ?? [], R = [...L];
13199
- for (let r of L) if (r instanceof SchemaSymbol && r.isKind(SymbolKind.Schema)) {
13200
- let M = this.symbolMembers(r).getFiltered(UNHANDLED);
13201
- M && R.push(...M);
13202
- }
13203
- for (let j of R) if (j.filepath.intern() === r.intern() && j.originalSymbol === P && (j instanceof UseSymbol || j instanceof AliasSymbol)) {
13204
- let r = j.name;
13205
- if (!r) continue;
13206
- if (r !== P.name) return Report.create({
13293
+ function canonicalName(r, j) {
13294
+ let N = j.originalSymbol;
13295
+ if (N.filepath.equals(r)) return Report.create(fullnameToCanonical(this, N));
13296
+ let P = [...this.symbolUses(N).getValue(), ...this.symbolAliases(N).getValue()].filter((j) => j.filepath.equals(r));
13297
+ for (let r of P) {
13298
+ let j = r.name;
13299
+ if (!j) continue;
13300
+ if (j !== N.name) return Report.create({
13207
13301
  schema: "",
13208
- name: r
13302
+ name: j
13303
+ });
13304
+ let P = getSchemaChain(this, r);
13305
+ if (P.length > 0) return Report.create({
13306
+ schema: P.join("."),
13307
+ name: j
13209
13308
  });
13210
- let N = fullnameToCanonical(this, P);
13211
- return N ? Report.create({
13212
- ...N,
13213
- name: r
13309
+ let F = fullnameToCanonical(this, N);
13310
+ return F ? Report.create({
13311
+ ...F,
13312
+ name: j
13214
13313
  }) : Report.create({
13215
13314
  schema: DEFAULT_SCHEMA_NAME,
13216
- name: r
13315
+ name: j
13217
13316
  });
13218
13317
  }
13219
- return Report.create(fullnameToCanonical(this, P));
13318
+ return Report.create(fullnameToCanonical(this, N));
13319
+ }
13320
+ function getSchemaChain(r, j) {
13321
+ let M = [], N = j;
13322
+ for (; N;) {
13323
+ let j = r.symbolParent(N).find((r) => r instanceof SchemaSymbol && r.isKind(SymbolKind.Schema));
13324
+ if (!j || j.isPublicSchema()) break;
13325
+ M.unshift(j.name ?? ""), N = j;
13326
+ }
13327
+ return M;
13220
13328
  }
13221
13329
  function fullnameToCanonical(r, M) {
13222
13330
  if (!M.declaration) return M.name ? {
@@ -13243,16 +13351,19 @@ function fileDependencies(r) {
13243
13351
  }
13244
13352
  return M;
13245
13353
  }
13246
- function reachableFiles(r) {
13247
- let j = /* @__PURE__ */ new Set(), M = [], N = r ? [r] : this.layout.getEntryPoints(), P = 0;
13248
- for (; P < N.length;) {
13249
- let r = N[P++], F = r.intern();
13250
- if (!j.has(F)) {
13251
- j.add(F), M.push(r);
13252
- for (let j of this.fileDependencies(r)) N.push(j);
13354
+ function walkDependencies(r, j) {
13355
+ let M = /* @__PURE__ */ new Set(), N = [], P = [...j], F = 0;
13356
+ for (; F < P.length;) {
13357
+ let j = P[F++], I = j.intern();
13358
+ if (!M.has(I)) {
13359
+ M.add(I), N.push(j);
13360
+ for (let M of r.fileDependencies(j)) P.push(M);
13253
13361
  }
13254
13362
  }
13255
- return M;
13363
+ return N;
13364
+ }
13365
+ function reachableFiles(r) {
13366
+ return walkDependencies(this, r ? [r] : this.layout.getEntrypoints());
13256
13367
  }
13257
13368
  function ast(r) {
13258
13369
  return this.bindFile(r), this.parseFile(r).getValue().ast;
@@ -13303,7 +13414,7 @@ function bindFile(r) {
13303
13414
  return new Report(void 0, [...j.getErrors(), ...P.getErrors()], [...j.getWarnings(), ...P.getWarnings()]);
13304
13415
  }
13305
13416
  function bindProject() {
13306
- let r = collectTransitiveDependencies(this, this.layout.getEntryPoints()), j = /* @__PURE__ */ new Map();
13417
+ let r = collectTransitiveDependencies(this, this.layout.getEntrypoints()), j = /* @__PURE__ */ new Map();
13307
13418
  for (let M of r) {
13308
13419
  let r = this.bindFile(M);
13309
13420
  j.set(M.absolute, r);
@@ -13318,7 +13429,7 @@ function interpretFile(r) {
13318
13429
  }
13319
13430
  function interpretProject() {
13320
13431
  let r = [], N = [], P = /* @__PURE__ */ new Set(), F = [];
13321
- for (let r of this.layout.getEntryPoints()) for (let j of this.reachableFiles(r)) {
13432
+ for (let r of this.layout.getEntrypoints()) for (let j of this.reachableFiles(r)) {
13322
13433
  let r = j.intern();
13323
13434
  P.has(r) || (P.add(r), F.push(j));
13324
13435
  }
@@ -13326,8 +13437,10 @@ function interpretProject() {
13326
13437
  for (let P of F) {
13327
13438
  let F = this.parseFile(P);
13328
13439
  r.push(...F.getErrors()), N.push(...F.getWarnings());
13329
- let L = F.getValue().ast, R = this.nodeSymbol(L).getFiltered(UNHANDLED), z = R ? this.interpretSymbol(R, P) : Report.create(UNHANDLED), B = z.getFiltered(UNHANDLED);
13330
- B && I.set(P.absolute, B), r.push(...z.getErrors()), N.push(...z.getWarnings());
13440
+ let L = this.bindFile(P);
13441
+ r.push(...L.getErrors()), N.push(...L.getWarnings());
13442
+ let R = F.getValue().ast, z = this.nodeSymbol(R).getFiltered(UNHANDLED), B = z ? this.interpretSymbol(z, P) : Report.create(UNHANDLED), V = B.getFiltered(UNHANDLED);
13443
+ V && I.set(P.absolute, V), r.push(...B.getErrors()), N.push(...B.getWarnings());
13331
13444
  }
13332
13445
  let L = {};
13333
13446
  for (let [r, j] of I) L[r] = j;
@@ -13681,7 +13794,47 @@ var ParsingContextStack = class {
13681
13794
  }
13682
13795
  }, Parser = class {
13683
13796
  constructor(r, j, M, N) {
13684
- this.current = 0, this.errors = [], this.contextStack = new ParsingContextStack(), this.synchronizeProgram = () => {
13797
+ this.current = 0, this.errors = [], this.contextStack = new ParsingContextStack(), this.useSpecifierList = this.contextStack.withContextDo(ParsingContext.BlockExpression, () => {
13798
+ let r = { specifiers: [] }, j = () => this.nodeFactory.create(UseSpecifierListNode, r);
13799
+ try {
13800
+ this.consume("Expect an opening brace '{'", SyntaxTokenKind.LBRACE), r.openBrace = this.previous();
13801
+ } catch (r) {
13802
+ if (!(r instanceof PartialParsingError)) throw r;
13803
+ if (!this.canHandle(r)) throw new PartialParsingError(r.token, j(), r.handlerContext);
13804
+ this.synchronizeUseSpecifierList();
13805
+ }
13806
+ for (; !this.isAtEnd() && !this.check(SyntaxTokenKind.RBRACE);) {
13807
+ try {
13808
+ r.specifiers.push(this.useSpecifier());
13809
+ } catch (M) {
13810
+ if (!(M instanceof PartialParsingError)) throw M;
13811
+ if (M.partialNode instanceof UseSpecifierNode && r.specifiers.push(M.partialNode), !this.canHandle(M)) throw new PartialParsingError(M.token, j(), M.handlerContext);
13812
+ this.synchronizeUseSpecifierList();
13813
+ }
13814
+ if (this.check(SyntaxTokenKind.IDENTIFIER) && isFromKeyword(this.peek())) break;
13815
+ !hasTrailingNewLines(this.previous()) && !this.check(SyntaxTokenKind.RBRACE) && this.logError(this.peek(), CompileErrorCode.MISSING_SPACES, "Expect a newline between use specifiers");
13816
+ }
13817
+ try {
13818
+ this.consume("Expect a closing brace '}'", SyntaxTokenKind.RBRACE), r.closeBrace = this.previous();
13819
+ } catch (r) {
13820
+ if (!(r instanceof PartialParsingError)) throw r;
13821
+ if (!this.canHandle(r)) throw new PartialParsingError(r.token, j(), r.handlerContext);
13822
+ this.synchronizeUseSpecifierList();
13823
+ }
13824
+ return j();
13825
+ }), this.synchronizeUseSpecifierList = () => {
13826
+ if (!this.check(SyntaxTokenKind.RBRACE)) for (markInvalid(this.advance()); !this.isAtEnd();) {
13827
+ let r = this.peek();
13828
+ if (this.check(SyntaxTokenKind.RBRACE) || this.check(SyntaxTokenKind.STRING_LITERAL) || this.check(SyntaxTokenKind.IDENTIFIER) && isFromKeyword(r) || isAtStartOfLine(this.previous(), r)) break;
13829
+ markInvalid(r), this.advance();
13830
+ }
13831
+ }, this.synchronizeUseSpecifier = () => {
13832
+ for (; !this.isAtEnd();) {
13833
+ let r = this.peek();
13834
+ if (this.check(SyntaxTokenKind.RBRACE) || isAtStartOfLine(this.previous(), r)) break;
13835
+ markInvalid(r), this.advance();
13836
+ }
13837
+ }, this.synchronizeProgram = () => {
13685
13838
  let r = this.peek();
13686
13839
  r.kind === SyntaxTokenKind.EOF ? this.logError(r, CompileErrorCode.UNEXPECTED_EOF, "Unexpected EOF") : markInvalid(this.advance());
13687
13840
  }, this.synchronizeElementDeclarationName = () => {
@@ -13903,59 +14056,39 @@ var ParsingContextStack = class {
13903
14056
  if (this.advance(), r.useKeyword = this.previous(), this.peek().kind === SyntaxTokenKind.WILDCARD) this.advance(), r.specifiers = this.nodeFactory.create(WildcardNode, { token: this.previous() });
13904
14057
  else try {
13905
14058
  r.specifiers = this.useSpecifierList();
13906
- } catch (M) {
13907
- throw M instanceof PartialParsingError ? (r.specifiers = M.partialNode, new PartialParsingError(M.token, j(), M.handlerContext)) : M;
14059
+ } catch (j) {
14060
+ if (!(j instanceof PartialParsingError)) throw j;
14061
+ j.partialNode instanceof UseSpecifierListNode && (r.specifiers = j.partialNode);
13908
14062
  }
13909
14063
  let M = r.specifiers instanceof WildcardNode ? "'*'" : "specifier list";
13910
- if (isFromKeyword(this.peek())) r.fromKeyword = this.advance();
13911
- else throw this.logError(this.peek(), CompileErrorCode.UNEXPECTED_TOKEN, `Expect 'from' after ${M}`), new PartialParsingError(this.peek(), j(), this.contextStack.findHandlerContext(this.tokens, this.current));
13912
- if (this.match(SyntaxTokenKind.STRING_LITERAL)) r.importPath = this.previous();
13913
- else throw this.logError(this.peek(), CompileErrorCode.UNEXPECTED_TOKEN, "Expect a string literal path"), new PartialParsingError(this.peek(), j(), this.contextStack.findHandlerContext(this.tokens, this.current));
13914
- return j();
13915
- }
13916
- useSpecifierList() {
13917
- let r = { specifiers: [] }, j = () => this.nodeFactory.create(UseSpecifierListNode, r);
13918
- try {
13919
- this.consume("Expect an opening brace '{'", SyntaxTokenKind.LBRACE), r.openBrace = this.previous();
13920
- } catch (r) {
13921
- throw r instanceof PartialParsingError ? new PartialParsingError(r.token, j(), r.handlerContext) : r;
13922
- }
13923
- for (; !this.isAtEnd() && !this.check(SyntaxTokenKind.RBRACE);) {
13924
- try {
13925
- r.specifiers.push(this.useSpecifier());
13926
- } catch (M) {
13927
- throw M instanceof PartialParsingError ? (M.partialNode instanceof UseSpecifierNode && r.specifiers.push(M.partialNode), new PartialParsingError(M.token, j(), M.handlerContext)) : M;
13928
- }
13929
- if (this.check(SyntaxTokenKind.IDENTIFIER) && isFromKeyword(this.peek())) break;
13930
- !hasTrailingNewLines(this.previous()) && !this.check(SyntaxTokenKind.RBRACE) && this.logError(this.peek(), CompileErrorCode.MISSING_SPACES, "Expect a newline between use specifiers");
13931
- }
13932
- try {
13933
- this.consume("Expect a closing brace '}'", SyntaxTokenKind.RBRACE), r.closeBrace = this.previous();
13934
- } catch (r) {
13935
- throw r instanceof PartialParsingError ? new PartialParsingError(r.token, j(), r.handlerContext) : r;
13936
- }
13937
- return j();
14064
+ return isFromKeyword(this.peek()) ? r.fromKeyword = this.advance() : this.logError(this.peek(), CompileErrorCode.UNEXPECTED_TOKEN, `Expect 'from' after ${M}`), this.match(SyntaxTokenKind.STRING_LITERAL) ? r.importPath = this.previous() : this.logError(this.peek(), CompileErrorCode.UNEXPECTED_TOKEN, "Expect a string literal path"), j();
13938
14065
  }
13939
14066
  useSpecifier() {
13940
14067
  let r = {}, j = () => this.nodeFactory.create(UseSpecifierNode, r);
13941
14068
  try {
13942
14069
  this.consume("Expect an import kind specifier (e.g. table, enum)", SyntaxTokenKind.IDENTIFIER), r.importKind = this.previous();
13943
14070
  } catch (r) {
13944
- throw r instanceof PartialParsingError ? new PartialParsingError(r.token, j(), r.handlerContext) : r;
14071
+ if (!(r instanceof PartialParsingError)) throw r;
14072
+ if (!this.canHandle(r)) throw new PartialParsingError(r.token, j(), r.handlerContext);
14073
+ this.synchronizeUseSpecifier();
13945
14074
  }
13946
14075
  if (this.check(SyntaxTokenKind.IDENTIFIER, SyntaxTokenKind.QUOTED_STRING)) try {
13947
14076
  r.name = this.normalExpression();
13948
- } catch (r) {
13949
- throw r instanceof PartialParsingError ? new PartialParsingError(r.token, j(), r.handlerContext) : r;
14077
+ } catch (M) {
14078
+ if (!(M instanceof PartialParsingError)) throw M;
14079
+ if (M.partialNode instanceof SyntaxNode && (r.name = M.partialNode), !this.canHandle(M)) throw new PartialParsingError(M.token, j(), M.handlerContext);
14080
+ this.synchronizeUseSpecifier();
13950
14081
  }
13951
- else this.logError(this.peek(), CompileErrorCode.UNEXPECTED_TOKEN, "Expect an element name");
14082
+ else r.importKind && this.logError(this.peek(), CompileErrorCode.UNEXPECTED_TOKEN, "Expect an element name");
13952
14083
  if (isAsKeyword(this.peek())) {
13953
14084
  r.asKeyword = this.advance();
13954
14085
  try {
13955
14086
  if (!this.check(SyntaxTokenKind.IDENTIFIER, SyntaxTokenKind.QUOTED_STRING)) throw this.logError(this.peek(), CompileErrorCode.UNEXPECTED_TOKEN, "Expect an alias name after 'as'"), new PartialParsingError(this.peek(), j(), this.contextStack.findHandlerContext(this.tokens, this.current));
13956
14087
  r.alias = this.normalExpression();
13957
14088
  } catch (r) {
13958
- throw r instanceof PartialParsingError ? new PartialParsingError(r.token, j(), r.handlerContext) : r;
14089
+ if (!(r instanceof PartialParsingError)) throw r;
14090
+ if (!this.canHandle(r)) throw new PartialParsingError(r.token, j(), r.handlerContext);
14091
+ this.synchronizeUseSpecifier();
13959
14092
  }
13960
14093
  }
13961
14094
  return j();
@@ -14335,7 +14468,7 @@ function postfixBindingPower(r) {
14335
14468
  };
14336
14469
  }
14337
14470
  function parseFile(r) {
14338
- let j = this.layout.getSource(r);
14471
+ let j = this.getSource(r);
14339
14472
  return new Lexer(j ?? "", r).lex().chain((M) => new Parser(j ?? "", M, this.nodeIdGenerator, r).parse()).map(({ ast: j, tokens: M }) => ({
14340
14473
  ast: j,
14341
14474
  tokens: M,
@@ -14343,7 +14476,7 @@ function parseFile(r) {
14343
14476
  }));
14344
14477
  }
14345
14478
  function parseProject() {
14346
- let r = collectTransitiveDependencies(this, this.layout.getEntryPoints()), j = /* @__PURE__ */ new Map();
14479
+ let r = collectTransitiveDependencies(this, this.layout.getEntrypoints()), j = /* @__PURE__ */ new Map();
14347
14480
  for (let M of r) {
14348
14481
  let r = this.parseFile(M);
14349
14482
  j.set(M.absolute, r);
@@ -14414,11 +14547,30 @@ function resolutionIndex() {
14414
14547
  };
14415
14548
  N(M);
14416
14549
  }
14550
+ let L = /* @__PURE__ */ new Map(), R = /* @__PURE__ */ new Set();
14551
+ function z(r, M) {
14552
+ let N = M.intern();
14553
+ if (R.has(N)) return;
14554
+ R.add(N);
14555
+ let P = r.symbolMembers(M).getFiltered(UNHANDLED);
14556
+ if (P) for (let j of P) {
14557
+ let N = j.intern(), P = L.get(N);
14558
+ P || (P = [], L.set(N, P)), P.push(M), j instanceof SchemaSymbol && j.isKind(SymbolKind.Schema) && z(r, j);
14559
+ }
14560
+ }
14561
+ for (let r of N.values()) {
14562
+ let M = r.getValue().ast, N = this.nodeSymbol(M).getFiltered(UNHANDLED);
14563
+ N && z(this, N);
14564
+ }
14417
14565
  return {
14418
14566
  references: r,
14419
- metadata: M
14567
+ metadata: M,
14568
+ parents: L
14420
14569
  };
14421
14570
  }
14571
+ function symbolParent(r) {
14572
+ return this.resolutionIndex().parents.get(r.intern()) ?? [];
14573
+ }
14422
14574
  function symbolReferences(r) {
14423
14575
  return this.resolutionIndex().references.get(r.intern()) ?? [];
14424
14576
  }
@@ -14527,59 +14679,60 @@ function findReplacements(r, j, M, N, P) {
14527
14679
  return F;
14528
14680
  }
14529
14681
  function renameTable(r, M, N) {
14530
- let P = this.layout.clone(), F = normalizeTableName(M), I = normalizeTableName(N), L = F.schema, R = F.table, z = I.schema, B = I.table, V = lookupTableSymbol(this, r, L, R);
14531
- if (!V || V instanceof AliasSymbol || checkForNameCollision(this, r, L, R, z, B)) return P;
14532
- if (V instanceof UseSymbol && V.useSpecifierDeclaration instanceof UseSpecifierNode && V.useSpecifierDeclaration.alias) {
14533
- let M = V.useSpecifierDeclaration;
14534
- if (this.nodeAlias(M).getFiltered(UNHANDLED) === R) return renameAlias(this, P, r, M, V, z, B);
14535
- }
14536
- return renameRealDeclaration(this, P, V, L, R, z, B);
14537
- }
14538
- function renameAlias(r, j, M, N, P, F, I) {
14539
- let L = r.layout.getSource(M) ?? "";
14540
- if (!N.alias) return j;
14541
- let R = formatTableName(F, I, L.substring(N.alias.start, N.alias.end).includes("\"")), z = [];
14542
- z.push({
14543
- start: N.alias.start,
14544
- end: N.alias.end,
14545
- newText: R.formattedTable
14682
+ let P = normalizeTableName(M), F = normalizeTableName(N), I = P.schema, L = P.table, R = F.schema, z = F.table, B = lookupTableSymbol(this, r, I, L);
14683
+ if (!B || B instanceof AliasSymbol || checkForNameCollision(this, r, I, L, R, z)) return /* @__PURE__ */ new Map();
14684
+ if (B instanceof UseSymbol && B.useSpecifierDeclaration instanceof UseSpecifierNode && B.useSpecifierDeclaration.alias) {
14685
+ let M = B.useSpecifierDeclaration;
14686
+ if (this.nodeAlias(M).getFiltered(UNHANDLED) === L) return renameAlias(this, r, M, B, R, z);
14687
+ }
14688
+ return renameRealDeclaration(this, B, I, L, R, z);
14689
+ }
14690
+ function renameAlias(r, j, M, N, P, F) {
14691
+ let I = r.getSource(j) ?? "";
14692
+ if (!M.alias) return /* @__PURE__ */ new Map();
14693
+ let L = formatTableName(P, F, I.substring(M.alias.start, M.alias.end).includes("\"")), R = [];
14694
+ R.push({
14695
+ start: M.alias.start,
14696
+ end: M.alias.end,
14697
+ newText: L.formattedTable
14546
14698
  });
14547
- let B = r.symbolReferences(P);
14548
- for (let r of B) r.filepath.absolute === M.absolute && r !== N.alias && z.push({
14699
+ let z = r.symbolReferences(N);
14700
+ for (let r of z) r.filepath.absolute === j.absolute && r !== M.alias && R.push({
14549
14701
  start: r.start,
14550
14702
  end: r.end,
14551
- newText: R.formattedTable
14703
+ newText: L.formattedTable
14552
14704
  });
14553
- return j.setSource(M, applyTextEdits(L, z)), j;
14554
- }
14555
- function renameRealDeclaration(r, j, M, N, P, F, I) {
14556
- let L = M.originalSymbol, R = L.declaration;
14557
- if (!R || !(R instanceof ElementDeclarationNode)) return j;
14558
- let z = R.filepath;
14559
- if (!z) return j;
14560
- let B = formatTableName(F, I, checkIfDeclarationUsesQuotes(R, r.layout.getSource(z) ?? "")), V = /* @__PURE__ */ new Map(), H = (r, j) => {
14561
- let M = r.absolute, N = V.get(M);
14705
+ return new Map([[j.absolute, applyTextEdits(I, R)]]);
14706
+ }
14707
+ function renameRealDeclaration(r, j, M, N, P, F) {
14708
+ let I = j.originalSymbol, L = I.declaration;
14709
+ if (!L || !(L instanceof ElementDeclarationNode)) return /* @__PURE__ */ new Map();
14710
+ let R = L.filepath;
14711
+ if (!R) return /* @__PURE__ */ new Map();
14712
+ let z = formatTableName(P, F, checkIfDeclarationUsesQuotes(L, r.getSource(R) ?? "")), B = /* @__PURE__ */ new Map(), V = (r, j) => {
14713
+ let M = r.absolute, N = B.get(M);
14562
14714
  N || (N = {
14563
14715
  fp: r,
14564
14716
  edits: []
14565
- }, V.set(M, N)), N.edits.push(j);
14566
- }, U = R.name;
14567
- U && H(z, {
14568
- start: U.start,
14569
- end: U.end,
14570
- newText: buildReplacementText(B)
14717
+ }, B.set(M, N)), N.edits.push(j);
14718
+ }, H = L.name;
14719
+ H && V(R, {
14720
+ start: H.start,
14721
+ end: H.end,
14722
+ newText: buildReplacementText(z)
14571
14723
  });
14572
- let W = r.symbolReferences(L);
14573
- for (let j of W) {
14574
- if (j === U) continue;
14575
- let M = j.filepath, F = r.layout.getSource(M) ?? "";
14576
- for (let r of findReplacements([j], N, P, B, F)) H(M, r);
14724
+ let U = r.symbolReferences(I);
14725
+ for (let j of U) {
14726
+ if (j === H) continue;
14727
+ let P = j.filepath, F = r.getSource(P) ?? "";
14728
+ for (let r of findReplacements([j], M, N, z, F)) V(P, r);
14577
14729
  }
14578
- for (let { fp: M, edits: N } of V.values()) {
14579
- let P = r.layout.getSource(M) ?? "";
14580
- j.setSource(M, applyTextEdits(P, N));
14730
+ let W = /* @__PURE__ */ new Map();
14731
+ for (let { fp: j, edits: M } of B.values()) {
14732
+ let N = r.getSource(j) ?? "";
14733
+ W.set(j.absolute, applyTextEdits(N, M));
14581
14734
  }
14582
- return j;
14735
+ return W;
14583
14736
  }
14584
14737
  function findDiagramViewBlocks(r) {
14585
14738
  let j = [], M = new Lexer(r, DEFAULT_ENTRY).lex();
@@ -14642,7 +14795,7 @@ function generateDiagramViewBlock(r, j) {
14642
14795
  return B.length > 0 && emitTablesBlock(G, B), V.length > 0 && emitTableGroupsBlock(G, V), H.length > 0 && emitSchemasBlock(G, H), W && emitNotesBlock(G, I), G.push("}"), G.join("\n");
14643
14796
  }
14644
14797
  function syncDiagramView(r, j, M) {
14645
- let N = this.layout.getSource(r) ?? "", P = M ?? findDiagramViewBlocks(N), F = [];
14798
+ let N = this.getSource(r) ?? "", P = M ?? findDiagramViewBlocks(N), F = [];
14646
14799
  for (let r of j) {
14647
14800
  let j = applyOperation(N, r, P);
14648
14801
  F.push(...j);
@@ -14702,12 +14855,12 @@ function computeDeleteEdit(r, j, M) {
14702
14855
  }];
14703
14856
  }
14704
14857
  var COMPUTING = Symbol("COMPUTING"), Compiler = class {
14705
- constructor() {
14706
- this.nodeIdGenerator = new SyntaxNodeIdGenerator(), this.symbolIdGenerator = new NodeSymbolIdGenerator(), this.symbolFactory = new SymbolFactory(this.symbolIdGenerator), this.layout = new MemoryProjectLayout(), this.globalCache = /* @__PURE__ */ new Map(), this.localCache = /* @__PURE__ */ new Map(), this.parseFile = this.localQuery(parseFile), this.parseProject = this.globalQuery(parseProject), this.validateNode = this.localQuery(validateNode), this.validateFile = this.localQuery(validateFile), this.nodeFullname = this.localQuery(nodeFullname), this.nodeAlias = this.localQuery(nodeAlias), this.nodeSettings = this.localQuery(nodeSettings), this.canonicalName = this.globalQuery(canonicalName), this.bindNode = this.globalQuery(bindNode), this.bindFile = this.globalQuery(bindFile), this.bindProject = this.globalQuery(bindProject), this.interpretSymbol = this.globalQuery(interpretSymbol), this.interpretMetadata = interpretMetadata.bind(this), this.interpretFile = this.globalQuery(interpretFile), this.interpretProject = this.globalQuery(interpretProject), this.nodeSymbol = this.globalQuery(nodeSymbol), this.nodeMetadata = this.globalQuery(nodeMetadata), this.symbolMembers = this.globalQuery(symbolMembers), this.lookupMembers = this.globalQuery(lookupMembers), this.nodeReferee = this.globalQuery(nodeReferee), this.resolutionIndex = this.globalQuery(resolutionIndex), this.symbolReferences = this.globalQuery(symbolReferences), this.symbolMetadata = this.globalQuery(symbolMetadata), this.symbolAliases = this.globalQuery(symbolAliases), this.symbolUses = this.globalQuery(symbolUses), this.fileDependencies = this.localQuery(fileDependencies), this.reachableFiles = this.globalQuery(reachableFiles), this.usableMembers = this.globalQuery(usableMembers), this.nodeAtPosition = this.localQuery(nodeAtPosition), this.renameTable = renameTable.bind(this), this.syncDiagramView = syncDiagramView.bind(this), this.token = {
14858
+ constructor(r = new MemoryProjectLayout()) {
14859
+ this.nodeIdGenerator = new SyntaxNodeIdGenerator(), this.symbolIdGenerator = new NodeSymbolIdGenerator(), this.symbolFactory = new SymbolFactory(this.symbolIdGenerator), this.sourceSnapshot = /* @__PURE__ */ new Map(), this.globalCache = /* @__PURE__ */ new Map(), this.localCache = /* @__PURE__ */ new Map(), this.parseFile = this.localQuery(parseFile), this.parseProject = this.globalQuery(parseProject), this.validateNode = this.localQuery(validateNode), this.validateFile = this.localQuery(validateFile), this.nodeFullname = this.localQuery(nodeFullname), this.nodeAlias = this.localQuery(nodeAlias), this.nodeSettings = this.localQuery(nodeSettings), this.canonicalName = this.globalQuery(canonicalName), this.bindNode = this.globalQuery(bindNode), this.bindFile = this.globalQuery(bindFile), this.bindProject = this.globalQuery(bindProject), this.interpretSymbol = this.globalQuery(interpretSymbol), this.interpretMetadata = interpretMetadata.bind(this), this.interpretFile = this.globalQuery(interpretFile), this.interpretProject = this.globalQuery(interpretProject), this.nodeSymbol = this.globalQuery(nodeSymbol), this.nodeMetadata = this.globalQuery(nodeMetadata), this.symbolMembers = this.globalQuery(symbolMembers), this.lookupMembers = this.globalQuery(lookupMembers), this.nodeReferee = this.globalQuery(nodeReferee), this.resolutionIndex = this.globalQuery(resolutionIndex), this.symbolReferences = this.globalQuery(symbolReferences), this.symbolParent = this.globalQuery(symbolParent), this.symbolMetadata = this.globalQuery(symbolMetadata), this.symbolAliases = this.globalQuery(symbolAliases), this.symbolUses = this.globalQuery(symbolUses), this.fileDependencies = this.localQuery(fileDependencies), this.reachableFiles = this.globalQuery(reachableFiles), this.usableMembers = this.globalQuery(usableMembers), this.nodeAtPosition = this.localQuery(nodeAtPosition), this.renameTable = renameTable.bind(this), this.syncDiagramView = syncDiagramView.bind(this), this.token = {
14707
14860
  invalidStream: this.localQuery(invalidStream),
14708
14861
  flatStream: this.localQuery(flatStream)
14709
14862
  }, this.parse = {
14710
- source: (r) => this.layout.getSource(r) || "",
14863
+ source: (r) => this.getSource(r) || "",
14711
14864
  ast: (r = DEFAULT_ENTRY) => ast.call(this, r),
14712
14865
  errors: (r = DEFAULT_ENTRY) => errors.call(this, r),
14713
14866
  warnings: (r = DEFAULT_ENTRY) => warnings.call(this, r),
@@ -14720,21 +14873,25 @@ var COMPUTING = Symbol("COMPUTING"), Compiler = class {
14720
14873
  element: this.localQuery(containerElement),
14721
14874
  scope: this.localQuery(containerScope),
14722
14875
  scopeKind: this.localQuery(containerScopeKind)
14723
- };
14876
+ }, this.layout = r;
14724
14877
  }
14725
- setSource(r, j) {
14726
- this.layout.setSource(r, j), this.localCache.delete(r.intern()), this.globalCache.clear();
14878
+ getSource(r) {
14879
+ return this.layout.getSource(r);
14727
14880
  }
14728
- clearSource() {
14729
- this.layout = new MemoryProjectLayout(), this.localCache.clear(), this.globalCache.clear();
14881
+ cleanStaleLocalCache(r) {
14882
+ let j = this.layout.getSource(r), M = r.absolute;
14883
+ this.sourceSnapshot.has(M) && this.sourceSnapshot.get(M) !== j && (this.localCache.delete(r.intern()), this.globalCache.clear()), this.sourceSnapshot.set(M, j);
14730
14884
  }
14731
- deleteSource(r) {
14732
- this.layout.deleteSource(r), this.localCache.delete(r.intern()), this.globalCache.clear();
14885
+ cleanStaleGlobalCache() {
14886
+ let r = this.layout.getEntrypoints(), j = !1;
14887
+ this.entrypointsSnapshot !== void 0 && (this.entrypointsSnapshot.length !== r.length || this.entrypointsSnapshot.some((j, M) => j.absolute !== r[M].absolute)) && (j = !0), this.entrypointsSnapshot = r;
14888
+ for (let j of walkDependencies(this, r)) this.cleanStaleLocalCache(j);
14889
+ j && this.globalCache.clear();
14733
14890
  }
14734
14891
  globalQuery(r) {
14735
14892
  let j = Symbol();
14736
14893
  return ((...M) => {
14737
- this.globalCache.has(j) || this.globalCache.set(j, /* @__PURE__ */ new Map());
14894
+ this.cleanStaleGlobalCache(), this.globalCache.has(j) || this.globalCache.set(j, /* @__PURE__ */ new Map());
14738
14895
  let N = M.map((r) => intern(r)).join("\0"), P = this.globalCache.get(j);
14739
14896
  if (P.has(N)) {
14740
14897
  let j = P.get(N);
@@ -14753,25 +14910,27 @@ var COMPUTING = Symbol("COMPUTING"), Compiler = class {
14753
14910
  localQuery(r) {
14754
14911
  let j = Symbol();
14755
14912
  return ((...M) => {
14756
- let [N] = M, P = (N instanceof SyntaxNode ? N.filepath : N).intern(), F = M.map((r) => intern(r)).join("\0"), I = this.localCache.get(P);
14757
- I || (I = /* @__PURE__ */ new Map(), this.localCache.set(P, I));
14758
- let L = I.get(j);
14759
- if (L || (L = /* @__PURE__ */ new Map(), I.set(j, L)), L.has(F)) {
14760
- let j = L.get(F);
14761
- if (j === COMPUTING) throw Error(`Cycle detected in query: ${r.name}(${P}, ${F})`);
14913
+ let [N] = M, P = N instanceof SyntaxNode ? N.filepath : N;
14914
+ this.cleanStaleLocalCache(P);
14915
+ let F = P.intern(), I = M.map((r) => intern(r)).join("\0"), L = this.localCache.get(F);
14916
+ L || (L = /* @__PURE__ */ new Map(), this.localCache.set(F, L));
14917
+ let R = L.get(j);
14918
+ if (R || (R = /* @__PURE__ */ new Map(), L.set(j, R)), R.has(I)) {
14919
+ let j = R.get(I);
14920
+ if (j === COMPUTING) throw Error(`Cycle detected in query: ${r.name}(${F}, ${I})`);
14762
14921
  return j;
14763
14922
  }
14764
- L.set(F, COMPUTING);
14923
+ R.set(I, COMPUTING);
14765
14924
  try {
14766
14925
  let j = r.apply(this, M);
14767
- return L.set(F, j), j;
14926
+ return R.set(I, j), j;
14768
14927
  } catch (r) {
14769
- throw L.delete(F), r;
14928
+ throw R.delete(I), r;
14770
14929
  }
14771
14930
  });
14772
14931
  }
14773
14932
  findDiagramViewBlocks(r) {
14774
- return findDiagramViewBlocks(this.layout.getSource(r) ?? "");
14933
+ return findDiagramViewBlocks(this.getSource(r) ?? "");
14775
14934
  }
14776
14935
  initMonacoServices(r) {
14777
14936
  let j = r?.autocompletion?.triggerCharacters ?? [
@@ -14797,4 +14956,4 @@ var COMPUTING = Symbol("COMPUTING"), Compiler = class {
14797
14956
  };
14798
14957
  }
14799
14958
  };
14800
- export { ArrayNode, AttributeNode, BlockExpressionNode, CallExpressionNode, CommaExpressionNode, CompileError, CompileErrorCode, Compiler, CompletionItemInsertTextRule, CompletionItemKind, DBMLCompletionItemProvider, DBMLDefinitionProvider, DBMLDiagnosticsProvider, DBMLReferencesProvider, DEFAULT_ENTRY, ElementDeclarationNode, ElementKind, EmptyNode, FALSY_VALUES, Filepath, FunctionApplicationNode, FunctionExpressionNode, GroupExpressionNode, IdentifierStreamNode, InfixExpressionNode, ListExpressionNode, LiteralNode, MarkerSeverity, MemoryProjectLayout, PostfixExpressionNode, PrefixExpressionNode, PrimaryExpressionNode, ProgramNode, ScopeKind, SymbolKind, SyntaxNode, SyntaxNodeIdGenerator, SyntaxNodeKind, SyntaxToken, SyntaxTokenKind, TRUTHY_VALUES, TupleExpressionNode, UNHANDLED, URI as Uri, UseDeclarationNode, UseSpecifierListNode, UseSpecifierNode, VariableNode, WildcardNode, addDoubleQuoteIfNeeded, dbmlLanguageConfig, dbmlMonarchTokensProvider, escapeString, extractNumericLiteral, extractSignedNumber, formatRecordValue, getRecordValueType, isBinaryType, isBooleanType, isDateTimeType, isEmptyStringLiteral, isFloatType, isFunctionExpression, isIntegerType, isNullish, isNumericType, isOp, isOpToken, isSerialType, isStringType, isTriviaToken, isValidIdentifier, normalizeTypeName, splitQualifiedIdentifier, tryExtractBoolean, tryExtractDateTime, tryExtractEnum, tryExtractInteger, tryExtractNumeric, tryExtractString, unescapeString };
14959
+ export { ArrayNode, AttributeNode, BlockExpressionNode, CallExpressionNode, CommaExpressionNode, CompileError, CompileErrorCode, Compiler, CompletionItemInsertTextRule, CompletionItemKind, DBMLCompletionItemProvider, DBMLDefinitionProvider, DBMLDiagnosticsProvider, DBMLReferencesProvider, DEFAULT_ENTRY, ElementDeclarationNode, ElementKind, EmptyNode, FALSY_VALUES, Filepath, FunctionApplicationNode, FunctionExpressionNode, GroupExpressionNode, IdentifierStreamNode, InfixExpressionNode, ListExpressionNode, LiteralNode, MarkerSeverity, MemoryProjectLayout, PostfixExpressionNode, PrefixExpressionNode, PrimaryExpressionNode, ProgramNode, ScopeKind, SymbolKind, SyntaxNode, SyntaxNodeIdGenerator, SyntaxNodeKind, SyntaxToken, SyntaxTokenKind, TRUTHY_VALUES, TupleExpressionNode, UNHANDLED, URI as Uri, UseDeclarationNode, UseSpecifierListNode, UseSpecifierNode, VariableNode, WildcardNode, addDoubleQuoteIfNeeded, dbmlLanguageConfig, dbmlMonarchTokensProvider, escapeString, extractNumericLiteral, extractSignedNumber, formatRecordValue, getRecordValueType, isBinaryType, isBooleanType, isDateTimeType, isEmptyStringLiteral, isFloatType, isFunctionExpression, isIntegerType, isNullish, isNumericType, isOp, isOpToken, isSerialType, isStringType, isTriviaToken, isValidIdentifier, normalizeTypeName, splitQualifiedIdentifier, tryExtractBoolean, tryExtractDateTime, tryExtractEnum, tryExtractInteger, tryExtractNumeric, tryExtractString, unescapeString, validateForeignKeys, validatePrimaryKey, validateUnique };