Skip to content

Commit

Permalink
Merge pull request #6643 from dazuma/camel-case-parens
Browse files Browse the repository at this point in the history
Add AllowParenthesesInCamelCaseMethod option
  • Loading branch information
Drenmi committed Jan 22, 2019
2 parents 303cada + eaab0f8 commit 3e11526
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### New features

* [#6643](https://github.com/rubocop-hq/rubocop/pull/6643): Support `AllowParenthesesInCamelCaseMethod` option on `Style/MethodCallWithArgsParentheses` `omit_parentheses`. ([@dazuma][])

## 0.63.1 (2019-01-22)

### Bug fixes
Expand Down Expand Up @@ -3771,3 +3775,4 @@
[@roooodcastro]: https://github.com/roooodcastro
[@rmm5t]: https://github.com/rmm5t
[@marcotc]: https://github.com/marcotc
[@dazuma]: https://github.com/dazuma
32 changes: 28 additions & 4 deletions lib/rubocop/cop/style/method_call_with_args_parentheses.rb
Expand Up @@ -10,7 +10,7 @@ module Style
# Additional methods can be added to the `IgnoredMethods` list. This
# option is valid only in the default style.
#
# In the alternative style (omit_parentheses), there are two additional
# In the alternative style (omit_parentheses), there are three additional
# options.
#
# 1. `AllowParenthesesInChaining` is `false` by default. Setting it to
Expand All @@ -21,6 +21,12 @@ module Style
# to `true` allows the presence of parentheses in multi-line method
# calls.
#
# 3. `AllowParenthesesInCamelCaseMethod` is `false` by default. This
# allows the presence of parentheses when calling a method whose name
# begins with a capital letter and which has no arguments. Setting it
# to `true` allows the presence of parentheses in such a method call
# even with arguments.
#
# @example EnforcedStyle: require_parentheses (default)
#
#
Expand Down Expand Up @@ -106,6 +112,22 @@ module Style
#
# # good
# foo().bar 1
#
# # AllowParenthesesInCamelCaseMethod: false (default)
#
# # bad
# Array(1)
#
# # good
# Array 1
#
# # AllowParenthesesInCamelCaseMethod: true
#
# # good
# Array(1)
#
# # good
# Array 1
class MethodCallWithArgsParentheses < Cop
include ConfigurableEnforcedStyle
include IgnoredMethods
Expand Down Expand Up @@ -155,7 +177,7 @@ def add_offense_for_omit_parentheses(node)
return unless node.parenthesized?
return if node.implicit_call?
return if super_call_without_arguments?(node)
return if camel_case_method_call_without_arguments?(node)
return if allowed_camel_case_method_call?(node)
return if legitimate_call_with_parentheses?(node)

add_offense(node, location: node.loc.begin.join(node.loc.end))
Expand Down Expand Up @@ -221,8 +243,10 @@ def super_call_without_arguments?(node)
node.super_type? && node.arguments.none?
end

def camel_case_method_call_without_arguments?(node)
node.camel_case_method? && node.arguments.none?
def allowed_camel_case_method_call?(node)
node.camel_case_method? &&
(node.arguments.none? ||
cop_config['AllowParenthesesInCamelCaseMethod'])
end

def legitimate_call_with_parentheses?(node)
Expand Down
24 changes: 23 additions & 1 deletion manual/cops_style.md
Expand Up @@ -2771,7 +2771,7 @@ In the default style (require_parentheses), macro methods are ignored.
Additional methods can be added to the `IgnoredMethods` list. This
option is valid only in the default style.

In the alternative style (omit_parentheses), there are two additional
In the alternative style (omit_parentheses), there are three additional
options.

1. `AllowParenthesesInChaining` is `false` by default. Setting it to
Expand All @@ -2782,6 +2782,12 @@ options.
to `true` allows the presence of parentheses in multi-line method
calls.

3. `AllowParenthesesInCamelCaseMethod` is `false` by default. This
allows the presence of parentheses when calling a method whose name
begins with a capital letter and which has no arguments. Setting it
to `true` allows the presence of parentheses in such a method call
even with arguments.

### Examples

#### EnforcedStyle: require_parentheses (default)
Expand Down Expand Up @@ -2870,6 +2876,22 @@ foo().bar(1)
# good
foo().bar 1
# AllowParenthesesInCamelCaseMethod: false (default)
# bad
Array(1)
# good
Array 1
# AllowParenthesesInCamelCaseMethod: true
# good
Array(1)
# good
Array 1
```

### Configurable attributes
Expand Down
30 changes: 30 additions & 0 deletions spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb
Expand Up @@ -374,6 +374,13 @@ def foo
RUBY
end

it 'register an offense for camel-case methods with arguments' do
expect_offense(<<-RUBY.strip_indent)
Array(:arg)
^^^^^^ Omit parentheses for method calls with arguments.
RUBY
end

it 'accepts no parens in method call without args' do
expect_no_offenses('top.test')
end
Expand Down Expand Up @@ -558,6 +565,16 @@ def seatle_style arg = default(42)
RUBY
end

it 'auto-corrects camel-case methods with arguments' do
original = <<-RUBY.strip_indent
Array(:arg)
RUBY

expect(autocorrect_source(original)).to eq(<<-RUBY.strip_indent)
Array :arg
RUBY
end

context 'TargetRubyVersion >= 2.3', :ruby23 do
it 'accepts parens in chaining with safe operators' do
expect_no_offenses('Something.find(criteria: given)&.field')
Expand Down Expand Up @@ -639,5 +656,18 @@ def seatle_style arg = default(42)
expect(autocorrect_source(original)).to eq(original)
end
end

context 'allowing parens in camel-case methods' do
let(:cop_config) do
{
'EnforcedStyle' => 'omit_parentheses',
'AllowParenthesesInCamelCaseMethod' => true
}
end

it 'accepts parens for camel-case method names' do
expect_no_offenses('Array(nil)')
end
end
end
end

0 comments on commit 3e11526

Please sign in to comment.