Class: RuboCop::Cop::RBS::Style::RedundantParentheses::ParenChecker

Inherits:
Object
  • Object
show all
Includes:
RangeHelp, BeforeTokenIfLparen
Defined in:
lib/rubocop/cop/rbs/style/redundant_parentheses.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BeforeTokenIfLparen

#before_token_if_lparen

Constructor Details

#initialize(processed_source:, base:, tokens:, type:, skip:, cop:) ⇒ ParenChecker

Returns a new instance of ParenChecker.



51
52
53
54
55
56
57
58
# File 'lib/rubocop/cop/rbs/style/redundant_parentheses.rb', line 51

def initialize(processed_source:, base:, tokens:, type:, skip:, cop:)
  @processed_source = processed_source
  @base = base
  @tokens = tokens
  @type = type
  @skip = skip
  @cop = cop
end

Instance Attribute Details

#processed_sourceObject (readonly)

Returns the value of attribute processed_source.



49
50
51
# File 'lib/rubocop/cop/rbs/style/redundant_parentheses.rb', line 49

def processed_source
  @processed_source
end

Instance Method Details

#checkObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rubocop/cop/rbs/style/redundant_parentheses.rb', line 60

def check
  @cop.on_type([::RBS::Types::Proc], @type) do |proc_type|
    before_token_if_lparen(@tokens, @base, proc_type.type) do |b|
      @skip << (b.location.start_pos + @base)
    end
    if proc_type.block
      before_token_if_lparen(@tokens, @base, proc_type.block.type) do |b|
        @skip << (b.location.start_pos + @base)
      end
    end
  end

  excludes = [
    ::RBS::Types::Union,
    ::RBS::Types::Intersection,
    ::RBS::Types::Proc,
  ]
  @cop.on_not_type(excludes, @type) do |type|
    case type
    when ::RBS::Types::Optional
      case type.type
      when ::RBS::Types::Literal
        case type.type.literal
        when Symbol
          # Skip optional with symbol literal (e.g. `(:sym)?`)
          @skip << type.location.start_pos
        end
      end
    end
    check_parentheses(type)
  end
end

#check_parentheses(type) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rubocop/cop/rbs/style/redundant_parentheses.rb', line 93

def check_parentheses(type)
  type_token_start_index = @tokens.bsearch_index do |token|
    (token.location.start_pos + @base) >= type.location.start_pos
  end or raise
  before_token = @tokens[type_token_start_index - 1]
  return if @skip.include?(before_token.location.start_pos + @base)
  return if before_token&.type != :pLPAREN

  type_token_end_index = @tokens.bsearch_index do |token|
    (token.location.start_pos + @base) >= type.location.end_pos
  end or raise
  after_token = @tokens[type_token_end_index]
  return if after_token&.type != :pRPAREN

  range = range_between(before_token.location.start_pos + @base, after_token.location.end_pos + @base)
  @cop.add_offense(range, message: "Don't use parentheses around simple type.") do |corrector|
    corrector.remove(range_between(before_token.location.start_pos + @base,
                                   before_token.location.end_pos + @base))
    corrector.remove(range_between(after_token.location.start_pos + @base,
                                   after_token.location.end_pos + @base))
  end
end