Class: Spoom::Deadcode::Remover::NodeFinder
- Inherits:
-
SyntaxTree::Visitor
- Object
- SyntaxTree::Visitor
- Spoom::Deadcode::Remover::NodeFinder
- Extended by:
- T::Sig
- Defined in:
- lib/spoom/deadcode/remover.rb
Instance Attribute Summary collapse
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#nodes_nesting ⇒ Object
Returns the value of attribute nodes_nesting.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(location) ⇒ NodeFinder
constructor
A new instance of NodeFinder.
- #visit(node) ⇒ Object
Constructor Details
#initialize(location) ⇒ NodeFinder
Returns a new instance of NodeFinder.
554 555 556 557 558 559 |
# File 'lib/spoom/deadcode/remover.rb', line 554 def initialize(location) super() @location = location @node = T.let(nil, T.nilable(SyntaxTree::Node)) @nodes_nesting = T.let([], T::Array[SyntaxTree::Node]) end |
Instance Attribute Details
#node ⇒ Object (readonly)
Returns the value of attribute node.
548 549 550 |
# File 'lib/spoom/deadcode/remover.rb', line 548 def node @node end |
#nodes_nesting ⇒ Object
Returns the value of attribute nodes_nesting.
551 552 553 |
# File 'lib/spoom/deadcode/remover.rb', line 551 def nodes_nesting @nodes_nesting end |
Class Method Details
.find(source, location, kind) ⇒ Object
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
# File 'lib/spoom/deadcode/remover.rb', line 512 def find(source, location, kind) tree = SyntaxTree.parse(source) visitor = new(location) visitor.visit(tree) node = visitor.node unless node raise Error, "Can't find node at #{location}" end unless node_match_kind?(node, kind) raise Error, "Can't find node at #{location}, expected #{kind} but got #{node.class}" end NodeContext.new(source, node, visitor.nodes_nesting) end |
.node_match_kind?(node, kind) ⇒ Boolean
531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/spoom/deadcode/remover.rb', line 531 def node_match_kind?(node, kind) case kind when Definition::Kind::AttrReader, Definition::Kind::AttrWriter node.is_a?(SyntaxTree::SymbolLiteral) when Definition::Kind::Class node.is_a?(SyntaxTree::ClassDeclaration) when Definition::Kind::Constant node.is_a?(SyntaxTree::Const) || node.is_a?(SyntaxTree::ConstPathField) when Definition::Kind::Method node.is_a?(SyntaxTree::DefNode) when Definition::Kind::Module node.is_a?(SyntaxTree::ModuleDeclaration) end end |
Instance Method Details
#visit(node) ⇒ Object
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
# File 'lib/spoom/deadcode/remover.rb', line 562 def visit(node) return unless node location = location_from_node(node) if location == @location # We found the node we're looking for at `@location` @node = node # There may be a more precise child inside the node that also matches `@location`, let's visit them @nodes_nesting << node super(node) @nodes_nesting.pop if @nodes_nesting.last == @node elsif location.include?(@location) # The node we're looking for is inside `node`, let's visit it @nodes_nesting << node super(node) end end |