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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layout/LineEndStringConcatenationIndentation - undefined method `parent' for nil:NilClass #11994

Closed
snutij opened this issue Jun 29, 2023 · 0 comments · Fixed by #11995
Closed
Labels

Comments

@snutij
Copy link

snutij commented Jun 29, 2023

Hello 👋

Given a file foo.rb:

x = %

Which is a valid syntax:

ruby -c foo.rb
Syntax OK

Expected behavior

Should not trigger error.

Actual behavior

rubocop -d --only Layout/LineEndStringConcatenationIndentation foo.rb

An error occurred while Layout/LineEndStringConcatenationIndentation cop was inspecting foo.rb:1:4.
undefined method `parent' for nil:NilClass
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb:127:in `base_column'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb:121:in `check_indented'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb:90:in `on_dstr'
/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

In LineEndStringConcatenationIndentation the on_dstr receive a node:

(rdbg) node
s(:dstr)

Then node.children result in:

(ruby) node.children
[]

So the base_column method receive a [][0] which is nil.

def on_dstr(node)
return unless strings_concatenated_with_backslash?(node)
children = node.children
if style == :aligned && !always_indented?(node)
check_aligned(children, 1)
else
check_indented(children)
check_aligned(children, 2)
end
end

def check_indented(children)
@column_delta = base_column(children[0]) + configured_indentation_width -
children[1].loc.column
add_offense_and_correction(children[1], MSG_INDENT) if @column_delta != 0
end

def base_column(child)
grandparent = child.parent.parent
if grandparent&.type == :pair
grandparent.loc.column
else
child.source_range.source_line =~ /\S/
end
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
…onIndentation`

Fixes rubocop#11994.

This PR fixes an error for `Layout/LineEndStringConcatenationIndentation`
when inspecting the `%` form string `%\n\n`.
@snutij snutij changed the title Layout/LineEndStringConcatenationIndentation - undefined method `parent' for nil:NilClass Layout/LineEndStringConcatenationIndentation - undefined method `parent' for nil:NilClass Jun 29, 2023
bbatsov pushed a commit that referenced this issue Jun 29, 2023
…tation`

Fixes #11994.

This PR fixes an error for `Layout/LineEndStringConcatenationIndentation`
when inspecting the `%` form string `%\n\n`.
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.

2 participants