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
@@ -28,7 +28,7 @@ module Rubocop
|
|
28
28
|
processed_source[0...(line - 1)],
|
29
29
|
0,
|
30
30
|
1)
|
31
|
-
|
31
|
+
add_offence(range, range)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
@@ -36,6 +36,10 @@ module Rubocop
|
|
36
36
|
prev_line = cur_line
|
37
37
|
end
|
38
38
|
end
|
39
|
+
|
40
|
+
def autocorrect(range)
|
41
|
+
@corrections << ->(corrector) { corrector.remove(range) }
|
42
|
+
end
|
39
43
|
end
|
40
44
|
end
|
41
45
|
end
|
@@ -3,34 +3,49 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# This cops checks
|
6
|
+
# This cops checks redundant blanks lines around the bodies of classes,
|
7
|
+
# modules & methods.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
#
|
11
|
+
# class Test
|
12
|
+
#
|
13
|
+
# def something
|
14
|
+
# ...
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# def something(arg)
|
20
|
+
#
|
21
|
+
# ...
|
22
|
+
# end
|
23
|
+
#
|
7
24
|
class EmptyLinesAroundBody < Cop
|
25
|
+
include CheckMethods
|
26
|
+
|
8
27
|
MSG_BEG = 'Extra blank line detected at body beginning.'
|
9
28
|
MSG_END = 'Extra blank line detected at body end.'
|
10
29
|
|
11
30
|
def on_class(node)
|
12
|
-
|
31
|
+
check(node)
|
13
32
|
end
|
14
33
|
|
15
34
|
def on_module(node)
|
16
|
-
|
35
|
+
check(node)
|
17
36
|
end
|
18
37
|
|
19
38
|
def on_sclass(node)
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
def on_def(node)
|
24
|
-
check_node(node)
|
39
|
+
check(node)
|
25
40
|
end
|
26
41
|
|
27
|
-
def
|
28
|
-
|
42
|
+
def autocorrect(range)
|
43
|
+
@corrections << ->(corrector) { corrector.remove(range) }
|
29
44
|
end
|
30
45
|
|
31
46
|
private
|
32
47
|
|
33
|
-
def
|
48
|
+
def check(node, *_)
|
34
49
|
start_line = node.loc.keyword.line
|
35
50
|
end_line = node.loc.end.line
|
36
51
|
|
@@ -45,7 +60,7 @@ module Rubocop
|
|
45
60
|
processed_source[0...start_line],
|
46
61
|
0,
|
47
62
|
1)
|
48
|
-
|
63
|
+
add_offence(range, range, MSG_BEG)
|
49
64
|
end
|
50
65
|
|
51
66
|
if processed_source.lines[end_line - 2].blank?
|
@@ -53,7 +68,7 @@ module Rubocop
|
|
53
68
|
processed_source[0...(end_line - 2)],
|
54
69
|
0,
|
55
70
|
1)
|
56
|
-
|
71
|
+
add_offence(range, range, MSG_END)
|
57
72
|
end
|
58
73
|
end
|
59
74
|
end
|
@@ -33,11 +33,11 @@ module Rubocop
|
|
33
33
|
|
34
34
|
case node
|
35
35
|
when ARRAY_NODE
|
36
|
-
|
36
|
+
add_offence(node, :expression, ARR_MSG)
|
37
37
|
when HASH_NODE
|
38
|
-
|
38
|
+
add_offence(node, :expression, HASH_MSG)
|
39
39
|
when STR_NODE
|
40
|
-
|
40
|
+
add_offence(node, :expression, STR_MSG)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -16,11 +16,11 @@ module Rubocop
|
|
16
16
|
line_number += 1 if processed_source[line_number] =~ /^#!/
|
17
17
|
line = processed_source[line_number]
|
18
18
|
unless line =~ /#.*coding\s?[:=]\s?(UTF|utf)-8/
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
add_offence(nil,
|
20
|
+
source_range(processed_source.buffer,
|
21
|
+
processed_source[0...line_number],
|
22
|
+
0, 1),
|
23
|
+
MSG)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
@@ -8,14 +8,16 @@ module Rubocop
|
|
8
8
|
MSG = 'Carriage return character detected.'
|
9
9
|
|
10
10
|
def investigate(processed_source)
|
11
|
-
|
11
|
+
buffer = processed_source.buffer
|
12
|
+
original_source = IO.read(buffer.name,
|
13
|
+
encoding: buffer.source.encoding)
|
12
14
|
original_source.lines.each_with_index do |line, index|
|
13
15
|
if line =~ /\r$/
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
add_offence(nil,
|
17
|
+
source_range(buffer,
|
18
|
+
processed_source[0...index],
|
19
|
+
0, line.length),
|
20
|
+
MSG)
|
19
21
|
# Usually there will be carriage return characters on all or none
|
20
22
|
# of the lines in a file, so we report only one offence.
|
21
23
|
break
|
@@ -28,13 +28,13 @@ module Rubocop
|
|
28
28
|
return unless div_by_2?(receiver)
|
29
29
|
|
30
30
|
if args == ZERO
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
add_offence(node,
|
32
|
+
:expression,
|
33
|
+
method == :== ? MSG_EVEN : MSG_ODD)
|
34
34
|
elsif args == ONE
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
add_offence(node,
|
36
|
+
:expression,
|
37
|
+
method == :== ? MSG_ODD : MSG_EVEN)
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
@@ -5,6 +5,8 @@ module Rubocop
|
|
5
5
|
module Style
|
6
6
|
# Common functionality for modifier cops.
|
7
7
|
module FavorModifier
|
8
|
+
include IfNode
|
9
|
+
|
8
10
|
# TODO: Extremely ugly solution that needs lots of polish.
|
9
11
|
def check(sexp, comments)
|
10
12
|
case sexp.loc.keyword.source
|
@@ -13,23 +15,23 @@ module Rubocop
|
|
13
15
|
else cond, body = *sexp
|
14
16
|
end
|
15
17
|
|
16
|
-
if length(sexp) > 3
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
indentation = sexp.loc.keyword.column
|
25
|
-
kw_length = sexp.loc.keyword.size
|
26
|
-
cond_length = conditional_length(cond)
|
27
|
-
space = 1
|
28
|
-
total = indentation + body_length + space + kw_length + space +
|
29
|
-
cond_length
|
30
|
-
total <= max_line_length && !body_has_comment?(body, comments)
|
31
|
-
end
|
18
|
+
return false if length(sexp) > 3
|
19
|
+
|
20
|
+
body_length = body_length(body)
|
21
|
+
|
22
|
+
return false if body_length == 0
|
23
|
+
|
24
|
+
on_node(:lvasgn, cond) do
|
25
|
+
return false
|
32
26
|
end
|
27
|
+
|
28
|
+
indentation = sexp.loc.keyword.column
|
29
|
+
kw_length = sexp.loc.keyword.size
|
30
|
+
cond_length = conditional_length(cond)
|
31
|
+
space = 1
|
32
|
+
total = indentation + body_length + space + kw_length + space +
|
33
|
+
cond_length
|
34
|
+
total <= max_line_length && !body_has_comment?(body, comments)
|
33
35
|
end
|
34
36
|
|
35
37
|
def max_line_length
|
@@ -85,26 +87,10 @@ module Rubocop
|
|
85
87
|
next if if_else?(node)
|
86
88
|
|
87
89
|
if check(node, processed_source.comments)
|
88
|
-
|
90
|
+
add_offence(node, :keyword, error_message)
|
89
91
|
end
|
90
92
|
end
|
91
93
|
end
|
92
|
-
|
93
|
-
def ternary_op?(node)
|
94
|
-
node.loc.respond_to?(:question)
|
95
|
-
end
|
96
|
-
|
97
|
-
def modifier_if?(node)
|
98
|
-
node.loc.end.nil?
|
99
|
-
end
|
100
|
-
|
101
|
-
def elsif?(node)
|
102
|
-
node.loc.keyword.is?('elsif')
|
103
|
-
end
|
104
|
-
|
105
|
-
def if_else?(node)
|
106
|
-
node.loc.respond_to?(:else) && node.loc.else
|
107
|
-
end
|
108
94
|
end
|
109
95
|
|
110
96
|
# Checks for while and until statements that would fit on one line
|
@@ -122,7 +108,7 @@ module Rubocop
|
|
122
108
|
next unless node.loc.end
|
123
109
|
|
124
110
|
if check(node, processed_source.comments)
|
125
|
-
|
111
|
+
add_offence(node, :keyword)
|
126
112
|
end
|
127
113
|
end
|
128
114
|
end
|
@@ -15,7 +15,7 @@ module Rubocop
|
|
15
15
|
if condition.type == :send
|
16
16
|
_object, method = *condition
|
17
17
|
if method == :! && !(node.loc.respond_to?(:else) && node.loc.else)
|
18
|
-
|
18
|
+
add_offence(node, :expression, error_message)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop looks for uses of flip flop operator
|
7
|
+
class FlipFlop < Cop
|
8
|
+
MSG = 'Avoid the use of flip flop operators.'
|
9
|
+
|
10
|
+
def on_iflipflop(node)
|
11
|
+
add_offence(node, :expression)
|
12
|
+
end
|
13
|
+
|
14
|
+
def on_eflipflop(node)
|
15
|
+
add_offence(node, :expression)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -3,12 +3,43 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# This cop looks for uses of the *for* keyword.
|
6
|
+
# This cop looks for uses of the *for* keyword, or *each* method. The
|
7
|
+
# preferred alternative is set in the EnforcedStyle configuration
|
8
|
+
# parameter. An *each* call with a block on a single line is always
|
9
|
+
# allowed, however.
|
7
10
|
class For < Cop
|
8
|
-
|
11
|
+
include ConfigurableEnforcedStyle
|
9
12
|
|
10
13
|
def on_for(node)
|
11
|
-
|
14
|
+
if style == :each
|
15
|
+
add_offence(node, :keyword, 'Prefer *each* over *for*.') do
|
16
|
+
opposite_style_detected
|
17
|
+
end
|
18
|
+
else
|
19
|
+
correct_style_detected
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def on_block(node)
|
24
|
+
return if block_length(node) == 0
|
25
|
+
|
26
|
+
method, _args, _body = *node
|
27
|
+
return unless method.type == :send
|
28
|
+
|
29
|
+
_receiver, method_name, *args = *method
|
30
|
+
return unless method_name == :each && args.empty?
|
31
|
+
|
32
|
+
if style == :for
|
33
|
+
end_pos = method.loc.expression.end_pos
|
34
|
+
range = Parser::Source::Range.new(processed_source.buffer,
|
35
|
+
end_pos - 'each'.length,
|
36
|
+
end_pos)
|
37
|
+
add_offence(range, range, 'Prefer *for* over *each*.') do
|
38
|
+
opposite_style_detected
|
39
|
+
end
|
40
|
+
else
|
41
|
+
correct_style_detected
|
42
|
+
end
|
12
43
|
end
|
13
44
|
end
|
14
45
|
end
|
@@ -14,9 +14,9 @@ module Rubocop
|
|
14
14
|
_receiver, method_name, *args = *node
|
15
15
|
|
16
16
|
if args.size == 1 && DEPRECATED_METHODS.include?(method_name)
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
add_offence(node, :selector,
|
18
|
+
MSG.format(method_name,
|
19
|
+
proper_method_name(method_name)))
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -10,15 +10,13 @@ module Rubocop
|
|
10
10
|
#
|
11
11
|
# A separate offence is registered for each problematic pair.
|
12
12
|
class HashSyntax < Cop
|
13
|
+
include ConfigurableEnforcedStyle
|
14
|
+
|
13
15
|
MSG_19 = 'Use the new Ruby 1.9 hash syntax.'
|
14
16
|
MSG_HASH_ROCKETS = 'Always use hash rockets in hashes.'
|
15
17
|
|
16
18
|
def on_hash(node)
|
17
|
-
|
18
|
-
when 'ruby19' then ruby19_check(node)
|
19
|
-
when 'hash_rockets' then hash_rockets_check(node)
|
20
|
-
else fail 'Unknown HashSyntax style'
|
21
|
-
end
|
19
|
+
style == :ruby19 ? ruby19_check(node) : hash_rockets_check(node)
|
22
20
|
end
|
23
21
|
|
24
22
|
def ruby19_check(node)
|
@@ -38,8 +36,7 @@ module Rubocop
|
|
38
36
|
def autocorrect(node)
|
39
37
|
key = node.children.first.loc.expression
|
40
38
|
op = node.loc.operator
|
41
|
-
if
|
42
|
-
!space_before_operator?(op, key) &&
|
39
|
+
if style == :ruby19 && !space_before_operator?(op, key) &&
|
43
40
|
config.for_cop('SpaceAroundOperators')['Enabled']
|
44
41
|
# Don't do the correction if there is no space before '=>'. The
|
45
42
|
# combined corrections of this cop and SpaceAroundOperators could
|
@@ -48,7 +45,7 @@ module Rubocop
|
|
48
45
|
end
|
49
46
|
|
50
47
|
@corrections << lambda do |corrector|
|
51
|
-
if
|
48
|
+
if style == :ruby19
|
52
49
|
corrector.insert_after(key, ' ')
|
53
50
|
corrector.replace(key, key.source.sub(/^:(.*)/, '\1:'))
|
54
51
|
else
|
@@ -68,9 +65,9 @@ module Rubocop
|
|
68
65
|
def check(pairs, delim, msg)
|
69
66
|
pairs.each do |pair|
|
70
67
|
if pair.loc.operator && pair.loc.operator.is?(delim)
|
71
|
-
|
72
|
-
|
73
|
-
|
68
|
+
add_offence(pair,
|
69
|
+
pair.loc.expression.begin.join(pair.loc.operator),
|
70
|
+
msg)
|
74
71
|
end
|
75
72
|
end
|
76
73
|
end
|