-
Notifications
You must be signed in to change notification settings - Fork 2
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
Replace inappropriate uses of let
with const
#973
Comments
Lint can catch this:
And lint can auto-fix this. There are currently 564 occurrences of this across our codebase. |
I'm going to go through and try to hit a bunch of these that are my fault. |
As I'm going through this I'm finding that there is one issue with the lint rule above. There is one case that requires either ignoring the lint rule, or refactoring.
This will be triggered by the prefer-const rule because the value is not reassigned. Unfortunately the following is illegal:
So either we need to disable the rule, or code around it. I'll be committing shortly, so far we are down around 300 lint errors. |
Declaring a variable without initializing its value is a code smell. Both of the examples in the preceding comment have that smell. In both of the examples, simply wait to declare until you assign. |
Scratch that I'm going to do all of them. |
I agree except that sometimes it is nice to declare in a larger scope, and then initialized in a loop or something. |
Signed-off-by: Chris Malley <[email protected]>
Then use Example from GQCheckBox: let content = null;
if ( options.icon ) {
content = new HBox( {
align: 'center',
spacing: 8,
children: [ textNode, options.icon ]
} );
}
else {
content = textNode;
} If you really want to be bulletproof, |
Sounds good to me, thanks. |
I committed all of the fixes for this, and also the lint rule. We can tweak the rule if we want to, but I figured that it was best to just get it out there, since there were a lot of places it was wrong. Marking for dev meeting as a PSA that this is a rule, and to discuss if there are questions or concerns with it. |
I'm not sure we need this. I think it is good for devs to manually have to write the code in the way we decide is best. |
I agree we should not have auto-fix on by default. If anything, autofix should be considered for initial batches, not ongoing development. |
Lint rule has been created by @zepumph. Devs have been made aware. |
(1) Replace inappropriate uses of
let
withconst
. Uselet
only for things that will be modified.(2) Investigate whether we need another lint rule.
Slack discussion
Chris Malley [11:53 AM]
Hey devs, I’m looking through recent common code changes, and I suspect that some devs are confused about when to use
const
vslet
. Useconst
for things that will not change (constants), uselet
for things that need to change. Looking only in scenery-phet, I see improper uses oflet
in: Utterance, BorderAlertsDescriber, HeaterCooler*, DirectionEnum, MovementDescriber, A11yGrabDragNode. (edited)Example in BorderAlertsDescriber:
These are all constants, should be
const
.This is the whole point of moving away from
var
, and usingconst
andlet
instead.Michael Kauzmann [11:58 AM]
Hey all those files are mine. Will you make me an issue and I'll take a look.
Chris Malley [11:58 AM]
Not all yours, but I will make an issue.
Denzell Barnett [11:58 AM]
Assign me as well for HeaterCooler*
Chris Malley [11:59 AM]
I’m going to put the issue in tasks repo, since this looks like it cuts across all repos.
Sam Reid [12:00 PM]
Can a lint rule catch lets that should be const?
The text was updated successfully, but these errors were encountered: