New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cop_helpers should be deprecated #8003
Comments
No objections from me. Those are all remnants from the early days of RuboCop and we certainly have better APIs at our disposal today. |
I am actually working on adding (optional) looping to the |
Sounds great! |
I originally posted this info in #8127. I think it makes more sense here since it is a more technical discussion. I haven't found a way to use it 'registers an offense for class method body starting with a blank' do
expect_offense(<<~RUBY)
def Test.some_method
^ Extra empty line detected at method body beginning.
do_something
end
RUBY
end Failures:
1) RuboCop::Cop::Layout::EmptyLinesAroundMethodBody registers an offense for class method body starting with a blank
Failure/Error: expect(actual_annotations.to_s).to eq(expected_annotations.to_s)
expected: "def Test.some_method\n\n^ Extra empty line detected at method body beginning.\n do_something\nend\n"
got: "def Test.some_method\n\n Extra empty line detected at method body beginning.\n do_something\nend\n"
(compared using ==)
Diff:
@@ -1,6 +1,6 @@
def Test.some_method
-^ Extra empty line detected at method body beginning.
+ Extra empty line detected at method body beginning.
do_something
end
# ./lib/rubocop/rspec/expect_offense.rb:125:in `expect_offense'
# ./spec/rubocop/cop/layout/empty_lines_around_method_body_spec.rb:43:in `block (2 levels) in <top (required)>'
Finished in 0.04196 seconds (files took 1.03 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/rubocop/cop/layout/empty_lines_around_method_body_spec.rb:42 # RuboCop::Cop::Layout::EmptyLinesAroundMethodBody registers an offense for class method body starting with a blank I think there might be a couple of other oddities to work through in order to convert everything. It's been a while since I worked on converting tests. It might be worth while to document some of the tricks to handling white space and interpolation with different types of HEREDOCS. For instance, <<~RUBY
puts 'foo' +
"#{bar}"
puts 'a' +
'b'
"#{c}"
RUBY
# vs
# The markdown syntax highlighting is incorrect here.
# This does not actually perform string interpolation
<<~'RUBY'
puts 'foo' +
"#{bar}"
puts 'a' +
'b'
"#{c}"
RUBY |
A bit cryptic, but we could use |
Part of rubocop#8003 Use `^{}` to denote an offense at an empty line in `expect_offense`. Example: ```ruby expect_offense(<<~RUBY) ^{} Missing frozen string literal comment. puts 1 RUBY ``` Enable pending frozen string literal specs Correct which line the offense is added to (always the first line). As the first line is non-empty in these cases, use a normal caret `^` to mark the offense.
I like |
Part of #8003 Use `^{}` to denote an offense at an empty line in `expect_offense`. Example: ```ruby expect_offense(<<~RUBY) ^{} Missing frozen string literal comment. puts 1 RUBY ``` Enable pending frozen string literal specs Correct which line the offense is added to (always the first line). As the first line is non-empty in these cases, use a normal caret `^` to mark the offense.
As a followup to #7970, I feel that all of the methods in
cop_helper
should be deprecated. Here is why:inspect_source
andautocorrect_source
seem too low level and don't lead to the right patterns. Among other things, they don't uselet
s for anything. So some things get duplicated (e.g. building theprocessed_source
). No instance is shared by default, so many specs are callinginspect_source
and thenautocorrect_source
, which is a really bad pattern: the samecop
is called twice, with differentsource_buffer
(although the same source code), with settings that has been hacked (viainstance_variable_get
😱), something that won't even work currently if thecop
is constructed withauto_correct
set totrue
.Example: this spec checks 78 cases (59 with offenses, 19 without). The
inspect_source
is called 118 times for nothing. Each case with offense also callsautocorrect_source
twice.I feel these (at least) should be officially deprecated (at least in the doc) in
1.0
, in favor ofexpect_offense
/expect_no_offense
or better.inspect_source_file
is used in one spec, I don't know if there's a good reason for it at all.Maybe
parse_source
could remain as is, although it is still very low level and I'd still favorprocessed_source
(in shared context).autocorrect_source_with_loop
=> I checked the one cop spec that uses it. Unless I'm mistaken, this isn't really about the cop but simply that some autocorrected cases may lead to another autocorrectable offense. Would be best handled with twoexpect_offense
with correction or similar.The text was updated successfully, but these errors were encountered: