Module: RuboCop::Cop::SurroundingSpace
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Extended In:
| |
Included In:
Layout::SpaceAroundEqualsInParameterDefault ,
Layout::SpaceInsideArrayLiteralBrackets ,
Layout::SpaceInsideBlockBraces ,
Layout::SpaceInsideHashLiteralBraces ,
Layout::SpaceInsideParens ,
Layout::SpaceInsideReferenceBrackets ,
Layout::SpaceInsideStringInterpolation ,
Lint::RedundantCopEnableDirective ,
Style::TernaryParentheses ,
Style::TrailingUnderscoreVariable
| |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
RangeHelp
|
|
Defined in: | lib/rubocop/cop/mixin/surrounding_space.rb |
Overview
Common functionality for checking and correcting surrounding whitespace.
Constant Summary
-
NO_SPACE_COMMAND =
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 9'Do not use'
-
SINGLE_SPACE_REGEXP =
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 12/[ \t]/.freeze
-
SPACE_COMMAND =
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 10'Use'
RangeHelp
- Included
Instance Method Summary
- #empty_brackets?(left_bracket_token, right_bracket_token, tokens: processed_source.tokens) ⇒ Boolean private
- #empty_offense(node, range, message, command) private
- #empty_offenses(node, left, right, message) private
- #extra_space?(token, side) ⇒ Boolean private
- #no_character_between?(left_bracket_token, right_bracket_token) ⇒ Boolean private
- #no_space_offenses(node, left_token, right_token, message, start_ok: false, end_ok: false) private
- #offending_empty_no_space?(config, left_token, right_token) ⇒ Boolean private
- #offending_empty_space?(config, left_token, right_token) ⇒ Boolean private
- #on_new_investigation private
- #reposition(src, pos, step, include_newlines: false) private
- #side_space_range(range:, side:, include_newlines: false) private
- #space_between?(left_bracket_token, right_bracket_token) ⇒ Boolean private
- #space_offense(node, token, side, message, command) private
- #space_offenses(node, left_token, right_token, message, start_ok: false, end_ok: false) private
RangeHelp
- Included
#add_range, #column_offset_between, | |
#contents_range | A range containing only the contents of a literal with delimiters (e.g. |
#directions, | |
#effective_column | Returns the column attribute of the range, except if the range is on the first line and there’s a byte order mark at the beginning of that line, in which case 1 is subtracted from the column value. |
#final_pos, #move_pos, #move_pos_str, #range_between, #range_by_whole_lines, #range_with_comments, #range_with_comments_and_lines, #range_with_surrounding_comma, #range_with_surrounding_space, #source_range |
Instance Method Details
#empty_brackets?(left_bracket_token, right_bracket_token, tokens: processed_source.tokens) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 110
def empty_brackets?(left_bracket_token, right_bracket_token, tokens: processed_source.tokens) left_index = tokens.index(left_bracket_token) right_index = tokens.index(right_bracket_token) right_index && left_index == right_index - 1 end
#empty_offense(node, range, message, command) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 104
def empty_offense(node, range, , command) add_offense(range, message: format(, command: command)) do |corrector| autocorrect(corrector, node) end end
#empty_offenses(node, left, right, message) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 94
def empty_offenses(node, left, right, ) range = range_between(left.begin_pos, right.end_pos) if offending_empty_space?(empty_config, left, right) empty_offense(node, range, , 'Use one') end return unless offending_empty_no_space?(empty_config, left, right) empty_offense(node, range, , 'Do not use') end
#extra_space?(token, side) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 68
def extra_space?(token, side) return false unless token if side == :left SINGLE_SPACE_REGEXP.match?(String(token.space_after?)) else SINGLE_SPACE_REGEXP.match?(String(token.space_before?)) end end
#no_character_between?(left_bracket_token, right_bracket_token) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 129
def no_character_between?(left_bracket_token, right_bracket_token) left_bracket_token.end_pos == right_bracket_token.begin_pos end
#no_space_offenses(node, left_token, right_token, message, start_ok: false, end_ok: false) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 38
def no_space_offenses(node, # rubocop:disable Metrics/ParameterLists left_token, right_token, , start_ok: false, end_ok: false) if extra_space?(left_token, :left) && !start_ok space_offense(node, left_token, :right, , NO_SPACE_COMMAND) end return if (!extra_space?(right_token, :right) || end_ok) || (autocorrect_with_disable_uncorrectable? && !start_ok) space_offense(node, right_token, :left, , NO_SPACE_COMMAND) end
#offending_empty_no_space?(config, left_token, right_token) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 120
def offending_empty_no_space?(config, left_token, right_token) config == 'no_space' && !no_character_between?(left_token, right_token) end
#offending_empty_space?(config, left_token, right_token) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 116
def offending_empty_space?(config, left_token, right_token) config == 'space' && !space_between?(left_token, right_token) end
#on_new_investigation (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 33
def on_new_investigation @token_table = nil super end
#reposition(src, pos, step, include_newlines: false) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 78
def reposition(src, pos, step, include_newlines: false) offset = step == -1 ? -1 : 0 pos += step while SINGLE_SPACE_REGEXP.match?(src[pos + offset]) || (include_newlines && src[pos + offset] == "\n") pos.negative? ? 0 : pos end
#side_space_range(range:, side:, include_newlines: false) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 16
def side_space_range(range:, side:, include_newlines: false) buffer = processed_source.buffer src = buffer.source begin_pos = range.begin_pos end_pos = range.end_pos if side == :left end_pos = begin_pos begin_pos = reposition(src, begin_pos, -1, include_newlines: include_newlines) end if side == :right begin_pos = end_pos end_pos = reposition(src, end_pos, 1, include_newlines: include_newlines) end Parser::Source::Range.new(buffer, begin_pos, end_pos) end
#space_between?(left_bracket_token, right_bracket_token) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 124
def space_between?(left_bracket_token, right_bracket_token) left_bracket_token.end_pos + 1 == right_bracket_token.begin_pos && processed_source.buffer.source[left_bracket_token.end_pos] == ' ' end
#space_offense(node, token, side, message, command) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 85
def space_offense(node, token, side, , command) range = side_space_range(range: token.pos, side: side) add_offense(range, message: format(, command: command)) do |corrector| autocorrect(corrector, node) unless ignored_node?(node) ignore_node(node) end end
#space_offenses(node, left_token, right_token, message, start_ok: false, end_ok: false) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 53
def space_offenses(node, # rubocop:disable Metrics/ParameterLists left_token, right_token, , start_ok: false, end_ok: false) unless extra_space?(left_token, :left) || start_ok space_offense(node, left_token, :none, , SPACE_COMMAND) end return if (extra_space?(right_token, :right) || end_ok) || (autocorrect_with_disable_uncorrectable? && !start_ok) space_offense(node, right_token, :none, , SPACE_COMMAND) end