-
-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
RSpec/VariableDefinition
cop
Closes #908 Also refactor common matcher into a module & change `RSpec/VariableName` cop to consider interpolated strings & symbols (they are parsed as `dsym` & `dstr` nodes).
- Loading branch information
1 parent
c361d5d
commit 4b1f284
Showing
11 changed files
with
209 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Checks that memoized helpers names are symbols or strings. | ||
# | ||
# @example EnforcedStyle: symbols (default) | ||
# # bad | ||
# let('user_name') { 'Adam' } | ||
# subject('user') { create_user } | ||
# | ||
# # good | ||
# let(:user_name) { 'Adam' } | ||
# subject(:user) { create_user } | ||
# | ||
# @example EnforcedStyle: strings | ||
# # bad | ||
# let(:user_name) { 'Adam' } | ||
# subject(:user) { create_user } | ||
# | ||
# # good | ||
# let('user_name') { 'Adam' } | ||
# subject('user') { create_user } | ||
class VariableDefinition < Cop | ||
include ConfigurableEnforcedStyle | ||
include RuboCop::RSpec::Variable | ||
|
||
MSG = 'Use %<style>s for variable names.' | ||
|
||
def on_send(node) | ||
variable_definition?(node) do |variable| | ||
if style_violation?(variable) | ||
add_offense(variable, message: format(MSG, style: style)) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def style_violation?(variable) | ||
style == :symbols && string?(variable) || | ||
style == :strings && symbol?(variable) | ||
end | ||
|
||
def string?(node) | ||
node.str_type? || node.dstr_type? | ||
end | ||
|
||
def symbol?(node) | ||
node.sym_type? || node.dsym_type? | ||
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
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,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module RSpec | ||
# Helps check offenses with variable definitions | ||
module Variable | ||
include Language | ||
extend RuboCop::NodePattern::Macros | ||
|
||
def_node_matcher :variable_definition?, <<~PATTERN | ||
(send #{RSPEC} #{(Helpers::ALL + Subject::ALL).node_pattern_union} | ||
$({sym str dsym dstr} ...) ...) | ||
PATTERN | ||
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
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,61 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::VariableDefinition, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
context 'when EnforcedStyle is `symbols`' do | ||
let(:cop_config) { { 'EnforcedStyle' => 'symbols' } } | ||
|
||
it 'registers an offense for string name' do | ||
expect_offense(<<~RUBY) | ||
let("user_name") { 'Adam' } | ||
^^^^^^^^^^^ Use symbols for variable names. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for interpolated string' do | ||
expect_offense(<<~'RUBY') | ||
let("user-#{id}") { 'Adam' } | ||
^^^^^^^^^^^^ Use symbols for variable names. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for multiline string' do | ||
expect_offense(<<~'RUBY') | ||
let("user"\ | ||
^^^^^^^ Use symbols for variable names. | ||
"-foo") { 'Adam' } | ||
RUBY | ||
end | ||
|
||
it 'does not register offense for symbol names' do | ||
expect_no_offenses(<<~RUBY) | ||
let(:user_name) { 'Adam' } | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when EnforcedStyle is `strings`' do | ||
let(:cop_config) { { 'EnforcedStyle' => 'strings' } } | ||
|
||
it 'registers an offense for symbol name' do | ||
expect_offense(<<~RUBY) | ||
let(:user_name) { 'Adam' } | ||
^^^^^^^^^^ Use strings for variable names. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for interpolated symbol' do | ||
expect_offense(<<~'RUBY') | ||
let(:"user-#{id}") { 'Adam' } | ||
^^^^^^^^^^^^^ Use strings for variable names. | ||
RUBY | ||
end | ||
|
||
it 'does not register offense for string names' do | ||
expect_no_offenses(<<~RUBY) | ||
let("user_name") { 'Adam' } | ||
RUBY | ||
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