forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RSpec/NoStringifiedNoStringifiedInstanceDoubleConstant
Addresses rubocop#1136
- Loading branch information
1 parent
16cf19c
commit 9f1a248
Showing
10 changed files
with
173 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# EXAMPLE USAGE: | ||
# | ||
# Refer for explanation to following link: | ||
# https://github.com/evilmartians/lefthook/blob/master/docs/configuration.md | ||
# | ||
# pre-push: | ||
# commands: | ||
# packages-audit: | ||
# tags: frontend security | ||
# run: yarn audit | ||
# gems-audit: | ||
# tags: backend security | ||
# run: bundle audit | ||
# | ||
# pre-commit: | ||
# parallel: true | ||
# commands: | ||
# eslint: | ||
# glob: "*.{js,ts,jsx,tsx}" | ||
# run: yarn eslint {staged_files} | ||
# rubocop: | ||
# tags: backend style | ||
# glob: "*.rb" | ||
# exclude: '(^|/)(application|routes)\.rb$' | ||
# run: bundle exec rubocop --force-exclusion {all_files} | ||
# govet: | ||
# tags: backend style | ||
# files: git ls-files -m | ||
# glob: "*.go" | ||
# run: go vet {files} | ||
# scripts: | ||
# "hello.js": | ||
# runner: node | ||
# "any.go": | ||
# runner: go run |
42 changes: 42 additions & 0 deletions
42
lib/rubocop/cop/rspec/no_stringified_instance_double_constant.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Do not use a string as `instance_double` constant. | ||
# | ||
# @example | ||
# # bad | ||
# instance_double('Foo', failure_message, stubs) | ||
# | ||
# # good | ||
# instance_double(Foo, failure_message, stubs) | ||
# | ||
class NoStringifiedInstanceDoubleConstant < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Do not use a string as `instance_double` constant.' | ||
RESTRICT_ON_SEND = %i[instance_double].freeze | ||
|
||
# @!method stringified_instance_double_const?(node) | ||
def_node_matcher :stringified_instance_double_const?, <<~PATTERN | ||
(send nil? :instance_double $...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
stringified_instance_double_const?(node) do |args_node| | ||
if args_node.first.str_type? | ||
add_offense(args_node.first) do |corrector| | ||
autocorrect(corrector, args_node.first) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def autocorrect(corrector, node) | ||
corrector.replace(node, node.value) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
spec/rubocop/cop/rspec/no_stringified_instance_double_constant_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::NoStringifiedInstanceDoubleConstant, | ||
:config do | ||
context 'when using a class for instance_double' do | ||
let(:source) do | ||
<<~RUBY | ||
instance_double(Foo, failure_message, stubs) | ||
RUBY | ||
end | ||
|
||
it { expect_no_offenses source } | ||
end | ||
|
||
context 'when using a symbol for instance_double' do | ||
let(:source) do | ||
<<~RUBY | ||
instance_double(:Foo, failure_message, stubs) | ||
RUBY | ||
end | ||
|
||
it { expect_no_offenses source } | ||
end | ||
|
||
context 'when using a string for instance_double' do | ||
it 'replaces the string with the class' do | ||
expect_offense <<~RUBY | ||
instance_double('Foo', failure_message, stubs) | ||
^^^^^ Do not use a string as `instance_double` constant. | ||
RUBY | ||
|
||
expect_correction <<~RUBY | ||
instance_double(Foo, failure_message, stubs) | ||
RUBY | ||
end | ||
end | ||
end |