-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Fix linter settings #7584
Comments
Oh, the page in the ESLint's docs also says: "Disable this rule if the code is clearer without it" :) |
Is there any particular code example you couldn’t use because of this rule? By the way. You can always disable this rule per a bunch of lines or per file with a code comment. We do it from time to time when it makes sense. |
I would leave this setting on in general and disable it for the times it does make sense, as @gziolo said. In general this kind of nesting looks like a code smell and is likely a sign things could be clearer/refactored. I think conditionally disabling it is better than disabling it all the time. |
Yep, trying to make this bit: 287f623#diff-59b3e108ef76b1269ef0cecf2b417214R348 more readable here: 078b3db#diff-59b3e108ef76b1269ef0cecf2b417214R352.
Right, was just about to do that but then thought the rule doesn't really make much sense. I mean, who writes conditionals like:
That seems to be why the rule exists, but imho in cases with several
Sure, that works too, although don't think we need it in the first place :) |
Huh, in your example it might be easier to have less nesting I think. What about: if ( align === 'center' ) {
// When the image is centered, show both handles.
showRightHandle = true;
showLeftHandle = true;
} else if ( isRTL ) {
// In RTL mode the image is on the right by default.
// Show the right handle and hide the left handle only when it is aligned left.
// Otherwise always show the left handle.
if ( align === 'left' ) {
showRightHandle = true;
} else {
showLeftHandle = true;
}
} else if ( align === 'right' ) {
// Show the left handle and hide the right handle only when the image is aligned right.
showLeftHandle = true;
} else {
// Otherwise show the right handle.
showRightHandle = true;
} But actually I don't like that the conditional varies… I think the eslint rule is working here to lessen the indentation/nesting of the code. You are essentially doing a switch case on align here… I think your example would be better expressed as: switch ( align ) {
case 'center':
// When the image is centered, show both handles.
showLeftHandle = true;
showRightHandle = true;
break;
case 'left':
// In RTL mode the image is on the right by default.
// Show the right handle (and hide the left handle) when it is aligned left.
if ( isRTL ) {
showRightHandle = true;
} else {
// Show the left handle in LTR, for left-aligned images.
showLeftHandle = true;
}
break;
case 'right':
default:
// Show the left handle (and hide the right handle) when the image is aligned right in RTL.
// If no align value is set we show the handle on the right side as well.
if ( isRTL ) {
showLeftHandle = true;
} else {
// Show the right handle for right-aligned images in LTR.
showRightHandle = true;
}
break;
} There's a lot of nesting here and I think keeping which conditional we're checking consistent makes the code easier to follow. If you're comfortable with just leaving it on in general for now I think it's worth it–I know it would stop me from writing badly-nested code and refactoring. If it doesn't apply in this case and you're happy to ignore the rule and explain why (a link to this issue would be nice in the comments) I'll close this for now 😄 |
Yep, that's exactly what I'm trying to avoid. There are two "sets" of conditions, one is The Thinking to keep the exception here. The |
* In progress * Fix resizing of images by dragging * Use the editor settings to get RTL state. Clean up a bit. * Make it more readable, add inline comments, fix linter errors * Add ESLint exception to 'no-lonely-if', see #7584
Currently ESLint throws error on:
This is coming from the
no-lonely-if
setting: https://eslint.org/docs/rules/no-lonely-if which says that you should never have anif
inside anelse
. That may be true in some cases, but in the above example makes the nesting irregular which makes the whole thing harder to read/understand.Think this rule should be disabled. It is not in the WP coding standards for a good reason :)
The text was updated successfully, but these errors were encountered: