Class: RBI::Rewriters::RemoveKnownDefinitions
- Extended by:
- T::Sig
- Defined in:
- lib/rbi/rewriters/remove_known_definitions.rb
Overview
Remove all definitions existing in the index from the current tree
Let’s create an ‘Index` from two different `Tree`s: ~~~rb tree1 = Parse.parse_string(<<~RBI)
class Foo
def foo; end
end
RBI
tree2 = Parse.parse_string(<<~RBI)
FOO = 10
RBI
index = Index.index(tree1, tree2) ~~~
We can use ‘RemoveKnownDefinitions` to remove the definitions found in the `index` from the `Tree` to clean: ~~~rb tree_to_clean = Parser.parse_string(<<~RBI)
class Foo
def foo; end
def ; end
end
FOO = 10
BAR = 42
RBI
cleaned_tree, operations = RemoveKnownDefinitions.remove(tree_to_clean, index)
assert_equal(<<~RBI, cleaned_tree)
class Foo
def ; end
end
BAR = 42
RBI
assert_equal(<<~OPERATIONS, operations.join(“n”))
Deleted ::Foo#foo at -:2:2-2-16 (duplicate from -:2:2-2:16)
Deleted ::FOO at -:5:0-5:8 (duplicate from -:1:0-1:8)
OPERATIONS ~~~
Defined Under Namespace
Classes: Operation
Instance Attribute Summary collapse
-
#operations ⇒ Object
readonly
Returns the value of attribute operations.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(index) ⇒ RemoveKnownDefinitions
constructor
A new instance of RemoveKnownDefinitions.
- #visit(node) ⇒ Object
- #visit_all(nodes) ⇒ Object
Methods inherited from Visitor
Constructor Details
#initialize(index) ⇒ RemoveKnownDefinitions
Returns a new instance of RemoveKnownDefinitions.
55 56 57 58 59 |
# File 'lib/rbi/rewriters/remove_known_definitions.rb', line 55 def initialize(index) super() @index = index @operations = T.let([], T::Array[Operation]) end |
Instance Attribute Details
#operations ⇒ Object (readonly)
Returns the value of attribute operations.
52 53 54 |
# File 'lib/rbi/rewriters/remove_known_definitions.rb', line 52 def operations @operations end |
Class Method Details
.remove(tree, index) ⇒ Object
70 71 72 73 74 |
# File 'lib/rbi/rewriters/remove_known_definitions.rb', line 70 def remove(tree, index) v = RemoveKnownDefinitions.new(index) v.visit(tree) [tree, v.operations] end |
Instance Method Details
#visit(node) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rbi/rewriters/remove_known_definitions.rb', line 83 def visit(node) return unless node case node when Scope visit_all(node.nodes) previous = previous_definition_for(node) delete_node(node, previous) if previous && can_delete_node?(node, previous) when Tree visit_all(node.nodes) when Indexable previous = previous_definition_for(node) delete_node(node, previous) if previous && can_delete_node?(node, previous) end end |
#visit_all(nodes) ⇒ Object
78 79 80 |
# File 'lib/rbi/rewriters/remove_known_definitions.rb', line 78 def visit_all(nodes) nodes.dup.each { |node| visit(node) } end |