Class: Spoom::Deadcode::Remover::NodeContext
- Inherits:
-
Object
- Object
- Spoom::Deadcode::Remover::NodeContext
- Extended by:
- T::Sig
- Defined in:
- lib/spoom/deadcode/remover.rb
Instance Attribute Summary collapse
-
#nesting ⇒ Object
Returns the value of attribute nesting.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
Instance Method Summary collapse
- #attached_comments_and_sigs ⇒ Object
- #attached_sig ⇒ Object
- #comment_for_node?(comment, node) ⇒ Boolean
-
#initialize(source, node, nesting) ⇒ NodeContext
constructor
A new instance of NodeContext.
- #next_node ⇒ Object
- #next_nodes ⇒ Object
- #node_string(node) ⇒ Object
- #parent_context ⇒ Object
- #parent_node ⇒ Object
- #previous_node ⇒ Object
- #previous_nodes ⇒ Object
- #sclass_context ⇒ Object
- #sorbet_extend_sig?(node) ⇒ Boolean
- #sorbet_signature?(node) ⇒ Boolean
Constructor Details
#initialize(source, node, nesting) ⇒ NodeContext
Returns a new instance of NodeContext.
358 359 360 361 362 |
# File 'lib/spoom/deadcode/remover.rb', line 358 def initialize(source, node, nesting) @source = source @node = node @nesting = nesting end |
Instance Attribute Details
#nesting ⇒ Object
Returns the value of attribute nesting.
355 356 357 |
# File 'lib/spoom/deadcode/remover.rb', line 355 def nesting @nesting end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
352 353 354 |
# File 'lib/spoom/deadcode/remover.rb', line 352 def node @node end |
Instance Method Details
#attached_comments_and_sigs ⇒ Object
467 468 469 470 471 472 473 474 475 476 477 |
# File 'lib/spoom/deadcode/remover.rb', line 467 def attached_comments_and_sigs nodes = T.let([], T::Array[SyntaxTree::Node]) previous_nodes.reverse_each do |prev_node| break unless comment_for_node?(prev_node, nodes.last || node) || sorbet_signature?(prev_node) nodes << prev_node end nodes.reverse end |
#attached_sig ⇒ Object
480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/spoom/deadcode/remover.rb', line 480 def attached_sig previous_nodes.reverse_each do |node| if node.is_a?(SyntaxTree::Comment) next elsif sorbet_signature?(node) return T.cast(node, SyntaxTree::MethodAddBlock) else break end end nil end |
#comment_for_node?(comment, node) ⇒ Boolean
460 461 462 463 464 |
# File 'lib/spoom/deadcode/remover.rb', line 460 def comment_for_node?(comment, node) return false unless comment.is_a?(SyntaxTree::Comment) comment.location.end_line == node.location.start_line - 1 end |
#next_node ⇒ Object
407 408 409 |
# File 'lib/spoom/deadcode/remover.rb', line 407 def next_node next_nodes.first end |
#next_nodes ⇒ Object
397 398 399 400 401 402 403 404 |
# File 'lib/spoom/deadcode/remover.rb', line 397 def next_nodes parent = parent_node index = parent.child_nodes.index(node) raise "Node #{@node} not found in nesting node #{parent}" unless index parent.child_nodes[(index + 1)..-1].reject { |node| node.is_a?(SyntaxTree::VoidStmt) } end |
#node_string(node) ⇒ Object
495 496 497 498 499 500 501 502 |
# File 'lib/spoom/deadcode/remover.rb', line 495 def node_string(node) case node when Symbol node.to_s else T.must(@source[node.location.start_char...node.location.end_char]) end end |
#parent_context ⇒ Object
373 374 375 376 377 378 379 |
# File 'lib/spoom/deadcode/remover.rb', line 373 def parent_context nesting = @nesting.dup parent = nesting.pop raise "No parent context for node #{@node}" unless parent NodeContext.new(@source, parent, nesting) end |
#parent_node ⇒ Object
365 366 367 368 369 370 |
# File 'lib/spoom/deadcode/remover.rb', line 365 def parent_node parent = @nesting.last raise "No parent for node #{node}" unless parent parent end |
#previous_node ⇒ Object
392 393 394 |
# File 'lib/spoom/deadcode/remover.rb', line 392 def previous_node previous_nodes.last end |
#previous_nodes ⇒ Object
382 383 384 385 386 387 388 389 |
# File 'lib/spoom/deadcode/remover.rb', line 382 def previous_nodes parent = parent_node index = parent.child_nodes.index(@node) raise "Node #{@node} not found in parent #{parent}" unless index parent.child_nodes[0...index].reject { |child| child.is_a?(SyntaxTree::VoidStmt) } end |
#sclass_context ⇒ Object
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/spoom/deadcode/remover.rb', line 412 def sclass_context sclass = T.let(nil, T.nilable(SyntaxTree::SClass)) nesting = @nesting.dup until nesting.empty? || sclass node = nesting.pop next unless node.is_a?(SyntaxTree::SClass) sclass = node end return unless sclass.is_a?(SyntaxTree::SClass) nodes = sclass.bodystmt.statements.body.reject do |node| node.is_a?(SyntaxTree::VoidStmt) || node.is_a?(SyntaxTree::Comment) || sorbet_signature?(node) || sorbet_extend_sig?(node) end if nodes.size <= 1 return NodeContext.new(@source, sclass, nesting) end nil end |
#sorbet_extend_sig?(node) ⇒ Boolean
451 452 453 454 455 456 457 |
# File 'lib/spoom/deadcode/remover.rb', line 451 def sorbet_extend_sig?(node) return false unless node.is_a?(SyntaxTree::Command) return false unless node_string(node.) == "extend" return false unless node.arguments.parts.size == 1 node_string(T.must(node.arguments.parts.first)) == "T::Sig" end |
#sorbet_signature?(node) ⇒ Boolean
438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/spoom/deadcode/remover.rb', line 438 def sorbet_signature?(node) return false unless node.is_a?(SyntaxTree::MethodAddBlock) call = node.call return false unless call.is_a?(SyntaxTree::CallNode) ident = call. return false unless ident.is_a?(SyntaxTree::Ident) ident.value == "sig" end |