From 8c000033d3604dcad50faf31b2dbecc4f0fb98b6 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 11 Dec 2020 23:42:29 +0000 Subject: [PATCH] clarify corner case in object literal (#4371) closes #4366 --- README.md | 18 +++++++++++++++--- test/compress/dead-code.js | 24 +++++++++++++++++++++++ test/compress/side_effects.js | 36 +++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f1c51a80..c65229fa 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,12 @@ UglifyJS 3 UglifyJS is a JavaScript parser, minifier, compressor and beautifier toolkit. #### Note: -- **`uglify-js@3` has a simplified [API](#api-reference) and [CLI](#command-line-usage) that is not backwards compatible with [`uglify-js@2`](https://github.com/mishoo/UglifyJS/tree/v2.x)**. +- **`uglify-js@3` has a simplified [API](#api-reference) and [CLI](#command-line-usage) + that is not backwards compatible with [`uglify-js@2`](https://github.com/mishoo/UglifyJS/tree/v2.x)**. - **Documentation for UglifyJS `2.x` releases can be found [here](https://github.com/mishoo/UglifyJS/tree/v2.x)**. -- `uglify-js` only supports JavaScript (ECMAScript 5). -- To minify ECMAScript 2015 or above, transpile using tools like [Babel](https://babeljs.io/). +- `uglify-js` supports ECMAScript 5 and some newer language features. +- To minify ECMAScript 2015 or above, you may need to transpile using tools like + [Babel](https://babeljs.io/). Install ------- @@ -1175,6 +1177,16 @@ To allow for better optimizations, the compiler makes various assumptions: - Object properties can be added, removed and modified (not prevented with `Object.defineProperty()`, `Object.defineProperties()`, `Object.freeze()`, `Object.preventExtensions()` or `Object.seal()`). +- Earlier versions of JavaScript will throw `SyntaxError` with the following: + ```js + ({ + p: 42, + get p() {}, + }); + // SyntaxError: Object literal may not have data and accessor property with + // the same name + ``` + UglifyJS may modify the input which in turn may suppress those errors. - Iteration order of keys over an object which contains spread syntax in later versions of Chrome and Node.js may be altered. - When `toplevel` is enabled, UglifyJS effectively assumes input code is wrapped diff --git a/test/compress/dead-code.js b/test/compress/dead-code.js index 62efa460..84e7613a 100644 --- a/test/compress/dead-code.js +++ b/test/compress/dead-code.js @@ -1375,3 +1375,27 @@ issue_4051: { } expect_stdout: "PASS" } + +issue_4366: { + options = { + dead_code: true, + } + input: { + function f() { + return "PASS"; + ({ + p: 42, + get p() {}, + }); + } + console.log(f()); + } + expect: { + function f() { + return "PASS"; + } + console.log(f()); + } + expect_stdout: "PASS" + node_version: ">=4" +} diff --git a/test/compress/side_effects.js b/test/compress/side_effects.js index 0654a4ce..6428b172 100644 --- a/test/compress/side_effects.js +++ b/test/compress/side_effects.js @@ -470,3 +470,39 @@ issue_4325: { } expect_stdout: "PASS" } + +issue_4366_1: { + options = { + side_effects: true, + } + input: { + ({ + p: 42, + get p() {}, + q: console.log("PASS"), + }); + } + expect: { + console.log("PASS"); + } + expect_stdout: "PASS" + node_version: ">=4" +} + +issue_4366_2: { + options = { + side_effects: true, + } + input: { + ({ + set p(v) {}, + q: console.log("PASS"), + p: 42, + }); + } + expect: { + console.log("PASS"); + } + expect_stdout: "PASS" + node_version: ">=4" +} -- 2.34.1