Skip to content

Commit

Permalink
Merge pull request #7518 from ozydingo/layout-space_around_keyword-na…
Browse files Browse the repository at this point in the history
…mespace

[Fix #7517] Layout/WhitespaceAroundKeyword: allow `super::const`
  • Loading branch information
koic committed Nov 22, 2019
2 parents f6ab020 + 0a0c442 commit aa09fd6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down Expand Up @@ -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
12 changes: 12 additions & 0 deletions lib/rubocop/cop/layout/space_around_keyword.rb
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/rubocop/cop/layout/space_around_keyword_spec.rb
Expand Up @@ -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'
Expand Down

0 comments on commit aa09fd6

Please sign in to comment.