Skip to content

Commit

Permalink
Merge pull request #12092 from jonas054/12061_StringLiteralsInInterpo…
Browse files Browse the repository at this point in the history
…lation_regex

[Fix #12061] Support regex in StringLiteralsInInterpolation
  • Loading branch information
koic committed Aug 2, 2023
2 parents fe172e4 + 8b4de3a commit 3eb5dda
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12061](https://github.com/rubocop/rubocop/issues/12061): Support regex in StringLiteralsInInterpolation. ([@jonas054][])
6 changes: 4 additions & 2 deletions lib/rubocop/cop/mixin/string_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ def on_regexp(node)
private

def inside_interpolation?(node)
# A :begin node inside a :dstr or :dsym node is an interpolation.
node.ancestors.drop_while { |a| !a.begin_type? }.any? { |a| a.dstr_type? || a.dsym_type? }
# A :begin node inside a :dstr, :dsym, or :regexp node is an interpolation.
node.ancestors
.drop_while { |a| !a.begin_type? }
.any? { |a| a.dstr_type? || a.dsym_type? || a.regexp_type? }
end
end
end
Expand Down
35 changes: 30 additions & 5 deletions lib/rubocop/cop/style/string_literals_in_interpolation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,42 @@
module RuboCop
module Cop
module Style
# Checks that quotes inside the string interpolation
# Checks that quotes inside string, symbol, and regexp interpolations
# match the configured preference.
#
# @example EnforcedStyle: single_quotes (default)
# # bad
# result = "Tests #{success ? "PASS" : "FAIL"}"
# string = "Tests #{success ? "PASS" : "FAIL"}"
# symbol = :"Tests #{success ? "PASS" : "FAIL"}"
# heredoc = <<~TEXT
# Tests #{success ? "PASS" : "FAIL"}
# TEXT
# regexp = /Tests #{success ? "PASS" : "FAIL"}/
#
# # good
# result = "Tests #{success ? 'PASS' : 'FAIL'}"
# string = "Tests #{success ? 'PASS' : 'FAIL'}"
# symbol = :"Tests #{success ? 'PASS' : 'FAIL'}"
# heredoc = <<~TEXT
# Tests #{success ? 'PASS' : 'FAIL'}
# TEXT
# regexp = /Tests #{success ? 'PASS' : 'FAIL'}/
#
# @example EnforcedStyle: double_quotes
# # bad
# result = "Tests #{success ? 'PASS' : 'FAIL'}"
# string = "Tests #{success ? 'PASS' : 'FAIL'}"
# symbol = :"Tests #{success ? 'PASS' : 'FAIL'}"
# heredoc = <<~TEXT
# Tests #{success ? 'PASS' : 'FAIL'}
# TEXT
# regexp = /Tests #{success ? 'PASS' : 'FAIL'}/
#
# # good
# result = "Tests #{success ? "PASS" : "FAIL"}"
# string = "Tests #{success ? "PASS" : "FAIL"}"
# symbol = :"Tests #{success ? "PASS" : "FAIL"}"
# heredoc = <<~TEXT
# Tests #{success ? "PASS" : "FAIL"}
# TEXT
# regexp = /Tests #{success ? "PASS" : "FAIL"}/
class StringLiteralsInInterpolation < Base
include ConfigurableEnforcedStyle
include StringLiteralsHelp
Expand All @@ -29,6 +49,11 @@ def autocorrect(corrector, node)
StringLiteralCorrector.correct(corrector, node, style)
end

# Cop classes that include the StringHelp module usually ignore regexp
# nodes. Not so for this cop, which is why we override the on_regexp
# definition with an empty one.
def on_regexp(node); end

private

def message(_node)
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/style/string_literals_in_interpolation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@
SOURCE
end

it 'registers an offense for double quotes within a regexp' do
expect_offense(<<~'RUBY')
/foo#{"sar".sub("s", 'b')}/
^^^^^ Prefer single-quoted strings inside interpolations.
^^^ Prefer single-quoted strings inside interpolations.
RUBY

expect_correction(<<~'RUBY')
/foo#{'sar'.sub('s', 'b')}/
RUBY
end

it 'accepts double quotes on a static string' do
expect_no_offenses('"A"')
end
Expand Down

0 comments on commit 3eb5dda

Please sign in to comment.