/dead_code_terminator

❌ Remove dead conditional branches

Primary LanguageRubyMIT LicenseMIT

DeadCodeTerminator

ci gem codecov

This acts like webpack's DefinePlugin with minification pass. It allows to eliminate dead code statically, which can be required by regulations.

value = if ENV['FLAG']
  :then_branch
else
  value2 = unless ENV['PRODUCTION']
    :then_branch
  else
    ENV['RUNTIME'] ? :else1 : :else2
  end
end
# returns a valid ruby code string back with statically evaluated conditions
DeadCodeTerminator.strip(string, env: { "PRODUCTION" => true, "FLAG" => false })
value = 


  value2 = 


    ENV['RUNTIME'] ? :else1 : :else2

 

Note: it keeps precise code locations (including whitespaces and line-breaks). So if you have hotfix patches from upstream - they'll be applied without conflicts. Backtrace line numbers are also preserved and can point to original code.

Other examples can be found in specs

TODO

  • builtin file tree processing

License

The gem is available as open source under the terms of the MIT License.