Module: DeadCodeTerminator

Defined in:
lib/dead_code_terminator.rb,
lib/dead_code_terminator/ast.rb,
lib/dead_code_terminator/cond.rb,
lib/dead_code_terminator/if_env.rb,
lib/dead_code_terminator/version.rb,
lib/dead_code_terminator/plain_if.rb,
lib/dead_code_terminator/cond/base.rb,
lib/dead_code_terminator/cond/literal.rb,
lib/dead_code_terminator/cond/env_fetch.rb,
lib/dead_code_terminator/cond/env_index.rb

Defined Under Namespace

Modules: Cond Classes: Ast, Error, IfEnv, PlainIf

Constant Summary collapse

VERSION =
"0.5.0"

Class Method Summary collapse

Class Method Details

.on_node(node, &block) ⇒ Object



31
32
33
34
35
# File 'lib/dead_code_terminator.rb', line 31

def self.on_node(node, &block)
  Array(yield(node)) + node.children.flat_map do |elem|
    on_node(elem, &block) if elem.is_a?(Parser::AST::Node)
  end.compact
end

.rewrite(buffer, replaces) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/dead_code_terminator.rb', line 23

def self.rewrite(buffer, replaces)
  Parser::Source::TreeRewriter.new(buffer).tap do |rewriter|
    replaces.each do |replace|
      rewriter.replace(*replace)
    end
  end.process
end

.strip(io, env: {}) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/dead_code_terminator.rb', line 15

def self.strip(io, env: {})
  replaces = on_node(Parser::CurrentRuby.parse(io)) do |ast|
    Ast.new(env: env, ast: ast).process if ast.type == :if
  end

  rewrite(Parser::Source::Buffer.new("buffer-or-filename", source: io), replaces)
end