Skip to content

Commit

Permalink
Merge pull request #1431 from ydah/fix_rspec_rails_inferred_spec_type
Browse files Browse the repository at this point in the history
Fix an error for `RSpec/Rails/InferredSpecType` with redundant type before other Hash metadata
  • Loading branch information
pirj authored and bquorning committed Oct 24, 2022
1 parent 472c64f commit c98efd1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Fix an error for `RSpec/Rails/InferredSpecType` with redundant type before other Hash metadata. ([@ydah])

## 2.14.0 (2022-10-23)

- Add `require_implicit` style to `RSpec/ImplicitSubject`. ([@r7kamura])
Expand Down
18 changes: 14 additions & 4 deletions lib/rubocop/cop/rspec/rails/inferred_spec_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,21 @@ def on_block(node)
# @param [RuboCop::AST::Corrector] corrector
# @param [RuboCop::AST::Node] node
def autocorrect(corrector, node)
corrector.remove(
node.location.expression.with(
begin_pos: node.left_sibling.location.expression.end_pos
corrector.remove(remove_range(node))
end

# @param [RuboCop::AST::Node] node
# @return [Parser::Source::Range]
def remove_range(node)
if node.left_sibling
node.loc.expression.with(
begin_pos: node.left_sibling.loc.expression.end_pos
)
)
elsif node.right_sibling
node.loc.expression.with(
end_pos: node.right_sibling.loc.expression.begin_pos
)
end
end

# @param [RuboCop::AST::PairNode] node
Expand Down
17 changes: 16 additions & 1 deletion spec/rubocop/cop/rspec/rails/inferred_spec_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,22 @@
end
end

describe 'with redundant type and other Hash metadata' do
describe 'with redundant type before other Hash metadata' do
it 'register and corrects an offense' do
expect_offense(<<~RUBY, '/path/to/project/spec/models/user_spec.rb')
RSpec.describe User, type: :model, other: true do
^^^^^^^^^^^^ Remove redundant spec type.
end
RUBY

expect_correction(<<~RUBY)
RSpec.describe User, other: true do
end
RUBY
end
end

describe 'with redundant type after other Hash metadata' do
it 'register and corrects an offense' do
expect_offense(<<~RUBY, '/path/to/project/spec/models/user_spec.rb')
RSpec.describe User, other: true, type: :model do
Expand Down

0 comments on commit c98efd1

Please sign in to comment.