KEMBAR78
[#4136] Change ClosingParenthesisIndentation to be relative to parameters by jfelchner · Pull Request #5698 · rubocop/rubocop · GitHub
Skip to content

Conversation

@jfelchner
Copy link
Contributor

@jfelchner jfelchner commented Mar 18, 2018

I think the best approach for this cop is to remove most of the logic and
let it depend on where the parameters are aligned at. I think there should
be only two rules:

If the method is called (or defined) and the arguments/parameters...

  • span more than one line, the closing parenthesis should be
    outdented by one IndentationWidth from the last argument/parameter
  • are all on one line, the closing parenthesis should be aligned
    with the opening parenthesis

This lets the other cops like Layout/FirstParameterIndentation,
Layout/FirstMethodParameterLineBreak,
Layout/FirstMethodArgumentLineBreak,
Layout/MultilineMethodDefinitionBraceLayout,
Layout/MultilineMethodCallBraceLayout and Layout/AlignParameters be
responsible for their jobs and Layout/ClosingParenthesisIndentation would
simply be the result of the end of that flow.

By simplifying the cop, it allows for all of the cases described in #5650,
all of my use cases here, and will keep almost every current
ClosingParenthesisIndentation spec passing.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run rake default or rake parallel. It executes all tests and RuboCop for itself, and generates the documentation.

@jfelchner jfelchner changed the title Change ClosingParenthesisIndentation to be relative to parameters [#4136] Change ClosingParenthesisIndentation to be relative to parameters Mar 18, 2018
@jfelchner
Copy link
Contributor Author

@bbatsov I'll squash these once you've had a chance to review 👍

@jfelchner
Copy link
Contributor Author

@bbatsov Also, this single PR should close all 5 issues listed in my first commit message ;)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this off by 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just making it more explicit that the paren wasn't aligned.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just making it more explicit that the paren wasn't aligned.

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 21, 2018

`If the method is called (or defined) and the arguments/parameters...

  • that span more than one line, the closing parenthesis should be
    outdented by one IndentationWidth from the last argument/parameter
  • that are all on one line, the closing parenthesis should be aligned
    with the opening parenthesis`

Can you provide just two basic code snippets to make sure we're on the same page about this?

Btw, before we merge this we should also update the cop documentation as well.

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 21, 2018

I think the best approach for this cop is to remove most of the logic and
let it depend on where the parameters are aligned at. I think there should
be only two rules:

Looking at the diff I don't see much logic removal. 😄

@jfelchner
Copy link
Contributor Author

jfelchner commented Mar 21, 2018

Looking at the diff I don't see much logic removal. 😄

"It's easy" - Famous Last Words 😉

There's more code, but logic itself is easier to understand IMO:

  • If the arguments are on one line or there's no line break before the first argument and the rest of the arguments are lined up, then the right paren is aligned with the left paren.
  • If there's a line break before the first argument, the right paren is outdented by IndentationWidth spaces.

There's no more looking up what kind of style the parameters are set at and therefore no more dependency on other cops.

@jfelchner
Copy link
Contributor Author

jfelchner commented Mar 21, 2018

I'll be happy to update the documentation. If you want to make sure we're on the same page, the easiest way to do that would be to just look at the tests, but here is the only example of code that Rubocop will change its results for:

Before

some_method(a,
)

After

some_method(a,
           )

Not only do I think this is correct, but keeping the tests passing for this one case would have required more logic that I didn't think was worth the tradeoff.

New code snippets that now work include:

          foo = some_method(
                             a
                           )
          foo = some_method(
                  a
                )

No additional configuration is required to make either of those work, they're based on allowing the parameter cops to put the parameters in the correct place and then placing the ) relative to them.

@bbatsov
Copy link
Collaborator

bbatsov commented Apr 5, 2018

Fair enough. You'll have to rebase this, but the changes seem sound to me. Some doc updates would be appreciated.

@jfelchner
Copy link
Contributor Author

jfelchner commented Apr 22, 2018

@bbatsov requested doc updates are completed. Rebased onto master and should be ready to roll.

I think the best approach for this cop is to remove most of the logic
and let it depend on where the parameters are aligned at.  I think there
should be only two rules:

If the method is called (or defined) and the arguments/parameters...

* that span more than one line, the closing parenthesis should be
  outdented by one `IndentationWidth` from the last argument/parameter
* that are all on one line, the closing parenthesis should be aligned
  with the opening parenthesis

This lets the other cops like `Layout/FirstParameterIndentation`,
`Layout/FirstMethodParameterLineBreak`,
`Layout/FirstMethodArgumentLineBreak`,
`Layout/MultilineMethodDefinitionBraceLayout`,
`Layout/MultilineMethodCallBraceLayout` and `Layout/AlignParameters` be
responsible for their jobs and `Layout/ClosingParenthesisIndentation`
would simply be the result of the end of that flow.

By simplifying the cop, it allows for all of the cases described in #5650,
all of my use cases here, and will keep almost every current
`ClosingParenthesisIndentation` spec passing.

References #4136, #5650, #3204, #3139, #2791
@jfelchner
Copy link
Contributor Author

@bbatsov I referenced all the relevant issue numbers that this issue closes in my first commit message but didn't explicitly close them there in case you had other opinions, so if you agree, that will have to be done manually.

@bbatsov bbatsov merged commit 43fcb4c into rubocop:master Apr 23, 2018
@bbatsov
Copy link
Collaborator

bbatsov commented Apr 23, 2018

👍

@jfelchner jfelchner deleted the feature/closing-parenthesis-indenation-enhancements branch April 23, 2018 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants