rubocop 0.3.0 → 0.6.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.
- data/.rubocop.yml +176 -0
- data/.travis.yml +1 -0
- data/CHANGELOG.md +60 -0
- data/CONTRIBUTING.md +13 -6
- data/Gemfile +1 -13
- data/README.md +131 -6
- data/Rakefile +1 -15
- data/bin/rubocop +2 -1
- data/lib/rubocop/cli.rb +133 -21
- data/lib/rubocop/cop/alias.rb +18 -0
- data/lib/rubocop/cop/align_parameters.rb +5 -4
- data/lib/rubocop/cop/array_literal.rb +22 -0
- data/lib/rubocop/cop/ascii_identifiers_and_comments.rb +18 -0
- data/lib/rubocop/cop/avoid_class_vars.rb +20 -0
- data/lib/rubocop/cop/avoid_for.rb +17 -0
- data/lib/rubocop/cop/avoid_perl_backrefs.rb +20 -0
- data/lib/rubocop/cop/avoid_perlisms.rb +46 -0
- data/lib/rubocop/cop/blocks.rb +1 -1
- data/lib/rubocop/cop/brace_after_percent.rb +28 -0
- data/lib/rubocop/cop/class_and_module_camel_case.rb +20 -0
- data/lib/rubocop/cop/collection_methods.rb +29 -0
- data/lib/rubocop/cop/cop.rb +16 -4
- data/lib/rubocop/cop/def_parentheses.rb +4 -1
- data/lib/rubocop/cop/encoding.rb +7 -5
- data/lib/rubocop/cop/end_of_line.rb +1 -3
- data/lib/rubocop/cop/ensure_return.rb +36 -0
- data/lib/rubocop/cop/favor_modifier.rb +82 -0
- data/lib/rubocop/cop/favor_percent_r.rb +19 -0
- data/lib/rubocop/cop/favor_sprintf.rb +37 -0
- data/lib/rubocop/cop/favor_unless_over_negated_if.rb +47 -0
- data/lib/rubocop/cop/grammar.rb +12 -8
- data/lib/rubocop/cop/handle_exceptions.rb +21 -0
- data/lib/rubocop/cop/hash_literal.rb +22 -0
- data/lib/rubocop/cop/indentation.rb +1 -1
- data/lib/rubocop/cop/line_length.rb +7 -3
- data/lib/rubocop/cop/method_and_variable_snake_case.rb +31 -0
- data/lib/rubocop/cop/method_length.rb +66 -0
- data/lib/rubocop/cop/new_lambda_literal.rb +17 -0
- data/lib/rubocop/cop/offence.rb +3 -1
- data/lib/rubocop/cop/op_method.rb +23 -0
- data/lib/rubocop/cop/parentheses_around_condition.rb +24 -0
- data/lib/rubocop/cop/percent_literals.rb +25 -0
- data/lib/rubocop/cop/percent_r.rb +19 -0
- data/lib/rubocop/cop/reduce_arguments.rb +67 -0
- data/lib/rubocop/cop/rescue_exception.rb +39 -0
- data/lib/rubocop/cop/rescue_modifier.rb +20 -0
- 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/surrounding_space.rb +48 -47
- data/lib/rubocop/cop/symbol_snake_case.rb +27 -0
- data/lib/rubocop/cop/syntax.rb +42 -0
- data/lib/rubocop/cop/tab.rb +1 -3
- data/lib/rubocop/cop/variable_interpolation.rb +20 -0
- data/lib/rubocop/cop/when_then.rb +3 -0
- data/lib/rubocop/report/emacs_style.rb +2 -2
- data/lib/rubocop/report/plain_text.rb +1 -1
- data/lib/rubocop/report/report.rb +5 -2
- data/lib/rubocop/version.rb +3 -1
- data/lib/rubocop.rb +37 -0
- data/rubocop.gemspec +36 -134
- data/spec/rubocop/cli_spec.rb +326 -48
- data/spec/rubocop/cops/alias_spec.rb +36 -0
- data/spec/rubocop/cops/align_parameters_spec.rb +43 -34
- data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +4 -4
- data/spec/rubocop/cops/array_literal_spec.rb +29 -0
- data/spec/rubocop/cops/ascii_identifiers_and_comments_spec.rb +38 -0
- data/spec/rubocop/cops/avoid_class_vars_spec.rb +20 -0
- data/spec/rubocop/cops/avoid_for_spec.rb +22 -0
- data/spec/rubocop/cops/avoid_perl_backrefs_spec.rb +18 -0
- data/spec/rubocop/cops/avoid_perlisms_spec.rb +44 -0
- data/spec/rubocop/cops/brace_after_percent_spec.rb +27 -0
- data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +34 -0
- data/spec/rubocop/cops/collection_methods_spec.rb +39 -0
- data/spec/rubocop/cops/cop_spec.rb +5 -5
- data/spec/rubocop/cops/def_with_parentheses_spec.rb +5 -5
- data/spec/rubocop/cops/def_without_parentheses_spec.rb +4 -4
- data/spec/rubocop/cops/empty_lines_spec.rb +8 -8
- data/spec/rubocop/cops/encoding_spec.rb +17 -11
- data/spec/rubocop/cops/end_of_line_spec.rb +5 -5
- data/spec/rubocop/cops/ensure_return_spec.rb +37 -0
- data/spec/rubocop/cops/favor_modifier_spec.rb +112 -0
- data/spec/rubocop/cops/favor_percent_r.rb +29 -0
- data/spec/rubocop/cops/favor_sprintf_spec.rb +57 -0
- data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +62 -0
- data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +45 -0
- data/spec/rubocop/cops/grammar_spec.rb +59 -44
- data/spec/rubocop/cops/handle_exceptions_spec.rb +36 -0
- data/spec/rubocop/cops/hash_literal_spec.rb +29 -0
- data/spec/rubocop/cops/hash_syntax_spec.rb +10 -10
- data/spec/rubocop/cops/if_with_semicolon_spec.rb +3 -3
- data/spec/rubocop/cops/indentation_spec.rb +7 -7
- data/spec/rubocop/cops/line_length_spec.rb +4 -4
- data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +47 -0
- data/spec/rubocop/cops/method_length_spec.rb +150 -0
- data/spec/rubocop/cops/multiline_blocks_spec.rb +4 -4
- data/spec/rubocop/cops/multiline_if_then_spec.rb +7 -7
- data/spec/rubocop/cops/new_lambda_literal_spec.rb +23 -0
- data/spec/rubocop/cops/numeric_literals_spec.rb +10 -10
- data/spec/rubocop/cops/offence_spec.rb +10 -4
- data/spec/rubocop/cops/one_line_conditional_spec.rb +2 -3
- data/spec/rubocop/cops/op_method_spec.rb +58 -0
- data/spec/rubocop/cops/parameter_lists_spec.rb +4 -4
- data/spec/rubocop/cops/parentheses_around_condition_spec.rb +64 -0
- data/spec/rubocop/cops/percent_literals_spec.rb +47 -0
- data/spec/rubocop/cops/percent_r_spec.rb +29 -0
- data/spec/rubocop/cops/reduce_arguments_spec.rb +57 -0
- data/spec/rubocop/cops/rescue_exception_spec.rb +73 -0
- data/spec/rubocop/cops/rescue_modifier.rb +40 -0
- data/spec/rubocop/cops/semicolon_spec.rb +29 -0
- data/spec/rubocop/cops/single_line_blocks_spec.rb +4 -4
- data/spec/rubocop/cops/space_after_colon_spec.rb +5 -5
- data/spec/rubocop/cops/space_after_comma_spec.rb +3 -3
- data/spec/rubocop/cops/space_after_semicolon_spec.rb +3 -3
- data/spec/rubocop/cops/space_around_braces_spec.rb +6 -6
- data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +22 -0
- data/spec/rubocop/cops/space_around_operators_spec.rb +57 -25
- data/spec/rubocop/cops/space_inside_brackets_spec.rb +8 -8
- data/spec/rubocop/cops/space_inside_parens_spec.rb +4 -4
- data/spec/rubocop/cops/string_literals_spec.rb +9 -6
- data/spec/rubocop/cops/symbol_snake_case_spec.rb +74 -0
- data/spec/rubocop/cops/syntax_spec.rb +34 -0
- data/spec/rubocop/cops/tab_spec.rb +4 -4
- data/spec/rubocop/cops/ternary_operator_spec.rb +8 -8
- data/spec/rubocop/cops/trailing_whitespace_spec.rb +4 -4
- data/spec/rubocop/cops/unless_else_spec.rb +4 -4
- data/spec/rubocop/cops/variable_interpolation_spec.rb +45 -0
- data/spec/rubocop/cops/when_then_spec.rb +12 -5
- data/spec/rubocop/reports/emacs_style_spec.rb +3 -3
- data/spec/rubocop/reports/report_spec.rb +5 -5
- data/spec/spec_helper.rb +6 -0
- metadata +165 -36
- data/.rbenv-version +0 -1
- data/.rvmrc +0 -1
- data/Gemfile.lock +0 -39
- data/VERSION +0 -1
- data/features/rubocop.feature +0 -9
- data/features/step_definitions/rubocop_steps.rb +0 -1
- data/features/support/env.rb +0 -15
data/lib/rubocop/cli.rb
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'optparse'
|
|
4
4
|
require 'yaml'
|
|
5
|
+
require_relative 'cop/grammar'
|
|
5
6
|
|
|
6
7
|
module Rubocop
|
|
7
8
|
# The CLI is a class responsible of handling all the command line interface
|
|
@@ -17,8 +18,8 @@ module Rubocop
|
|
|
17
18
|
OptionParser.new do |opts|
|
|
18
19
|
opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'
|
|
19
20
|
|
|
20
|
-
opts.on('-
|
|
21
|
-
$options[:
|
|
21
|
+
opts.on('-d', '--[no-]debug', 'Display debug info') do |d|
|
|
22
|
+
$options[:debug] = d
|
|
22
23
|
end
|
|
23
24
|
opts.on('-e', '--emacs', 'Emacs style output') do
|
|
24
25
|
$options[:mode] = :emacs_style
|
|
@@ -29,31 +30,51 @@ module Rubocop
|
|
|
29
30
|
opts.on('-s', '--silent', 'Silence summary') do |s|
|
|
30
31
|
$options[:silent] = s
|
|
31
32
|
end
|
|
33
|
+
opts.on('-v', '--version', 'Display version') do
|
|
34
|
+
puts Rubocop::Version::STRING
|
|
35
|
+
exit(0)
|
|
36
|
+
end
|
|
32
37
|
end.parse!(args)
|
|
33
38
|
|
|
34
39
|
cops = Cop::Cop.all
|
|
35
|
-
show_cops_on_duty(cops) if $options[:
|
|
40
|
+
show_cops_on_duty(cops) if $options[:debug]
|
|
36
41
|
total_offences = 0
|
|
37
42
|
@configs = {}
|
|
38
43
|
|
|
39
44
|
target_files(args).each do |file|
|
|
45
|
+
puts "Scanning #{file}" if $options[:debug]
|
|
46
|
+
|
|
40
47
|
report = Report.create(file, $options[:mode])
|
|
41
48
|
source = File.readlines(file).map do |line|
|
|
42
49
|
get_rid_of_invalid_byte_sequences(line)
|
|
43
50
|
line.chomp
|
|
44
51
|
end
|
|
45
52
|
|
|
46
|
-
|
|
53
|
+
syntax_cop = Rubocop::Cop::Syntax.new
|
|
54
|
+
syntax_cop.inspect(file, source, nil, nil)
|
|
47
55
|
|
|
48
|
-
|
|
56
|
+
if syntax_cop.offences.map(&:severity).include?(:error)
|
|
57
|
+
# In case of a syntax error we just report that error and do
|
|
58
|
+
# no more checking in the file.
|
|
59
|
+
report << syntax_cop
|
|
60
|
+
total_offences += syntax_cop.offences.count
|
|
61
|
+
else
|
|
62
|
+
tokens, sexp, correlations = CLI.rip_source(source)
|
|
49
63
|
config = $options[:config] || config_from_dotfile(File.dirname(file))
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
64
|
+
disabled_lines = disabled_lines_in(source)
|
|
65
|
+
|
|
66
|
+
cops.each do |cop_klass|
|
|
67
|
+
cop_name = cop_klass.name.split('::').last
|
|
68
|
+
cop_config = config[cop_name] if config
|
|
69
|
+
if cop_config.nil? || cop_config['Enabled']
|
|
70
|
+
cop_klass.config = cop_config
|
|
71
|
+
cop = cop_klass.new
|
|
72
|
+
cop.correlations = correlations
|
|
73
|
+
cop.disabled_lines = disabled_lines[cop_name]
|
|
74
|
+
cop.inspect(file, source, tokens, sexp)
|
|
75
|
+
total_offences += cop.offences.count
|
|
76
|
+
report << cop if cop.has_report?
|
|
77
|
+
end
|
|
57
78
|
end
|
|
58
79
|
end
|
|
59
80
|
|
|
@@ -61,13 +82,58 @@ module Rubocop
|
|
|
61
82
|
end
|
|
62
83
|
|
|
63
84
|
unless $options[:silent]
|
|
64
|
-
|
|
65
|
-
puts "#{total_offences} offences detected"
|
|
85
|
+
display_summary(target_files(args).count, total_offences)
|
|
66
86
|
end
|
|
67
87
|
|
|
68
88
|
return total_offences == 0 ? 0 : 1
|
|
69
89
|
end
|
|
70
90
|
|
|
91
|
+
def display_summary(num_files, total_offences)
|
|
92
|
+
print "\n#{num_files} file#{num_files > 1 ? 's' : ''} inspected, "
|
|
93
|
+
offences_string = if total_offences.zero?
|
|
94
|
+
'no offences'
|
|
95
|
+
elsif total_offences == 1
|
|
96
|
+
'1 offence'
|
|
97
|
+
else
|
|
98
|
+
"#{total_offences} offences"
|
|
99
|
+
end
|
|
100
|
+
puts "#{offences_string} detected"
|
|
101
|
+
.send(total_offences.zero? ? :green : :red)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def disabled_lines_in(source)
|
|
105
|
+
disabled_lines = Hash.new([])
|
|
106
|
+
disabled_section = {}
|
|
107
|
+
regexp = '# rubocop : (%s)\b ((?:\w+,? )+)'.gsub(' ', '\s*')
|
|
108
|
+
section_regexp = '^\s*' + sprintf(regexp, '(?:dis|en)able')
|
|
109
|
+
single_line_regexp = '\S.*' + sprintf(regexp, 'disable')
|
|
110
|
+
|
|
111
|
+
source.each_with_index do |line, ix|
|
|
112
|
+
each_mentioned_cop(/#{section_regexp}/, line) do |cop_name, kind|
|
|
113
|
+
disabled_section[cop_name] = (kind == 'disable')
|
|
114
|
+
end
|
|
115
|
+
disabled_section.keys.each do |cop_name|
|
|
116
|
+
disabled_lines[cop_name] += [ix + 1] if disabled_section[cop_name]
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
each_mentioned_cop(/#{single_line_regexp}/, line) do |cop_name, kind|
|
|
120
|
+
disabled_lines[cop_name] += [ix + 1] if kind == 'disable'
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
disabled_lines
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def each_mentioned_cop(regexp, line)
|
|
127
|
+
match = line.match(regexp)
|
|
128
|
+
if match
|
|
129
|
+
kind, cops = match.captures
|
|
130
|
+
if cops.include?('all')
|
|
131
|
+
cops = Cop::Cop.all.map { |c| c.name.split('::').last }.join(',')
|
|
132
|
+
end
|
|
133
|
+
cops.split(/,\s*/).each { |cop_name| yield cop_name, kind }
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
71
137
|
def get_rid_of_invalid_byte_sequences(line)
|
|
72
138
|
enc = line.encoding.name
|
|
73
139
|
# UTF-16 works better in this algorithm but is not supported in 1.9.2.
|
|
@@ -89,6 +155,7 @@ module Rubocop
|
|
|
89
155
|
# directory where the inspected file is. If no .rubocop.yml is
|
|
90
156
|
# found there, the user's home directory is checked.
|
|
91
157
|
def config_from_dotfile(target_file_dir)
|
|
158
|
+
return unless target_file_dir
|
|
92
159
|
# @configs is a cache that maps directories to
|
|
93
160
|
# configurations. We search for .rubocop.yml only if we haven't
|
|
94
161
|
# already found it for the given directory.
|
|
@@ -98,7 +165,7 @@ module Rubocop
|
|
|
98
165
|
path = File.join(dir, '.rubocop.yml')
|
|
99
166
|
if File.exist?(path)
|
|
100
167
|
@configs[target_file_dir] = YAML.load_file(path)
|
|
101
|
-
|
|
168
|
+
return @configs[target_file_dir]
|
|
102
169
|
end
|
|
103
170
|
dir = File.expand_path('..', dir)
|
|
104
171
|
end
|
|
@@ -108,24 +175,35 @@ module Rubocop
|
|
|
108
175
|
@configs[target_file_dir]
|
|
109
176
|
end
|
|
110
177
|
|
|
178
|
+
def cops_on_duty(config)
|
|
179
|
+
cops_on_duty = []
|
|
180
|
+
|
|
181
|
+
Cop::Cop.all.each do |cop_klass|
|
|
182
|
+
cop_config = config[cop_klass.name.split('::').last] if config
|
|
183
|
+
cops_on_duty << cop_klass if cop_config.nil? || cop_config['Enabled']
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
cops_on_duty
|
|
187
|
+
end
|
|
188
|
+
|
|
111
189
|
def show_cops_on_duty(cops)
|
|
112
|
-
puts 'Reporting for duty
|
|
113
|
-
cops.each { |c| puts c }
|
|
114
|
-
puts '
|
|
190
|
+
puts '== Reporting for duty =='
|
|
191
|
+
cops.each { |c| puts ' * '.yellow + c.to_s.green }
|
|
192
|
+
puts '========================'
|
|
115
193
|
end
|
|
116
194
|
|
|
117
195
|
# Generate a list of target files by expanding globing patterns
|
|
118
196
|
# (if any). If args is empty recursively finds all Ruby source
|
|
119
|
-
# files
|
|
197
|
+
# files under the current directory
|
|
120
198
|
# @return [Array] array of filenames
|
|
121
199
|
def target_files(args)
|
|
122
|
-
return
|
|
200
|
+
return ruby_files if args.empty?
|
|
123
201
|
|
|
124
202
|
files = []
|
|
125
203
|
|
|
126
204
|
args.each do |target|
|
|
127
205
|
if File.directory?(target)
|
|
128
|
-
files <<
|
|
206
|
+
files << ruby_files(target)
|
|
129
207
|
elsif target =~ /\*/
|
|
130
208
|
files << Dir[target]
|
|
131
209
|
else
|
|
@@ -135,5 +213,39 @@ module Rubocop
|
|
|
135
213
|
|
|
136
214
|
files.flatten
|
|
137
215
|
end
|
|
216
|
+
|
|
217
|
+
# Finds all Ruby source files under the current or other supplied
|
|
218
|
+
# directory. A Ruby source file is defined as a file with the `.rb`
|
|
219
|
+
# extension or a file with no extension that has a ruby shebang line
|
|
220
|
+
# as its first line.
|
|
221
|
+
# @param root Root directory under which to search for ruby source files
|
|
222
|
+
# @return [Array] Array of filenames
|
|
223
|
+
def ruby_files(root = Dir.pwd)
|
|
224
|
+
files = Dir["#{root}/**/*"].reject { |file| FileTest.directory? file }
|
|
225
|
+
|
|
226
|
+
rb = []
|
|
227
|
+
|
|
228
|
+
rb << files.select { |file| File.extname(file) == '.rb' }
|
|
229
|
+
rb << files.select do |file|
|
|
230
|
+
File.extname(file) == '' &&
|
|
231
|
+
begin
|
|
232
|
+
File.open(file) { |f| f.readline } =~ /#!.*ruby/
|
|
233
|
+
rescue EOFError, ArgumentError => e
|
|
234
|
+
log_error(e, "Unprocessable file #{file.inspect}: ")
|
|
235
|
+
false
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
rb.flatten
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
private
|
|
243
|
+
|
|
244
|
+
def log_error(e, msg='')
|
|
245
|
+
if $options[:debug]
|
|
246
|
+
error_message = "#{e.class}, #{e.message}"
|
|
247
|
+
STDERR.puts "#{msg}\t#{error_message}"
|
|
248
|
+
end
|
|
249
|
+
end
|
|
138
250
|
end
|
|
139
251
|
end
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
tokens.each_index do |ix|
|
|
10
|
+
t = tokens[ix]
|
|
11
|
+
if t.type == :on_kw && t.text == 'alias'
|
|
12
|
+
add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -41,11 +41,12 @@ module Rubocop
|
|
|
41
41
|
# means there's only one parameter.
|
|
42
42
|
return nil if [:command, :command_call].include?(arg_paren[1][0][0])
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
if arg_paren[1][0] == :args_add_block
|
|
45
|
+
args_add_block = arg_paren[1]
|
|
46
|
+
args_add_block[1].empty? ? [args_add_block[2]] : args_add_block[1]
|
|
47
|
+
else
|
|
48
|
+
arg_paren[1]
|
|
47
49
|
end
|
|
48
|
-
args_add_block[1].empty? ? [args_add_block[2]] : args_add_block[1]
|
|
49
50
|
end
|
|
50
51
|
|
|
51
52
|
def divide_args(args)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class ArrayLiteral < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Use array literal [] instead of Array.new.'
|
|
7
|
+
|
|
8
|
+
def inspect(file, source, tokens, sexp)
|
|
9
|
+
each(:method_add_arg, sexp) do |s|
|
|
10
|
+
potential_class = s[1][1][1]
|
|
11
|
+
|
|
12
|
+
if potential_class[1] == 'Array' && s[1][3][1] == 'new' &&
|
|
13
|
+
s[2] == [:arg_paren, nil]
|
|
14
|
+
add_offence(:convention,
|
|
15
|
+
potential_class[2].lineno,
|
|
16
|
+
ERROR_MESSAGE)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class AsciiIdentifiersAndComments < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Use only ascii symbols in identifiers and comments.'
|
|
7
|
+
|
|
8
|
+
def inspect(file, source, tokens, sexp)
|
|
9
|
+
tokens.each do |t|
|
|
10
|
+
if [:on_ident, :on_comment].include?(t.type) &&
|
|
11
|
+
t.text =~ /[^\x00-\x7f]/
|
|
12
|
+
add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class AvoidClassVars < Cop
|
|
6
|
+
def inspect(file, source, tokens, sexp)
|
|
7
|
+
each(:@cvar, sexp) do |s|
|
|
8
|
+
class_var = s[1]
|
|
9
|
+
lineno = s[2].lineno
|
|
10
|
+
|
|
11
|
+
add_offence(
|
|
12
|
+
:convention,
|
|
13
|
+
lineno,
|
|
14
|
+
"Replace class var #{class_var} with a class instance var."
|
|
15
|
+
)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -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,46 @@
|
|
|
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
|
+
'$!' => '$ERROR_INFO from English library',
|
|
11
|
+
'$@' => '$ERROR_POSITION from English library',
|
|
12
|
+
'$;' => '$FS or $FIELD_SEPARATOR from English library',
|
|
13
|
+
'$,' => '$OFS or $OUTPUT_FIELD_SEPARATOR from English library',
|
|
14
|
+
'$/' => '$RS or $INPUT_RECORD_SEPARATOR from English library',
|
|
15
|
+
'$\\' => '$ORS or $OUTPUT_RECORD_SEPARATOR from English library',
|
|
16
|
+
'$.' => '$NR or $INPUT_LINE_NUMBER from English library',
|
|
17
|
+
'$_' => '$LAST_READ_LINE from English library',
|
|
18
|
+
'$>' => '$DEFAULT_OUTPUT from English library',
|
|
19
|
+
'$<' => '$DEFAULT_INPUT from English library',
|
|
20
|
+
'$$' => '$PID or $PROCESS_ID from English library',
|
|
21
|
+
'$?' => '$CHILD_STATUS from English library',
|
|
22
|
+
'$~' => '$LAST_MATCH_INFO from English library',
|
|
23
|
+
'$=' => '$IGNORECASE from English library',
|
|
24
|
+
'$*' => '$ARGV from English library or ARGV constant',
|
|
25
|
+
'$&' => '$MATCH from English library',
|
|
26
|
+
'$`' => '$PREMATCH from English library',
|
|
27
|
+
'$\'' => '$POSTMATCH from English library',
|
|
28
|
+
'$+' => '$LAST_PAREN_MATCH from English library'
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
def inspect(file, source, tokens, sexp)
|
|
32
|
+
each(:@gvar, sexp) do |s|
|
|
33
|
+
global_var = s[1]
|
|
34
|
+
|
|
35
|
+
if PREFERRED_VARS[global_var]
|
|
36
|
+
add_offence(
|
|
37
|
+
:convention,
|
|
38
|
+
s[2].lineno,
|
|
39
|
+
"Prefer #{PREFERRED_VARS[global_var]} over #{global_var}."
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
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
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class BraceAfterPercent < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Prefer () as delimiters for all % literals.'
|
|
7
|
+
LITERALS = {
|
|
8
|
+
on_tstring_beg: '%q',
|
|
9
|
+
on_words_beg: '%W',
|
|
10
|
+
on_qwords_beg: '%w',
|
|
11
|
+
on_regexp_beg: '%r',
|
|
12
|
+
on_symbeg: '%s',
|
|
13
|
+
on_backtick: '%x'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
def inspect(file, source, tokens, sexp)
|
|
17
|
+
tokens.each_index do |ix|
|
|
18
|
+
t = tokens[ix]
|
|
19
|
+
token = LITERALS[t.type]
|
|
20
|
+
if token && t.text.downcase.start_with?(token) && t.text[2] != '('
|
|
21
|
+
add_offence(:convention, t.pos.lineno,
|
|
22
|
+
ERROR_MESSAGE)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class ClassAndModuleCamelCase < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Use CamelCase for classes and modules.'
|
|
7
|
+
|
|
8
|
+
def inspect(file, source, tokens, sexp)
|
|
9
|
+
[:class, :module].each do |keyword|
|
|
10
|
+
each(keyword, sexp) do |s|
|
|
11
|
+
if s[1][0] == :const_ref && s[1][1][0] == :@const &&
|
|
12
|
+
s[1][1][1] =~ /_/
|
|
13
|
+
add_offence(:convention, s[1][1][2].lineno, ERROR_MESSAGE)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class CollectionMethods < Cop
|
|
6
|
+
PREFERRED_METHODS = {
|
|
7
|
+
'collect' => 'map',
|
|
8
|
+
'inject' => 'reduce',
|
|
9
|
+
'detect' => 'find',
|
|
10
|
+
'find_all' => 'select',
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
def inspect(file, source, tokens, sexp)
|
|
14
|
+
each(:call, sexp) do |s|
|
|
15
|
+
s.drop(2).each_slice(2) do |m|
|
|
16
|
+
method_name = m[1][1]
|
|
17
|
+
if PREFERRED_METHODS[method_name]
|
|
18
|
+
add_offence(
|
|
19
|
+
:convention,
|
|
20
|
+
m[1][2].lineno,
|
|
21
|
+
"Prefer #{PREFERRED_METHODS[method_name]} over #{method_name}."
|
|
22
|
+
)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/rubocop/cop/cop.rb
CHANGED
|
@@ -26,13 +26,13 @@ module Rubocop
|
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
def to_s
|
|
29
|
-
"[[#{@pos.lineno}, #{@pos.column}],
|
|
29
|
+
"[[#{@pos.lineno}, #{@pos.column}], #{@type}, #{@text.inspect}]"
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
class Cop
|
|
34
34
|
attr_accessor :offences
|
|
35
|
-
attr_writer :correlations
|
|
35
|
+
attr_writer :correlations, :disabled_lines
|
|
36
36
|
|
|
37
37
|
@all = []
|
|
38
38
|
@config = {}
|
|
@@ -55,7 +55,9 @@ module Rubocop
|
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
def add_offence(severity, line_number, message)
|
|
58
|
-
@
|
|
58
|
+
unless @disabled_lines && @disabled_lines.include?(line_number)
|
|
59
|
+
@offences << Offence.new(severity, line_number, message)
|
|
60
|
+
end
|
|
59
61
|
end
|
|
60
62
|
|
|
61
63
|
private
|
|
@@ -83,13 +85,23 @@ module Rubocop
|
|
|
83
85
|
end
|
|
84
86
|
end
|
|
85
87
|
|
|
88
|
+
def find_all(sym, sexp)
|
|
89
|
+
result = []
|
|
90
|
+
each(sym, sexp) { |s| result << s }
|
|
91
|
+
result
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def find_first(sym, sexp)
|
|
95
|
+
find_all(sym, sexp).first
|
|
96
|
+
end
|
|
97
|
+
|
|
86
98
|
def whitespace?(token)
|
|
87
99
|
[:on_sp, :on_ignored_nl, :on_nl].include?(token.type)
|
|
88
100
|
end
|
|
89
101
|
|
|
90
102
|
def all_positions(sexp)
|
|
91
103
|
return [sexp[2]] if sexp[0] =~ /^@/
|
|
92
|
-
sexp.grep(Array).
|
|
104
|
+
sexp.grep(Array).reduce([]) { |a, e| a + all_positions(e) }
|
|
93
105
|
end
|
|
94
106
|
end
|
|
95
107
|
end
|
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
module Rubocop
|
|
4
4
|
module Cop
|
|
5
5
|
module DefParentheses
|
|
6
|
-
EMPTY_PARAMS = [:params, nil, nil, nil,
|
|
6
|
+
EMPTY_PARAMS = [:params, nil, nil, nil,
|
|
7
|
+
nil, nil, nil, nil] if RUBY_VERSION >= '2.0.0'
|
|
8
|
+
EMPTY_PARAMS = [:params, nil, nil, nil,
|
|
9
|
+
nil, nil] if RUBY_VERSION < '2.0.0'
|
|
7
10
|
|
|
8
11
|
def inspect(file, source, tokens, sexp)
|
|
9
12
|
each(:def, sexp) { |def_sexp| check(tokens, def_sexp) }
|
data/lib/rubocop/cop/encoding.rb
CHANGED
|
@@ -3,13 +3,15 @@
|
|
|
3
3
|
module Rubocop
|
|
4
4
|
module Cop
|
|
5
5
|
class Encoding < Cop
|
|
6
|
-
ERROR_MESSAGE = 'Missing encoding comment.'
|
|
6
|
+
ERROR_MESSAGE = 'Missing utf-8 encoding comment.'
|
|
7
7
|
|
|
8
8
|
def inspect(file, source, tokens, sexp)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
unless RUBY_VERSION >= '2.0.0'
|
|
10
|
+
expected_line = 0
|
|
11
|
+
expected_line += 1 if source[expected_line] =~ /^#!/
|
|
12
|
+
unless source[expected_line] =~ /#.*coding: (UTF|utf)-8/
|
|
13
|
+
add_offence(:convention, 1, ERROR_MESSAGE)
|
|
14
|
+
end
|
|
13
15
|
end
|
|
14
16
|
end
|
|
15
17
|
end
|
|
@@ -7,9 +7,7 @@ module Rubocop
|
|
|
7
7
|
|
|
8
8
|
def inspect(file, source, tokens, sexp)
|
|
9
9
|
source.each_with_index do |line, index|
|
|
10
|
-
if line =~ /\r$/
|
|
11
|
-
add_offence(:convention, index + 1, ERROR_MESSAGE)
|
|
12
|
-
end
|
|
10
|
+
add_offence(:convention, index + 1, ERROR_MESSAGE) if line =~ /\r$/
|
|
13
11
|
end
|
|
14
12
|
end
|
|
15
13
|
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class EnsureReturn < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Never return from an ensure block.'
|
|
7
|
+
|
|
8
|
+
def inspect(file, source, tokens, sexp)
|
|
9
|
+
in_ensure = false
|
|
10
|
+
ensure_col = nil
|
|
11
|
+
|
|
12
|
+
tokens.each_index do |ix|
|
|
13
|
+
t = tokens[ix]
|
|
14
|
+
if ensure_start?(t)
|
|
15
|
+
in_ensure = true
|
|
16
|
+
ensure_col = t.pos.column
|
|
17
|
+
elsif ensure_end?(t, ensure_col)
|
|
18
|
+
in_ensure = false
|
|
19
|
+
elsif in_ensure && t.type == :on_kw && t.text == 'return'
|
|
20
|
+
add_offence(:warning, t.pos.lineno, ERROR_MESSAGE)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def ensure_start?(t)
|
|
28
|
+
t.type == :on_kw && t.text == 'ensure'
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def ensure_end?(t, column)
|
|
32
|
+
t.type == :on_kw && t.text == 'end' && t.pos.column == column
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|