Class: RuboCop::Cop::RBS::Layout::SpaceAroundBraces

Inherits:
RBS::CopBase
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rbs/layout/space_around_braces.rb

Overview

Checks for missing spaces around braces in method definitions.

Examples:

# bad
def bar: (){() -> void}-> void

# good
def bar: () { () -> void } -> void

Instance Attribute Summary

Attributes inherited from RBS::CopBase

#processed_rbs_source

Instance Method Summary collapse

Methods inherited from RBS::CopBase

documentation_url, #investigation_rbs, #location_to_range, #on_new_investigation, #on_other_file, #on_rbs_attribute, #on_rbs_class, #on_rbs_constant, #on_rbs_global, #on_rbs_interface, #on_rbs_module, #on_rbs_new_investigation, #on_rbs_parsing_error, #on_rbs_private, #on_rbs_public, #on_rbs_type_alias, #on_rbs_var, #parse_rbs, #rbs_buffer, #tokenize, #walk

Methods included from RBS::OnTypeHelper

#on_not_type, #on_type

Instance Method Details

#check_around_space(base_loc, before, token, after) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubocop/cop/rbs/layout/space_around_braces.rb', line 44

def check_around_space(base_loc, before, token, after)
  return unless before.nil? || before.location.end_line == token.location.start_line
  return unless token.location.end_line == after.location.start_line

  if before && (before.location.end_pos + 1 != token.location.start_pos)
    brace = range_between(
      base_loc.start_pos + token.location.start_pos,
      base_loc.start_pos + token.location.end_pos,
    )
    add_offense(brace, message: "Use one space before `#{token.location.source}`.") do |corrector|
      range = range_between(
        base_loc.start_pos + before.location.end_pos,
        base_loc.start_pos + token.location.start_pos
      )
      corrector.replace(range, ' ')
    end
  end

  if token.location.end_pos + 1 != after.location.start_pos
    brace = range_between(
      base_loc.start_pos + token.location.start_pos,
      base_loc.start_pos + token.location.end_pos,
    )
    add_offense(brace, message: "Use one space after `#{token.location.source}`.") do |corrector|
      range = range_between(
        base_loc.start_pos + token.location.end_pos,
        base_loc.start_pos + after.location.start_pos
      )
      corrector.replace(range, ' ')
    end
  end
end

#on_rbs_def(decl) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/rbs/layout/space_around_braces.rb', line 20

def on_rbs_def(decl)
  paren_level = 0
  tokens = ::RBS::Parser.lex(decl.location.source).value.reject { |t| t.type == :tTRIVIA }
  ([nil] + tokens).each_cons(3) do |before, token, after|
    case token.type
    when :pLPAREN # '('
      paren_level += 1
    when :pRPAREN # ')'
      paren_level -= 1
    when :pLBRACE # '{'
      next unless before&.type != :pQUESTION # '?'
      next unless (after.type == :pLPAREN || after.type == :pARROW)
      next unless paren_level == 0

      check_around_space(decl.location, before, token, after)
    when :pRBRACE # '}'
      next unless after.type == :pARROW
      next unless paren_level == 0

      check_around_space(decl.location, before, token, after)
    end
  end
end