@effect/language-service 0.5.1 → 0.6.0

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/index.js +141 -21
  2. package/index.js.map +1 -1
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -115,6 +115,7 @@ var globalValue = (id, compute) => {
115
115
  };
116
116
 
117
117
  // node_modules/.pnpm/effect@3.12.5/node_modules/effect/dist/esm/Predicate.js
118
+ var isBoolean = (input) => typeof input === "boolean";
118
119
  var isFunction2 = isFunction;
119
120
  var isRecordOrArray = (input) => typeof input === "object" && input !== null;
120
121
  var isObject = (input) => isRecordOrArray(input) || isFunction2(input);
@@ -814,12 +815,14 @@ function effectGen(ts, typeChecker) {
814
815
  if (!ts.isPropertyAccessExpression(node.expression)) return yield* none2();
815
816
  const propertyAccess = node.expression;
816
817
  if (propertyAccess.name.text !== "gen") return yield* none2();
817
- return yield* importedEffectModule(ts, typeChecker)(propertyAccess.expression).pipe(
818
- map(() => ({
819
- body: generatorFunction.body,
820
- functionStar: generatorFunction.getFirstToken()
821
- }))
822
- );
818
+ const effectModule = yield* importedEffectModule(ts, typeChecker)(propertyAccess.expression);
819
+ return {
820
+ node,
821
+ effectModule,
822
+ generatorFunction,
823
+ body: generatorFunction.body,
824
+ functionStar: generatorFunction.getFirstToken()
825
+ };
823
826
  });
824
827
  }
825
828
  function effectFnUntracedGen(ts, typeChecker) {
@@ -832,12 +835,14 @@ function effectFnUntracedGen(ts, typeChecker) {
832
835
  if (!ts.isPropertyAccessExpression(node.expression)) return yield* none2();
833
836
  const propertyAccess = node.expression;
834
837
  if (propertyAccess.name.text !== "fnUntraced") return yield* none2();
835
- return yield* importedEffectModule(ts, typeChecker)(propertyAccess.expression).pipe(
836
- map(() => ({
837
- body: generatorFunction.body,
838
- functionStar: generatorFunction.getFirstToken()
839
- }))
840
- );
838
+ const effectModule = yield* importedEffectModule(ts, typeChecker)(propertyAccess.expression);
839
+ return {
840
+ node,
841
+ effectModule,
842
+ generatorFunction,
843
+ body: generatorFunction.body,
844
+ functionStar: generatorFunction.getFirstToken()
845
+ };
841
846
  });
842
847
  }
843
848
  function effectFnGen(ts, typeChecker) {
@@ -851,12 +856,14 @@ function effectFnGen(ts, typeChecker) {
851
856
  if (!ts.isPropertyAccessExpression(expressionToTest)) return yield* none2();
852
857
  const propertyAccess = expressionToTest;
853
858
  if (propertyAccess.name.text !== "fn") return yield* none2();
854
- return yield* importedEffectModule(ts, typeChecker)(propertyAccess.expression).pipe(
855
- map(() => ({
856
- body: generatorFunction.body,
857
- functionStar: generatorFunction.getFirstToken()
858
- }))
859
- );
859
+ const effectModule = yield* importedEffectModule(ts, typeChecker)(propertyAccess.expression);
860
+ return {
861
+ node,
862
+ generatorFunction,
863
+ effectModule,
864
+ body: generatorFunction.body,
865
+ functionStar: generatorFunction.getFirstToken()
866
+ };
860
867
  });
861
868
  }
862
869
  function expectedAndRealType(ts, typeChecker) {
@@ -958,6 +965,25 @@ var floatingEffect = createDiagnostic({
958
965
  // node_modules/.pnpm/effect@3.12.5/node_modules/effect/dist/esm/internal/array.js
959
966
  var isNonEmptyArray = (self) => self.length > 0;
960
967
 
968
+ // node_modules/.pnpm/effect@3.12.5/node_modules/effect/dist/esm/Iterable.js
969
+ var findFirst = /* @__PURE__ */ dual(2, (self, f) => {
970
+ let i = 0;
971
+ for (const a of self) {
972
+ const o = f(a, i);
973
+ if (isBoolean(o)) {
974
+ if (o) {
975
+ return some2(a);
976
+ }
977
+ } else {
978
+ if (isSome2(o)) {
979
+ return o;
980
+ }
981
+ }
982
+ i++;
983
+ }
984
+ return none2();
985
+ });
986
+
961
987
  // node_modules/.pnpm/effect@3.12.5/node_modules/effect/dist/esm/Array.js
962
988
  var fromIterable = (collection) => Array.isArray(collection) ? collection : Array.from(collection);
963
989
  var append = /* @__PURE__ */ dual(2, (self, last) => [...self, last]);
@@ -979,6 +1005,7 @@ var unsafeGet = /* @__PURE__ */ dual(2, (self, index) => {
979
1005
  var head = /* @__PURE__ */ get(0);
980
1006
  var headNonEmpty = /* @__PURE__ */ unsafeGet(0);
981
1007
  var tailNonEmpty = (self) => self.slice(1);
1008
+ var findFirst2 = findFirst;
982
1009
  var sort = /* @__PURE__ */ dual(2, (self, O) => {
983
1010
  const out = Array.from(self);
984
1011
  out.sort(O);
@@ -1544,7 +1571,9 @@ var asyncAwaitToGen = createRefactor({
1544
1571
  description: "Convert to Effect.gen",
1545
1572
  apply: (ts, program) => (sourceFile, textRange) => pipe(
1546
1573
  getNodesContainingRange(ts)(sourceFile, textRange),
1547
- filter(ts.isFunctionDeclaration),
1574
+ filter(
1575
+ (node) => ts.isFunctionDeclaration(node) || ts.isArrowFunction(node) || ts.isFunctionExpression(node)
1576
+ ),
1548
1577
  filter((node) => !!node.body),
1549
1578
  filter(
1550
1579
  (node) => !!(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Async)
@@ -1590,7 +1619,9 @@ var asyncAwaitToGenTryPromise = createRefactor({
1590
1619
  description: "Convert to Effect.gen with failures",
1591
1620
  apply: (ts, program) => (sourceFile, textRange) => pipe(
1592
1621
  getNodesContainingRange(ts)(sourceFile, textRange),
1593
- filter(ts.isFunctionDeclaration),
1622
+ filter(
1623
+ (node) => ts.isFunctionDeclaration(node) || ts.isArrowFunction(node) || ts.isFunctionExpression(node)
1624
+ ),
1594
1625
  filter((node) => !!node.body),
1595
1626
  filter(
1596
1627
  (node) => !!(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Async)
@@ -1660,6 +1691,94 @@ var asyncAwaitToGenTryPromise = createRefactor({
1660
1691
  )
1661
1692
  });
1662
1693
 
1694
+ // src/refactors/effectGenToFn.ts
1695
+ var effectGenToFn = createRefactor({
1696
+ name: "effect/effectGenToFn",
1697
+ description: "Convert to Effect.fn",
1698
+ apply: (ts, program) => (sourceFile, textRange) => pipe(
1699
+ getNodesContainingRange(ts)(sourceFile, textRange),
1700
+ findFirst2(
1701
+ (node) => gen(function* () {
1702
+ const effectGen2 = yield* effectGen(ts, program.getTypeChecker())(node);
1703
+ let pipeArgs = ts.factory.createNodeArray([]);
1704
+ let nodeToReplace = node.parent;
1705
+ if (ts.isPropertyAccessExpression(node.parent) && node.parent.name.text === "pipe" && ts.isCallExpression(node.parent.parent)) {
1706
+ pipeArgs = node.parent.parent.arguments;
1707
+ nodeToReplace = node.parent.parent.parent;
1708
+ }
1709
+ while (nodeToReplace) {
1710
+ if (ts.isArrowFunction(nodeToReplace) || ts.isFunctionDeclaration(nodeToReplace)) {
1711
+ return { ...effectGen2, pipeArgs, nodeToReplace };
1712
+ }
1713
+ if (ts.isConciseBody(nodeToReplace) || ts.isReturnStatement(nodeToReplace)) {
1714
+ nodeToReplace = nodeToReplace.parent;
1715
+ continue;
1716
+ }
1717
+ if (ts.isBlock(nodeToReplace) && nodeToReplace.statements.length === 1) {
1718
+ nodeToReplace = nodeToReplace.parent;
1719
+ continue;
1720
+ }
1721
+ break;
1722
+ }
1723
+ return yield* none2();
1724
+ })
1725
+ ),
1726
+ map(
1727
+ ({ effectModule, generatorFunction, nodeToReplace, pipeArgs }) => ({
1728
+ kind: "refactor.rewrite.effect.effectGenToFn",
1729
+ description: "Convert to Effect.fn",
1730
+ apply: (changeTracker) => {
1731
+ const effectFn = nodeToReplace.name ? ts.factory.createCallExpression(
1732
+ ts.factory.createPropertyAccessExpression(
1733
+ effectModule,
1734
+ "fn"
1735
+ ),
1736
+ void 0,
1737
+ [ts.factory.createStringLiteral(nodeToReplace.name.text)]
1738
+ ) : ts.factory.createPropertyAccessExpression(
1739
+ effectModule,
1740
+ "fn"
1741
+ );
1742
+ const effectFnCallWithGenerator = ts.factory.createCallExpression(
1743
+ effectFn,
1744
+ void 0,
1745
+ [ts.factory.createFunctionExpression(
1746
+ void 0,
1747
+ ts.factory.createToken(ts.SyntaxKind.AsteriskToken),
1748
+ void 0,
1749
+ nodeToReplace.typeParameters,
1750
+ nodeToReplace.parameters,
1751
+ nodeToReplace.type,
1752
+ generatorFunction.body
1753
+ )].concat(pipeArgs)
1754
+ );
1755
+ if (!ts.isArrowFunction(nodeToReplace) && nodeToReplace.name) {
1756
+ const variableDeclaration = ts.factory.createVariableStatement(
1757
+ nodeToReplace.modifiers,
1758
+ ts.factory.createVariableDeclarationList(
1759
+ [ts.factory.createVariableDeclaration(
1760
+ nodeToReplace.name,
1761
+ void 0,
1762
+ void 0,
1763
+ effectFnCallWithGenerator
1764
+ )],
1765
+ ts.NodeFlags.Const
1766
+ )
1767
+ );
1768
+ changeTracker.replaceNode(
1769
+ sourceFile,
1770
+ nodeToReplace,
1771
+ variableDeclaration
1772
+ );
1773
+ } else {
1774
+ changeTracker.replaceNode(sourceFile, nodeToReplace, effectFnCallWithGenerator);
1775
+ }
1776
+ }
1777
+ })
1778
+ )
1779
+ )
1780
+ });
1781
+
1663
1782
  // src/refactors/functionToArrow.ts
1664
1783
  var functionToArrow = createRefactor({
1665
1784
  name: "effect/functionToArrow",
@@ -1953,7 +2072,8 @@ var refactors = {
1953
2072
  toggleLazyConst,
1954
2073
  toggleReturnTypeAnnotation,
1955
2074
  toggleTypeAnnotation,
1956
- wrapWithPipe
2075
+ wrapWithPipe,
2076
+ effectGenToFn
1957
2077
  };
1958
2078
 
1959
2079
  // src/index.ts