Alex Lam S.L
f7ca4f2297
fix corner cases in switch and undefined ( #1762 )
...
- fix side effects in switch condition for singular blocks
- fix `undefined` confusion with local variable
- gate `OPT(AST_Switch)` with `switches`
fixes #1758
fixes #1759
2017-04-02 14:52:25 +08:00
Alex Lam S.L
ee3fe0f4cd
fix switch branch elimination ( #1752 )
...
Merge unreachable case body with previous fallthrough case
fixes #1750
2017-04-01 17:19:57 +08:00
Alex Lam S.L
257ddc3bdb
improve compression of undefined, NaN & Infinitiy ( #1748 )
...
- migrate transformation logic from `OutputStream` to `Compressor`
- always turn `undefined` into `void 0` (unless `unsafe`)
- always keep `NaN` except when avoiding local variable redefinition
- introduce `keep_infinity` to suppress `1/0` transform, except when avoiding local variable redefinition
supersedes #1723
fixes #1730
2017-04-01 03:02:14 +08:00
Alex Lam S.L
1ddc05725d
combine rules for binary boolean operations ( #1744 )
2017-03-31 18:47:44 +08:00
Alex Lam S.L
a0c3836ba0
sort options in alphabetical order ( #1743 )
...
They started off as functional groups I guess, but given the sheer number of options this is becoming too difficult to read.
2017-03-31 16:41:04 +08:00
Alex Lam S.L
11e9bdc427
fix missing preamble when shebang is absent ( #1742 )
2017-03-31 15:26:57 +08:00
Alex Lam S.L
c595b84032
fix catch symbol mangling ( #1734 )
...
Only need to look up the immediate non-block/catch scope for the same-name special case.
fixes #1733
2017-03-31 02:57:47 +08:00
Alex Lam S.L
7cb1adf455
remove paranthesis for -(x*y) ( #1732 )
2017-03-30 16:09:00 +08:00
Alex Lam S.L
7bea38a05d
optimize try-catch-finally ( #1731 )
...
- eliminate empty blocks
- flatten out if try-block does not throw
2017-03-30 12:16:58 +08:00
Alex Lam S.L
beb9659778
speed up IIFE elimination ( #1728 )
...
- `side_effects` will clean up inner statements, so checking for an empty function body should suffice
- drop side effects when dropping `return` from statement
2017-03-29 23:27:35 +08:00
Alex Lam S.L
f1a833a7aa
speed up equivalent_to() and AST_Switch ( #1727 )
2017-03-29 22:08:26 +08:00
Alex Lam S.L
2e41cd6394
fix missing parentheses around NaN/Infinity shorthands ( #1726 )
...
fixes #1724
fixes #1725
2017-03-29 20:53:03 +08:00
Alex Lam S.L
09f77c7d4d
output optimal representations of NaN & Infinity ( #1723 )
...
- move these optimisations out from `Compressor` to `OutputStream`
- fixes behaviour inconsistency when running uglified code from global or module levels due to redefinition
2017-03-29 18:31:55 +08:00
Alex Lam S.L
fef0bf9ee0
improve beautified output of switch blocks ( #1721 )
2017-03-29 04:40:05 +08:00
Alex Lam S.L
eb48a035e7
fix corner case in unused ( #1718 )
...
When fixing catch-related issue in #1715 , it tries to optimise for duplicate definitions but did not take anonymous functions into account.
Remove such optimisation for now and we can cover this as a more general rule later.
2017-03-29 01:00:21 +08:00
Alex Lam S.L
c909ffb715
fix unused on var of the same name within catch ( #1716 )
...
fixes #1715
2017-03-28 21:25:49 +08:00
Alex Lam S.L
f71f4905b0
fix is_number() on += ( #1714 )
...
fixes #1710
2017-03-28 17:08:16 +08:00
Alex Lam S.L
fb177a6312
drop anonymous function name when overshadowed by other declarations ( #1712 )
...
fixes #1709
2017-03-28 17:02:20 +08:00
Alex Lam S.L
65da9acce6
handle var within catch of the same name ( #1711 )
...
The following code prints `1`:
var a = 1;
!function(){
a = 4;
try{
throw 2;
} catch (a) {
var a = 3;
}
}();
console.log(a);
fixes #1708
2017-03-28 16:42:39 +08:00
Alex Lam S.L
67d0237f73
fix tail trimming of switch blocks ( #1707 )
...
now guarded under `dead_code`
fixes #1705
2017-03-28 03:59:13 +08:00
Alex Lam S.L
984a21704e
fix mangle for variable declared within catch block ( #1706 )
...
fixes #1704
2017-03-28 03:26:35 +08:00
Alex Lam S.L
c526da59a1
has_side_effects() should take AST_Switch.expression into account (#1699 )
...
fixes #1698
2017-03-27 18:09:35 +08:00
Alex Lam S.L
581630e0a7
fix typeof side effects ( #1696 )
...
`statement_to_expression()` drops `typeof` even if it operates on undeclared variables.
Since we now have `drop_side_effect_free()`, replace and remove this deprecated functionality.
2017-03-27 04:37:42 +08:00
Alex Lam S.L
f5952933a0
preserve side effects in switch expression ( #1694 )
...
fixes #1690
2017-03-27 02:32:46 +08:00
Alex Lam S.L
f001e4cb9d
fix cascade on anonymous function reference ( #1693 )
...
Unlike normal variables and even function definitions, these cannot be reassigned, even though assignment expressions would "leak" the assigned value as normal.
2017-03-27 01:58:21 +08:00
Alex Lam S.L
57ce5bd9e0
handle overlapped variable definitions ( #1691 )
...
Process variable definitions with or without assigned values against:
- `arguments`
- named function arguments
- multiple definitions within same scope
Essentially demote variable declarations with no value assignments.
Also fixed invalid use of `AST_VarDef` over `arguments` - should use a member of `AST_SymbolDeclaration` instead.
2017-03-27 01:30:21 +08:00
Alex Lam S.L
861a79ac9f
fix delete related issues in collapse_vars and reduce_vars ( #1689 )
2017-03-26 19:14:30 +08:00
Alex Lam S.L
e76fb354eb
fix cascade on delete operator ( #1687 )
...
Conditions including strict mode would make `delete` return `true` or `false`, and are too complex to be evaluated by the compressor.
Suppress assignment folding into said operator.
fixes #1685
2017-03-26 18:08:44 +08:00
Alex Lam S.L
3276740779
fallthrough should not execute case expression ( #1683 )
...
- de-duplicate trailing cases only, avoid all potential side-effects
- enable switch statement fuzzing
fixes #1680
2017-03-26 16:52:38 +08:00
kzc
5509e51098
optimize conditional when condition symbol matches consequent ( #1684 )
2017-03-26 16:36:33 +08:00
Alex Lam S.L
94f84727ce
suppress switch branch de-duplication upon side effects ( #1682 )
...
fixes #1679
2017-03-26 13:32:43 +08:00
Alex Lam S.L
8a4f86528f
fix side-effects detection on switch statements ( #1678 )
...
extension of #1675
2017-03-26 12:05:44 +08:00
Alex Lam S.L
f83d370f57
improve switch optimisations ( #1677 )
...
- correctly determine reachability of (default) branches
- gracefully handle multiple default branches
- optimise branches with duplicate bodies
fixes #376
fixes #441
fixes #1674
2017-03-26 05:15:46 +08:00
Alex Lam S.L
b19aa58cff
fix has_side_effects() ( #1675 )
...
`AST_Try` is an `AST_Block`, so besides try block we also need to inspect catch and finally blocks for possible side effects.
Also extend this functionality to handle `AST_If` and `AST_LabeledStatement` while we are at it.
fixes #1673
2017-03-25 23:03:26 +08:00
Alex Lam S.L
0a65de89b9
fix reduce_vars on AST_Switch ( #1671 )
...
Take conditional nature of switch branches into account.
fixes #1670
2017-03-25 21:17:30 +08:00
Alex Lam S.L
6e86ee950d
fix typeof side-effects ( #1669 )
...
`has_side_effects()` does not take `typeof`'s magical power of not tripping over undeclared variable into account.
fixes #1668
2017-03-25 17:40:18 +08:00
Alex Lam S.L
8ca2401ebe
fix dead_code on AST_Switch ( #1667 )
...
Need to call `extract_declarations_from_unreachable_code()`.
fixes #1663
2017-03-25 16:21:42 +08:00
Alex Lam S.L
a30092e20f
fix invalid AST_For.init ( #1657 )
...
Turns out the only place in `Compressor` which can generate invalid `AST_For.init` is within `drop_unused()`, so focus the fix-up efforts.
supercedes #1652
fixes #1656
2017-03-25 03:18:36 +08:00
Alex Lam S.L
32283a0def
fix cascade of evaluate optimisation ( #1654 )
...
Operator has changed, so break out from rest of the rules.
fixes #1649
2017-03-24 22:09:19 +08:00
Alex Lam S.L
ac51d4c5a0
fix corner case in AST_For.init ( #1652 )
...
Enforce `null` as value for empty initialisation blocks.
fixes #1648
2017-03-24 19:31:17 +08:00
Alex Lam S.L
0432a7abb9
fix assignment extraction from conditional ( #1651 )
...
fixes #1645
fixes #1646
2017-03-24 18:52:48 +08:00
Alex Lam S.L
f3a1694a41
fix assignment substitution in sequences ( #1643 )
...
take side effects of binary boolean operations into account
fixes #1639
2017-03-24 14:30:31 +08:00
Alex Lam S.L
2e0dc97003
improve error marker placement ( #1644 )
...
For AST_UnaryPrefix, points to the operator rather than end of expression.
2017-03-24 14:28:40 +08:00
kzc
79334dda10
fix regression: CLI options with hyphens like -b ascii-only ( #1640 )
...
fixes #1637
2017-03-24 11:55:03 +08:00
Alex Lam S.L
e918748d88
improve collapsible value detection ( #1638 )
...
- #1634 bars variables with cross-scope references in between to collapse
- but if assigned value is side-effect-free, no states can be modified, so it is safe to move
2017-03-24 02:55:32 +08:00
Alex Lam S.L
48ffbef51d
account for cross-scope modifications in collapse_vars ( #1634 )
...
mostly done by @kzc
fixes #1631
2017-03-23 07:17:34 +08:00
Alex Lam S.L
c0f3feae9f
introduce compressor.info() ( #1633 )
...
report the following only when `options.warnings = "verbose"`
- unused elements due to inlining
- collpased variables
2017-03-23 06:49:49 +08:00
Alex Lam S.L
4bceb85cbf
throw parse error on invalid assignments ( #1627 )
...
fixes #1626
2017-03-21 14:11:32 +08:00
Alex Lam S.L
96f8befdd7
fix commit 88fb83a ( #1622 )
...
The following is wrong:
`a == (b ? a : c)` => `b`
Because:
- `b` may not be boolean
- `a` might have side effects
- `a == a` is not always `true` (think `NaN`)
- `a == c` is not always `false`
2017-03-19 11:59:42 +08:00
Alex Lam S.L
cd58635dcc
fix AST_Binary.lift_sequences() ( #1621 )
...
Commit eab99a1c fails to account for side effects from compound assignments.
2017-03-19 03:04:22 +08:00