microsoft / microsoft/TypeScript

Feature: Analyze @throws tags

未关闭
#31,329 12 条评论 55 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Awaiting More Feedback Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

Description

Given a function has a @throws JSDoc tag, it would be helpful to apply some analysis and raise a compiler warning (or, if configured, an error) when the caller does not handle the error and it doesn't declare its own @throws tag.

Examples
/**
 * @throws {SomeException}
 */
function myFunction() {
  // some logic
  // then for some reason we throw an exception
  if (somethingWentWrong) {
    throw new SomeException("something happened");
  }
  // some more logic
}

function potentialMess() {
  myFunction(); // Compiler error: 'myFunction' may throw `SomeException`. ts(9876)
}

/**
 * @throws {SomeException}
 */
function letItBubble() {
  myFunction(); // No compiler errors
}

function aFunctionThatHandlesTheException() {
  try {
    myFunction(); // No compiler errors
  }
  catch (error) {
    // do something with the error
  }
}
Questions
  • Ideally, it would be even better if VSCode realizes that myFunction throws an error, and then @throws is not required for the analysis. Still, this approach would be useful when dealing with external code/interfaces.

  • It is my understanding that JSDoc's @throws is allowed once -- if this is correct, then VSCode could ignore this and accept multiple tags anyway? Otherwise, maybe we can use a different tag, e.g. @exception?

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 issue 的 JSDoc @throws 示例开始,跟踪 TypeScript 编译器如何处理调用、抛出的错误以及 try/catch 控制流。将完成定义为:对未处理的调用给出警告或可配置的错误;当调用方声明 @throws 时进行传播;异常被捕获时不产生诊断;关于 inferred throws 和重复标签的问题需要做出设计决策。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。