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
Update: Add always-multiline option to multiline-ternary (fixes #8770) #8841
Conversation
LGTM |
@nwoltman, thanks for your PR! By analyzing the history of the files in this pull request, we identified @kaicataldo and @mysticatea to be potential reviewers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for contributing to ESLint!
lib/rules/multiline-ternary.js
Outdated
@@ -69,6 +71,10 @@ module.exports = { | |||
reportError(node.consequent, node, false); | |||
} | |||
} else { | |||
if (allowSingleLine && astUtils.isTokenOnSameLine(node, node)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha, interesting use of astUtils.isTokenOnSameLine()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not to ruin the fun, but I feel like this makes the code a bit hard to read -- semantically a node would always be on the same line as itself, and this only works due to an implementation detail of astUtils.isTokenOnSameLine
where it assumed that the two tokens would be different. Personally I think it would be better to be explicit here instead:
if (allowSingleLine && node.loc.start.line === node.loc.end.line) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I agree that is more readable. Fixed 👍
LGTM |
Thanks for contributing to ESLint! 🎉 |
What is the purpose of this pull request? (put an "X" next to item)
[x] Changes an existing rule
See #8770
What changes did you make? (Give an overview)
This adds an
always-multiline
option tomultiline-ternary
. This option enforces newlines between the operands of a ternary expression only if the expression spans multiple lines.Is there anything you'd like reviewers to focus on?
Nope.
However, I would like out point out that with the addition of this option, the name of this rule could be changed to
ternary-newline
to follow the naming convention of similar ESLint rules.