@effect/language-service 0.4.0 → 0.5.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 +89 -5
  2. package/index.js.map +1 -1
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -1434,12 +1434,47 @@ var missingStarInYieldEffectGen = createDiagnostic({
1434
1434
  }
1435
1435
  });
1436
1436
 
1437
+ // src/diagnostics/unnecessaryEffectGen.ts
1438
+ var unnecessaryEffectGen = createDiagnostic({
1439
+ code: 5,
1440
+ apply: (ts, program) => (sourceFile) => {
1441
+ const typeChecker = program.getTypeChecker();
1442
+ const effectDiagnostics = [];
1443
+ const brokenGenerators = /* @__PURE__ */ new Set();
1444
+ const visit = (node) => {
1445
+ const effectGenLike = effectGen(ts, typeChecker)(node);
1446
+ if (isSome2(effectGenLike)) {
1447
+ const body = effectGenLike.value.body;
1448
+ if (body.statements.length === 1 && ts.isReturnStatement(body.statements[0]) && body.statements[0].expression && ts.isYieldExpression(body.statements[0].expression) && body.statements[0].expression.expression) {
1449
+ const nodeToCheck = body.statements[0].expression.expression;
1450
+ const type = typeChecker.getTypeAtLocation(nodeToCheck);
1451
+ const maybeEffect = effectType(ts, typeChecker)(type, nodeToCheck);
1452
+ if (isSome2(maybeEffect)) {
1453
+ brokenGenerators.add(node);
1454
+ }
1455
+ }
1456
+ }
1457
+ ts.forEachChild(node, visit);
1458
+ };
1459
+ ts.forEachChild(sourceFile, visit);
1460
+ brokenGenerators.forEach(
1461
+ (node) => effectDiagnostics.push({
1462
+ node,
1463
+ category: ts.DiagnosticCategory.Warning,
1464
+ messageText: `This Effect.gen is useless here because it only contains a single return statement.`
1465
+ })
1466
+ );
1467
+ return effectDiagnostics;
1468
+ }
1469
+ });
1470
+
1437
1471
  // src/diagnostics.ts
1438
1472
  var diagnostics = {
1439
1473
  missingEffectContext,
1440
1474
  missingEffectError,
1441
1475
  floatingEffect,
1442
- missingStarInYieldEffectGen
1476
+ missingStarInYieldEffectGen,
1477
+ unnecessaryEffectGen
1443
1478
  };
1444
1479
 
1445
1480
  // src/quickinfo.ts
@@ -1458,6 +1493,47 @@ function dedupeJsDocTags(quickInfo) {
1458
1493
  }
1459
1494
  return quickInfo;
1460
1495
  }
1496
+ function formatTypeForQuickInfo(ts, typeChecker) {
1497
+ return (channelType, channelName) => {
1498
+ const stringRepresentation = typeChecker.typeToString(
1499
+ channelType,
1500
+ void 0,
1501
+ ts.TypeFormatFlags.NoTruncation
1502
+ );
1503
+ return `type ${channelName} = ${stringRepresentation}`;
1504
+ };
1505
+ }
1506
+ function prependEffectTypeArguments(ts, program) {
1507
+ return (sourceFileName, position, quickInfo) => {
1508
+ const sourceFile = program.getSourceFile(sourceFileName);
1509
+ if (!sourceFile) return quickInfo;
1510
+ const hasTruncationHappened = ts.displayPartsToString(quickInfo.displayParts).indexOf("...") > -1;
1511
+ if (!hasTruncationHappened) return quickInfo;
1512
+ const typeChecker = program.getTypeChecker();
1513
+ const effectTypeArgsDocumentation = pipe(
1514
+ getNodesContainingRange(ts)(sourceFile, toTextRange(position)),
1515
+ head,
1516
+ flatMap(
1517
+ (_) => effectType(ts, typeChecker)(typeChecker.getTypeAtLocation(_), _)
1518
+ ),
1519
+ map((_) => [{
1520
+ kind: "text",
1521
+ text: "```ts\n/* Effect Type Parameters */\n" + formatTypeForQuickInfo(ts, typeChecker)(_.A, "Success") + "\n" + formatTypeForQuickInfo(ts, typeChecker)(_.E, "Failure") + "\n" + formatTypeForQuickInfo(ts, typeChecker)(_.R, "Requirements") + "\n```\n"
1522
+ }]),
1523
+ getOrElse(() => [])
1524
+ );
1525
+ if (quickInfo.documentation) {
1526
+ return {
1527
+ ...quickInfo,
1528
+ documentation: effectTypeArgsDocumentation.concat(quickInfo.documentation)
1529
+ };
1530
+ }
1531
+ return {
1532
+ ...quickInfo,
1533
+ documentation: effectTypeArgsDocumentation
1534
+ };
1535
+ };
1536
+ }
1461
1537
 
1462
1538
  // src/refactors/asyncAwaitToGen.ts
1463
1539
  var asyncAwaitToGen = createRefactor({
@@ -1800,7 +1876,9 @@ var toggleTypeAnnotation = createRefactor({
1800
1876
  description: "Toggle type annotation",
1801
1877
  apply: (ts, program) => (sourceFile, textRange) => pipe(
1802
1878
  getNodesContainingRange(ts)(sourceFile, textRange),
1803
- filter(ts.isVariableDeclaration),
1879
+ filter(
1880
+ (node) => ts.isVariableDeclaration(node) || ts.isPropertyDeclaration(node)
1881
+ ),
1804
1882
  filter((node) => isNodeInRange(textRange)(node.name)),
1805
1883
  filter((node) => !!node.initializer),
1806
1884
  head,
@@ -1872,7 +1950,8 @@ var init = (modules) => {
1872
1950
  function create(info) {
1873
1951
  const languageService = info.languageService;
1874
1952
  const pluginOptions = {
1875
- diagnostics: info.config && "diagnostics" in info.config && typeof info.config.diagnostics === "boolean" ? info.config.diagnostics : true
1953
+ diagnostics: info.config && "diagnostics" in info.config && typeof info.config.diagnostics === "boolean" ? info.config.diagnostics : true,
1954
+ quickinfo: info.config && "quickinfo" in info.config && typeof info.config.quickinfo === "boolean" ? info.config.quickinfo : true
1876
1955
  };
1877
1956
  const proxy = /* @__PURE__ */ Object.create(null);
1878
1957
  for (const k of Object.keys(info.languageService)) {
@@ -2005,8 +2084,13 @@ var init = (modules) => {
2005
2084
  };
2006
2085
  proxy.getQuickInfoAtPosition = (fileName, position, ...args) => {
2007
2086
  const quickInfo = languageService.getQuickInfoAtPosition(fileName, position, ...args);
2008
- if (quickInfo) {
2009
- return dedupeJsDocTags(quickInfo);
2087
+ if (pluginOptions.quickinfo && quickInfo) {
2088
+ const dedupedTagsQuickInfo = dedupeJsDocTags(quickInfo);
2089
+ const program = languageService.getProgram();
2090
+ if (program) {
2091
+ return prependEffectTypeArguments(ts, program)(fileName, position, dedupedTagsQuickInfo);
2092
+ }
2093
+ return dedupedTagsQuickInfo;
2010
2094
  }
2011
2095
  return quickInfo;
2012
2096
  };