Skip to content

Commit

Permalink
Fix breaking changes detailed in https://docs.rubocop.org/rubocop/v1_…
Browse files Browse the repository at this point in the history
  • Loading branch information
ric2b committed Jun 24, 2021
1 parent 4708f67 commit bbd011e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 35 deletions.
6 changes: 3 additions & 3 deletions lib/rubocop/cop/salsify/rspec_doc_string.rb
Expand Up @@ -57,9 +57,9 @@ def on_send(node)
def check_quotes(doc_node)
return unless wrong_quotes?(doc_node)

add_offense(doc_node,
message: style == :single_quotes ? SINGLE_QUOTE_MSG : DOUBLE_QUOTE_MSG,
&StringLiteralCorrector.correct(doc_node, style))
add_offense(doc_node, message: style == :single_quotes ? SINGLE_QUOTE_MSG : DOUBLE_QUOTE_MSG) do |corrector|
StringLiteralCorrector.correct(corrector, doc_node, style)
end
end

def wrong_quotes?(node)
Expand Down
7 changes: 4 additions & 3 deletions lib/rubocop/cop/salsify/rspec_string_literals.rb
Expand Up @@ -11,7 +11,8 @@ module Salsify
# Used together with Salsify/RspecDocString it allows one quote style to
# be used for doc strings (`describe "foobar"`) and another style to be
# used for all other strings in specs.
class RspecStringLiterals < Cop
class RspecStringLiterals < RuboCop::Cop::RSpec::Base
extend RuboCop::Cop::AutoCorrector
include ConfigurableEnforcedStyle
include StringLiteralsHelp

Expand All @@ -22,8 +23,8 @@ class RspecStringLiterals < Cop
DOUBLE_QUOTE_MSG = 'Prefer double-quoted strings unless you need ' \
'single quotes to avoid extra backslashes for escaping.'

def autocorrect(node)
StringLiteralCorrector.correct(node, style)
def autocorrect(corrector, node)
StringLiteralCorrector.correct(corrector, node, style)
end

private
Expand Down
78 changes: 49 additions & 29 deletions spec/rubocop/cop/salsify/rspec_string_literals_spec.rb
@@ -1,25 +1,29 @@
# frozen_string_literal: true

describe RuboCop::Cop::Salsify::RspecStringLiterals, :config do
subject(:cop) { described_class.new(config) }

shared_examples_for "string quoting exceptions" do |name|
it "accepts `#{name}` with a single character" do
inspect_source(["#{name} 'ignored' do", '?a', 'end'].join($RS))
expect(cop.offenses).to be_empty
expect_no_offenses(<<~RUBY)
#{name} 'ignored' do
?a
end
RUBY
end

it "accepts `#{name}` with a %q string" do
inspect_source(["#{name} 'ignored' do", '%q(doc string)', 'end'].join($RS))
expect(cop.offenses).to be_empty
expect_no_offenses(<<~RUBY)
#{name} 'ignored' do
%q(doc string)
end
RUBY
end

it "accepts `#{name}` with a string the requires interpolation" do
# rubocop:disable Lint/InterpolationCheck
doc_string = '"#{\'DOC\'.downcase} string"'
# rubocop:enable Lint/InterpolationCheck
inspect_source(["#{name} 'ignored' do", doc_string, 'end'].join($RS))
expect(cop.offenses).to be_empty
expect_no_offenses(<<~RUBY)
#{name} 'ignored' do
"\#{'DOC'.downcase} string"
end
RUBY
end
end

Expand All @@ -29,18 +33,26 @@
described_class::DOCUMENTED_METHODS.each do |name|
context "within #{name}" do
it "corrects a double-quoted string" do
source = ["#{name} \"doc string\" do", '"literal"', 'end']
inspect_source(source.join($RS))
expect(cop.messages).to eq([described_class::SINGLE_QUOTE_MSG])
expect(cop.highlights).to eq([source[1]])
expect(autocorrect_source(source.join($RS)))
.to eq("#{name} \"doc string\" do\n'literal'\nend")
expect_offense(<<~RUBY)
#{name} "doc string" do
"literal"
^^^^^^^^^ Prefer single-quoted strings unless you need interpolation or special symbols.
end
RUBY

expect_correction(<<~RUBY)
#{name} "doc string" do
'literal'
end
RUBY
end

it "accepts a single-quoted string" do
source = ["#{name} 'doc string' do", "'literal'", 'end']
inspect_source(source.join($RS))
expect(cop.offenses).to be_empty
expect_no_offenses(<<~RUBY)
#{name} 'doc string' do
'literal'
end
RUBY
end

it_behaves_like "string quoting exceptions", name
Expand All @@ -54,18 +66,26 @@
described_class::DOCUMENTED_METHODS.each do |name|
context "within #{name}" do
it "corrects a single-quoted string" do
source = ["#{name} 'doc string' do", "'literal'", 'end']
inspect_source(source.join($RS))
expect(cop.messages).to eq([described_class::DOUBLE_QUOTE_MSG])
expect(cop.highlights).to eq([source[1]])
expect(autocorrect_source(source.join($RS)))
.to eq("#{name} 'doc string' do\n\"literal\"\nend")
expect_offense(<<~RUBY)
#{name} 'doc string' do
'literal'
^^^^^^^^^ Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
end
RUBY

expect_correction(<<~RUBY)
#{name} 'doc string' do
"literal"
end
RUBY
end

it "accepts a double-quoted string" do
source = ["#{name} \"doc string\" do", '"literal"', 'end']
inspect_source(source.join($RS))
expect(cop.offenses).to be_empty
expect_no_offenses(<<~RUBY)
#{name} "doc string" do
"literal"
end
RUBY
end

it_behaves_like "string quoting exceptions", name
Expand Down

0 comments on commit bbd011e

Please sign in to comment.