@effect/language-service 0.61.0 → 0.62.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cli.js CHANGED
@@ -21014,14 +21014,14 @@ var foldChunksReader = (s, contFn, f) => {
21014
21014
  };
21015
21015
  var foldLeftChunks = (s, f) => foldChunks(s, constTrue, f);
21016
21016
  var forEach9 = (f) => {
21017
- const process2 = readWithCause({
21017
+ const process3 = readWithCause({
21018
21018
  onInput: (input) => pipe(fromEffect4(forEach8(input, (v) => f(v), {
21019
21019
  discard: true
21020
- })), flatMap12(() => process2)),
21020
+ })), flatMap12(() => process3)),
21021
21021
  onFailure: failCause8,
21022
21022
  onDone: () => void_5
21023
21023
  });
21024
- return new SinkImpl(process2);
21024
+ return new SinkImpl(process3);
21025
21025
  };
21026
21026
  var fromChannel = (channel) => new SinkImpl(channel);
21027
21027
  var fromEffect6 = (effect3) => new SinkImpl(fromEffect4(effect3));
@@ -21279,12 +21279,12 @@ var bufferChunks = /* @__PURE__ */ dual(2, (self, options3) => {
21279
21279
  }
21280
21280
  const queue = toQueue(self, options3);
21281
21281
  return new StreamImpl(unwrapScoped2(map17(queue, (queue2) => {
21282
- const process2 = pipe(fromEffect4(take3(queue2)), flatMap12(match16({
21282
+ const process3 = pipe(fromEffect4(take3(queue2)), flatMap12(match16({
21283
21283
  onEnd: () => void_5,
21284
21284
  onFailure: failCause8,
21285
- onSuccess: (value5) => pipe(write(value5), flatMap12(() => process2))
21285
+ onSuccess: (value5) => pipe(write(value5), flatMap12(() => process3))
21286
21286
  })));
21287
- return process2;
21287
+ return process3;
21288
21288
  })));
21289
21289
  });
21290
21290
  var bufferChunksDropping = /* @__PURE__ */ dual(2, (self, capacity3) => {
@@ -21305,12 +21305,12 @@ var bufferSignal = (scoped6, bufferChannel) => {
21305
21305
  });
21306
21306
  };
21307
21307
  const consumer = (queue) => {
21308
- const process2 = pipe(fromEffect4(take3(queue)), flatMap12(([take6, deferred]) => zipRight4(fromEffect4(succeed6(deferred, void 0)), match16(take6, {
21308
+ const process3 = pipe(fromEffect4(take3(queue)), flatMap12(([take6, deferred]) => zipRight4(fromEffect4(succeed6(deferred, void 0)), match16(take6, {
21309
21309
  onEnd: () => void_5,
21310
21310
  onFailure: failCause8,
21311
- onSuccess: (value5) => pipe(write(value5), flatMap12(() => process2))
21311
+ onSuccess: (value5) => pipe(write(value5), flatMap12(() => process3))
21312
21312
  }))));
21313
- return process2;
21313
+ return process3;
21314
21314
  };
21315
21315
  return unwrapScoped2(pipe(scoped6, flatMap9((queue) => pipe(make38(), tap2((start4) => succeed6(start4, void 0)), flatMap9((start4) => pipe(make25(start4), flatMap9((ref) => pipe(bufferChannel, pipeTo(producer(queue, ref)), runScoped, forkScoped2)), as4(consumer(queue))))))));
21316
21316
  };
@@ -23772,9 +23772,9 @@ var resolve = function resolve2() {
23772
23772
  if (i >= 0) {
23773
23773
  path2 = arguments[i];
23774
23774
  } else {
23775
- const process2 = globalThis.process;
23776
- if (cwd === void 0 && "process" in globalThis && typeof process2 === "object" && process2 !== null && typeof process2.cwd === "function") {
23777
- cwd = process2.cwd();
23775
+ const process3 = globalThis.process;
23776
+ if (cwd === void 0 && "process" in globalThis && typeof process3 === "object" && process3 !== null && typeof process3.cwd === "function") {
23777
+ cwd = process3.cwd();
23778
23778
  }
23779
23779
  path2 = cwd;
23780
23780
  }
@@ -28305,7 +28305,7 @@ var ExitCode = /* @__PURE__ */ nominal();
28305
28305
  var ProcessId = /* @__PURE__ */ nominal();
28306
28306
  var CommandExecutor = /* @__PURE__ */ GenericTag("@effect/platform/CommandExecutor");
28307
28307
  var makeExecutor = (start4) => {
28308
- const stream3 = (command) => unwrapScoped6(map17(start4(command), (process2) => process2.stdout));
28308
+ const stream3 = (command) => unwrapScoped6(map17(start4(command), (process3) => process3.stdout));
28309
28309
  const streamLines2 = (command, encoding) => {
28310
28310
  const decoder2 = new TextDecoder(encoding);
28311
28311
  return splitLines3(mapChunks2(stream3(command), map5((bytes) => decoder2.decode(bytes))));
@@ -28313,11 +28313,11 @@ var makeExecutor = (start4) => {
28313
28313
  return {
28314
28314
  [TypeId24]: TypeId24,
28315
28315
  start: start4,
28316
- exitCode: (command) => scoped2(flatMap9(start4(command), (process2) => process2.exitCode)),
28316
+ exitCode: (command) => scoped2(flatMap9(start4(command), (process3) => process3.exitCode)),
28317
28317
  stream: stream3,
28318
28318
  string: (command, encoding = "utf-8") => {
28319
28319
  const decoder2 = new TextDecoder(encoding);
28320
- return pipe(start4(command), flatMap9((process2) => run4(process2.stdout, collectUint8Array)), map17((bytes) => decoder2.decode(bytes)), scoped2);
28320
+ return pipe(start4(command), flatMap9((process3) => run4(process3.stdout, collectUint8Array)), map17((bytes) => decoder2.decode(bytes)), scoped2);
28321
28321
  },
28322
28322
  lines: (command, encoding = "utf-8") => {
28323
28323
  return pipe(streamLines2(command, encoding), runCollect3, map17(toArray2));
@@ -28662,7 +28662,7 @@ var runCommand = (fileSystem) => (command) => {
28662
28662
  stdout: stdout2
28663
28663
  });
28664
28664
  }),
28665
- typeof command.stdin === "string" ? identity : tap2((process2) => forkDaemon2(run4(command.stdin, process2.stdin)))
28665
+ typeof command.stdin === "string" ? identity : tap2((process3) => forkDaemon2(run4(command.stdin, process3.stdin)))
28666
28666
  );
28667
28667
  }
28668
28668
  case "PipedCommand": {
@@ -28674,7 +28674,7 @@ var runCommand = (fileSystem) => (command) => {
28674
28674
  const tail = flattened2.slice(1);
28675
28675
  const initial = tail.slice(0, tail.length - 1);
28676
28676
  const last5 = tail[tail.length - 1];
28677
- const stream3 = initial.reduce((stdin3, command2) => pipe(stdin2(command2, stdin3), runCommand(fileSystem), map17((process2) => process2.stdout), unwrapScoped6), pipe(runCommand(fileSystem)(head5), map17((process2) => process2.stdout), unwrapScoped6));
28677
+ const stream3 = initial.reduce((stdin3, command2) => pipe(stdin2(command2, stdin3), runCommand(fileSystem), map17((process3) => process3.stdout), unwrapScoped6), pipe(runCommand(fileSystem)(head5), map17((process3) => process3.stdout), unwrapScoped6));
28678
28678
  return pipe(stdin2(last5, stream3), runCommand(fileSystem));
28679
28679
  }
28680
28680
  }
@@ -31668,6 +31668,8 @@ var nanoLayer2 = (fa) => pipe(
31668
31668
  )
31669
31669
  );
31670
31670
  function makeTypeCheckerUtils(ts, typeChecker, tsUtils) {
31671
+ const readonlyArraySymbol = typeChecker.resolveName("ReadonlyArray", void 0, ts.SymbolFlags.Type, false);
31672
+ const globalReadonlyArrayType = readonlyArraySymbol ? typeChecker.getDeclaredTypeOfSymbol(readonlyArraySymbol) : void 0;
31671
31673
  function isUnion4(type2) {
31672
31674
  return !!(type2.flags & ts.TypeFlags.Union);
31673
31675
  }
@@ -31677,6 +31679,12 @@ function makeTypeCheckerUtils(ts, typeChecker, tsUtils) {
31677
31679
  function isThisTypeParameter(type2) {
31678
31680
  return !!(type2.flags & ts.TypeFlags.TypeParameter && type2.isThisType);
31679
31681
  }
31682
+ function isReadonlyArrayType(type2) {
31683
+ return type2 && "target" in type2 && type2.target === globalReadonlyArrayType;
31684
+ }
31685
+ function isMissingIntrinsicType(type2) {
31686
+ return (type2.flags & ts.TypeFlags.Undefined) !== 0 && "debugIntrinsicName" in type2 && type2.debugIntrinsicName === "missing";
31687
+ }
31680
31688
  function getTypeParameterAtPosition(signature, pos) {
31681
31689
  const type2 = typeChecker.getParameterType(signature, pos);
31682
31690
  if (isIndexType(type2) && isThisTypeParameter(type2.type)) {
@@ -31981,6 +31989,8 @@ function makeTypeCheckerUtils(ts, typeChecker, tsUtils) {
31981
31989
  }
31982
31990
  return {
31983
31991
  isUnion: isUnion4,
31992
+ isReadonlyArrayType,
31993
+ isMissingIntrinsicType,
31984
31994
  getTypeParameterAtPosition,
31985
31995
  getMissingTypeEntriesInTargetType,
31986
31996
  unrollUnionMembers,
@@ -33537,8 +33547,664 @@ var annotate3 = createCodegen({
33537
33547
  })
33538
33548
  });
33539
33549
 
33550
+ // src/utils/StructuralSchemaGen.ts
33551
+ var UnsupportedTypeError = class {
33552
+ constructor(type2, reason) {
33553
+ this.type = type2;
33554
+ this.reason = reason;
33555
+ }
33556
+ _tag = "@effect/language-service/UnsupportedTypeError";
33557
+ toString() {
33558
+ return `Unsupported type: ${this.reason}`;
33559
+ }
33560
+ };
33561
+ var StructuralSchemaGenContext = Tag4("StructuralSchemaGenContext");
33562
+ var makeStructuralSchemaGenContext = fn2("StructuralSchemaGen.makeContext")(
33563
+ function* (sourceFile, schemaIdentifier) {
33564
+ const ts = yield* service2(TypeScriptApi);
33565
+ const program = yield* service2(TypeScriptProgram);
33566
+ const typeChecker = yield* service2(TypeCheckerApi);
33567
+ const typeCheckerUtils = yield* service2(TypeCheckerUtils);
33568
+ const effectSchemaIdentifier = schemaIdentifier || "Schema";
33569
+ return identity({
33570
+ ts,
33571
+ program,
33572
+ typeChecker,
33573
+ typeCheckerUtils,
33574
+ sourceFile,
33575
+ createApiPropertyAccess: (apiName) => ts.factory.createPropertyAccessExpression(
33576
+ ts.factory.createIdentifier(effectSchemaIdentifier),
33577
+ apiName
33578
+ ),
33579
+ createApiCall: (apiName, args3) => ts.factory.createCallExpression(
33580
+ ts.factory.createPropertyAccessExpression(
33581
+ ts.factory.createIdentifier(effectSchemaIdentifier),
33582
+ apiName
33583
+ ),
33584
+ [],
33585
+ args3
33586
+ ),
33587
+ hoistedSchemas: /* @__PURE__ */ new Map(),
33588
+ typeToStatementIndex: /* @__PURE__ */ new Map(),
33589
+ nameToType: /* @__PURE__ */ new Map(),
33590
+ usedGlobalIdentifiers: /* @__PURE__ */ new Map(),
33591
+ schemaStatements: [],
33592
+ rangesToDelete: []
33593
+ });
33594
+ }
33595
+ );
33596
+ var pushHoistedStatement = fn2("StructuralSchemaGen.pushHoistedStatement")(
33597
+ function* (ctx, name, type2, statement, createReference) {
33598
+ ctx.usedGlobalIdentifiers.set(name, (ctx.usedGlobalIdentifiers.get(name) || 0) + 1);
33599
+ ctx.schemaStatements.push(statement);
33600
+ ctx.typeToStatementIndex.set(type2, ctx.schemaStatements.length - 1);
33601
+ ctx.hoistedSchemas.set(type2, createReference);
33602
+ }
33603
+ );
33604
+ var pushHoistedVariableStatement = fn2("StructuralSchemaGen.pushHoistedVariableStatement")(
33605
+ function* (ts, ctx, name, type2, result) {
33606
+ return yield* pushHoistedStatement(
33607
+ ctx,
33608
+ name,
33609
+ type2,
33610
+ ts.factory.createVariableStatement(
33611
+ void 0,
33612
+ ts.factory.createVariableDeclarationList(
33613
+ [ts.factory.createVariableDeclaration(ts.factory.createIdentifier(name), void 0, void 0, result)],
33614
+ ts.NodeFlags.Const
33615
+ )
33616
+ ),
33617
+ () => ts.factory.createIdentifier(name)
33618
+ );
33619
+ }
33620
+ );
33621
+ var createProcessingContext = (maxDepth = 200) => ({
33622
+ depth: 0,
33623
+ maxDepth,
33624
+ hoistName: void 0
33625
+ });
33626
+ var processType = fn2(
33627
+ "StructuralSchemaGen.processType"
33628
+ )(
33629
+ function* (type2, context7) {
33630
+ const processingContext = context7 || createProcessingContext();
33631
+ const { hoistedSchemas, nameToType, ts, typeChecker, usedGlobalIdentifiers } = yield* service2(
33632
+ StructuralSchemaGenContext
33633
+ );
33634
+ if (processingContext.depth >= processingContext.maxDepth) {
33635
+ return yield* fail18(new UnsupportedTypeError(type2, "Maximum depth exceeded"));
33636
+ }
33637
+ let hoistName = fromIterable(nameToType.entries()).find(([_, existingType]) => existingType === type2)?.[0];
33638
+ if (!hoistName && type2 && type2.symbol && type2.symbol.declarations && type2.symbol.declarations.length === 1) {
33639
+ const declaration = type2.symbol.declarations[0];
33640
+ if (ts.isInterfaceDeclaration(declaration)) {
33641
+ hoistName = ts.idText(declaration.name);
33642
+ } else if (declaration.parent && ts.isTypeAliasDeclaration(declaration.parent)) {
33643
+ hoistName = ts.idText(declaration.parent.name);
33644
+ }
33645
+ if (hoistName) {
33646
+ const existingType = nameToType.get(hoistName);
33647
+ const isSame = existingType && typeChecker.isTypeAssignableTo(type2, existingType) && typeChecker.isTypeAssignableTo(existingType, type2);
33648
+ if (!isSame) {
33649
+ const usedCount = usedGlobalIdentifiers.get(hoistName) || 0;
33650
+ hoistName = usedCount > 0 ? hoistName + "_" + usedCount : hoistName;
33651
+ }
33652
+ }
33653
+ }
33654
+ const nestedContext = {
33655
+ ...processingContext,
33656
+ depth: processingContext.depth + 1,
33657
+ hoistName
33658
+ };
33659
+ for (const [hoistedType, hoistedSchema] of hoistedSchemas.entries()) {
33660
+ if (hoistedType === type2 || typeChecker.isTypeAssignableTo(type2, hoistedType) && typeChecker.isTypeAssignableTo(hoistedType, type2)) {
33661
+ return hoistedSchema();
33662
+ }
33663
+ }
33664
+ const [schemaExpr, skipHoisting] = yield* processTypeImpl(type2, nestedContext);
33665
+ if (!skipHoisting && hoistName) {
33666
+ const ctx = yield* service2(StructuralSchemaGenContext);
33667
+ yield* pushHoistedVariableStatement(ts, ctx, hoistName, type2, schemaExpr);
33668
+ return ctx.hoistedSchemas.get(type2)();
33669
+ }
33670
+ return schemaExpr;
33671
+ }
33672
+ );
33673
+ var processTypeImpl = fn2(
33674
+ "StructuralSchemaGen.processTypeImpl"
33675
+ )(
33676
+ function* (type2, context7) {
33677
+ const { createApiCall, createApiPropertyAccess, ts, typeChecker, typeCheckerUtils } = yield* service2(
33678
+ StructuralSchemaGenContext
33679
+ );
33680
+ if (type2.flags & ts.TypeFlags.String) {
33681
+ return [createApiPropertyAccess("String"), true];
33682
+ }
33683
+ if (type2.flags & ts.TypeFlags.Number) {
33684
+ return [createApiPropertyAccess("Number"), true];
33685
+ }
33686
+ if (type2.flags & ts.TypeFlags.Boolean) {
33687
+ return [createApiPropertyAccess("Boolean"), true];
33688
+ }
33689
+ if (type2.flags & ts.TypeFlags.BigInt) {
33690
+ return [createApiPropertyAccess("BigInt"), true];
33691
+ }
33692
+ if (type2.flags & ts.TypeFlags.Void) {
33693
+ return [createApiPropertyAccess("Void"), true];
33694
+ }
33695
+ if (type2.flags & ts.TypeFlags.Undefined) {
33696
+ return [createApiPropertyAccess("Undefined"), true];
33697
+ }
33698
+ if (type2.flags & ts.TypeFlags.Null) {
33699
+ return [createApiPropertyAccess("Null"), true];
33700
+ }
33701
+ if (type2.flags & ts.TypeFlags.Never) {
33702
+ return [createApiPropertyAccess("Never"), true];
33703
+ }
33704
+ if (type2.flags & ts.TypeFlags.Any) {
33705
+ return [createApiPropertyAccess("Any"), true];
33706
+ }
33707
+ if (type2.flags & ts.TypeFlags.Unknown) {
33708
+ return [createApiPropertyAccess("Unknown"), true];
33709
+ }
33710
+ if (type2.flags & ts.TypeFlags.StringLiteral) {
33711
+ const literalType = type2;
33712
+ return [createApiCall("Literal", [ts.factory.createStringLiteral(literalType.value)]), true];
33713
+ }
33714
+ if (type2.flags & ts.TypeFlags.NumberLiteral) {
33715
+ const literalType = type2;
33716
+ return [createApiCall("Literal", [ts.factory.createNumericLiteral(literalType.value)]), true];
33717
+ }
33718
+ if (type2.flags & ts.TypeFlags.BooleanLiteral) {
33719
+ const value5 = type2.intrinsicName === "true";
33720
+ return [createApiCall("Literal", [value5 ? ts.factory.createTrue() : ts.factory.createFalse()]), true];
33721
+ }
33722
+ if (typeCheckerUtils.isUnion(type2)) {
33723
+ return yield* processUnionType(type2.types, context7);
33724
+ }
33725
+ if (type2.flags & ts.TypeFlags.Intersection) {
33726
+ return yield* processIntersectionType(type2, context7);
33727
+ }
33728
+ if (typeChecker.isArrayType(type2)) {
33729
+ return yield* processArrayType(type2, context7);
33730
+ }
33731
+ if (typeChecker.isTupleType(type2)) {
33732
+ return yield* processTupleType(type2, context7);
33733
+ }
33734
+ if (type2.flags & ts.TypeFlags.Object) {
33735
+ const symbol3 = type2.symbol || type2.aliasSymbol;
33736
+ if (symbol3) {
33737
+ const typeName = typeChecker.symbolToString(symbol3);
33738
+ if (typeName === "Date") {
33739
+ return [createApiPropertyAccess("Date"), false];
33740
+ }
33741
+ if (typeName === "ReadonlyArray" || typeName === "Array") {
33742
+ return yield* processArrayType(type2, context7);
33743
+ }
33744
+ }
33745
+ const objectType = type2;
33746
+ return yield* processObjectType(objectType, context7);
33747
+ }
33748
+ return yield* fail18(
33749
+ new UnsupportedTypeError(
33750
+ type2,
33751
+ `Type with flags ${type2.flags} is not supported`
33752
+ )
33753
+ );
33754
+ }
33755
+ );
33756
+ var processUnionType = fn2(
33757
+ "StructuralSchemaGen.processUnionType"
33758
+ )(
33759
+ function* (types, context7) {
33760
+ const { createApiCall, ts } = yield* service2(StructuralSchemaGenContext);
33761
+ const allLiterals = types.every(
33762
+ (t) => t.flags & ts.TypeFlags.StringLiteral || t.flags & ts.TypeFlags.NumberLiteral || t.flags & ts.TypeFlags.BooleanLiteral
33763
+ );
33764
+ if (allLiterals) {
33765
+ const literals = yield* all9(
33766
+ ...types.map((t) => processType(t, context7))
33767
+ );
33768
+ const literalValues = literals.map((expr) => {
33769
+ if (ts.isCallExpression(expr) && expr.arguments.length > 0) {
33770
+ return expr.arguments[0];
33771
+ }
33772
+ return expr;
33773
+ }).filter((arg) => arg !== void 0);
33774
+ return [createApiCall("Literal", literalValues), false];
33775
+ }
33776
+ const members = yield* all9(
33777
+ ...types.map((t) => processType(t, context7))
33778
+ );
33779
+ if (members.length === 1) {
33780
+ return [members[0], false];
33781
+ }
33782
+ return [createApiCall("Union", members), false];
33783
+ }
33784
+ );
33785
+ var processIntersectionType = fn2(
33786
+ "StructuralSchemaGen.processIntersectionType"
33787
+ )(
33788
+ function* (type2, context7) {
33789
+ const { createApiCall, ts } = yield* service2(StructuralSchemaGenContext);
33790
+ const [firstSchema, ...otherSchemas] = yield* all9(
33791
+ ...type2.types.map((t) => processType(t, context7))
33792
+ );
33793
+ if (otherSchemas.length === 0) {
33794
+ return [firstSchema, false];
33795
+ }
33796
+ return [
33797
+ ts.factory.createCallExpression(
33798
+ ts.factory.createPropertyAccessExpression(
33799
+ firstSchema,
33800
+ "pipe"
33801
+ ),
33802
+ [],
33803
+ otherSchemas.map((schema) => createApiCall("extend", [schema]))
33804
+ ),
33805
+ false
33806
+ ];
33807
+ }
33808
+ );
33809
+ var processArrayType = fn2(
33810
+ "StructuralSchemaGen.processArrayType"
33811
+ )(
33812
+ function* (type2, context7) {
33813
+ const { createApiCall, typeChecker, typeCheckerUtils } = yield* service2(StructuralSchemaGenContext);
33814
+ const typeArgs = typeChecker.getTypeArguments(type2);
33815
+ if (typeArgs.length === 0) {
33816
+ return yield* fail18(new UnsupportedTypeError(type2, "Array type has no type arguments"));
33817
+ }
33818
+ const elementSchema = yield* processType(typeArgs[0], context7);
33819
+ const expr = createApiCall("Array", [elementSchema]);
33820
+ if (typeCheckerUtils.isReadonlyArrayType(type2)) return [expr, false];
33821
+ return [createApiCall("mutable", [expr]), false];
33822
+ }
33823
+ );
33824
+ var processTupleType = fn2(
33825
+ "StructuralSchemaGen.processTupleType"
33826
+ )(
33827
+ function* (type2, context7) {
33828
+ const { createApiCall, typeChecker } = yield* service2(StructuralSchemaGenContext);
33829
+ const typeArgs = typeChecker.getTypeArguments(type2);
33830
+ const elementSchemas = yield* all9(
33831
+ ...typeArgs.map((t) => processType(t, context7))
33832
+ );
33833
+ return [createApiCall("Tuple", elementSchemas), false];
33834
+ }
33835
+ );
33836
+ var processObjectType = fn2(
33837
+ "StructuralSchemaGen.processObjectType"
33838
+ )(
33839
+ function* (type2, context7) {
33840
+ const {
33841
+ createApiCall,
33842
+ createApiPropertyAccess,
33843
+ program,
33844
+ ts,
33845
+ typeChecker,
33846
+ typeCheckerUtils
33847
+ } = yield* service2(
33848
+ StructuralSchemaGenContext
33849
+ );
33850
+ let hasRecords = false;
33851
+ const properties = typeChecker.getPropertiesOfType(type2);
33852
+ const propertyAssignments = [];
33853
+ for (const property of properties) {
33854
+ const propertyName = typeChecker.symbolToString(property);
33855
+ const propertyType = typeChecker.getTypeOfSymbol(property);
33856
+ const isOptional = (property.flags & ts.SymbolFlags.Optional) !== 0;
33857
+ let schemaExpr;
33858
+ if (isOptional) {
33859
+ if (program.getCompilerOptions().exactOptionalPropertyTypes) {
33860
+ if (typeCheckerUtils.isUnion(propertyType)) {
33861
+ const typeWithoutMissing = propertyType.types.filter((t) => !typeCheckerUtils.isMissingIntrinsicType(t));
33862
+ const [result, _] = yield* processUnionType(typeWithoutMissing, context7);
33863
+ schemaExpr = createApiCall("optionalWith", [
33864
+ result,
33865
+ ts.factory.createObjectLiteralExpression([
33866
+ ts.factory.createPropertyAssignment("exact", ts.factory.createTrue())
33867
+ ])
33868
+ ]);
33869
+ }
33870
+ } else {
33871
+ schemaExpr = yield* processType(propertyType, context7);
33872
+ schemaExpr = createApiCall("optional", [schemaExpr]);
33873
+ }
33874
+ }
33875
+ if (!schemaExpr) {
33876
+ schemaExpr = yield* processType(propertyType, context7);
33877
+ }
33878
+ const propertyNameNode = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(propertyName) ? ts.factory.createIdentifier(propertyName) : ts.factory.createStringLiteral(propertyName);
33879
+ propertyAssignments.push(
33880
+ ts.factory.createPropertyAssignment(
33881
+ propertyNameNode,
33882
+ schemaExpr
33883
+ )
33884
+ );
33885
+ }
33886
+ const indexInfos = typeChecker.getIndexInfosOfType(type2);
33887
+ const args3 = [
33888
+ ts.factory.createObjectLiteralExpression(propertyAssignments, propertyAssignments.length > 0)
33889
+ ];
33890
+ for (const indexInfo of indexInfos) {
33891
+ hasRecords = true;
33892
+ const keyType = indexInfo.keyType;
33893
+ const valueType = indexInfo.type;
33894
+ const keySchema = yield* processType(keyType, context7);
33895
+ const valueSchema = yield* processType(valueType, context7);
33896
+ args3.push(
33897
+ ts.factory.createObjectLiteralExpression([
33898
+ ts.factory.createPropertyAssignment("key", keySchema),
33899
+ ts.factory.createPropertyAssignment("value", valueSchema)
33900
+ ])
33901
+ );
33902
+ }
33903
+ if (!hasRecords && context7.hoistName) {
33904
+ const ctx = yield* service2(StructuralSchemaGenContext);
33905
+ yield* pushHoistedStatement(
33906
+ ctx,
33907
+ context7.hoistName,
33908
+ type2,
33909
+ ts.factory.createClassDeclaration(
33910
+ void 0,
33911
+ ts.factory.createIdentifier(context7.hoistName),
33912
+ [],
33913
+ [ts.factory.createHeritageClause(
33914
+ ts.SyntaxKind.ExtendsKeyword,
33915
+ [
33916
+ ts.factory.createExpressionWithTypeArguments(
33917
+ ts.factory.createCallExpression(
33918
+ ts.factory.createCallExpression(
33919
+ createApiPropertyAccess("Class"),
33920
+ [ts.factory.createTypeReferenceNode(
33921
+ context7.hoistName
33922
+ )],
33923
+ [ts.factory.createStringLiteral(context7.hoistName)]
33924
+ ),
33925
+ [],
33926
+ args3
33927
+ ),
33928
+ []
33929
+ )
33930
+ ]
33931
+ )],
33932
+ []
33933
+ ),
33934
+ () => ts.factory.createIdentifier(context7.hoistName)
33935
+ );
33936
+ return [ctx.hoistedSchemas.get(type2)(), true];
33937
+ }
33938
+ return [createApiCall("Struct", args3), propertyAssignments.length === 0];
33939
+ }
33940
+ );
33941
+ var findNodeToProcess = fn2("StructuralSchemaGen.findNodeToProcess")(
33942
+ function* (sourceFile, textRange) {
33943
+ const ts = yield* service2(TypeScriptApi);
33944
+ const tsUtils = yield* service2(TypeScriptUtils);
33945
+ const typeChecker = yield* service2(TypeCheckerApi);
33946
+ return pipe(
33947
+ tsUtils.getAncestorNodesInRange(sourceFile, textRange),
33948
+ filter2((node) => ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node)),
33949
+ filter2((node) => tsUtils.isNodeInRange(textRange)(node.name)),
33950
+ filter2((node) => (node.typeParameters || []).length === 0),
33951
+ map4((node) => ({
33952
+ node,
33953
+ identifier: node.name,
33954
+ type: typeChecker.getTypeAtLocation(node.name),
33955
+ isExported: node.modifiers ? (ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export) !== 0 : false
33956
+ })),
33957
+ filter2(({ type: type2 }) => !!type2),
33958
+ head
33959
+ );
33960
+ }
33961
+ );
33962
+ var process2 = fn2("StructuralSchemaGen.process")(
33963
+ function* (sourceFile, scope4, typeMap, isExported, handleCodegeneratedComments) {
33964
+ const ts = yield* service2(TypeScriptApi);
33965
+ const tsUtils = yield* service2(TypeScriptUtils);
33966
+ const typeChecker = yield* service2(TypeCheckerApi);
33967
+ const typeParser = yield* service2(TypeParser);
33968
+ const schemaIdentifier = tsUtils.findImportedModuleIdentifierByPackageAndNameOrBarrel(sourceFile, "effect", "Schema") || "Schema";
33969
+ const ctx = yield* makeStructuralSchemaGenContext(sourceFile, schemaIdentifier);
33970
+ for (const [name, type2] of typeMap.entries()) {
33971
+ ctx.nameToType.set(name, type2);
33972
+ }
33973
+ if (handleCodegeneratedComments) {
33974
+ for (const declaration of sourceFile.statements) {
33975
+ const nodeText = sourceFile.text.slice(declaration.pos, declaration.end);
33976
+ if (!nodeText.toLowerCase().includes("@effect-schema-codegenerated")) continue;
33977
+ const interleavingRange = ctx.rangesToDelete.find(
33978
+ (range3) => range3.pos < declaration.end && range3.end > declaration.pos
33979
+ );
33980
+ if (interleavingRange) {
33981
+ interleavingRange.pos = Math.min(interleavingRange.pos, declaration.pos);
33982
+ interleavingRange.end = Math.max(interleavingRange.end, declaration.end);
33983
+ } else {
33984
+ ctx.rangesToDelete.push({
33985
+ pos: declaration.pos,
33986
+ end: declaration.end
33987
+ });
33988
+ }
33989
+ }
33990
+ }
33991
+ for (const symbol3 of typeChecker.getSymbolsInScope(scope4, ts.SymbolFlags.Value)) {
33992
+ const name = typeChecker.symbolToString(symbol3);
33993
+ ctx.usedGlobalIdentifiers.set(name, 1);
33994
+ const type2 = typeChecker.getTypeOfSymbolAtLocation(symbol3, sourceFile);
33995
+ if (type2) {
33996
+ const schemaType = yield* pipe(
33997
+ typeParser.effectSchemaType(type2, scope4),
33998
+ orElse14(() => void_8)
33999
+ );
34000
+ if (schemaType) {
34001
+ ctx.hoistedSchemas.set(
34002
+ schemaType.A,
34003
+ () => {
34004
+ const expression = typeChecker.symbolToExpression(
34005
+ symbol3,
34006
+ ts.SymbolFlags.Value,
34007
+ scope4,
34008
+ ts.NodeBuilderFlags.NoTruncation
34009
+ );
34010
+ if (expression) {
34011
+ return expression;
34012
+ }
34013
+ return ts.factory.createIdentifier(name);
34014
+ }
34015
+ );
34016
+ }
34017
+ }
34018
+ }
34019
+ const results = yield* pipe(
34020
+ all9(
34021
+ ...fromIterable(ctx.nameToType.entries()).map(
34022
+ ([name, type2]) => pipe(
34023
+ processType(type2),
34024
+ orElse14(
34025
+ (error4) => succeed17(ts.addSyntheticLeadingComment(
34026
+ ts.factory.createIdentifier(""),
34027
+ ts.SyntaxKind.MultiLineCommentTrivia,
34028
+ " " + String(error4) + " ",
34029
+ true
34030
+ ))
34031
+ ),
34032
+ map34((_) => ({ requestedName: name, type: type2, result: _ }))
34033
+ )
34034
+ )
34035
+ ),
34036
+ provideService7(StructuralSchemaGenContext, ctx)
34037
+ );
34038
+ for (const { requestedName, result, type: type2 } of results) {
34039
+ const statementIndex = ctx.typeToStatementIndex.get(type2);
34040
+ if (statementIndex !== void 0) continue;
34041
+ ctx.schemaStatements.push(ts.factory.createVariableStatement(
34042
+ [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
34043
+ ts.factory.createVariableDeclarationList(
34044
+ [ts.factory.createVariableDeclaration(
34045
+ ts.factory.createIdentifier(requestedName),
34046
+ void 0,
34047
+ void 0,
34048
+ result
34049
+ )],
34050
+ ts.NodeFlags.Const
34051
+ )
34052
+ ));
34053
+ ctx.typeToStatementIndex.set(type2, ctx.schemaStatements.length - 1);
34054
+ }
34055
+ if (isExported) {
34056
+ const statementsToExport = pipe(
34057
+ fromIterable(ctx.nameToType),
34058
+ map4(([_, type2]) => ctx.typeToStatementIndex.get(type2)),
34059
+ filter2((index) => index !== void 0),
34060
+ dedupe
34061
+ );
34062
+ for (let i = 0; i < ctx.schemaStatements.length; i++) {
34063
+ if (!statementsToExport.includes(i)) continue;
34064
+ const statement = ctx.schemaStatements[i];
34065
+ if (ts.isVariableStatement(statement)) {
34066
+ ctx.schemaStatements[i] = ts.factory.updateVariableStatement(
34067
+ statement,
34068
+ ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export),
34069
+ statement.declarationList
34070
+ );
34071
+ } else if (ts.isClassDeclaration(statement)) {
34072
+ ctx.schemaStatements[i] = ts.factory.updateClassDeclaration(
34073
+ statement,
34074
+ ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export),
34075
+ statement.name,
34076
+ statement.typeParameters,
34077
+ statement.heritageClauses,
34078
+ statement.members
34079
+ );
34080
+ }
34081
+ }
34082
+ }
34083
+ if (handleCodegeneratedComments) {
34084
+ for (let i = 0; i < ctx.schemaStatements.length; i++) {
34085
+ const statement = ctx.schemaStatements[i];
34086
+ ctx.schemaStatements[i] = ts.addSyntheticLeadingComment(
34087
+ statement,
34088
+ ts.SyntaxKind.SingleLineCommentTrivia,
34089
+ " @effect-schema-codegenerated: This schema will be re-generated by the effect-schema-codegens command, remove this comment to disable re-generation.",
34090
+ true
34091
+ );
34092
+ }
34093
+ }
34094
+ return ctx;
34095
+ }
34096
+ );
34097
+ var applyAtNode = fn2("StructuralSchemaGen.applyAtNode")(
34098
+ function* (sourceFile, node, identifier2, type2, isExported) {
34099
+ const changeTracker = yield* service2(ChangeTracker);
34100
+ const ts = yield* service2(TypeScriptApi);
34101
+ const ctx = yield* process2(sourceFile, node, /* @__PURE__ */ new Map([[ts.idText(identifier2), type2]]), isExported, false);
34102
+ for (const statement of ctx.schemaStatements) {
34103
+ changeTracker.insertNodeAt(sourceFile, node.pos, statement, { prefix: "\n", suffix: "\n" });
34104
+ }
34105
+ }
34106
+ );
34107
+
34108
+ // src/codegens/typeToSchema.ts
34109
+ var typeToSchema = createCodegen({
34110
+ name: "typeToSchema",
34111
+ apply: fn2("typeToSchema.apply")(function* (sourceFile, textRange) {
34112
+ const ts = yield* service2(TypeScriptApi);
34113
+ const tsUtils = yield* service2(TypeScriptUtils);
34114
+ const typeChecker = yield* service2(TypeCheckerApi);
34115
+ const typeCheckerUtils = yield* service2(TypeCheckerUtils);
34116
+ const typeParser = yield* service2(TypeParser);
34117
+ const program = yield* service2(TypeScriptProgram);
34118
+ const inThisFile = yield* getCodegensForSourceFile([typeToSchema], sourceFile);
34119
+ if (inThisFile.length > 1) {
34120
+ return yield* fail18(
34121
+ new CodegenNotApplicableError("the typeToSchema codegen can be used only once per file")
34122
+ );
34123
+ }
34124
+ const parse6 = (node) => gen3(function* () {
34125
+ if (!ts.isTypeAliasDeclaration(node)) {
34126
+ return yield* fail18(
34127
+ new CodegenNotApplicableError(
34128
+ "this codegen is applicable only to a type alias where each object member is a schema to generate. e.g. `type ToGenerate = { UserSchema: User, TodoSchema: Todo}`"
34129
+ )
34130
+ );
34131
+ }
34132
+ const type2 = typeChecker.getTypeAtLocation(node.name);
34133
+ if (!type2) {
34134
+ return yield* fail18(
34135
+ new CodegenNotApplicableError(
34136
+ "error getting the type to process"
34137
+ )
34138
+ );
34139
+ }
34140
+ const nameToType = /* @__PURE__ */ new Map();
34141
+ const typeProperties = typeChecker.getPropertiesOfType(type2);
34142
+ for (const symProp of typeProperties) {
34143
+ const symName = ts.symbolName(symProp);
34144
+ const propType = typeChecker.getTypeOfSymbolAtLocation(symProp, node);
34145
+ if (propType) nameToType.set(symName, propType);
34146
+ }
34147
+ const hash2 = pipe(
34148
+ fromIterable(nameToType),
34149
+ map4(([name, type3]) => {
34150
+ const typeString = typeChecker.typeToString(
34151
+ type3,
34152
+ node,
34153
+ ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.UseStructuralFallback
34154
+ );
34155
+ return name + ": " + typeString;
34156
+ }),
34157
+ join("\n"),
34158
+ cyrb53
34159
+ );
34160
+ return {
34161
+ hash: hash2,
34162
+ nameToType
34163
+ };
34164
+ });
34165
+ const nodeAndCommentRange = tsUtils.findNodeWithLeadingCommentAtPosition(sourceFile, textRange.pos);
34166
+ if (!nodeAndCommentRange) {
34167
+ return yield* fail18(new CodegenNotApplicableError("no node and comment range affected"));
34168
+ }
34169
+ return yield* pipe(
34170
+ parse6(nodeAndCommentRange.node),
34171
+ map34(
34172
+ (_) => ({
34173
+ hash: _.hash,
34174
+ description: "Generate Schemas from types",
34175
+ apply: pipe(
34176
+ gen3(function* () {
34177
+ const changeTracker = yield* service2(ChangeTracker);
34178
+ const ctx = yield* process2(
34179
+ sourceFile,
34180
+ nodeAndCommentRange.node,
34181
+ _.nameToType,
34182
+ true,
34183
+ true
34184
+ );
34185
+ const pos = sourceFile.end;
34186
+ for (const range3 of ctx.rangesToDelete) {
34187
+ changeTracker.deleteRange(sourceFile, range3);
34188
+ }
34189
+ for (const statement of ctx.schemaStatements) {
34190
+ changeTracker.insertNodeAt(sourceFile, pos, statement, { prefix: "\n", suffix: "\n" });
34191
+ }
34192
+ }),
34193
+ provideService7(TypeScriptApi, ts),
34194
+ provideService7(TypeScriptUtils, tsUtils),
34195
+ provideService7(TypeCheckerApi, typeChecker),
34196
+ provideService7(TypeCheckerUtils, typeCheckerUtils),
34197
+ provideService7(TypeParser, typeParser),
34198
+ provideService7(TypeScriptProgram, program)
34199
+ )
34200
+ })
34201
+ )
34202
+ );
34203
+ })
34204
+ });
34205
+
33540
34206
  // src/codegens.ts
33541
- var codegens = [accessors, annotate3];
34207
+ var codegens = [accessors, annotate3, typeToSchema];
33542
34208
 
33543
34209
  // src/cli/codegen.ts
33544
34210
  var NoFilesToCodegenError = class extends TaggedError("NoFilesToCodegenError") {