dead_code_terminator 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 35a0026a3312154a1c97d73ae012b264a34c32c9d338737aa22ac6922f8635ad
4
+ data.tar.gz: '079e266498ab09e5a82568a189418befc50890ef7461eecb2b1bce54f5cc7a75'
5
+ SHA512:
6
+ metadata.gz: fc50239484be93bda9bcb7d015cece2b06f5a4dcc091861b304ead379a0f4253de8195cb1581fe630df122ec8f4c196f311580d636ad8d91691c47c84585622d
7
+ data.tar.gz: 3ee1ca75f87a1402e1b6e14d7f9d390300759340aef3445d5ed8ab9dd39259056963c5fbe815bebe30413f60ca79a96d08c3cbdc762b8111c89ff83ace3f3d63
@@ -0,0 +1,16 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.0
14
+ bundler-cache: true
15
+ - name: Run the default task
16
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,37 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ NewCops: enable
4
+
5
+ Style/Documentation:
6
+ Enabled: false
7
+
8
+ Style/StringLiterals:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Style/StringLiteralsInInterpolation:
13
+ Enabled: true
14
+ EnforcedStyle: double_quotes
15
+
16
+ Layout/LineLength:
17
+ Max: 120
18
+
19
+ Layout/TrailingWhitespace:
20
+ Exclude:
21
+ - spec/**/*rb
22
+
23
+ Lint/BooleanSymbol:
24
+ Enabled: false
25
+
26
+ # Metrics/AbcSize:
27
+ # Enabled: false
28
+
29
+ # Metrics/MethodLength:
30
+ # Enabled: false
31
+
32
+ Metrics/BlockLength:
33
+ Exclude:
34
+ - spec/**/*rb
35
+
36
+ # Lint/UnusedMethodArgument:
37
+ # Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in dead_code_terminator.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
13
+ gem "rubocop-rake", "~> 0.5"
14
+ gem "rubocop-rspec", "~> 2.3"
15
+
16
+ gem "pry-byebug" unless ENV["CI"]
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Vlad Bokov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # DeadCodeTerminator
2
+
3
+ This acts like [webpack's DefinePlugin](https://webpack.js.org/plugins/define-plugin/) with minification pass. It allows to eliminate dead code statically, which can be required by regulations.
4
+
5
+ ```ruby
6
+ if ENV['PRODUCTION']
7
+ :then_branch
8
+ else
9
+ :else_branch
10
+ end
11
+ ```
12
+
13
+ ```ruby
14
+
15
+ :then_branch
16
+
17
+
18
+
19
+ ```
20
+
21
+ Note: it keeps *precise* code locations (including whitespaces and line-breaks).
22
+ So if you have hotfix patches from upstream - they'll be applied without conflicts.
23
+
24
+ ## TODO
25
+
26
+ - `unless`
27
+ - `x ? a : b`
28
+ - builtin file tree processing
29
+ ## License
30
+
31
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "dead_code_terminator"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/dead_code_terminator/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "dead_code_terminator"
7
+ spec.version = DeadCodeTerminator::VERSION
8
+ spec.authors = ["Vlad Bokov"]
9
+ spec.email = ["vlad@razum2um.me"]
10
+
11
+ spec.summary = "Remove dead conditional branches"
12
+ spec.description = "Statically remove dead conditional branches, acts like webpack's DefinePlugin and minifier"
13
+ spec.homepage = "https://github.com/razum2um/dead_code_terminator"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.5.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/razum2um/dead_code_terminator"
19
+ spec.metadata["changelog_uri"] = "https://github.com/razum2um/dead_code_terminator"
20
+
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "parser", ">= 2.5.0.0"
29
+ spec.add_dependency "unparser", "~> 0.6"
30
+ end
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "dead_code_terminator"
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "parser/current"
4
+ require "unparser"
5
+ require_relative "dead_code_terminator/version"
6
+ require_relative "dead_code_terminator/ast"
7
+ require_relative "dead_code_terminator/cond"
8
+ require_relative "dead_code_terminator/cond/base"
9
+ require_relative "dead_code_terminator/cond/literal"
10
+ require_relative "dead_code_terminator/cond/env_index"
11
+ require_relative "dead_code_terminator/cond/env_fetch"
12
+
13
+ module DeadCodeTerminator
14
+ class Error < StandardError; end
15
+
16
+ def self.strip(io, env: {})
17
+ Ast.new(env: env, ast: Unparser.parse(io)).process
18
+ end
19
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeadCodeTerminator
4
+ class Ast
5
+ attr_reader :env, :ast
6
+
7
+ def initialize(env:, ast:)
8
+ @env = env
9
+ @ast = ast
10
+ end
11
+
12
+ def process
13
+ erase_before!
14
+ erase_after!
15
+ rewriter.process
16
+ end
17
+
18
+ private
19
+
20
+ def erase_before!
21
+ rewriter.replace(range(end_pos, (total_end - end_pos)), "\n" * (total_lines - to_line))
22
+ end
23
+
24
+ def erase_after!
25
+ rewriter.replace(range(0, begin_pos), "\n" * (from_line - 1))
26
+ end
27
+
28
+ def begin_pos
29
+ new_ast.loc.expression.begin_pos
30
+ end
31
+
32
+ def end_pos
33
+ new_ast.loc.expression.end_pos
34
+ end
35
+
36
+ def total_end
37
+ ast.loc.expression.end_pos
38
+ end
39
+
40
+ def from_line
41
+ new_ast.loc.expression.source_buffer.line_for_position(begin_pos)
42
+ end
43
+
44
+ def to_line
45
+ new_ast.loc.expression.source_buffer.line_for_position(end_pos)
46
+ end
47
+
48
+ def total_lines
49
+ ast.loc.last_line
50
+ end
51
+
52
+ def rewriter
53
+ @rewriter ||= Parser::Source::TreeRewriter.new(buf)
54
+ end
55
+
56
+ def range(from, len = nil)
57
+ unless len
58
+ len = from.end - from.begin
59
+ from = from.begin
60
+ end
61
+ Parser::Source::Range.new(buf, from, from + len)
62
+ end
63
+
64
+ def buf
65
+ @buf ||= ast.loc.expression.source_buffer
66
+ end
67
+
68
+ def new_ast
69
+ @new_ast ||= case ast.type
70
+ when :if then if_ast
71
+ else ast
72
+ end
73
+ end
74
+
75
+ def if_ast
76
+ cond, then_branch, else_branch = ast.children
77
+
78
+ Cond.nodes.each do |klass|
79
+ if (value = klass.new(env: env, ast: cond).value)
80
+ return then_branch if value == Cond::Base::THEN
81
+ return else_branch if value == Cond::Base::ELSE
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeadCodeTerminator
4
+ module Cond
5
+ @nodes = []
6
+
7
+ def self.register(node)
8
+ @nodes << node
9
+ end
10
+
11
+ def self.nodes
12
+ @nodes
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeadCodeTerminator
4
+ module Cond
5
+ class Base
6
+ THEN = :then
7
+ ELSE = :else
8
+
9
+ attr_reader :env, :ast
10
+
11
+ def initialize(env:, ast:)
12
+ @env = env
13
+ @ast = ast
14
+ end
15
+
16
+ def value; end
17
+
18
+ private
19
+
20
+ def s(type, *children)
21
+ Parser::AST::Node.new(type, children)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeadCodeTerminator
4
+ module Cond
5
+ # ENV.fetch('PRODUCTION')
6
+ # s(:send,
7
+ # s(:const, nil, :ENV), :fetch,
8
+ # s(:str, "PRODUCTION"))
9
+ class EnvFetch < Base
10
+ def value
11
+ return if ast.type != :send
12
+
13
+ receiver, method, *args = ast.children
14
+ return unless receiver == s(:const, nil, :ENV) && method == :fetch
15
+
16
+ return unless (matched_env_key = given_env_key(args[0]))
17
+
18
+ env[matched_env_key] ? THEN : ELSE
19
+ end
20
+
21
+ private
22
+
23
+ def given_env_key(ast)
24
+ env.keys.detect { |key| ast == s(:str, key) }
25
+ end
26
+ end
27
+
28
+ register EnvFetch
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeadCodeTerminator
4
+ module Cond
5
+ # ENV['PRODUCTION']
6
+ # s(:index,
7
+ # s(:const, nil, :ENV),
8
+ # s(:str, "PRODUCTION"))
9
+ class EnvIndex < Base
10
+ def value
11
+ return if ast.type != :index
12
+
13
+ hash, key = ast.children
14
+ return if hash != s(:const, nil, :ENV)
15
+
16
+ return unless (matched_env_key = given_env_key(key))
17
+
18
+ env[matched_env_key] ? THEN : ELSE
19
+ end
20
+
21
+ private
22
+
23
+ def given_env_key(ast)
24
+ env.keys.detect { |key| ast == s(:str, key) }
25
+ end
26
+ end
27
+
28
+ register EnvIndex
29
+ end
30
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeadCodeTerminator
4
+ module Cond
5
+ class Literal < Base
6
+ def value
7
+ return THEN if ast.type == :true
8
+ return ELSE if ast.type == :false
9
+ end
10
+ end
11
+
12
+ register Literal
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeadCodeTerminator
4
+ class IfEnv < PlainIf
5
+ def self.match?(ast); end
6
+
7
+ def then_branch; end
8
+
9
+ def else_branch; end
10
+
11
+ def self.nodes
12
+ ancestors
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeadCodeTerminator
4
+ class PlainIf < Node
5
+ def self.match?(ast); end
6
+
7
+ def then_branch; end
8
+
9
+ def else_branch; end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeadCodeTerminator
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dead_code_terminator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vlad Bokov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: parser
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.5.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.5.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: unparser
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.6'
41
+ description: Statically remove dead conditional branches, acts like webpack's DefinePlugin
42
+ and minifier
43
+ email:
44
+ - vlad@razum2um.me
45
+ executables:
46
+ - dead_code_terminator
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".github/workflows/main.yml"
51
+ - ".gitignore"
52
+ - ".rspec"
53
+ - ".rubocop.yml"
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - bin/console
59
+ - bin/setup
60
+ - dead_code_terminator.gemspec
61
+ - exe/dead_code_terminator
62
+ - lib/dead_code_terminator.rb
63
+ - lib/dead_code_terminator/ast.rb
64
+ - lib/dead_code_terminator/cond.rb
65
+ - lib/dead_code_terminator/cond/base.rb
66
+ - lib/dead_code_terminator/cond/env_fetch.rb
67
+ - lib/dead_code_terminator/cond/env_index.rb
68
+ - lib/dead_code_terminator/cond/literal.rb
69
+ - lib/dead_code_terminator/if_env.rb
70
+ - lib/dead_code_terminator/plain_if.rb
71
+ - lib/dead_code_terminator/version.rb
72
+ homepage: https://github.com/razum2um/dead_code_terminator
73
+ licenses:
74
+ - MIT
75
+ metadata:
76
+ homepage_uri: https://github.com/razum2um/dead_code_terminator
77
+ source_code_uri: https://github.com/razum2um/dead_code_terminator
78
+ changelog_uri: https://github.com/razum2um/dead_code_terminator
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 2.5.0
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubygems_version: 3.2.3
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Remove dead conditional branches
98
+ test_files: []