@effect/language-service 0.56.0 → 0.57.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.
@@ -90,6 +90,8 @@ var dual = function(arity, body) {
90
90
  }
91
91
  };
92
92
  var identity = (a) => a;
93
+ var constant = (value) => () => value;
94
+ var constUndefined = /* @__PURE__ */ constant(void 0);
93
95
  function pipe(a, ab, bc, cd, de, ef, fg, gh, hi) {
94
96
  switch (arguments.length) {
95
97
  case 1:
@@ -738,8 +740,10 @@ var none2 = () => none;
738
740
  var some2 = some;
739
741
  var isNone2 = isNone;
740
742
  var isSome2 = isSome;
743
+ var getOrElse2 = /* @__PURE__ */ dual(2, (self, onNone) => isNone2(self) ? onNone() : self.value);
741
744
  var orElse = /* @__PURE__ */ dual(2, (self, that) => isNone2(self) ? that() : self);
742
745
  var fromNullable = (nullableValue) => nullableValue == null ? none2() : some2(nullableValue);
746
+ var getOrUndefined = /* @__PURE__ */ getOrElse2(constUndefined);
743
747
 
744
748
  // node_modules/.pnpm/effect@3.19.0/node_modules/effect/dist/esm/Record.js
745
749
  var map2 = /* @__PURE__ */ dual(2, (self, f) => {
@@ -2601,6 +2605,51 @@ function make2(ts, tsUtils, typeChecker, typeCheckerUtils, program) {
2601
2605
  if (!symbol3) return typeParserIssue("Node has no symbol", void 0, givenNode);
2602
2606
  return isSymbolExportOfPackageModule(symbol3, packageName, memberName, isCorrectSourceFile);
2603
2607
  };
2608
+ const findSymbolsMatchingPackageAndExportedName = (packageName, exportedSymbolName) => cachedBy(
2609
+ fn("TypeParser.findSymbolsMatchingPackageAndExportedName")(function* (_fromSourceFile) {
2610
+ const result = [];
2611
+ for (const sourceFile of program.getSourceFiles()) {
2612
+ const moduleSymbol = typeChecker.getSymbolAtLocation(sourceFile);
2613
+ if (!moduleSymbol) continue;
2614
+ const symbol3 = typeChecker.tryGetMemberInModuleExports(exportedSymbolName, moduleSymbol);
2615
+ if (!symbol3) continue;
2616
+ const packageInfo = yield* getSourceFilePackageInfo(sourceFile);
2617
+ if (!packageInfo || packageInfo.name.toLowerCase() !== packageName.toLowerCase()) continue;
2618
+ result.push([symbol3, sourceFile]);
2619
+ }
2620
+ return result;
2621
+ }),
2622
+ `TypeParser.findSymbolsMatchingPackageAndExportedName(${packageName}, ${exportedSymbolName})`,
2623
+ (sourceFile) => sourceFile
2624
+ );
2625
+ const isCauseTypeSourceFile = cachedBy(
2626
+ fn("TypeParser.isCauseTypeSourceFile")(function* (sourceFile) {
2627
+ const moduleSymbol = typeChecker.getSymbolAtLocation(sourceFile);
2628
+ if (!moduleSymbol) return yield* typeParserIssue("Node has no symbol", void 0, sourceFile);
2629
+ const causeTypeSymbol = typeChecker.tryGetMemberInModuleExports("Cause", moduleSymbol);
2630
+ if (!causeTypeSymbol) return yield* typeParserIssue("Cause type not found", void 0, sourceFile);
2631
+ const type = typeChecker.getDeclaredTypeOfSymbol(causeTypeSymbol);
2632
+ yield* pipeableType(type, sourceFile);
2633
+ return sourceFile;
2634
+ }),
2635
+ "TypeParser.isCauseTypeSourceFile",
2636
+ (sourceFile) => sourceFile
2637
+ );
2638
+ const effectCauseYieldableErrorTypes = cachedBy(
2639
+ fn("TypeParser.effectCauseYieldableErrorTypes")(function* (fromSourceFile) {
2640
+ const symbols = yield* findSymbolsMatchingPackageAndExportedName("effect", "YieldableError")(fromSourceFile);
2641
+ const result = [];
2642
+ for (const [symbol3, sourceFile] of symbols) {
2643
+ const causeFile = yield* isCauseTypeSourceFile(sourceFile);
2644
+ if (!causeFile) continue;
2645
+ const type = typeChecker.getDeclaredTypeOfSymbol(symbol3);
2646
+ result.push(type);
2647
+ }
2648
+ return result;
2649
+ }),
2650
+ "TypeParser.effectCauseYieldableErrorTypes",
2651
+ (fromSourceFile) => fromSourceFile
2652
+ );
2604
2653
  function covariantTypeArgument(type) {
2605
2654
  const signatures = typeChecker.getSignaturesOfType(type, ts.SignatureKind.Call);
2606
2655
  if (signatures.length !== 1) {
@@ -3590,6 +3639,7 @@ function make2(ts, tsUtils, typeChecker, typeCheckerUtils, program) {
3590
3639
  effectGen,
3591
3640
  effectFnUntracedGen,
3592
3641
  effectFnGen,
3642
+ effectCauseYieldableErrorTypes,
3593
3643
  unnecessaryEffectGen: unnecessaryEffectGen2,
3594
3644
  effectSchemaType,
3595
3645
  contextTag,
@@ -5508,8 +5558,93 @@ var accessors = createCodegen({
5508
5558
  })
5509
5559
  });
5510
5560
 
5561
+ // src/codegens/annotate.ts
5562
+ var annotate = createCodegen({
5563
+ name: "annotate",
5564
+ apply: fn("annotate.apply")(function* (sourceFile, textRange) {
5565
+ const ts = yield* service(TypeScriptApi);
5566
+ const tsUtils = yield* service(TypeScriptUtils);
5567
+ const typeChecker = yield* service(TypeCheckerApi);
5568
+ const typeCheckerUtils = yield* service(TypeCheckerUtils);
5569
+ const parse3 = (node) => gen(function* () {
5570
+ let variableDeclarations = [];
5571
+ const result = [];
5572
+ if (ts.isVariableStatement(node)) {
5573
+ variableDeclarations = [...variableDeclarations, ...node.declarationList.declarations];
5574
+ } else if (ts.isVariableDeclarationList(node)) {
5575
+ variableDeclarations = [...variableDeclarations, ...node.declarations];
5576
+ } else if (ts.isVariableDeclaration(node)) {
5577
+ variableDeclarations = [...variableDeclarations, node];
5578
+ }
5579
+ if (variableDeclarations.length === 0) {
5580
+ return yield* fail(new CodegenNotApplicableError("not a variable declaration"));
5581
+ }
5582
+ for (const variableDeclaration of variableDeclarations) {
5583
+ if (!variableDeclaration.initializer) continue;
5584
+ const initializerType = typeChecker.getTypeAtLocation(variableDeclaration.initializer);
5585
+ const initializerTypeNode = fromNullable(typeCheckerUtils.typeToSimplifiedTypeNode(
5586
+ initializerType,
5587
+ node,
5588
+ ts.NodeBuilderFlags.NoTruncation
5589
+ )).pipe(
5590
+ orElse(
5591
+ () => fromNullable(typeCheckerUtils.typeToSimplifiedTypeNode(
5592
+ initializerType,
5593
+ void 0,
5594
+ ts.NodeBuilderFlags.NoTruncation
5595
+ ))
5596
+ ),
5597
+ getOrUndefined
5598
+ );
5599
+ if (!initializerTypeNode) continue;
5600
+ const typeNodeString = typeChecker.typeToString(initializerType, void 0, ts.TypeFormatFlags.NoTruncation);
5601
+ const hash3 = cyrb53(typeNodeString);
5602
+ result.push({ variableDeclaration, initializerTypeNode, hash: hash3 });
5603
+ }
5604
+ if (result.length === 0) {
5605
+ return yield* fail(new CodegenNotApplicableError("no variable declarations with initializers"));
5606
+ }
5607
+ const hash2 = cyrb53(result.map((_) => _.hash).join("/"));
5608
+ return {
5609
+ hash: hash2,
5610
+ result
5611
+ };
5612
+ });
5613
+ const nodeAndCommentRange = tsUtils.findNodeWithLeadingCommentAtPosition(sourceFile, textRange.pos);
5614
+ if (!nodeAndCommentRange) return yield* fail(new CodegenNotApplicableError("no node and comment range"));
5615
+ return yield* pipe(
5616
+ parse3(nodeAndCommentRange.node),
5617
+ map4(
5618
+ (_) => ({
5619
+ hash: _.hash,
5620
+ description: "Annotate with type",
5621
+ apply: gen(function* () {
5622
+ const changeTracker = yield* service(ChangeTracker);
5623
+ for (const { initializerTypeNode, variableDeclaration } of _.result) {
5624
+ if (variableDeclaration.type) {
5625
+ changeTracker.deleteRange(sourceFile, {
5626
+ pos: variableDeclaration.name.end,
5627
+ end: variableDeclaration.type.end
5628
+ });
5629
+ }
5630
+ changeTracker.insertNodeAt(
5631
+ sourceFile,
5632
+ variableDeclaration.name.end,
5633
+ initializerTypeNode,
5634
+ {
5635
+ prefix: ": "
5636
+ }
5637
+ );
5638
+ }
5639
+ })
5640
+ })
5641
+ )
5642
+ );
5643
+ })
5644
+ });
5645
+
5511
5646
  // src/codegens.ts
5512
- var codegens = [accessors];
5647
+ var codegens = [accessors, annotate];
5513
5648
 
5514
5649
  // src/diagnostics/outdatedEffectCodegen.ts
5515
5650
  var outdatedEffectCodegen = createDiagnostic({
@@ -5766,6 +5901,11 @@ var runEffectInsideEffect = createDiagnostic({
5766
5901
  apply: fn("runEffectInsideEffect.apply")(function* (sourceFile, report) {
5767
5902
  const ts = yield* service(TypeScriptApi);
5768
5903
  const typeParser = yield* service(TypeParser);
5904
+ const tsUtils = yield* service(TypeScriptUtils);
5905
+ const parseEffectMethod = (node, methodName) => pipe(
5906
+ typeParser.isNodeReferenceToEffectModuleApi(methodName)(node),
5907
+ map4(() => ({ node, methodName }))
5908
+ );
5769
5909
  const nodeToVisit = [];
5770
5910
  const appendNodeToVisit = (node) => {
5771
5911
  nodeToVisit.push(node);
@@ -5776,11 +5916,12 @@ var runEffectInsideEffect = createDiagnostic({
5776
5916
  const node = nodeToVisit.shift();
5777
5917
  ts.forEachChild(node, appendNodeToVisit);
5778
5918
  if (!ts.isCallExpression(node)) continue;
5919
+ if (node.arguments.length === 0) continue;
5779
5920
  const isEffectRunCall = yield* pipe(
5780
- typeParser.isNodeReferenceToEffectModuleApi("runPromise")(node.expression),
5781
- orElse2(() => typeParser.isNodeReferenceToEffectModuleApi("runSync")(node.expression)),
5782
- orElse2(() => typeParser.isNodeReferenceToEffectModuleApi("runFork")(node.expression)),
5783
- orElse2(() => typeParser.isNodeReferenceToEffectModuleApi("runCallback")(node.expression)),
5921
+ parseEffectMethod(node.expression, "runPromise"),
5922
+ orElse2(() => parseEffectMethod(node.expression, "runSync")),
5923
+ orElse2(() => parseEffectMethod(node.expression, "runFork")),
5924
+ orElse2(() => parseEffectMethod(node.expression, "runCallback")),
5784
5925
  option
5785
5926
  );
5786
5927
  if (isNone2(isEffectRunCall)) continue;
@@ -5800,18 +5941,90 @@ var runEffectInsideEffect = createDiagnostic({
5800
5941
  orElse2(() => typeParser.effectFnGen(possiblyEffectGen)),
5801
5942
  option
5802
5943
  );
5803
- if (isSome2(isInEffectGen)) {
5944
+ if (isSome2(isInEffectGen) && isInEffectGen.value.body.statements.length > 0) {
5804
5945
  const nodeText = sourceFile.text.substring(
5805
5946
  ts.getTokenPosOfNode(node.expression, sourceFile),
5806
5947
  node.expression.end
5807
5948
  );
5808
- const messageText = nodeIntroduceScope && nodeIntroduceScope !== isInEffectGen.value.generatorFunction ? `Using ${nodeText} inside an Effect is not recommended. The same runtime should generally be used instead to run child effects.
5809
- Consider extracting the Runtime by using for example Effect.runtime and then use Runtime.run* with the extracted runtime instead.` : `Using ${nodeText} inside an Effect is not recommended. Effects inside generators can usually just be yielded.`;
5810
- report({
5811
- location: node.expression,
5812
- messageText,
5813
- fixes: []
5814
- });
5949
+ if (nodeIntroduceScope && nodeIntroduceScope !== isInEffectGen.value.generatorFunction) {
5950
+ const fixAddRuntime = gen(function* () {
5951
+ const changeTracker = yield* service(ChangeTracker);
5952
+ const runtimeModuleIdentifier = tsUtils.findImportedModuleIdentifierByPackageAndNameOrBarrel(sourceFile, "effect", "Runtime") || "Runtime";
5953
+ const effectModuleIdentifier = tsUtils.findImportedModuleIdentifierByPackageAndNameOrBarrel(sourceFile, "effect", "Effect") || "Effect";
5954
+ let runtimeIdentifier = void 0;
5955
+ for (const statement of isInEffectGen.value.generatorFunction.body.statements) {
5956
+ if (ts.isVariableStatement(statement) && statement.declarationList.declarations.length === 1) {
5957
+ const declaration = statement.declarationList.declarations[0];
5958
+ if (declaration.initializer && ts.isYieldExpression(declaration.initializer) && declaration.initializer.asteriskToken && declaration.initializer.expression) {
5959
+ const yieldedExpression = declaration.initializer.expression;
5960
+ if (ts.isCallExpression(yieldedExpression)) {
5961
+ const maybeEffectRuntime = yield* pipe(
5962
+ typeParser.isNodeReferenceToEffectModuleApi("runtime")(yieldedExpression.expression),
5963
+ option
5964
+ );
5965
+ if (isSome2(maybeEffectRuntime) && ts.isIdentifier(declaration.name)) {
5966
+ runtimeIdentifier = ts.idText(declaration.name);
5967
+ }
5968
+ }
5969
+ }
5970
+ }
5971
+ }
5972
+ if (!runtimeIdentifier) {
5973
+ changeTracker.insertNodeAt(
5974
+ sourceFile,
5975
+ isInEffectGen.value.body.statements[0].pos,
5976
+ ts.factory.createVariableStatement(
5977
+ void 0,
5978
+ ts.factory.createVariableDeclarationList([ts.factory.createVariableDeclaration(
5979
+ "effectRuntime",
5980
+ void 0,
5981
+ void 0,
5982
+ ts.factory.createYieldExpression(
5983
+ ts.factory.createToken(ts.SyntaxKind.AsteriskToken),
5984
+ ts.factory.createCallExpression(
5985
+ ts.factory.createPropertyAccessExpression(
5986
+ ts.factory.createIdentifier(effectModuleIdentifier),
5987
+ "runtime"
5988
+ ),
5989
+ [ts.factory.createTypeReferenceNode("never")],
5990
+ []
5991
+ )
5992
+ )
5993
+ )], ts.NodeFlags.Const)
5994
+ ),
5995
+ {
5996
+ prefix: "\n",
5997
+ suffix: "\n"
5998
+ }
5999
+ );
6000
+ }
6001
+ changeTracker.deleteRange(sourceFile, {
6002
+ pos: ts.getTokenPosOfNode(node.expression, sourceFile),
6003
+ end: node.arguments[0].pos
6004
+ });
6005
+ changeTracker.insertText(
6006
+ sourceFile,
6007
+ node.arguments[0].pos,
6008
+ `${runtimeModuleIdentifier}.${isEffectRunCall.value.methodName}(${runtimeIdentifier || "effectRuntime"}, `
6009
+ );
6010
+ });
6011
+ report({
6012
+ location: node.expression,
6013
+ messageText: `Using ${nodeText} inside an Effect is not recommended. The same runtime should generally be used instead to run child effects.
6014
+ Consider extracting the Runtime by using for example Effect.runtime and then use Runtime.${isEffectRunCall.value.methodName} with the extracted runtime instead.`,
6015
+ fixes: [{
6016
+ fixName: "runEffectInsideEffect_fix",
6017
+ description: "Use a runtime to run the Effect",
6018
+ apply: fixAddRuntime
6019
+ }]
6020
+ });
6021
+ } else {
6022
+ report({
6023
+ location: node.expression,
6024
+ messageText: `Using ${nodeText} inside an Effect is not recommended. Effects inside generators can usually just be yielded.`,
6025
+ fixes: []
6026
+ });
6027
+ }
5815
6028
  }
5816
6029
  currentParent = currentParent.parent;
5817
6030
  }
@@ -6241,6 +6454,66 @@ var unnecessaryEffectGen = createDiagnostic({
6241
6454
  })
6242
6455
  });
6243
6456
 
6457
+ // src/diagnostics/unnecessaryFailYieldableError.ts
6458
+ var unnecessaryFailYieldableError = createDiagnostic({
6459
+ name: "unnecessaryFailYieldableError",
6460
+ code: 29,
6461
+ severity: "suggestion",
6462
+ apply: fn("unnecessaryFailYieldableError.apply")(function* (sourceFile, report) {
6463
+ const ts = yield* service(TypeScriptApi);
6464
+ const typeParser = yield* service(TypeParser);
6465
+ const typeChecker = yield* service(TypeCheckerApi);
6466
+ const yieldableErrorTypes = yield* pipe(
6467
+ typeParser.effectCauseYieldableErrorTypes(sourceFile),
6468
+ orElse2(() => succeed([]))
6469
+ );
6470
+ const nodeToVisit = [];
6471
+ const appendNodeToVisit = (node) => {
6472
+ nodeToVisit.push(node);
6473
+ return void 0;
6474
+ };
6475
+ ts.forEachChild(sourceFile, appendNodeToVisit);
6476
+ while (nodeToVisit.length > 0) {
6477
+ const node = nodeToVisit.shift();
6478
+ ts.forEachChild(node, appendNodeToVisit);
6479
+ if (ts.isYieldExpression(node) && node.asteriskToken && node.expression && ts.isCallExpression(node.expression)) {
6480
+ const callExpression = node.expression;
6481
+ yield* pipe(
6482
+ typeParser.isNodeReferenceToEffectModuleApi("fail")(callExpression.expression),
6483
+ map4(() => {
6484
+ if (callExpression.arguments.length > 0) {
6485
+ const failArgument = callExpression.arguments[0];
6486
+ const argumentType = typeChecker.getTypeAtLocation(failArgument);
6487
+ const isYieldableError = yieldableErrorTypes.some(
6488
+ (yieldableType) => typeChecker.isTypeAssignableTo(argumentType, yieldableType)
6489
+ );
6490
+ if (isYieldableError) {
6491
+ report({
6492
+ location: node,
6493
+ messageText: `This Effect.fail call uses a yieldable error type as argument. You can yield* the error directly instead.`,
6494
+ fixes: [{
6495
+ fixName: "unnecessaryFailYieldableError_fix",
6496
+ description: "Replace yield* Effect.fail with yield*",
6497
+ apply: gen(function* () {
6498
+ const changeTracker = yield* service(ChangeTracker);
6499
+ changeTracker.replaceNode(
6500
+ sourceFile,
6501
+ callExpression,
6502
+ failArgument
6503
+ );
6504
+ })
6505
+ }]
6506
+ });
6507
+ }
6508
+ }
6509
+ }),
6510
+ ignore
6511
+ );
6512
+ }
6513
+ }
6514
+ })
6515
+ });
6516
+
6244
6517
  // src/diagnostics/unnecessaryPipe.ts
6245
6518
  var unnecessaryPipe = createDiagnostic({
6246
6519
  name: "unnecessaryPipe",
@@ -6428,6 +6701,7 @@ var diagnostics = [
6428
6701
  floatingEffect,
6429
6702
  missingStarInYieldEffectGen,
6430
6703
  unnecessaryEffectGen,
6704
+ unnecessaryFailYieldableError,
6431
6705
  missingReturnYieldStar,
6432
6706
  leakingRequirements,
6433
6707
  unnecessaryPipe,