minifyrb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/ISSUE_TEMPLATE/bug_report.md +39 -0
- data/.github/workflows/test.yml +35 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.rubocop.yml +51 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +21 -0
- data/README.md +97 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/lex +11 -0
- data/bin/setup +8 -0
- data/exe/minifyrb +47 -0
- data/lib/minifyrb/minifier.rb +193 -0
- data/lib/minifyrb/rake_task.rb +38 -0
- data/lib/minifyrb/version.rb +5 -0
- data/lib/minifyrb.rb +4 -0
- data/minifyrb.gemspec +32 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1a44790e72286a5725a7260f5fb3fee2124e5d56970539b6a2a1ff9a2ae2f67e
|
4
|
+
data.tar.gz: f6a4e1d3f0462e380ed55613fb02b72d10fdcc7ba292195b8065fc33993b53b5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2185f292c456b5edd03a0b07c055dd56d7ed2698599c315eba54d6c45d0715ef89f61995938e17a33cb8ca5c3c6bc06b22f9568b7654d93bd792ab28968585d0
|
7
|
+
data.tar.gz: a338db6cbc0ac8d124eb0f92202966603ea5736362664088d8317dd7202fa24f24ef5de0b7aab46e82977b678118550e6e9dd50fc7c3ec882f68cb6ce09fca48
|
@@ -0,0 +1,39 @@
|
|
1
|
+
---
|
2
|
+
name: Bug Report
|
3
|
+
about: Report an issue with Minify Ruby you've discovered.
|
4
|
+
---
|
5
|
+
|
6
|
+
*Be clear, concise and precise in your description of the problem.
|
7
|
+
Open an issue with a descriptive title and a summary in grammatically correct,
|
8
|
+
complete sentences.*
|
9
|
+
|
10
|
+
*Use the template below when reporting bugs. Please, make sure that
|
11
|
+
you're running the latest stable Minify Ruby and that the problem you're reporting
|
12
|
+
hasn't been reported (and potentially fixed) already.*
|
13
|
+
|
14
|
+
*Before filing the ticket you should replace all text above the horizontal
|
15
|
+
rule with your own words.*
|
16
|
+
|
17
|
+
--------
|
18
|
+
|
19
|
+
## Expected behavior
|
20
|
+
|
21
|
+
Describe here how you expected RuboCop to behave in this particular situation.
|
22
|
+
|
23
|
+
## Actual behavior
|
24
|
+
|
25
|
+
Describe here what actually happened.
|
26
|
+
|
27
|
+
## Steps to reproduce the problem
|
28
|
+
|
29
|
+
This is extremely important! Providing us with a reliable way to reproduce
|
30
|
+
a problem will expedite its solution.
|
31
|
+
|
32
|
+
## RuboCop version
|
33
|
+
|
34
|
+
Include the output of `minifyrb -v` or `bundle exec minifyrb -v` if using Bundler.
|
35
|
+
|
36
|
+
```console
|
37
|
+
$ [bundle exec] minifyrb -v
|
38
|
+
0.1.0
|
39
|
+
```
|
@@ -0,0 +1,35 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
pull_request:
|
8
|
+
workflow_dispatch:
|
9
|
+
|
10
|
+
concurrency:
|
11
|
+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
|
12
|
+
cancel-in-progress: true
|
13
|
+
|
14
|
+
jobs:
|
15
|
+
main:
|
16
|
+
name: Ruby ${{ matrix.ruby }}
|
17
|
+
runs-on: ubuntu-latest
|
18
|
+
strategy:
|
19
|
+
fail-fast: false
|
20
|
+
matrix:
|
21
|
+
os: [ubuntu]
|
22
|
+
ruby: ['3.3', 'head']
|
23
|
+
|
24
|
+
steps:
|
25
|
+
- name: checkout
|
26
|
+
uses: actions/checkout@v4
|
27
|
+
- name: set up Ruby
|
28
|
+
uses: ruby/setup-ruby@v1
|
29
|
+
with:
|
30
|
+
ruby-version: ${{ matrix.ruby }}
|
31
|
+
bundler-cache: true
|
32
|
+
- name: lint
|
33
|
+
run: bundle exec rake rubocop
|
34
|
+
- name: spec
|
35
|
+
run: bundle exec rake spec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# The behavior of RuboCop can be controlled via the .rubocop.yml
|
2
|
+
# configuration file. It makes it possible to enable/disable
|
3
|
+
# certain cops (checks) and to alter their behavior if they accept
|
4
|
+
# any parameters. The file can be placed either in your home
|
5
|
+
# directory or in some project directory.
|
6
|
+
#
|
7
|
+
# RuboCop will start looking for the configuration file in the directory
|
8
|
+
# where the inspected file is and continue its way up to the root directory.
|
9
|
+
#
|
10
|
+
# See https://docs.rubocop.org/rubocop/configuration
|
11
|
+
require:
|
12
|
+
- rubocop-rspec
|
13
|
+
|
14
|
+
AllCops:
|
15
|
+
TargetRubyVersion: 3.3
|
16
|
+
NewCops: enable
|
17
|
+
ParserEngine: parser_prism
|
18
|
+
SuggestExtensions: false
|
19
|
+
|
20
|
+
Layout/EndAlignment:
|
21
|
+
EnforcedStyleAlignWith: variable
|
22
|
+
|
23
|
+
Layout/LineLength:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Lint/AmbiguousOperatorPrecedence:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Metrics:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Style/BlockDelimiters:
|
33
|
+
EnforcedStyle: semantic
|
34
|
+
|
35
|
+
Style/Documentation:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
Style/IfUnlessModifier:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
Style/MutableConstant:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
Style/PercentLiteralDelimiters:
|
45
|
+
PreferredDelimiters:
|
46
|
+
default: ()
|
47
|
+
'%i': '()'
|
48
|
+
'%I': '()'
|
49
|
+
'%r': '||'
|
50
|
+
'%w': '()'
|
51
|
+
'%W': '()'
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Koichi ITO
|
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,97 @@
|
|
1
|
+
# Minify Ruby
|
2
|
+
|
3
|
+
[![CI](https://github.com/koic/minifyrb/actions/workflows/test.yml/badge.svg)](https://github.com/koic/minifyrb/actions/workflows/test.yml)
|
4
|
+
|
5
|
+
A minifier of Ruby files.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'minifyrb'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
```console
|
18
|
+
$ bundle install
|
19
|
+
```
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
```console
|
24
|
+
$ gem install minifyrb
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
For execution from the command line, use `minifyrb` command:
|
30
|
+
|
31
|
+
```console
|
32
|
+
$ cat path/to/example.rb
|
33
|
+
def foo
|
34
|
+
bar(arg, **options) do
|
35
|
+
baz.qux
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
$ minifyrb path/to/example.rb
|
40
|
+
def foo;bar(arg,**options) do baz.qux;end;end
|
41
|
+
```
|
42
|
+
|
43
|
+
You can check the command line options with `-h` or `--help`:
|
44
|
+
|
45
|
+
```console
|
46
|
+
$ minifyrb -h
|
47
|
+
Usage: minifyrb [options] [file1, file2, ...]
|
48
|
+
-v, --version Output the version number.
|
49
|
+
-o, --output <file> Output file (default STDOUT).
|
50
|
+
```
|
51
|
+
|
52
|
+
### Rake Task
|
53
|
+
|
54
|
+
You can define a Rake task for Minify. Please write the following code in your Rakefile:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
# Rakefile
|
58
|
+
require 'minifyrb/rake_task'
|
59
|
+
Minifyrb::RakeTask.new
|
60
|
+
```
|
61
|
+
|
62
|
+
The task `rake minifyrb` will be defined:
|
63
|
+
|
64
|
+
```console
|
65
|
+
$ bundle exec rake -T
|
66
|
+
rake minifyrb # Minify Ruby files in the current directory and subdirectories
|
67
|
+
```
|
68
|
+
|
69
|
+
### Ruby API
|
70
|
+
|
71
|
+
From Ruby code, use `Minifyrb::Minifier#minify` API:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
require 'minifyrb'
|
75
|
+
|
76
|
+
source = <<~'RUBY'
|
77
|
+
def say(name)
|
78
|
+
puts "Hello, #{name}!"
|
79
|
+
end
|
80
|
+
RUBY
|
81
|
+
|
82
|
+
Minifyrb::Minifier.new(source).minify # => "def say(name)puts\"Hello, \#{name}!\";end\n"
|
83
|
+
```
|
84
|
+
|
85
|
+
## Development
|
86
|
+
|
87
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
88
|
+
|
89
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/koic/minifyrb.
|
94
|
+
|
95
|
+
## License
|
96
|
+
|
97
|
+
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
|
+
require 'rubocop/rake_task'
|
6
|
+
require 'minifyrb/rake_task'
|
7
|
+
|
8
|
+
RSpec::Core::RakeTask.new(:spec)
|
9
|
+
RuboCop::RakeTask.new
|
10
|
+
Minifyrb::RakeTask.new
|
11
|
+
|
12
|
+
task default: %i(rubocop spec)
|
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 'minifyrb'
|
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/lex
ADDED
data/bin/setup
ADDED
data/exe/minifyrb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift("#{__dir__}/../lib")
|
5
|
+
|
6
|
+
require 'optparse'
|
7
|
+
require 'minifyrb'
|
8
|
+
|
9
|
+
params = {}
|
10
|
+
|
11
|
+
OptionParser.new do |opt|
|
12
|
+
opt.banner = 'Usage: minifyrb [options] [file1, file2, ...]'
|
13
|
+
|
14
|
+
opt.on('-v', '--version', 'Output the version number.') do
|
15
|
+
params[:version] = true
|
16
|
+
end
|
17
|
+
|
18
|
+
opt.on('-o', '--output <file>', 'Output file (default STDOUT).') do |filepath|
|
19
|
+
params[:output] = filepath
|
20
|
+
end
|
21
|
+
|
22
|
+
opt.parse!
|
23
|
+
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
|
24
|
+
puts opt.help
|
25
|
+
exit 1
|
26
|
+
end
|
27
|
+
|
28
|
+
if params[:version]
|
29
|
+
puts Minifyrb::VERSION
|
30
|
+
exit
|
31
|
+
end
|
32
|
+
|
33
|
+
filepaths = ARGV
|
34
|
+
|
35
|
+
minified_rubies = if filepaths.empty?
|
36
|
+
Minifyrb::Minifier.new($stdin.read).minify
|
37
|
+
else
|
38
|
+
filepaths.map { |filepath|
|
39
|
+
Minifyrb::Minifier.new(File.read(filepath), filepath:).minify
|
40
|
+
}.join
|
41
|
+
end
|
42
|
+
|
43
|
+
if (filepath = params[:output])
|
44
|
+
File.write(filepath, minified_rubies)
|
45
|
+
else
|
46
|
+
puts minified_rubies
|
47
|
+
end
|
@@ -0,0 +1,193 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'prism'
|
4
|
+
|
5
|
+
module Minifyrb
|
6
|
+
class Minifier
|
7
|
+
AFTER_SPACE_REQUIRED_KEYWORDS = %i(
|
8
|
+
KEYWORD_ALIAS KEYWORD_AND KEYWORD_BEGIN KEYWORD_BREAK KEYWORD_CASE KEYWORD_CLASS KEYWORD_DEF KEYWORD_DO KEYWORD_ELSE
|
9
|
+
KEYWORD_ELSIF KEYWORD_ENSURE KEYWORD_FOR KEYWORD_IF KEYWORD_IF_MODIFIER KEYWORD_IN KEYWORD_MODULE KEYWORD_NEXT
|
10
|
+
KEYWORD_NOT KEYWORD_OR KEYWORD_REDO KEYWORD_RESCUE KEYWORD_RESCUE_MODIFIER KEYWORD_RETURN KEYWORD_SUPER KEYWORD_THEN
|
11
|
+
KEYWORD_UNDEF KEYWORD_UNLESS KEYWORD_UNLESS_MODIFIER KEYWORD_UNTIL KEYWORD_UNTIL_MODIFIER KEYWORD_WHEN KEYWORD_WHILE
|
12
|
+
KEYWORD_WHILE_MODIFIER KEYWORD_YIELD
|
13
|
+
)
|
14
|
+
BEFORE_SPACE_REQUIRED_KEYWORDS = %i(
|
15
|
+
KEYWORD_AND KEYWORD_DO KEYWORD_IF_MODIFIER KEYWORD_IN KEYWORD_OR KEYWORD_RESCUE_MODIFIER KEYWORD_THEN
|
16
|
+
KEYWORD_UNLESS_MODIFIER KEYWORD_UNTIL_MODIFIER KEYWORD_WHILE_MODIFIER
|
17
|
+
)
|
18
|
+
NUMERIC_LITERAL_TYPES = %i(FLOAT FLOAT_RATIONAL INTEGER INTEGER_RATIONAL)
|
19
|
+
PERCENT_ARRAY_LITERAL_TYPES = %i(PERCENT_LOWER_W PERCENT_UPPER_W PERCENT_LOWER_I PERCENT_UPPER_I)
|
20
|
+
NO_DELIMITER_VALUE_TYPES = %i(CONSTANT IDENTIFIER) + NUMERIC_LITERAL_TYPES
|
21
|
+
REQUIRE_SPACE_AFTER_IDENTIFIER_TYPES = %i(KEYWORD_SELF KEYWORD_TRUE KEYWORD_FALSE KEYWORD_NIL METHOD_NAME) + NUMERIC_LITERAL_TYPES
|
22
|
+
CLOSING_DELIMITER_TYPES = %i(PARENTHESIS_RIGHT BRACKET_RIGHT BRACE_RIGHT)
|
23
|
+
|
24
|
+
def initialize(source, filepath: nil)
|
25
|
+
result = Prism.lex(source)
|
26
|
+
raise SyntaxError, filepath || 'compile error' unless result.errors.empty?
|
27
|
+
|
28
|
+
@tokens = result.value
|
29
|
+
|
30
|
+
@in_heredoc = false
|
31
|
+
@string_quote = nil
|
32
|
+
@minified_values = []
|
33
|
+
@heredoc_content_tokens = []
|
34
|
+
end
|
35
|
+
|
36
|
+
def minify
|
37
|
+
squiggly_heredoc = false
|
38
|
+
prev_token = nil
|
39
|
+
|
40
|
+
@tokens.each_cons(2) do |(token, _lex_state), (next_token, _next_lex_state)|
|
41
|
+
case token.type
|
42
|
+
when :COMMENT
|
43
|
+
if prev_token && prev_token.location.start_line < next_token.location.start_line && prev_token.type == :IDENTIFIER && next_token.type == :IDENTIFIER
|
44
|
+
append_token_value_to_minified_values(';')
|
45
|
+
elsif prev_token && prev_token.location.start_line == token.location.start_line && token.location.start_line < next_token.location.start_line || next_token.type == :EOF
|
46
|
+
append_token_value_to_minified_values("\n")
|
47
|
+
end
|
48
|
+
when :IDENTIFIER
|
49
|
+
append_token_to_minified_values(token)
|
50
|
+
|
51
|
+
if REQUIRE_SPACE_AFTER_IDENTIFIER_TYPES.include?(next_token.type)
|
52
|
+
append_token_value_to_minified_values(' ')
|
53
|
+
end
|
54
|
+
when :IGNORED_NEWLINE, :EMBDOC_BEGIN, :EMBDOC_LINE, :EMBDOC_END
|
55
|
+
# noop
|
56
|
+
when :NEWLINE
|
57
|
+
token_value = if next_token.type == :EOF
|
58
|
+
token.value
|
59
|
+
elsif CLOSING_DELIMITER_TYPES.include?(next_token.type)
|
60
|
+
# NOTE: This noop is required due to https://github.com/ruby/prism/issues/2967.
|
61
|
+
else
|
62
|
+
';'
|
63
|
+
end
|
64
|
+
|
65
|
+
append_token_value_to_minified_values(token_value)
|
66
|
+
when :KEYWORD_END
|
67
|
+
if NO_DELIMITER_VALUE_TYPES.include?(prev_token.type) && prev_token.location.start_line == token.location.start_line
|
68
|
+
append_token_value_to_minified_values(' ')
|
69
|
+
end
|
70
|
+
append_token_to_minified_values(token)
|
71
|
+
when :HEREDOC_START
|
72
|
+
@in_heredoc = true
|
73
|
+
squiggly_heredoc = token.value.start_with?('<<~')
|
74
|
+
@string_quote = if token.value.end_with?("'")
|
75
|
+
"'"
|
76
|
+
elsif token.value.end_with?('`')
|
77
|
+
'`'
|
78
|
+
else
|
79
|
+
'"'
|
80
|
+
end
|
81
|
+
|
82
|
+
@minified_values << (@string_quote == '`' ? '`' : '"')
|
83
|
+
when :HEREDOC_END
|
84
|
+
heredoc_contents = @heredoc_content_tokens.join
|
85
|
+
|
86
|
+
if squiggly_heredoc
|
87
|
+
lines = heredoc_contents.lines
|
88
|
+
|
89
|
+
minimum_indentation_length = lines.map { |line|
|
90
|
+
line.match(/\A(?<indentation_spaces> *)/)[:indentation_spaces].length
|
91
|
+
}.min
|
92
|
+
|
93
|
+
indentation = ' ' * minimum_indentation_length
|
94
|
+
|
95
|
+
heredoc_contents = lines.map { |line| line.delete_prefix(indentation) }
|
96
|
+
end
|
97
|
+
|
98
|
+
@minified_values << heredoc_contents
|
99
|
+
@minified_values << (@string_quote == '`' ? '`' : '"')
|
100
|
+
|
101
|
+
@heredoc_content_tokens.clear
|
102
|
+
@in_heredoc = false
|
103
|
+
when :QUESTION_MARK
|
104
|
+
# NOTE: Prevent syntax errors by converting `cond? ? x : y` to `cond??x:y`.
|
105
|
+
token_value = if prev_token.value.end_with?('?')
|
106
|
+
"#{token.value} "
|
107
|
+
else
|
108
|
+
# NOTE: Require both spaces to prevent syntax error of `foo==bar?x:y`.
|
109
|
+
" #{token.value} "
|
110
|
+
end
|
111
|
+
|
112
|
+
append_token_value_to_minified_values(token_value)
|
113
|
+
when :COLON
|
114
|
+
# NOTE: Prevent syntax errors by converting `cond(arg) ? x : y` to `cond(arg)?x:y`.
|
115
|
+
append_token_value_to_minified_values("#{token.value} ")
|
116
|
+
when :UCOLON_COLON
|
117
|
+
append_token_value_to_minified_values(' ') if prev_token.type == :IDENTIFIER || prev_token.type == :METHOD_NAME
|
118
|
+
|
119
|
+
append_token_to_minified_values(token)
|
120
|
+
when :BANG_TILDE
|
121
|
+
append_token_value_to_minified_values(' ') if prev_token.type == :IDENTIFIER
|
122
|
+
|
123
|
+
append_token_to_minified_values(token)
|
124
|
+
when :LABEL
|
125
|
+
append_token_value_to_minified_values(' ') if prev_token.type == :IDENTIFIER
|
126
|
+
|
127
|
+
append_token_to_minified_values(token)
|
128
|
+
|
129
|
+
append_token_value_to_minified_values(' ') if next_token.type == :SYMBOL_BEGIN
|
130
|
+
when :EQUAL, :EQUAL_EQUAL, :EQUAL_EQUAL_EQUAL, :EQUAL_GREATER
|
131
|
+
token_value = (prev_token.value.end_with?('*', '<', '=', '>', '?') ? " #{token.value}" : token.value)
|
132
|
+
|
133
|
+
append_token_value_to_minified_values(token_value)
|
134
|
+
when :STRING_BEGIN, :REGEXP_BEGIN, :PERCENT_LOWER_X, *PERCENT_ARRAY_LITERAL_TYPES
|
135
|
+
append_token_value_to_minified_values(' ') if token.value.start_with?('%')
|
136
|
+
|
137
|
+
append_token_to_minified_values(token)
|
138
|
+
when :KEYWORD_DEF
|
139
|
+
append_token_value_to_minified_values(' ') if prev_token&.type == :IDENTIFIER
|
140
|
+
|
141
|
+
append_token_to_minified_values(token)
|
142
|
+
when :WORDS_SEP
|
143
|
+
if !PERCENT_ARRAY_LITERAL_TYPES.include?(prev_token.type) && next_token.type != :STRING_END
|
144
|
+
append_token_value_to_minified_values(' ')
|
145
|
+
end
|
146
|
+
else
|
147
|
+
append_token_to_minified_values(token)
|
148
|
+
end
|
149
|
+
|
150
|
+
if padding_required?(token, next_token)
|
151
|
+
append_token_value_to_minified_values(' ')
|
152
|
+
end
|
153
|
+
|
154
|
+
prev_token = token
|
155
|
+
end
|
156
|
+
|
157
|
+
@minified_values.join
|
158
|
+
end
|
159
|
+
|
160
|
+
private
|
161
|
+
|
162
|
+
def append_token_to_minified_values(token)
|
163
|
+
if @in_heredoc
|
164
|
+
token_value = if token.type == :STRING_CONTENT
|
165
|
+
token.value.gsub!(/(?<!\\)"/, '\\\"') # Escape quotes.
|
166
|
+
if @in_heredoc && @string_quote == "'"
|
167
|
+
token.value.gsub!(/\#{/, '\\#{') # FIXME: Dirty Hack for escape of string interpolation
|
168
|
+
end
|
169
|
+
end
|
170
|
+
token_value ||= token.value
|
171
|
+
|
172
|
+
@heredoc_content_tokens << token_value
|
173
|
+
else
|
174
|
+
@minified_values << token.value
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def append_token_value_to_minified_values(token_value)
|
179
|
+
if @in_heredoc
|
180
|
+
@heredoc_content_tokens << token_value
|
181
|
+
else
|
182
|
+
@minified_values << token_value
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
def padding_required?(token, next_token)
|
187
|
+
return true if token.type == :IDENTIFIER && (next_token.type == :IDENTIFIER || next_token.type == :CONSTANT)
|
188
|
+
return false if token.type == :SYMBOL_BEGIN || next_token.type == :PARENTHESIS_LEFT
|
189
|
+
|
190
|
+
AFTER_SPACE_REQUIRED_KEYWORDS.include?(token.type) || BEFORE_SPACE_REQUIRED_KEYWORDS.include?(next_token.type)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rake'
|
4
|
+
require 'rake/tasklib'
|
5
|
+
|
6
|
+
# Provides a custom rake task.
|
7
|
+
#
|
8
|
+
# require 'minifyrb/rake_task'
|
9
|
+
# Minifyrb::RakeTask.new
|
10
|
+
#
|
11
|
+
module Minifyrb
|
12
|
+
class RakeTask < ::Rake::TaskLib
|
13
|
+
def initialize(name = :minifyrb, *, &)
|
14
|
+
super()
|
15
|
+
|
16
|
+
desc 'Minify Ruby files in the current directory and subdirectories' unless ::Rake.application.last_description
|
17
|
+
task(name) do
|
18
|
+
RakeFileUtils.verbose(verbose) do
|
19
|
+
run_minify_ruby(verbose)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def run_minify_ruby(verbose)
|
27
|
+
require_relative 'minifier'
|
28
|
+
|
29
|
+
puts 'Running Minify Ruby...' if verbose
|
30
|
+
Dir['**/*.rb'].each { |ruby_filepath|
|
31
|
+
code = File.read(ruby_filepath)
|
32
|
+
minified_code = Minifyrb::Minifier.new(code, filepath: ruby_filepath).minify
|
33
|
+
|
34
|
+
File.write(ruby_filepath, minified_code)
|
35
|
+
}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/minifyrb.rb
ADDED
data/minifyrb.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/minifyrb/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'minifyrb'
|
7
|
+
spec.version = Minifyrb::VERSION
|
8
|
+
spec.authors = ['Koichi ITO']
|
9
|
+
spec.email = ['koic.ito@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'A minifier of Ruby files.'
|
12
|
+
spec.description = 'A minifier of Ruby files.'
|
13
|
+
spec.homepage = 'http://github.com/koic/minifyrb'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 3.3.0')
|
16
|
+
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = 'http://github.com/koic/minifyrb'
|
19
|
+
spec.metadata['changelog_uri'] = 'http://github.com/koic/minifyrb/CHANGELOG.md'
|
20
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) {
|
25
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
26
|
+
}
|
27
|
+
spec.bindir = 'exe'
|
28
|
+
spec.executables = spec.files.grep(%r|\Aexe/|) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ['lib']
|
30
|
+
|
31
|
+
spec.add_dependency 'prism'
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: minifyrb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Koichi ITO
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 2024-08-24 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: prism
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0'
|
26
|
+
description: A minifier of Ruby files.
|
27
|
+
email:
|
28
|
+
- koic.ito@gmail.com
|
29
|
+
executables:
|
30
|
+
- minifyrb
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".github/ISSUE_TEMPLATE/bug_report.md"
|
35
|
+
- ".github/workflows/test.yml"
|
36
|
+
- ".gitignore"
|
37
|
+
- ".rspec"
|
38
|
+
- ".rubocop.yml"
|
39
|
+
- CHANGELOG.md
|
40
|
+
- Gemfile
|
41
|
+
- LICENSE.txt
|
42
|
+
- README.md
|
43
|
+
- Rakefile
|
44
|
+
- bin/console
|
45
|
+
- bin/lex
|
46
|
+
- bin/setup
|
47
|
+
- exe/minifyrb
|
48
|
+
- lib/minifyrb.rb
|
49
|
+
- lib/minifyrb/minifier.rb
|
50
|
+
- lib/minifyrb/rake_task.rb
|
51
|
+
- lib/minifyrb/version.rb
|
52
|
+
- minifyrb.gemspec
|
53
|
+
homepage: http://github.com/koic/minifyrb
|
54
|
+
licenses:
|
55
|
+
- MIT
|
56
|
+
metadata:
|
57
|
+
homepage_uri: http://github.com/koic/minifyrb
|
58
|
+
source_code_uri: http://github.com/koic/minifyrb
|
59
|
+
changelog_uri: http://github.com/koic/minifyrb/CHANGELOG.md
|
60
|
+
rubygems_mfa_required: 'true'
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.3.0
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubygems_version: 3.6.0.dev
|
76
|
+
specification_version: 4
|
77
|
+
summary: A minifier of Ruby files.
|
78
|
+
test_files: []
|