rubocop 0.4.0 → 0.4.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.

@@ -1,61 +1,80 @@
1
1
  # This is the default configuration file with all checking enabled. It is also
2
2
  # the configuration used to check the rubocop source code.
3
3
 
4
+ # Use UTF-8 as the source file encoding.
4
5
  Encoding:
5
6
  Enabled: true
6
7
 
8
+ # Limit lines to 80 characters.
7
9
  LineLength:
8
10
  Enabled: true
9
11
  Max: 79
10
12
 
13
+ # No hard tabs.
11
14
  Tab:
12
15
  Enabled: true
13
16
 
17
+ # Avoid trailing whitespace.
14
18
  TrailingWhitespace:
15
19
  Enabled: true
16
20
 
21
+ # Use two spaces per indentation level.
17
22
  Indentation:
18
23
  Enabled: true
19
24
 
25
+ # Use empty lines between defs and to break up a method into logical paragraphs.
20
26
  EmptyLines:
21
27
  Enabled: true
22
28
 
29
+ # Use spaces around operators.
23
30
  SpaceAroundOperators:
24
31
  Enabled: true
25
32
 
33
+ # Use spaces around { and before }.
26
34
  SpaceAroundBraces:
27
35
  Enabled: true
28
36
 
37
+ # No spaces after ( or before ).
29
38
  SpaceInsideParens:
30
39
  Enabled: true
31
40
 
41
+ # No spaces after [ or before ].
32
42
  SpaceInsideBrackets:
33
43
  Enabled: true
34
44
 
45
+ # Use spaces after commas.
35
46
  SpaceAfterComma:
36
47
  Enabled: true
37
48
 
49
+ # Use spaces after semicolons.
38
50
  SpaceAfterSemicolon:
39
51
  Enabled: true
40
52
 
53
+ # Use spaces after colons.
41
54
  SpaceAfterColon:
42
55
  Enabled: true
43
56
 
57
+ # Prefer symbols instead of strings as hash keys.
44
58
  HashSyntax:
45
59
  Enabled: true
46
60
 
61
+ # Use Unix-style line endings.
47
62
  EndOfLine:
48
63
  Enabled: true
49
64
 
65
+ # Add underscores to large numeric literals to improve their readability.
50
66
  NumericLiterals:
51
67
  Enabled: true
52
68
 
69
+ # Align the parameters of a method call if they span more than one line.
53
70
  AlignParameters:
54
71
  Enabled: true
55
72
 
73
+ # Use def with parentheses when there are arguments.
56
74
  DefWithParentheses:
57
75
  Enabled: true
58
76
 
77
+ # Omit the parentheses when the method doesn't accept any arguments.
59
78
  DefWithoutParentheses:
60
79
  Enabled: true
61
80
 
@@ -68,62 +87,94 @@ MultilineIfThen:
68
87
  OneLineConditional:
69
88
  Enabled: true
70
89
 
90
+ # Avoid using {...} for multi-line blocks (multiline chaining is always ugly).
71
91
  MultilineBlocks:
72
92
  Enabled: true
73
93
 
94
+ # Prefer {...} over do...end for single-line blocks.
74
95
  SingleLineBlocks:
75
96
  Enabled: true
76
97
 
98
+ # Avoid parameter lists longer than three or four parameters.
77
99
  ParameterLists:
78
100
  Enabled: true
79
101
 
102
+ # Prefer ' strings when you don't need string interpolation or special symbols.
80
103
  StringLiterals:
81
104
  Enabled: true
82
105
 
106
+ # Avoid multi-line ?: (the ternary operator); use if/unless instead.
83
107
  MultilineTernaryOperator:
84
108
  Enabled: true
85
109
 
110
+ # Use one expression per branch in a ternary operator.
86
111
  NestedTernaryOperator:
87
112
  Enabled: true
88
113
 
114
+ # Never use unless with else. Rewrite these with the positive case first.
89
115
  UnlessElse:
90
116
  Enabled: true
91
117
 
118
+ # Use &&/|| for boolean expressions, and/or for control flow.
92
119
  AmpersandsPipesVsAndOr:
93
120
  Enabled: true
94
121
 
122
+ # Use when x then ... for one-line cases.
95
123
  WhenThen:
96
124
  Enabled: true
97
125
 
126
+ # Favor modifier if/unless usage when you have a single-line body.
98
127
  IfUnlessModifier:
99
128
  Enabled: true
100
129
 
130
+ # Favor modifier while/until usage when you have a single-line body.
101
131
  WhileUntilModifier:
102
132
  Enabled: true
103
133
 
134
+ # Favor unless over if for negative conditions (or control flow or).
104
135
  FavorUnlessOverNegatedIf:
105
136
  Enabled: true
106
137
 
138
+ # Favor until over while for negative conditions.
107
139
  FavorUntilOverNegatedWhile:
108
140
  Enabled: true
109
141
 
142
+ # Use spaces around the = operator when assigning default values in def params.
110
143
  SpaceAroundEqualsInParameterDefault:
111
144
  Enabled: true
112
145
 
146
+ # Use the new lambda literal syntax.
113
147
  NewLambdaLiteral:
114
148
  Enabled: true
115
149
 
150
+ # Don't use parentheses around the condition of an if/unless/while.
116
151
  ParenthesesAroundCondition:
117
152
  Enabled: true
118
153
 
154
+ # Use snake_case for symbols, methods and variables.
119
155
  MethodAndVariableSnakeCase:
120
156
  Enabled: true
121
157
 
158
+ # Use CamelCase for classes and modules.
122
159
  ClassAndModuleCamelCase:
123
160
  Enabled: true
124
161
 
162
+ # Causes Ruby to check the syntax of the script and exit without executing.
125
163
  Syntax:
126
164
  Enabled: true
127
165
 
166
+ # Preferred collection methods.
128
167
  CollectionMethods:
129
168
  Enabled: true
169
+
170
+ # Prefer each over for.
171
+ AvoidFor:
172
+ Enabled: true
173
+
174
+ # Avoid Perl-style global variables.
175
+ AvoidPerlisms:
176
+ Enabled: true
177
+
178
+ # Avoid Perl-style regex back references.
179
+ AvoidPerlBackrefs:
180
+ Enabled: true
data/README.md CHANGED
@@ -41,7 +41,7 @@ Command flag | Description
41
41
  `-d/--debug` | Displays some extra debug output
42
42
  `-e/--emacs` | Output the results in Emacs format
43
43
  `-c/--config` | Run with specified config file
44
- `-s/-silent` | Suppress the final summary
44
+ `-s/--silent` | Suppress the final summary
45
45
 
46
46
  ## Configuration
47
47
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
@@ -38,6 +38,9 @@ require 'rubocop/cop/parentheses_around_condition'
38
38
  require 'rubocop/cop/method_and_variable_snake_case'
39
39
  require 'rubocop/cop/class_and_module_camel_case'
40
40
  require 'rubocop/cop/collection_methods'
41
+ require 'rubocop/cop/avoid_for'
42
+ require 'rubocop/cop/avoid_perlisms'
43
+ require 'rubocop/cop/avoid_perl_backrefs'
41
44
 
42
45
  require 'rubocop/report/report'
43
46
  require 'rubocop/report/plain_text'
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class AvoidFor < Cop
6
+ ERROR_MESSAGE = 'Prefer *each* over *for*.'
7
+
8
+ def inspect(file, source, tokens, sexp)
9
+ each(:for, sexp) do |s|
10
+ add_offence(:convention,
11
+ s[1][1][2].lineno,
12
+ ERROR_MESSAGE)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class AvoidPerlBackrefs < Cop
6
+ def inspect(file, source, tokens, sexp)
7
+ each(:@backref, sexp) do |s|
8
+ backref = s[1]
9
+ lineno = s[2].lineno
10
+
11
+ add_offence(
12
+ :convention,
13
+ lineno,
14
+ "Prefer the use of MatchData over #{backref}."
15
+ )
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class AvoidPerlisms < Cop
6
+ PREFERRED_VARS = {
7
+ '$:' => '$LOAD_PATH',
8
+ '$"' => '$LOADED_FEATURES',
9
+ '$0' => '$PROGRAM_NAME',
10
+ '$1' => 'MatchData',
11
+ '$2' => 'MatchData',
12
+ '$3' => 'MatchData',
13
+ '$4' => 'MatchData',
14
+ '$5' => 'MatchData',
15
+ '$6' => 'MatchData',
16
+ '$7' => 'MatchData',
17
+ '$8' => 'MatchData',
18
+ '$9' => 'MatchData',
19
+ '$!' => '$ERROR_INFO',
20
+ '$@' => '$ERROR_POSITION',
21
+ '$;' => '$FIELD_SEPARATOR',
22
+ '$,' => '$OUTPUT_FIELD_SEPARATOR',
23
+ '$/' => '$INPUT_RECORD_SEPARATOR',
24
+ '$\\' => 'OUTPUT_RECORD_SEPARATOR',
25
+ '$.' => '$INPUT_LINE_NUMBER',
26
+ '$_' => '$LAST_READ_LINE',
27
+ '$>' => '$DEFAULT_OUTPUT',
28
+ '$<' => '$DEFAULT_INPUT',
29
+ '$$' => '$PROCESS_ID',
30
+ '$?' => '$CHILD_STATUS',
31
+ '$~' => '$LAST_MATCH_INFO',
32
+ '$=' => '$IGNORECASE',
33
+ '$*' => '$ARGV',
34
+ '$&' => '$MATCH',
35
+ '$`' => '$PREMATCH',
36
+ '$\'' => 'POSTMATCH',
37
+ '$+' => '$LAST_PAREN_MATCH'
38
+ }
39
+
40
+ def inspect(file, source, tokens, sexp)
41
+ each(:@gvar, sexp) do |s|
42
+ global_var = s[1]
43
+
44
+ if PREFERRED_VARS[global_var]
45
+ add_offence(
46
+ :convention,
47
+ s[2].lineno,
48
+ "Prefer #{PREFERRED_VARS[global_var]} over #{global_var}."
49
+ )
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -5,7 +5,7 @@ module Rubocop
5
5
  class Grammar
6
6
  def initialize(tokens)
7
7
  @tokens_without_pos = tokens.map { |t| [t.type, t.text] }
8
- process_embedded_expressions
8
+ process_embedded_expressions if RUBY_VERSION < '2.0'
9
9
  @token_indexes = {}
10
10
  @tokens_without_pos.each_with_index do |t, i|
11
11
  @token_indexes[t] ||= []
@@ -21,7 +21,7 @@ module Rubocop
21
21
  }
22
22
  end
23
23
 
24
- # The string "#{x}" will give the tokens
24
+ # In ruby 1.9.3 and below, the string "#{x}" will give the tokens
25
25
  # [:on_tstring_beg, '"'], [:on_embexpr_beg, '#{'], [:on_ident, 'x'],
26
26
  # [:on_rbrace, '}'], [:on_tstring_end, '"']
27
27
  # which is not so good for us. We want to distinguish between a
@@ -45,7 +45,7 @@ module Rubocop
45
45
  end
46
46
  when :inside_expr
47
47
  case type
48
- when :on_lbrace
48
+ when :on_lbrace, :on_embexpr_end
49
49
  brace_depth += 1
50
50
  when :on_rbrace
51
51
  if brace_depth == 1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "rubocop"
8
- s.version = "0.4.0"
8
+ s.version = "0.4.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Bozhidar Batsov"]
12
- s.date = "2013-04-11"
12
+ s.date = "2013-04-13"
13
13
  s.description = "Automatic Ruby code style checking tool. Aims to enforce the community-driven Ruby Style Guide."
14
14
  s.email = "bozhidar@batsov.com"
15
15
  s.executables = ["rubocop"]
@@ -34,6 +34,9 @@ Gem::Specification.new do |s|
34
34
  "lib/rubocop/cli.rb",
35
35
  "lib/rubocop/cop/align_parameters.rb",
36
36
  "lib/rubocop/cop/ampersands_pipes_vs_and_or.rb",
37
+ "lib/rubocop/cop/avoid_for.rb",
38
+ "lib/rubocop/cop/avoid_perl_backrefs.rb",
39
+ "lib/rubocop/cop/avoid_perlisms.rb",
37
40
  "lib/rubocop/cop/blocks.rb",
38
41
  "lib/rubocop/cop/class_and_module_camel_case.rb",
39
42
  "lib/rubocop/cop/collection_methods.rb",
@@ -72,6 +75,9 @@ Gem::Specification.new do |s|
72
75
  "spec/rubocop/cli_spec.rb",
73
76
  "spec/rubocop/cops/align_parameters_spec.rb",
74
77
  "spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb",
78
+ "spec/rubocop/cops/avoid_for_spec.rb",
79
+ "spec/rubocop/cops/avoid_perl_backrefs_spec.rb",
80
+ "spec/rubocop/cops/avoid_perlisms_spec.rb",
75
81
  "spec/rubocop/cops/class_and_module_camel_case_spec.rb",
76
82
  "spec/rubocop/cops/collection_methods_spec.rb",
77
83
  "spec/rubocop/cops/cop_spec.rb",
@@ -215,7 +215,7 @@ module Rubocop
215
215
  )
216
216
  end
217
217
 
218
- it 'can process a file with an invalide UTF-8 byte sequence' do
218
+ it 'can process a file with an invalid UTF-8 byte sequence' do
219
219
  File.open('example.rb', 'w') do |f|
220
220
  f.puts '# encoding: utf-8'
221
221
  f.puts "# \xf9\x29"
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe AvoidFor do
8
+ let(:af) { AvoidFor.new }
9
+
10
+ it 'registers an offence for for' do
11
+ inspect_source(af,
12
+ 'file.rb',
13
+ ['for n in [1, 2, 3] do',
14
+ 'puts n',
15
+ 'end'])
16
+ expect(af.offences.size).to eq(1)
17
+ expect(af.offences.map(&:message))
18
+ .to eq([AvoidFor::ERROR_MESSAGE])
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe AvoidPerlBackrefs do
8
+ let(:ap) { AvoidPerlBackrefs.new }
9
+
10
+ it 'registers an offence for $1' do
11
+ inspect_source(ap, 'file.rb', ['puts $1'])
12
+ expect(ap.offences.size).to eq(1)
13
+ expect(ap.offences.map(&:message))
14
+ .to eq(['Prefer the use of MatchData over $1.'])
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe AvoidPerlisms do
8
+ let(:ap) { AvoidPerlisms.new }
9
+
10
+ it 'registers an offence for $:' do
11
+ inspect_source(ap, 'file.rb', ['puts $:'])
12
+ expect(ap.offences.size).to eq(1)
13
+ expect(ap.offences.map(&:message))
14
+ .to eq(['Prefer $LOAD_PATH over $:.'])
15
+ end
16
+
17
+ it 'registers an offence for $"' do
18
+ inspect_source(ap, 'file.rb', ['puts $"'])
19
+ expect(ap.offences.size).to eq(1)
20
+ expect(ap.offences.map(&:message))
21
+ .to eq(['Prefer $LOADED_FEATURES over $".'])
22
+ end
23
+
24
+ it 'registers an offence for $0' do
25
+ inspect_source(ap, 'file.rb', ['puts $0'])
26
+ expect(ap.offences.size).to eq(1)
27
+ expect(ap.offences.map(&:message))
28
+ .to eq(['Prefer $PROGRAM_NAME over $0.'])
29
+ end
30
+ end
31
+ end
32
+ end
@@ -9,7 +9,7 @@ module Rubocop
9
9
  tokens = Ripper.lex(EXAMPLE).map { |t| Token.new(*t) }
10
10
  let(:grammar) { Grammar.new(tokens) }
11
11
 
12
- it 'correlates token indices to grammar paths', ruby: 2.0 do
12
+ it 'correlates token indices to grammar paths' do
13
13
  method_block = [:program, :method_add_block]
14
14
  brace_block = method_block + [:brace_block]
15
15
  test_2_0 = [[[1, 0], :on_int, '3'],
@@ -58,13 +58,9 @@ module Rubocop
58
58
  :string_embexpr, varref, :@ident], # z
59
59
  21 => brace_block + [:assign, :string_literal, :string_content,
60
60
  :@tstring_content], # }
61
+ 24 => brace_block
61
62
  }
62
- if RUBY_VERSION >= '2.0'
63
- expect(grammar.correlate(sexp)).to eq(test)
64
- else
65
- test = (test[24] = brace_block)
66
- expect(grammar.correlate(sexp)).to eq(test)
67
- end
63
+ expect(grammar.correlate(sexp)).to eq(test)
68
64
  end
69
65
  end
70
66
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-11 00:00:00.000000000 Z
12
+ date: 2013-04-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: term-ansicolor
@@ -149,6 +149,9 @@ files:
149
149
  - lib/rubocop/cli.rb
150
150
  - lib/rubocop/cop/align_parameters.rb
151
151
  - lib/rubocop/cop/ampersands_pipes_vs_and_or.rb
152
+ - lib/rubocop/cop/avoid_for.rb
153
+ - lib/rubocop/cop/avoid_perl_backrefs.rb
154
+ - lib/rubocop/cop/avoid_perlisms.rb
152
155
  - lib/rubocop/cop/blocks.rb
153
156
  - lib/rubocop/cop/class_and_module_camel_case.rb
154
157
  - lib/rubocop/cop/collection_methods.rb
@@ -187,6 +190,9 @@ files:
187
190
  - spec/rubocop/cli_spec.rb
188
191
  - spec/rubocop/cops/align_parameters_spec.rb
189
192
  - spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb
193
+ - spec/rubocop/cops/avoid_for_spec.rb
194
+ - spec/rubocop/cops/avoid_perl_backrefs_spec.rb
195
+ - spec/rubocop/cops/avoid_perlisms_spec.rb
190
196
  - spec/rubocop/cops/class_and_module_camel_case_spec.rb
191
197
  - spec/rubocop/cops/collection_methods_spec.rb
192
198
  - spec/rubocop/cops/cop_spec.rb
@@ -246,7 +252,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
246
252
  version: '0'
247
253
  segments:
248
254
  - 0
249
- hash: 1039361930821170753
255
+ hash: -395662942126839838
250
256
  required_rubygems_version: !ruby/object:Gem::Requirement
251
257
  none: false
252
258
  requirements: