rubocop 0.16.0 → 0.17.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/.gitignore +1 -0
- data/.travis.yml +3 -1
- data/CHANGELOG.md +44 -0
- data/CONTRIBUTING.md +40 -8
- data/Gemfile +6 -0
- data/README.md +65 -20
- data/Rakefile +0 -1
- data/config/default.yml +15 -3
- data/config/enabled.yml +143 -109
- data/lib/rubocop.rb +45 -26
- data/lib/rubocop/cli.rb +26 -27
- data/lib/rubocop/config.rb +0 -1
- data/lib/rubocop/config_loader.rb +16 -23
- data/lib/rubocop/cop/commissioner.rb +2 -7
- data/lib/rubocop/cop/cop.rb +24 -51
- data/lib/rubocop/cop/ignored_node.rb +31 -0
- data/lib/rubocop/cop/lint/ambiguous_operator.rb +50 -0
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +36 -0
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +3 -11
- data/lib/rubocop/cop/lint/block_alignment.rb +6 -20
- data/lib/rubocop/cop/lint/condition_position.rb +52 -0
- data/lib/rubocop/cop/lint/else_layout.rb +57 -0
- data/lib/rubocop/cop/lint/end_alignment.rb +33 -8
- data/lib/rubocop/cop/lint/invalid_character_literal.rb +37 -0
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +2 -4
- data/lib/rubocop/cop/lint/syntax.rb +6 -12
- data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +25 -0
- data/lib/rubocop/cop/mixin/array_syntax.rb +20 -0
- data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +76 -0
- data/lib/rubocop/cop/mixin/check_assignment.rb +26 -0
- data/lib/rubocop/cop/{check_methods.rb → mixin/check_methods.rb} +0 -0
- data/lib/rubocop/cop/mixin/code_length.rb +33 -0
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +53 -0
- data/lib/rubocop/cop/mixin/configurable_max.rb +19 -0
- data/lib/rubocop/cop/mixin/configurable_naming.rb +45 -0
- data/lib/rubocop/cop/{style → mixin}/if_node.rb +0 -0
- data/lib/rubocop/cop/mixin/if_then_else.rb +23 -0
- data/lib/rubocop/cop/mixin/negative_conditional.rb +24 -0
- data/lib/rubocop/cop/mixin/parser_diagnostic.rb +34 -0
- data/lib/rubocop/cop/mixin/safe_assignment.rb +19 -0
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +32 -0
- data/lib/rubocop/cop/mixin/space_inside.rb +31 -0
- data/lib/rubocop/cop/mixin/statement_modifier.rb +59 -0
- data/lib/rubocop/cop/mixin/string_help.rb +32 -0
- data/lib/rubocop/cop/mixin/surrounding_space.rb +42 -0
- data/lib/rubocop/cop/rails/default_scope.rb +3 -1
- data/lib/rubocop/cop/style/accessor_method_name.rb +4 -12
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +16 -1
- data/lib/rubocop/cop/style/case_indentation.rb +33 -16
- data/lib/rubocop/cop/style/character_literal.rb +10 -0
- data/lib/rubocop/cop/style/dot_position.rb +23 -6
- data/lib/rubocop/cop/style/empty_lines_around_body.rb +5 -5
- data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +1 -32
- data/lib/rubocop/cop/style/favor_until_over_negated_while.rb +20 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +5 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +34 -0
- data/lib/rubocop/cop/style/if_with_semicolon.rb +1 -1
- data/lib/rubocop/cop/style/indentation_consistency.rb +51 -0
- data/lib/rubocop/cop/style/indentation_width.rb +0 -26
- data/lib/rubocop/cop/style/lambda_call.rb +12 -5
- data/lib/rubocop/cop/style/method_def_parentheses.rb +29 -11
- data/lib/rubocop/cop/style/multiline_if_then.rb +4 -9
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +22 -0
- data/lib/rubocop/cop/style/{ternary_operator.rb → nested_ternary_operator.rb} +0 -15
- data/lib/rubocop/cop/style/numeric_literals.rb +30 -2
- data/lib/rubocop/cop/style/one_line_conditional.rb +2 -1
- data/lib/rubocop/cop/style/parameter_lists.rb +7 -3
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +9 -11
- data/lib/rubocop/cop/style/predicate_name.rb +4 -12
- data/lib/rubocop/cop/style/raise_args.rb +19 -11
- data/lib/rubocop/cop/style/regexp_literal.rb +19 -6
- data/lib/rubocop/cop/style/space_after_colon.rb +36 -0
- data/lib/rubocop/cop/style/space_after_comma.rb +16 -0
- data/lib/rubocop/cop/style/space_after_semicolon.rb +16 -0
- data/lib/rubocop/cop/style/space_around_block_braces.rb +38 -38
- data/lib/rubocop/cop/style/space_around_operators.rb +1 -2
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +6 -2
- data/lib/rubocop/cop/style/string_literals.rb +5 -5
- data/lib/rubocop/cop/style/trailing_comma.rb +94 -0
- data/lib/rubocop/cop/style/unless_else.rb +2 -2
- data/lib/rubocop/cop/style/while_until_modifier.rb +32 -0
- data/lib/rubocop/cop/style/word_array.rb +9 -1
- data/lib/rubocop/cop/util.rb +14 -0
- data/lib/rubocop/cop/variable_inspector.rb +11 -6
- data/lib/rubocop/cop/variable_inspector/scope.rb +4 -3
- data/lib/rubocop/file_inspector.rb +22 -6
- data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/colorizable.rb +37 -0
- data/lib/rubocop/formatter/disabled_config_formatter.rb +27 -6
- data/lib/rubocop/formatter/progress_formatter.rb +1 -1
- data/lib/rubocop/formatter/simple_text_formatter.rb +9 -5
- data/lib/rubocop/options.rb +19 -4
- data/lib/rubocop/target_finder.rb +4 -0
- data/lib/rubocop/version.rb +1 -1
- data/rubocop-todo.yml +10 -2
- data/rubocop.gemspec +3 -2
- data/spec/project_spec.rb +12 -7
- data/spec/rubocop/cli_spec.rb +262 -99
- data/spec/rubocop/config_loader_spec.rb +5 -5
- data/spec/rubocop/config_spec.rb +3 -3
- data/spec/rubocop/config_store_spec.rb +12 -11
- data/spec/rubocop/cop/commissioner_spec.rb +21 -5
- data/spec/rubocop/cop/cop_spec.rb +1 -1
- data/spec/rubocop/cop/lint/ambiguous_operator_spec.rb +113 -0
- data/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb +35 -0
- data/spec/rubocop/cop/lint/block_alignment_spec.rb +2 -2
- data/spec/rubocop/cop/lint/condition_position_spec.rb +49 -0
- data/spec/rubocop/cop/lint/else_layout_spec.rb +65 -0
- data/spec/rubocop/cop/lint/end_alignment_spec.rb +41 -1
- data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +33 -0
- data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +3 -3
- data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +12 -12
- data/spec/rubocop/cop/lint/syntax_spec.rb +2 -2
- data/spec/rubocop/cop/lint/useless_assignment_spec.rb +72 -54
- data/spec/rubocop/cop/lint/useless_else_without_rescue_spec.rb +48 -0
- data/spec/rubocop/cop/offence_spec.rb +1 -1
- data/spec/rubocop/cop/rails/default_scope_spec.rb +6 -0
- data/spec/rubocop/cop/rails/output_spec.rb +2 -1
- data/spec/rubocop/cop/style/align_hash_spec.rb +9 -9
- data/spec/rubocop/cop/style/align_parameters_spec.rb +1 -1
- data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +5 -0
- data/spec/rubocop/cop/style/case_indentation_spec.rb +53 -2
- data/spec/rubocop/cop/style/class_and_module_camel_case_spec.rb +3 -3
- data/spec/rubocop/cop/style/documentation_spec.rb +0 -1
- data/spec/rubocop/cop/style/dot_position_spec.rb +18 -3
- data/spec/rubocop/cop/style/empty_line_between_defs_spec.rb +4 -4
- data/spec/rubocop/cop/style/empty_lines_around_body_spec.rb +13 -0
- data/spec/rubocop/cop/style/favor_unless_over_negated_if_spec.rb +1 -1
- data/spec/rubocop/cop/style/favor_until_over_negated_while_spec.rb +1 -1
- data/spec/rubocop/cop/style/hash_syntax_spec.rb +5 -0
- data/spec/rubocop/cop/style/{favor_modifier_spec.rb → if_unless_modifier_spec.rb} +4 -111
- data/spec/rubocop/cop/style/indentation_consistency_spec.rb +490 -0
- data/spec/rubocop/cop/style/indentation_width_spec.rb +19 -91
- data/spec/rubocop/cop/style/lambda_call_spec.rb +18 -0
- data/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb +2 -2
- data/spec/rubocop/cop/style/method_def_parentheses_spec.rb +35 -1
- data/spec/rubocop/cop/style/method_length_spec.rb +1 -0
- data/spec/rubocop/cop/style/method_name_spec.rb +27 -5
- data/spec/rubocop/cop/style/multiline_block_chain_spec.rb +4 -4
- data/spec/rubocop/cop/style/multiline_if_then_spec.rb +2 -2
- data/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb +18 -0
- data/spec/rubocop/cop/style/{ternary_operator_spec.rb → nested_ternary_operator_spec.rb} +0 -15
- data/spec/rubocop/cop/style/numeric_literals_spec.rb +18 -1
- data/spec/rubocop/cop/style/one_line_conditional_spec.rb +2 -1
- data/spec/rubocop/cop/style/parameter_lists_spec.rb +1 -0
- data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +13 -4
- data/spec/rubocop/cop/style/raise_args_spec.rb +22 -0
- data/spec/rubocop/cop/style/redundant_self_spec.rb +4 -4
- data/spec/rubocop/cop/style/regexp_literal_spec.rb +4 -0
- data/spec/rubocop/cop/style/space_after_colon_spec.rb +12 -4
- data/spec/rubocop/cop/style/space_after_method_name_spec.rb +2 -2
- data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +30 -1
- data/spec/rubocop/cop/style/{space_around_equals_in_default_parameter_spec.rb → space_around_equals_in_parameter_default_spec.rb} +0 -0
- data/spec/rubocop/cop/style/space_around_operators_spec.rb +2 -1
- data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +20 -3
- data/spec/rubocop/cop/style/string_literals_spec.rb +33 -0
- data/spec/rubocop/cop/style/trailing_comma_spec.rb +200 -0
- data/spec/rubocop/cop/style/variable_name_spec.rb +27 -3
- data/spec/rubocop/cop/style/while_until_modifier_spec.rb +75 -0
- data/spec/rubocop/cop/style/word_array_spec.rb +1 -0
- data/spec/rubocop/cop/team_spec.rb +1 -1
- data/spec/rubocop/cop/variable_inspector/scope_spec.rb +3 -4
- data/spec/rubocop/file_inspector_spec.rb +1 -1
- data/spec/rubocop/formatter/base_formatter_spec.rb +12 -11
- data/spec/rubocop/formatter/colorizable_spec.rb +107 -0
- data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +2 -0
- data/spec/rubocop/formatter/formatter_set_spec.rb +1 -1
- data/spec/rubocop/formatter/json_formatter_spec.rb +4 -3
- data/spec/rubocop/formatter/progress_formatter_spec.rb +2 -2
- data/spec/rubocop/options_spec.rb +3 -1
- data/spec/rubocop/target_finder_spec.rb +13 -11
- data/spec/spec_helper.rb +5 -1
- data/spec/support/shared_examples.rb +2 -2
- data/spec/support/statement_modifier_helper.rb +41 -0
- metadata +88 -30
- data/lib/rubocop/cop/check_assignment.rb +0 -43
- data/lib/rubocop/cop/style/array_syntax.rb +0 -22
- data/lib/rubocop/cop/style/autocorrect_alignment.rb +0 -78
- data/lib/rubocop/cop/style/code_length.rb +0 -35
- data/lib/rubocop/cop/style/configurable_enforced_style.rb +0 -51
- data/lib/rubocop/cop/style/configurable_max.rb +0 -17
- data/lib/rubocop/cop/style/configurable_naming.rb +0 -41
- data/lib/rubocop/cop/style/favor_modifier.rb +0 -118
- data/lib/rubocop/cop/style/if_then_else.rb +0 -27
- data/lib/rubocop/cop/style/space_after_comma_etc.rb +0 -73
- data/lib/rubocop/cop/style/space_inside.rb +0 -33
- data/lib/rubocop/cop/style/string_help.rb +0 -30
- data/lib/rubocop/cop/style/surrounding_space.rb +0 -44
@@ -5,8 +5,8 @@ module Rubocop
|
|
5
5
|
module Style
|
6
6
|
# This cop looks for *unless* expressions with *else* clauses.
|
7
7
|
class UnlessElse < Cop
|
8
|
-
MSG = 'Never use unless with else. Rewrite these with the '
|
9
|
-
|
8
|
+
MSG = 'Never use unless with else. Rewrite these with the ' \
|
9
|
+
'positive case first.'
|
10
10
|
|
11
11
|
def on_if(node)
|
12
12
|
loc = node.loc
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Checks for while and until statements that would fit on one line
|
7
|
+
# if written as a modifier while/until.
|
8
|
+
class WhileUntilModifier < Cop
|
9
|
+
include StatementModifier
|
10
|
+
|
11
|
+
def investigate(processed_source)
|
12
|
+
return unless processed_source.ast
|
13
|
+
on_node([:while, :until], processed_source.ast) do |node|
|
14
|
+
# discard modifier while/until
|
15
|
+
next unless node.loc.end
|
16
|
+
|
17
|
+
if check(node, processed_source.comments)
|
18
|
+
add_offence(node, :keyword,
|
19
|
+
message(node.loc.keyword.source))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def message(keyword)
|
27
|
+
"Favor modifier #{keyword} usage when you have a single-line body."
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -7,6 +7,10 @@ module Rubocop
|
|
7
7
|
# strings, that are not using the %w() syntax.
|
8
8
|
class WordArray < Cop
|
9
9
|
include ArraySyntax
|
10
|
+
# The parameter is called MinSize (meaning the minimum array size for
|
11
|
+
# which an offence can be registered), but essentially it's a Max
|
12
|
+
# parameter (the maximum number of something that's allowed).
|
13
|
+
include ConfigurableMax
|
10
14
|
|
11
15
|
MSG = 'Use %w or %W for array of words.'
|
12
16
|
|
@@ -14,12 +18,16 @@ module Rubocop
|
|
14
18
|
array_elems = node.children
|
15
19
|
if array_of?(:str, node) && !complex_content?(array_elems) &&
|
16
20
|
array_elems.size > min_size && !comments_in_array?(node)
|
17
|
-
add_offence(node, :expression)
|
21
|
+
add_offence(node, :expression) { self.max = array_elems.size }
|
18
22
|
end
|
19
23
|
end
|
20
24
|
|
21
25
|
private
|
22
26
|
|
27
|
+
def parameter_name
|
28
|
+
'MinSize'
|
29
|
+
end
|
30
|
+
|
23
31
|
def comments_in_array?(node)
|
24
32
|
comments = processed_source.comments
|
25
33
|
|
data/lib/rubocop/cop/util.rb
CHANGED
@@ -7,9 +7,23 @@ module Rubocop
|
|
7
7
|
extend AST::Sexp
|
8
8
|
|
9
9
|
PROC_NEW_NODE = s(:send, s(:const, nil, :Proc), :new)
|
10
|
+
EQUALS_ASGN_NODES = [:lvasgn, :ivasgn, :cvasgn, :gvasgn, :casgn, :masgn]
|
11
|
+
SHORTHAND_ASGN_NODES = [:op_asgn, :or_asgn, :and_asgn]
|
12
|
+
ASGN_NODES = EQUALS_ASGN_NODES + SHORTHAND_ASGN_NODES
|
13
|
+
|
14
|
+
# http://phrogz.net/programmingruby/language.html#table_18.4
|
15
|
+
# Backtick is added last just to help editors parse this code.
|
16
|
+
OPERATOR_METHODS = %w(
|
17
|
+
| ^ & <=> == === =~ > >= < <= << >>
|
18
|
+
+ - * / % ** ~ +@ -@ [] []= ! != !~
|
19
|
+
).map(&:to_sym) + [:'`']
|
10
20
|
|
11
21
|
module_function
|
12
22
|
|
23
|
+
def operator?(symbol)
|
24
|
+
OPERATOR_METHODS.include?(symbol)
|
25
|
+
end
|
26
|
+
|
13
27
|
def strip_quotes(str)
|
14
28
|
if str[0] == '"' || str[0] == "'"
|
15
29
|
str[0] = ''
|
@@ -40,7 +40,7 @@ module Rubocop
|
|
40
40
|
ZERO_ARITY_SUPER_TYPE = :zsuper
|
41
41
|
|
42
42
|
TWISTED_SCOPE_TYPES = [:block, :class, :sclass, :defs].freeze
|
43
|
-
SCOPE_TYPES = (TWISTED_SCOPE_TYPES + [:module, :def]).freeze
|
43
|
+
SCOPE_TYPES = (TWISTED_SCOPE_TYPES + [:top_level, :module, :def]).freeze
|
44
44
|
|
45
45
|
def variable_table
|
46
46
|
@variable_table ||= VariableTable.new(self)
|
@@ -50,14 +50,19 @@ module Rubocop
|
|
50
50
|
def inspect_variables(root_node)
|
51
51
|
return unless root_node
|
52
52
|
|
53
|
-
# Wrap
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
# Wrap the root node with :top_level scope node.
|
54
|
+
top_level_node = wrap_with_top_level_node(root_node)
|
55
|
+
|
56
|
+
inspect_variables_in_scope(top_level_node)
|
57
|
+
end
|
57
58
|
|
58
|
-
|
59
|
+
def wrap_with_top_level_node(node)
|
60
|
+
# This is a custom node type, not defined in Parser.
|
61
|
+
Parser::AST::Node.new(:top_level, [node])
|
59
62
|
end
|
60
63
|
|
64
|
+
module_function :wrap_with_top_level_node
|
65
|
+
|
61
66
|
# This is called for each scope recursively.
|
62
67
|
def inspect_variables_in_scope(scope_node)
|
63
68
|
variable_table.push_scope(scope_node)
|
@@ -26,9 +26,10 @@ module Rubocop
|
|
26
26
|
|
27
27
|
def body_node
|
28
28
|
child_index = case @node.type
|
29
|
-
when :
|
30
|
-
when :
|
31
|
-
when :
|
29
|
+
when :top_level then 0
|
30
|
+
when :module, :sclass then 1
|
31
|
+
when :def, :class, :block then 2
|
32
|
+
when :defs then 3
|
32
33
|
end
|
33
34
|
|
34
35
|
@node.children[child_index]
|
@@ -20,15 +20,10 @@ module Rubocop
|
|
20
20
|
|
21
21
|
target_files.each do |file|
|
22
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)
|
23
|
+
offences = process_file(file, config_store)
|
28
24
|
|
29
25
|
any_failed = true unless offences.empty?
|
30
26
|
inspected_files << file
|
31
|
-
formatter_set.file_finished(file, offences.freeze)
|
32
27
|
end
|
33
28
|
|
34
29
|
formatter_set.finished(inspected_files.freeze)
|
@@ -49,6 +44,27 @@ module Rubocop
|
|
49
44
|
|
50
45
|
private
|
51
46
|
|
47
|
+
def process_file(file, config_store)
|
48
|
+
puts "Scanning #{file}" if @options[:debug]
|
49
|
+
offences = []
|
50
|
+
formatter_set.file_started(file, {})
|
51
|
+
|
52
|
+
# When running with --auto-correct, we need to inspect the file (which
|
53
|
+
# includes writing a corrected version of it) until no more corrections
|
54
|
+
# are made. This is because automatic corrections can introduce new
|
55
|
+
# offences. In the normal case the loop is only executed once.
|
56
|
+
loop do
|
57
|
+
new_offences = inspect_file(file, config_store)
|
58
|
+
unique_new = new_offences.reject { |n| offences.include?(n) }
|
59
|
+
|
60
|
+
offences += unique_new
|
61
|
+
break unless unique_new.any?(&:corrected?)
|
62
|
+
end
|
63
|
+
|
64
|
+
formatter_set.file_finished(file, offences.freeze)
|
65
|
+
offences
|
66
|
+
end
|
67
|
+
|
52
68
|
def inspect_file(file, config_store)
|
53
69
|
config = config_store.for(file)
|
54
70
|
team = Cop::Team.new(mobilized_cop_classes(config), config, @options)
|
@@ -9,7 +9,7 @@ module Rubocop
|
|
9
9
|
def report_file(file, offences)
|
10
10
|
offences.each do |o|
|
11
11
|
output.printf("%s:%d:%d: %s: %s\n",
|
12
|
-
smart_path(file)
|
12
|
+
cyan(smart_path(file)), o.line, o.real_column,
|
13
13
|
colored_severity_code(o), message(o))
|
14
14
|
|
15
15
|
source_line = o.location.source_line
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Formatter
|
5
|
+
# This mix-in module provides string coloring methods for terminals.
|
6
|
+
# It automatically disables coloring if coloring is disabled in the process
|
7
|
+
# globally or the formatter's output is not a terminal.
|
8
|
+
module Colorizable
|
9
|
+
def rainbow
|
10
|
+
@rainbow ||= begin
|
11
|
+
rainbow = Rainbow.new
|
12
|
+
rainbow.enabled = false unless output.tty?
|
13
|
+
rainbow
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def colorize(string, *args)
|
18
|
+
rainbow.wrap(string).color(*args)
|
19
|
+
end
|
20
|
+
|
21
|
+
[
|
22
|
+
:black,
|
23
|
+
:red,
|
24
|
+
:green,
|
25
|
+
:yellow,
|
26
|
+
:blue,
|
27
|
+
:magenta,
|
28
|
+
:cyan,
|
29
|
+
:white
|
30
|
+
].each do |color|
|
31
|
+
define_method(color) do |string|
|
32
|
+
colorize(string, color)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -6,35 +6,56 @@ module Rubocop
|
|
6
6
|
# detected any offences are configured to not detect the offence.
|
7
7
|
class DisabledConfigFormatter < BaseFormatter
|
8
8
|
HEADING =
|
9
|
-
['# This configuration was generated by `rubocop --auto-gen-config
|
9
|
+
['# This configuration was generated by `rubocop --auto-gen-config`',
|
10
|
+
"# on #{Time.now} using RuboCop version #{Version.version}.",
|
10
11
|
'# The point is for the user to remove these configuration records',
|
11
|
-
'# one by one as the offences are removed from the code base.'
|
12
|
+
'# one by one as the offences are removed from the code base.',
|
13
|
+
'# Note that changes in the inspected code, or installation of new',
|
14
|
+
'# versions of RuboCop, may require this file to be generated again.']
|
12
15
|
.join("\n")
|
13
16
|
|
14
17
|
@config_to_allow_offences = {}
|
15
18
|
|
19
|
+
COPS = Cop::Cop.all.group_by { |c| c.cop_name }
|
20
|
+
|
16
21
|
class << self
|
17
22
|
attr_accessor :config_to_allow_offences
|
18
23
|
end
|
19
24
|
|
20
25
|
def file_finished(file, offences)
|
21
|
-
@cops_with_offences ||=
|
22
|
-
offences.each { |o| @cops_with_offences[o.cop_name]
|
26
|
+
@cops_with_offences ||= Hash.new(0)
|
27
|
+
offences.each { |o| @cops_with_offences[o.cop_name] += 1 }
|
23
28
|
end
|
24
29
|
|
25
30
|
def finished(inspected_files)
|
26
31
|
output.puts HEADING
|
27
|
-
@cops_with_offences.
|
32
|
+
@cops_with_offences.sort.each do |cop_name, offence_count|
|
28
33
|
output.puts
|
29
|
-
output.puts "#{cop_name}:"
|
30
34
|
cfg = self.class.config_to_allow_offences[cop_name]
|
31
35
|
cfg ||= { 'Enabled' => false }
|
36
|
+
output_cop_comments(output, cfg, cop_name, offence_count)
|
37
|
+
output.puts "#{cop_name}:"
|
32
38
|
cfg.each { |key, value| output.puts " #{key}: #{value}" }
|
33
39
|
end
|
34
40
|
puts "Created #{output.path}."
|
35
41
|
puts "Run rubocop with --config #{output.path}, or"
|
36
42
|
puts "add inherit_from: #{output.path} in a .rubocop.yml file."
|
37
43
|
end
|
44
|
+
|
45
|
+
def output_cop_comments(output, cfg, cop_name, offence_count)
|
46
|
+
output.puts "# Offence count: #{offence_count}"
|
47
|
+
if COPS[cop_name] && COPS[cop_name].first.new.support_autocorrect?
|
48
|
+
output.puts '# Cop supports --auto-correct.'
|
49
|
+
end
|
50
|
+
|
51
|
+
default_cfg = Rubocop::ConfigLoader.default_configuration[cop_name]
|
52
|
+
if default_cfg
|
53
|
+
params = default_cfg.keys - %w(Description Enabled) - cfg.keys
|
54
|
+
unless params.empty?
|
55
|
+
output.puts "# Configuration parameters: #{params.join(', ')}."
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
38
59
|
end
|
39
60
|
end
|
40
61
|
end
|
@@ -1,11 +1,15 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
+
require 'rubocop/formatter/colorizable'
|
4
|
+
|
3
5
|
module Rubocop
|
4
6
|
module Formatter
|
5
7
|
# A basic formatter that displays only files with offences.
|
6
8
|
# Offences are displayed at compact form - just the
|
7
9
|
# location of the problem and the associated message.
|
8
10
|
class SimpleTextFormatter < BaseFormatter
|
11
|
+
include Colorizable
|
12
|
+
|
9
13
|
COLOR_FOR_SEVERITY = {
|
10
14
|
refactor: :yellow,
|
11
15
|
convention: :yellow,
|
@@ -32,7 +36,7 @@ module Rubocop
|
|
32
36
|
end
|
33
37
|
|
34
38
|
def report_file(file, offences)
|
35
|
-
output.puts "== #{smart_path(file)} =="
|
39
|
+
output.puts yellow("== #{smart_path(file)} ==")
|
36
40
|
|
37
41
|
offences.each do |o|
|
38
42
|
output.printf("%s:%3d:%3d: %s\n",
|
@@ -47,14 +51,14 @@ module Rubocop
|
|
47
51
|
|
48
52
|
offences_text = pluralize(offence_count, 'offence', no_for_zero: true)
|
49
53
|
offences_text << ' detected'
|
50
|
-
summary << offences_text
|
54
|
+
summary << colorize(offences_text, offence_count.zero? ? :green : :red)
|
51
55
|
|
52
56
|
if correction_count > 0
|
53
57
|
summary << ', '
|
54
58
|
correction_text = pluralize(correction_count, 'offence')
|
55
59
|
correction_text << ' corrected'
|
56
60
|
color = correction_count == offence_count ? :green : :cyan
|
57
|
-
summary << correction_text
|
61
|
+
summary << colorize(correction_text, color)
|
58
62
|
end
|
59
63
|
|
60
64
|
output.puts
|
@@ -78,11 +82,11 @@ module Rubocop
|
|
78
82
|
|
79
83
|
def colored_severity_code(offence)
|
80
84
|
color = COLOR_FOR_SEVERITY[offence.severity]
|
81
|
-
offence.severity_code
|
85
|
+
colorize(offence.severity_code, color)
|
82
86
|
end
|
83
87
|
|
84
88
|
def message(offence)
|
85
|
-
message = offence.corrected? ? '[Corrected] '
|
89
|
+
message = offence.corrected? ? green('[Corrected] ') : ''
|
86
90
|
message << offence.message
|
87
91
|
end
|
88
92
|
|
data/lib/rubocop/options.rb
CHANGED
@@ -30,6 +30,7 @@ module Rubocop
|
|
30
30
|
require f
|
31
31
|
end
|
32
32
|
|
33
|
+
add_flags_with_optional_args(opts)
|
33
34
|
add_boolean_flags(opts)
|
34
35
|
end.parse!(args)
|
35
36
|
|
@@ -74,15 +75,29 @@ module Rubocop
|
|
74
75
|
end
|
75
76
|
end
|
76
77
|
|
78
|
+
def add_flags_with_optional_args(opts)
|
79
|
+
option(opts, '--show-cops [cop1,cop2,...]',
|
80
|
+
'Shows the given cops, or all cops by',
|
81
|
+
'default, and their configurations for the',
|
82
|
+
'current directory.') do |list|
|
83
|
+
@options[:show_cops] = list.nil? ? [] : list.split(',')
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
77
87
|
def add_boolean_flags(opts)
|
78
|
-
option(opts, '--show-cops',
|
79
|
-
'Shows cops and their config for the',
|
80
|
-
'current directory.')
|
81
88
|
option(opts, '-d', '--debug', 'Display debug info.')
|
89
|
+
option(opts,
|
90
|
+
'-D', '--display-cop-names',
|
91
|
+
'Display cop names in offence messages.')
|
82
92
|
option(opts, '-R', '--rails', 'Run extra Rails cops.')
|
83
93
|
option(opts, '-l', '--lint', 'Run only lint cops.')
|
84
94
|
option(opts, '-a', '--auto-correct', 'Auto-correct offences.')
|
85
|
-
|
95
|
+
|
96
|
+
@options[:color] = true
|
97
|
+
opts.on('-n', '--no-color', 'Disable color output.') do
|
98
|
+
@options[:color] = false
|
99
|
+
end
|
100
|
+
|
86
101
|
option(opts, '-v', '--version', 'Display version.')
|
87
102
|
option(opts, '-V', '--verbose-version', 'Display verbose version.')
|
88
103
|
end
|
@@ -41,6 +41,10 @@ module Rubocop
|
|
41
41
|
# ruby source files
|
42
42
|
# @return [Array] Array of filenames
|
43
43
|
def target_files_in_dir(base_dir = Dir.pwd)
|
44
|
+
# Support Windows: Backslashes from command-line -> forward slashes
|
45
|
+
if File::ALT_SEPARATOR
|
46
|
+
base_dir.gsub!(File::ALT_SEPARATOR, File::SEPARATOR)
|
47
|
+
end
|
44
48
|
files = Dir["#{base_dir}/**/*"].select { |path| FileTest.file?(path) }
|
45
49
|
base_dir_config = @config_store.for("#{base_dir}/foobar.rb")
|
46
50
|
|
data/lib/rubocop/version.rb
CHANGED
data/rubocop-todo.yml
CHANGED
@@ -1,12 +1,20 @@
|
|
1
|
-
# This configuration was generated by `rubocop --auto-gen-config
|
1
|
+
# This configuration was generated by `rubocop --auto-gen-config`
|
2
|
+
# on 2014-01-15 12:15:45 +0100 using RuboCop version 0.16.0.
|
2
3
|
# The point is for the user to remove these configuration records
|
3
4
|
# one by one as the offences are removed from the code base.
|
5
|
+
# Note that changes in the inspected code, or installation of new
|
6
|
+
# versions of RuboCop, may require this file to be generated again.
|
4
7
|
|
8
|
+
# Offence count: 6
|
9
|
+
# Configuration parameters: CountComments.
|
5
10
|
ClassLength:
|
6
|
-
Max:
|
11
|
+
Max: 124
|
7
12
|
|
13
|
+
# Offence count: 14
|
8
14
|
CyclomaticComplexity:
|
9
15
|
Max: 10
|
10
16
|
|
17
|
+
# Offence count: 97
|
18
|
+
# Configuration parameters: CountComments.
|
11
19
|
MethodLength:
|
12
20
|
Max: 20
|