Skip to content

Commit

Permalink
Merge pull request #904 from tejasbubane/fix-771
Browse files Browse the repository at this point in the history
Fix multiple cops to detect `let` with proc argument
  • Loading branch information
pirj committed Apr 29, 2020
2 parents b5c18e1 + 7e046a0 commit 52a7fe1
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@
* Add new `Capybara/VisibilityMatcher` cop. ([@aried3r][])
* Ignore String constants by `RSpec/Describe`. ([@AlexWayfer][])
* Drop support for ruby 2.3. ([@bquorning][])
* Fix multiple cops to detect `let` with proc argument. ([@tejasbubane][])

## 1.38.1 (2020-02-15)

Expand Down Expand Up @@ -497,3 +498,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@eitoball]: https://github.com/eitoball
[@aried3r]: https://github.com/aried3r
[@AlexWayfer]: https://github.com/AlexWayfer
[@tejasbubane]: https://github.com/tejasbubane
8 changes: 8 additions & 0 deletions lib/rubocop/rspec/language.rb
Expand Up @@ -28,6 +28,14 @@ def block_pattern
"(block #{send_pattern} ...)"
end

def block_pass_pattern
"(send #{RSPEC} #{node_pattern_union} _ block_pass)"
end

def block_or_block_pass_pattern
"{#{block_pattern} #{block_pass_pattern}}"
end

def send_pattern
"(send #{RSPEC} #{node_pattern_union} ...)"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/rspec/language/node_pattern.rb
Expand Up @@ -17,7 +17,7 @@ module NodePattern

def_node_matcher :hook?, Hooks::ALL.block_pattern

def_node_matcher :let?, Helpers::ALL.block_pattern
def_node_matcher :let?, Helpers::ALL.block_or_block_pass_pattern

def_node_matcher :subject?, Subject::ALL.block_pattern
end
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rspec/align_left_let_brace_spec.rb
Expand Up @@ -45,6 +45,12 @@
end
# rubocop:enable RSpec/ExampleLength

it 'does not register offense for let with proc argument' do
expect_no_offenses(<<-RUBY)
let(:user, &args[:build_user])
RUBY
end

it 'works with empty file' do
expect_no_offenses('')
end
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rspec/align_right_let_brace_spec.rb
Expand Up @@ -45,6 +45,12 @@
end
# rubocop:enable RSpec/ExampleLength

it 'does not register offense for let with proc argument' do
expect_no_offenses(<<-RUBY)
let(:user, &args[:build_user])
RUBY
end

it 'works with empty file' do
expect_no_offenses('')
end
Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cop/rspec/empty_line_after_final_let_spec.rb
Expand Up @@ -47,6 +47,26 @@
RUBY
end

it 'checks for empty line after let with proc argument' do
expect_offense(<<-RUBY)
RSpec.describe User do
let(:a) { a }
let(:user, &args[:build_user])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after the last `let` block.
it { expect(a).to eq(b) }
end
RUBY

expect_correction(<<-RUBY)
RSpec.describe User do
let(:a) { a }
let(:user, &args[:build_user])
it { expect(a).to eq(b) }
end
RUBY
end

it 'approves empty line after let' do
expect_no_offenses(<<-RUBY)
RSpec.describe User do
Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cop/rspec/leading_subject_spec.rb
Expand Up @@ -41,6 +41,25 @@
RUBY
end

it 'checks subject below let with proc argument' do
expect_offense(<<-RUBY)
RSpec.describe User do
let(:user, &args[:build_user])
subject { described_class.new }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Declare `subject` above any other `let` declarations.
end
RUBY

expect_correction(<<-RUBY)
RSpec.describe User do
subject { described_class.new }
let(:user, &args[:build_user])
end
RUBY
end

it 'approves of subject above let' do
expect_no_offenses(<<-RUBY)
RSpec.describe User do
Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cop/rspec/let_before_examples_spec.rb
Expand Up @@ -62,6 +62,25 @@
RUBY
end

it 'flags `let` with proc argument' do
expect_offense(<<-RUBY)
RSpec.describe User do
include_examples('should be after let')
let(:user, &args[:build_user])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Move `let` before the examples in the group.
end
RUBY

expect_correction(<<-RUBY)
RSpec.describe User do
let(:user, &args[:build_user])
include_examples('should be after let')
end
RUBY
end

it 'does not flag `let` before the examples' do
expect_no_offenses(<<-RUBY)
RSpec.describe User do
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/rspec/scattered_let_spec.rb
Expand Up @@ -25,4 +25,15 @@
end
RUBY
end

it 'flags `let` with proc argument' do
expect_offense(<<-RUBY)
RSpec.describe User do
let(:a) { a }
subject { User }
let(:user, &args[:build_user])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Group all let/let! blocks in the example group together.
end
RUBY
end
end

0 comments on commit 52a7fe1

Please sign in to comment.