-
Notifications
You must be signed in to change notification settings - Fork 29.3k
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
Migrate to tslint 5 and adopt TS noUnusedLocals #37212
Comments
I think for the
|
@egamma good stuff, thanks for the prep work. |
@egamma very useful. Thanks a lot |
@egamma thanks for preparing this. |
This is great! Thanks! 0438b90 |
Closing - no more @ts-ignore! |
Background
We were running with tslint 3.7 which is now over a year old and the latest version of tslint is 5.8.
The migration was blocked since one of the popular tslint rules
no-unused-variables
changed its implementation in tslint 4.0 so that it could no longer be supported by thevscode-tslint
extension. The reason is that this rule now requires type information, this means, it needs a TypeScript project to run which is too costly (https://github.com/Microsoft/vscode-tslint/issues/70#issuecomment-241041929).TypeScript
noUnsuedLocals
TypeScript provides a compiler option
noUnusedLocals
that can replace the tslint rule. The issue we had with this option is that it reports false positives and until recently there was no way to suppress a false positive. This has changed in TypeScript 2.6. You can now suppress a check on the next line using the//@ts-ignore
comment.The
noUsedLocals
option is more powerful than theno-unused-variables
tslint rule. It also recognizes unused properties/instance variables. This helps us to identify injected services that are not used.An important difference between the tslint
no-unused-variables
rule and the TypeScript compiler option is that TypeScript reports unused locals as an error and not as a warning. There is #37616 which adds the settingtypescript.reportStyleChecksAsWarnings
. to control whether style checks should be reported as warnings.Switching to
noUnusedLocals
I've added the
noUnusedLocals
to our top-leveltsconfig.json
and thetsconfig.json
files of the bundled extensions. I've removed theno-unused-locals
rule from thetslint.json
.To make the transition to tslint 5 smooth and to avoid committing code into git with errors, I've added
//@ts-ignore
comments for all thenoUnusedLocals
warnings. I've categorized these warnings as follows using a description in the comments:@ts-ignore @editorAction uses the class
(68 instances) these are false positives that we cannot fix Decorated private propoerty flagged as unused withnoUnusedLocals:true
TypeScript#13120. We will likely remove the use of the @editorAction decorator to avoid polluting our code withts-ignore
comments.@ts-ignore unused injected service
(164) instances) we have many places where a service is injected into a class but the service is not used. We must fix those warnings.@ts-ignore unused local
(11 instances) these are most of the time correct and need to be reviewed and fixed@ts-ignore unused property
(83 instances) some false positives, they need to be reviewed and fixed if possible.@ts-ignore unused type
(23 instances) most of them are false positives but need to be reviewed@ts-ignore unused generic parameter
(14 instances) these are all correct but the type parameter was added for consistency. Nothing we can do about them.Action
As part of the migration tslint 5 you now have to review your code and remove these comments where possible by removing the unused local. There will still be some false positives that need the
//@ts-ignore
comment. If this is the case then we should describe the reason in the comment and file an issue against TypeScript.To review the comments:
@ts-ignore
in the code.To check whether you have removed the warnings you can run the
Build VS Code
task. This task has a problem matcher so that the TypeScript errors show up in the Problems panel. This requires that the locally installed TS version is 2.6 and picked up bygulp-tsb
(we have switched to TS 2.6 recently and you should run an npm install).Checklist
Reviewed the "no unused locals" errors:
The text was updated successfully, but these errors were encountered: