diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-03-27 01:30:21 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-27 01:30:21 +0800 |
commit | 57ce5bd9e085546a5c1cb8dd4a3ea71ab6c56f26 (patch) | |
tree | ee5e196f4aa4fcbe867eb82287b73b9db6b4174e /test/compress/reduce_vars.js | |
parent | 861a79ac9fdb2cdbb54054306eb896e2c134af73 (diff) | |
download | tracifyjs-57ce5bd9e085546a5c1cb8dd4a3ea71ab6c56f26.tar.gz tracifyjs-57ce5bd9e085546a5c1cb8dd4a3ea71ab6c56f26.zip |
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.
Diffstat (limited to 'test/compress/reduce_vars.js')
-rw-r--r-- | test/compress/reduce_vars.js | 289 |
1 files changed, 286 insertions, 3 deletions
diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index f4dd68d2..87942ab9 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -425,7 +425,7 @@ iife_new: { expect_stdout: true } -multi_def: { +multi_def_1: { options = { evaluate: true, reduce_vars: true, @@ -435,7 +435,7 @@ multi_def: { if (a) var b = 1; else - var b = 2 + var b = 2; console.log(b + 1); } } @@ -444,7 +444,7 @@ multi_def: { if (a) var b = 1; else - var b = 2 + var b = 2; console.log(b + 1); } } @@ -479,6 +479,33 @@ multi_def_2: { } } +multi_def_3: { + options = { + evaluate: true, + reduce_vars: true, + } + input: { + function f(a) { + var b = 2; + if (a) + var b; + else + var b; + console.log(b + 1); + } + } + expect: { + function f(a) { + var b = 2; + if (a) + var b; + else + var b; + console.log(3); + } + } +} + use_before_var: { options = { evaluate: true, @@ -1571,3 +1598,259 @@ unary_delete: { } expect_stdout: true } + +redefine_arguments_1: { + options = { + evaluate: true, + keep_fargs: false, + reduce_vars: true, + unused: true, + } + input: { + function f() { + var arguments; + return typeof arguments; + } + function g() { + var arguments = 42; + return typeof arguments; + } + function h(x) { + var arguments = x; + return typeof arguments; + } + console.log(f(), g(), h()); + } + expect: { + function f() { + var arguments; + return typeof arguments; + } + function g() { + return"number"; + } + function h(x) { + var arguments = x; + return typeof arguments; + } + console.log(f(), g(), h()); + } + expect_stdout: "object number undefined" +} + +redefine_arguments_2: { + options = { + evaluate: true, + keep_fargs: false, + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + function f() { + var arguments; + return typeof arguments; + } + function g() { + var arguments = 42; + return typeof arguments; + } + function h(x) { + var arguments = x; + return typeof arguments; + } + console.log(f(), g(), h()); + } + expect: { + console.log(function() { + var arguments; + return typeof arguments; + }(), function() { + return"number"; + }(), function(x) { + var arguments = x; + return typeof arguments; + }()); + } + expect_stdout: "object number undefined" +} + +redefine_arguments_3: { + options = { + evaluate: true, + keep_fargs: false, + passes: 3, + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + function f() { + var arguments; + return typeof arguments; + } + function g() { + var arguments = 42; + return typeof arguments; + } + function h(x) { + var arguments = x; + return typeof arguments; + } + console.log(f(), g(), h()); + } + expect: { + console.log(function() { + var arguments; + return typeof arguments; + }(), "number", "undefined"); + } + expect_stdout: "object number undefined" +} + +redefine_farg_1: { + options = { + evaluate: true, + keep_fargs: false, + reduce_vars: true, + unused: true, + } + input: { + function f(a) { + var a; + return typeof a; + } + function g(a) { + var a = 42; + return typeof a; + } + function h(a, b) { + var a = b; + return typeof a; + } + console.log(f([]), g([]), h([])); + } + expect: { + function f(a) { + var a; + return typeof a; + } + function g() { + return"number"; + } + function h(a, b) { + var a = b; + return typeof a; + } + console.log(f([]), g([]), h([])); + } + expect_stdout: "object number undefined" +} + +redefine_farg_2: { + options = { + evaluate: true, + keep_fargs: false, + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + function f(a) { + var a; + return typeof a; + } + function g(a) { + var a = 42; + return typeof a; + } + function h(a, b) { + var a = b; + return typeof a; + } + console.log(f([]), g([]), h([])); + } + expect: { + console.log(function(a) { + var a; + return typeof a; + }([]), function() { + return "number"; + }(),function(a, b) { + var a = b; + return typeof a; + }([])); + } + expect_stdout: "object number undefined" +} + +redefine_farg_3: { + options = { + evaluate: true, + keep_fargs: false, + passes: 3, + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + function f(a) { + var a; + return typeof a; + } + function g(a) { + var a = 42; + return typeof a; + } + function h(a, b) { + var a = b; + return typeof a; + } + console.log(f([]), g([]), h([])); + } + expect: { + console.log(function(a) { + var a; + return typeof a; + }([]), "number", function(a) { + var a = void 0; + return typeof a; + }([])); + } + expect_stdout: "object number undefined" +} + +delay_def: { + options = { + evaluate: true, + reduce_vars: true, + unused: true, + } + input: { + function f() { + return a; + var a; + } + function g() { + return a; + var a = 1; + } + console.log(f(), g()); + } + expect: { + function f() { + return a; + var a; + } + function g() { + return a; + var a = 1; + } + console.log(f(), g()); + } + expect_stdout: true +} |