add new compress option unsafe_methods for ecma >= 6 (#2325)

fixes #2321
This commit is contained in:
kzc
2017-09-19 12:15:54 -04:00
committed by Alex Lam S.L
parent c46b9f361a
commit e8235657e4
5 changed files with 50 additions and 1 deletions

View File

@@ -631,6 +631,11 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u
- `unsafe_math` (default: false) -- optimize numerical expressions like - `unsafe_math` (default: false) -- optimize numerical expressions like
`2 * x * 3` into `6 * x`, which may give imprecise floating point results. `2 * x * 3` into `6 * x`, which may give imprecise floating point results.
- `unsafe_methods` (default: false) -- Converts `{ m: function(){} }` to
`{ m(){} }`. `ecma` must be set to `6` or greater to enable this transform.
Note: if enabled there is a risk of getting a "`<method name>` is not a
constructor" TypeError should any code try to `new` the former function.
- `unsafe_proto` (default: false) -- optimize expressions like - `unsafe_proto` (default: false) -- optimize expressions like
`Array.prototype.slice.call(a)` into `[].slice.call(a)` `Array.prototype.slice.call(a)` into `[].slice.call(a)`

View File

@@ -88,6 +88,7 @@ function Compressor(options, false_by_default) {
unsafe_comps : false, unsafe_comps : false,
unsafe_Func : false, unsafe_Func : false,
unsafe_math : false, unsafe_math : false,
unsafe_methods: false,
unsafe_proto : false, unsafe_proto : false,
unsafe_regexp : false, unsafe_regexp : false,
unused : !false_by_default, unused : !false_by_default,
@@ -4866,7 +4867,7 @@ merge(Compressor.prototype, {
// p:async function(){} ---> async p(){} // p:async function(){} ---> async p(){}
// p:()=>{} ---> p(){} // p:()=>{} ---> p(){}
// p:async()=>{} ---> async p(){} // p:async()=>{} ---> async p(){}
if (compressor.option("ecma") >= 6) { if (compressor.option("unsafe_methods") && compressor.option("ecma") >= 6) {
var key = self.key; var key = self.key;
var value = self.value; var value = self.value;
var is_arrow_with_block = value instanceof AST_Arrow var is_arrow_with_block = value instanceof AST_Arrow

View File

@@ -290,6 +290,7 @@ issue_2105_1: {
passes: 3, passes: 3,
reduce_vars: true, reduce_vars: true,
side_effects: true, side_effects: true,
unsafe_methods: true,
unused: true, unused: true,
} }
input: { input: {

View File

@@ -516,6 +516,7 @@ variable_as_computed_property: {
prop_func_to_concise_method: { prop_func_to_concise_method: {
options = { options = {
ecma: 6, ecma: 6,
unsafe_methods: true,
} }
input: { input: {
({ ({
@@ -544,6 +545,7 @@ prop_func_to_concise_method: {
prop_arrow_to_concise_method: { prop_arrow_to_concise_method: {
options = { options = {
ecma: 6, ecma: 6,
unsafe_methods: true,
} }
input: { input: {
({ ({
@@ -592,6 +594,7 @@ concise_method_to_prop_arrow: {
prop_func_to_async_concise_method: { prop_func_to_async_concise_method: {
options = { options = {
ecma: 8, ecma: 8,
unsafe_methods: true,
} }
input: { input: {
({ ({
@@ -614,6 +617,7 @@ prop_func_to_async_concise_method: {
prop_func_to_concise_method_various: { prop_func_to_concise_method_various: {
options = { options = {
ecma: 6, ecma: 6,
unsafe_methods: true,
} }
input: { input: {
({ ({
@@ -646,6 +650,7 @@ prop_func_to_concise_method_various: {
prop_arrows_to_concise_method_various: { prop_arrows_to_concise_method_various: {
options = { options = {
ecma: 6, ecma: 6,
unsafe_methods: true,
} }
input: { input: {
({ ({
@@ -674,6 +679,7 @@ prop_arrows_to_concise_method_various: {
prop_arrow_with_this: { prop_arrow_with_this: {
options = { options = {
ecma: 6, ecma: 6,
unsafe_methods: true,
} }
input: { input: {
function run(arg) { function run(arg) {
@@ -711,6 +717,7 @@ prop_arrow_with_this: {
prop_arrow_with_nested_this: { prop_arrow_with_nested_this: {
options = { options = {
ecma: 6, ecma: 6,
unsafe_methods: true,
} }
input: { input: {
function run(arg) { function run(arg) {

View File

@@ -887,6 +887,7 @@ methods_keep_quoted_true: {
options = { options = {
arrows: true, arrows: true,
ecma: 6, ecma: 6,
unsafe_methods: true,
} }
mangle = { mangle = {
properties: { properties: {
@@ -906,6 +907,7 @@ methods_keep_quoted_false: {
options = { options = {
arrows: true, arrows: true,
ecma: 6, ecma: 6,
unsafe_methods: true,
} }
mangle = { mangle = {
properties: { properties: {
@@ -931,6 +933,7 @@ methods_keep_quoted_from_dead_code: {
evaluate: true, evaluate: true,
reduce_vars: true, reduce_vars: true,
side_effects: true, side_effects: true,
unsafe_methods: true,
} }
mangle = { mangle = {
properties: { properties: {
@@ -964,3 +967,35 @@ issue_2256: {
g.keep = g.g; g.keep = g.g;
} }
} }
issue_2321: {
options = {
ecma: 6,
unsafe_methods: false,
}
input: {
var f = {
foo: function(){ console.log("foo") },
bar() { console.log("bar") }
};
var foo = new f.foo();
var bar = f.bar();
}
expect: {
var f = {
foo: function() {
console.log("foo");
},
bar() {
console.log("bar");
}
};
var foo = new f.foo();
var bar = f.bar();
}
expect_stdout: [
"foo",
"bar",
]
node_version: ">=6"
}