rubocop 0.4.3 → 0.4.4

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.

@@ -184,4 +184,8 @@ AvoidPerlBackrefs:
184
184
 
185
185
  # Avoid the use of class variables.
186
186
  AvoidClassVars:
187
+ Enabled: true
188
+
189
+ # Symbol literals should use snake_case.
190
+ SymbolSnakeCase:
187
191
  Enabled: true
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.3
1
+ 0.4.4
@@ -42,6 +42,7 @@ require 'rubocop/cop/avoid_for'
42
42
  require 'rubocop/cop/avoid_perlisms'
43
43
  require 'rubocop/cop/avoid_perl_backrefs'
44
44
  require 'rubocop/cop/avoid_class_vars'
45
+ require 'rubocop/cop/symbol_snake_case'
45
46
 
46
47
  require 'rubocop/report/report'
47
48
  require 'rubocop/report/plain_text'
@@ -52,16 +52,26 @@ module Rubocop
52
52
  line.chomp
53
53
  end
54
54
 
55
- tokens, sexp, correlations = CLI.rip_source(source)
56
-
57
- cops.each do |cop_klass|
58
- cop_config = config[cop_klass.name.split('::').last] if config
59
- cop_klass.config = cop_config
60
- cop = cop_klass.new
61
- cop.correlations = correlations
62
- cop.inspect(file, source, tokens, sexp)
63
- total_offences += cop.offences.count
64
- report << cop if cop.has_report?
55
+ syntax_cop = Rubocop::Cop::Syntax.new
56
+ syntax_cop.inspect(file, source, nil, nil)
57
+
58
+ if syntax_cop.offences.map(&:severity).include?(:error)
59
+ # In case of a syntax error we just report that error and do
60
+ # no more checking in the file.
61
+ report << syntax_cop
62
+ total_offences += syntax_cop.offences.count
63
+ else
64
+ tokens, sexp, correlations = CLI.rip_source(source)
65
+
66
+ cops.each do |cop_klass|
67
+ cop_config = config[cop_klass.name.split('::').last] if config
68
+ cop_klass.config = cop_config
69
+ cop = cop_klass.new
70
+ cop.correlations = correlations
71
+ cop.inspect(file, source, tokens, sexp)
72
+ total_offences += cop.offences.count
73
+ report << cop if cop.has_report?
74
+ end
65
75
  end
66
76
 
67
77
  report.display unless report.empty?
@@ -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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "rubocop"
8
- s.version = "0.4.3"
8
+ s.version = "0.4.4"
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"]
@@ -62,6 +62,7 @@ Gem::Specification.new do |s|
62
62
  "lib/rubocop/cop/space_after_comma_etc.rb",
63
63
  "lib/rubocop/cop/string_literals.rb",
64
64
  "lib/rubocop/cop/surrounding_space.rb",
65
+ "lib/rubocop/cop/symbol_snake_case.rb",
65
66
  "lib/rubocop/cop/syntax.rb",
66
67
  "lib/rubocop/cop/tab.rb",
67
68
  "lib/rubocop/cop/ternary_operator.rb",
@@ -115,6 +116,7 @@ Gem::Specification.new do |s|
115
116
  "spec/rubocop/cops/space_inside_brackets_spec.rb",
116
117
  "spec/rubocop/cops/space_inside_parens_spec.rb",
117
118
  "spec/rubocop/cops/string_literals_spec.rb",
119
+ "spec/rubocop/cops/symbol_snake_case_spec.rb",
118
120
  "spec/rubocop/cops/syntax_spec.rb",
119
121
  "spec/rubocop/cops/tab_spec.rb",
120
122
  "spec/rubocop/cops/ternary_operator_spec.rb",
@@ -215,6 +215,26 @@ module Rubocop
215
215
  )
216
216
  end
217
217
 
218
+ it 'registers an offence for a syntax error' do
219
+ File.open('example.rb', 'w') do |f|
220
+ f.puts '# encoding: utf-8'
221
+ f.puts 'class Test'
222
+ f.puts 'en'
223
+ end
224
+ begin
225
+ expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
226
+ unexpected_part = RUBY_VERSION >= '2.0' ? 'end-of-input' : '$end'
227
+ expect($stdout.string.uncolored).to eq(
228
+ ["example.rb:3: E: Syntax error, unexpected #{unexpected_part}, " +
229
+ 'expecting keyword_end',
230
+ '',
231
+ '1 files inspected, 1 offences detected',
232
+ ''].join("\n"))
233
+ ensure
234
+ File.delete 'example.rb'
235
+ end
236
+ end
237
+
218
238
  it 'can process a file with an invalid UTF-8 byte sequence' do
219
239
  File.open('example.rb', 'w') do |f|
220
240
  f.puts '# encoding: utf-8'
@@ -0,0 +1,62 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe SymbolSnakeCase do
8
+ let(:snake_case) { SymbolSnakeCase.new }
9
+
10
+ it 'registers an offence for camel case in names' do
11
+ inspect_source(snake_case, 'file.rb',
12
+ ['test = :BadIdea'])
13
+ expect(snake_case.offences.map(&:message)).to eq(
14
+ ['Use snake_case for symbols.'])
15
+ end
16
+
17
+ it 'accepts snake case in names' do
18
+ inspect_source(snake_case, 'file.rb',
19
+ ['test = :good_idea'])
20
+ expect(snake_case.offences.map(&:message)).to be_empty
21
+ end
22
+
23
+ it 'accepts snake case with a prefix @ in names' do
24
+ inspect_source(snake_case, 'file.rb',
25
+ ['test = :@good_idea'])
26
+ expect(snake_case.offences.map(&:message)).to be_empty
27
+ end
28
+
29
+ it 'accepts snake case with ? suffix' do
30
+ inspect_source(snake_case, 'file.rb',
31
+ ['test = :good_idea?'])
32
+ expect(snake_case.offences.map(&:message)).to be_empty
33
+ end
34
+
35
+ it 'accepts snake case with ! suffix' do
36
+ inspect_source(snake_case, 'file.rb',
37
+ ['test = :good_idea!'])
38
+ expect(snake_case.offences.map(&:message)).to be_empty
39
+ end
40
+
41
+ it 'accepts snake case with = suffix' do
42
+ inspect_source(snake_case, 'file.rb',
43
+ ['test = :good_idea='])
44
+ expect(snake_case.offences.map(&:message)).to be_empty
45
+ end
46
+
47
+ it 'accepts special cases - !, [] and **' do
48
+ inspect_source(snake_case, 'file.rb',
49
+ ['test = :**',
50
+ 'test = :!',
51
+ 'test = :[]'])
52
+ expect(snake_case.offences.map(&:message)).to be_empty
53
+ end
54
+
55
+ it 'registers an offence for SCREAMING_SNAKE_CASE' do
56
+ inspect_source(snake_case, 'file.rb',
57
+ ['test = :BAD_IDEA'])
58
+ expect(snake_case.offences.size).to eq(1)
59
+ end
60
+ end
61
+ end
62
+ 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.3
4
+ version: 0.4.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -177,6 +177,7 @@ files:
177
177
  - lib/rubocop/cop/space_after_comma_etc.rb
178
178
  - lib/rubocop/cop/string_literals.rb
179
179
  - lib/rubocop/cop/surrounding_space.rb
180
+ - lib/rubocop/cop/symbol_snake_case.rb
180
181
  - lib/rubocop/cop/syntax.rb
181
182
  - lib/rubocop/cop/tab.rb
182
183
  - lib/rubocop/cop/ternary_operator.rb
@@ -230,6 +231,7 @@ files:
230
231
  - spec/rubocop/cops/space_inside_brackets_spec.rb
231
232
  - spec/rubocop/cops/space_inside_parens_spec.rb
232
233
  - spec/rubocop/cops/string_literals_spec.rb
234
+ - spec/rubocop/cops/symbol_snake_case_spec.rb
233
235
  - spec/rubocop/cops/syntax_spec.rb
234
236
  - spec/rubocop/cops/tab_spec.rb
235
237
  - spec/rubocop/cops/ternary_operator_spec.rb
@@ -254,7 +256,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
254
256
  version: '0'
255
257
  segments:
256
258
  - 0
257
- hash: -2923028741214038028
259
+ hash: 2433240001762549702
258
260
  required_rubygems_version: !ruby/object:Gem::Requirement
259
261
  none: false
260
262
  requirements: