Skip to content

Commit

Permalink
Use semicolon instead of newline to minify
Browse files Browse the repository at this point in the history
Minification aims to shorten code and reduce the number of bytes transferred.
Both "\n" and semicolons are 1 byte each, but in environments like Windows,
where "\r\n" is used for newline, a newline is 2 bytes.
Therefore, newlines are replaced with semicolons to reduce the byte count to 1 byte.

While newlines can improve the readability of backtrace in case of error,
and reducing byte count with one-liner is a trade-off,
the primary goal of minification is to reduce byte count.
Hence, reducing the byte count is prioritized.

Additionally, to comply with POSIX specification, the newline before EOF is preserved.
  • Loading branch information
koic committed Aug 4, 2024
1 parent af70f3a commit cdba400
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 42 deletions.
2 changes: 2 additions & 0 deletions lib/minifyrb/minifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def minify

heredoc_content_tokens.clear
in_heredoc = false
when :NEWLINE
minified_values << (next_token.type == :EOF ? token.value : ';')
else
minified_values << token.value
end
Expand Down
61 changes: 19 additions & 42 deletions spec/minifyrb/minifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@

it 'does not contain a blank line' do
expect(minified_ruby).to eq <<~RUBY
foo
bar
foo;bar
RUBY
end
end
Expand Down Expand Up @@ -105,8 +104,7 @@

it 'contain a space after method call' do
expect(minified_ruby).to eq <<~RUBY
val='str'
foo val
val='str';foo val
RUBY
end
end
Expand Down Expand Up @@ -165,8 +163,7 @@

it 'contain a space after method call' do
expect(minified_ruby).to eq <<~RUBY
val='str'
foo(val)
val='str';foo(val)
RUBY
end
end
Expand Down Expand Up @@ -195,8 +192,7 @@

it 'contain a space after the keyword' do
expect(minified_ruby).to eq <<~RUBY
if cond
end
if cond;end
RUBY
end
end
Expand All @@ -211,8 +207,7 @@

it 'contain a space around the keyword' do
expect(minified_ruby).to eq <<~RUBY
if cond then foo
end
if cond then foo;end
RUBY
end
end
Expand Down Expand Up @@ -242,9 +237,7 @@

it 'contain a space after the keyword' do
expect(minified_ruby).to eq <<~RUBY
if cond
elsif cond2
end
if cond;elsif cond2;end
RUBY
end
end
Expand All @@ -259,8 +252,7 @@

it 'contain a space after the keyword' do
expect(minified_ruby).to eq <<~RUBY
unless cond
end
unless cond;end
RUBY
end
end
Expand Down Expand Up @@ -304,9 +296,7 @@

it 'contain a space after the keyword' do
expect(minified_ruby).to eq <<~RUBY
case var
when cond
end
case var;when cond;end
RUBY
end
end
Expand All @@ -323,9 +313,7 @@
# TODO: Remove the redundant space before `in` keyword.
it 'contain a space after the keyword' do
expect(minified_ruby).to eq <<~RUBY
case var
in cond
end
case var; in cond;end
RUBY
end
end
Expand All @@ -340,8 +328,7 @@

it 'contain a space after the keyword' do
expect(minified_ruby).to eq <<~RUBY
while cond
end
while cond;end
RUBY
end
end
Expand All @@ -356,8 +343,7 @@

it 'contain a space after the keyword' do
expect(minified_ruby).to eq <<~RUBY
until cond
end
until cond;end
RUBY
end
end
Expand Down Expand Up @@ -400,8 +386,7 @@

it 'contain a space after the keyword' do
expect(minified_ruby).to eq <<~RUBY
for item in items
end
for item in items;end
RUBY
end
end
Expand Down Expand Up @@ -599,8 +584,7 @@ def x() = foo do

it 'contain a space after the keyword' do
expect(minified_ruby).to eq <<~RUBY
def x()=foo do yield(x,y)
end
def x()=foo do yield(x,y);end
RUBY
end
end
Expand Down Expand Up @@ -660,8 +644,7 @@ def x()=foo do yield(x,y)

it 'contain a space after the keyword' do
expect(minified_ruby).to eq <<~RUBY
begin rescue CustomError=>e
else ensure end
begin rescue CustomError=>e;else ensure end
RUBY
end
end
Expand Down Expand Up @@ -733,9 +716,7 @@ def foo arg

it 'contains spaces' do
expect(minified_ruby).to eq <<~RUBY
def foo arg
super arg
end
def foo arg;super arg;end
RUBY
end
end
Expand All @@ -750,8 +731,7 @@ def foo

it 'contains spaces' do
expect(minified_ruby).to eq <<~RUBY
def foo
end
def foo;end
RUBY
end
end
Expand All @@ -766,8 +746,7 @@ def self.foo

it 'contains spaces' do
expect(minified_ruby).to eq <<~RUBY
def self.foo
end
def self.foo;end
RUBY
end
end
Expand All @@ -782,8 +761,7 @@ class Foo

it 'contains spaces' do
expect(minified_ruby).to eq <<~RUBY
class Foo
end
class Foo;end
RUBY
end
end
Expand Down Expand Up @@ -826,8 +804,7 @@ module Foo

it 'contains spaces' do
expect(minified_ruby).to eq <<~RUBY
module Foo
end
module Foo;end
RUBY
end
end
Expand Down

0 comments on commit cdba400

Please sign in to comment.