Class: Sorbet::Eraser::Ranges

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/sorbet/eraser.rb

Overview

This class is a YARP visitor that finds the ranges of bytes that should be erased from the source code.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ranges) ⇒ Ranges

Returns a new instance of Ranges.



29
30
31
# File 'lib/sorbet/eraser.rb', line 29

def initialize(ranges)
  @ranges = ranges
end

Instance Attribute Details

#rangesObject (readonly)

Returns the value of attribute ranges.



27
28
29
# File 'lib/sorbet/eraser.rb', line 27

def ranges
  @ranges
end

Instance Method Details

#visit_call_node(node) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sorbet/eraser.rb', line 33

def visit_call_node(node)
  case node.name
  when :abstract!, :final!, :interface!
    # abstract!
    # abstract!()
    # final!
    # final!()
    # interface!
    # interface!()
    if !node.receiver && !node.arguments && !node.block
      ranges << (node.location.start_offset...node.location.end_offset)
    end
  when :assert_type!, :bind, :cast, :let
    # T.assert_type! foo, String
    # T.assert_type!(foo, String)
    # T.bind self, String
    # T.bind(self, String)
    # T.cast foo, String
    # T.cast(foo, String)
    # T.let foo, String
    # T.let(foo, String)
    if node.receiver.is_a?(Prism::ConstantReadNode) && node.receiver.name == :T && (arguments = node.arguments&.arguments) && !node.block && arguments.length == 2
      if node.opening_loc
        ranges << (node.location.start_offset...node.opening_loc.start_offset)
        ranges << (arguments.first.location.end_offset...node.closing_loc.start_offset)
      else
        ranges << (node.location.start_offset...arguments.first.location.start_offset)
        ranges << (arguments.last.location.end_offset...node.location.end_offset)
      end
    end
  when :const, :prop
    # const :foo, String
    # const :foo, String, required: true
    # const(:foo, String)
    # const(:foo, String, required: true)
    # prop :foo, String
    # prop :foo, String, required: true
    # prop(:foo, String)
    # prop(:foo, String, required: true)
    if !node.receiver && (arguments = node.arguments) && !node.block
      arguments = arguments.arguments

      case arguments.length
      when 2
        ranges << (arguments[0].location.end_offset...arguments[1].location.end_offset)
      when 3
        ranges << (arguments[1].location.start_offset...arguments[2].location.start_offset)
      end
    end
  when :mixes_in_class_methods
    # mixes_in_class_methods Foo
    # mixes_in_class_methods(Foo)
    if !node.receiver && (arguments = node.arguments&.arguments) && !node.block && arguments.length == 1
      ranges << (node.location.start_offset...node.location.end_offset)
    end
  when :must, :reveal_type, :unsafe
    # T.must foo
    # T.must(foo)
    # T.reveal_type foo
    # T.reveal_type(foo)
    # T.unsafe foo
    # T.unsafe(foo)
    if (receiver = node.receiver).is_a?(Prism::ConstantReadNode) && receiver.name == :T && (arguments = node.arguments&.arguments) && !node.block && arguments.length == 1
      argument = arguments.first

      if node.opening_loc
        ranges << (node.location.start_offset...node.opening_loc.start_offset)
        ranges << (argument.location.end_offset...node.closing_loc.start_offset)
      else
        ranges << (node.location.start_offset...argument.location.start_offset)
        ranges << (argument.location.end_offset...node.location.end_offset)
      end
    end
  when :sig
    # sig { ... }
    # sig do ... end
    if !node.receiver && !node.arguments && node.block
      ranges << (node.location.start_offset...node.location.end_offset)
    end
  end

  super
end