From 06309e1fc66c9b2f72f795459c02db58a7de8e36 Mon Sep 17 00:00:00 2001 From: Tejas Bubane Date: Sun, 29 Mar 2020 17:30:11 +0530 Subject: [PATCH] [Fix #7825] Fix crash for `Layout/MultilineMethodCallIndentation` With key access to hash. --- CHANGELOG.md | 1 + .../cop/layout/multiline_method_call_indentation.rb | 2 +- .../cop/layout/multiline_method_call_indentation_spec.rb | 9 +++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb915212dda..98b262fd229 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ * [#7806](https://github.com/rubocop-hq/rubocop/pull/7806): Fix an error for `Lint/ErbNewArguments` cop when inspecting `ActionView::Template::Handlers::ERB.new`. ([@koic][]) * [#7814](https://github.com/rubocop-hq/rubocop/issues/7814): Fix a false positive for `Migrate/DepartmentName` cop when inspecting an unexpected disabled comment format. ([@koic][]) * [#7728](https://github.com/rubocop-hq/rubocop/issues/7728): Fix an error for `Style/OneLineConditional` when one of the branches contains a self keyword. ([@koic][]) +* [#7825](https://github.com/rubocop-hq/rubocop/issues/7825): Fix crash for `Layout/MultilineMethodCallIndentation` with key access to hash. ([@tejasbubane][]) ### Changes diff --git a/lib/rubocop/cop/layout/multiline_method_call_indentation.rb b/lib/rubocop/cop/layout/multiline_method_call_indentation.rb index 3cf0bb1c578..d22bae7347e 100644 --- a/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +++ b/lib/rubocop/cop/layout/multiline_method_call_indentation.rb @@ -193,7 +193,7 @@ def semantic_alignment_node(node) node = node.receiver while node.receiver # ascend to first call which has a dot node = node.parent - node = node.parent until node.loc.dot + node = node.parent until node.loc.respond_to?(:dot) && node.loc.dot return if node.loc.dot.line != node.first_line diff --git a/spec/rubocop/cop/layout/multiline_method_call_indentation_spec.rb b/spec/rubocop/cop/layout/multiline_method_call_indentation_spec.rb index 500dbc701f0..b96e1646966 100644 --- a/spec/rubocop/cop/layout/multiline_method_call_indentation_spec.rb +++ b/spec/rubocop/cop/layout/multiline_method_call_indentation_spec.rb @@ -304,6 +304,15 @@ def foo RUBY end + context 'target_ruby_version >= 2.5', :ruby25 do + it 'accepts key access to hash' do + expect_no_offenses(<<~RUBY) + hash[key] { 10 / 0 } + .fmap { |x| x * 3 } + RUBY + end + end + it 'accepts 3 aligned methods' do expect_no_offenses(<<~RUBY) a_class.new(severity, location, 'message', 'CopName')