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.3.0"
Class Method Summary
collapse
Class Method Details
.on_node(node, &block) ⇒ Object
32
33
34
35
36
|
# File 'lib/dead_code_terminator.rb', line 32
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
24
25
26
27
28
29
30
|
# File 'lib/dead_code_terminator.rb', line 24
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
16
17
18
19
20
21
22
|
# File 'lib/dead_code_terminator.rb', line 16
def self.strip(io, env: {})
replaces = on_node(Unparser.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
|