Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Saurabh Maurya <saurabh.maurya@scripbox.com>
- Loading branch information
1 parent
ba598be
commit d4a9bf5
Showing
9 changed files
with
672 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
lib/rubocop/cop/layout/space_around_method_call_operator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Layout | ||
# Checks method call operators to not have spaces around them. | ||
# | ||
# @example | ||
# # bad | ||
# foo. bar | ||
# foo .bar | ||
# foo . bar | ||
# foo. bar .buzz | ||
# foo | ||
# . bar | ||
# . buzz | ||
# foo&. bar | ||
# foo &.bar | ||
# foo &. bar | ||
# foo &. bar&. buzz | ||
# RuboCop:: Cop | ||
# RuboCop:: Cop:: Cop | ||
# :: RuboCop::Cop | ||
# | ||
# # good | ||
# foo.bar | ||
# foo.bar.buzz | ||
# foo | ||
# .bar | ||
# .buzz | ||
# foo&.bar | ||
# foo&.bar&.buzz | ||
# RuboCop::Cop | ||
# RuboCop::Cop::Cop | ||
# ::RuboCop::Cop | ||
# | ||
class SpaceAroundMethodCallOperator < Cop | ||
include SurroundingSpace | ||
|
||
MSG = 'Avoid using spaces around a method call operator.' | ||
|
||
def on_send(node) | ||
return unless dot_or_safe_navigation_operator?(node) | ||
|
||
check_and_add_offense(node) | ||
end | ||
|
||
def on_const(node) | ||
return unless node.loc.double_colon | ||
|
||
check_and_add_offense(node, false) | ||
end | ||
|
||
def autocorrect(node) | ||
operator = operator_token(node) | ||
left = left_token_for_auto_correction(node, operator) | ||
right = right_token_for_auto_correction(operator) | ||
|
||
lambda do |corrector| | ||
SpaceCorrector.remove_space( | ||
processed_source, corrector, left, right | ||
) | ||
end | ||
end | ||
|
||
alias on_csend on_send | ||
|
||
private | ||
|
||
def check_and_add_offense(node, add_left_offense = true) | ||
operator = operator_token(node) | ||
left = previous_token(operator) | ||
right = next_token(operator) | ||
|
||
if valid_right_token?(right, operator) | ||
no_space_offenses(node, operator, right, MSG) | ||
end | ||
return unless valid_left_token?(left, operator) | ||
|
||
no_space_offenses(node, left, operator, MSG) if add_left_offense | ||
end | ||
|
||
def operator_token(node) | ||
operator_location = | ||
node.const_type? ? node.loc.double_colon : node.loc.dot | ||
|
||
processed_source.find_token do |token| | ||
token.pos == operator_location | ||
end | ||
end | ||
|
||
def previous_token(current_token) | ||
index = processed_source.tokens.index(current_token) | ||
index.zero? ? nil : processed_source.tokens[index - 1] | ||
end | ||
|
||
def next_token(current_token) | ||
index = processed_source.tokens.index(current_token) | ||
processed_source.tokens[index + 1] | ||
end | ||
|
||
def dot_or_safe_navigation_operator?(node) | ||
node.dot? || node.safe_navigation? | ||
end | ||
|
||
def valid_left_token?(left, operator) | ||
left && left.line == operator.line | ||
end | ||
|
||
def valid_right_token?(right, operator) | ||
right && right.line == operator.line | ||
end | ||
|
||
def left_token_for_auto_correction(node, operator) | ||
left_token = previous_token(operator) | ||
return operator if node.const_type? | ||
return left_token if valid_left_token?(left_token, operator) | ||
|
||
operator | ||
end | ||
|
||
def right_token_for_auto_correction(operator) | ||
right_token = next_token(operator) | ||
return right_token if valid_right_token?(right_token, operator) | ||
|
||
operator | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.