rubocop 0.9.1 → 0.10.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.

Files changed (149) hide show
  1. data/.travis.yml +3 -1
  2. data/CHANGELOG.md +38 -0
  3. data/README.md +34 -0
  4. data/Rakefile +3 -0
  5. data/config/default.yml +14 -1
  6. data/config/enabled.yml +30 -7
  7. data/lib/rubocop.rb +15 -0
  8. data/lib/rubocop/cli.rb +48 -154
  9. data/lib/rubocop/config.rb +19 -22
  10. data/lib/rubocop/config_store.rb +2 -4
  11. data/lib/rubocop/cop/commissioner.rb +90 -0
  12. data/lib/rubocop/cop/cop.rb +38 -31
  13. data/lib/rubocop/cop/corrector.rb +84 -0
  14. data/lib/rubocop/cop/lint/assignment_in_condition.rb +0 -3
  15. data/lib/rubocop/cop/lint/block_alignment.rb +151 -0
  16. data/lib/rubocop/cop/lint/empty_ensure.rb +18 -0
  17. data/lib/rubocop/cop/lint/end_alignment.rb +0 -124
  18. data/lib/rubocop/cop/lint/end_in_method.rb +0 -2
  19. data/lib/rubocop/cop/lint/ensure_return.rb +3 -3
  20. data/lib/rubocop/cop/lint/eval.rb +0 -2
  21. data/lib/rubocop/cop/lint/handle_exceptions.rb +0 -2
  22. data/lib/rubocop/cop/lint/literal_in_condition.rb +0 -10
  23. data/lib/rubocop/cop/lint/loop.rb +0 -2
  24. data/lib/rubocop/cop/lint/rescue_exception.rb +0 -2
  25. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +2 -2
  26. data/lib/rubocop/cop/lint/unreachable_code.rb +0 -2
  27. data/lib/rubocop/cop/lint/unused_local_variable.rb +2 -2
  28. data/lib/rubocop/cop/lint/void.rb +0 -2
  29. data/lib/rubocop/cop/offence.rb +9 -0
  30. data/lib/rubocop/cop/rails/validation.rb +2 -1
  31. data/lib/rubocop/cop/style/access_control.rb +4 -3
  32. data/lib/rubocop/cop/style/alias.rb +2 -4
  33. data/lib/rubocop/cop/style/align_parameters.rb +0 -2
  34. data/lib/rubocop/cop/style/and_or.rb +4 -6
  35. data/lib/rubocop/cop/style/ascii_comments.rb +2 -2
  36. data/lib/rubocop/cop/style/ascii_identifiers.rb +2 -2
  37. data/lib/rubocop/cop/style/attr.rb +0 -2
  38. data/lib/rubocop/cop/style/avoid_class_vars.rb +0 -1
  39. data/lib/rubocop/cop/style/avoid_for.rb +0 -2
  40. data/lib/rubocop/cop/style/avoid_global_vars.rb +3 -7
  41. data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +0 -2
  42. data/lib/rubocop/cop/style/avoid_perlisms.rb +2 -4
  43. data/lib/rubocop/cop/style/begin_block.rb +0 -2
  44. data/lib/rubocop/cop/style/block_comments.rb +2 -2
  45. data/lib/rubocop/cop/style/block_nesting.rb +3 -3
  46. data/lib/rubocop/cop/style/blocks.rb +0 -2
  47. data/lib/rubocop/cop/style/case_equality.rb +0 -2
  48. data/lib/rubocop/cop/style/case_indentation.rb +0 -2
  49. data/lib/rubocop/cop/style/character_literal.rb +10 -6
  50. data/lib/rubocop/cop/style/class_and_module_camel_case.rb +0 -4
  51. data/lib/rubocop/cop/style/class_methods.rb +1 -1
  52. data/lib/rubocop/cop/style/collection_methods.rb +3 -5
  53. data/lib/rubocop/cop/style/colon_method_call.rb +3 -3
  54. data/lib/rubocop/cop/style/comment_annotation.rb +44 -0
  55. data/lib/rubocop/cop/style/constant_name.rb +0 -2
  56. data/lib/rubocop/cop/style/def_parentheses.rb +0 -8
  57. data/lib/rubocop/cop/style/documentation.rb +6 -2
  58. data/lib/rubocop/cop/style/dot_position.rb +0 -2
  59. data/lib/rubocop/cop/style/empty_line_between_defs.rb +0 -2
  60. data/lib/rubocop/cop/style/empty_lines.rb +10 -8
  61. data/lib/rubocop/cop/style/empty_literal.rb +3 -1
  62. data/lib/rubocop/cop/style/encoding.rb +7 -6
  63. data/lib/rubocop/cop/style/end_block.rb +0 -2
  64. data/lib/rubocop/cop/style/end_of_line.rb +4 -3
  65. data/lib/rubocop/cop/style/favor_join.rb +0 -2
  66. data/lib/rubocop/cop/style/favor_modifier.rb +9 -9
  67. data/lib/rubocop/cop/style/favor_sprintf.rb +0 -2
  68. data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +0 -2
  69. data/lib/rubocop/cop/style/hash_syntax.rb +0 -2
  70. data/lib/rubocop/cop/style/if_then_else.rb +0 -2
  71. data/lib/rubocop/cop/style/lambda.rb +0 -2
  72. data/lib/rubocop/cop/style/leading_comment_space.rb +2 -2
  73. data/lib/rubocop/cop/style/line_continuation.rb +4 -3
  74. data/lib/rubocop/cop/style/line_length.rb +4 -3
  75. data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +4 -3
  76. data/lib/rubocop/cop/style/method_call_parentheses.rb +0 -2
  77. data/lib/rubocop/cop/style/method_length.rb +0 -4
  78. data/lib/rubocop/cop/style/not.rb +0 -2
  79. data/lib/rubocop/cop/style/op_method.rb +0 -2
  80. data/lib/rubocop/cop/style/parameter_lists.rb +0 -2
  81. data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -6
  82. data/lib/rubocop/cop/style/proc.rb +0 -2
  83. data/lib/rubocop/cop/style/reduce_arguments.rb +0 -2
  84. data/lib/rubocop/cop/style/redundant_begin.rb +45 -0
  85. data/lib/rubocop/cop/style/redundant_return.rb +59 -0
  86. data/lib/rubocop/cop/style/redundant_self.rb +83 -0
  87. data/lib/rubocop/cop/style/regexp_literal.rb +0 -2
  88. data/lib/rubocop/cop/style/rescue_modifier.rb +13 -21
  89. data/lib/rubocop/cop/style/semicolon.rb +15 -9
  90. data/lib/rubocop/cop/style/single_line_methods.rb +0 -4
  91. data/lib/rubocop/cop/style/space_after_comma_etc.rb +2 -2
  92. data/lib/rubocop/cop/style/space_after_control_keyword.rb +0 -1
  93. data/lib/rubocop/cop/style/string_literals.rb +5 -2
  94. data/lib/rubocop/cop/style/surrounding_space.rb +106 -91
  95. data/lib/rubocop/cop/style/tab.rb +4 -3
  96. data/lib/rubocop/cop/style/ternary_operator.rb +0 -4
  97. data/lib/rubocop/cop/style/trailing_whitespace.rb +4 -3
  98. data/lib/rubocop/cop/style/trivial_accessors.rb +51 -6
  99. data/lib/rubocop/cop/style/unless_else.rb +0 -2
  100. data/lib/rubocop/cop/style/variable_interpolation.rb +0 -2
  101. data/lib/rubocop/cop/style/when_then.rb +3 -3
  102. data/lib/rubocop/cop/style/while_until_do.rb +3 -5
  103. data/lib/rubocop/cop/style/word_array.rb +0 -2
  104. data/lib/rubocop/cop/util.rb +0 -4
  105. data/lib/rubocop/formatter/file_list_formatter.rb +18 -0
  106. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  107. data/lib/rubocop/processed_source.rb +27 -0
  108. data/lib/rubocop/rake_task.rb +50 -0
  109. data/lib/rubocop/source_parser.rb +105 -0
  110. data/lib/rubocop/target_finder.rb +67 -0
  111. data/lib/rubocop/token.rb +22 -0
  112. data/lib/rubocop/version.rb +1 -1
  113. data/rubocop.gemspec +5 -3
  114. data/spec/project_spec.rb +0 -11
  115. data/spec/rubocop/cli_spec.rb +112 -6
  116. data/spec/rubocop/config_spec.rb +13 -17
  117. data/spec/rubocop/config_store_spec.rb +8 -23
  118. data/spec/rubocop/cops/commissioner_spec.rb +72 -0
  119. data/spec/rubocop/cops/corrector_spec.rb +63 -0
  120. data/spec/rubocop/cops/lint/assignment_in_condition_spec.rb +2 -2
  121. data/spec/rubocop/cops/lint/block_alignment_spec.rb +357 -0
  122. data/spec/rubocop/cops/lint/empty_ensure_spec.rb +33 -0
  123. data/spec/rubocop/cops/lint/end_alignment_spec.rb +0 -263
  124. data/spec/rubocop/cops/lint/ensure_return_spec.rb +6 -9
  125. data/spec/rubocop/cops/offence_spec.rb +28 -0
  126. data/spec/rubocop/cops/style/and_or_spec.rb +21 -11
  127. data/spec/rubocop/cops/style/ascii_identifiers_spec.rb +14 -0
  128. data/spec/rubocop/cops/style/avoid_global_vars_spec.rb +10 -14
  129. data/spec/rubocop/cops/style/character_literal_spec.rb +17 -2
  130. data/spec/rubocop/cops/style/colon_method_call_spec.rb +20 -15
  131. data/spec/rubocop/cops/style/comment_annotation_spec.rb +62 -0
  132. data/spec/rubocop/cops/style/encoding_spec.rb +7 -0
  133. data/spec/rubocop/cops/style/parentheses_around_condition_spec.rb +37 -9
  134. data/spec/rubocop/cops/style/redundant_begin_spec.rb +63 -0
  135. data/spec/rubocop/cops/style/redundant_return_spec.rb +64 -0
  136. data/spec/rubocop/cops/style/redundant_self_spec.rb +76 -0
  137. data/spec/rubocop/cops/style/string_literals_spec.rb +18 -13
  138. data/spec/rubocop/cops/style/trivial_accessors_spec.rb +110 -52
  139. data/spec/rubocop/cops/style/when_then_spec.rb +14 -7
  140. data/spec/rubocop/cops/style/while_until_do_spec.rb +12 -0
  141. data/spec/rubocop/cops/variable_inspector_spec.rb +3 -5
  142. data/spec/rubocop/formatter/file_list_formatter_spec.rb +33 -0
  143. data/spec/rubocop/processed_source_spec.rb +67 -0
  144. data/spec/rubocop/source_parser_spec.rb +141 -0
  145. data/spec/rubocop/target_finder_spec.rb +180 -0
  146. data/spec/rubocop/token_spec.rb +27 -0
  147. data/spec/spec_helper.rb +24 -4
  148. metadata +108 -18
  149. checksums.yaml +0 -7
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ module Lint
6
+ # This cop checks for empty `ensure` blocks
7
+ class EmptyEnsure < Cop
8
+ MSG = 'Empty ensure block detected.'
9
+
10
+ def on_ensure(node)
11
+ _body, ensure_body = *node
12
+
13
+ add_offence(:warning, node.loc.keyword, MSG) unless ensure_body
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -7,160 +7,44 @@ module Rubocop
7
7
  #
8
8
  # For keywords (if, def, etc.) the end is aligned with the start
9
9
  # of the keyword.
10
- # For blocks - with the start of the expression where the block
11
- # is defined.
12
10
  #
13
11
  # @example
14
12
  #
15
13
  # variable = if true
16
14
  # end
17
- #
18
- # variable = lambda do |i|
19
- # i
20
- # end
21
15
  class EndAlignment < Cop
22
16
  MSG = 'end at %d, %d is not aligned with %s at %d, %d'
23
17
 
24
- def initialize
25
- super
26
- @inspected_blocks = []
27
- end
28
-
29
- def inspect(source_buffer, source, tokens, ast, comments)
30
- @inspected_blocks = []
31
- super
32
- end
33
-
34
18
  def on_def(node)
35
19
  check(node)
36
- super
37
20
  end
38
21
 
39
22
  def on_defs(node)
40
23
  check(node)
41
- super
42
24
  end
43
25
 
44
26
  def on_class(node)
45
27
  check(node)
46
- super
47
28
  end
48
29
 
49
30
  def on_module(node)
50
31
  check(node)
51
- super
52
32
  end
53
33
 
54
34
  def on_if(node)
55
35
  check(node) if node.loc.respond_to?(:end)
56
- super
57
36
  end
58
37
 
59
38
  def on_while(node)
60
39
  check(node)
61
- super
62
40
  end
63
41
 
64
42
  def on_until(node)
65
43
  check(node)
66
- super
67
- end
68
-
69
- # Block related alignments
70
-
71
- def on_block(node)
72
- return if already_processed_node?(node)
73
- check_block_alignment(node.loc.expression, node.loc)
74
- super
75
- end
76
-
77
- def on_and(node)
78
- return if already_processed_node?(node)
79
-
80
- _left, right = *node
81
- if right.type == :block
82
- check_block_alignment(node.loc.expression, right.loc)
83
- @inspected_blocks << right
84
- end
85
- super
86
- end
87
-
88
- alias_method :on_or, :on_and
89
-
90
- def on_lvasgn(node)
91
- _, children = *node
92
- process_block_assignment(node, children)
93
- super
94
- end
95
-
96
- alias_method :on_ivasgn, :on_lvasgn
97
- alias_method :on_cvasgn, :on_lvasgn
98
- alias_method :on_gvasgn, :on_lvasgn
99
- alias_method :on_and_asgn, :on_lvasgn
100
- alias_method :on_or_asgn, :on_lvasgn
101
-
102
- def on_casgn(node)
103
- _, _, children = *node
104
- process_block_assignment(node, children)
105
- super
106
- end
107
-
108
- def on_op_asgn(node)
109
- variable, _op, args = *node
110
- process_block_assignment(variable, args)
111
- super
112
- end
113
-
114
- def on_send(node)
115
- _receiver, _method, *args = *node
116
- process_block_assignment(node, args.last)
117
- super
118
- end
119
-
120
- def on_masgn(node)
121
- variables, args = *node
122
- process_block_assignment(variables, args)
123
- super
124
44
  end
125
45
 
126
46
  private
127
47
 
128
- def process_block_assignment(begin_node, block_node)
129
- return unless block_node
130
- return if already_processed_node?(block_node)
131
-
132
- while block_node.type == :send
133
- receiver, _method, args = *block_node
134
- if receiver && [:block, :send].include?(receiver.type)
135
- block_node = receiver
136
- elsif args && [:block, :send].include?(args.type)
137
- block_node = args
138
- else
139
- break
140
- end
141
- end
142
- if block_node.type == :block
143
- # Align with the expression that is on the same line
144
- # where the block is defined
145
- return if block_is_on_next_line?(begin_node, block_node)
146
-
147
- @inspected_blocks << block_node
148
- check_block_alignment(begin_node.loc.expression, block_node.loc)
149
- end
150
- end
151
-
152
- def check_block_alignment(start_loc, block_loc)
153
- end_loc = block_loc.end
154
- if block_loc.begin.line != end_loc.line &&
155
- start_loc.column != end_loc.column
156
- add_offence(:warning,
157
- end_loc,
158
- sprintf(MSG, end_loc.line, end_loc.column,
159
- start_loc.source.lines.to_a.first.chomp,
160
- start_loc.line, start_loc.column))
161
- end
162
- end
163
-
164
48
  def check(node)
165
49
  # discard modifier forms of if/while/until
166
50
  return unless node.loc.end
@@ -175,14 +59,6 @@ module Rubocop
175
59
  kw_loc.source, kw_loc.line, kw_loc.column))
176
60
  end
177
61
  end
178
-
179
- def already_processed_node?(node)
180
- @inspected_blocks.include?(node)
181
- end
182
-
183
- def block_is_on_next_line?(begin_node, block_node)
184
- begin_node.loc.line != block_node.loc.line
185
- end
186
62
  end
187
63
  end
188
64
  end
@@ -9,12 +9,10 @@ module Rubocop
9
9
 
10
10
  def on_def(node)
11
11
  check(node)
12
- super
13
12
  end
14
13
 
15
14
  def on_defs(node)
16
15
  check(node)
17
- super
18
16
  end
19
17
 
20
18
  private
@@ -10,11 +10,11 @@ module Rubocop
10
10
  def on_ensure(node)
11
11
  _body, ensure_body = *node
12
12
 
13
+ return unless ensure_body
14
+
13
15
  on_node(:return, ensure_body) do |e|
14
16
  add_offence(:warning, e.loc.expression, MSG)
15
- end if ensure_body
16
-
17
- super
17
+ end
18
18
  end
19
19
  end
20
20
  end
@@ -13,8 +13,6 @@ module Rubocop
13
13
  if receiver.nil? && method_name == :eval
14
14
  add_offence(:warning, node.loc.selector, MSG)
15
15
  end
16
-
17
- super
18
16
  end
19
17
  end
20
18
  end
@@ -11,8 +11,6 @@ module Rubocop
11
11
  _exc_list_node, _exc_var_node, body_node = *node
12
12
 
13
13
  add_offence(:warning, node.loc.expression, MSG) unless body_node
14
-
15
- super
16
14
  end
17
15
  end
18
16
  end
@@ -25,32 +25,22 @@ module Rubocop
25
25
 
26
26
  def on_if(node)
27
27
  check_for_literal(node)
28
-
29
- super
30
28
  end
31
29
 
32
30
  def on_while(node)
33
31
  check_for_literal(node)
34
-
35
- super
36
32
  end
37
33
 
38
34
  def on_while_post(node)
39
35
  check_for_literal(node)
40
-
41
- super
42
36
  end
43
37
 
44
38
  def on_until(node)
45
39
  check_for_literal(node)
46
-
47
- super
48
40
  end
49
41
 
50
42
  def on_until_post(node)
51
43
  check_for_literal(node)
52
-
53
- super
54
44
  end
55
45
 
56
46
  private
@@ -10,12 +10,10 @@ module Rubocop
10
10
 
11
11
  def on_while_post(node)
12
12
  register_offence(node)
13
- super
14
13
  end
15
14
 
16
15
  def on_until_post(node)
17
16
  register_offence(node)
18
- super
19
17
  end
20
18
 
21
19
  private
@@ -13,8 +13,6 @@ module Rubocop
13
13
  if rescue_args.any? { |a| targets_exception?(a) }
14
14
  add_offence(:warning, node.location.expression, MSG)
15
15
  end
16
-
17
- super
18
16
  end
19
17
 
20
18
  def targets_exception?(rescue_arg_node)
@@ -12,8 +12,8 @@ module Rubocop
12
12
 
13
13
  MSG = 'Shadowing outer local variable - %s'
14
14
 
15
- def inspect(source_buffer, source, tokens, ast, comments)
16
- inspect_variables(ast)
15
+ def investigate(processed_source)
16
+ inspect_variables(processed_source.ast)
17
17
  end
18
18
 
19
19
  def before_declaring_variable(entry)
@@ -20,8 +20,6 @@ module Rubocop
20
20
  add_offence(:warning, e2.loc.expression, MSG)
21
21
  end
22
22
  end
23
-
24
- super
25
23
  end
26
24
 
27
25
  private
@@ -13,8 +13,8 @@ module Rubocop
13
13
  TYPES_TO_ACCEPT_UNUSED =
14
14
  (ARGUMENT_DECLARATION_TYPES - [:shadowarg]).freeze
15
15
 
16
- def inspect(source_buffer, source, tokens, ast, comments)
17
- inspect_variables(ast)
16
+ def investigate(processed_source)
17
+ inspect_variables(processed_source.ast)
18
18
  end
19
19
 
20
20
  def after_leaving_scope(scope)
@@ -23,8 +23,6 @@ module Rubocop
23
23
  check_for_literal(expr)
24
24
  check_for_var(expr)
25
25
  end
26
-
27
- super
28
26
  end
29
27
 
30
28
  private
@@ -9,6 +9,15 @@ module Rubocop
9
9
  # @api private
10
10
  SEVERITIES = [:refactor, :convention, :warning, :error, :fatal]
11
11
 
12
+ def self.from_diagnostic(diagnostic)
13
+ new(
14
+ diagnostic.level,
15
+ diagnostic.location,
16
+ diagnostic.message,
17
+ 'Syntax'
18
+ )
19
+ end
20
+
12
21
  # @api public
13
22
  #
14
23
  # @!attribute [r] severity
@@ -15,7 +15,8 @@ module Rubocop
15
15
  :validates_length_of,
16
16
  :validates_numericality_of,
17
17
  :validates_presence_of,
18
- :validates_size_of]
18
+ :validates_size_of,
19
+ :validates_uniqueness_of]
19
20
 
20
21
  def on_send(node)
21
22
  receiver, method_name, *_args = *node
@@ -14,7 +14,8 @@ module Rubocop
14
14
  PROTECTED_NODE = s(:send, nil, :protected)
15
15
  PUBLIC_NODE = s(:send, nil, :public)
16
16
 
17
- def inspect(source_buffer, source, tokens, ast, comments)
17
+ def investigate(processed_source)
18
+ ast = processed_source.ast
18
19
  return unless ast
19
20
  on_node([:class, :module, :sclass], ast) do |class_node|
20
21
  class_start_col = class_node.loc.expression.column
@@ -35,8 +36,8 @@ module Rubocop
35
36
 
36
37
  send_line = send_node.loc.line
37
38
 
38
- unless source[send_line].chomp.empty? &&
39
- source[send_line - 2].chomp.empty?
39
+ unless processed_source[send_line].chomp.empty? &&
40
+ processed_source[send_line - 2].chomp.empty?
40
41
  add_offence(:convention,
41
42
  send_node.loc.expression,
42
43
  format(BLANK_MSG, selector))
@@ -8,8 +8,8 @@ module Rubocop
8
8
  class Alias < Cop
9
9
  MSG = 'Use alias_method instead of alias.'
10
10
 
11
- # TODO make this check context aware - alias_method is not
12
- # available outside of classes/modules.
11
+ # TODO: Make this check context aware - alias_method is not
12
+ # available outside of classes/modules.
13
13
  def on_alias(node)
14
14
  # alias_method can't be used with global variables
15
15
  new, old = *node
@@ -19,8 +19,6 @@ module Rubocop
19
19
  add_offence(:convention,
20
20
  node.loc.keyword,
21
21
  MSG)
22
-
23
- super
24
22
  end
25
23
  end
26
24
  end
@@ -30,8 +30,6 @@ module Rubocop
30
30
  prev_arg_line = cur_arg_line
31
31
  end
32
32
  end
33
-
34
- super
35
33
  end
36
34
  end
37
35
  end
@@ -11,14 +11,10 @@ module Rubocop
11
11
 
12
12
  def on_and(node)
13
13
  process_logical_op(node)
14
-
15
- super
16
14
  end
17
15
 
18
16
  def on_or(node)
19
17
  process_logical_op(node)
20
-
21
- super
22
18
  end
23
19
 
24
20
  private
@@ -36,8 +32,10 @@ module Rubocop
36
32
  end
37
33
 
38
34
  def autocorrect_action(node)
39
- replacement = (node.type == :and ? '&&' : '||')
40
- replace(node.loc.operator, replacement)
35
+ @corrections << lambda do |corrector|
36
+ replacement = (node.type == :and ? '&&' : '||')
37
+ corrector.replace(node.loc.operator, replacement)
38
+ end
41
39
  end
42
40
  end
43
41
  end