Skip to content
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 #4656] Fix Style/ConditionalAssignment auto-correction #4682

Merged

Conversation

akhramov
Copy link

@akhramov akhramov commented Aug 27, 2017

Style/ConditionalAssignment cop's autocorrection results in invalid
syntax when it encounters assignment to unbracketed array.

This pull request modifies ConditionalAssignment cop's autocorrection so, it
wraps array in brackets, if needed.
This issue is very simillar to #3170, where Style/MutableConstant
wasn't able to properly handle assignment to unbracketed array.
Since both MutableConstant and ConditionalAssignment cops need to
check whether array node is bracketed or not, new method
Rubocop::AST::ArrayNode#bracketed? was introduced, to avoid
duplication.


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).
  • Used the same coding conventions as the rest of the project.
  • 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.
  • All tests(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

1
else
[2, 5, 6]
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess this happens because of interference from Lint/EndAlignment?

Copy link
Author

Choose a reason for hiding this comment

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

Actually, Style/ConditionalAssignment realigns end keyword by itself. Please see #2719 / #2800 for details.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That looks extremely strange.

Copy link
Author

Choose a reason for hiding this comment

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

@bbatsov

Agreed. Would it be better to have an "explanatory" variable? i.e.

indent = ' ' * 'bar = '.length
expect(new_source).to eq(<<-RUBY.strip_indent)
  bar = if foo
    1
  else
    [2, 5, 6]
  #{indent}end
RUBY

If so, I'd correct the other case, from which I've derived the style.

Copy link
Collaborator

@Drenmi Drenmi Sep 1, 2017

Choose a reason for hiding this comment

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

Is it possible to configure (from within the test suite) the auto-correct to produce a more ... reasonable style in the tests?

Either:

bar = if foo
        1
      else
        [2, 3]
      end

or:

bar = if foo
  1
else
  [2, 3]
end

Copy link
Author

Choose a reason for hiding this comment

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

@Drenmi, thanks for guidance! Fixed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well done, @akhramov! Looks much better now. 🙂

@Drenmi
Copy link
Collaborator

Drenmi commented Aug 27, 2017

Nice! Hadn't crossed my mind that parallel assignment is parsed as an array node. 🙂

@akhramov akhramov force-pushed the fix/#4656-conditional-assignment-fix branch from 8cc7413 to 9fe2db7 Compare September 1, 2017 11:46
`Style/ConditionalAssignment` cop's autocorrection results in invalid
syntax when it encounters assignment to unbracketed array.

This commit modifies ConditionalAssignment cop's autocorrection so, it
wraps array in brackets, if needed.
This issue is very simillar to rubocop#3170, where `Style/MutableConstant`
wasn't able to properly handle assignment to unbracketed array.
Since both `MutableConstant` and `ConditionalAssignment` cops need to
check whether array node is bracketed or not, new method
`Rubocop::AST::ArrayNode#bracketed?` was introduced, to avoid
duplication.
@akhramov akhramov force-pushed the fix/#4656-conditional-assignment-fix branch from 9fe2db7 to 28c1345 Compare September 1, 2017 11:51
@Drenmi
Copy link
Collaborator

Drenmi commented Sep 4, 2017

With the fix to the test configuration, I'm good with this @bbatsov. 🙂

@bbatsov bbatsov merged commit f66e466 into rubocop:master Sep 4, 2017
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.

4 participants