Rewrite prefer-const#2219
Conversation
|
I don't think we should special case variable declaration in The issue with #2195 actually didn't really have to do with |
|
@nchen63 I also thought about adding an option for multiple variables declared in the same The special handling is only for for (let foo of bar); // still fails
for (let foo in bar); // still fails
for (let foo=bar; someCondition;); // still fails because foo is not reassigned
let i = 0;
for (let len=foo.length; i<len;++i); // still fails because len is not reassigned
// this is the only case that does not fail anymore
// Otherwise you would need to move variable len out of the loop initializer to the parent scope, which could leed to unexpected behaviour if it shadows another variable
for (let i=0, len=foo.length; i<len; ++i); // no fail for len because i is reassignedBtw: eslint also works that way. |
nchen63
left a comment
There was a problem hiding this comment.
none of the options for fixing the broken test seems appealing:
- remove the cases that break pre-ts 2.0
- change infrastructure to detect when not to run a test (make a different set of tests run for pre-ts 2.0)
- somehow hack the logic to handle the unknown/newer ts code (not sure if possible)
src/rules/preferConstRule.ts
Outdated
| properties: { | ||
| destructuring: { | ||
| type: "string", | ||
| enum: [OPTION_DESTRUCTURING_ALL, "any"], |
|
I made some further changes to correctly handle reassigned variables in parameter initializer. |
|
Regarding the failing tests with older versions of typescirpt: #2253 will eventually have the same problem and so will all future rules based on new language features or syntax. |
[enhancement] show warnings for `var`
[bugfix] correctly handle variables shadowed by parameters and catched exceptions
[bugfix] don't warn if a variable in a for loop initializer can be const
[bugfix] handle more cases in destructuring
[bugfix] only fix initialized variables
[new-rule-option] `{destructuring: "all"}` to only warn if all variables in a destructuring can be const
|
The failing tests are now fixed. Should be ready to merge. |
|
@ajafff thanks! |
PR checklist
What changes did you make?
[enhancement] show warnings for
var[bugfix] correctly handle variables shadowed by parameters and catched exceptions
[bugfix] don't warn if one variable in a for loop initializer can not be const
[bugfix] handle more cases in destructuring
[bugfix] only fix initialized variables
[new-rule-option]
{destructuring: "all"}to only warn if all variables in a destructuring can be constIs there anything you'd like reviewers to focus on?
Tests fail with typescript@2.0 because one test contains syntax (spread) which is not supported in that version of the language. In real life this will not result in a problem.
How to handle the ci failure?