From 0a0c4427f0c32a14125090b41cb75401c2e22310 Mon Sep 17 00:00:00 2001 From: Andrew Schwartz Date: Wed, 20 Nov 2019 03:36:19 -0500 Subject: [PATCH] [Fix #7517] Layout/WhitespaceAroundKeyword: allow `super::const` --- CHANGELOG.md | 2 ++ lib/rubocop/cop/layout/space_around_keyword.rb | 12 ++++++++++++ spec/rubocop/cop/layout/space_around_keyword_spec.rb | 1 + 3 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cbc053c9cce..8dd55060d07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * [#7493](https://github.com/rubocop-hq/rubocop/issues/7493): Fix `Style/RedundantReturn` to inspect conditional constructs that are preceded by other statements. ([@buehmann][]) * [#7509](https://github.com/rubocop-hq/rubocop/issues/7509): Fix `Layout/SpaceInsideArrayLiteralBrackets` to correct empty lines. ([@ayacai115][]) +* [#7517](https://github.com/rubocop-hq/rubocop/issues/7517): `Style/SpaceAroundKeyword` allows `::` after `super`. ([@ozydingo][]) ### Changes @@ -4260,3 +4261,4 @@ [@cstyles]: https://github.com/cstyles [@avmnu-sng]: https://github.com/avmnu-sng [@ayacai115]: https://github.com/ayacai115 +[@ozydingo]: https://github.com/ozydingo diff --git a/lib/rubocop/cop/layout/space_around_keyword.rb b/lib/rubocop/cop/layout/space_around_keyword.rb index 166efc56375..f3dc4fdd173 100644 --- a/lib/rubocop/cop/layout/space_around_keyword.rb +++ b/lib/rubocop/cop/layout/space_around_keyword.rb @@ -30,10 +30,12 @@ class SpaceAroundKeyword < Cop DO = 'do' SAFE_NAVIGATION = '&.' + NAMESPACE_OPERATOR = '::' ACCEPT_LEFT_PAREN = %w[break defined? next not rescue return super yield].freeze ACCEPT_LEFT_SQUARE_BRACKET = %w[super yield].freeze + ACCEPT_NAMESPACE_OPERATOR = 'super' def on_and(node) check(node, [:operator].freeze) if node.keyword? @@ -193,6 +195,8 @@ def space_after_missing?(range) return false if accepted_opening_delimiter?(range, char) return false if safe_navigation_call?(range, pos) + return false if accept_namespace_operator?(range) && + namespace_operator?(range, pos) char !~ /[\s;,#\\\)\}\]\.]/ end @@ -212,10 +216,18 @@ def accept_left_square_bracket?(range) ACCEPT_LEFT_SQUARE_BRACKET.include?(range.source) end + def accept_namespace_operator?(range) + ACCEPT_NAMESPACE_OPERATOR == range.source + end + def safe_navigation_call?(range, pos) range.source_buffer.source[pos, 2].start_with?(SAFE_NAVIGATION) end + def namespace_operator?(range, pos) + range.source_buffer.source[pos, 2].start_with?(NAMESPACE_OPERATOR) + end + def preceded_by_operator?(node, _range) # regular dotted method calls bind more tightly than operators # so we need to climb up the AST past them diff --git a/spec/rubocop/cop/layout/space_around_keyword_spec.rb b/spec/rubocop/cop/layout/space_around_keyword_spec.rb index 0239662b7c3..98ea8f67db0 100644 --- a/spec/rubocop/cop/layout/space_around_keyword_spec.rb +++ b/spec/rubocop/cop/layout/space_around_keyword_spec.rb @@ -158,6 +158,7 @@ it_behaves_like 'accept after', '.', 'yield.method' it_behaves_like 'accept before', '!', '!yield.method' it_behaves_like 'accept before', '!', '!super.method' + it_behaves_like 'accept after', '::', 'super::ModuleName' context '&.' do it_behaves_like 'accept after', '&.', 'super&.foo'