rubocop 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. data/.rubocop.yml +78 -0
  2. data/CHANGELOG.md +22 -0
  3. data/CONTRIBUTING.md +13 -6
  4. data/Gemfile.lock +3 -3
  5. data/README.md +42 -1
  6. data/VERSION +1 -1
  7. data/bin/rubocop +1 -0
  8. data/lib/rubocop/cli.rb +47 -18
  9. data/lib/rubocop/cop/alias.rb +21 -0
  10. data/lib/rubocop/cop/align_parameters.rb +5 -4
  11. data/lib/rubocop/cop/avoid_class_vars.rb +20 -0
  12. data/lib/rubocop/cop/avoid_for.rb +17 -0
  13. data/lib/rubocop/cop/avoid_perl_backrefs.rb +20 -0
  14. data/lib/rubocop/cop/avoid_perlisms.rb +55 -0
  15. data/lib/rubocop/cop/blocks.rb +1 -1
  16. data/lib/rubocop/cop/cop.rb +1 -1
  17. data/lib/rubocop/cop/favor_sprintf.rb +45 -0
  18. data/lib/rubocop/cop/grammar.rb +3 -3
  19. data/lib/rubocop/cop/offence.rb +3 -1
  20. data/lib/rubocop/cop/semicolon.rb +18 -0
  21. data/lib/rubocop/cop/space_after_comma_etc.rb +2 -1
  22. data/lib/rubocop/cop/string_literals.rb +12 -0
  23. data/lib/rubocop/cop/symbol_snake_case.rb +27 -0
  24. data/lib/rubocop/cop/syntax.rb +14 -2
  25. data/lib/rubocop/cop/variable_interpolation.rb +20 -0
  26. data/lib/rubocop/cop/when_then.rb +3 -0
  27. data/lib/rubocop/report/emacs_style.rb +2 -2
  28. data/lib/rubocop/version.rb +1 -1
  29. data/lib/rubocop.rb +9 -0
  30. data/rubocop.gemspec +21 -2
  31. data/spec/rubocop/cli_spec.rb +68 -1
  32. data/spec/rubocop/cops/alias_spec.rb +27 -0
  33. data/spec/rubocop/cops/align_parameters_spec.rb +14 -5
  34. data/spec/rubocop/cops/avoid_class_vars_spec.rb +20 -0
  35. data/spec/rubocop/cops/avoid_for_spec.rb +22 -0
  36. data/spec/rubocop/cops/avoid_perl_backrefs_spec.rb +18 -0
  37. data/spec/rubocop/cops/avoid_perlisms_spec.rb +32 -0
  38. data/spec/rubocop/cops/favor_sprintf_spec.rb +50 -0
  39. data/spec/rubocop/cops/grammar_spec.rb +3 -7
  40. data/spec/rubocop/cops/offence_spec.rb +6 -0
  41. data/spec/rubocop/cops/semicolon_spec.rb +29 -0
  42. data/spec/rubocop/cops/string_literals_spec.rb +4 -1
  43. data/spec/rubocop/cops/symbol_snake_case_spec.rb +62 -0
  44. data/spec/rubocop/cops/syntax_spec.rb +16 -0
  45. data/spec/rubocop/cops/variable_interpolation_spec.rb +45 -0
  46. data/spec/rubocop/cops/when_then_spec.rb +7 -0
  47. metadata +22 -3
@@ -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
@@ -14,7 +14,9 @@ module Rubocop
14
14
  end
15
15
 
16
16
  def to_s
17
- "#{encode_severity}:%3d: #{message}" % [line_number]
17
+ # we must be wary of messages containing % in them
18
+ sprintf("#{encode_severity}:%3d: #{message.gsub(/%/, '%%')}",
19
+ line_number)
18
20
  end
19
21
 
20
22
  def encode_severity
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class Semicolon < Cop
6
+ ERROR_MESSAGE = 'Do not use semicolons to terminate expressions.'
7
+
8
+ def inspect(file, source, tokens, sexp)
9
+ tokens.each_index do |ix|
10
+ t = tokens[ix]
11
+ if t.type == :on_semicolon
12
+ add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -9,7 +9,8 @@ module Rubocop
9
9
  tokens.each_index do |ix|
10
10
  t = tokens[ix]
11
11
  if kind(t) && !whitespace?(tokens[ix + 1])
12
- add_offence(:convention, t.pos.lineno, ERROR_MESSAGE % kind(t))
12
+ add_offence(:convention, t.pos.lineno,
13
+ sprintf(ERROR_MESSAGE, kind(t)))
13
14
  end
14
15
  end
15
16
  end
@@ -19,6 +19,9 @@ module Rubocop
19
19
  when [:double_quote, :on_embexpr_beg]
20
20
  :embedded_expression
21
21
 
22
+ when [:double_quote, :on_embvar]
23
+ :embedded_variable
24
+
22
25
  when [:double_quote, :on_tstring_end]
23
26
  add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
24
27
  :outside
@@ -26,6 +29,15 @@ module Rubocop
26
29
  when [:embedded_expression, :on_rbrace]
27
30
  :valid_double_quote
28
31
 
32
+ when [:embedded_variable, :on_ivar]
33
+ :valid_double_quote
34
+
35
+ when [:embedded_variable, :on_cvar]
36
+ :valid_double_quote
37
+
38
+ when [:embedded_variable, :on_gvar]
39
+ :valid_double_quote
40
+
29
41
  when [:valid_double_quote, :on_tstring_end]
30
42
  :outside
31
43
  end || state
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class SymbolSnakeCase < Cop
6
+ ERROR_MESSAGE = 'Use snake_case for symbols.'
7
+ SNAKE_CASE = /^@?[\da-z_]+[!?=]?$/
8
+ OPERATORS = %w(! + - % * ** [])
9
+
10
+ def inspect(file, source, tokens, sexp)
11
+ each(:symbol_literal, sexp) do |s|
12
+ symbol_ident = s[1][1][1]
13
+
14
+ # handle a couple of special cases
15
+ next if OPERATORS.include?(symbol_ident)
16
+
17
+ unless symbol_ident =~ SNAKE_CASE
18
+ line_no = s[1][1][2].lineno
19
+ add_offence(:convention,
20
+ line_no,
21
+ ERROR_MESSAGE)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -10,8 +10,20 @@ module Rubocop
10
10
  Open3.capture3('ruby -wc', stdin_data: source.join("\n"))
11
11
 
12
12
  stderr.each_line do |line|
13
- line_no, warning = line.match(/.+:(\d+): warning: (.+)/).captures
14
- add_offence(:warning, line_no.to_i, warning.capitalize) if line_no
13
+ # discard lines that are not containing relevant info
14
+ if line =~ /.+:(\d+): (.+)/
15
+ line_no, severity, message = process_line(line)
16
+ add_offence(severity, line_no, message)
17
+ end
18
+ end
19
+ end
20
+
21
+ def process_line(line)
22
+ line_no, message = line.match(/.+:(\d+): (.+)/).captures
23
+ if message.start_with?('warning: ')
24
+ [line_no.to_i, :warning, message.sub(/warning: /, '').capitalize]
25
+ else
26
+ [line_no.to_i, :error, message.capitalize]
15
27
  end
16
28
  end
17
29
  end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class VariableInterpolation < Cop
6
+ def inspect(file, source, tokens, sexp)
7
+ each(:string_dvar, sexp) do |s|
8
+ var = s[1][1][1]
9
+ lineno = s[1][1][2].lineno
10
+
11
+ add_offence(
12
+ :convention,
13
+ lineno,
14
+ "Replace interpolated var #{var} with expression \#{#{var}}."
15
+ )
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -11,6 +11,9 @@ module Rubocop
11
11
  # when <value> <divider> <body>
12
12
  # where divider is either semicolon, then, or line break.
13
13
  last_pos_in_value = all_positions(s[1])[-1]
14
+
15
+ next unless last_pos_in_value # Give up if no positions found.
16
+
14
17
  start_index = tokens.index { |t| t.pos == last_pos_in_value }
15
18
  tokens[start_index..-1].each do |t|
16
19
  break if ['then', "\n"].include?(t.text)
@@ -7,8 +7,8 @@ module Rubocop
7
7
  # Generates a string representation of the report
8
8
  def generate
9
9
  report = entries.map do |e|
10
- '%s:%d: %s: %s' % [@filename, e.line_number, e.encode_severity,
11
- e.message]
10
+ sprintf('%s:%d: %s: %s', @filename,
11
+ e.line_number, e.encode_severity, e.message)
12
12
  end
13
13
  report.join("\n")
14
14
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Rubocop
4
- VERSION = '0.3.2'
4
+ VERSION = IO.read(File.join(File.dirname(__FILE__), '../../VERSION'))
5
5
  end
data/lib/rubocop.rb CHANGED
@@ -38,6 +38,15 @@ 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'
44
+ require 'rubocop/cop/avoid_class_vars'
45
+ require 'rubocop/cop/symbol_snake_case'
46
+ require 'rubocop/cop/variable_interpolation'
47
+ require 'rubocop/cop/semicolon'
48
+ require 'rubocop/cop/favor_sprintf'
49
+ require 'rubocop/cop/alias'
41
50
 
42
51
  require 'rubocop/report/report'
43
52
  require 'rubocop/report/plain_text'
data/rubocop.gemspec CHANGED
@@ -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.5.0"
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-17"
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"]
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
  ".rspec",
23
23
  ".rubocop.yml",
24
24
  ".travis.yml",
25
+ "CHANGELOG.md",
25
26
  "CONTRIBUTING.md",
26
27
  "Gemfile",
27
28
  "Gemfile.lock",
@@ -32,8 +33,13 @@ Gem::Specification.new do |s|
32
33
  "bin/rubocop",
33
34
  "lib/rubocop.rb",
34
35
  "lib/rubocop/cli.rb",
36
+ "lib/rubocop/cop/alias.rb",
35
37
  "lib/rubocop/cop/align_parameters.rb",
36
38
  "lib/rubocop/cop/ampersands_pipes_vs_and_or.rb",
39
+ "lib/rubocop/cop/avoid_class_vars.rb",
40
+ "lib/rubocop/cop/avoid_for.rb",
41
+ "lib/rubocop/cop/avoid_perl_backrefs.rb",
42
+ "lib/rubocop/cop/avoid_perlisms.rb",
37
43
  "lib/rubocop/cop/blocks.rb",
38
44
  "lib/rubocop/cop/class_and_module_camel_case.rb",
39
45
  "lib/rubocop/cop/collection_methods.rb",
@@ -43,6 +49,7 @@ Gem::Specification.new do |s|
43
49
  "lib/rubocop/cop/encoding.rb",
44
50
  "lib/rubocop/cop/end_of_line.rb",
45
51
  "lib/rubocop/cop/favor_modifier.rb",
52
+ "lib/rubocop/cop/favor_sprintf.rb",
46
53
  "lib/rubocop/cop/favor_unless_over_negated_if.rb",
47
54
  "lib/rubocop/cop/grammar.rb",
48
55
  "lib/rubocop/cop/hash_syntax.rb",
@@ -55,14 +62,17 @@ Gem::Specification.new do |s|
55
62
  "lib/rubocop/cop/offence.rb",
56
63
  "lib/rubocop/cop/parameter_lists.rb",
57
64
  "lib/rubocop/cop/parentheses_around_condition.rb",
65
+ "lib/rubocop/cop/semicolon.rb",
58
66
  "lib/rubocop/cop/space_after_comma_etc.rb",
59
67
  "lib/rubocop/cop/string_literals.rb",
60
68
  "lib/rubocop/cop/surrounding_space.rb",
69
+ "lib/rubocop/cop/symbol_snake_case.rb",
61
70
  "lib/rubocop/cop/syntax.rb",
62
71
  "lib/rubocop/cop/tab.rb",
63
72
  "lib/rubocop/cop/ternary_operator.rb",
64
73
  "lib/rubocop/cop/trailing_whitespace.rb",
65
74
  "lib/rubocop/cop/unless_else.rb",
75
+ "lib/rubocop/cop/variable_interpolation.rb",
66
76
  "lib/rubocop/cop/when_then.rb",
67
77
  "lib/rubocop/report/emacs_style.rb",
68
78
  "lib/rubocop/report/plain_text.rb",
@@ -70,8 +80,13 @@ Gem::Specification.new do |s|
70
80
  "lib/rubocop/version.rb",
71
81
  "rubocop.gemspec",
72
82
  "spec/rubocop/cli_spec.rb",
83
+ "spec/rubocop/cops/alias_spec.rb",
73
84
  "spec/rubocop/cops/align_parameters_spec.rb",
74
85
  "spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb",
86
+ "spec/rubocop/cops/avoid_class_vars_spec.rb",
87
+ "spec/rubocop/cops/avoid_for_spec.rb",
88
+ "spec/rubocop/cops/avoid_perl_backrefs_spec.rb",
89
+ "spec/rubocop/cops/avoid_perlisms_spec.rb",
75
90
  "spec/rubocop/cops/class_and_module_camel_case_spec.rb",
76
91
  "spec/rubocop/cops/collection_methods_spec.rb",
77
92
  "spec/rubocop/cops/cop_spec.rb",
@@ -81,6 +96,7 @@ Gem::Specification.new do |s|
81
96
  "spec/rubocop/cops/encoding_spec.rb",
82
97
  "spec/rubocop/cops/end_of_line_spec.rb",
83
98
  "spec/rubocop/cops/favor_modifier_spec.rb",
99
+ "spec/rubocop/cops/favor_sprintf_spec.rb",
84
100
  "spec/rubocop/cops/favor_unless_over_negated_if_spec.rb",
85
101
  "spec/rubocop/cops/favor_until_over_negated_while_spec.rb",
86
102
  "spec/rubocop/cops/grammar_spec.rb",
@@ -97,6 +113,7 @@ Gem::Specification.new do |s|
97
113
  "spec/rubocop/cops/one_line_conditional_spec.rb",
98
114
  "spec/rubocop/cops/parameter_lists_spec.rb",
99
115
  "spec/rubocop/cops/parentheses_around_condition_spec.rb",
116
+ "spec/rubocop/cops/semicolon_spec.rb",
100
117
  "spec/rubocop/cops/single_line_blocks_spec.rb",
101
118
  "spec/rubocop/cops/space_after_colon_spec.rb",
102
119
  "spec/rubocop/cops/space_after_comma_spec.rb",
@@ -107,11 +124,13 @@ Gem::Specification.new do |s|
107
124
  "spec/rubocop/cops/space_inside_brackets_spec.rb",
108
125
  "spec/rubocop/cops/space_inside_parens_spec.rb",
109
126
  "spec/rubocop/cops/string_literals_spec.rb",
127
+ "spec/rubocop/cops/symbol_snake_case_spec.rb",
110
128
  "spec/rubocop/cops/syntax_spec.rb",
111
129
  "spec/rubocop/cops/tab_spec.rb",
112
130
  "spec/rubocop/cops/ternary_operator_spec.rb",
113
131
  "spec/rubocop/cops/trailing_whitespace_spec.rb",
114
132
  "spec/rubocop/cops/unless_else_spec.rb",
133
+ "spec/rubocop/cops/variable_interpolation_spec.rb",
115
134
  "spec/rubocop/cops/when_then_spec.rb",
116
135
  "spec/rubocop/reports/emacs_style_spec.rb",
117
136
  "spec/rubocop/reports/report_spec.rb",
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'fileutils'
3
4
  require 'spec_helper'
4
5
 
5
6
  module Rubocop
@@ -208,6 +209,32 @@ module Rubocop
208
209
  end
209
210
  end
210
211
 
212
+ it 'can have different config files in different directories' do
213
+ %w(src lib).each do |dir|
214
+ FileUtils.mkdir_p "example/#{dir}"
215
+ File.open("example/#{dir}/example1.rb", 'w') do |f|
216
+ f.puts '# encoding: utf-8'
217
+ f.puts '#' * 90
218
+ end
219
+ end
220
+ File.open('example/src/.rubocop.yml', 'w') do |f|
221
+ f.puts('LineLength:',
222
+ ' Enabled: true',
223
+ ' Max: 100')
224
+ end
225
+ begin
226
+ expect(cli.run(['example'])).to eq(1)
227
+ expect($stdout.string.uncolored).to eq(
228
+ ['== example/lib/example1.rb ==',
229
+ 'C: 2: Line is too long. [90/79]',
230
+ '',
231
+ '2 files inspected, 1 offences detected',
232
+ ''].join("\n"))
233
+ ensure
234
+ FileUtils.rm_rf 'example'
235
+ end
236
+ end
237
+
211
238
  it 'finds no violations when checking the rubocop source code' do
212
239
  cli.run
213
240
  expect($stdout.string.uncolored).to match(
@@ -215,7 +242,27 @@ module Rubocop
215
242
  )
216
243
  end
217
244
 
218
- it 'can process a file with an invalide UTF-8 byte sequence' do
245
+ it 'registers an offence for a syntax error' do
246
+ File.open('example.rb', 'w') do |f|
247
+ f.puts '# encoding: utf-8'
248
+ f.puts 'class Test'
249
+ f.puts 'en'
250
+ end
251
+ begin
252
+ expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
253
+ unexpected_part = RUBY_VERSION >= '2.0' ? 'end-of-input' : '$end'
254
+ expect($stdout.string.uncolored).to eq(
255
+ ["example.rb:3: E: Syntax error, unexpected #{unexpected_part}, " +
256
+ 'expecting keyword_end',
257
+ '',
258
+ '1 files inspected, 1 offences detected',
259
+ ''].join("\n"))
260
+ ensure
261
+ File.delete 'example.rb'
262
+ end
263
+ end
264
+
265
+ it 'can process a file with an invalid UTF-8 byte sequence' do
219
266
  File.open('example.rb', 'w') do |f|
220
267
  f.puts '# encoding: utf-8'
221
268
  f.puts "# \xf9\x29"
@@ -233,6 +280,26 @@ module Rubocop
233
280
  end
234
281
  expect(YAML.load_file('.rubocop.yml').keys.sort).to eq(cop_names.sort)
235
282
  end
283
+
284
+ it 'finds a file with no .rb extension but has a shebang line' do
285
+ FileUtils::mkdir 'test'
286
+ File.open('test/example', 'w') do |f|
287
+ f.puts '#!/usr/bin/env ruby'
288
+ f.puts '# encoding: utf-8'
289
+ f.puts 'x = 0'
290
+ f.puts 'puts x'
291
+ end
292
+ begin
293
+ FileUtils::cd 'test' do
294
+ expect(cli.run([])).to eq(0)
295
+ expect($stdout.string.uncolored).to eq(
296
+ ['', '1 files inspected, 0 offences detected',
297
+ ''].join("\n"))
298
+ end
299
+ ensure
300
+ FileUtils::rm_rf 'test'
301
+ end
302
+ end
236
303
  end
237
304
  end
238
305
  end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe Alias do
8
+ let(:a) { Alias.new }
9
+
10
+ it 'registers an offence for alias' do
11
+ inspect_source(a,
12
+ 'file.rb',
13
+ ['alias :ala :bala'])
14
+ expect(a.offences.size).to eq(1)
15
+ expect(a.offences.map(&:message))
16
+ .to eq([Alias::ERROR_MESSAGE])
17
+ end
18
+
19
+ it 'does not register an offence for alias_method' do
20
+ inspect_source(a,
21
+ 'file.rb',
22
+ ['alias_method :ala, :bala'])
23
+ expect(a.offences).to be_empty
24
+ end
25
+ end
26
+ end
27
+ end
@@ -9,7 +9,7 @@ module Rubocop
9
9
 
10
10
  it 'registers an offence for parameters with single indent' do
11
11
  inspect_source(align, 'file.rb', ['function(a,',
12
- ' if b then c else d end)'])
12
+ ' if b then c else d end)'])
13
13
  expect(align.offences.map(&:message)).to eq(
14
14
  ['Align the parameters of a method call if they span more than ' +
15
15
  'one line.'])
@@ -17,7 +17,7 @@ module Rubocop
17
17
 
18
18
  it 'registers an offence for parameters with double indent' do
19
19
  inspect_source(align, 'file.rb', ['function(a,',
20
- ' if b then c else d end)'])
20
+ ' if b then c else d end)'])
21
21
  expect(align.offences.map(&:message)).to eq(
22
22
  ['Align the parameters of a method call if they span more than ' +
23
23
  'one line.'])
@@ -25,9 +25,9 @@ module Rubocop
25
25
 
26
26
  it 'accepts correctly aligned parameters' do
27
27
  inspect_source(align, 'file.rb', ['function(a,',
28
- ' 0, 1,',
29
- ' (x + y),',
30
- ' if b then c else d end)'])
28
+ ' 0, 1,',
29
+ ' (x + y),',
30
+ ' if b then c else d end)'])
31
31
  expect(align.offences.map(&:message)).to be_empty
32
32
  end
33
33
 
@@ -58,6 +58,15 @@ module Rubocop
58
58
  'more than one line.'])
59
59
  end
60
60
 
61
+ it "doesn't get confused by extra comma at the end" do
62
+ inspect_source(align, '',
63
+ ['func1(a,',
64
+ ' b,)'])
65
+ expect(align.offences.map(&:to_s)).to eq(
66
+ ['C: 2: Align the parameters of a method call if they span ' +
67
+ 'more than one line.'])
68
+ end
69
+
61
70
  it 'can handle a correctly aligned string literal as first argument' do
62
71
  inspect_source(align, '',
63
72
  ['add_offence(:convention, x,',
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe AvoidClassVars do
8
+ let(:acv) { AvoidClassVars.new }
9
+
10
+ it 'registers an offence for class variables' do
11
+ inspect_source(acv,
12
+ 'file.rb',
13
+ ['class TestClass; @@test = 10; end'])
14
+ expect(acv.offences.size).to eq(1)
15
+ expect(acv.offences.map(&:message))
16
+ .to eq(['Replace class var @@test with a class instance var.'])
17
+ end
18
+ end
19
+ end
20
+ end
@@ -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
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe FavorSprintf do
8
+ let(:fs) { FavorSprintf.new }
9
+
10
+ it 'registers an offence for a string followed by something' do
11
+ inspect_source(fs,
12
+ 'file.rb',
13
+ ['puts "%d" % 10'])
14
+ expect(fs.offences.size).to eq(1)
15
+ expect(fs.offences.map(&:message))
16
+ .to eq([FavorSprintf::ERROR_MESSAGE])
17
+ end
18
+
19
+ it 'registers an offence for something followed by an array' do
20
+ inspect_source(fs,
21
+ 'file.rb',
22
+ ['puts x % [10, 11]'])
23
+ expect(fs.offences.size).to eq(1)
24
+ expect(fs.offences.map(&:message))
25
+ .to eq([FavorSprintf::ERROR_MESSAGE])
26
+ end
27
+
28
+ it 'does not register an offence for numbers' do
29
+ inspect_source(fs,
30
+ 'file.rb',
31
+ ['puts 10 % 4'])
32
+ expect(fs.offences).to be_empty
33
+ end
34
+
35
+ it 'does not register an offence for ambiguous cases' do
36
+ inspect_source(fs,
37
+ 'file.rb',
38
+ ['puts x % 4'])
39
+ expect(fs.offences).to be_empty
40
+
41
+ inspect_source(fs,
42
+ 'file.rb',
43
+ ['puts x % Y'])
44
+ expect(fs.offences).to be_empty
45
+ end
46
+
47
+ it 'should work if the first operand contains embedded expressions'
48
+ end
49
+ end
50
+ 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