rubocop 0.6.0 → 0.7.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.
- checksums.yaml +7 -0
- data/.rubocop.yml +4 -262
- data/CHANGELOG.md +65 -1
- data/Gemfile +4 -0
- data/README.md +89 -2
- data/Rakefile +2 -2
- data/bin/rubocop +15 -10
- data/lib/rubocop/cli.rb +156 -114
- data/lib/rubocop/config.rb +202 -0
- data/lib/rubocop/config_store.rb +37 -0
- data/lib/rubocop/cop/alias.rb +2 -5
- data/lib/rubocop/cop/align_parameters.rb +1 -1
- data/lib/rubocop/cop/array_literal.rb +43 -4
- data/lib/rubocop/cop/{ascii_identifiers_and_comments.rb → ascii_comments.rb} +3 -3
- data/lib/rubocop/cop/ascii_identifiers.rb +18 -0
- data/lib/rubocop/cop/avoid_for.rb +2 -4
- data/lib/rubocop/cop/avoid_global_vars.rb +49 -0
- data/lib/rubocop/cop/block_comments.rb +17 -0
- data/lib/rubocop/cop/brace_after_percent.rb +9 -5
- data/lib/rubocop/cop/{indentation.rb → case_indentation.rb} +1 -1
- data/lib/rubocop/cop/class_methods.rb +20 -0
- data/lib/rubocop/cop/colon_method_call.rb +44 -0
- data/lib/rubocop/cop/cop.rb +33 -0
- data/lib/rubocop/cop/def_parentheses.rb +1 -1
- data/lib/rubocop/cop/empty_line_between_defs.rb +26 -0
- data/lib/rubocop/cop/empty_lines.rb +10 -13
- data/lib/rubocop/cop/eval.rb +22 -0
- data/lib/rubocop/cop/favor_join.rb +37 -0
- data/lib/rubocop/cop/grammar.rb +2 -2
- data/lib/rubocop/cop/hash_literal.rb +43 -4
- data/lib/rubocop/cop/hash_syntax.rb +2 -2
- data/lib/rubocop/cop/if_then_else.rb +1 -1
- data/lib/rubocop/cop/leading_comment_space.rb +20 -0
- data/lib/rubocop/cop/line_continuation.rb +18 -0
- data/lib/rubocop/cop/line_length.rb +1 -1
- data/lib/rubocop/cop/method_and_variable_snake_case.rb +7 -6
- data/lib/rubocop/cop/method_length.rb +4 -15
- data/lib/rubocop/cop/not.rb +15 -0
- data/lib/rubocop/cop/offence.rb +9 -0
- data/lib/rubocop/cop/op_method.rb +6 -1
- data/lib/rubocop/cop/semicolon.rb +74 -3
- data/lib/rubocop/cop/single_line_methods.rb +60 -0
- data/lib/rubocop/cop/space_after_comma_etc.rb +4 -5
- data/lib/rubocop/cop/space_after_control_keyword.rb +28 -0
- data/lib/rubocop/cop/surrounding_space.rb +48 -9
- data/lib/rubocop/cop/symbol_array.rb +29 -0
- data/lib/rubocop/cop/symbol_snake_case.rb +20 -0
- data/lib/rubocop/cop/trivial_accessors.rb +103 -0
- data/lib/rubocop/cop/unless_else.rb +1 -1
- data/lib/rubocop/cop/variable_interpolation.rb +3 -2
- data/lib/rubocop/cop/word_array.rb +38 -0
- data/lib/rubocop/report/plain_text.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +22 -7
- data/rubocop.gemspec +11 -7
- data/spec/project_spec.rb +27 -0
- data/spec/rubocop/cli_spec.rb +556 -388
- data/spec/rubocop/config_spec.rb +399 -0
- data/spec/rubocop/config_store_spec.rb +66 -0
- data/spec/rubocop/cops/alias_spec.rb +7 -0
- data/spec/rubocop/cops/array_literal_spec.rb +18 -1
- data/spec/rubocop/cops/ascii_comments_spec.rb +27 -0
- data/spec/rubocop/cops/ascii_identifiers_spec.rb +27 -0
- data/spec/rubocop/cops/avoid_for_spec.rb +15 -1
- data/spec/rubocop/cops/avoid_global_vars.rb +32 -0
- data/spec/rubocop/cops/block_comments_spec.rb +29 -0
- data/spec/rubocop/cops/brace_after_percent_spec.rb +19 -13
- data/spec/rubocop/cops/{indentation_spec.rb → case_indentation_spec.rb} +2 -2
- data/spec/rubocop/cops/class_methods_spec.rb +49 -0
- data/spec/rubocop/cops/colon_method_call_spec.rb +47 -0
- data/spec/rubocop/cops/empty_line_between_defs_spec.rb +83 -0
- data/spec/rubocop/cops/empty_lines_spec.rb +6 -63
- data/spec/rubocop/cops/eval_spec.rb +36 -0
- data/spec/rubocop/cops/favor_join_spec.rb +39 -0
- data/spec/rubocop/cops/hash_literal_spec.rb +18 -1
- data/spec/rubocop/cops/leading_comment_space_spec.rb +60 -0
- data/spec/rubocop/cops/line_continuation_spec.rb +24 -0
- data/spec/rubocop/cops/line_length_spec.rb +1 -0
- data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +20 -0
- data/spec/rubocop/cops/method_length_spec.rb +2 -5
- data/spec/rubocop/cops/new_lambda_literal_spec.rb +2 -3
- data/spec/rubocop/cops/not_spec.rb +34 -0
- data/spec/rubocop/cops/offence_spec.rb +7 -0
- data/spec/rubocop/cops/op_method_spec.rb +11 -0
- data/spec/rubocop/cops/semicolon_spec.rb +79 -4
- data/spec/rubocop/cops/single_line_methods_spec.rb +50 -0
- data/spec/rubocop/cops/space_after_control_keyword_spec.rb +28 -0
- data/spec/rubocop/cops/space_after_semicolon_spec.rb +5 -0
- data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +11 -1
- data/spec/rubocop/cops/space_inside_hash_literal_braces_spec.rb +74 -0
- data/spec/rubocop/cops/symbol_array_spec.rb +25 -0
- data/spec/rubocop/cops/symbol_snake_case_spec.rb +13 -0
- data/spec/rubocop/cops/trivial_accessors_spec.rb +332 -0
- data/spec/rubocop/cops/variable_interpolation_spec.rb +10 -1
- data/spec/rubocop/cops/word_array_spec.rb +39 -0
- data/spec/spec_helper.rb +18 -5
- data/spec/support/file_helper.rb +21 -0
- data/spec/support/isolated_environment.rb +27 -0
- metadata +82 -36
- data/spec/rubocop/cops/ascii_identifiers_and_comments_spec.rb +0 -38
data/lib/rubocop/cli.rb
CHANGED
|
@@ -1,95 +1,162 @@
|
|
|
1
1
|
# encoding: utf-8
|
|
2
|
-
|
|
2
|
+
require 'pathname'
|
|
3
3
|
require 'optparse'
|
|
4
|
-
require 'yaml'
|
|
5
4
|
require_relative 'cop/grammar'
|
|
6
5
|
|
|
7
6
|
module Rubocop
|
|
8
7
|
# The CLI is a class responsible of handling all the command line interface
|
|
9
8
|
# logic.
|
|
10
9
|
class CLI
|
|
10
|
+
# If set true while running,
|
|
11
|
+
# RuboCop will abort processing and exit gracefully.
|
|
12
|
+
attr_accessor :wants_to_quit
|
|
13
|
+
attr_accessor :options
|
|
14
|
+
|
|
15
|
+
alias_method :wants_to_quit?, :wants_to_quit
|
|
16
|
+
|
|
17
|
+
def initialize
|
|
18
|
+
@cops = Cop::Cop.all
|
|
19
|
+
@processed_file_count = 0
|
|
20
|
+
@total_offences = 0
|
|
21
|
+
@errors = []
|
|
22
|
+
@options = { mode: :default }
|
|
23
|
+
ConfigStore.prepare
|
|
24
|
+
end
|
|
25
|
+
|
|
11
26
|
# Entry point for the application logic. Here we
|
|
12
27
|
# do the command line arguments processing and inspect
|
|
13
28
|
# the target files
|
|
14
29
|
# @return [Fixnum] UNIX exit code
|
|
15
30
|
def run(args = ARGV)
|
|
16
|
-
|
|
31
|
+
trap_interrupt
|
|
17
32
|
|
|
18
|
-
|
|
19
|
-
opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'
|
|
20
|
-
|
|
21
|
-
opts.on('-d', '--[no-]debug', 'Display debug info') do |d|
|
|
22
|
-
$options[:debug] = d
|
|
23
|
-
end
|
|
24
|
-
opts.on('-e', '--emacs', 'Emacs style output') do
|
|
25
|
-
$options[:mode] = :emacs_style
|
|
26
|
-
end
|
|
27
|
-
opts.on('-c FILE', '--config FILE', 'Configuration file') do |f|
|
|
28
|
-
$options[:config] = YAML.load_file(f)
|
|
29
|
-
end
|
|
30
|
-
opts.on('-s', '--silent', 'Silence summary') do |s|
|
|
31
|
-
$options[:silent] = s
|
|
32
|
-
end
|
|
33
|
-
opts.on('-v', '--version', 'Display version') do
|
|
34
|
-
puts Rubocop::Version::STRING
|
|
35
|
-
exit(0)
|
|
36
|
-
end
|
|
37
|
-
end.parse!(args)
|
|
33
|
+
parse_options(args)
|
|
38
34
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
35
|
+
begin
|
|
36
|
+
handle_only_option if @options[:only]
|
|
37
|
+
rescue ArgumentError => e
|
|
38
|
+
puts e.message
|
|
39
|
+
return 1
|
|
40
|
+
end
|
|
43
41
|
|
|
44
42
|
target_files(args).each do |file|
|
|
45
|
-
|
|
43
|
+
break if wants_to_quit?
|
|
46
44
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
45
|
+
config = ConfigStore.for(file)
|
|
46
|
+
report = Report.create(file, @options[:mode])
|
|
47
|
+
source = read_source(file)
|
|
48
|
+
|
|
49
|
+
puts "Scanning #{file}" if @options[:debug]
|
|
52
50
|
|
|
53
51
|
syntax_cop = Rubocop::Cop::Syntax.new
|
|
52
|
+
syntax_cop.debug = @options[:debug]
|
|
54
53
|
syntax_cop.inspect(file, source, nil, nil)
|
|
55
54
|
|
|
56
55
|
if syntax_cop.offences.map(&:severity).include?(:error)
|
|
57
56
|
# In case of a syntax error we just report that error and do
|
|
58
57
|
# no more checking in the file.
|
|
59
58
|
report << syntax_cop
|
|
60
|
-
total_offences += syntax_cop.offences.count
|
|
59
|
+
@total_offences += syntax_cop.offences.count
|
|
61
60
|
else
|
|
62
|
-
|
|
63
|
-
config = $options[:config] || config_from_dotfile(File.dirname(file))
|
|
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
|
|
78
|
-
end
|
|
61
|
+
inspect_file(file, source, config, report)
|
|
79
62
|
end
|
|
80
63
|
|
|
64
|
+
@processed_file_count += 1
|
|
81
65
|
report.display unless report.empty?
|
|
82
66
|
end
|
|
83
67
|
|
|
84
|
-
unless
|
|
85
|
-
display_summary(
|
|
68
|
+
unless @options[:silent]
|
|
69
|
+
display_summary(@processed_file_count, @total_offences, @errors)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
(@total_offences == 0) && !wants_to_quit ? 0 : 1
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def handle_only_option
|
|
76
|
+
@cops = @cops.select { |c| c.cop_name == @options[:only] }
|
|
77
|
+
if @cops.empty?
|
|
78
|
+
fail ArgumentError, "Unrecognized cop name: #{@options[:only]}."
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def read_source(file)
|
|
83
|
+
get_rid_of_invalid_byte_sequences(File.read(file)).split($RS)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def inspect_file(file, source, config, report)
|
|
87
|
+
tokens, sexp, correlations = CLI.rip_source(source)
|
|
88
|
+
disabled_lines = disabled_lines_in(source)
|
|
89
|
+
|
|
90
|
+
@cops.each do |cop_klass|
|
|
91
|
+
cop_name = cop_klass.cop_name
|
|
92
|
+
cop_config = config.for_cop(cop_name)
|
|
93
|
+
if config.cop_enabled?(cop_name)
|
|
94
|
+
cop_klass.config = cop_config
|
|
95
|
+
cop = cop_klass.new
|
|
96
|
+
cop.debug = @options[:debug]
|
|
97
|
+
cop.correlations = correlations
|
|
98
|
+
cop.disabled_lines = disabled_lines[cop_name]
|
|
99
|
+
begin
|
|
100
|
+
cop.inspect(file, source, tokens, sexp)
|
|
101
|
+
rescue => e
|
|
102
|
+
message = "An error occurred while #{cop.name} cop".color(:red) +
|
|
103
|
+
" was inspecting #{file}.".color(:red)
|
|
104
|
+
@errors << message
|
|
105
|
+
warn message
|
|
106
|
+
if @options[:debug]
|
|
107
|
+
puts e.message, e.backtrace
|
|
108
|
+
else
|
|
109
|
+
warn 'To see the complete backtrace run rubocop -d.'
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
@total_offences += cop.offences.count
|
|
113
|
+
report << cop if cop.has_report?
|
|
114
|
+
end
|
|
86
115
|
end
|
|
116
|
+
end
|
|
87
117
|
|
|
88
|
-
|
|
118
|
+
def parse_options(args)
|
|
119
|
+
OptionParser.new do |opts|
|
|
120
|
+
opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'
|
|
121
|
+
|
|
122
|
+
opts.on('-d', '--debug', 'Display debug info') do |d|
|
|
123
|
+
@options[:debug] = d
|
|
124
|
+
end
|
|
125
|
+
opts.on('-e', '--emacs', 'Emacs style output') do
|
|
126
|
+
@options[:mode] = :emacs_style
|
|
127
|
+
end
|
|
128
|
+
opts.on('-c FILE', '--config FILE', 'Configuration file') do |f|
|
|
129
|
+
@options[:config] = f
|
|
130
|
+
ConfigStore.set_options_config(@options[:config])
|
|
131
|
+
end
|
|
132
|
+
opts.on('--only COP', 'Run just one cop') do |s|
|
|
133
|
+
@options[:only] = s
|
|
134
|
+
end
|
|
135
|
+
opts.on('-s', '--silent', 'Silence summary') do |s|
|
|
136
|
+
@options[:silent] = s
|
|
137
|
+
end
|
|
138
|
+
opts.on('-n', '--no-color', 'Disable color output') do |s|
|
|
139
|
+
Sickill::Rainbow.enabled = false
|
|
140
|
+
end
|
|
141
|
+
opts.on('-v', '--version', 'Display version') do
|
|
142
|
+
puts Rubocop::Version::STRING
|
|
143
|
+
exit(0)
|
|
144
|
+
end
|
|
145
|
+
end.parse!(args)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def trap_interrupt
|
|
149
|
+
Signal.trap('INT') do
|
|
150
|
+
exit!(1) if wants_to_quit?
|
|
151
|
+
self.wants_to_quit = true
|
|
152
|
+
$stderr.puts
|
|
153
|
+
$stderr.puts 'Exiting... Interrupt again to exit immediately.'
|
|
154
|
+
end
|
|
89
155
|
end
|
|
90
156
|
|
|
91
|
-
def display_summary(num_files, total_offences)
|
|
92
|
-
|
|
157
|
+
def display_summary(num_files, total_offences, errors)
|
|
158
|
+
plural = num_files == 0 || num_files > 1 ? 's' : ''
|
|
159
|
+
print "\n#{num_files} file#{plural} inspected, "
|
|
93
160
|
offences_string = if total_offences.zero?
|
|
94
161
|
'no offences'
|
|
95
162
|
elsif total_offences == 1
|
|
@@ -98,7 +165,15 @@ module Rubocop
|
|
|
98
165
|
"#{total_offences} offences"
|
|
99
166
|
end
|
|
100
167
|
puts "#{offences_string} detected"
|
|
101
|
-
.
|
|
168
|
+
.color(total_offences.zero? ? :green : :red)
|
|
169
|
+
|
|
170
|
+
if errors.count > 0
|
|
171
|
+
plural = errors.count > 1 ? 's' : ''
|
|
172
|
+
puts "\n#{errors.count} error#{plural} occurred:".color(:red)
|
|
173
|
+
errors.each { |error| puts error }
|
|
174
|
+
puts 'Errors are usually caused by RuboCop bugs.'
|
|
175
|
+
puts 'Please, report your problems to RuboCop\'s issue tracker.'
|
|
176
|
+
end
|
|
102
177
|
end
|
|
103
178
|
|
|
104
179
|
def disabled_lines_in(source)
|
|
@@ -127,19 +202,18 @@ module Rubocop
|
|
|
127
202
|
match = line.match(regexp)
|
|
128
203
|
if match
|
|
129
204
|
kind, cops = match.captures
|
|
130
|
-
if cops.include?('all')
|
|
131
|
-
cops = Cop::Cop.all.map { |c| c.name.split('::').last }.join(',')
|
|
132
|
-
end
|
|
205
|
+
cops = Cop::Cop.all.map(&:cop_name).join(',') if cops.include?('all')
|
|
133
206
|
cops.split(/,\s*/).each { |cop_name| yield cop_name, kind }
|
|
134
207
|
end
|
|
135
208
|
end
|
|
136
209
|
|
|
137
|
-
def get_rid_of_invalid_byte_sequences(
|
|
138
|
-
|
|
210
|
+
def get_rid_of_invalid_byte_sequences(source)
|
|
211
|
+
source_encoding = source.encoding.name
|
|
139
212
|
# UTF-16 works better in this algorithm but is not supported in 1.9.2.
|
|
140
213
|
temporary_encoding = (RUBY_VERSION == '1.9.2') ? 'UTF-8' : 'UTF-16'
|
|
141
|
-
|
|
142
|
-
|
|
214
|
+
source.encode!(temporary_encoding, source_encoding,
|
|
215
|
+
invalid: :replace, replace: '')
|
|
216
|
+
source.encode!(source_encoding, temporary_encoding)
|
|
143
217
|
end
|
|
144
218
|
|
|
145
219
|
def self.rip_source(source)
|
|
@@ -150,48 +224,6 @@ module Rubocop
|
|
|
150
224
|
[tokens, sexp, correlations]
|
|
151
225
|
end
|
|
152
226
|
|
|
153
|
-
# Returns the configuration hash from .rubocop.yml searching
|
|
154
|
-
# upwards in the directory structure starting at the given
|
|
155
|
-
# directory where the inspected file is. If no .rubocop.yml is
|
|
156
|
-
# found there, the user's home directory is checked.
|
|
157
|
-
def config_from_dotfile(target_file_dir)
|
|
158
|
-
return unless target_file_dir
|
|
159
|
-
# @configs is a cache that maps directories to
|
|
160
|
-
# configurations. We search for .rubocop.yml only if we haven't
|
|
161
|
-
# already found it for the given directory.
|
|
162
|
-
unless @configs[target_file_dir]
|
|
163
|
-
dir = target_file_dir
|
|
164
|
-
while dir != '/'
|
|
165
|
-
path = File.join(dir, '.rubocop.yml')
|
|
166
|
-
if File.exist?(path)
|
|
167
|
-
@configs[target_file_dir] = YAML.load_file(path)
|
|
168
|
-
return @configs[target_file_dir]
|
|
169
|
-
end
|
|
170
|
-
dir = File.expand_path('..', dir)
|
|
171
|
-
end
|
|
172
|
-
path = File.join(Dir.home, '.rubocop.yml')
|
|
173
|
-
@configs[target_file_dir] = YAML.load_file(path) if File.exist?(path)
|
|
174
|
-
end
|
|
175
|
-
@configs[target_file_dir]
|
|
176
|
-
end
|
|
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
|
-
|
|
189
|
-
def show_cops_on_duty(cops)
|
|
190
|
-
puts '== Reporting for duty =='
|
|
191
|
-
cops.each { |c| puts ' * '.yellow + c.to_s.green }
|
|
192
|
-
puts '========================'
|
|
193
|
-
end
|
|
194
|
-
|
|
195
227
|
# Generate a list of target files by expanding globing patterns
|
|
196
228
|
# (if any). If args is empty recursively finds all Ruby source
|
|
197
229
|
# files under the current directory
|
|
@@ -203,21 +235,23 @@ module Rubocop
|
|
|
203
235
|
|
|
204
236
|
args.each do |target|
|
|
205
237
|
if File.directory?(target)
|
|
206
|
-
files
|
|
238
|
+
files += ruby_files(target.chomp(File::SEPARATOR))
|
|
207
239
|
elsif target =~ /\*/
|
|
208
|
-
files
|
|
240
|
+
files += Dir[target]
|
|
209
241
|
else
|
|
210
242
|
files << target
|
|
211
243
|
end
|
|
212
244
|
end
|
|
213
245
|
|
|
214
|
-
files.
|
|
246
|
+
files.uniq
|
|
215
247
|
end
|
|
216
248
|
|
|
217
249
|
# Finds all Ruby source files under the current or other supplied
|
|
218
250
|
# directory. A Ruby source file is defined as a file with the `.rb`
|
|
219
251
|
# extension or a file with no extension that has a ruby shebang line
|
|
220
252
|
# as its first line.
|
|
253
|
+
# It is possible to specify includes and excludes using the config file,
|
|
254
|
+
# so you can include other Ruby files like Rakefiles and gemspecs.
|
|
221
255
|
# @param root Root directory under which to search for ruby source files
|
|
222
256
|
# @return [Array] Array of filenames
|
|
223
257
|
def ruby_files(root = Dir.pwd)
|
|
@@ -225,8 +259,8 @@ module Rubocop
|
|
|
225
259
|
|
|
226
260
|
rb = []
|
|
227
261
|
|
|
228
|
-
rb
|
|
229
|
-
rb
|
|
262
|
+
rb += files.select { |file| File.extname(file) == '.rb' }
|
|
263
|
+
rb += files.select do |file|
|
|
230
264
|
File.extname(file) == '' &&
|
|
231
265
|
begin
|
|
232
266
|
File.open(file) { |f| f.readline } =~ /#!.*ruby/
|
|
@@ -236,13 +270,21 @@ module Rubocop
|
|
|
236
270
|
end
|
|
237
271
|
end
|
|
238
272
|
|
|
239
|
-
rb.
|
|
273
|
+
rb += files.select do |file|
|
|
274
|
+
config = ConfigStore.for(file)
|
|
275
|
+
config.file_to_include?(file)
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
rb.reject do |file|
|
|
279
|
+
config = ConfigStore.for(file)
|
|
280
|
+
config.file_to_exclude?(file)
|
|
281
|
+
end.uniq
|
|
240
282
|
end
|
|
241
283
|
|
|
242
284
|
private
|
|
243
285
|
|
|
244
|
-
def log_error(e, msg='')
|
|
245
|
-
if
|
|
286
|
+
def log_error(e, msg = '')
|
|
287
|
+
if @options[:debug]
|
|
246
288
|
error_message = "#{e.class}, #{e.message}"
|
|
247
289
|
STDERR.puts "#{msg}\t#{error_message}"
|
|
248
290
|
end
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'delegate'
|
|
4
|
+
require 'yaml'
|
|
5
|
+
require 'pathname'
|
|
6
|
+
|
|
7
|
+
module Rubocop
|
|
8
|
+
class Config < DelegateClass(Hash)
|
|
9
|
+
class ValidationError < StandardError; end
|
|
10
|
+
|
|
11
|
+
DOTFILE = '.rubocop.yml'
|
|
12
|
+
RUBOCOP_HOME = File.realpath(File.join(File.dirname(__FILE__), '..', '..'))
|
|
13
|
+
DEFAULT_FILE = File.join(RUBOCOP_HOME, 'config', 'default.yml')
|
|
14
|
+
|
|
15
|
+
attr_reader :loaded_path
|
|
16
|
+
|
|
17
|
+
class << self
|
|
18
|
+
def load_file(path)
|
|
19
|
+
hash = YAML.load_file(path)
|
|
20
|
+
|
|
21
|
+
base_configs(path, hash['inherit_from']).reverse.each do |base_config|
|
|
22
|
+
base_config.each do |key, value|
|
|
23
|
+
if value.is_a?(Hash)
|
|
24
|
+
hash[key] = hash.has_key?(key) ? merge(value, hash[key]) : value
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
hash.delete('inherit_from')
|
|
30
|
+
config = new(hash, File.realpath(path))
|
|
31
|
+
config.warn_unless_valid
|
|
32
|
+
config
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Return a recursive merge of two hashes. That is, a normal hash
|
|
36
|
+
# merge, with the addition that any value that is a hash, and
|
|
37
|
+
# occurs in both arguments, will also be merged. And so on.
|
|
38
|
+
def merge(base_hash, derived_hash)
|
|
39
|
+
result = {}
|
|
40
|
+
base_hash.each do |key, value|
|
|
41
|
+
result[key] = if derived_hash.has_key?(key)
|
|
42
|
+
if value.is_a?(Hash)
|
|
43
|
+
merge(value, derived_hash[key])
|
|
44
|
+
else
|
|
45
|
+
derived_hash[key]
|
|
46
|
+
end
|
|
47
|
+
else
|
|
48
|
+
base_hash[key]
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
derived_hash.each do |key, value|
|
|
52
|
+
result[key] = value unless base_hash.has_key?(key)
|
|
53
|
+
end
|
|
54
|
+
result
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def base_configs(path, inherit_from)
|
|
58
|
+
base_files = case inherit_from
|
|
59
|
+
when nil then []
|
|
60
|
+
when String then [inherit_from]
|
|
61
|
+
when Array then inherit_from
|
|
62
|
+
end
|
|
63
|
+
base_files.map do |f|
|
|
64
|
+
f = File.join(File.dirname(path), f) unless f.start_with?('/')
|
|
65
|
+
load_file(f)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Returns the path of .rubocop.yml searching upwards in the
|
|
70
|
+
# directory structure starting at the given directory where the
|
|
71
|
+
# inspected file is. If no .rubocop.yml is found there, the
|
|
72
|
+
# user's home directory is checked. If there's no .rubocop.yml
|
|
73
|
+
# there either, the path to the default file is returned.
|
|
74
|
+
def configuration_file_for(target_dir)
|
|
75
|
+
possible_config_files = dirs_to_search(target_dir).map do |dir|
|
|
76
|
+
File.join(dir, DOTFILE)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
found_file = possible_config_files.find do |config_file|
|
|
80
|
+
File.exist?(config_file)
|
|
81
|
+
end
|
|
82
|
+
found_file || DEFAULT_FILE
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def configuration_from_file(config_file)
|
|
86
|
+
config = load_file(config_file)
|
|
87
|
+
merge_with_default(config, config_file)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def merge_with_default(config, config_file)
|
|
91
|
+
default_config = load_file(DEFAULT_FILE)
|
|
92
|
+
new(merge(default_config, config), config_file)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
private
|
|
96
|
+
|
|
97
|
+
def dirs_to_search(target_dir)
|
|
98
|
+
dirs_to_search = []
|
|
99
|
+
target_dir_pathname = Pathname.new(File.expand_path(target_dir))
|
|
100
|
+
target_dir_pathname.ascend do |dir_pathname|
|
|
101
|
+
dirs_to_search << dir_pathname.to_s
|
|
102
|
+
end
|
|
103
|
+
dirs_to_search << Dir.home
|
|
104
|
+
dirs_to_search
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def initialize(hash = {}, loaded_path = nil)
|
|
109
|
+
@hash = hash
|
|
110
|
+
@loaded_path = loaded_path
|
|
111
|
+
super(@hash)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def for_cop(cop)
|
|
115
|
+
self[cop]
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def cop_enabled?(cop)
|
|
119
|
+
self[cop].nil? || self[cop]['Enabled']
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def warn_unless_valid
|
|
123
|
+
validate!
|
|
124
|
+
rescue Config::ValidationError => e
|
|
125
|
+
puts "Warning: #{e.message}".color(:red)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# TODO: This should be a private method
|
|
129
|
+
def validate!
|
|
130
|
+
# Don't validate RuboCop's own files. Avoids inifinite recursion.
|
|
131
|
+
return if @loaded_path.start_with?(RUBOCOP_HOME)
|
|
132
|
+
|
|
133
|
+
default_config = Config.load_file(DEFAULT_FILE)
|
|
134
|
+
|
|
135
|
+
valid_cop_names, invalid_cop_names = @hash.keys.partition do |key|
|
|
136
|
+
default_config.has_key?(key)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
invalid_cop_names.each do |name|
|
|
140
|
+
fail ValidationError,
|
|
141
|
+
"unrecognized cop #{name} found in #{loaded_path || self}"
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
valid_cop_names.each do |name|
|
|
145
|
+
@hash[name].each_key do |param|
|
|
146
|
+
unless default_config[name].has_key?(param)
|
|
147
|
+
fail ValidationError,
|
|
148
|
+
"unrecognized parameter #{name}:#{param} found " +
|
|
149
|
+
"in #{loaded_path || self}"
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def file_to_include?(file)
|
|
156
|
+
relative_file_path = relative_path_to_loaded_dir(file)
|
|
157
|
+
patterns_to_include.any? do |pattern|
|
|
158
|
+
match_path?(pattern, relative_file_path)
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def file_to_exclude?(file)
|
|
163
|
+
relative_file_path = relative_path_to_loaded_dir(file)
|
|
164
|
+
patterns_to_exclude.any? do |pattern|
|
|
165
|
+
match_path?(pattern, relative_file_path)
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def patterns_to_include
|
|
170
|
+
@hash['AllCops']['Includes']
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def patterns_to_exclude
|
|
174
|
+
@hash['AllCops']['Excludes']
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
private
|
|
178
|
+
|
|
179
|
+
def relative_path_to_loaded_dir(file)
|
|
180
|
+
return file unless loaded_path
|
|
181
|
+
file_pathname = Pathname.new(File.expand_path(file))
|
|
182
|
+
file_pathname.relative_path_from(loaded_dir_pathname).to_s
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def loaded_dir_pathname
|
|
186
|
+
return nil unless loaded_path
|
|
187
|
+
@loaded_dir ||= begin
|
|
188
|
+
loaded_dir = File.expand_path(File.dirname(loaded_path))
|
|
189
|
+
Pathname.new(loaded_dir)
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def match_path?(pattern, path)
|
|
194
|
+
case pattern
|
|
195
|
+
when String
|
|
196
|
+
File.basename(path) == pattern || File.fnmatch(pattern, path)
|
|
197
|
+
when Regexp
|
|
198
|
+
path =~ pattern
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module ConfigStore
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
def prepare
|
|
8
|
+
# @options_config stores a config that is specified in the command line.
|
|
9
|
+
# This takes precedence over configs located in any directories
|
|
10
|
+
@options_config = nil
|
|
11
|
+
|
|
12
|
+
# @path_cache maps directories to configuration paths. We search
|
|
13
|
+
# for .rubocop.yml only if we haven't already found it for the
|
|
14
|
+
# given directory.
|
|
15
|
+
@path_cache = {}
|
|
16
|
+
|
|
17
|
+
# @object_cache maps configuration file paths to
|
|
18
|
+
# configuration objects so we only need to load them once.
|
|
19
|
+
@object_cache = {}
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def set_options_config(options_config)
|
|
23
|
+
loaded_config = Config.load_file(options_config)
|
|
24
|
+
@options_config = Config.merge_with_default(loaded_config,
|
|
25
|
+
options_config)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def for(file)
|
|
29
|
+
return @options_config if @options_config
|
|
30
|
+
|
|
31
|
+
dir = File.dirname(file)
|
|
32
|
+
@path_cache[dir] ||= Config.configuration_file_for(dir)
|
|
33
|
+
path = @path_cache[dir]
|
|
34
|
+
@object_cache[path] ||= Config.configuration_from_file(path)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
data/lib/rubocop/cop/alias.rb
CHANGED
|
@@ -6,11 +6,8 @@ module Rubocop
|
|
|
6
6
|
ERROR_MESSAGE = 'Use alias_method instead of alias.'
|
|
7
7
|
|
|
8
8
|
def inspect(file, source, tokens, sexp)
|
|
9
|
-
tokens
|
|
10
|
-
t
|
|
11
|
-
if t.type == :on_kw && t.text == 'alias'
|
|
12
|
-
add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
|
|
13
|
-
end
|
|
9
|
+
each_keyword('alias', tokens) do |t|
|
|
10
|
+
add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
|
|
14
11
|
end
|
|
15
12
|
end
|
|
16
13
|
end
|
|
@@ -6,16 +6,55 @@ module Rubocop
|
|
|
6
6
|
ERROR_MESSAGE = 'Use array literal [] instead of Array.new.'
|
|
7
7
|
|
|
8
8
|
def inspect(file, source, tokens, sexp)
|
|
9
|
+
offences = preliminary_scan(sexp)
|
|
10
|
+
|
|
11
|
+
# find Array.new()
|
|
9
12
|
each(:method_add_arg, sexp) do |s|
|
|
10
|
-
|
|
13
|
+
next if s[1][0] != :call
|
|
14
|
+
|
|
15
|
+
receiver = s[1][1][1]
|
|
16
|
+
method_name = s[1][3][1]
|
|
11
17
|
|
|
12
|
-
if
|
|
13
|
-
s[2] == [:arg_paren, nil]
|
|
18
|
+
if receiver && receiver[1] == 'Array' &&
|
|
19
|
+
method_name == 'new' && s[2] == [:arg_paren, nil]
|
|
20
|
+
offences.delete(Offence.new(:convention,
|
|
21
|
+
receiver[2].lineno,
|
|
22
|
+
ERROR_MESSAGE))
|
|
14
23
|
add_offence(:convention,
|
|
15
|
-
|
|
24
|
+
receiver[2].lineno,
|
|
16
25
|
ERROR_MESSAGE)
|
|
17
26
|
end
|
|
27
|
+
|
|
28
|
+
# check for false positives
|
|
29
|
+
if receiver && receiver[1] == 'Array' &&
|
|
30
|
+
method_name == 'new' && s[2] != [:arg_paren, nil]
|
|
31
|
+
offences.delete(Offence.new(:convention,
|
|
32
|
+
receiver[2].lineno,
|
|
33
|
+
ERROR_MESSAGE))
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
offences.each { |o| add_offence(*o.explode) }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def preliminary_scan(sexp)
|
|
41
|
+
offences = []
|
|
42
|
+
|
|
43
|
+
# find Array.new
|
|
44
|
+
# there will be some false positives here, which
|
|
45
|
+
# we'll eliminate later on
|
|
46
|
+
each(:call, sexp) do |s|
|
|
47
|
+
receiver = s[1][1]
|
|
48
|
+
|
|
49
|
+
if receiver && receiver[1] == 'Array' &&
|
|
50
|
+
s[3][1] == 'new'
|
|
51
|
+
offences << Offence.new(:convention,
|
|
52
|
+
receiver[2].lineno,
|
|
53
|
+
ERROR_MESSAGE)
|
|
54
|
+
end
|
|
18
55
|
end
|
|
56
|
+
|
|
57
|
+
offences
|
|
19
58
|
end
|
|
20
59
|
end
|
|
21
60
|
end
|