rubocop 0.4.6 → 0.5.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.
- data/.rubocop.yml +16 -0
- data/CHANGELOG.md +22 -0
- data/CONTRIBUTING.md +1 -0
- data/README.md +33 -0
- data/VERSION +1 -1
- data/bin/rubocop +1 -0
- data/lib/rubocop.rb +4 -0
- data/lib/rubocop/cli.rb +23 -3
- data/lib/rubocop/cop/alias.rb +21 -0
- data/lib/rubocop/cop/blocks.rb +1 -1
- data/lib/rubocop/cop/cop.rb +1 -1
- data/lib/rubocop/cop/favor_sprintf.rb +45 -0
- data/lib/rubocop/cop/offence.rb +3 -1
- data/lib/rubocop/cop/semicolon.rb +18 -0
- data/lib/rubocop/cop/space_after_comma_etc.rb +2 -1
- data/lib/rubocop/cop/string_literals.rb +12 -0
- data/lib/rubocop/cop/variable_interpolation.rb +20 -0
- data/lib/rubocop/report/emacs_style.rb +2 -2
- data/rubocop.gemspec +11 -2
- data/spec/rubocop/cli_spec.rb +21 -0
- data/spec/rubocop/cops/alias_spec.rb +27 -0
- data/spec/rubocop/cops/favor_sprintf_spec.rb +50 -0
- data/spec/rubocop/cops/offence_spec.rb +6 -0
- data/spec/rubocop/cops/semicolon_spec.rb +29 -0
- data/spec/rubocop/cops/string_literals_spec.rb +4 -1
- data/spec/rubocop/cops/variable_interpolation_spec.rb +45 -0
- metadata +12 -3
data/.rubocop.yml
CHANGED
@@ -188,4 +188,20 @@ AvoidClassVars:
|
|
188
188
|
|
189
189
|
# Symbol literals should use snake_case.
|
190
190
|
SymbolSnakeCase:
|
191
|
+
Enabled: true
|
192
|
+
|
193
|
+
# Don't interpolate global, instance and class variables directly in strings.
|
194
|
+
VariableInterpolation:
|
195
|
+
Enabled: true
|
196
|
+
|
197
|
+
# Don't use semicolons to terminate expressions.
|
198
|
+
Semicolon:
|
199
|
+
Enabled: true
|
200
|
+
|
201
|
+
# Use sprintf instead of %
|
202
|
+
FavorSprintf:
|
203
|
+
Enabled: true
|
204
|
+
|
205
|
+
# Use alias_method instead of alias.
|
206
|
+
Alias:
|
191
207
|
Enabled: true
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## master (unreleased)
|
4
|
+
|
5
|
+
### New features
|
6
|
+
|
7
|
+
### Bugs fixed
|
8
|
+
|
9
|
+
## 0.5.0 (04/17/2013)
|
10
|
+
|
11
|
+
### New features
|
12
|
+
|
13
|
+
* New cop `FavorSprintf` that checks for usages of `String#%`
|
14
|
+
* New cop `Semicolon` that checks for usages of `;` as expression separator
|
15
|
+
* New cop `VariableInterpolation` that checks for variable interpolation in double quoted strings
|
16
|
+
* New cop `Alias` that checks for uses of the keyword `alias`
|
17
|
+
* Automatically detect extensionless Ruby files with shebangs when search for Ruby source files in a directory
|
18
|
+
|
19
|
+
### Bugs fixed
|
20
|
+
|
21
|
+
* [#59](https://github.com/bbatsov/rubocop/issues/59) - Interpolated variables not enclosed in braces are not noticed
|
22
|
+
* [#42](https://github.com/bbatsov/rubocop/issues/42) - Received malformed format string ArgumentError from rubocop
|
data/CONTRIBUTING.md
CHANGED
@@ -9,6 +9,7 @@
|
|
9
9
|
* Commit and push until you are happy with your contribution.
|
10
10
|
* Make sure to add tests for it. This is important so I don't break it
|
11
11
|
in a future version unintentionally.
|
12
|
+
* Update the [Changelog](CHANGELOG.md) accordingly.
|
12
13
|
* Please try not to mess with the Rakefile, version, or history. If
|
13
14
|
you want to have your own version, or is otherwise necessary, that
|
14
15
|
is fine, but please isolate to its own commit so I can cherry-pick
|
data/README.md
CHANGED
@@ -84,6 +84,39 @@ Rubinius is not planned at this point.
|
|
84
84
|
Emacs interface for RuboCop. It allows you to run RuboCop inside Emacs
|
85
85
|
and quickly jump between problems in your code.
|
86
86
|
|
87
|
+
[flycheck](https://github.com/lunaryorn/flycheck) > 0.9 also supports
|
88
|
+
RuboCop and uses it by default when available.
|
89
|
+
|
90
|
+
### Other Editors
|
91
|
+
|
92
|
+
Here's one great opportunity to contribute to RuboCop - implement
|
93
|
+
RuboCop integration for your favorite editor.
|
94
|
+
|
95
|
+
## Contributors
|
96
|
+
|
97
|
+
Here's a [list](https://github.com/bbatsov/rubocop/contributors) of
|
98
|
+
all the people who have contributed to the development of RuboCop.
|
99
|
+
|
100
|
+
I'm extremely grateful to each and every one of them!
|
101
|
+
|
102
|
+
I'd like to single out [Jonas Arvidsson](https://github.com/jonas054)
|
103
|
+
for his many excellent code contributions as well as valuable feedback
|
104
|
+
and ideas!
|
105
|
+
|
106
|
+
If you'd like to contribute to RuboCop, please take the time to go
|
107
|
+
through our short
|
108
|
+
[contribution guidelines](CONTRIBUTING.md).
|
109
|
+
|
110
|
+
Converting more of the Ruby Style Guide into RuboCop cops is our top
|
111
|
+
priority right now. Writing a new cop is a great way to dive into RuboCop!
|
112
|
+
|
113
|
+
Of course, bug reports and suggestions for improvements are always
|
114
|
+
welcome. GitHub pull requests are even better! :-)
|
115
|
+
|
116
|
+
## Changelog
|
117
|
+
|
118
|
+
RuboCop's changelog is available [here](CHANGELOG.md).
|
119
|
+
|
87
120
|
## Copyright
|
88
121
|
|
89
122
|
Copyright (c) 2012-2013 Bozhidar Batsov. See [LICENSE.txt](LICENSE.txt) for
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.0
|
data/bin/rubocop
CHANGED
data/lib/rubocop.rb
CHANGED
@@ -43,6 +43,10 @@ require 'rubocop/cop/avoid_perlisms'
|
|
43
43
|
require 'rubocop/cop/avoid_perl_backrefs'
|
44
44
|
require 'rubocop/cop/avoid_class_vars'
|
45
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'
|
46
50
|
|
47
51
|
require 'rubocop/report/report'
|
48
52
|
require 'rubocop/report/plain_text'
|
data/lib/rubocop/cli.rb
CHANGED
@@ -145,16 +145,16 @@ module Rubocop
|
|
145
145
|
|
146
146
|
# Generate a list of target files by expanding globing patterns
|
147
147
|
# (if any). If args is empty recursively finds all Ruby source
|
148
|
-
# files
|
148
|
+
# files under the current directory
|
149
149
|
# @return [Array] array of filenames
|
150
150
|
def target_files(args)
|
151
|
-
return
|
151
|
+
return ruby_files if args.empty?
|
152
152
|
|
153
153
|
files = []
|
154
154
|
|
155
155
|
args.each do |target|
|
156
156
|
if File.directory?(target)
|
157
|
-
files <<
|
157
|
+
files << ruby_files(target)
|
158
158
|
elsif target =~ /\*/
|
159
159
|
files << Dir[target]
|
160
160
|
else
|
@@ -164,5 +164,25 @@ module Rubocop
|
|
164
164
|
|
165
165
|
files.flatten
|
166
166
|
end
|
167
|
+
|
168
|
+
# Finds all Ruby source files under the current or other supplied
|
169
|
+
# directory. A Ruby source file is defined as a file with the `.rb`
|
170
|
+
# extension or a file with no extension that has a ruby shebang line
|
171
|
+
# as its first line.
|
172
|
+
# @param root Root directory under which to search for ruby source files
|
173
|
+
# @return [Array] Array of filenames
|
174
|
+
def ruby_files(root = Dir.pwd)
|
175
|
+
files = Dir["#{root}/**/*"].reject { |file| FileTest.directory? file }
|
176
|
+
|
177
|
+
rb = []
|
178
|
+
|
179
|
+
rb << files.select { |file| File.extname(file) == '.rb' }
|
180
|
+
rb << files.select do |file|
|
181
|
+
File.extname(file) == '' &&
|
182
|
+
File.open(file) { |f| f.readline } =~ /#!.*ruby/
|
183
|
+
end
|
184
|
+
|
185
|
+
rb.flatten
|
186
|
+
end
|
167
187
|
end
|
168
188
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
class Alias < Cop
|
6
|
+
ERROR_MESSAGE = 'Use alias_method instead of alias.'
|
7
|
+
|
8
|
+
def inspect(file, source, tokens, sexp)
|
9
|
+
each(:alias, sexp) do |s|
|
10
|
+
lineno = s[1][1][1][2].lineno
|
11
|
+
|
12
|
+
add_offence(
|
13
|
+
:convention,
|
14
|
+
lineno,
|
15
|
+
ERROR_MESSAGE
|
16
|
+
)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/rubocop/cop/blocks.rb
CHANGED
@@ -29,7 +29,7 @@ module Rubocop
|
|
29
29
|
if path.last == :brace_block
|
30
30
|
rbrace_ix = @reverse_correlations[path.object_id] - [ix]
|
31
31
|
if rbrace_ix.empty?
|
32
|
-
fail "\n
|
32
|
+
fail "\n#{@file}:#{t.pos.lineno}:#{t.pos.column}: " +
|
33
33
|
'Matching brace not found'
|
34
34
|
end
|
35
35
|
if tokens[*rbrace_ix].pos.lineno > t.pos.lineno
|
data/lib/rubocop/cop/cop.rb
CHANGED
@@ -0,0 +1,45 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
class FavorSprintf < Cop
|
6
|
+
ERROR_MESSAGE = 'Favor sprintf over String#%.'
|
7
|
+
|
8
|
+
def inspect(file, source, tokens, sexp)
|
9
|
+
each(:binary, sexp) do |s|
|
10
|
+
op1 = s[1]
|
11
|
+
operator = s[2]
|
12
|
+
op2 = s[3]
|
13
|
+
|
14
|
+
# we care only about the % operator
|
15
|
+
next unless matching?(operator, op1, op2)
|
16
|
+
|
17
|
+
# FIXME implement reliable lineno extraction
|
18
|
+
# we either have some string literal
|
19
|
+
if op1[0] == :string_literal
|
20
|
+
lineno_struct = s[1][1][1][2]
|
21
|
+
lineno = lineno_struct.lineno if lineno_struct.respond_to?(:lineno)
|
22
|
+
else
|
23
|
+
# or some identifier
|
24
|
+
lineno_struct = s[1][1][2]
|
25
|
+
lineno = lineno_struct.lineno if lineno_struct.respond_to?(:lineno)
|
26
|
+
end
|
27
|
+
|
28
|
+
add_offence(
|
29
|
+
:convention,
|
30
|
+
lineno,
|
31
|
+
ERROR_MESSAGE
|
32
|
+
) if lineno
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def matching?(operator, op1, op2)
|
39
|
+
return false unless operator == :%
|
40
|
+
|
41
|
+
op1[0] == :string_literal or op2[0] == :array
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/rubocop/cop/offence.rb
CHANGED
@@ -14,7 +14,9 @@ module Rubocop
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def to_s
|
17
|
-
|
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,
|
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,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
|
@@ -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'
|
11
|
-
|
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
|
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.
|
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-
|
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,6 +33,7 @@ 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",
|
37
39
|
"lib/rubocop/cop/avoid_class_vars.rb",
|
@@ -47,6 +49,7 @@ Gem::Specification.new do |s|
|
|
47
49
|
"lib/rubocop/cop/encoding.rb",
|
48
50
|
"lib/rubocop/cop/end_of_line.rb",
|
49
51
|
"lib/rubocop/cop/favor_modifier.rb",
|
52
|
+
"lib/rubocop/cop/favor_sprintf.rb",
|
50
53
|
"lib/rubocop/cop/favor_unless_over_negated_if.rb",
|
51
54
|
"lib/rubocop/cop/grammar.rb",
|
52
55
|
"lib/rubocop/cop/hash_syntax.rb",
|
@@ -59,6 +62,7 @@ Gem::Specification.new do |s|
|
|
59
62
|
"lib/rubocop/cop/offence.rb",
|
60
63
|
"lib/rubocop/cop/parameter_lists.rb",
|
61
64
|
"lib/rubocop/cop/parentheses_around_condition.rb",
|
65
|
+
"lib/rubocop/cop/semicolon.rb",
|
62
66
|
"lib/rubocop/cop/space_after_comma_etc.rb",
|
63
67
|
"lib/rubocop/cop/string_literals.rb",
|
64
68
|
"lib/rubocop/cop/surrounding_space.rb",
|
@@ -68,6 +72,7 @@ Gem::Specification.new do |s|
|
|
68
72
|
"lib/rubocop/cop/ternary_operator.rb",
|
69
73
|
"lib/rubocop/cop/trailing_whitespace.rb",
|
70
74
|
"lib/rubocop/cop/unless_else.rb",
|
75
|
+
"lib/rubocop/cop/variable_interpolation.rb",
|
71
76
|
"lib/rubocop/cop/when_then.rb",
|
72
77
|
"lib/rubocop/report/emacs_style.rb",
|
73
78
|
"lib/rubocop/report/plain_text.rb",
|
@@ -75,6 +80,7 @@ Gem::Specification.new do |s|
|
|
75
80
|
"lib/rubocop/version.rb",
|
76
81
|
"rubocop.gemspec",
|
77
82
|
"spec/rubocop/cli_spec.rb",
|
83
|
+
"spec/rubocop/cops/alias_spec.rb",
|
78
84
|
"spec/rubocop/cops/align_parameters_spec.rb",
|
79
85
|
"spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb",
|
80
86
|
"spec/rubocop/cops/avoid_class_vars_spec.rb",
|
@@ -90,6 +96,7 @@ Gem::Specification.new do |s|
|
|
90
96
|
"spec/rubocop/cops/encoding_spec.rb",
|
91
97
|
"spec/rubocop/cops/end_of_line_spec.rb",
|
92
98
|
"spec/rubocop/cops/favor_modifier_spec.rb",
|
99
|
+
"spec/rubocop/cops/favor_sprintf_spec.rb",
|
93
100
|
"spec/rubocop/cops/favor_unless_over_negated_if_spec.rb",
|
94
101
|
"spec/rubocop/cops/favor_until_over_negated_while_spec.rb",
|
95
102
|
"spec/rubocop/cops/grammar_spec.rb",
|
@@ -106,6 +113,7 @@ Gem::Specification.new do |s|
|
|
106
113
|
"spec/rubocop/cops/one_line_conditional_spec.rb",
|
107
114
|
"spec/rubocop/cops/parameter_lists_spec.rb",
|
108
115
|
"spec/rubocop/cops/parentheses_around_condition_spec.rb",
|
116
|
+
"spec/rubocop/cops/semicolon_spec.rb",
|
109
117
|
"spec/rubocop/cops/single_line_blocks_spec.rb",
|
110
118
|
"spec/rubocop/cops/space_after_colon_spec.rb",
|
111
119
|
"spec/rubocop/cops/space_after_comma_spec.rb",
|
@@ -122,6 +130,7 @@ Gem::Specification.new do |s|
|
|
122
130
|
"spec/rubocop/cops/ternary_operator_spec.rb",
|
123
131
|
"spec/rubocop/cops/trailing_whitespace_spec.rb",
|
124
132
|
"spec/rubocop/cops/unless_else_spec.rb",
|
133
|
+
"spec/rubocop/cops/variable_interpolation_spec.rb",
|
125
134
|
"spec/rubocop/cops/when_then_spec.rb",
|
126
135
|
"spec/rubocop/reports/emacs_style_spec.rb",
|
127
136
|
"spec/rubocop/reports/report_spec.rb",
|
data/spec/rubocop/cli_spec.rb
CHANGED
@@ -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
|
@@ -279,6 +280,26 @@ module Rubocop
|
|
279
280
|
end
|
280
281
|
expect(YAML.load_file('.rubocop.yml').keys.sort).to eq(cop_names.sort)
|
281
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
|
282
303
|
end
|
283
304
|
end
|
284
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
|
@@ -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
|
@@ -18,6 +18,12 @@ module Rubocop
|
|
18
18
|
|
19
19
|
expect(offence.to_s).to eq('C: 1: message')
|
20
20
|
end
|
21
|
+
|
22
|
+
it 'does not blow up if a message contains %' do
|
23
|
+
offence = Offence.new(:convention, 1, 'message % test')
|
24
|
+
|
25
|
+
expect(offence.to_s).to eq('C: 1: message % test')
|
26
|
+
end
|
21
27
|
end
|
22
28
|
end
|
23
29
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe Semicolon do
|
8
|
+
let(:s) { Semicolon.new }
|
9
|
+
|
10
|
+
it 'registers an offence for a single expression' do
|
11
|
+
inspect_source(s,
|
12
|
+
'file.rb',
|
13
|
+
['puts "this is a test";'])
|
14
|
+
expect(s.offences.size).to eq(1)
|
15
|
+
expect(s.offences.map(&:message))
|
16
|
+
.to eq([Semicolon::ERROR_MESSAGE])
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'registers an offence for several expressions' do
|
20
|
+
inspect_source(s,
|
21
|
+
'file.rb',
|
22
|
+
['puts "this is a test"; puts "So is this"'])
|
23
|
+
expect(s.offences.size).to eq(1)
|
24
|
+
expect(s.offences.map(&:message))
|
25
|
+
.to eq([Semicolon::ERROR_MESSAGE])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -17,7 +17,10 @@ module Rubocop
|
|
17
17
|
it 'accepts double quotes when they are needed' do
|
18
18
|
src = ['a = "\n"',
|
19
19
|
'b = "#{encode_severity}:#{sprintf("%3d", line_number)}: #{m}"',
|
20
|
-
'c = "\'"'
|
20
|
+
'c = "\'"',
|
21
|
+
'd = "#@test"',
|
22
|
+
'e = "#$test"',
|
23
|
+
'f = "#@@test"']
|
21
24
|
inspect_source(sl, 'file.rb', src)
|
22
25
|
expect(sl.offences.map(&:message)).to be_empty
|
23
26
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe VariableInterpolation do
|
8
|
+
let(:vi) { VariableInterpolation.new }
|
9
|
+
|
10
|
+
it 'registers an offence for interpolated global variables' do
|
11
|
+
inspect_source(vi,
|
12
|
+
'file.rb',
|
13
|
+
['puts "this is a #$test"'])
|
14
|
+
expect(vi.offences.size).to eq(1)
|
15
|
+
expect(vi.offences.map(&:message))
|
16
|
+
.to eq(['Replace interpolated var $test with expression #{$test}.'])
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'registers an offence for interpolated instance variables' do
|
20
|
+
inspect_source(vi,
|
21
|
+
'file.rb',
|
22
|
+
['puts "this is a #@test"'])
|
23
|
+
expect(vi.offences.size).to eq(1)
|
24
|
+
expect(vi.offences.map(&:message))
|
25
|
+
.to eq(['Replace interpolated var @test with expression #{@test}.'])
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'registers an offence for interpolated class variables' do
|
29
|
+
inspect_source(vi,
|
30
|
+
'file.rb',
|
31
|
+
['puts "this is a #@@t"'])
|
32
|
+
expect(vi.offences.size).to eq(1)
|
33
|
+
expect(vi.offences.map(&:message))
|
34
|
+
.to eq(['Replace interpolated var @@t with expression #{@@t}.'])
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'does not register an offence for variables in expressions' do
|
38
|
+
inspect_source(vi,
|
39
|
+
'file.rb',
|
40
|
+
['puts "this is a #{@test}"'])
|
41
|
+
expect(vi.offences).to be_empty
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
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
|
+
version: 0.5.0
|
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-
|
12
|
+
date: 2013-04-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: term-ansicolor
|
@@ -137,6 +137,7 @@ files:
|
|
137
137
|
- .rspec
|
138
138
|
- .rubocop.yml
|
139
139
|
- .travis.yml
|
140
|
+
- CHANGELOG.md
|
140
141
|
- CONTRIBUTING.md
|
141
142
|
- Gemfile
|
142
143
|
- Gemfile.lock
|
@@ -147,6 +148,7 @@ files:
|
|
147
148
|
- bin/rubocop
|
148
149
|
- lib/rubocop.rb
|
149
150
|
- lib/rubocop/cli.rb
|
151
|
+
- lib/rubocop/cop/alias.rb
|
150
152
|
- lib/rubocop/cop/align_parameters.rb
|
151
153
|
- lib/rubocop/cop/ampersands_pipes_vs_and_or.rb
|
152
154
|
- lib/rubocop/cop/avoid_class_vars.rb
|
@@ -162,6 +164,7 @@ files:
|
|
162
164
|
- lib/rubocop/cop/encoding.rb
|
163
165
|
- lib/rubocop/cop/end_of_line.rb
|
164
166
|
- lib/rubocop/cop/favor_modifier.rb
|
167
|
+
- lib/rubocop/cop/favor_sprintf.rb
|
165
168
|
- lib/rubocop/cop/favor_unless_over_negated_if.rb
|
166
169
|
- lib/rubocop/cop/grammar.rb
|
167
170
|
- lib/rubocop/cop/hash_syntax.rb
|
@@ -174,6 +177,7 @@ files:
|
|
174
177
|
- lib/rubocop/cop/offence.rb
|
175
178
|
- lib/rubocop/cop/parameter_lists.rb
|
176
179
|
- lib/rubocop/cop/parentheses_around_condition.rb
|
180
|
+
- lib/rubocop/cop/semicolon.rb
|
177
181
|
- lib/rubocop/cop/space_after_comma_etc.rb
|
178
182
|
- lib/rubocop/cop/string_literals.rb
|
179
183
|
- lib/rubocop/cop/surrounding_space.rb
|
@@ -183,6 +187,7 @@ files:
|
|
183
187
|
- lib/rubocop/cop/ternary_operator.rb
|
184
188
|
- lib/rubocop/cop/trailing_whitespace.rb
|
185
189
|
- lib/rubocop/cop/unless_else.rb
|
190
|
+
- lib/rubocop/cop/variable_interpolation.rb
|
186
191
|
- lib/rubocop/cop/when_then.rb
|
187
192
|
- lib/rubocop/report/emacs_style.rb
|
188
193
|
- lib/rubocop/report/plain_text.rb
|
@@ -190,6 +195,7 @@ files:
|
|
190
195
|
- lib/rubocop/version.rb
|
191
196
|
- rubocop.gemspec
|
192
197
|
- spec/rubocop/cli_spec.rb
|
198
|
+
- spec/rubocop/cops/alias_spec.rb
|
193
199
|
- spec/rubocop/cops/align_parameters_spec.rb
|
194
200
|
- spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb
|
195
201
|
- spec/rubocop/cops/avoid_class_vars_spec.rb
|
@@ -205,6 +211,7 @@ files:
|
|
205
211
|
- spec/rubocop/cops/encoding_spec.rb
|
206
212
|
- spec/rubocop/cops/end_of_line_spec.rb
|
207
213
|
- spec/rubocop/cops/favor_modifier_spec.rb
|
214
|
+
- spec/rubocop/cops/favor_sprintf_spec.rb
|
208
215
|
- spec/rubocop/cops/favor_unless_over_negated_if_spec.rb
|
209
216
|
- spec/rubocop/cops/favor_until_over_negated_while_spec.rb
|
210
217
|
- spec/rubocop/cops/grammar_spec.rb
|
@@ -221,6 +228,7 @@ files:
|
|
221
228
|
- spec/rubocop/cops/one_line_conditional_spec.rb
|
222
229
|
- spec/rubocop/cops/parameter_lists_spec.rb
|
223
230
|
- spec/rubocop/cops/parentheses_around_condition_spec.rb
|
231
|
+
- spec/rubocop/cops/semicolon_spec.rb
|
224
232
|
- spec/rubocop/cops/single_line_blocks_spec.rb
|
225
233
|
- spec/rubocop/cops/space_after_colon_spec.rb
|
226
234
|
- spec/rubocop/cops/space_after_comma_spec.rb
|
@@ -237,6 +245,7 @@ files:
|
|
237
245
|
- spec/rubocop/cops/ternary_operator_spec.rb
|
238
246
|
- spec/rubocop/cops/trailing_whitespace_spec.rb
|
239
247
|
- spec/rubocop/cops/unless_else_spec.rb
|
248
|
+
- spec/rubocop/cops/variable_interpolation_spec.rb
|
240
249
|
- spec/rubocop/cops/when_then_spec.rb
|
241
250
|
- spec/rubocop/reports/emacs_style_spec.rb
|
242
251
|
- spec/rubocop/reports/report_spec.rb
|
@@ -256,7 +265,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
256
265
|
version: '0'
|
257
266
|
segments:
|
258
267
|
- 0
|
259
|
-
hash: -
|
268
|
+
hash: -3879767922505531373
|
260
269
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
261
270
|
none: false
|
262
271
|
requirements:
|