Skip to content

Commit

Permalink
pass lhs_constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ahorek committed May 9, 2023
1 parent 57138b8 commit 496f367
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased
## 1.1.15 (09 May 2023)
- update TerserJS to [5.17.3]
- add lhs_constants (default is true)

## 1.1.14 (24 February 2023)
- update TerserJS to [5.16.5]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Available options and their defaults are
:keep_fnames => false, # Do not drop names in function definitions
:passes => 1, # Number of times to run compress. Raising the number of passes will increase compress time, but can produce slightly smaller code.
:keep_infinity => false, # Prevent compression of Infinity to 1/0
:lhs_constants => true, # Moves constant values to the left-hand side of binary nodes. `foo == 42 → 42 == foo`
:side_effects => true, # Pass false to disable potentially dropping functions marked as "pure" using pure comment annotation. See UglifyJS documentation for details.
:switches => true, # de-duplicate and remove unreachable switch branches
}, # Apply transformations to code, set to false to skip
Expand Down
1 change: 1 addition & 0 deletions lib/terser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Error < StandardError; end
:keep_fnames => false, # Do not drop names in function definitions
:passes => 1, # Number of times to run compress. Raising the number of passes will increase compress time, but can produce slightly smaller code.
:keep_infinity => false, # Prevent compression of Infinity to 1/0
:lhs_constants => true, # Moves constant values to the left-hand side of binary nodes. `foo == 42 → 42 == foo`
:side_effects => true, # Pass false to disable potentially dropping functions marked as "pure" using pure comment annotation. See TerserJS documentation for details.
:switches => true # de-duplicate and remove unreachable switch branches
}, # Apply transformations to code, set to false to skip
Expand Down
31 changes: 31 additions & 0 deletions spec/terser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,37 @@ class Bar {}
end
end

describe 'lhs_constants' do
let(:code) do
<<-JS
function fun(a) {
var x;
x = a == 42;
x = a === 42;
return x;
}
JS
end

it 'compresses with lhs_constants' do
compiled = Terser.compile(code, :compress => {
:evaluate => true,
:lhs_constants => true
})
expect(compiled).to include("42==n")
expect(compiled).to include("42===n")
end

it 'can be disabled to preserve the order' do
compiled = Terser.compile(code, :compress => {
:evaluate => true,
:lhs_constants => false
})
expect(compiled).to include("n==42")
expect(compiled).to include("n===42")
end
end

describe 'quote style' do
let(:code) do
<<-JS
Expand Down

0 comments on commit 496f367

Please sign in to comment.