rubocop 0.13.0 → 0.13.1

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 (35) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +18 -0
  3. data/config/default.yml +4 -0
  4. data/config/enabled.yml +13 -9
  5. data/lib/rubocop.rb +5 -1
  6. data/lib/rubocop/backports/bsearch.rb +39 -0
  7. data/lib/rubocop/cop/lint/useless_comparison.rb +2 -0
  8. data/lib/rubocop/cop/lint/useless_setter_call.rb +88 -13
  9. data/lib/rubocop/cop/style/align_array.rb +2 -20
  10. data/lib/rubocop/cop/style/align_parameters.rb +2 -20
  11. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  12. data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -1
  13. data/lib/rubocop/cop/style/autocorrect_alignment.rb +32 -0
  14. data/lib/rubocop/cop/style/character_literal.rb +5 -13
  15. data/lib/rubocop/cop/style/final_newline.rb +2 -2
  16. data/lib/rubocop/cop/style/hash_syntax.rb +10 -3
  17. data/lib/rubocop/cop/style/lambda_call.rb +39 -0
  18. data/lib/rubocop/cop/style/nil_comparison.rb +2 -0
  19. data/lib/rubocop/cop/style/redundant_self.rb +48 -2
  20. data/lib/rubocop/cop/style/string_help.rb +27 -0
  21. data/lib/rubocop/cop/style/string_literals.rb +5 -13
  22. data/lib/rubocop/cop/style/surrounding_space.rb +17 -5
  23. data/lib/rubocop/version.rb +1 -1
  24. data/rubocop.gemspec +0 -1
  25. data/spec/rubocop/cli_spec.rb +909 -914
  26. data/spec/rubocop/cop/lint/useless_comparison_spec.rb +5 -0
  27. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +41 -0
  28. data/spec/rubocop/cop/style/final_newline_spec.rb +6 -0
  29. data/spec/rubocop/cop/style/hash_syntax_spec.rb +26 -21
  30. data/spec/rubocop/cop/style/lambda_call_spec.rb +29 -0
  31. data/spec/rubocop/cop/style/nil_comparison_spec.rb +5 -0
  32. data/spec/rubocop/cop/style/redundant_self_spec.rb +18 -0
  33. data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +68 -0
  34. metadata +20 -50
  35. data/spec/rubocop/cop/style/space_around_braces_spec.rb +0 -50
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 93f1416caf62b3dbb7452feeeb5be8b017bf3926
4
+ data.tar.gz: 6de08a58bf2d425bb08411fd33f0e20e9515bba6
5
+ SHA512:
6
+ metadata.gz: 59017f0bc97a22ee8b1ed0795d14cafa479992b8e5338af31c8db0dd0dc3c9b0a999799a2a01cf6843e6f39c10e03cacd87c88bdcf3d8455962f1187be033fc2
7
+ data.tar.gz: a54661cf2a09e2078ed40b5fb0e3649356d82a2fb2a6f4821b9837e1c1975a0df23c4f25178b7ed782f17b04634d696cfc87316e2f587929d9ead94c195d701b
data/CHANGELOG.md CHANGED
@@ -2,6 +2,24 @@
2
2
 
3
3
  ## master (unreleased)
4
4
 
5
+ ## 0.13.1 (19/09/2013)
6
+
7
+ ### New features
8
+
9
+ * `HashSyntax` cop does auto-correction.
10
+ * Allow calls to self to fix name clash with argument [#484](https://github.com/bbatsov/rubocop/pull/484)
11
+ * Renamed `SpaceAroundBraces` to `SpaceAroundBlockBraces`.
12
+ * `SpaceAroundBlockBraces` now has a `NoSpaceBeforeBlockParameters` config option to enforce a style for blocks with parameters like `{|foo| puts }`.
13
+ * New cop `LambdaCall` tracks uses of the obscure `lambda.(...)` syntax
14
+
15
+ ### Bugs fixed
16
+
17
+ * Fix crash on empty input file in `FinalNewline`.
18
+ * [#485](https://github.com/bbatsov/rubocop/issues/485) - Fix crash on multiple-assignment and op-assignment in `UselessSetterCall`.
19
+ * [#497](https://github.com/bbatsov/rubocop/issues/497) - Fix crash in `UselessComparison` and `NilComparison`
20
+
21
+ ## 0.13.0 (13/09/2013)
22
+
5
23
  ### New features
6
24
 
7
25
  * New configuration parameter `AllowAdjacentOneLineDefs` for `EmptyLineBetweenDefs`.
data/config/default.yml CHANGED
@@ -22,6 +22,10 @@ MethodLength:
22
22
  CountComments: false # count full line comments?
23
23
  Max: 10
24
24
 
25
+ SpaceAroundBlockBraces:
26
+ Description: 'Use spaces around { and before } in blocks.'
27
+ NoSpaceBeforeBlockParameters: false
28
+
25
29
  ParameterLists:
26
30
  Description: 'Avoid parameter lists longer than five parameters.'
27
31
  Max: 5
data/config/enabled.yml CHANGED
@@ -44,8 +44,8 @@ SpaceAroundOperators:
44
44
  Description: 'Use spaces around operators.'
45
45
  Enabled: true
46
46
 
47
- SpaceAroundBraces:
48
- Description: 'Use spaces around { and before }.'
47
+ SpaceAroundBlockBraces:
48
+ Description: 'Use spaces around { and before } in blocks.'
49
49
  Enabled: true
50
50
 
51
51
  SpaceInsideParens:
@@ -127,7 +127,7 @@ Blocks:
127
127
 
128
128
 
129
129
  ParameterLists:
130
- Description: Avoid parameter lists longer than three or four parameters.
130
+ Description: 'Avoid parameter lists longer than three or four parameters.'
131
131
  Enabled: true
132
132
 
133
133
  StringLiterals:
@@ -194,6 +194,10 @@ Lambda:
194
194
  Description: 'Use the new lambda literal syntax for single-line blocks.'
195
195
  Enabled: true
196
196
 
197
+ LambdaCall:
198
+ Description: 'Use lambda.call(...) instead of lambda.(...).'
199
+ Enabled: true
200
+
197
201
  Proc:
198
202
  Description: 'Use proc instead of Proc.new.'
199
203
  Enabled: true
@@ -239,7 +243,7 @@ VariableInterpolation:
239
243
  Enabled: true
240
244
 
241
245
  Semicolon:
242
- Desription: "Don't use semicolons to terminate expressions."
246
+ Description: "Don't use semicolons to terminate expressions."
243
247
  Enabled: true
244
248
 
245
249
  FavorSprintf:
@@ -441,11 +445,11 @@ SpaceAfterMethodName:
441
445
  Enabled: true
442
446
 
443
447
  HashMethods:
444
- Description: Checks for use of deprecated Hash methods.
448
+ Description: 'Checks for use of deprecated Hash methods.'
445
449
  Enabled: true
446
450
 
447
451
  MultilineBlockChain:
448
- Description: Avoid multi-line chains of blocks.
452
+ Description: 'Avoid multi-line chains of blocks.'
449
453
  Enabled: true
450
454
 
451
455
  AlignHash:
@@ -461,7 +465,7 @@ AlignArray:
461
465
  Enabled: true
462
466
 
463
467
  SpaceBeforeModifierKeyword:
464
- Description: Put a space before the modifier keyword.
468
+ Description: 'Put a space before the modifier keyword.'
465
469
  Enabled: true
466
470
 
467
471
  #################### Lint ################################
@@ -532,11 +536,11 @@ ParenthesesAsGroupedExpression:
532
536
  ##################### Rails ##################################
533
537
 
534
538
  HasAndBelongsToMany:
535
- Description: Prefer has_many :through to has_and_belongs_to_many.
539
+ Description: 'Prefer has_many :through to has_and_belongs_to_many.'
536
540
  Enabled: true
537
541
 
538
542
  ReadAttribute:
539
- Description: Prefer self[:attribute] over read_attribute(:attribute).
543
+ Description: 'Prefer self[:attribute] over read_attribute(:attribute).'
540
544
  Enabled: true
541
545
 
542
546
  Validation:
data/lib/rubocop.rb CHANGED
@@ -1,12 +1,13 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'backports/2.0.0/array/bsearch'
4
3
  require 'rainbow'
5
4
  require 'English'
6
5
  require 'parser/current'
7
6
  require 'ast/sexp'
8
7
  require 'powerpack'
9
8
 
9
+ require 'rubocop/backports/bsearch'
10
+
10
11
  require 'rubocop/cop/util'
11
12
  require 'rubocop/cop/offence'
12
13
  require 'rubocop/cop/cop'
@@ -41,6 +42,8 @@ require 'rubocop/cop/lint/useless_comparison'
41
42
  require 'rubocop/cop/lint/useless_setter_call'
42
43
  require 'rubocop/cop/lint/void'
43
44
 
45
+ require 'rubocop/cop/style/autocorrect_alignment'
46
+ require 'rubocop/cop/style/string_help'
44
47
  require 'rubocop/cop/style/access_control'
45
48
  require 'rubocop/cop/style/alias'
46
49
  require 'rubocop/cop/style/align_array'
@@ -87,6 +90,7 @@ require 'rubocop/cop/style/if_then_else'
87
90
  require 'rubocop/cop/style/if_with_semicolon'
88
91
  require 'rubocop/cop/style/indentation_width'
89
92
  require 'rubocop/cop/style/lambda'
93
+ require 'rubocop/cop/style/lambda_call'
90
94
  require 'rubocop/cop/style/leading_comment_space'
91
95
  require 'rubocop/cop/style/line_length'
92
96
  require 'rubocop/cop/style/method_and_variable_snake_case'
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ # rubocop:disable LineLength
4
+ # rubocop:disable BlockNesting
5
+
6
+ unless Array.method_defined? :bsearch
7
+ # Backport of Array#bsearch from Ruby 2.0.
8
+ # Needed to boost Parser's performance under Ruby 1.9.
9
+ class Array
10
+ def bsearch
11
+ return to_enum(__method__) unless block_given?
12
+ from = 0
13
+ to = size - 1
14
+ satisfied = nil
15
+ while from <= to
16
+ midpoint = (from + to).div(2)
17
+ result = yield(cur = self[midpoint])
18
+ case result
19
+ when Numeric
20
+ return cur if result == 0
21
+ result = result < 0
22
+ when true
23
+ satisfied = cur
24
+ when nil, false
25
+ # nothing to do
26
+ else
27
+ fail TypeError, "wrong argument type #{result.class} (must be numeric, true, false or nil)"
28
+ end
29
+
30
+ if result
31
+ to = midpoint - 1
32
+ else
33
+ from = midpoint + 1
34
+ end
35
+ end
36
+ satisfied
37
+ end
38
+ end
39
+ end
@@ -14,6 +14,8 @@ module Rubocop
14
14
  OPS = %w(== === != < > <= >= <=>)
15
15
 
16
16
  def on_send(node)
17
+ # lambda.() does not have a selector
18
+ return unless node.loc.selector
17
19
  op = node.loc.selector.source
18
20
 
19
21
  if OPS.include?(op)
@@ -14,6 +14,7 @@ module Rubocop
14
14
  # end
15
15
  class UselessSetterCall < Cop
16
16
  MSG = 'Useless setter call to local variable %s.'
17
+ ASSIGNMENT_TYPES = [:lvasgn, :ivasgn, :cvasgn, :gvasgn].freeze
17
18
 
18
19
  def on_def(node)
19
20
  _name, args, body = *node
@@ -42,9 +43,10 @@ module Rubocop
42
43
 
43
44
  return unless setter_call_to_local_variable?(last_expr)
44
45
 
46
+ tracker = MethodVariableTracker.new(args, body)
45
47
  receiver, = *last_expr
46
48
  var_name, = *receiver
47
- return if contains_object_passed_as_argument?(var_name, body, args)
49
+ return if tracker.contain_object_passed_as_argument?(var_name)
48
50
 
49
51
  warning(receiver,
50
52
  :name,
@@ -58,26 +60,99 @@ module Rubocop
58
60
  method =~ /\w=$/
59
61
  end
60
62
 
61
- def contains_object_passed_as_argument?(lvar_name, body, args)
62
- variable_table = {}
63
+ # This class tracks variable assignments in a method body
64
+ # and if a variable contains object passed as argument at the end of
65
+ # the method.
66
+ class MethodVariableTracker
67
+ def initialize(args_node, body_node)
68
+ @args_node = args_node
69
+ @body_node = body_node
70
+ end
71
+
72
+ def contain_object_passed_as_argument?(variable_name)
73
+ return @table[variable_name] if @table
74
+
75
+ @table = {}
76
+
77
+ @args_node.children.each do |arg_node|
78
+ arg_name, = *arg_node
79
+ @table[arg_name] = true
80
+ end
81
+
82
+ scan(@body_node) do |node|
83
+ case node.type
84
+ when :masgn
85
+ process_multiple_assignment(node)
86
+ when :or_asgn, :and_asgn
87
+ process_logical_operator_assignment(node)
88
+ when :op_asgn
89
+ process_binary_operator_assignment(node)
90
+ when *ASSIGNMENT_TYPES
91
+ _, rhs_node = *node
92
+ process_assignment(node, rhs_node)
93
+ end
94
+ end
63
95
 
64
- args.children.each do |arg_node|
65
- arg_name, = *arg_node
66
- variable_table[arg_name] = true
96
+ @table[variable_name]
67
97
  end
68
98
 
69
- on_node([:lvasgn, :ivasgn, :cvasgn, :gvasgn], body) do |asgn_node|
70
- lhs_var_name, rhs_node = *asgn_node
99
+ def scan(node, &block)
100
+ catch(:skip_children) do
101
+ yield node
102
+
103
+ node.children.each do |child|
104
+ next unless child.is_a?(Parser::AST::Node)
105
+ scan(child, &block)
106
+ end
107
+ end
108
+ end
109
+
110
+ def process_multiple_assignment(masgn_node)
111
+ mlhs_node, mrhs_node = *masgn_node
112
+
113
+ mlhs_node.children.each_with_index do |lhs_node, index|
114
+ next unless ASSIGNMENT_TYPES.include?(lhs_node.type)
115
+
116
+ lhs_variable_name, = *lhs_node
117
+ rhs_node = mrhs_node.children[index]
118
+
119
+ if mrhs_node.type == :array && rhs_node
120
+ process_assignment(lhs_variable_name, rhs_node)
121
+ else
122
+ table[lhs_variable_name] = false
123
+ end
124
+ end
125
+
126
+ throw :skip_children
127
+ end
128
+
129
+ def process_logical_operator_assignment(asgn_node)
130
+ lhs_node, rhs_node = *asgn_node
131
+ return unless ASSIGNMENT_TYPES.include?(lhs_node.type)
132
+ process_assignment(lhs_node, rhs_node)
133
+
134
+ throw :skip_children
135
+ end
136
+
137
+ def process_binary_operator_assignment(op_asgn_node)
138
+ lhs_node, = *op_asgn_node
139
+ return unless ASSIGNMENT_TYPES.include?(lhs_node.type)
140
+ lhs_variable_name, = *lhs_node
141
+ @table[lhs_variable_name] = false
142
+
143
+ throw :skip_children
144
+ end
145
+
146
+ def process_assignment(asgn_node, rhs_node)
147
+ lhs_variable_name, = *asgn_node
71
148
 
72
149
  if [:lvar, :ivar, :cvar, :gvar].include?(rhs_node.type)
73
- rhs_var_name, = *rhs_node
74
- variable_table[lhs_var_name] = variable_table[rhs_var_name]
150
+ rhs_variable_name, = *rhs_node
151
+ @table[lhs_variable_name] = @table[rhs_variable_name]
75
152
  else
76
- variable_table[lhs_var_name] = false
153
+ @table[lhs_variable_name] = false
77
154
  end
78
155
  end
79
-
80
- variable_table[lvar_name]
81
156
  end
82
157
  end
83
158
  end
@@ -6,6 +6,8 @@ module Rubocop
6
6
  # Here we check if the elements of a multi-line array literal are
7
7
  # aligned.
8
8
  class AlignArray < Cop
9
+ include AutocorrectAlignment
10
+
9
11
  MSG = 'Align the elements of an array literal if they span more ' +
10
12
  'than one line.'
11
13
 
@@ -21,26 +23,6 @@ module Rubocop
21
23
  end
22
24
  end
23
25
  end
24
-
25
- def autocorrect_action(node)
26
- # We can't use the instance variable inside the lambda. That would
27
- # just give each lambda the same reference and they would all get
28
- # the last value of @column_delta. A local variable fixes the
29
- # problem.
30
- column_delta = @column_delta
31
-
32
- @corrections << lambda do |corrector|
33
- expr = node.loc.expression
34
- if column_delta > 0
35
- corrector.replace(expr, ' ' * column_delta + expr.source)
36
- else
37
- range = Parser::Source::Range.new(expr.source_buffer,
38
- expr.begin_pos + column_delta,
39
- expr.end_pos)
40
- corrector.replace(range, expr.source)
41
- end
42
- end
43
- end
44
26
  end
45
27
  end
46
28
  end
@@ -6,6 +6,8 @@ module Rubocop
6
6
  # Here we check if the parameters on a multi-line method call are
7
7
  # aligned.
8
8
  class AlignParameters < Cop
9
+ include AutocorrectAlignment
10
+
9
11
  MSG = 'Align the parameters of a method call if they span ' +
10
12
  'more than one line.'
11
13
 
@@ -27,26 +29,6 @@ module Rubocop
27
29
  end
28
30
  end
29
31
  end
30
-
31
- def autocorrect_action(node)
32
- # We can't use the instance variable inside the lambda. That would
33
- # just give each lambda the same reference and they would all get
34
- # the last value of @column_delta. A local variable fixes the
35
- # problem.
36
- column_delta = @column_delta
37
-
38
- @corrections << lambda do |corrector|
39
- expr = node.loc.expression
40
- if column_delta > 0
41
- corrector.replace(expr, ' ' * column_delta + expr.source)
42
- else
43
- range = Parser::Source::Range.new(expr.source_buffer,
44
- expr.begin_pos + column_delta,
45
- expr.end_pos)
46
- corrector.replace(range, expr.source)
47
- end
48
- end
49
- end
50
32
  end
51
33
  end
52
34
  end
@@ -10,7 +10,7 @@ module Rubocop
10
10
 
11
11
  def investigate(processed_source)
12
12
  processed_source.comments.each do |comment|
13
- convention(comment, :expression) if comment.text =~ /[^\x00-\x7f]/
13
+ convention(comment, :expression) unless comment.text.ascii_only?
14
14
  end
15
15
  end
16
16
  end
@@ -11,7 +11,7 @@ module Rubocop
11
11
 
12
12
  def investigate(processed_source)
13
13
  processed_source.tokens.each do |t|
14
- if t.type == :tIDENTIFIER && t.text =~ /[^\x00-\x7f]/
14
+ if t.type == :tIDENTIFIER && !t.text.ascii_only?
15
15
  convention(nil, t.pos)
16
16
  end
17
17
  end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ module Style
6
+ # This module does auto-correction of nodes that should just be moved to
7
+ # the left or to the right, amount being determined by the instance
8
+ # variable @column_delta.
9
+ module AutocorrectAlignment
10
+ def autocorrect_action(node)
11
+ # We can't use the instance variable inside the lambda. That would
12
+ # just give each lambda the same reference and they would all get
13
+ # the last value of @column_delta. A local variable fixes the
14
+ # problem.
15
+ column_delta = @column_delta
16
+
17
+ @corrections << lambda do |corrector|
18
+ expr = node.loc.expression
19
+ if column_delta > 0
20
+ corrector.replace(expr, ' ' * column_delta + expr.source)
21
+ else
22
+ range = Parser::Source::Range.new(expr.source_buffer,
23
+ expr.begin_pos + column_delta,
24
+ expr.end_pos)
25
+ corrector.replace(range, expr.source)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end