I have a coding standard that suggests that the initial argument to a ternary should always be inside parenthesis, regardless of the expression.
E.g. foo = (thing.baz?) ? [] : thing.bar
The following should be considered an infraction:
E.g. foo = thing.baz? ? [] : thing.bar
Is it possible to achieve this with Rubocop's built-in Cops, or does this require a custom Cop. If so, how would I implement it?
I saw your question, so I went ahead and implemented the cop for you. The name is Style/TernaryParentheses
, and the EnforcedStyle
option you want is require_parentheses
(not the default.)
# .rubocop.yml
Style/TernaryParentheses:
Enabled: true
EnforcedStyle: require_parentheses
You can start using it right now, by putting this in your Gemfile
:
gem 'rubocop', git: 'git://github.com/bbatsov/rubocop.git'
or you can wait for the 0.42.0
release.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With