rubocop 0.6.1 → 0.7.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.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -266
- data/CHANGELOG.md +49 -7
- data/README.md +75 -2
- data/Rakefile +2 -2
- data/bin/rubocop +15 -10
- data/lib/rubocop.rb +19 -1
- data/lib/rubocop/cli.rb +113 -116
- 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/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 +30 -2
- 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/semicolon.rb +74 -3
- data/lib/rubocop/cop/single_line_methods.rb +60 -0
- 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/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/version.rb +1 -1
- data/rubocop.gemspec +11 -7
- data/spec/project_spec.rb +27 -0
- data/spec/rubocop/cli_spec.rb +549 -487
- 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 +8 -1
- 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 +8 -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/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_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/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 +16 -9
- data/spec/support/file_helper.rb +21 -0
- data/spec/support/isolated_environment.rb +27 -0
- metadata +66 -6
data/Rakefile
CHANGED
@@ -7,7 +7,7 @@ begin
|
|
7
7
|
Bundler.setup(:default, :development)
|
8
8
|
rescue Bundler::BundlerError => e
|
9
9
|
$stderr.puts e.message
|
10
|
-
$stderr.puts
|
10
|
+
$stderr.puts 'Run `bundle install` to install missing gems'
|
11
11
|
exit e.status_code
|
12
12
|
end
|
13
13
|
require 'rake'
|
@@ -23,7 +23,7 @@ task :coverage do
|
|
23
23
|
Rake::Task['spec'].execute
|
24
24
|
end
|
25
25
|
|
26
|
-
task :
|
26
|
+
task default: :spec
|
27
27
|
|
28
28
|
require 'yard'
|
29
29
|
YARD::Rake::YardocTask.new
|
data/bin/rubocop
CHANGED
@@ -1,17 +1,22 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# encoding: utf-8
|
3
3
|
|
4
|
-
|
4
|
+
if RUBY_ENGINE == 'ruby' && RUBY_VERSION >= '1.9.2'
|
5
|
+
$LOAD_PATH.unshift(File.dirname(File.realpath(__FILE__)) + '/../lib')
|
5
6
|
|
6
|
-
require 'rubocop'
|
7
|
-
require 'benchmark'
|
7
|
+
require 'rubocop'
|
8
|
+
require 'benchmark'
|
8
9
|
|
9
|
-
cli = Rubocop::CLI.new
|
10
|
-
result = 0
|
10
|
+
cli = Rubocop::CLI.new
|
11
|
+
result = 0
|
11
12
|
|
12
|
-
time = Benchmark.realtime do
|
13
|
-
|
14
|
-
end
|
13
|
+
time = Benchmark.realtime do
|
14
|
+
result = cli.run
|
15
|
+
end
|
15
16
|
|
16
|
-
puts "Finished in #{time} seconds" if
|
17
|
-
exit result
|
17
|
+
puts "Finished in #{time} seconds" if cli.options[:debug]
|
18
|
+
exit result
|
19
|
+
else
|
20
|
+
puts 'RuboCop supports only MRI 1.9.2+'
|
21
|
+
exit(-1)
|
22
|
+
end
|
data/lib/rubocop.rb
CHANGED
@@ -2,18 +2,22 @@
|
|
2
2
|
|
3
3
|
require 'ripper'
|
4
4
|
require 'rainbow'
|
5
|
+
require 'English'
|
5
6
|
|
6
7
|
require 'rubocop/cop/offence'
|
7
8
|
require 'rubocop/cop/cop'
|
8
9
|
require 'rubocop/cop/encoding'
|
9
10
|
require 'rubocop/cop/line_length'
|
11
|
+
require 'rubocop/cop/line_continuation'
|
10
12
|
require 'rubocop/cop/syntax'
|
11
13
|
require 'rubocop/cop/tab'
|
12
14
|
require 'rubocop/cop/trailing_whitespace'
|
13
|
-
require 'rubocop/cop/
|
15
|
+
require 'rubocop/cop/case_indentation'
|
16
|
+
require 'rubocop/cop/empty_line_between_defs'
|
14
17
|
require 'rubocop/cop/empty_lines'
|
15
18
|
require 'rubocop/cop/surrounding_space'
|
16
19
|
require 'rubocop/cop/space_after_comma_etc'
|
20
|
+
require 'rubocop/cop/space_after_control_keyword'
|
17
21
|
require 'rubocop/cop/hash_syntax'
|
18
22
|
require 'rubocop/cop/end_of_line'
|
19
23
|
require 'rubocop/cop/numeric_literals'
|
@@ -37,11 +41,13 @@ require 'rubocop/cop/collection_methods'
|
|
37
41
|
require 'rubocop/cop/avoid_for'
|
38
42
|
require 'rubocop/cop/avoid_perlisms'
|
39
43
|
require 'rubocop/cop/avoid_perl_backrefs'
|
44
|
+
require 'rubocop/cop/avoid_global_vars'
|
40
45
|
require 'rubocop/cop/avoid_class_vars'
|
41
46
|
require 'rubocop/cop/symbol_snake_case'
|
42
47
|
require 'rubocop/cop/variable_interpolation'
|
43
48
|
require 'rubocop/cop/semicolon'
|
44
49
|
require 'rubocop/cop/favor_sprintf'
|
50
|
+
require 'rubocop/cop/favor_join'
|
45
51
|
require 'rubocop/cop/alias'
|
46
52
|
require 'rubocop/cop/rescue_modifier'
|
47
53
|
require 'rubocop/cop/percent_literals'
|
@@ -51,6 +57,7 @@ require 'rubocop/cop/handle_exceptions'
|
|
51
57
|
require 'rubocop/cop/rescue_exception'
|
52
58
|
require 'rubocop/cop/ascii_identifiers'
|
53
59
|
require 'rubocop/cop/ascii_comments'
|
60
|
+
require 'rubocop/cop/block_comments'
|
54
61
|
require 'rubocop/cop/hash_literal'
|
55
62
|
require 'rubocop/cop/array_literal'
|
56
63
|
require 'rubocop/cop/method_length'
|
@@ -58,10 +65,21 @@ require 'rubocop/cop/op_method'
|
|
58
65
|
require 'rubocop/cop/reduce_arguments'
|
59
66
|
require 'rubocop/cop/percent_r'
|
60
67
|
require 'rubocop/cop/favor_percent_r'
|
68
|
+
require 'rubocop/cop/class_methods'
|
69
|
+
require 'rubocop/cop/single_line_methods'
|
70
|
+
require 'rubocop/cop/word_array'
|
71
|
+
require 'rubocop/cop/symbol_array'
|
72
|
+
require 'rubocop/cop/trivial_accessors'
|
73
|
+
require 'rubocop/cop/leading_comment_space'
|
74
|
+
require 'rubocop/cop/colon_method_call'
|
75
|
+
require 'rubocop/cop/not'
|
76
|
+
require 'rubocop/cop/eval'
|
61
77
|
|
62
78
|
require 'rubocop/report/report'
|
63
79
|
require 'rubocop/report/plain_text'
|
64
80
|
require 'rubocop/report/emacs_style'
|
65
81
|
|
82
|
+
require 'rubocop/config'
|
83
|
+
require 'rubocop/config_store'
|
66
84
|
require 'rubocop/cli'
|
67
85
|
require 'rubocop/version'
|
data/lib/rubocop/cli.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
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
|
@@ -11,8 +10,19 @@ module Rubocop
|
|
11
10
|
# If set true while running,
|
12
11
|
# RuboCop will abort processing and exit gracefully.
|
13
12
|
attr_accessor :wants_to_quit
|
13
|
+
attr_accessor :options
|
14
|
+
|
14
15
|
alias_method :wants_to_quit?, :wants_to_quit
|
15
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
|
+
|
16
26
|
# Entry point for the application logic. Here we
|
17
27
|
# do the command line arguments processing and inspect
|
18
28
|
# the target files
|
@@ -20,73 +30,89 @@ module Rubocop
|
|
20
30
|
def run(args = ARGV)
|
21
31
|
trap_interrupt
|
22
32
|
|
23
|
-
$options = { mode: :default }
|
24
|
-
|
25
33
|
parse_options(args)
|
26
34
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
35
|
+
begin
|
36
|
+
handle_only_option if @options[:only]
|
37
|
+
rescue ArgumentError => e
|
38
|
+
puts e.message
|
39
|
+
return 1
|
40
|
+
end
|
33
41
|
|
34
42
|
target_files(args).each do |file|
|
35
43
|
break if wants_to_quit?
|
36
44
|
|
37
|
-
|
45
|
+
config = ConfigStore.for(file)
|
46
|
+
report = Report.create(file, @options[:mode])
|
47
|
+
source = read_source(file)
|
38
48
|
|
39
|
-
|
40
|
-
source = File.readlines(file).map do |line|
|
41
|
-
get_rid_of_invalid_byte_sequences(line)
|
42
|
-
line.chomp
|
43
|
-
end
|
49
|
+
puts "Scanning #{file}" if @options[:debug]
|
44
50
|
|
45
51
|
syntax_cop = Rubocop::Cop::Syntax.new
|
52
|
+
syntax_cop.debug = @options[:debug]
|
46
53
|
syntax_cop.inspect(file, source, nil, nil)
|
47
54
|
|
48
55
|
if syntax_cop.offences.map(&:severity).include?(:error)
|
49
56
|
# In case of a syntax error we just report that error and do
|
50
57
|
# no more checking in the file.
|
51
58
|
report << syntax_cop
|
52
|
-
total_offences += syntax_cop.offences.count
|
59
|
+
@total_offences += syntax_cop.offences.count
|
53
60
|
else
|
54
|
-
|
55
|
-
config = $options[:config] || config_from_dotfile(File.dirname(file))
|
56
|
-
disabled_lines = disabled_lines_in(source)
|
57
|
-
|
58
|
-
cops.each do |cop_klass|
|
59
|
-
cop_name = cop_klass.name.split('::').last
|
60
|
-
cop_config = config[cop_name] if config
|
61
|
-
if cop_config.nil? || cop_config['Enabled']
|
62
|
-
cop_klass.config = cop_config
|
63
|
-
cop = cop_klass.new
|
64
|
-
cop.correlations = correlations
|
65
|
-
cop.disabled_lines = disabled_lines[cop_name]
|
66
|
-
begin
|
67
|
-
cop.inspect(file, source, tokens, sexp)
|
68
|
-
rescue => e
|
69
|
-
errors_count += 1
|
70
|
-
warn "An error occurred while #{cop.name} cop" +
|
71
|
-
" was inspecting #{file}."
|
72
|
-
warn 'To see the complete backtrace run rubocop -d.'
|
73
|
-
puts e.backtrace if $options[:debug]
|
74
|
-
end
|
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
|
|
81
|
-
processed_file_count += 1
|
64
|
+
@processed_file_count += 1
|
82
65
|
report.display unless report.empty?
|
83
66
|
end
|
84
67
|
|
85
|
-
unless
|
86
|
-
display_summary(processed_file_count, total_offences,
|
68
|
+
unless @options[:silent]
|
69
|
+
display_summary(@processed_file_count, @total_offences, @errors)
|
87
70
|
end
|
88
71
|
|
89
|
-
(total_offences == 0) && !wants_to_quit ? 0 : 1
|
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
|
115
|
+
end
|
90
116
|
end
|
91
117
|
|
92
118
|
def parse_options(args)
|
@@ -94,16 +120,20 @@ module Rubocop
|
|
94
120
|
opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'
|
95
121
|
|
96
122
|
opts.on('-d', '--debug', 'Display debug info') do |d|
|
97
|
-
|
123
|
+
@options[:debug] = d
|
98
124
|
end
|
99
125
|
opts.on('-e', '--emacs', 'Emacs style output') do
|
100
|
-
|
126
|
+
@options[:mode] = :emacs_style
|
101
127
|
end
|
102
128
|
opts.on('-c FILE', '--config FILE', 'Configuration file') do |f|
|
103
|
-
|
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
|
104
134
|
end
|
105
135
|
opts.on('-s', '--silent', 'Silence summary') do |s|
|
106
|
-
|
136
|
+
@options[:silent] = s
|
107
137
|
end
|
108
138
|
opts.on('-n', '--no-color', 'Disable color output') do |s|
|
109
139
|
Sickill::Rainbow.enabled = false
|
@@ -119,13 +149,14 @@ module Rubocop
|
|
119
149
|
Signal.trap('INT') do
|
120
150
|
exit!(1) if wants_to_quit?
|
121
151
|
self.wants_to_quit = true
|
122
|
-
|
123
|
-
|
152
|
+
$stderr.puts
|
153
|
+
$stderr.puts 'Exiting... Interrupt again to exit immediately.'
|
124
154
|
end
|
125
155
|
end
|
126
156
|
|
127
|
-
def display_summary(num_files, total_offences,
|
128
|
-
|
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, "
|
129
160
|
offences_string = if total_offences.zero?
|
130
161
|
'no offences'
|
131
162
|
elsif total_offences == 1
|
@@ -136,9 +167,10 @@ module Rubocop
|
|
136
167
|
puts "#{offences_string} detected"
|
137
168
|
.color(total_offences.zero? ? :green : :red)
|
138
169
|
|
139
|
-
if
|
140
|
-
plural =
|
141
|
-
puts "\n#{
|
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 }
|
142
174
|
puts 'Errors are usually caused by RuboCop bugs.'
|
143
175
|
puts 'Please, report your problems to RuboCop\'s issue tracker.'
|
144
176
|
end
|
@@ -170,19 +202,18 @@ module Rubocop
|
|
170
202
|
match = line.match(regexp)
|
171
203
|
if match
|
172
204
|
kind, cops = match.captures
|
173
|
-
if cops.include?('all')
|
174
|
-
cops = Cop::Cop.all.map { |c| c.name.split('::').last }.join(',')
|
175
|
-
end
|
205
|
+
cops = Cop::Cop.all.map(&:cop_name).join(',') if cops.include?('all')
|
176
206
|
cops.split(/,\s*/).each { |cop_name| yield cop_name, kind }
|
177
207
|
end
|
178
208
|
end
|
179
209
|
|
180
|
-
def get_rid_of_invalid_byte_sequences(
|
181
|
-
|
210
|
+
def get_rid_of_invalid_byte_sequences(source)
|
211
|
+
source_encoding = source.encoding.name
|
182
212
|
# UTF-16 works better in this algorithm but is not supported in 1.9.2.
|
183
213
|
temporary_encoding = (RUBY_VERSION == '1.9.2') ? 'UTF-8' : 'UTF-16'
|
184
|
-
|
185
|
-
|
214
|
+
source.encode!(temporary_encoding, source_encoding,
|
215
|
+
invalid: :replace, replace: '')
|
216
|
+
source.encode!(source_encoding, temporary_encoding)
|
186
217
|
end
|
187
218
|
|
188
219
|
def self.rip_source(source)
|
@@ -193,50 +224,6 @@ module Rubocop
|
|
193
224
|
[tokens, sexp, correlations]
|
194
225
|
end
|
195
226
|
|
196
|
-
# Returns the configuration hash from .rubocop.yml searching
|
197
|
-
# upwards in the directory structure starting at the given
|
198
|
-
# directory where the inspected file is. If no .rubocop.yml is
|
199
|
-
# found there, the user's home directory is checked.
|
200
|
-
def config_from_dotfile(target_file_dir)
|
201
|
-
return unless target_file_dir
|
202
|
-
# @configs is a cache that maps directories to
|
203
|
-
# configurations. We search for .rubocop.yml only if we haven't
|
204
|
-
# already found it for the given directory.
|
205
|
-
unless @configs[target_file_dir]
|
206
|
-
dir = target_file_dir
|
207
|
-
while dir != '/'
|
208
|
-
path = File.join(dir, '.rubocop.yml')
|
209
|
-
if File.exist?(path)
|
210
|
-
@configs[target_file_dir] = YAML.load_file(path)
|
211
|
-
return @configs[target_file_dir]
|
212
|
-
end
|
213
|
-
dir = File.expand_path('..', dir)
|
214
|
-
end
|
215
|
-
path = File.join(Dir.home, '.rubocop.yml')
|
216
|
-
@configs[target_file_dir] = YAML.load_file(path) if File.exist?(path)
|
217
|
-
end
|
218
|
-
@configs[target_file_dir]
|
219
|
-
end
|
220
|
-
|
221
|
-
def cops_on_duty(config)
|
222
|
-
cops_on_duty = []
|
223
|
-
|
224
|
-
Cop::Cop.all.each do |cop_klass|
|
225
|
-
cop_config = config[cop_klass.name.split('::').last] if config
|
226
|
-
cops_on_duty << cop_klass if cop_config.nil? || cop_config['Enabled']
|
227
|
-
end
|
228
|
-
|
229
|
-
cops_on_duty
|
230
|
-
end
|
231
|
-
|
232
|
-
def show_cops_on_duty(cops)
|
233
|
-
puts '== Reporting for duty =='
|
234
|
-
cops.each do |c|
|
235
|
-
puts ' * '.color(:yellow) + c.to_s.color(:green)
|
236
|
-
end
|
237
|
-
puts '========================'
|
238
|
-
end
|
239
|
-
|
240
227
|
# Generate a list of target files by expanding globing patterns
|
241
228
|
# (if any). If args is empty recursively finds all Ruby source
|
242
229
|
# files under the current directory
|
@@ -248,21 +235,23 @@ module Rubocop
|
|
248
235
|
|
249
236
|
args.each do |target|
|
250
237
|
if File.directory?(target)
|
251
|
-
files
|
238
|
+
files += ruby_files(target.chomp(File::SEPARATOR))
|
252
239
|
elsif target =~ /\*/
|
253
|
-
files
|
240
|
+
files += Dir[target]
|
254
241
|
else
|
255
242
|
files << target
|
256
243
|
end
|
257
244
|
end
|
258
245
|
|
259
|
-
files.
|
246
|
+
files.uniq
|
260
247
|
end
|
261
248
|
|
262
249
|
# Finds all Ruby source files under the current or other supplied
|
263
250
|
# directory. A Ruby source file is defined as a file with the `.rb`
|
264
251
|
# extension or a file with no extension that has a ruby shebang line
|
265
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.
|
266
255
|
# @param root Root directory under which to search for ruby source files
|
267
256
|
# @return [Array] Array of filenames
|
268
257
|
def ruby_files(root = Dir.pwd)
|
@@ -270,8 +259,8 @@ module Rubocop
|
|
270
259
|
|
271
260
|
rb = []
|
272
261
|
|
273
|
-
rb
|
274
|
-
rb
|
262
|
+
rb += files.select { |file| File.extname(file) == '.rb' }
|
263
|
+
rb += files.select do |file|
|
275
264
|
File.extname(file) == '' &&
|
276
265
|
begin
|
277
266
|
File.open(file) { |f| f.readline } =~ /#!.*ruby/
|
@@ -281,13 +270,21 @@ module Rubocop
|
|
281
270
|
end
|
282
271
|
end
|
283
272
|
|
284
|
-
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
|
285
282
|
end
|
286
283
|
|
287
284
|
private
|
288
285
|
|
289
|
-
def log_error(e, msg='')
|
290
|
-
if
|
286
|
+
def log_error(e, msg = '')
|
287
|
+
if @options[:debug]
|
291
288
|
error_message = "#{e.class}, #{e.message}"
|
292
289
|
STDERR.puts "#{msg}\t#{error_message}"
|
293
290
|
end
|