From cdba4004020864459aa68e81831fa4a3fc5cc774 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sun, 4 Aug 2024 22:30:38 +0900 Subject: [PATCH] Use semicolon instead of newline to minify 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. --- lib/minifyrb/minifier.rb | 2 ++ spec/minifyrb/minifier_spec.rb | 61 +++++++++++----------------------- 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/lib/minifyrb/minifier.rb b/lib/minifyrb/minifier.rb index 1939df2..9e6bdcc 100644 --- a/lib/minifyrb/minifier.rb +++ b/lib/minifyrb/minifier.rb @@ -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 diff --git a/spec/minifyrb/minifier_spec.rb b/spec/minifyrb/minifier_spec.rb index 470227f..0cc1113 100644 --- a/spec/minifyrb/minifier_spec.rb +++ b/spec/minifyrb/minifier_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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