Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API request: allow ignoring ts-(expect-error|ignore) in getPreEmitDiagnostics #51749

Open
5 tasks done
JoshuaKGoldberg opened this issue Dec 4, 2022 · 3 comments
Open
5 tasks done
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented Dec 4, 2022

Suggestion

πŸ” Search Terms

ignore comment directives ts-expect-error ts-ignore

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

API consumers such as typescript-eslint would benefit from being able to get the diagnostics in a file that would be emitted were it not for a // @ts-expect-error or // @ts-ignore. Can we add in an options parameter to program.getPreEmitDiagnostics to allow consumers to stop those comment directives from suppressing errors?

I threw together a rough draft of how this could look in the TypeScript source here: JoshuaKGoldberg/TypeScript@a7c07ff...JoshuaKGoldberg:TypeScript:get-diagnostics-options. Just in case it's helpful.

πŸ“ƒ Motivating Example

JoshuaKGoldberg/eslint-plugin-expect-type#65: we'd like to make lint rules that assert on the error message matching what's expected, as a userland solution to #19139 not being accepted.

πŸ’» Use Cases

Here's how we could change what eslint-plugin-expect-type does today:

- const diagnostics = ts.getPreEmitDiagnostics(program, sourceFile);
+ const diagnostics = ts.getPreEmitDiagnostics(program, sourceFile, { skipCommentDirectiveErrorSuppressions: true });

Without this API, users either:

  • Can't both suppress type errors and lint that the suppressions match up with TypeScript
  • Need to have lint rules & similar rewrite files to remove the comments - essentially parsing & type checking them twice
@RyanCavanaugh
Copy link
Member

We try to keep our API surface pretty minimal. Since it's an API call, you can transform the source text (replace ts-ignore with ts-zzzzzz) before handing it over to get equivalent results. Thoughts?

@JoshuaKGoldberg
Copy link
Contributor Author

No juice 😞. We need to be able to get the original text in case of anything that transforms text. For example, ESLint rule complaints can have fixers that move entire functions / other chunks of code. Fixers have no way of knowing that the user has configured @typescript-eslint/parser -> @typescript-eslint/estree with some setting like { rewriteDirectives: true }.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Dec 6, 2022
@fatcerberus
Copy link

Rewriting the comments also has the concerns about duplicated parsing work mentioned in the OP (unless it's just a dumb text-based find/replace but that feels problematic).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants