diff --git a/changelog/fix_an_error_when_inspecting_blank_heredoc_delimiter.md b/changelog/fix_an_error_when_inspecting_blank_heredoc_delimiter.md new file mode 100644 index 000000000000..e0563dae4d40 --- /dev/null +++ b/changelog/fix_an_error_when_inspecting_blank_heredoc_delimiter.md @@ -0,0 +1 @@ +* [#11998](https://github.com/rubocop/rubocop/issues/11998): Fix an error when inspecting blank heredoc delimiter. ([@koic][]) diff --git a/lib/rubocop/cop/mixin/heredoc.rb b/lib/rubocop/cop/mixin/heredoc.rb index 2ed16bb661bb..52f64f5cd7db 100644 --- a/lib/rubocop/cop/mixin/heredoc.rb +++ b/lib/rubocop/cop/mixin/heredoc.rb @@ -26,11 +26,15 @@ def indent_level(str) end def delimiter_string(node) - node.source.match(OPENING_DELIMITER).captures[1] + return '' unless (match = node.source.match(OPENING_DELIMITER)) + + match.captures[1] end def heredoc_type(node) - node.source.match(OPENING_DELIMITER).captures[0] + return '' unless (match = node.source.match(OPENING_DELIMITER)) + + match.captures[0] end end end diff --git a/spec/rubocop/cop/naming/heredoc_delimiter_case_spec.rb b/spec/rubocop/cop/naming/heredoc_delimiter_case_spec.rb index df309ed6451a..bb50c2267c8d 100644 --- a/spec/rubocop/cop/naming/heredoc_delimiter_case_spec.rb +++ b/spec/rubocop/cop/naming/heredoc_delimiter_case_spec.rb @@ -173,6 +173,14 @@ RUBY end end + + context 'when using blank heredoc delimiters' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + <<'' + RUBY + end + end end context 'with a squiggly heredoc' do diff --git a/spec/rubocop/cop/style/redundant_heredoc_delimiter_quotes_spec.rb b/spec/rubocop/cop/style/redundant_heredoc_delimiter_quotes_spec.rb index 95b19111c2e3..842f69bf198f 100644 --- a/spec/rubocop/cop/style/redundant_heredoc_delimiter_quotes_spec.rb +++ b/spec/rubocop/cop/style/redundant_heredoc_delimiter_quotes_spec.rb @@ -161,4 +161,10 @@ EOS RUBY end + + it 'does not register an offense when using the blank heredoc delimiter' do + expect_no_offenses(<<~RUBY) + <<'' + RUBY + end end