8000 Add new Style/NonLocalControlFlowInTernary cop by Drenmi · Pull Request #7406 · rubocop/rubocop · GitHub
[go: up one dir, main page]

Skip to content

Add new Style/NonLocalControlFlowInTernary cop #7406

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

Conversation

Drenmi
Copy link
Collaborator
@Drenmi Drenmi commented Oct 5, 2019

This cop checks for non-local control flow, i.e. return, break, next, raise, etc. used in ternary expressions.


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 bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@bbatsov
Copy link
Collaborator
bbatsov commented Oct 5, 2019

This will also need an entry in the style guide.

@rrosenblum
Copy link
Contributor

What if control flow exists on both sides of the ternary? Should it still be flagged? foo? ? next 1 : next 2 Or foo? ? break : next? Or foo? ? raise 'Error 1' : raise 'Error 2'?

@Drenmi
Copy link
Collaborator Author
Drenmi commented Oct 8, 2019

This will also need an entry in the style guide.

👍 Will add when I have some time.

What if control flow exists on both sides of the ternary? Should it still be flagged?

Yes. I should add a test case for that. 🙂

@bbatsov
Copy link
Collaborator
bbatsov commented Dec 30, 2019

@Drenmi ping :-)

@Drenmi
Copy link
Collaborator Author
Drenmi commented Dec 31, 2019

@bbatsov I have thought about it, and I think it's worth generalizing beyond just ternary expressions.

I opened a PR to The Ruby Style Guide here.

@koic
Copy link
Member
koic commented Dec 31, 2019

It may not be a problem when limited to ternary operator.
On the other hand, I commented on the Style Guide about cases that I found difficult to generalize.
rubocop/ruby-style-guide#785 (comment)

@Drenmi Drenmi force-pushed the feature/non-local-control-flow-in-ternary-cop branch from 618ef96 to c0a54e8 Compare March 28, 2020 14:45
@Drenmi
Copy link
Collaborator Author
Drenmi commented Mar 28, 2020

@koic @bbatsov I have rebased this and added the spec that @rrosenblum suggested. I think we can merge this. I suspect the Style Guide PR will take some time to iron out. 🙂

Style/NonLocalControlFlowInTernary:
Description: 'checks for non-local control flow in ternary expressions.'
Enabled: true
VersionAdded: '0.76'
Copy link
Member

Choose a reason for hiding this comment

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

It looks like the version needs to be updated.

Suggested change
VersionAdded: '0.76'
VersionAdded: '0.81'

@@ -0,0 +1,46 @@
# frozen_string_literal: true

# TODO: when finished, run `rake generate_cops_documentation` to update the docs
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove the auto-generated line?

@bbatsov
Copy link
Collaborator
bbatsov commented Nov 4, 2020

I'm closing this PR due to no recent activity. Feel free to re-open it if you ever come back to it.

@bbatsov bbatsov closed this Nov 4, 2020
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
0