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

[Fix #7517] Layout/WhitespaceAroundKeyword: allow super::const #7518

Merged
merged 1 commit into from Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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