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

Use token helpers from rubocop-ast #8729

Merged
merged 1 commit into from Sep 18, 2020
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
1 change: 0 additions & 1 deletion lib/rubocop.rb
Expand Up @@ -25,7 +25,6 @@
require_relative 'rubocop/error'
require_relative 'rubocop/warning'

require_relative 'rubocop/cop/tokens_util'
require_relative 'rubocop/cop/util'
require_relative 'rubocop/cop/offense'
require_relative 'rubocop/cop/message_annotator'
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/correctors/line_break_corrector.rb
Expand Up @@ -53,7 +53,7 @@ def remove_semicolon(node, corrector)

def semicolon(node)
@semicolon ||= {}
@semicolon[node.object_id] ||= tokens(node).find(&:semicolon?)
@semicolon[node.object_id] ||= processed_source.tokens_within(node).find(&:semicolon?)
end
end
end
Expand Down
Expand Up @@ -36,8 +36,8 @@ class SpaceAroundEqualsInParameterDefault < Base
MSG = 'Surrounding space %<type>s in default value assignment.'

def on_optarg(node)
index = index_of_first_token(node)
arg, equals, value = processed_source.tokens[index, 3]
tokens = processed_source.tokens_within(node)
arg, equals, value = tokens.take(3)
check_optarg(arg, equals, value)
end

Expand Down
14 changes: 7 additions & 7 deletions lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb
Expand Up @@ -108,19 +108,19 @@ def array_brackets(node)
end

def left_array_bracket(node)
tokens(node).find(&:left_array_bracket?)
processed_source.tokens_within(node).find(&:left_array_bracket?)
end

def right_array_bracket(node)
tokens(node).reverse.find(&:right_bracket?)
processed_source.tokens_within(node).reverse.find(&:right_bracket?)
end

def empty_config
cop_config['EnforcedStyleForEmptyBrackets']
end

def next_to_newline?(node, token)
tokens(node)[index_for(node, token) + 1].line != token.line
processed_source.tokens_within(node)[index_for(node, token) + 1].line != token.line
end

def end_has_own_line?(token)
Expand All @@ -131,7 +131,7 @@ def end_has_own_line?(token)
end

def index_for(node, token)
tokens(node).index(token)
processed_source.tokens_within(node).index(token)
end

def line_and_column_for(token)
Expand All @@ -153,7 +153,7 @@ def issue_offenses(node, left, right, start_ok, end_ok)
end

def next_to_comment?(node, token)
tokens(node)[index_for(node, token) + 1].comment?
processed_source.tokens_within(node)[index_for(node, token) + 1].comment?
end

def compact_offenses(node, left, right, start_ok, end_ok)
Expand Down Expand Up @@ -184,9 +184,9 @@ def qualifies_for_compact?(node, token, side: :right)
def multi_dimensional_array?(node, token, side: :right)
i = index_for(node, token)
if side == :right
tokens(node)[i - 1].right_bracket?
processed_source.tokens_within(node)[i - 1].right_bracket?
else
tokens(node)[i + 1].left_array_bracket?
processed_source.tokens_within(node)[i + 1].left_array_bracket?
end
end

Expand Down
22 changes: 4 additions & 18 deletions lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb
Expand Up @@ -72,29 +72,15 @@ class SpaceInsideHashLiteralBraces < Base
MSG = 'Space inside %<problem>s.'

def on_hash(node)
tokens = processed_source.tokens
tokens = processed_source.tokens_within(node)
return unless tokens.first.left_brace? && tokens.last.right_curly_brace?

hash_literal_with_braces(node) do |begin_index, end_index|
check(tokens[begin_index], tokens[begin_index + 1])
return if begin_index == end_index - 1

check(tokens[end_index - 1], tokens[end_index])
end
check(tokens[0], tokens[1])
check(tokens[-2], tokens[-1]) if tokens.size > 2
end

private

def hash_literal_with_braces(node)
tokens = processed_source.tokens
begin_index = index_of_first_token(node)
return unless tokens[begin_index].left_brace?

end_index = index_of_last_token(node)
return unless tokens[end_index].right_curly_brace?

yield begin_index, end_index
end

def check(token1, token2)
# No offense if line break inside.
return if token1.line < token2.line
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/layout/space_inside_reference_brackets.rb
Expand Up @@ -66,7 +66,7 @@ class SpaceInsideReferenceBrackets < Base
def on_send(node)
return if node.multiline?

tokens = tokens(node)
tokens = processed_source.tokens_within(node)
left_token = left_ref_bracket(node, tokens)
return unless left_token

Expand Down Expand Up @@ -98,7 +98,7 @@ def autocorrect(corrector, node)
end

def reference_brackets(node)
tokens = tokens(node)
tokens = processed_source.tokens_within(node)
left = left_ref_bracket(node, tokens)
[left, closing_bracket(tokens, left)]
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/layout/space_inside_string_interpolation.rb
Expand Up @@ -54,8 +54,8 @@ def autocorrect(corrector, begin_node)
end

def delimiters(begin_node)
left = processed_source.tokens[index_of_first_token(begin_node)]
right = processed_source.tokens[index_of_last_token(begin_node)]
left = processed_source.first_token_of(begin_node)
right = processed_source.last_token_of(begin_node)
[left, right]
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/style/ternary_parentheses.rb
Expand Up @@ -211,8 +211,7 @@ def correct_unparenthesized(corrector, condition)
end

def whitespace_after?(node)
index = index_of_last_token(node)
last_token = processed_source.tokens[index]
last_token = processed_source.last_token_of(node)
last_token.space_after?
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/rubocop/cop/style/trailing_comma_in_block_args.rb
Expand Up @@ -76,12 +76,13 @@ def last_comma(node)
end

def argument_tokens(node)
pipes = tokens(node).select { |token| token.type == :tPIPE }
tokens = processed_source.tokens_within(node)
pipes = tokens.select { |token| token.type == :tPIPE }
begin_pos, end_pos = pipes.map do |pipe|
tokens(node).index(pipe)
tokens.index(pipe)
end

tokens(node)[begin_pos + 1..end_pos - 1]
tokens[begin_pos + 1..end_pos - 1]
end
end
end
Expand Down
84 changes: 0 additions & 84 deletions lib/rubocop/cop/tokens_util.rb

This file was deleted.

1 change: 0 additions & 1 deletion lib/rubocop/cop/util.rb
Expand Up @@ -5,7 +5,6 @@ module Cop
# This module contains a collection of useful utility methods.
module Util
include PathUtil
include TokensUtil

# Match literal regex characters, not including anchors, character
# classes, alternatives, groups, repetitions, references, etc
Expand Down