diff --git a/lib/tailor/rulers/allow_unnecessary_double_quotes_ruler.rb b/lib/tailor/rulers/allow_unnecessary_double_quotes_ruler.rb index 862afed..9c3257f 100644 --- a/lib/tailor/rulers/allow_unnecessary_double_quotes_ruler.rb +++ b/lib/tailor/rulers/allow_unnecessary_double_quotes_ruler.rb @@ -11,7 +11,8 @@ def initialize(config, options) def nl_update(lexed_line, lineno, _) quotes(lexed_line).each do |quote| unless contains_embedded_expression?(quote) || - contains_escape_sequence?(quote) + contains_escape_sequence?(quote) || + contains_single_quote?(quote) measure(lineno, column(quote.first)) end end @@ -39,6 +40,12 @@ def contains_escape_sequence?(tokens) end end + def contains_single_quote?(tokens) + tokens.any? do |t| + t[1] == :on_tstring_content and t[2].match(/'/) + end + end + def quotes(tokens) tokens.select do |t| true if (double_quote_start?(t))..(double_quote_end?(t)) diff --git a/spec/functional/string_quoting_spec.rb b/spec/functional/string_quoting_spec.rb index 2fb26c5..e5d42f0 100644 --- a/spec/functional/string_quoting_spec.rb +++ b/spec/functional/string_quoting_spec.rb @@ -87,6 +87,13 @@ def contents end end + context :double_quotes_contains_single_quote do + it 'does not warn' do + critic.check_file(file_name, style.to_hash) + expect(critic.problems[file_name]).to be_empty + end + end + context :escape_sequence do it 'does not warn when a double quoted string contains a newline' do critic.check_file(file_name, style.to_hash) diff --git a/spec/support/string_quoting_cases.rb b/spec/support/string_quoting_cases.rb index c71b0b4..8101fcd 100644 --- a/spec/support/string_quoting_cases.rb +++ b/spec/support/string_quoting_cases.rb @@ -23,3 +23,7 @@ QUOTING['nested_quotes'] = %q{foo = "foo#{bar('baz')}" } + +QUOTING['double_quotes_contains_single_quote'] = + %q{foo = "foo'd" +}