Class: Spoom::Deadcode::Remover::NodeRemover

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/spoom/deadcode/remover.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, kind, location) ⇒ NodeRemover

Returns a new instance of NodeRemover.



36
37
38
39
40
41
42
43
# File 'lib/spoom/deadcode/remover.rb', line 36

def initialize(source, kind, location)
  @old_source = source
  @new_source = T.let(source.dup, String)
  @kind = kind
  @location = location

  @node_context = T.let(NodeFinder.find(source, location, kind), NodeContext)
end

Instance Attribute Details

#new_sourceObject (readonly)

Returns the value of attribute new_source.



33
34
35
# File 'lib/spoom/deadcode/remover.rb', line 33

def new_source
  @new_source
end

Instance Method Details

#apply_editObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/spoom/deadcode/remover.rb', line 46

def apply_edit
  sclass_context = @node_context.sclass_context
  if sclass_context
    delete_node_and_comments_and_sigs(sclass_context)
    return
  end

  node = @node_context.node
  case node
  when SyntaxTree::ClassDeclaration, SyntaxTree::ModuleDeclaration, SyntaxTree::DefNode
    delete_node_and_comments_and_sigs(@node_context)
  when SyntaxTree::Const, SyntaxTree::ConstPathField
    delete_constant_assignment(@node_context)
  when SyntaxTree::SymbolLiteral # for attr accessors
    delete_attr_accessor(@node_context)
  else
    raise Error, "Unsupported node type: #{node.class}"
  end
end