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
@@ -8,7 +8,9 @@ module Rubocop
|
|
8
8
|
MSG = 'Never use "when x;". Use "when x then" instead.'
|
9
9
|
|
10
10
|
def on_when(node)
|
11
|
-
|
11
|
+
if node.loc.begin && node.loc.begin.is?(';')
|
12
|
+
add_offence(node, :begin)
|
13
|
+
end
|
12
14
|
end
|
13
15
|
|
14
16
|
def autocorrect(node)
|
@@ -6,21 +6,15 @@ module Rubocop
|
|
6
6
|
# This cop checks for array literals made up of word-like
|
7
7
|
# strings, that are not using the %w() syntax.
|
8
8
|
class WordArray < Cop
|
9
|
+
include ArraySyntax
|
10
|
+
|
9
11
|
MSG = 'Use %w or %W for array of words.'
|
10
12
|
|
11
13
|
def on_array(node)
|
12
|
-
return unless node.loc.begin && node.loc.begin.is?('[')
|
13
|
-
|
14
14
|
array_elems = node.children
|
15
|
-
|
16
|
-
# no need to check empty arrays
|
17
|
-
return unless array_elems && array_elems.size > 1
|
18
|
-
|
19
|
-
string_array = array_elems.all? { |e| e.type == :str }
|
20
|
-
|
21
|
-
if string_array && !complex_content?(array_elems) &&
|
15
|
+
if array_of?(:str, node) && !complex_content?(array_elems) &&
|
22
16
|
array_elems.size > min_size && !comments_in_array?(node)
|
23
|
-
|
17
|
+
add_offence(node, :expression)
|
24
18
|
end
|
25
19
|
end
|
26
20
|
|
data/lib/rubocop/cop/util.rb
CHANGED
@@ -4,6 +4,10 @@ module Rubocop
|
|
4
4
|
module Cop
|
5
5
|
# This module contains a collection of useful utility methods.
|
6
6
|
module Util
|
7
|
+
extend AST::Sexp
|
8
|
+
|
9
|
+
PROC_NEW_NODE = s(:send, s(:const, nil, :Proc), :new)
|
10
|
+
|
7
11
|
module_function
|
8
12
|
|
9
13
|
def strip_quotes(str)
|
@@ -23,18 +27,6 @@ module Rubocop
|
|
23
27
|
block_node.loc.end.line - block_node.loc.begin.line
|
24
28
|
end
|
25
29
|
|
26
|
-
def source_length(source, count_comments = nil)
|
27
|
-
lines = source.lines.to_a[1...-1]
|
28
|
-
|
29
|
-
return 0 unless lines
|
30
|
-
|
31
|
-
lines.reject!(&:blank?)
|
32
|
-
|
33
|
-
lines.reject! { |line| comment_line?(line) } unless count_comments
|
34
|
-
|
35
|
-
lines.size
|
36
|
-
end
|
37
|
-
|
38
30
|
def comment_line?(line_source)
|
39
31
|
line_source =~ /^\s*#/
|
40
32
|
end
|
@@ -69,6 +61,87 @@ module Rubocop
|
|
69
61
|
|
70
62
|
const_names.reverse.join('::')
|
71
63
|
end
|
64
|
+
|
65
|
+
def command?(name, node)
|
66
|
+
return unless node.type == :send
|
67
|
+
|
68
|
+
receiver, method_name, _args = *node
|
69
|
+
|
70
|
+
# commands have no explicit receiver
|
71
|
+
!receiver && method_name == name
|
72
|
+
end
|
73
|
+
|
74
|
+
def lambda?(node)
|
75
|
+
fail 'Not a block node' unless node.type == :block
|
76
|
+
|
77
|
+
send_node, _block_args, _block_body = *node
|
78
|
+
|
79
|
+
command?(:lambda, send_node)
|
80
|
+
end
|
81
|
+
|
82
|
+
def proc?(node)
|
83
|
+
fail 'Not a block node' unless node.type == :block
|
84
|
+
|
85
|
+
send_node, _block_args, _block_body = *node
|
86
|
+
|
87
|
+
command?(:proc, send_node) || send_node == PROC_NEW_NODE
|
88
|
+
end
|
89
|
+
|
90
|
+
def lambda_or_proc?(node)
|
91
|
+
lambda?(node) || proc?(node)
|
92
|
+
end
|
93
|
+
|
94
|
+
def on_node(syms, sexp, excludes = [])
|
95
|
+
yield sexp if Array(syms).include?(sexp.type)
|
96
|
+
|
97
|
+
return if Array(excludes).include?(sexp.type)
|
98
|
+
|
99
|
+
sexp.children.each do |elem|
|
100
|
+
if elem.is_a?(Parser::AST::Node)
|
101
|
+
on_node(syms, elem, excludes) { |s| yield s }
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def source_range(source_buffer, preceding_lines, begin_column,
|
107
|
+
column_count)
|
108
|
+
newline_length = 1
|
109
|
+
begin_pos = preceding_lines.reduce(0) do |a, e|
|
110
|
+
a + e.length + newline_length
|
111
|
+
end + begin_column
|
112
|
+
Parser::Source::Range.new(source_buffer, begin_pos,
|
113
|
+
begin_pos + column_count)
|
114
|
+
end
|
115
|
+
|
116
|
+
def range_with_surrounding_space(range, side = :both)
|
117
|
+
src = @processed_source.buffer.source
|
118
|
+
go_left = side == :left || side == :both
|
119
|
+
go_right = side == :right || side == :both
|
120
|
+
begin_pos = range.begin_pos
|
121
|
+
begin_pos -= 1 while go_left && src[begin_pos - 1] =~ /[ \t]/
|
122
|
+
end_pos = range.end_pos
|
123
|
+
end_pos += 1 while go_right && src[end_pos] =~ /[ \t]/
|
124
|
+
end_pos += 1 if go_right && src[end_pos] == "\n"
|
125
|
+
Parser::Source::Range.new(@processed_source.buffer, begin_pos, end_pos)
|
126
|
+
end
|
127
|
+
|
128
|
+
# Returns for example a bare `if` node if the given node is an `if` whith
|
129
|
+
# calls chained to the end of it.
|
130
|
+
def first_part_of_call_chain(node)
|
131
|
+
while node
|
132
|
+
case node.type
|
133
|
+
when :send
|
134
|
+
receiver, _method_name, _args = *node
|
135
|
+
node = receiver
|
136
|
+
when :block
|
137
|
+
method, _args, _body = *node
|
138
|
+
node = method
|
139
|
+
else
|
140
|
+
break
|
141
|
+
end
|
142
|
+
end
|
143
|
+
node
|
144
|
+
end
|
72
145
|
end
|
73
146
|
end
|
74
147
|
end
|
@@ -4,7 +4,7 @@ module Rubocop
|
|
4
4
|
module Cop
|
5
5
|
# This module provides a way to track local variables and scopes of Ruby.
|
6
6
|
# This is intended to be used as mix-in, and the user class may override
|
7
|
-
# some of hook methods.
|
7
|
+
# some of the hook methods.
|
8
8
|
module VariableInspector
|
9
9
|
VARIABLE_ASSIGNMENT_TYPE = :lvasgn
|
10
10
|
REGEXP_NAMED_CAPTURE_TYPE = :match_with_lvasgn
|
@@ -50,7 +50,7 @@ module Rubocop
|
|
50
50
|
def inspect_variables(root_node)
|
51
51
|
return unless root_node
|
52
52
|
|
53
|
-
# Wrap with begin node if it's standalone node.
|
53
|
+
# Wrap with begin node if it's a standalone node.
|
54
54
|
unless root_node.type == :begin
|
55
55
|
root_node = Parser::AST::Node.new(:begin, [root_node])
|
56
56
|
end
|
@@ -84,6 +84,7 @@ module Rubocop
|
|
84
84
|
throw :skip_children
|
85
85
|
end
|
86
86
|
|
87
|
+
# rubocop:disable MethodLength, CyclomaticComplexity
|
87
88
|
def dispatch_node(node)
|
88
89
|
case node.type
|
89
90
|
when *DECLARATION_TYPES
|
@@ -108,6 +109,7 @@ module Rubocop
|
|
108
109
|
process_scope(node)
|
109
110
|
end
|
110
111
|
end
|
112
|
+
# rubocop:enable MethodLength, CyclomaticComplexity
|
111
113
|
|
112
114
|
def process_variable_declaration(node)
|
113
115
|
# restarg would have no name:
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
# This class handles the processing of files, which includes dealing with
|
5
|
+
# formatters and letting cops inspect the files.
|
6
|
+
class FileInspector
|
7
|
+
def initialize(options)
|
8
|
+
@options = options
|
9
|
+
@errors = []
|
10
|
+
end
|
11
|
+
|
12
|
+
# Takes a block which it calls once per inspected file. The block shall
|
13
|
+
# return true if the caller wants to break the loop early.
|
14
|
+
def process_files(target_files, config_store)
|
15
|
+
target_files.each(&:freeze).freeze
|
16
|
+
inspected_files = []
|
17
|
+
any_failed = false
|
18
|
+
|
19
|
+
formatter_set.started(target_files)
|
20
|
+
|
21
|
+
target_files.each do |file|
|
22
|
+
break if yield
|
23
|
+
|
24
|
+
puts "Scanning #{file}" if @options[:debug]
|
25
|
+
formatter_set.file_started(file, {})
|
26
|
+
|
27
|
+
offences = inspect_file(file, config_store)
|
28
|
+
|
29
|
+
any_failed = true unless offences.empty?
|
30
|
+
inspected_files << file
|
31
|
+
formatter_set.file_finished(file, offences.freeze)
|
32
|
+
end
|
33
|
+
|
34
|
+
formatter_set.finished(inspected_files.freeze)
|
35
|
+
formatter_set.close_output_files
|
36
|
+
any_failed
|
37
|
+
end
|
38
|
+
|
39
|
+
def display_error_summary
|
40
|
+
return if @errors.empty?
|
41
|
+
plural = @errors.count > 1 ? 's' : ''
|
42
|
+
warn "\n#{@errors.count} error#{plural} occurred:".color(:red)
|
43
|
+
@errors.each { |error| warn error }
|
44
|
+
warn 'Errors are usually caused by RuboCop bugs.'
|
45
|
+
warn 'Please, report your problems to RuboCop\'s issue tracker.'
|
46
|
+
warn 'Mention the following information in the issue report:'
|
47
|
+
warn Rubocop::Version.version(true)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def inspect_file(file, config_store)
|
53
|
+
config = config_store.for(file)
|
54
|
+
team = Cop::Team.new(mobilized_cop_classes(config), config, @options)
|
55
|
+
offences = team.inspect_file(file)
|
56
|
+
@errors.concat(team.errors)
|
57
|
+
offences
|
58
|
+
end
|
59
|
+
|
60
|
+
def mobilized_cop_classes(config)
|
61
|
+
@mobilized_cop_classes ||= {}
|
62
|
+
@mobilized_cop_classes[config.object_id] ||= begin
|
63
|
+
cop_classes = Cop::Cop.all
|
64
|
+
|
65
|
+
if @options[:only]
|
66
|
+
cop_classes.select! { |c| c.cop_name == @options[:only] }
|
67
|
+
else
|
68
|
+
# filter out Rails cops unless requested
|
69
|
+
cop_classes.reject!(&:rails?) unless run_rails_cops?(config)
|
70
|
+
|
71
|
+
# filter out style cops when --lint is passed
|
72
|
+
cop_classes.select!(&:lint?) if @options[:lint]
|
73
|
+
end
|
74
|
+
|
75
|
+
cop_classes
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def run_rails_cops?(config)
|
80
|
+
@options[:rails] || config['AllCops']['RunRailsCops']
|
81
|
+
end
|
82
|
+
|
83
|
+
def formatter_set
|
84
|
+
@formatter_set ||= begin
|
85
|
+
set = Formatter::FormatterSet.new
|
86
|
+
pairs = @options[:formatters] || [[Options::DEFAULT_FORMATTER]]
|
87
|
+
pairs.each do |formatter_key, output_path|
|
88
|
+
set.add_formatter(formatter_key, output_path)
|
89
|
+
end
|
90
|
+
set
|
91
|
+
rescue => error
|
92
|
+
warn error.message
|
93
|
+
$stderr.puts error.backtrace
|
94
|
+
exit(1)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -9,8 +9,8 @@ module Rubocop
|
|
9
9
|
# ## Creating Custom Formatter
|
10
10
|
#
|
11
11
|
# You can create a custom formatter by subclassing
|
12
|
-
# `Rubocop::Formatter::BaseFormatter` and
|
13
|
-
# or
|
12
|
+
# `Rubocop::Formatter::BaseFormatter` and overriding some methods,
|
13
|
+
# or by implementing all the methods by duck typing.
|
14
14
|
#
|
15
15
|
# ## Using Custom Formatter in Command Line
|
16
16
|
#
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Formatter
|
5
5
|
# This formatter displays a YAML configuration file where all cops that
|
6
|
-
# detected any offences are
|
6
|
+
# detected any offences are configured to not detect the offence.
|
7
7
|
class DisabledConfigFormatter < BaseFormatter
|
8
8
|
HEADING =
|
9
9
|
['# This configuration was generated by `rubocop --auto-gen-config`.',
|
@@ -11,6 +11,12 @@ module Rubocop
|
|
11
11
|
'# one by one as the offences are removed from the code base.']
|
12
12
|
.join("\n")
|
13
13
|
|
14
|
+
@config_to_allow_offences = {}
|
15
|
+
|
16
|
+
class << self
|
17
|
+
attr_accessor :config_to_allow_offences
|
18
|
+
end
|
19
|
+
|
14
20
|
def file_finished(file, offences)
|
15
21
|
@cops_with_offences ||= {}
|
16
22
|
offences.each { |o| @cops_with_offences[o.cop_name] = true }
|
@@ -21,7 +27,9 @@ module Rubocop
|
|
21
27
|
@cops_with_offences.keys.sort.each do |cop_name|
|
22
28
|
output.puts
|
23
29
|
output.puts "#{cop_name}:"
|
24
|
-
|
30
|
+
cfg = self.class.config_to_allow_offences[cop_name]
|
31
|
+
cfg ||= { 'Enabled' => false }
|
32
|
+
cfg.each { |key, value| output.puts " #{key}: #{value}" }
|
25
33
|
end
|
26
34
|
puts "Created #{output.path}."
|
27
35
|
puts "Run rubocop with --config #{output.path}, or"
|
data/lib/rubocop/rake_task.rb
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
require 'rake'
|
4
4
|
require 'rake/tasklib'
|
5
5
|
|
6
|
+
require 'rubocop/options'
|
7
|
+
|
6
8
|
module Rubocop
|
7
9
|
# Provides a custom rake task.
|
8
10
|
#
|
@@ -13,6 +15,7 @@ module Rubocop
|
|
13
15
|
attr_accessor :verbose
|
14
16
|
attr_accessor :fail_on_error
|
15
17
|
attr_accessor :patterns
|
18
|
+
attr_accessor :formatters
|
16
19
|
|
17
20
|
def initialize(*args, &task_block)
|
18
21
|
setup_ivars(args)
|
@@ -36,7 +39,7 @@ module Rubocop
|
|
36
39
|
|
37
40
|
cli = CLI.new
|
38
41
|
puts 'Running RuboCop...' if verbose
|
39
|
-
result = cli.run(patterns)
|
42
|
+
result = cli.run([formatters.map { |f| ['-f', f] }, patterns])
|
40
43
|
abort('RuboCop failed!') if fail_on_error unless result == 0
|
41
44
|
end
|
42
45
|
|
@@ -47,6 +50,7 @@ module Rubocop
|
|
47
50
|
@verbose = true
|
48
51
|
@fail_on_error = true
|
49
52
|
@patterns = []
|
53
|
+
@formatters = [Rubocop::Options::DEFAULT_FORMATTER]
|
50
54
|
end
|
51
55
|
end
|
52
56
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module Rubocop
|
4
|
-
# This class finds target files to inspect by scanning directory tree
|
4
|
+
# This class finds target files to inspect by scanning the directory tree
|
5
5
|
# and picking ruby files.
|
6
6
|
class TargetFinder
|
7
7
|
def initialize(config_store, debug = false)
|
@@ -9,8 +9,8 @@ module Rubocop
|
|
9
9
|
@debug = debug
|
10
10
|
end
|
11
11
|
|
12
|
-
# Generate a list of target files by expanding
|
13
|
-
# (if any). If args is empty recursively
|
12
|
+
# Generate a list of target files by expanding globbing patterns
|
13
|
+
# (if any). If args is empty, recursively find all Ruby source
|
14
14
|
# files under the current directory
|
15
15
|
# @return [Array] array of file paths
|
16
16
|
def find(args)
|
data/lib/rubocop/version.rb
CHANGED
data/rubocop-todo.yml
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# This configuration was generated by `rubocop --auto-gen-config`.
|
2
|
+
# The point is for the user to remove these configuration records
|
3
|
+
# one by one as the offences are removed from the code base.
|
4
|
+
|
5
|
+
ClassLength:
|
6
|
+
Max: 140
|
7
|
+
|
8
|
+
CyclomaticComplexity:
|
9
|
+
Max: 10
|
10
|
+
|
11
|
+
MethodLength:
|
12
|
+
Max: 20
|
data/rubocop.gemspec
CHANGED
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
s.summary = 'Automatic Ruby code style checking tool.'
|
28
28
|
|
29
29
|
s.add_runtime_dependency('rainbow', '>= 1.1.4')
|
30
|
-
s.add_runtime_dependency('parser', '~> 2.
|
30
|
+
s.add_runtime_dependency('parser', '~> 2.1')
|
31
31
|
s.add_runtime_dependency('powerpack', '~> 0.0.6')
|
32
32
|
s.add_development_dependency('rake', '~> 10.1')
|
33
33
|
s.add_development_dependency('rspec', '~> 2.14')
|