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

Valid "unreachable" code is being aggressively removed as a result of syntax errors when allowUnreachableCode: false #109530

Closed
bbugh opened this issue Oct 27, 2020 · 4 comments
Assignees
Labels
*as-designed Described behavior is as designed typescript Typescript support issues

Comments

@bbugh
Copy link

bbugh commented Oct 27, 2020

  • VSCode Version: 1.50.1
  • OS Version: 10.14.6
  • TypeScript: 4.0.3

Steps to Reproduce:

  1. Set allowUnreachableCode: false in tsconfig
  2. In a TS file, have a syntax error that TypeScript thinks results in unreachable code (see screenshot)
  3. Save a TS file
  4. Watch as valid code is removed, causing a lot of confusion about why everything breaks suddenly

Does this issue occur when all extensions are disabled?: Yes

This has been happening a lot lately but as far as I can remember it's never happened before. We had recently upgraded TypeScript from 3.7.2 to 4.0.3, but as far as I can tell with investigation nothing else about our app configuration or VSCode common settings have changed.

I disabled all extensions, ran all of the linters separately, etc, and my code keeps being removed when I accidentally save with syntax errors.

The TypeScript compiler reports the syntax issues but does not report a 7027 error, the unreachable code error only appears in VS Code.

This does not happen when allowUnreachableCode is true, but we don't want to allow unreachable code.

Example:

Pasted_Image_10_27_20__2_31_PM

If I save this file, the underscore code will be removed.

image

@mjbvz mjbvz self-assigned this Oct 27, 2020
@mjbvz
Copy link
Collaborator

mjbvz commented Oct 27, 2020

Please always share the text of the code along with screenshots

@mjbvz mjbvz added the info-needed Issue requires more information from poster label Oct 27, 2020
@bbugh
Copy link
Author

bbugh commented Oct 27, 2020

Sure. I didn't type it out because that code doesn't matter, it's any unreachable code. I can see how that wasn't clear. Here's a minimally reproducible file. Copy/paste, then save, and the console.log line will disappear.

// some_file.ts
return

console.log("hello")

Type it out, Cmd+S, voila!

Screen-Recording-2020-10-27-at-4

@mjbvz
Copy link
Collaborator

mjbvz commented Oct 27, 2020

This is not the default behavior. It looks like you've configured code actions on save, which means this is the expected behavior

We generally can't know that the code is unreachable because of an expected syntax error vs being truly unreachable (and we also can't assume that code will always be syntax error free and just disable unreachable code checking)

@mjbvz mjbvz closed this as completed Oct 27, 2020
@mjbvz mjbvz added *as-designed Described behavior is as designed typescript Typescript support issues and removed info-needed Issue requires more information from poster labels Oct 27, 2020
@matt-tingen
Copy link

matt-tingen commented Nov 13, 2020

For what it's worth, I also found this behavior to be quite surprising and it took a few instances for me to be sure I wasn't accidentally deleting code myself since the deletion can occur far away from the syntax error where a change has been made.

I didn't even realize TS had fixes to run so I was using source.fixAll on save thinking eslint was the only provider. Changing that to source.fixAll.eslint works for me, but I figured I should share my experience.

As a point of comparison, it's worth mentioning that eslint's no-unreachable does not have an auto-fix and that's on top of eslint bailing out when there's a syntax error.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
*as-designed Described behavior is as designed typescript Typescript support issues
Projects
None yet
Development

No branches or pull requests

3 participants