@dbml/core 8.0.0-alpha.0 → 8.0.0-alpha.5

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 (3) hide show
  1. package/lib/index.cjs +107 -107
  2. package/lib/index.mjs +206 -110
  3. package/package.json +3 -3
package/lib/index.mjs CHANGED
@@ -2475,13 +2475,13 @@ var isAbsolute = function(_) {
2475
2475
  function resolveImportFilepath(_, VY) {
2476
2476
  if (!Filepath.isRelative(VY)) return;
2477
2477
  let P = Filepath.resolve(_.dirname, VY);
2478
- return P.absolute.endsWith(".dbml") ? P : Filepath.from(P.absolute + DBML_EXT);
2478
+ return P.absolute.endsWith(".dbml") ? P : Filepath.from(P.absolute + ".dbml");
2479
2479
  }
2480
2480
  var KEYWORDS_OF_DEFAULT_SETTING = [
2481
2481
  "null",
2482
2482
  "true",
2483
2483
  "false"
2484
- ], DBML_EXT = ".dbml", NUMERIC_LITERAL_PREFIX = ["-", "+"], DEFAULT_SCHEMA_NAME$1 = "public", DEFAULT_ENTRY = Filepath.from("/main.dbml"), CompileErrorCode = /* @__PURE__ */ function(_) {
2484
+ ], NUMERIC_LITERAL_PREFIX = ["-", "+"], DEFAULT_SCHEMA_NAME$1 = "public", DEFAULT_ENTRY = Filepath.from("/main.dbml"), CompileErrorCode = /* @__PURE__ */ function(_) {
2485
2485
  return _[_.UNKNOWN_SYMBOL = 1e3] = "UNKNOWN_SYMBOL", _[_.UNEXPECTED_SYMBOL = 1001] = "UNEXPECTED_SYMBOL", _[_.UNEXPECTED_EOF = 1002] = "UNEXPECTED_EOF", _[_.UNEXPECTED_NEWLINE = 1003] = "UNEXPECTED_NEWLINE", _[_.UNKNOWN_TOKEN = 1004] = "UNKNOWN_TOKEN", _[_.UNEXPECTED_TOKEN = 1005] = "UNEXPECTED_TOKEN", _[_.UNEXPECTED_ELEMENT_DECLARATION = 1006] = "UNEXPECTED_ELEMENT_DECLARATION", _[_.MISSING_SPACES = 1007] = "MISSING_SPACES", _[_.UNKNOWN_PREFIX_OP = 1008] = "UNKNOWN_PREFIX_OP", _[_.INVALID_OPERAND = 1009] = "INVALID_OPERAND", _[_.EMPTY_ATTRIBUTE_NAME = 1010] = "EMPTY_ATTRIBUTE_NAME", _[_.INVALID_ESCAPE_SEQUENCE = 1011] = "INVALID_ESCAPE_SEQUENCE", _[_.INVALID_NAME = 3e3] = "INVALID_NAME", _[_.UNEXPECTED_NAME = 3001] = "UNEXPECTED_NAME", _[_.NAME_NOT_FOUND = 3002] = "NAME_NOT_FOUND", _[_.DUPLICATE_NAME = 3003] = "DUPLICATE_NAME", _[_.INVALID_ALIAS = 3004] = "INVALID_ALIAS", _[_.UNEXPECTED_ALIAS = 3005] = "UNEXPECTED_ALIAS", _[_.UNEXPECTED_SETTINGS = 3006] = "UNEXPECTED_SETTINGS", _[_.INVALID_SETTINGS = 3007] = "INVALID_SETTINGS", _[_.UNEXPECTED_SIMPLE_BODY = 3008] = "UNEXPECTED_SIMPLE_BODY", _[_.UNEXPECTED_COMPLEX_BODY = 3009] = "UNEXPECTED_COMPLEX_BODY", _[_.INVALID_TABLE_CONTEXT = 3010] = "INVALID_TABLE_CONTEXT", _[_.UNKNOWN_TABLE_SETTING = 3011] = "UNKNOWN_TABLE_SETTING", _[_.DUPLICATE_TABLE_SETTING = 3012] = "DUPLICATE_TABLE_SETTING", _[_.INVALID_TABLEGROUP_CONTEXT = 3013] = "INVALID_TABLEGROUP_CONTEXT", _[_.INVALID_TABLEGROUP_ELEMENT_NAME = 3014] = "INVALID_TABLEGROUP_ELEMENT_NAME", _[_.DUPLICATE_TABLEGROUP_ELEMENT_NAME = 3015] = "DUPLICATE_TABLEGROUP_ELEMENT_NAME", _[_.DUPLICATE_TABLEGROUP_FIELD_NAME = 3016] = "DUPLICATE_TABLEGROUP_FIELD_NAME", _[_.INVALID_TABLEGROUP_FIELD = 3017] = "INVALID_TABLEGROUP_FIELD", _[_.EMPTY_TABLE = 3018] = "EMPTY_TABLE", _[_.INVALID_COLUMN = 3019] = "INVALID_COLUMN", _[_.INVALID_COLUMN_NAME = 3020] = "INVALID_COLUMN_NAME", _[_.UNKNOWN_COLUMN_SETTING = 3021] = "UNKNOWN_COLUMN_SETTING", _[_.INVALID_COLUMN_TYPE = 3022] = "INVALID_COLUMN_TYPE", _[_.DUPLICATE_COLUMN_NAME = 3023] = "DUPLICATE_COLUMN_NAME", _[_.DUPLICATE_COLUMN_SETTING = 3024] = "DUPLICATE_COLUMN_SETTING", _[_.INVALID_COLUMN_SETTING_VALUE = 3025] = "INVALID_COLUMN_SETTING_VALUE", _[_.INVALID_ENUM_CONTEXT = 3026] = "INVALID_ENUM_CONTEXT", _[_.INVALID_ENUM_ELEMENT_NAME = 3027] = "INVALID_ENUM_ELEMENT_NAME", _[_.INVALID_ENUM_ELEMENT = 3028] = "INVALID_ENUM_ELEMENT", _[_.DUPLICATE_ENUM_ELEMENT_NAME = 3029] = "DUPLICATE_ENUM_ELEMENT_NAME", _[_.UNKNOWN_ENUM_ELEMENT_SETTING = 3030] = "UNKNOWN_ENUM_ELEMENT_SETTING", _[_.DUPLICATE_ENUM_ELEMENT_SETTING = 3031] = "DUPLICATE_ENUM_ELEMENT_SETTING", _[_.INVALID_ENUM_ELEMENT_SETTING = 3032] = "INVALID_ENUM_ELEMENT_SETTING", _[_.EMPTY_ENUM = 3033] = "EMPTY_ENUM", _[_.INVALID_REF_CONTEXT = 3034] = "INVALID_REF_CONTEXT", _[_.UNKNOWN_REF_SETTING = 3035] = "UNKNOWN_REF_SETTING", _[_.DUPLICATE_REF_SETTING = 3036] = "DUPLICATE_REF_SETTING", _[_.INVALID_REF_SETTING_VALUE = 3037] = "INVALID_REF_SETTING_VALUE", _[_.INVALID_REF_RELATIONSHIP = 3038] = "INVALID_REF_RELATIONSHIP", _[_.INVALID_REF_FIELD = 3039] = "INVALID_REF_FIELD", _[_.EMPTY_REF = 3040] = "EMPTY_REF", _[_.REF_REDEFINED = 3041] = "REF_REDEFINED", _[_.INVALID_NOTE_CONTEXT = 3042] = "INVALID_NOTE_CONTEXT", _[_.INVALID_NOTE = 3043] = "INVALID_NOTE", _[_.NOTE_REDEFINED = 3044] = "NOTE_REDEFINED", _[_.NOTE_CONTENT_REDEFINED = 3045] = "NOTE_CONTENT_REDEFINED", _[_.EMPTY_NOTE = 3046] = "EMPTY_NOTE", _[_.INVALID_INDEXES_CONTEXT = 3047] = "INVALID_INDEXES_CONTEXT", _[_.INVALID_INDEXES_FIELD = 3048] = "INVALID_INDEXES_FIELD", _[_.INVALID_INDEX = 3049] = "INVALID_INDEX", _[_.UNKNOWN_INDEX_SETTING = 3050] = "UNKNOWN_INDEX_SETTING", _[_.DUPLICATE_INDEX_SETTING = 3051] = "DUPLICATE_INDEX_SETTING", _[_.UNEXPECTED_INDEX_SETTING_VALUE = 3052] = "UNEXPECTED_INDEX_SETTING_VALUE", _[_.INVALID_INDEX_SETTING_VALUE = 3053] = "INVALID_INDEX_SETTING_VALUE", _[_.INVALID_PROJECT_CONTEXT = 3054] = "INVALID_PROJECT_CONTEXT", _[_.PROJECT_REDEFINED = 3055] = "PROJECT_REDEFINED", _[_.INVALID_PROJECT_FIELD = 3056] = "INVALID_PROJECT_FIELD", _[_.INVALID_CUSTOM_CONTEXT = 3057] = "INVALID_CUSTOM_CONTEXT", _[_.INVALID_CUSTOM_ELEMENT_VALUE = 3058] = "INVALID_CUSTOM_ELEMENT_VALUE", _[_.INVALID_ELEMENT_IN_SIMPLE_BODY = 3059] = "INVALID_ELEMENT_IN_SIMPLE_BODY", _[_.INVALID_TABLE_PARTIAL_CONTEXT = 3060] = "INVALID_TABLE_PARTIAL_CONTEXT", _[_.INVALID_TABLE_PARTIAL_ELEMENT_NAME = 3061] = "INVALID_TABLE_PARTIAL_ELEMENT_NAME", _[_.INVALID_TABLE_PARTIAL_SETTING_VALUE = 3062] = "INVALID_TABLE_PARTIAL_SETTING_VALUE", _[_.DUPLICATE_TABLE_PARTIAL_ELEMENT_NAME = 3063] = "DUPLICATE_TABLE_PARTIAL_ELEMENT_NAME", _[_.DUPLICATE_TABLE_PARTIAL_SETTING = 3064] = "DUPLICATE_TABLE_PARTIAL_SETTING", _[_.INVALID_TABLE_PARTIAL_INJECTION = 3065] = "INVALID_TABLE_PARTIAL_INJECTION", _[_.INVALID_TABLE_PARTIAL_INJECTION_OP = 3066] = "INVALID_TABLE_PARTIAL_INJECTION_OP", _[_.INVALID_TABLE_PARTIAL_INJECTION_NAME = 3067] = "INVALID_TABLE_PARTIAL_INJECTION_NAME", _[_.DUPLICATE_TABLE_PARTIAL_INJECTION_NAME = 3068] = "DUPLICATE_TABLE_PARTIAL_INJECTION_NAME", _[_.UNKNOWN_TABLE_PARTIAL_SETTING = 3069] = "UNKNOWN_TABLE_PARTIAL_SETTING", _[_.INVALID_TABLE_SETTING_VALUE = 3070] = "INVALID_TABLE_SETTING_VALUE", _[_.INVALID_CHECKS_CONTEXT = 3071] = "INVALID_CHECKS_CONTEXT", _[_.INVALID_CHECKS_FIELD = 3072] = "INVALID_CHECKS_FIELD", _[_.INVALID_CHECK = 3073] = "INVALID_CHECK", _[_.UNKNOWN_CHECK_SETTING = 3074] = "UNKNOWN_CHECK_SETTING", _[_.DUPLICATE_CHECK_SETTING = 3075] = "DUPLICATE_CHECK_SETTING", _[_.INVALID_CHECK_SETTING_VALUE = 3076] = "INVALID_CHECK_SETTING_VALUE", _[_.INVALID_RECORDS_CONTEXT = 3077] = "INVALID_RECORDS_CONTEXT", _[_.INVALID_RECORDS_NAME = 3078] = "INVALID_RECORDS_NAME", _[_.INVALID_RECORDS_FIELD = 3079] = "INVALID_RECORDS_FIELD", _[_.DUPLICATE_COLUMN_REFERENCES_IN_RECORDS = 3080] = "DUPLICATE_COLUMN_REFERENCES_IN_RECORDS", _[_.DUPLICATE_RECORDS_FOR_TABLE = 3081] = "DUPLICATE_RECORDS_FOR_TABLE", _[_.INVALID_USE_CONTEXT = 3082] = "INVALID_USE_CONTEXT", _[_.INVALID_USE_SPECIFIER_KIND = 3083] = "INVALID_USE_SPECIFIER_KIND", _[_.INVALID_USE_SPECIFIER_NAME = 3084] = "INVALID_USE_SPECIFIER_NAME", _[_.INVALID_DIAGRAMVIEW_CONTEXT = 3085] = "INVALID_DIAGRAMVIEW_CONTEXT", _[_.DUPLICATE_DIAGRAMVIEW_NAME = 3086] = "DUPLICATE_DIAGRAMVIEW_NAME", _[_.INVALID_DIAGRAMVIEW_FIELD = 3087] = "INVALID_DIAGRAMVIEW_FIELD", _[_.DUPLICATE_DIAGRAMVIEW_FIELD = 3088] = "DUPLICATE_DIAGRAMVIEW_FIELD", _[_.BINDING_ERROR = 4e3] = "BINDING_ERROR", _[_.NONEXISTENT_MODULE = 4001] = "NONEXISTENT_MODULE", _[_.UNSUPPORTED = 5e3] = "UNSUPPORTED", _[_.CIRCULAR_REF = 5001] = "CIRCULAR_REF", _[_.SAME_ENDPOINT = 5002] = "SAME_ENDPOINT", _[_.UNEQUAL_FIELDS_BINARY_REF = 5003] = "UNEQUAL_FIELDS_BINARY_REF", _[_.CONFLICTING_SETTING = 5004] = "CONFLICTING_SETTING", _[_.TABLE_REAPPEAR_IN_TABLEGROUP = 5005] = "TABLE_REAPPEAR_IN_TABLEGROUP", _;
2486
2486
  }({}), CompileError = class _ extends Error {
2487
2487
  constructor(VY, P, HY) {
@@ -2720,7 +2720,7 @@ function destructureIndexNode(_) {
2720
2720
  };
2721
2721
  }
2722
2722
  function extractQuotedStringToken(_) {
2723
- if (isExpressionAQuotedString(_)) return _?.expression instanceof VariableNode ? _?.expression?.variable?.value : _.expression.literal.value;
2723
+ if (isExpressionAQuotedString(_)) return _.expression instanceof VariableNode ? _?.expression?.variable?.value : _.expression.literal.value;
2724
2724
  }
2725
2725
  function extractNumericLiteral(_) {
2726
2726
  return _ instanceof PrimaryExpressionNode && _.expression instanceof LiteralNode && _.expression.literal?.kind === SyntaxTokenKind.NUMERIC_LITERAL ? Number(_.expression.literal.value) : null;
@@ -8760,24 +8760,16 @@ var ProgramInterpreter = class {
8760
8760
  },
8761
8761
  symbolMembers(_, VY) {
8762
8762
  if (!VY.isKind(SymbolKind.Program)) return Report.create(PASS_THROUGH);
8763
- let P = VY.declaration;
8764
- if (!(P instanceof ProgramNode)) return Report.create([]);
8765
- let HY = new Map([[DEFAULT_SCHEMA_NAME$1, _.symbolFactory.create(SchemaSymbol, { name: DEFAULT_SCHEMA_NAME$1 }, VY.filepath)]]);
8766
- for (let UY of P.declarations) {
8767
- let P = _.nodeFullname(UY).getFiltered(UNHANDLED) || [], WY = P.length <= 1 ? DEFAULT_SCHEMA_NAME$1 : P[0];
8768
- HY.has(WY) || HY.set(WY, _.symbolFactory.create(SchemaSymbol, { name: WY }, VY.filepath));
8769
- }
8770
- for (let P of _.reachableFiles(VY.filepath)) {
8771
- let { schemaMembers: UY } = _.usableMembers(P).getValue();
8772
- for (let P of UY) {
8773
- let { name: UY } = P;
8774
- HY.has(UY) || HY.set(UY, _.symbolFactory.create(SchemaSymbol, { name: UY }, VY.filepath));
8775
- }
8776
- }
8777
- let UY = HY.get(DEFAULT_SCHEMA_NAME$1);
8778
- if (!UY) return Report.create([...HY.values()]);
8779
- let WY = _.symbolMembers(UY).getFiltered(UNHANDLED)?.filter((_) => !_.isKind(SymbolKind.Schema));
8780
- return WY ? Report.create([...HY.values(), ...WY]) : Report.create([...HY.values()]);
8763
+ if (!(VY.declaration instanceof ProgramNode)) return Report.create([]);
8764
+ let P = /* @__PURE__ */ new Map();
8765
+ for (let HY of _.reachableFiles(VY.filepath)) {
8766
+ let { schemaMembers: VY } = _.usableMembers(HY).getValue();
8767
+ for (let _ of VY) P.has(_.name) || P.set(_.name, _);
8768
+ }
8769
+ let HY = P.get(DEFAULT_SCHEMA_NAME$1);
8770
+ if (!HY) return Report.create([...P.values()]);
8771
+ let UY = _.symbolMembers(HY).getFiltered(UNHANDLED)?.filter((_) => !_.isKind(SymbolKind.Schema));
8772
+ return UY ? Report.create([...P.values(), ...UY]) : Report.create([...P.values()]);
8781
8773
  },
8782
8774
  bindNode(_, VY) {
8783
8775
  return isProgramNode(VY) ? new Binder(VY, _).resolve() : Report.create(PASS_THROUGH);
@@ -9403,20 +9395,20 @@ function nodeRefereeOfRefEndpoint(_, VY, P) {
9403
9395
  }
9404
9396
  var useUtils = { visibleName(_, VY) {
9405
9397
  let P = VY instanceof SyntaxNode ? VY : VY.useSpecifierDeclaration instanceof UseSpecifierNode ? VY.useSpecifierDeclaration : VY.declaration;
9406
- if (P) return _.nodeAlias(P).mapFiltered((_) => [_], UNHANDLED, void 0).getFiltered(UNHANDLED) || _.nodeFullname(P).getFiltered(UNHANDLED);
9398
+ if (P instanceof UseSpecifierNode && VY instanceof NodeSymbol && P.getSymbolKind() !== VY.kind && (P = VY.declaration), P) return _.nodeAlias(P).mapFiltered((_) => [_], UNHANDLED, void 0).getFiltered(UNHANDLED) || _.nodeFullname(P).getFiltered(UNHANDLED);
9407
9399
  } }, useModule$1 = {
9408
9400
  nodeSymbol(_, VY) {
9409
9401
  if (!isUseSpecifier(VY) || !VY.name) return Report.create(PASS_THROUGH);
9410
9402
  let P = VY.getSymbolKind();
9411
- if (P === void 0 || P === SymbolKind.Schema) return Report.create(PASS_THROUGH);
9403
+ if (P === void 0) return Report.create(PASS_THROUGH);
9412
9404
  let HY = _.nodeReferee(VY.name).getFiltered(UNHANDLED);
9413
- return HY && !HY.canBeImported ? Report.create(PASS_THROUGH) : Report.create(_.symbolFactory.create(UseSymbol, {
9405
+ return HY && !HY.canBeImported && P !== SymbolKind.Schema ? Report.create(PASS_THROUGH) : Report.create(_.symbolFactory.create(UseSymbol, {
9414
9406
  useSpecifierDeclaration: VY,
9415
9407
  declaration: HY?.declaration,
9416
9408
  usedSymbol: HY,
9417
9409
  kind: P,
9418
9410
  name: useUtils.visibleName(_, VY)?.at(-1)
9419
- }, VY.filepath), HY === void 0 ? [new CompileError(CompileErrorCode.BINDING_ERROR, `Failed to resolve the import of ${P} '${(_.nodeFullname(VY).getFiltered(UNHANDLED) || []).map(addDoubleQuoteIfNeeded).join(".")}'`, VY)] : []);
9411
+ }, VY.filepath), HY === void 0 ? [new CompileError(CompileErrorCode.BINDING_ERROR, `Failed to resolve the import of ${P} '${(destructureComplexVariable(VY.name) || []).map(addDoubleQuoteIfNeeded).join(".")}'`, VY)] : []);
9420
9412
  },
9421
9413
  symbolMembers(_, VY) {
9422
9414
  if (!(VY instanceof UseSymbol)) return Report.create(PASS_THROUGH);
@@ -9497,64 +9489,23 @@ function lookupMemberInFilepath(_, VY, P, HY, UY = /* @__PURE__ */ new Set()) {
9497
9489
  }
9498
9490
  }
9499
9491
  function usableMembers(_) {
9500
- let VY = [], P = /* @__PURE__ */ new Map(), HY = [], UY = [], WY = [], GY = [], KY = _ instanceof Filepath ? _ : _.filepath, qY = this.parseFile(KY), { ast: JY } = qY.getValue();
9501
- for (let _ of JY.body) {
9502
- if (!(_ instanceof UseDeclarationNode) || !_.specifiers || !_.importPath) continue;
9503
- let VY = resolveImportFilepath(KY, _.importPath.value);
9504
- VY && (_.isReuse ? _.specifiers instanceof WildcardNode ? HY.push({
9505
- importPath: VY,
9506
- node: _.specifiers
9507
- }) : UY.push(..._.specifiers.specifiers) : _.specifiers instanceof WildcardNode ? WY.push({
9508
- importPath: VY,
9509
- node: _.specifiers
9510
- }) : GY.push(..._.specifiers.specifiers));
9511
- }
9512
- let YY = [];
9513
9492
  if (_ instanceof Filepath) {
9514
- let P = new Map([[DEFAULT_SCHEMA_NAME$1, this.symbolFactory.create(SchemaSymbol, { name: DEFAULT_SCHEMA_NAME$1 }, _)]]);
9515
- for (let VY of JY.declarations) {
9516
- let HY = this.nodeFullname(VY).getFiltered(UNHANDLED) || [], UY = HY.length <= 1 ? DEFAULT_SCHEMA_NAME$1 : HY[0];
9517
- P.has(UY) || P.set(UY, this.symbolFactory.create(SchemaSymbol, { name: UY }, _));
9518
- }
9519
- YY.push(...P.values());
9520
- let HY = YY.find((_) => _.isPublicSchema());
9521
- if (HY) {
9522
- let _ = this.usableMembers(HY).getFiltered(UNHANDLED);
9523
- _ && VY.push(..._.nonSchemaMembers);
9524
- }
9525
- } else {
9526
- let HY = _;
9527
- for (let _ of JY.body) {
9528
- if (!(_ instanceof ElementDeclarationNode)) continue;
9529
- let UY = schemaMembership(this, HY, _), WY = this.nodeAlias(_).getFiltered(UNHANDLED), GY = this.nodeSymbol(_).getFiltered(UNHANDLED);
9530
- if (WY !== void 0 && HY.isPublicSchema() && GY && VY.push(this.symbolFactory.create(AliasSymbol, {
9531
- kind: GY.kind,
9532
- declaration: _,
9533
- aliasedSymbol: GY,
9534
- name: WY
9535
- }, HY.filepath)), UY.kind !== "none") if (UY.kind === "direct") {
9536
- let P = this.nodeSymbol(_).getFiltered(UNHANDLED);
9537
- if (!P) continue;
9538
- VY.push(P);
9539
- } else P.has(UY.schemaName) || P.set(UY.schemaName, this.symbolFactory.create(SchemaSymbol, {
9540
- name: UY.schemaName,
9541
- parent: HY
9542
- }, HY.filepath));
9543
- }
9544
- YY.push(...P.values());
9493
+ let { ast: VY } = this.parseFile(_).getValue(), P = this.nodeSymbol(VY).getFiltered(UNHANDLED);
9494
+ return this.usableMembers(P);
9545
9495
  }
9496
+ let VY = _.filepath, P = collectImports(this, VY), { nonSchemaMembers: HY, schemaMembers: UY } = _ instanceof ProgramSymbol ? usableMembersForProgram(this, _, P) : usableMembersForSchema(this, _, P);
9546
9497
  return new Report({
9547
- nonSchemaMembers: VY,
9548
- schemaMembers: YY,
9498
+ nonSchemaMembers: HY,
9499
+ schemaMembers: UY,
9549
9500
  reuses: {
9550
- selective: UY,
9551
- wildcard: HY
9501
+ selective: P.selectiveReuses,
9502
+ wildcard: P.wildcardReuses
9552
9503
  },
9553
9504
  uses: {
9554
- selective: GY,
9555
- wildcard: WY
9505
+ selective: P.selectiveUses,
9506
+ wildcard: P.wildcardUses
9556
9507
  }
9557
- }, qY.getErrors(), qY.getWarnings());
9508
+ }, P.parseResult.getErrors(), P.parseResult.getWarnings());
9558
9509
  }
9559
9510
  function schemaMembership(_, VY, P) {
9560
9511
  let HY = VY.qualifiedName, UY;
@@ -9565,6 +9516,124 @@ function schemaMembership(_, VY, P) {
9565
9516
  schemaName: WY[HY.length]
9566
9517
  };
9567
9518
  }
9519
+ function collectImports(_, VY) {
9520
+ let P = _.parseFile(VY), { ast: HY } = P.getValue(), UY = [], WY = [], GY = [], KY = [];
9521
+ for (let _ of HY.body) {
9522
+ if (!(_ instanceof UseDeclarationNode) || !_.specifiers || !_.importPath) continue;
9523
+ let P = resolveImportFilepath(VY, _.importPath.value);
9524
+ P && (_.isReuse ? _.specifiers instanceof WildcardNode ? UY.push({
9525
+ importPath: P,
9526
+ node: _.specifiers
9527
+ }) : WY.push(..._.specifiers.specifiers) : _.specifiers instanceof WildcardNode ? GY.push({
9528
+ importPath: P,
9529
+ node: _.specifiers
9530
+ }) : KY.push(..._.specifiers.specifiers));
9531
+ }
9532
+ return {
9533
+ ast: HY,
9534
+ parseResult: P,
9535
+ selectiveReuses: WY,
9536
+ wildcardReuses: UY,
9537
+ selectiveUses: KY,
9538
+ wildcardUses: GY
9539
+ };
9540
+ }
9541
+ function usableMembersForProgram(_, VY, P) {
9542
+ let { ast: HY, selectiveUses: UY, selectiveReuses: WY } = P, GY = VY.filepath, KY = new Map([[DEFAULT_SCHEMA_NAME$1, _.symbolFactory.create(SchemaSymbol, { name: DEFAULT_SCHEMA_NAME$1 }, GY)]]);
9543
+ for (let VY of HY.declarations) {
9544
+ let P = _.nodeFullname(VY).getFiltered(UNHANDLED) || [], HY = P.length <= 1 ? DEFAULT_SCHEMA_NAME$1 : P[0];
9545
+ KY.has(HY) || KY.set(HY, _.symbolFactory.create(SchemaSymbol, { name: HY }, GY));
9546
+ }
9547
+ registerProgramSchemas(_, [...UY, ...WY], KY, GY);
9548
+ for (let VY of _.reachableFiles(GY)) for (let P of collectTopLevelSchemaNames(_, VY)) KY.has(P) || KY.set(P, _.symbolFactory.create(SchemaSymbol, { name: P }, GY));
9549
+ let qY = [...KY.values()], JY = [], YY = qY.find((_) => _.isPublicSchema());
9550
+ if (YY) {
9551
+ let VY = _.usableMembers(YY).getFiltered(UNHANDLED);
9552
+ VY && JY.push(...VY.nonSchemaMembers);
9553
+ }
9554
+ return {
9555
+ nonSchemaMembers: JY,
9556
+ schemaMembers: qY
9557
+ };
9558
+ }
9559
+ function usableMembersForSchema(_, VY, P) {
9560
+ let { ast: HY, selectiveUses: UY, selectiveReuses: WY } = P, GY = [], KY = /* @__PURE__ */ new Map();
9561
+ for (let P of HY.body) {
9562
+ if (!(P instanceof ElementDeclarationNode)) continue;
9563
+ let HY = schemaMembership(_, VY, P), UY = _.nodeAlias(P).getFiltered(UNHANDLED), WY = _.nodeSymbol(P).getFiltered(UNHANDLED);
9564
+ if (UY !== void 0 && VY.isPublicSchema() && WY && GY.push(_.symbolFactory.create(AliasSymbol, {
9565
+ kind: WY.kind,
9566
+ declaration: P,
9567
+ aliasedSymbol: WY,
9568
+ name: UY
9569
+ }, VY.filepath)), HY.kind !== "none") if (HY.kind === "direct") {
9570
+ let VY = _.nodeSymbol(P).getFiltered(UNHANDLED);
9571
+ if (!VY) continue;
9572
+ GY.push(VY);
9573
+ } else KY.has(HY.schemaName) || KY.set(HY.schemaName, _.symbolFactory.create(SchemaSymbol, {
9574
+ name: HY.schemaName,
9575
+ parent: VY
9576
+ }, VY.filepath));
9577
+ }
9578
+ registerSchemaChildren(_, [...UY, ...WY], VY, KY);
9579
+ let qY = VY.qualifiedName;
9580
+ for (let P of _.reachableFiles(VY.filepath)) for (let HY of collectChildSchemaNames(_, P, qY)) KY.has(HY) || KY.set(HY, _.symbolFactory.create(SchemaSymbol, {
9581
+ name: HY,
9582
+ parent: VY
9583
+ }, VY.filepath));
9584
+ return {
9585
+ nonSchemaMembers: GY,
9586
+ schemaMembers: [...KY.values()]
9587
+ };
9588
+ }
9589
+ function registerProgramSchemas(_, VY, P, HY) {
9590
+ for (let UY of VY) {
9591
+ if (UY.getSymbolKind() !== SymbolKind.Schema) continue;
9592
+ let VY = useUtils.visibleName(_, UY)?.at(0);
9593
+ VY !== void 0 && !P.has(VY) && P.set(VY, _.symbolFactory.create(SchemaSymbol, { name: VY }, HY));
9594
+ }
9595
+ }
9596
+ function registerSchemaChildren(_, VY, P, HY) {
9597
+ let UY = P.qualifiedName;
9598
+ for (let WY of VY) {
9599
+ if (WY.getSymbolKind() !== SymbolKind.Schema) continue;
9600
+ let VY = useUtils.visibleName(_, WY);
9601
+ if (!VY || VY.length <= UY.length || !UY.every((_, P) => _ === VY[P])) continue;
9602
+ let GY = VY[UY.length];
9603
+ GY && !HY.has(GY) && HY.set(GY, _.symbolFactory.create(SchemaSymbol, {
9604
+ name: GY,
9605
+ parent: P
9606
+ }, P.filepath));
9607
+ }
9608
+ }
9609
+ function collectTopLevelSchemaNames(_, VY) {
9610
+ let { ast: P } = _.parseFile(VY).getValue(), HY = [];
9611
+ for (let VY of P.declarations) {
9612
+ let P = _.nodeFullname(VY).getFiltered(UNHANDLED) || [], UY = P.length <= 1 ? DEFAULT_SCHEMA_NAME$1 : P[0];
9613
+ HY.push(UY);
9614
+ }
9615
+ let UY = collectImports(_, VY);
9616
+ for (let VY of [...UY.selectiveUses, ...UY.selectiveReuses]) {
9617
+ if (VY.getSymbolKind() !== SymbolKind.Schema) continue;
9618
+ let P = useUtils.visibleName(_, VY)?.at(0);
9619
+ P !== void 0 && HY.push(P);
9620
+ }
9621
+ return HY;
9622
+ }
9623
+ function collectChildSchemaNames(_, VY, P) {
9624
+ let { ast: HY } = _.parseFile(VY).getValue(), UY = [];
9625
+ for (let VY of HY.declarations) {
9626
+ let HY = _.nodeFullname(VY).getFiltered(UNHANDLED) || [], WY = HY.length <= 1 ? [DEFAULT_SCHEMA_NAME$1] : HY.slice(0, -1);
9627
+ WY.length > P.length && P.every((_, VY) => _ === WY[VY]) && UY.push(WY[P.length]);
9628
+ }
9629
+ let WY = collectImports(_, VY);
9630
+ for (let VY of [...WY.selectiveUses, ...WY.selectiveReuses]) {
9631
+ if (VY.getSymbolKind() !== SymbolKind.Schema) continue;
9632
+ let HY = useUtils.visibleName(_, VY);
9633
+ !HY || HY.length <= P.length || P.every((_, VY) => _ === HY[VY]) && UY.push(HY[P.length]);
9634
+ }
9635
+ return UY;
9636
+ }
9568
9637
  var TableBinder = class {
9569
9638
  constructor(_, VY) {
9570
9639
  this.compiler = _, this.declarationNode = VY;
@@ -10225,16 +10294,11 @@ function nodeRefereeOfTableGroupField(_, VY, P) {
10225
10294
  let HY = extractVarNameFromPrimaryVariable(P) ?? "";
10226
10295
  if (!isAccessExpression(P.parentNode)) {
10227
10296
  let UY = _.symbolMembers(VY).getFiltered(UNHANDLED);
10228
- if (UY) for (let VY of UY) {
10229
- if (!(VY instanceof SchemaSymbol)) continue;
10230
- let P = _.lookupMembers(VY, SymbolKind.Table, HY);
10231
- if (P) return Report.create(P);
10232
- if (!VY.isPublicSchema()) {
10233
- let P = _.symbolMembers(VY).getFiltered(UNHANDLED);
10234
- if (P) {
10235
- let VY = P.find((VY) => !VY.isKind(SymbolKind.Table) || !VY.declaration ? !1 : _.nodeAlias(VY.declaration).getFiltered(UNHANDLED) === HY);
10236
- if (VY) return new Report(VY);
10237
- }
10297
+ if (UY) {
10298
+ let VY = UY.find((_) => _ instanceof SchemaSymbol && _.isPublicSchema());
10299
+ if (VY) {
10300
+ let P = _.lookupMembers(VY, SymbolKind.Table, HY);
10301
+ if (P) return Report.create(P);
10238
10302
  }
10239
10303
  }
10240
10304
  let WY = _.lookupMembers(VY, SymbolKind.Table, HY);
@@ -10554,19 +10618,29 @@ var schemaModule = { symbolMembers(_, VY) {
10554
10618
  let HY = handleMemberSelectiveUses(_, VY, P, WY);
10555
10619
  GY.push(...HY.getErrors());
10556
10620
  let KY = HY.getFiltered(UNHANDLED);
10557
- KY && UY.push(KY);
10621
+ KY && UY.push(KY), UY.push(...mergeImportedSchema(_, VY, P));
10558
10622
  }
10559
10623
  for (let { importPath: P, node: GY } of HY.reuses.wildcard) UY.push(...handleMemberWildcardUses(_, VY, P, GY, WY));
10560
10624
  for (let P of HY.uses.selective) {
10561
10625
  let HY = handleMemberSelectiveUses(_, VY, P, WY);
10562
10626
  GY.push(...HY.getErrors());
10563
10627
  let KY = HY.getFiltered(UNHANDLED);
10564
- KY && UY.push(KY);
10628
+ KY && UY.push(KY), UY.push(...mergeImportedSchema(_, VY, P));
10565
10629
  }
10566
10630
  for (let { importPath: P, node: GY } of HY.uses.wildcard) UY.push(...handleMemberWildcardUses(_, VY, P, GY, WY));
10567
10631
  UY.push(...WY.values());
10568
10632
  let KY = [...UY];
10569
- for (let VY of UY) VY.isKind(SymbolKind.TableGroup) && KY.push(...expandTableGroup(_, VY));
10633
+ {
10634
+ let P = UY.filter((_) => _.isKind(SymbolKind.TableGroup));
10635
+ if (!VY.isPublicSchema()) {
10636
+ let HY = _.usableMembers(VY.filepath).getFiltered(UNHANDLED)?.schemaMembers.find((_) => _.isPublicSchema());
10637
+ if (HY) {
10638
+ let VY = _.symbolMembers(HY).getFiltered(UNHANDLED);
10639
+ VY && P.push(...VY.filter((_) => _.isKind(SymbolKind.TableGroup)));
10640
+ }
10641
+ }
10642
+ for (let HY of P) KY.push(...expandTableGroup(_, VY, HY));
10643
+ }
10570
10644
  let qY = /* @__PURE__ */ new Set(), JY = KY.filter((_) => {
10571
10645
  let VY = `${_.originalSymbol.intern()}:${_.name ?? ""}`;
10572
10646
  return qY.has(VY) ? !1 : (qY.add(VY), !0);
@@ -10602,6 +10676,26 @@ function handleMemberSelectiveUses(_, VY, P, HY) {
10602
10676
  parent: VY
10603
10677
  }, VY.filepath)), Report.create(void 0);
10604
10678
  }
10679
+ function mergeImportedSchema(_, VY, P, HY = /* @__PURE__ */ new Set()) {
10680
+ if (P.getSymbolKind() !== SymbolKind.Schema || !P.name) return [];
10681
+ let UY = _.nodeReferee(P.name).getFiltered(UNHANDLED);
10682
+ if (!UY || !UY.isKind(SymbolKind.Schema) || UY.name !== VY.name) return [];
10683
+ let WY = UY.intern();
10684
+ if (HY.has(WY)) return [];
10685
+ HY.add(WY);
10686
+ let GY = _.usableMembers(UY).getFiltered(UNHANDLED);
10687
+ if (!GY) return [];
10688
+ let KY = GY.nonSchemaMembers.filter((_) => _.canBeImported).map((HY) => _.symbolFactory.create(UseSymbol, {
10689
+ kind: HY.kind,
10690
+ declaration: HY.originalSymbol.declaration,
10691
+ usedSymbol: HY.originalSymbol,
10692
+ useSpecifierDeclaration: P,
10693
+ name: HY.name
10694
+ }, VY.filepath));
10695
+ for (let P of [...KY]) P.isKind(SymbolKind.TableGroup) && KY.push(...expandTableGroup(_, VY, P));
10696
+ for (let P of GY.reuses.selective) KY.push(...mergeImportedSchema(_, VY, P, HY));
10697
+ return KY;
10698
+ }
10605
10699
  function handleMemberWildcardUses(_, VY, P, HY, UY, WY = /* @__PURE__ */ new Set()) {
10606
10700
  if (WY.has(P)) return [];
10607
10701
  WY.add(P);
@@ -10620,7 +10714,7 @@ function handleMemberWildcardUses(_, VY, P, HY, UY, WY = /* @__PURE__ */ new Set
10620
10714
  let { reuses: { selective: JY, wildcard: YY } } = KY;
10621
10715
  for (let P of JY) {
10622
10716
  let HY = handleMemberSelectiveUses(_, VY, P, UY).getFiltered(UNHANDLED);
10623
- HY && qY.push(HY);
10717
+ HY && qY.push(HY), qY.push(...mergeImportedSchema(_, VY, P));
10624
10718
  }
10625
10719
  for (let { importPath: P } of YY) qY.push(...handleMemberWildcardUses(_, VY, P, HY, UY, WY));
10626
10720
  return qY;
@@ -10638,34 +10732,36 @@ function findSchemaSymbolInFilepath(_, VY, P) {
10638
10732
  }
10639
10733
  return WY;
10640
10734
  }
10641
- function expandTableGroup(_, VY) {
10642
- if (!VY.isKind(SymbolKind.TableGroup)) return [];
10643
- let P = VY.originalSymbol, HY = _.symbolMembers(P).getFiltered(UNHANDLED);
10644
- if (!HY) return [];
10645
- let UY = [];
10646
- for (let P of HY) {
10647
- if (!P.isKind(SymbolKind.TableGroupField) || !P.declaration) continue;
10648
- let HY = P.declaration.callee;
10735
+ function expandTableGroup(_, VY, P) {
10736
+ if (!P.isKind(SymbolKind.TableGroup) || !(P instanceof UseSymbol)) return [];
10737
+ let HY = P.originalSymbol, UY = _.symbolMembers(HY).getFiltered(UNHANDLED);
10738
+ if (!UY) return [];
10739
+ let WY = VY.qualifiedName, GY = [];
10740
+ for (let VY of UY) {
10741
+ if (!VY.isKind(SymbolKind.TableGroupField) || !VY.declaration) continue;
10742
+ let HY = VY.declaration.callee;
10649
10743
  if (!HY) continue;
10650
- let WY = destructureComplexVariable(HY);
10651
- if (!WY || WY.length === 0) continue;
10652
- let GY = P.declaration.filepath, KY = lookupTableInFile(_, GY, WY);
10653
- KY && VY instanceof UseSymbol && UY.push(_.symbolFactory.create(UseSymbol, {
10744
+ let UY = destructureComplexVariable(HY);
10745
+ if (!UY || UY.length === 0) continue;
10746
+ let KY = UY.length <= 1 ? [DEFAULT_SCHEMA_NAME$1] : UY.slice(0, -1);
10747
+ if (KY.length !== WY.length || !WY.every((_, VY) => _ === KY[VY])) continue;
10748
+ let qY = VY.declaration.filepath, JY = lookupTableInFile(_, qY, UY);
10749
+ JY && GY.push(_.symbolFactory.create(UseSymbol, {
10654
10750
  kind: SymbolKind.Table,
10655
- declaration: KY.declaration,
10656
- usedSymbol: KY,
10751
+ declaration: JY.declaration,
10752
+ usedSymbol: JY,
10657
10753
  useSpecifierDeclaration: void 0,
10658
- name: KY.name
10659
- }, VY.filepath));
10754
+ name: JY.name
10755
+ }, P.filepath));
10660
10756
  }
10661
- return UY;
10757
+ return GY;
10662
10758
  }
10663
10759
  function lookupTableInFile(_, VY, P) {
10664
10760
  if (P.length === 1) {
10665
10761
  let HY = _.usableMembers(VY).getFiltered(UNHANDLED);
10666
10762
  return HY ? HY.nonSchemaMembers.find((_) => _.isKind(SymbolKind.Table) && _.name === P[0]) : void 0;
10667
10763
  }
10668
- let [HY, UY] = [P[0], P[P.length - 1]], WY = _.usableMembers(VY).getFiltered(UNHANDLED);
10764
+ let HY = P[0], UY = P[P.length - 1], WY = _.usableMembers(VY).getFiltered(UNHANDLED);
10669
10765
  if (!WY) return;
10670
10766
  let GY = WY.schemaMembers.find((_) => _.name === HY);
10671
10767
  if (!GY) return;
@@ -12196,7 +12292,7 @@ var tablePartialModule = {
12196
12292
  nodeFullname(_, VY) {
12197
12293
  if (!isUseSpecifier(VY)) return Report.create(PASS_THROUGH);
12198
12294
  let P = destructureComplexVariable(VY.name);
12199
- 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)", VY)]) : VY.isKind(ImportKind.TableGroup) && P.length > 1 ? Report.create(P, [new CompileError(CompileErrorCode.INVALID_USE_SPECIFIER_NAME, "A TableGroup name must be a simple name", VY)]) : VY.isKind(ImportKind.TablePartial) && P.length > 1 ? Report.create(P, [new CompileError(CompileErrorCode.INVALID_USE_SPECIFIER_NAME, "A TablePartial name must be a simple name", VY)]) : VY.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", VY)]) : (P.length === 1 && P.unshift(DEFAULT_SCHEMA_NAME$1), new Report(P));
12295
+ 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)", VY)]) : VY.isKind(ImportKind.TableGroup) && P.length > 1 ? Report.create(P, [new CompileError(CompileErrorCode.INVALID_USE_SPECIFIER_NAME, "A TableGroup name must be a simple name", VY)]) : VY.isKind(ImportKind.TablePartial) && P.length > 1 ? Report.create(P, [new CompileError(CompileErrorCode.INVALID_USE_SPECIFIER_NAME, "A TablePartial name must be a simple name", VY)]) : VY.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", VY)]) : (P.length === 1 && !VY.isKind(ImportKind.Schema) && P.unshift(DEFAULT_SCHEMA_NAME$1), new Report(P));
12200
12296
  },
12201
12297
  nodeAlias(_, VY) {
12202
12298
  return isUseSpecifier(VY) ? VY.alias ? isValidAlias(VY.alias) ? new Report(extractVariableFromExpression(VY.alias)) : new Report(void 0, [new CompileError(CompileErrorCode.INVALID_ALIAS, "Use aliases can only contain alphanumeric and underscore unless surrounded by double quotes", VY.alias)]) : new Report(void 0) : Report.create(PASS_THROUGH);
@@ -1825646,5 +1825742,5 @@ function findDiagramViewBlocks(_) {
1825646
1825742
  let VY = new Compiler();
1825647
1825743
  return VY.setSource(DEFAULT_ENTRY, _), VY.findDiagramViewBlocks(DEFAULT_ENTRY);
1825648
1825744
  }
1825649
- const VERSION = "8.0.0-alpha.0";
1825745
+ const VERSION = "8.0.0-alpha.5";
1825650
1825746
  export { CompilerError, ModelExporter_default as ModelExporter, Parser_default as Parser, VERSION, addDoubleQuoteIfNeeded, dbmlMonarchTokensProvider, export_default as exporter, findDiagramViewBlocks, formatRecordValue, import_default as importer, isBinaryType, isBooleanType, isDateTimeType, isFloatType, isIntegerType, isNumericType, isSerialType, isStringType, renameTable, syncDiagramView, tryExtractBoolean, tryExtractDateTime, tryExtractEnum, tryExtractInteger, tryExtractNumeric, tryExtractString };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@dbml/core",
4
- "version": "8.0.0-alpha.0",
4
+ "version": "8.0.0-alpha.5",
5
5
  "description": "> TODO: description",
6
6
  "author": "Holistics <dev@holistics.io>",
7
7
  "license": "Apache-2.0",
@@ -46,7 +46,7 @@
46
46
  "lint:fix": "eslint --fix ."
47
47
  },
48
48
  "dependencies": {
49
- "@dbml/parse": "^8.0.0-alpha.0",
49
+ "@dbml/parse": "^8.0.0-alpha.5",
50
50
  "antlr4": "^4.13.1",
51
51
  "lodash": "^4.18.1",
52
52
  "lodash-es": "^4.18.1",
@@ -57,7 +57,7 @@
57
57
  "devDependencies": {
58
58
  "bluebird": "^3.5.5"
59
59
  },
60
- "gitHead": "4b99e7eab601b7d53e268d5d80048ee035614a79",
60
+ "gitHead": "2e6795b120c2ca4f16bb542295b2152f091d6ed9",
61
61
  "engines": {
62
62
  "node": ">=16"
63
63
  }