rubocop 0.15.0 → 0.16.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -10
- data/.travis.yml +2 -1
- data/CHANGELOG.md +55 -0
- data/Gemfile +3 -0
- data/README.md +37 -0
- data/config/default.yml +99 -16
- data/config/enabled.yml +28 -16
- data/lib/rubocop.rb +16 -2
- data/lib/rubocop/cli.rb +10 -91
- data/lib/rubocop/config.rb +4 -1
- data/lib/rubocop/config_loader.rb +18 -10
- data/lib/rubocop/config_store.rb +3 -2
- data/lib/rubocop/cop/check_assignment.rb +43 -0
- data/lib/rubocop/cop/check_methods.rb +18 -0
- data/lib/rubocop/cop/commissioner.rb +15 -3
- data/lib/rubocop/cop/cop.rb +51 -43
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -1
- data/lib/rubocop/cop/lint/block_alignment.rb +7 -7
- data/lib/rubocop/cop/lint/debugger.rb +1 -1
- data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
- data/lib/rubocop/cop/lint/end_alignment.rb +56 -19
- data/lib/rubocop/cop/lint/end_in_method.rb +4 -10
- data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
- data/lib/rubocop/cop/lint/eval.rb +1 -1
- data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_condition.rb +2 -2
- data/lib/rubocop/cop/lint/loop.rb +1 -1
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
- data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
- data/lib/rubocop/cop/lint/syntax.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
- data/lib/rubocop/cop/lint/useless_assignment.rb +2 -2
- data/lib/rubocop/cop/lint/useless_comparison.rb +1 -1
- data/lib/rubocop/cop/lint/useless_setter_call.rb +6 -16
- data/lib/rubocop/cop/lint/void.rb +5 -5
- data/lib/rubocop/cop/rails/default_scope.rb +33 -0
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +2 -4
- data/lib/rubocop/cop/rails/output.rb +1 -1
- data/lib/rubocop/cop/rails/read_attribute.rb +1 -1
- data/lib/rubocop/cop/rails/validation.rb +1 -1
- data/lib/rubocop/cop/style/access_modifier_indentation.rb +31 -16
- data/lib/rubocop/cop/style/accessor_method_name.rb +53 -0
- data/lib/rubocop/cop/style/alias.rb +14 -2
- data/lib/rubocop/cop/style/align_hash.rb +15 -16
- data/lib/rubocop/cop/style/and_or.rb +3 -3
- data/lib/rubocop/cop/style/array_syntax.rb +22 -0
- data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
- data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -3
- data/lib/rubocop/cop/style/attr.rb +1 -1
- data/lib/rubocop/cop/style/autocorrect_alignment.rb +13 -12
- data/lib/rubocop/cop/style/begin_block.rb +1 -1
- data/lib/rubocop/cop/style/block_comments.rb +1 -1
- data/lib/rubocop/cop/style/block_nesting.rb +5 -1
- data/lib/rubocop/cop/style/blocks.rb +4 -4
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +48 -22
- data/lib/rubocop/cop/style/case_equality.rb +1 -1
- data/lib/rubocop/cop/style/case_indentation.rb +25 -6
- data/lib/rubocop/cop/style/class_and_module_camel_case.rb +1 -1
- data/lib/rubocop/cop/style/class_length.rb +7 -19
- data/lib/rubocop/cop/style/class_methods.rb +1 -1
- data/lib/rubocop/cop/style/class_vars.rb +1 -1
- data/lib/rubocop/cop/style/code_length.rb +35 -0
- data/lib/rubocop/cop/style/collection_methods.rb +1 -1
- data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
- data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
- data/lib/rubocop/cop/style/configurable_enforced_style.rb +51 -0
- data/lib/rubocop/cop/style/configurable_max.rb +17 -0
- data/lib/rubocop/cop/style/configurable_naming.rb +4 -10
- data/lib/rubocop/cop/style/constant_name.rb +5 -3
- data/lib/rubocop/cop/style/cyclomatic_complexity.rb +8 -13
- data/lib/rubocop/cop/style/def_parentheses.rb +4 -43
- data/lib/rubocop/cop/style/documentation.rb +1 -1
- data/lib/rubocop/cop/style/dot_position.rb +6 -2
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -1
- data/lib/rubocop/cop/style/empty_lines.rb +5 -1
- data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +1 -1
- data/lib/rubocop/cop/style/empty_lines_around_body.rb +28 -13
- data/lib/rubocop/cop/style/empty_literal.rb +3 -3
- data/lib/rubocop/cop/style/encoding.rb +5 -5
- data/lib/rubocop/cop/style/end_block.rb +1 -1
- data/lib/rubocop/cop/style/end_of_line.rb +8 -6
- data/lib/rubocop/cop/style/even_odd.rb +6 -6
- data/lib/rubocop/cop/style/favor_join.rb +1 -1
- data/lib/rubocop/cop/style/favor_modifier.rb +20 -34
- data/lib/rubocop/cop/style/favor_sprintf.rb +1 -1
- data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +1 -1
- data/lib/rubocop/cop/style/final_newline.rb +1 -1
- data/lib/rubocop/cop/style/flip_flop.rb +20 -0
- data/lib/rubocop/cop/style/for.rb +34 -3
- data/lib/rubocop/cop/style/global_vars.rb +1 -1
- data/lib/rubocop/cop/style/hash_methods.rb +3 -3
- data/lib/rubocop/cop/style/hash_syntax.rb +8 -11
- data/lib/rubocop/cop/style/if_node.rb +25 -0
- data/lib/rubocop/cop/style/if_then_else.rb +1 -1
- data/lib/rubocop/cop/style/indentation_width.rb +77 -43
- data/lib/rubocop/cop/style/lambda.rb +2 -2
- data/lib/rubocop/cop/style/lambda_call.rb +4 -12
- data/lib/rubocop/cop/style/leading_comment_space.rb +1 -1
- data/lib/rubocop/cop/style/line_length.rb +9 -5
- data/lib/rubocop/cop/style/method_call_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +1 -1
- data/lib/rubocop/cop/style/method_def_parentheses.rb +52 -0
- data/lib/rubocop/cop/style/method_length.rb +9 -22
- data/lib/rubocop/cop/style/module_function.rb +1 -1
- data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
- data/lib/rubocop/cop/style/multiline_if_then.rb +11 -1
- data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
- data/lib/rubocop/cop/style/not.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literals.rb +2 -2
- data/lib/rubocop/cop/style/op_method.rb +2 -2
- data/lib/rubocop/cop/style/parameter_lists.rb +2 -2
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -1
- data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
- data/lib/rubocop/cop/style/predicate_name.rb +54 -0
- data/lib/rubocop/cop/style/proc.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +4 -10
- data/lib/rubocop/cop/style/redundant_begin.rb +5 -15
- data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
- data/lib/rubocop/cop/style/redundant_return.rb +9 -19
- data/lib/rubocop/cop/style/redundant_self.rb +8 -3
- data/lib/rubocop/cop/style/regexp_literal.rb +4 -3
- data/lib/rubocop/cop/style/rescue_modifier.rb +9 -17
- data/lib/rubocop/cop/style/semicolon.rb +20 -22
- data/lib/rubocop/cop/style/signal_exception.rb +10 -17
- data/lib/rubocop/cop/style/single_line_block_params.rb +62 -0
- data/lib/rubocop/cop/style/single_line_methods.rb +5 -15
- data/lib/rubocop/cop/style/space_after_comma_etc.rb +1 -3
- data/lib/rubocop/cop/style/space_after_control_keyword.rb +1 -1
- data/lib/rubocop/cop/style/space_after_method_name.rb +4 -12
- data/lib/rubocop/cop/style/space_after_not.rb +1 -1
- data/lib/rubocop/cop/style/space_around_block_braces.rb +105 -94
- data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +1 -1
- data/lib/rubocop/cop/style/space_around_operators.rb +38 -123
- data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +1 -1
- data/lib/rubocop/cop/style/space_inside.rb +1 -3
- data/lib/rubocop/cop/style/space_inside_brackets.rb +0 -2
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +5 -5
- data/lib/rubocop/cop/style/space_inside_parens.rb +0 -2
- data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
- data/lib/rubocop/cop/style/string_help.rb +1 -1
- data/lib/rubocop/cop/style/string_literals.rb +4 -11
- data/lib/rubocop/cop/style/surrounding_space.rb +0 -2
- data/lib/rubocop/cop/style/symbol_array.rb +3 -10
- data/lib/rubocop/cop/style/tab.rb +5 -5
- data/lib/rubocop/cop/style/ternary_operator.rb +2 -2
- data/lib/rubocop/cop/style/trailing_blank_lines.rb +1 -1
- data/lib/rubocop/cop/style/trailing_whitespace.rb +1 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +4 -14
- data/lib/rubocop/cop/style/unless_else.rb +1 -1
- data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
- data/lib/rubocop/cop/style/when_then.rb +3 -1
- data/lib/rubocop/cop/style/while_until_do.rb +2 -2
- data/lib/rubocop/cop/style/word_array.rb +4 -10
- data/lib/rubocop/cop/util.rb +85 -12
- data/lib/rubocop/cop/variable_inspector.rb +4 -2
- data/lib/rubocop/file_inspector.rb +98 -0
- data/lib/rubocop/formatter/base_formatter.rb +2 -2
- data/lib/rubocop/formatter/disabled_config_formatter.rb +10 -2
- data/lib/rubocop/rake_task.rb +5 -1
- data/lib/rubocop/target_finder.rb +3 -3
- data/lib/rubocop/version.rb +1 -1
- data/rubocop-todo.yml +12 -0
- data/rubocop.gemspec +1 -1
- data/spec/isolated_environment_spec.rb +24 -0
- data/spec/rubocop/cli_spec.rb +120 -27
- data/spec/rubocop/config_loader_spec.rb +25 -11
- data/spec/rubocop/config_spec.rb +25 -9
- data/spec/rubocop/config_store_spec.rb +1 -1
- data/spec/rubocop/cop/cop_spec.rb +22 -3
- data/spec/rubocop/cop/lint/block_alignment_spec.rb +4 -4
- data/spec/rubocop/cop/lint/end_alignment_spec.rb +79 -47
- data/spec/rubocop/cop/lint/syntax_spec.rb +4 -3
- data/spec/rubocop/cop/rails/default_scope_spec.rb +31 -0
- data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +37 -2
- data/spec/rubocop/cop/style/accessor_method_name_spec.rb +81 -0
- data/spec/rubocop/cop/style/alias_spec.rb +10 -0
- data/spec/rubocop/cop/style/align_parameters_spec.rb +3 -3
- data/spec/rubocop/cop/style/block_nesting_spec.rb +4 -1
- data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +83 -0
- data/spec/rubocop/cop/style/case_indentation_spec.rb +227 -71
- data/spec/rubocop/cop/style/class_length_spec.rb +2 -0
- data/spec/rubocop/cop/style/constant_name_spec.rb +6 -0
- data/spec/rubocop/cop/style/cyclomatic_complexity_spec.rb +1 -0
- data/spec/rubocop/cop/style/documentation_spec.rb +12 -0
- data/spec/rubocop/cop/style/dot_position_spec.rb +10 -0
- data/spec/rubocop/cop/style/empty_lines_around_body_spec.rb +22 -0
- data/spec/rubocop/cop/style/empty_lines_spec.rb +6 -0
- data/spec/rubocop/cop/style/end_of_line_spec.rb +17 -0
- data/spec/rubocop/cop/style/favor_modifier_spec.rb +15 -0
- data/spec/rubocop/cop/style/flip_flop_spec.rb +23 -0
- data/spec/rubocop/cop/style/for_spec.rb +96 -18
- data/spec/rubocop/cop/style/hash_syntax_spec.rb +8 -2
- data/spec/rubocop/cop/style/indentation_width_spec.rb +185 -20
- data/spec/rubocop/cop/style/line_length_spec.rb +1 -0
- data/spec/rubocop/cop/style/method_def_parentheses_spec.rb +72 -0
- data/spec/rubocop/cop/style/multiline_if_then_spec.rb +16 -0
- data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +34 -3
- data/spec/rubocop/cop/style/predicate_name_spec.rb +25 -0
- data/spec/rubocop/cop/style/redundant_self_spec.rb +15 -0
- data/spec/rubocop/cop/style/regexp_literal_spec.rb +5 -0
- data/spec/rubocop/cop/style/semicolon_spec.rb +16 -16
- data/spec/rubocop/cop/style/{reduce_arguments_spec.rb → single_line_block_params_spec.rb} +12 -4
- data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +58 -8
- data/spec/rubocop/cop/style/space_around_operators_spec.rb +71 -0
- data/spec/rubocop/cop/style/string_literals_spec.rb +18 -0
- data/spec/rubocop/file_inspector_spec.rb +78 -0
- data/spec/rubocop/formatter/clang_style_formatter_spec.rb +7 -7
- data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +3 -3
- data/spec/rubocop/formatter/file_list_formatter_spec.rb +2 -2
- data/spec/rubocop/processed_source_spec.rb +7 -3
- data/spec/rubocop/token_spec.rb +1 -1
- data/spec/spec_helper.rb +3 -2
- data/spec/support/isolated_environment.rb +7 -0
- data/spec/support/shared_context.rb +5 -1
- metadata +35 -12
- data/lib/rubocop/cop/style/reduce_arguments.rb +0 -34
- data/lib/rubocop/cop/style/symbol_name.rb +0 -45
- data/spec/rubocop/cop/style/def_without_parentheses_spec.rb +0 -33
- data/spec/rubocop/cop/style/symbol_name_spec.rb +0 -138
@@ -3,21 +3,40 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# This cop checks
|
7
|
-
# are indented
|
6
|
+
# This cop checks how the *when*s of a *case* expression
|
7
|
+
# are indented in relation to its *case* or *end* keyword.
|
8
8
|
#
|
9
9
|
# It will register a separate offence for each misaligned *when*.
|
10
10
|
class CaseIndentation < Cop
|
11
|
-
MSG = 'Indent when as deep as case.'
|
12
|
-
|
13
11
|
def on_case(case_node)
|
14
12
|
_condition, *whens, _else = *case_node
|
15
13
|
|
16
|
-
|
14
|
+
base, indent = cop_config.values_at('IndentWhenRelativeTo',
|
15
|
+
'IndentOneStep')
|
16
|
+
base_column = base_column(case_node, base)
|
17
17
|
|
18
18
|
whens.each do |when_node|
|
19
19
|
pos = when_node.loc.keyword
|
20
|
-
|
20
|
+
expected_column = base_column +
|
21
|
+
(indent ? IndentationWidth::CORRECT_INDENTATION : 0)
|
22
|
+
if pos.column != expected_column
|
23
|
+
msg = 'Indent when ' + if indent
|
24
|
+
"one step more than #{base}."
|
25
|
+
else
|
26
|
+
"as deep as #{base}."
|
27
|
+
end
|
28
|
+
add_offence(when_node, pos, msg)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def base_column(case_node, base)
|
36
|
+
case base
|
37
|
+
when 'case' then case_node.location.keyword.column
|
38
|
+
when 'end' then case_node.location.end.column
|
39
|
+
else fail "Unknown IndentWhenRelativeTo: #{base}"
|
21
40
|
end
|
22
41
|
end
|
23
42
|
end
|
@@ -7,41 +7,29 @@ module Rubocop
|
|
7
7
|
# Comment lines can optionally be ignored.
|
8
8
|
# The maximum allowed length is configurable.
|
9
9
|
class ClassLength < Cop
|
10
|
-
include
|
11
|
-
|
12
|
-
MSG = 'Class definition is too long. [%d/%d]'
|
10
|
+
include CodeLength
|
13
11
|
|
14
12
|
def on_class(node)
|
15
13
|
check(node)
|
16
14
|
end
|
17
15
|
|
18
|
-
|
19
|
-
cop_config['Max']
|
20
|
-
end
|
16
|
+
private
|
21
17
|
|
22
|
-
def
|
23
|
-
|
18
|
+
def message
|
19
|
+
'Class definition is too long. [%d/%d]'
|
24
20
|
end
|
25
21
|
|
26
|
-
|
27
|
-
|
28
|
-
def check(node)
|
22
|
+
def code_length(node)
|
29
23
|
class_body_line_numbers = line_range(node).to_a[1...-1]
|
30
24
|
|
31
25
|
target_line_numbers = class_body_line_numbers -
|
32
26
|
line_numbers_of_inner_classes(node)
|
33
27
|
|
34
|
-
|
28
|
+
target_line_numbers.reduce(0) do |length, line_number|
|
35
29
|
source_line = processed_source[line_number]
|
36
|
-
next length if source_line
|
37
|
-
next length if !count_comments? && comment_line?(source_line)
|
30
|
+
next length if irrelevant_line(source_line)
|
38
31
|
length + 1
|
39
32
|
end
|
40
|
-
|
41
|
-
if class_length > max_length
|
42
|
-
message = sprintf(MSG, class_length, max_length)
|
43
|
-
convention(node, :keyword, message)
|
44
|
-
end
|
45
33
|
end
|
46
34
|
|
47
35
|
def line_numbers_of_inner_classes(node)
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Common functionality for checking length of code segments.
|
7
|
+
module CodeLength
|
8
|
+
include ConfigurableMax
|
9
|
+
|
10
|
+
def max_length
|
11
|
+
cop_config['Max']
|
12
|
+
end
|
13
|
+
|
14
|
+
def count_comments?
|
15
|
+
cop_config['CountComments']
|
16
|
+
end
|
17
|
+
|
18
|
+
def check(node, *_)
|
19
|
+
length = code_length(node)
|
20
|
+
if length > max_length
|
21
|
+
add_offence(node, :keyword, sprintf(message, length,
|
22
|
+
max_length)) do
|
23
|
+
self.max = length
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns true for lines that shall not be included in the count.
|
29
|
+
def irrelevant_line(source_line)
|
30
|
+
source_line.blank? || !count_comments? && comment_line?(source_line)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Handles `EnforcedStyle` configuration parameters.
|
7
|
+
module ConfigurableEnforcedStyle
|
8
|
+
def opposite_style_detected
|
9
|
+
self.config_to_allow_offences ||=
|
10
|
+
{ 'EnforcedStyle' => alternative_style.to_s }
|
11
|
+
both_styles_detected if config_to_allow_offences['Enabled']
|
12
|
+
end
|
13
|
+
|
14
|
+
def correct_style_detected
|
15
|
+
# Enabled:true indicates, later when the opposite style is detected,
|
16
|
+
# that the correct style is used somewhere.
|
17
|
+
self.config_to_allow_offences ||= { 'Enabled' => true }
|
18
|
+
both_styles_detected if config_to_allow_offences['EnforcedStyle']
|
19
|
+
end
|
20
|
+
|
21
|
+
def both_styles_detected
|
22
|
+
# Both correct and opposite styles exist.
|
23
|
+
self.config_to_allow_offences = { 'Enabled' => false }
|
24
|
+
end
|
25
|
+
|
26
|
+
def unrecognized_style_detected
|
27
|
+
# All we can do is to disable.
|
28
|
+
self.config_to_allow_offences = { 'Enabled' => false }
|
29
|
+
end
|
30
|
+
|
31
|
+
def style
|
32
|
+
s = cop_config['EnforcedStyle']
|
33
|
+
if cop_config['SupportedStyles'].include?(s)
|
34
|
+
s.to_sym
|
35
|
+
else
|
36
|
+
fail "Unknown style #{s} selected!"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def alternative_style
|
41
|
+
a = cop_config['SupportedStyles'].map(&:to_sym)
|
42
|
+
if a.size != 2
|
43
|
+
fail 'alternative_style can only be used when there are exactly ' +
|
44
|
+
'2 SupportedStyles'
|
45
|
+
end
|
46
|
+
style == a.first ? a.last : a.first
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Handles `Max` configuration parameters, especially setting them to an
|
7
|
+
# appropriate value with --auto-gen-config.
|
8
|
+
module ConfigurableMax
|
9
|
+
def max=(value)
|
10
|
+
cfg = self.config_to_allow_offences ||= {}
|
11
|
+
value = [cfg['Max'], value].max if cfg['Max']
|
12
|
+
cfg['Max'] = value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -6,6 +6,8 @@ module Rubocop
|
|
6
6
|
# This module provides functionality for checking if names match the
|
7
7
|
# configured EnforcedStyle.
|
8
8
|
module ConfigurableNaming
|
9
|
+
include ConfigurableEnforcedStyle
|
10
|
+
|
9
11
|
SNAKE_CASE = /^@?[\da-z_]+[!?=]?$/
|
10
12
|
CAMEL_CASE = /^@?[a-z][\da-zA-Z]+[!?=]?$/
|
11
13
|
|
@@ -14,20 +16,12 @@ module Rubocop
|
|
14
16
|
|
15
17
|
name = range.source.to_sym
|
16
18
|
unless matches_config?(name) || Cop::OPERATOR_METHODS.include?(name)
|
17
|
-
|
19
|
+
add_offence(node, range, message(style))
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
21
23
|
def matches_config?(name)
|
22
|
-
|
23
|
-
when 'snake_case'
|
24
|
-
name =~ SNAKE_CASE
|
25
|
-
when 'camelCase'
|
26
|
-
name =~ CAMEL_CASE
|
27
|
-
else
|
28
|
-
fail 'Illegal value for EnforcedStyle. Must be snake_case or ' +
|
29
|
-
'camelCase.'
|
30
|
-
end
|
24
|
+
name =~ (style == :snake_case ? SNAKE_CASE : CAMEL_CASE)
|
31
25
|
end
|
32
26
|
|
33
27
|
# Returns a range containing the method name after the given regexp and
|
@@ -15,10 +15,12 @@ module Rubocop
|
|
15
15
|
def on_casgn(node)
|
16
16
|
_scope, const_name, value = *node
|
17
17
|
|
18
|
-
# We cannot know the result of method calls
|
18
|
+
# We cannot know the result of method calls like
|
19
19
|
# NewClass = something_that_returns_a_class
|
20
|
-
|
21
|
-
|
20
|
+
# It's also ok to assign a class constant another class constant
|
21
|
+
# SomeClass = SomeOtherClass
|
22
|
+
unless value && [:send, :block, :const].include?(value.type)
|
23
|
+
add_offence(node, :name) if const_name !~ SNAKE_CASE
|
22
24
|
end
|
23
25
|
end
|
24
26
|
end
|
@@ -14,30 +14,25 @@ module Rubocop
|
|
14
14
|
# and ||/or is shorthand for a sequence of ifs, so they also add one.
|
15
15
|
# Loops can be said to have an exit condition, so they add one.
|
16
16
|
class CyclomaticComplexity < Cop
|
17
|
+
include CheckMethods
|
18
|
+
include ConfigurableMax
|
19
|
+
|
17
20
|
MSG = 'Cyclomatic complexity for %s is too high. [%d/%d]'
|
18
21
|
DECISION_POINT_NODES = [:if, :while, :until, :for, :rescue, :when,
|
19
22
|
:and, :or]
|
20
23
|
|
21
|
-
def on_def(node)
|
22
|
-
method_name, _args, _body = *node
|
23
|
-
check(node, method_name)
|
24
|
-
end
|
25
|
-
|
26
|
-
def on_defs(node)
|
27
|
-
_scope, method_name, _args, _body = *node
|
28
|
-
check(node, method_name)
|
29
|
-
end
|
30
|
-
|
31
24
|
private
|
32
25
|
|
33
|
-
def check(node, method_name)
|
26
|
+
def check(node, method_name, *_)
|
34
27
|
complexity = 1
|
35
28
|
on_node(DECISION_POINT_NODES, node) { complexity += 1 }
|
36
29
|
|
37
30
|
max = cop_config['Max']
|
38
31
|
if complexity > max
|
39
|
-
|
40
|
-
|
32
|
+
add_offence(node, :keyword,
|
33
|
+
sprintf(MSG, method_name, complexity, max)) do
|
34
|
+
self.max = complexity
|
35
|
+
end
|
41
36
|
end
|
42
37
|
end
|
43
38
|
end
|
@@ -7,27 +7,18 @@ module Rubocop
|
|
7
7
|
# that does not take any arguments. Both instance and
|
8
8
|
# class/singleton methods are checked.
|
9
9
|
class DefWithParentheses < Cop
|
10
|
+
include CheckMethods
|
11
|
+
|
10
12
|
MSG = "Omit the parentheses in defs when the method doesn't accept " +
|
11
13
|
'any arguments.'
|
12
14
|
|
13
|
-
def
|
14
|
-
start_line = node.loc.keyword.line
|
15
|
-
end_line = node.loc.end.line
|
16
|
-
|
17
|
-
return if start_line == end_line
|
18
|
-
|
19
|
-
_, args = *node
|
20
|
-
convention(args, :begin) if args.children == [] && args.loc.begin
|
21
|
-
end
|
22
|
-
|
23
|
-
def on_defs(node)
|
15
|
+
def check(node, _method_name, args, _body)
|
24
16
|
start_line = node.loc.keyword.line
|
25
17
|
end_line = node.loc.end.line
|
26
18
|
|
27
19
|
return if start_line == end_line
|
28
20
|
|
29
|
-
|
30
|
-
convention(args, :begin) if args.children == [] && args.loc.begin
|
21
|
+
add_offence(args, :begin) if args.children == [] && args.loc.begin
|
31
22
|
end
|
32
23
|
|
33
24
|
def autocorrect(node)
|
@@ -37,36 +28,6 @@ module Rubocop
|
|
37
28
|
end
|
38
29
|
end
|
39
30
|
end
|
40
|
-
|
41
|
-
# This cop checks for missing parentheses in the definition of a
|
42
|
-
# method, that takes arguments. Both instance and
|
43
|
-
# class/singleton methods are checked.
|
44
|
-
class DefWithoutParentheses < Cop
|
45
|
-
MSG = 'Use def with parentheses when there are arguments.'
|
46
|
-
|
47
|
-
def on_def(node)
|
48
|
-
_, args = *node
|
49
|
-
|
50
|
-
if args.children.size > 0 && args.loc.begin.nil?
|
51
|
-
convention(args, :expression)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def on_defs(node)
|
56
|
-
_, _, args = *node
|
57
|
-
|
58
|
-
if args.children.size > 0 && args.loc.begin.nil?
|
59
|
-
convention(args, :expression)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def autocorrect(node)
|
64
|
-
@corrections << lambda do |corrector|
|
65
|
-
corrector.insert_before(node.loc.expression, '(')
|
66
|
-
corrector.insert_after(node.loc.expression, ')')
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
31
|
end
|
71
32
|
end
|
72
33
|
end
|
@@ -36,7 +36,7 @@ module Rubocop
|
|
36
36
|
next if node.type == :class && !body
|
37
37
|
next if namespace?(body)
|
38
38
|
next unless ast_with_comments[node].empty?
|
39
|
-
|
39
|
+
add_offence(node, :keyword, format(MSG, node.type.to_s))
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -10,7 +10,7 @@ module Rubocop
|
|
10
10
|
def on_send(node)
|
11
11
|
return unless node.loc.dot
|
12
12
|
|
13
|
-
|
13
|
+
add_offence(node, :dot) unless proper_dot_position?(node)
|
14
14
|
end
|
15
15
|
|
16
16
|
private
|
@@ -27,10 +27,14 @@ module Rubocop
|
|
27
27
|
|
28
28
|
case cop_config['Style'].downcase
|
29
29
|
when 'leading' then dot_line == selector_line
|
30
|
-
when 'trailing' then dot_line != selector_line
|
30
|
+
when 'trailing' then dot_line != selector_line || same_line?(node)
|
31
31
|
else fail 'Unknown dot position style selected.'
|
32
32
|
end
|
33
33
|
end
|
34
|
+
|
35
|
+
def same_line?(node)
|
36
|
+
node.loc.dot.line == node.loc.line
|
37
|
+
end
|
34
38
|
end
|
35
39
|
end
|
36
40
|
end
|