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