rubocop 0.14.1 → 0.15.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.

Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +5 -3
  3. data/CHANGELOG.md +245 -198
  4. data/README.md +7 -0
  5. data/Rakefile +5 -1
  6. data/config/default.yml +27 -4
  7. data/config/enabled.yml +18 -4
  8. data/lib/rubocop.rb +13 -1
  9. data/lib/rubocop/cli.rb +83 -23
  10. data/lib/rubocop/config.rb +1 -1
  11. data/lib/rubocop/cop/cop.rb +31 -6
  12. data/lib/rubocop/cop/lint/block_alignment.rb +11 -8
  13. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +21 -14
  14. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  15. data/lib/rubocop/cop/rails/output.rb +35 -0
  16. data/lib/rubocop/cop/style/{access_control.rb → access_modifier_indentation.rb} +18 -15
  17. data/lib/rubocop/cop/style/alias.rb +14 -2
  18. data/lib/rubocop/cop/style/align_hash.rb +174 -109
  19. data/lib/rubocop/cop/style/autocorrect_alignment.rb +38 -18
  20. data/lib/rubocop/cop/style/blocks.rb +4 -6
  21. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +3 -3
  22. data/lib/rubocop/cop/style/cyclomatic_complexity.rb +46 -0
  23. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +48 -0
  24. data/lib/rubocop/cop/style/empty_lines_around_body.rb +62 -0
  25. data/lib/rubocop/cop/style/end_of_line.rb +6 -2
  26. data/lib/rubocop/cop/style/favor_modifier.rb +11 -1
  27. data/lib/rubocop/cop/style/final_newline.rb +10 -4
  28. data/lib/rubocop/cop/style/hash_syntax.rb +32 -21
  29. data/lib/rubocop/cop/style/leading_comment_space.rb +9 -0
  30. data/lib/rubocop/cop/style/method_call_parentheses.rb +11 -1
  31. data/lib/rubocop/cop/style/numeric_literals.rb +11 -15
  32. data/lib/rubocop/cop/style/redundant_return.rb +7 -4
  33. data/lib/rubocop/cop/style/redundant_self.rb +3 -3
  34. data/lib/rubocop/cop/style/signal_exception.rb +4 -2
  35. data/lib/rubocop/cop/style/space_after_comma_etc.rb +7 -1
  36. data/lib/rubocop/cop/style/space_after_control_keyword.rb +6 -0
  37. data/lib/rubocop/cop/style/space_after_method_name.rb +7 -1
  38. data/lib/rubocop/cop/style/space_after_not.rb +6 -2
  39. data/lib/rubocop/cop/style/space_around_block_braces.rb +149 -0
  40. data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +33 -0
  41. data/lib/rubocop/cop/style/space_around_operators.rb +169 -0
  42. data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +6 -0
  43. data/lib/rubocop/cop/style/space_inside.rb +35 -0
  44. data/lib/rubocop/cop/style/space_inside_brackets.rb +18 -0
  45. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +99 -0
  46. data/lib/rubocop/cop/style/space_inside_parens.rb +18 -0
  47. data/lib/rubocop/cop/style/special_global_vars.rb +52 -25
  48. data/lib/rubocop/cop/style/string_literals.rb +1 -1
  49. data/lib/rubocop/cop/style/surrounding_space.rb +1 -344
  50. data/lib/rubocop/cop/style/trailing_blank_lines.rb +17 -5
  51. data/lib/rubocop/cop/style/trailing_whitespace.rb +9 -5
  52. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -2
  53. data/lib/rubocop/cop/style/word_array.rb +16 -1
  54. data/lib/rubocop/cop/team.rb +5 -5
  55. data/lib/rubocop/cop/util.rb +1 -0
  56. data/lib/rubocop/formatter/offence_count_formatter.rb +0 -1
  57. data/lib/rubocop/options.rb +76 -111
  58. data/lib/rubocop/rake_task.rb +4 -2
  59. data/lib/rubocop/target_finder.rb +3 -3
  60. data/lib/rubocop/version.rb +1 -1
  61. data/spec/rubocop/cli_spec.rb +123 -13
  62. data/spec/rubocop/config_spec.rb +2 -2
  63. data/spec/rubocop/cop/lint/rescue_exception_spec.rb +10 -0
  64. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +13 -0
  65. data/spec/rubocop/cop/offence_spec.rb +2 -0
  66. data/spec/rubocop/cop/rails/output_spec.rb +40 -0
  67. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +243 -0
  68. data/spec/rubocop/cop/style/alias_spec.rb +8 -0
  69. data/spec/rubocop/cop/style/align_array_spec.rb +12 -0
  70. data/spec/rubocop/cop/style/align_hash_spec.rb +15 -0
  71. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +7 -4
  72. data/spec/rubocop/cop/style/cyclomatic_complexity_spec.rb +203 -0
  73. data/spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb +56 -0
  74. data/spec/rubocop/cop/style/empty_lines_around_body_spec.rb +87 -0
  75. data/spec/rubocop/cop/style/end_of_line_spec.rb +17 -8
  76. data/spec/rubocop/cop/style/favor_modifier_spec.rb +34 -0
  77. data/spec/rubocop/cop/style/final_newline_spec.rb +5 -0
  78. data/spec/rubocop/cop/style/hash_syntax_spec.rb +22 -2
  79. data/spec/rubocop/cop/style/leading_comment_space_spec.rb +5 -0
  80. data/spec/rubocop/cop/style/method_call_parentheses_spec.rb +39 -4
  81. data/spec/rubocop/cop/style/numeric_literals_spec.rb +5 -0
  82. data/spec/rubocop/cop/style/signal_exception_spec.rb +11 -0
  83. data/spec/rubocop/cop/style/space_after_colon_spec.rb +7 -0
  84. data/spec/rubocop/cop/style/space_after_comma_spec.rb +5 -0
  85. data/spec/rubocop/cop/style/space_after_control_keyword_spec.rb +29 -8
  86. data/spec/rubocop/cop/style/space_after_method_name_spec.rb +15 -0
  87. data/spec/rubocop/cop/style/space_after_semicolon_spec.rb +5 -0
  88. data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +68 -0
  89. data/spec/rubocop/cop/style/space_around_equals_in_default_parameter_spec.rb +5 -0
  90. data/spec/rubocop/cop/style/space_around_operators_spec.rb +43 -0
  91. data/spec/rubocop/cop/style/space_before_modifier_keyword_spec.rb +23 -0
  92. data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +7 -0
  93. data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +65 -23
  94. data/spec/rubocop/cop/style/space_inside_parens_spec.rb +7 -0
  95. data/spec/rubocop/cop/style/special_global_vars_spec.rb +12 -2
  96. data/spec/rubocop/cop/style/string_literals_spec.rb +6 -0
  97. data/spec/rubocop/cop/style/symbol_array_spec.rb +5 -7
  98. data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +26 -1
  99. data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +7 -0
  100. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +8 -0
  101. data/spec/rubocop/cop/style/word_array_spec.rb +33 -2
  102. data/spec/rubocop/cop/team_spec.rb +4 -4
  103. data/spec/rubocop/formatter/json_formatter_spec.rb +1 -1
  104. data/spec/rubocop/options_spec.rb +5 -96
  105. data/spec/rubocop/processed_source_spec.rb +3 -3
  106. data/spec/spec_helper.rb +28 -23
  107. data/spec/support/mri_syntax_checker.rb +20 -16
  108. metadata +24 -5
  109. data/spec/rubocop/cop/style/access_control_spec.rb +0 -164
@@ -5,33 +5,31 @@ require 'spec_helper'
5
5
  describe Rubocop::Cop::Style::SymbolArray do
6
6
  subject(:cop) { described_class.new }
7
7
 
8
- it 'registers an offence for arrays of symbols', { ruby: 2.0 } do
8
+ it 'registers an offence for arrays of symbols', ruby: 2.0 do
9
9
  inspect_source(cop,
10
10
  ['[:one, :two, :three]'])
11
11
  expect(cop.offences.size).to eq(1)
12
12
  end
13
13
 
14
- it 'does not reg an offence for array with non-syms', { ruby: 2.0 } do
14
+ it 'does not reg an offence for array with non-syms', ruby: 2.0 do
15
15
  inspect_source(cop,
16
16
  ['[:one, :two, "three"]'])
17
17
  expect(cop.offences).to be_empty
18
18
  end
19
19
 
20
- it 'does not reg an offence for array starting with %i',
21
- { ruby: 2.0 } do
20
+ it 'does not reg an offence for array starting with %i', ruby: 2.0 do
22
21
  inspect_source(cop,
23
22
  ['%i(one two three)'])
24
23
  expect(cop.offences).to be_empty
25
24
  end
26
25
 
27
- it 'does not reg an offence for array with one element',
28
- { ruby: 2.0 } do
26
+ it 'does not reg an offence for array with one element', ruby: 2.0 do
29
27
  inspect_source(cop,
30
28
  ['[:three]'])
31
29
  expect(cop.offences).to be_empty
32
30
  end
33
31
 
34
- it 'does nothing on Ruby 1.9', { ruby: 1.9 } do
32
+ it 'does nothing on Ruby 1.9', ruby: 1.9 do
35
33
  inspect_source(cop,
36
34
  ['[:one, :two, :three]'])
37
35
  expect(cop.offences).to be_empty
@@ -3,7 +3,10 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Rubocop::Cop::Style::TrailingBlankLines do
6
- subject(:cop) { described_class.new }
6
+ subject(:cop) { described_class.new(config) }
7
+ let(:config) do
8
+ Rubocop::Config.new('TrailingWhitespace' => { 'Enabled' => true })
9
+ end
7
10
 
8
11
  it 'accepts final newline' do
9
12
  inspect_source(cop, ['x = 0', ''])
@@ -15,4 +18,26 @@ describe Rubocop::Cop::Style::TrailingBlankLines do
15
18
  expect(cop.offences.size).to eq(1)
16
19
  expect(cop.messages).to eq(['3 trailing blank lines detected.'])
17
20
  end
21
+
22
+ it 'auto-corrects unwanted blank lines' do
23
+ new_source = autocorrect_source(cop, ['x = 0', '', '', '', ''])
24
+ expect(new_source).to eq(['x = 0', ''].join("\n"))
25
+ end
26
+
27
+ it 'does not auto-correct if it interferes with TrailingWhitespace' do
28
+ original = ['x = 0', '', ' ', '', '']
29
+ new_source = autocorrect_source(cop, original)
30
+ expect(new_source).to eq(original.join("\n"))
31
+ end
32
+
33
+ context 'with TrailingWhitespace disabled' do
34
+ let(:config) do
35
+ Rubocop::Config.new('TrailingWhitespace' => { 'Enabled' => false })
36
+ end
37
+
38
+ it 'auto-corrects even if some lines have space' do
39
+ new_source = autocorrect_source(cop, ['x = 0', '', ' ', '', ''])
40
+ expect(new_source).to eq(['x = 0', ''].join("\n"))
41
+ end
42
+ end
18
43
  end
@@ -20,4 +20,11 @@ describe Rubocop::Cop::Style::TrailingWhitespace do
20
20
  inspect_source(cop, ["x = 0\n"])
21
21
  expect(cop.offences).to be_empty
22
22
  end
23
+
24
+ it 'auto-corrects unwanted space' do
25
+ new_source = autocorrect_source(cop, ['x = 0 ',
26
+ "x = 0\t"])
27
+ expect(new_source).to eq(['x = 0',
28
+ 'x = 0'].join("\n"))
29
+ end
23
30
  end
@@ -201,6 +201,14 @@ describe Rubocop::Cop::Style::TrivialAccessors, :config do
201
201
  expect(cop.offences).to be_empty
202
202
  end
203
203
 
204
+ it 'treats splats as non-trivial' do
205
+ inspect_source(cop,
206
+ [' def splatomatic(*values)',
207
+ ' @splatomatic = values',
208
+ ' end'])
209
+ expect(cop.offences).to be_empty
210
+ end
211
+
204
212
  it 'finds oneliner trivials' do
205
213
  inspect_source(cop,
206
214
  ['class Oneliner',
@@ -2,8 +2,9 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Rubocop::Cop::Style::WordArray do
6
- subject(:cop) { described_class.new }
5
+ describe Rubocop::Cop::Style::WordArray, :config do
6
+ subject(:cop) { described_class.new(config) }
7
+ let(:cop_config) { { 'MinSize' => 0 } }
7
8
 
8
9
  it 'registers an offence for arrays of single quoted strings' do
9
10
  inspect_source(cop,
@@ -52,4 +53,34 @@ describe Rubocop::Cop::Style::WordArray do
52
53
  ['["", "two", "three"]'])
53
54
  expect(cop.offences).to be_empty
54
55
  end
56
+
57
+ it 'does not register an offence for array with allowed number of strings' do
58
+ cop_config['MinSize'] = 3
59
+
60
+ inspect_source(cop,
61
+ ['["one", "two", "three"]'])
62
+ expect(cop.offences).to be_empty
63
+ end
64
+
65
+ it 'does not register an offence for an array with comments in it' do
66
+ inspect_source(cop,
67
+ ['[',
68
+ '"foo", # comment here',
69
+ '"bar", # this thing was done because of a bug',
70
+ '"baz" # do not delete this line',
71
+ ']'])
72
+
73
+ expect(cop.offences).to be_empty
74
+ end
75
+
76
+ it 'registers an offence for an array with comments outside of it' do
77
+ inspect_source(cop,
78
+ ['[',
79
+ '"foo",',
80
+ '"bar",',
81
+ '"baz"',
82
+ '] # test'])
83
+
84
+ expect(cop.offences.size).to eq(1)
85
+ end
55
86
  end
@@ -4,7 +4,7 @@ require 'spec_helper'
4
4
 
5
5
  describe Rubocop::Cop::Team do
6
6
  subject(:team) { described_class.new(cop_classes, config, options) }
7
- let(:cop_classes) { Rubocop::Cop::Cop.all }
7
+ let(:cop_classes) { Rubocop::Cop::Cop.non_rails }
8
8
  let(:config) { Rubocop::ConfigLoader.default_configuration }
9
9
  let(:options) { nil }
10
10
 
@@ -16,8 +16,8 @@ describe Rubocop::Cop::Team do
16
16
  it { should be_false }
17
17
  end
18
18
 
19
- context 'when { autocorrect: true } is passed to .new' do
20
- let(:options) { { autocorrect: true } }
19
+ context 'when { auto_correct: true } is passed to .new' do
20
+ let(:options) { { auto_correct: true } }
21
21
  it { should be_true }
22
22
  end
23
23
  end
@@ -91,7 +91,7 @@ describe Rubocop::Cop::Team do
91
91
  end
92
92
 
93
93
  context 'when autocorrection is enabled' do
94
- let(:options) { { autocorrect: true } }
94
+ let(:options) { { auto_correct: true } }
95
95
 
96
96
  before do
97
97
  create_file(file_path, [
@@ -76,7 +76,7 @@ module Rubocop
76
76
  it 'outputs #output_hash as JSON' do
77
77
  formatter.finished(files)
78
78
  json = output.string
79
- restored_hash = JSON.parse(json, { symbolize_names: true })
79
+ restored_hash = JSON.parse(json, symbolize_names: true)
80
80
  expect(restored_hash).to eq(formatter.output_hash)
81
81
  end
82
82
  end
@@ -5,8 +5,7 @@ require 'spec_helper'
5
5
  describe Rubocop::Options, :isolated_environment do
6
6
  include FileHelper
7
7
 
8
- subject(:options) { described_class.new(config_store) }
9
- let(:config_store) { Rubocop::ConfigStore.new }
8
+ subject(:options) { described_class.new }
10
9
 
11
10
  before(:each) do
12
11
  $stdout = StringIO.new
@@ -37,13 +36,10 @@ describe Rubocop::Options, :isolated_environment do
37
36
 
38
37
  expected_help = <<-END
39
38
  Usage: rubocop [options] [file1, file2, ...]
40
- -d, --debug Display debug info.
41
- -c, --config FILE Specify configuration file.
42
39
  --only COP Run just one cop.
40
+ -c, --config FILE Specify configuration file.
43
41
  --auto-gen-config Generate a configuration file acting as a
44
42
  TODO list.
45
- --show-cops Shows cops and their config for the
46
- current directory.
47
43
  -f, --format FORMATTER Choose an output formatter. This option
48
44
  can be specified multiple times to enable
49
45
  multiple formatters at the same time.
@@ -60,6 +56,9 @@ Usage: rubocop [options] [file1, file2, ...]
60
56
  specified --format, or the default format
61
57
  if no format is specified.
62
58
  -r, --require FILE Require Ruby file.
59
+ --show-cops Shows cops and their config for the
60
+ current directory.
61
+ -d, --debug Display debug info.
63
62
  -R, --rails Run extra Rails cops.
64
63
  -l, --lint Run only lint cops.
65
64
  -a, --auto-correct Auto-correct offences.
@@ -97,14 +96,6 @@ Usage: rubocop [options] [file1, file2, ...]
97
96
  end
98
97
  end
99
98
 
100
- describe '--version' do
101
- it 'exits cleanly' do
102
- expect { options.parse ['-v'] }.to exit_with_code(0)
103
- expect { options.parse ['--version'] }.to exit_with_code(0)
104
- expect($stdout.string).to eq((Rubocop::Version::STRING + "\n") * 2)
105
- end
106
- end
107
-
108
99
  describe '--only' do
109
100
  it 'exits with error if an incorrect cop name is passed' do
110
101
  expect { options.parse(%w(--only 123)) }
@@ -112,88 +103,6 @@ Usage: rubocop [options] [file1, file2, ...]
112
103
  end
113
104
  end
114
105
 
115
- describe '--show-cops' do
116
- let(:cops) { Rubocop::Cop::Cop.all }
117
-
118
- let(:global_conf) do
119
- config_path =
120
- Rubocop::ConfigLoader.configuration_file_for(Dir.pwd.to_s)
121
- Rubocop::ConfigLoader.configuration_from_file(config_path)
122
- end
123
-
124
- let(:stdout) { $stdout.string }
125
-
126
- before do
127
- expect { options.parse ['--show-cops'] }.to exit_with_code(0)
128
- end
129
-
130
- # Extracts the first line out of the description
131
- def short_description_of_cop(cop)
132
- desc = full_description_of_cop(cop)
133
- desc ? desc.lines.first.strip : ''
134
- end
135
-
136
- # Gets the full description of the cop or nil if no description is set.
137
- def full_description_of_cop(cop)
138
- cop_config = global_conf.for_cop(cop)
139
- cop_config['Description']
140
- end
141
-
142
- it 'prints all available cops and their description' do
143
- cops.each do |cop|
144
- expect(stdout).to include cop.cop_name
145
- expect(stdout).to include short_description_of_cop(cop)
146
- end
147
- end
148
-
149
- it 'prints all types' do
150
- cops
151
- .types
152
- .map(&:to_s)
153
- .map(&:capitalize)
154
- .each { |type| expect(stdout).to include(type) }
155
- end
156
-
157
- it 'prints all cops in their right type listing' do
158
- lines = stdout.lines
159
- lines.slice_before(/Type /).each do |slice|
160
- types = cops.types.map(&:to_s).map(&:capitalize)
161
- current = types.delete(slice.shift[/Type '(?<c>[^'']+)'/, 'c'])
162
- # all cops in their type listing
163
- cops.with_type(current).each do |cop|
164
- expect(slice.any? { |l| l.include? cop.cop_name }).to be_true
165
- end
166
-
167
- # no cop in wrong type listing
168
- types.each do |type|
169
- cops.with_type(type).each do |cop|
170
- expect(slice.any? { |l| l.include? cop.cop_name }).to be_false
171
- end
172
- end
173
- end
174
- end
175
-
176
- it 'prints the current configuration' do
177
- out = stdout.lines.to_a
178
- cops.each do |cop|
179
- conf = global_conf[cop.cop_name].dup
180
- confstrt =
181
- out.find_index { |i| i.include?("- #{cop.cop_name}") } + 1
182
- c = out[confstrt, conf.keys.size].to_s
183
- conf.delete('Description')
184
- expect(c).to include(short_description_of_cop(cop))
185
- conf.each do |k, v|
186
- # ugly hack to get hash/array content tested
187
- if v.kind_of?(Hash) || v.kind_of?(Array)
188
- expect(c).to include "#{k}: #{v.to_s.dump[2, -2]}"
189
- else
190
- expect(c).to include "#{k}: #{v}"
191
- end
192
- end
193
- end
194
- end
195
- end
196
-
197
106
  describe '--require' do
198
107
  let(:required_file_path) { './path/to/required_file.rb' }
199
108
 
@@ -78,7 +78,7 @@ describe Rubocop::ProcessedSource do
78
78
  end
79
79
  end
80
80
 
81
- context 'when the source has diagnostic with error level' do
81
+ context 'when the source has diagnostic with fatal level' do
82
82
  let(:level) { :fatal }
83
83
 
84
84
  it 'returns false' do
@@ -86,10 +86,10 @@ describe Rubocop::ProcessedSource do
86
86
  end
87
87
  end
88
88
 
89
- context 'when the source has diagnostic with error level' do
89
+ context 'when the source has diagnostic with warning level' do
90
90
  let(:level) { :warning }
91
91
 
92
- it 'returns false' do
92
+ it 'returns true' do
93
93
  expect(processed_source.valid_syntax?).to be_true
94
94
  end
95
95
  end
@@ -12,24 +12,19 @@ if ENV['TRAVIS'] && RUBY_ENGINE == 'jruby'
12
12
  ENV['TMPDIR'] = non_world_writable_tmp_dir
13
13
  end
14
14
 
15
- # Temporary disable the coverage report until Coveralls fix
16
- # their API or we find a way to not break the TRAVIS build
17
- # when Coveralls report error.
18
- # SimpleCov raises similar IOError - stream closed when ran
19
- # on Rubinius.
20
- # if ENV['TRAVIS'] || ENV['COVERAGE']
21
- # require 'simplecov'
22
-
23
- # if ENV['TRAVIS']
24
- # require 'coveralls'
25
- # SimpleCov.formatter = Coveralls::SimpleCov::Formatter
26
- # end
27
-
28
- # SimpleCov.start do
29
- # add_filter '/spec/'
30
- # add_filter '/vendor/bundle/'
31
- # end
32
- # end
15
+ if ENV['TRAVIS'] || ENV['COVERAGE']
16
+ require 'simplecov'
17
+
18
+ if ENV['TRAVIS']
19
+ require 'coveralls'
20
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
21
+ end
22
+
23
+ SimpleCov.start do
24
+ add_filter '/spec/'
25
+ add_filter '/vendor/bundle/'
26
+ end
27
+ end
33
28
 
34
29
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
35
30
  $LOAD_PATH.unshift(File.dirname(__FILE__))
@@ -79,19 +74,29 @@ RSpec.configure do |config|
79
74
  config.include(ExitCodeMatchers)
80
75
  end
81
76
 
82
- def inspect_source(cop, source)
83
- processed_source = parse_source(source)
77
+ def inspect_source_file(cop, source)
78
+ Tempfile.open('tmp') { |f| inspect_source(cop, source, f) }
79
+ end
80
+
81
+ def inspect_source(cop, source, file = nil)
82
+ processed_source = parse_source(source, file)
84
83
  fail 'Error parsing example code' unless processed_source.valid_syntax?
85
84
  _investigate(cop, processed_source)
86
85
  end
87
86
 
88
- def parse_source(source)
87
+ def parse_source(source, file = nil)
89
88
  source = source.join($RS) if source.is_a?(Array)
90
- Rubocop::SourceParser.parse(source)
89
+ if file
90
+ file.write(source)
91
+ file.rewind
92
+ Rubocop::SourceParser.parse(source, file.path)
93
+ else
94
+ Rubocop::SourceParser.parse(source)
95
+ end
91
96
  end
92
97
 
93
98
  def autocorrect_source(cop, source)
94
- cop.instance_variable_get(:@options)[:autocorrect] = true
99
+ cop.instance_variable_get(:@options)[:auto_correct] = true
95
100
  processed_source = parse_source(source)
96
101
  _investigate(cop, processed_source)
97
102
 
@@ -17,24 +17,28 @@ module MRISyntaxChecker
17
17
  source_buffer = Parser::Source::Buffer.new('test', 1)
18
18
  source_buffer.source = source
19
19
 
20
- offences = []
21
-
22
- check_syntax(source).each_line do |line|
23
- line_number, severity, message = process_line(line)
24
- next unless line_number
25
- next if grep_message && !message.include?(grep_message)
26
- begin_pos = source_lines[0...(line_number - 1)].reduce(0) do |a, e|
27
- a + e.length + "\n".length
28
- end
29
- offences << Rubocop::Cop::Offence.new(
30
- severity,
31
- Parser::Source::Range.new(source_buffer, begin_pos, begin_pos + 1),
32
- message.capitalize,
33
- fake_cop_name
34
- )
20
+ offences = check_syntax(source).each_line.map do |line|
21
+ check_line(line, source_lines, source_buffer, fake_cop_name,
22
+ grep_message)
35
23
  end
36
24
 
37
- offences
25
+ offences.compact
26
+ end
27
+
28
+ def check_line(line, source_lines, source_buffer, fake_cop_name,
29
+ grep_message)
30
+ line_number, severity, message = process_line(line)
31
+ return unless line_number
32
+ return if grep_message && !message.include?(grep_message)
33
+ begin_pos = source_lines[0...(line_number - 1)].reduce(0) do |a, e|
34
+ a + e.length + "\n".length
35
+ end
36
+ Rubocop::Cop::Offence.new(severity,
37
+ Parser::Source::Range.new(source_buffer,
38
+ begin_pos,
39
+ begin_pos + 1),
40
+ message.capitalize,
41
+ fake_cop_name)
38
42
  end
39
43
 
40
44
  def check_syntax(source)