Skip to content

Commit

Permalink
Add NoLet cop
Browse files Browse the repository at this point in the history
  • Loading branch information
mockdeep committed Jan 20, 2020
1 parent e76f7d4 commit 2b8310d
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Fix `RSpec/InstanceVariable` detection inside custom matchers. ([@pirj][])
* Fix `RSpec/ScatteredSetup` to distinguish hooks with different metadata. ([@pirj][])
* Add autocorrect support for `RSpec/ExpectActual` cop. ([@dduugg][], [@pirj][])
* Add `RSpec/NoLet` cop. ([@mockdeep][])

## 1.37.1 (2019-12-16)

Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,12 @@ RSpec/NestedGroups:
Max: 3
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NestedGroups

RSpec/NoLet:
Description: Checks for usage of `let` blocks in specs.
Enabled: false
AllowSubject: false
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NoLet

RSpec/NotToNot:
Description: Checks for consistent method usage for negating expectations.
EnforcedStyle: not_to
Expand Down
85 changes: 85 additions & 0 deletions lib/rubocop/cop/rspec/no_let.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# frozen_string_literal: true

# TODO: when finished, run `rake generate_cops_documentation` to update the docs
module RuboCop
module Cop
module RSpec
# Checks for usage of `let` blocks in specs.
#
# This cop can be configured with the option `AllowSubject` which
# will configure the cop to only register offenses on explicit calls to
# `let` and not calls to `subject`
#
# @example
# # bad
# describe MyClass do
# let(:foo) { [] }
# it { expect(foo).to be_empty }
# end
#
# describe MyClass do
# subject(:foo) { [] }
# it { expect(foo).to be_empty }
# end
#
# # good
# describe MyClass do
# it do
# foo = []
# expect(foo).to be_empty
# end
# end
#
# @example with AllowSubject configuration
#
# # rubocop.yml
# # RSpec/NoLet:
# # AllowSubject: true
#
# # bad
# describe MyClass do
# let(:foo) { [] }
# it { expect(foo).to be_empty }
# end
#
# # good
# describe MyClass do
# subject(:foo) { [] }
# it { expect(foo).to be_empty }
# end
#
# describe MyClass do
# it do
# foo = []
# expect(foo).to be_empty
# end
# end
#
class NoLet < Cop
MSG = 'Avoid using `let` and `subject` ' \
'– use a method call or local variable instead.'

def_node_matcher :let?, <<~PATTERN
(send nil? :let ...)
PATTERN

def_node_matcher :subject?, <<~PATTERN
(send nil? :subject ...)
PATTERN

def on_send(node)
return unless let?(node) || subject?(node)
return if subject?(node) && allow_subject?

add_offense(node)
end

private

def allow_subject?
cop_config['AllowSubject']
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
require_relative 'rspec/multiple_subjects'
require_relative 'rspec/named_subject'
require_relative 'rspec/nested_groups'
require_relative 'rspec/no_let'
require_relative 'rspec/not_to_not'
require_relative 'rspec/overwriting_setup'
require_relative 'rspec/pending'
Expand Down
1 change: 1 addition & 0 deletions manual/cops.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
* [RSpec/MultipleSubjects](cops_rspec.md#rspecmultiplesubjects)
* [RSpec/NamedSubject](cops_rspec.md#rspecnamedsubject)
* [RSpec/NestedGroups](cops_rspec.md#rspecnestedgroups)
* [RSpec/NoLet](cops_rspec.md#rspecnolet)
* [RSpec/NotToNot](cops_rspec.md#rspecnottonot)
* [RSpec/OverwritingSetup](cops_rspec.md#rspecoverwritingsetup)
* [RSpec/Pending](cops_rspec.md#rspecpending)
Expand Down
71 changes: 71 additions & 0 deletions manual/cops_rspec.md
Original file line number Diff line number Diff line change
Expand Up @@ -2263,6 +2263,77 @@ Max | `3` | Integer

* [https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NestedGroups](https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NestedGroups)

## RSpec/NoLet

Enabled by default | Supports autocorrection
--- | ---
Disabled | No

Checks for usage of `let` blocks in specs.

This cop can be configured with the option `AllowSubject` which
will configure the cop to only register offenses on explicit calls to
`let` and not calls to `subject`

### Examples

```ruby
# bad
describe MyClass do
let(:foo) { [] }
it { expect(foo).to be_empty }
end

describe MyClass do
subject(:foo) { [] }
it { expect(foo).to be_empty }
end

# good
describe MyClass do
it do
foo = []
expect(foo).to be_empty
end
end
```
#### with AllowSubject configuration

```ruby
# rubocop.yml
# RSpec/NoLet:
# AllowSubject: true

# bad
describe MyClass do
let(:foo) { [] }
it { expect(foo).to be_empty }
end

# good
describe MyClass do
subject(:foo) { [] }
it { expect(foo).to be_empty }
end

describe MyClass do
it do
foo = []
expect(foo).to be_empty
end
end
```

### Configurable attributes

Name | Default value | Configurable values
--- | --- | ---
AllowSubject | `false` | Boolean

### References

* [https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NoLet](https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NoLet)

## RSpec/NotToNot

Enabled by default | Supports autocorrection
Expand Down
64 changes: 64 additions & 0 deletions spec/rubocop/cop/rspec/no_let_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::RSpec::NoLet do
subject(:cop) { described_class.new(config) }

let(:config) { RuboCop::Config.new }

# TODO: Write test code
#
# For example
it 'registers an offense when using `#let`' do
expect_offense(<<~RUBY)
let(:foo) { Foo.new }
^^^^^^^^^ Avoid using `let` and `subject` – use a method call or local variable instead.
RUBY
end

it 'registers an offense when using `#subject` without name' do
expect_offense(<<~RUBY)
subject { Foo.new }
^^^^^^^ Avoid using `let` and `subject` – use a method call or local variable instead.
RUBY
end

it 'registers an offense when using `#subject` with name' do
expect_offense(<<~RUBY)
subject(:foo) { Foo.new }
^^^^^^^^^^^^^ Avoid using `let` and `subject` – use a method call or local variable instead.
RUBY
end

it 'does not register an offense when using `#before`' do
expect_no_offenses(<<~RUBY)
before { foo }
RUBY
end

context 'when using AllowSubject configuration', :config do
subject(:cop) { described_class.new(config) }

let(:cop_config) do
{ 'AllowSubject' => true }
end

it 'registers an offense when using `#let`' do
expect_offense(<<~RUBY)
let(:foo) { Foo.new }
^^^^^^^^^ Avoid using `let` and `subject` – use a method call or local variable instead.
RUBY
end

it 'does not register an offense when using `#subject` without a name' do
expect_no_offenses(<<~RUBY)
subject { Foo.new }
RUBY
end

it 'does not register an offense when using `#subject` with a name' do
expect_no_offenses(<<~RUBY)
subject(:foo) { Foo.new }
RUBY
end
end
end

0 comments on commit 2b8310d

Please sign in to comment.