Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layout/ClosingHeredocIndentation & Style/RedundantHeredocDelimiterQuotes - undefined method `captures' for nil:NilClass #11998

Closed
snutij opened this issue Jun 29, 2023 · 1 comment 路 Fixed by #11999
Labels

Comments

@snutij
Copy link

snutij commented Jun 29, 2023

Hello 馃憢 I open an issue for both cops, because it failed on the same method heredoc_type do not hesitate to tell me if you prefer two distinct issues.

Given a file foo.rb:

<<""

Which is a valid syntax:

ruby -c foo.rb
Syntax OK

Expected behavior

Should not trigger an error. I think this is a valid heredoc syntax, as it start with << maybe some related offenses can be displayed. I'm very terrible with regex, so I can't dig more on group expectation etc. 馃槥

Actual behavior

bin/rubocop -d --only Style/RedundantHeredocDelimiterQuotes foo.rb

An error occurred while Style/RedundantHeredocDelimiterQuotes cop was inspecting foo.rb:1:0.
undefined method `captures' for nil:NilClass
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/mixin/heredoc.rb:34:in `heredoc_type'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb:49:in `need_heredoc_delimiter_quotes?'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb:37:in `on_heredoc'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/mixin/heredoc.rb:12:in `on_str'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/commissioner.rb:107:in `public_send'

bin/rubocop -d --only Style/ClosingHeredocIndentation foo.rb

An error occurred while Layout/ClosingHeredocIndentation cop was inspecting foo.rb:1:0.
undefined method `captures' for nil:NilClass
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/mixin/heredoc.rb:34:in `heredoc_type'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/layout/closing_heredoc_indentation.rb:59:in `on_heredoc'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/mixin/heredoc.rb:12:in `on_str'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/commissioner.rb:107:in `public_send'

Steps to reproduce the problem

Both Layout/ClosingHeredocIndentation & Style/RedundantHeredocDelimiterQuotes are using the heredoc_type method from the module RuboCop::Cop::Heredoc

Which receive a node:

(rdbg) node
s(:dstr)
(ruby) node.source
"<<\"\""

Nothing match with the pattern:
OPENING_DELIMITER = /(<<[~-]?)['"`]?([^'"`]+)['"`]?/.freeze

(ruby) node.source.match(OPENING_DELIMITER)
nil

So the capture group node.source.match(OPENING_DELIMITER).captures[0] trigger an error.

def on_heredoc(node)
return if heredoc_type(node) == SIMPLE_HEREDOC ||
opening_indentation(node) == closing_indentation(node) ||
argument_indentation_correct?(node)
message = message(node)
add_offense(node.loc.heredoc_end, message: message) do |corrector|
corrector.replace(node.loc.heredoc_end, indented_end(node))
end
end

def need_heredoc_delimiter_quotes?(node)
heredoc_delimiter = node.source.delete(heredoc_type(node))
return true unless heredoc_delimiter.start_with?("'", '"')
node.loc.heredoc_end.source.strip.match?(/\W/) ||
node.loc.heredoc_body.source.match?(STRING_INTERPOLATION_OR_ESCAPED_CHARACTER_PATTERN)
end

def heredoc_type(node)
node.source.match(OPENING_DELIMITER).captures[0]
end

RuboCop version

bundle exec rubocop -V
1.53.1 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.2.2) [arm64-darwin22]
  - rubocop-minitest 0.31.0
  - rubocop-rake 0.6.0
  - rubocop-sorbet 0.7.0
@koic koic added the bug label Jun 29, 2023
koic added a commit to koic/rubocop that referenced this issue Jun 29, 2023
Fixes rubocop#11998.

This PR fixes an error when inspecting blank heredoc delimiter.
bbatsov pushed a commit that referenced this issue Jun 30, 2023
Fixes #11998.

This PR fixes an error when inspecting blank heredoc delimiter.
@AlexWayfer
Copy link
Contributor

AlexWayfer commented Jul 2, 2023

This can be relevant: #12013

Or this: rubocop/rubocop-rspec#1667

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants