From d0540776dc094fb85aa5d1d16cb79130e27e7b0e Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 30 Jun 2023 00:38:05 +0900 Subject: [PATCH] [Fix #11998] Fix an error when inspecting blank heredoc delimiter Fixes #11998. This PR fixes an error when inspecting blank heredoc delimiter. --- ...ix_an_error_when_inspecting_blank_heredoc_delimiter.md | 1 + lib/rubocop/cop/mixin/heredoc.rb | 8 ++++++-- spec/rubocop/cop/naming/heredoc_delimiter_case_spec.rb | 8 ++++++++ .../cop/style/redundant_heredoc_delimiter_quotes_spec.rb | 6 ++++++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_an_error_when_inspecting_blank_heredoc_delimiter.md 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