Merge branch 'master' into harmony-v3.0.8

This commit is contained in:
alexlamsl
2017-05-18 14:54:18 +08:00
8 changed files with 89 additions and 82 deletions

View File

@@ -1,6 +1,5 @@
uglify-es uglify-es
========= =========
[![Build Status](https://travis-ci.org/mishoo/UglifyJS2.svg)](https://travis-ci.org/mishoo/UglifyJS2)
**uglify-es** is an ECMAScript 2015 parser, minifier, compressor and beautifier toolkit. **uglify-es** is an ECMAScript 2015 parser, minifier, compressor and beautifier toolkit.
@@ -550,10 +549,10 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u
- `keep_infinity` -- default `false`. Pass `true` to prevent `Infinity` from - `keep_infinity` -- default `false`. Pass `true` to prevent `Infinity` from
being compressed into `1/0`, which may cause performance issues on Chrome. being compressed into `1/0`, which may cause performance issues on Chrome.
- `side_effects` -- default `false`. Pass `true` to potentially drop functions - `side_effects` -- default `true`. Pass `false` to disable potentially dropping
marked as "pure". A function call is marked as "pure" if a comment annotation functions marked as "pure". A function call is marked as "pure" if a comment
`/*@__PURE__*/` or `/*#__PURE__*/` immediately precedes the call. For example: annotation `/*@__PURE__*/` or `/*#__PURE__*/` immediately precedes the call. For
`/*@__PURE__*/foo()`; example: `/*@__PURE__*/foo()`;
## Mangle options ## Mangle options
@@ -604,36 +603,36 @@ The code generator tries to output shortest code possible by default. In
case you want beautified output, pass `--beautify` (`-b`). Optionally you case you want beautified output, pass `--beautify` (`-b`). Optionally you
can pass additional arguments that control the code output: can pass additional arguments that control the code output:
- `ascii_only` (default `false`) -- escape Unicode characters in strings and
regexps (affects directives with non-ascii characters becoming invalid)
- `beautify` (default `true`) -- whether to actually beautify the output. - `beautify` (default `true`) -- whether to actually beautify the output.
Passing `-b` will set this to true, but you might need to pass `-b` even Passing `-b` will set this to true, but you might need to pass `-b` even
when you want to generate minified code, in order to specify additional when you want to generate minified code, in order to specify additional
arguments, so you can use `-b beautify=false` to override it. arguments, so you can use `-b beautify=false` to override it.
- `indent_level` (default 4)
- `indent_start` (default 0) -- prefix all lines by that many spaces
- `quote_keys` (default `false`) -- pass `true` to quote all keys in literal
objects
- `space_colon` (default `true`) -- insert a space after the colon signs
- `ascii_only` (default `false`) -- escape Unicode characters in strings and
regexps (affects directives with non-ascii characters becoming invalid)
- `inline_script` (default `false`) -- escape the slash in occurrences of
`</script` in strings
- `width` (default 80) -- only takes effect when beautification is on, this
specifies an (orientative) line width that the beautifier will try to
obey. It refers to the width of the line text (excluding indentation).
It doesn't work very well currently, but it does make the code generated
by UglifyJS more readable.
- `max_line_len` (default 32000) -- maximum line length (for uglified code)
- `bracketize` (default `false`) -- always insert brackets in `if`, `for`, - `bracketize` (default `false`) -- always insert brackets in `if`, `for`,
`do`, `while` or `with` statements, even if their body is a single `do`, `while` or `with` statements, even if their body is a single
statement. statement.
- `semicolons` (default `true`) -- separate statements with semicolons. If - `comments` (default `false`) -- pass `true` or `"all"` to preserve all
you pass `false` then whenever possible we will use a newline instead of a comments, `"some"` to preserve some comments, a regular expression string
semicolon, leading to more readable output of uglified code (size before (e.g. `/^!/`) or a function.
gzip could be smaller; size after gzip insignificantly larger). - `ecma` (default `5`) -- set output printing mode. This will only change the
output in direct control of the beautifier. Non-compatible features in the
abstract syntax tree will still be outputted as is.
- `indent_level` (default 4)
- `indent_start` (default 0) -- prefix all lines by that many spaces
- `inline_script` (default `false`) -- escape the slash in occurrences of
`</script` in strings
- `keep_quoted_props` (default `false`) -- when turned on, prevents stripping
quotes from property names in object literals.
- `max_line_len` (default `false`) -- maximum line length (for uglified code)
- `preamble` (default `null`) -- when passed it must be a string and - `preamble` (default `null`) -- when passed it must be a string and
it will be prepended to the output literally. The source map will it will be prepended to the output literally. The source map will
adjust for this text. Can be used to insert a comment containing adjust for this text. Can be used to insert a comment containing
licensing information, for example. licensing information, for example.
- `preserve_line` (default `false`) -- pass `true` to preserve lines, but it
only works if `beautify` is set to `false`.
- `quote_keys` (default `false`) -- pass `true` to quote all keys in literal
objects
- `quote_style` (default `0`) -- preferred quote style for strings (affects - `quote_style` (default `0`) -- preferred quote style for strings (affects
quoted property names and directives as well): quoted property names and directives as well):
- `0` -- prefers double quotes, switches to single quotes when there are - `0` -- prefers double quotes, switches to single quotes when there are
@@ -641,12 +640,19 @@ can pass additional arguments that control the code output:
- `1` -- always use single quotes - `1` -- always use single quotes
- `2` -- always use double quotes - `2` -- always use double quotes
- `3` -- always use the original quotes - `3` -- always use the original quotes
- `keep_quoted_props` (default `false`) -- when turned on, prevents stripping - `semicolons` (default `true`) -- separate statements with semicolons. If
quotes from property names in object literals. you pass `false` then whenever possible we will use a newline instead of a
- `ecma` (default `5`) -- set output printing mode. This will only change the semicolon, leading to more readable output of uglified code (size before
output in direct control of the beautifier. Non-compatible features in the gzip could be smaller; size after gzip insignificantly larger).
abstract syntax tree will still be outputted as is. - `shebang` (default `true`) -- preserve shebang `#!` in preamble (bash scripts)
- `width` (default 80) -- only takes effect when beautification is on, this
specifies an (orientative) line width that the beautifier will try to
obey. It refers to the width of the line text (excluding indentation).
It doesn't work very well currently, but it does make the code generated
by UglifyJS more readable.
- `wrap_iife` (default `false`) -- pass `true` to wrap immediately invoked
function expressions. See
[#640](https://github.com/mishoo/UglifyJS2/issues/640) for more details.
# Miscellaneous # Miscellaneous

View File

@@ -73,8 +73,6 @@ function OutputStream(options) {
shebang : true, shebang : true,
shorthand : undefined, shorthand : undefined,
source_map : null, source_map : null,
space_colon : true,
unescape_regexps : false,
width : 80, width : 80,
wrap_iife : false, wrap_iife : false,
}, true); }, true);
@@ -384,7 +382,7 @@ function OutputStream(options) {
function colon() { function colon() {
print(":"); print(":");
if (options.space_colon) space(); space();
}; };
var add_mapping = options.source_map ? function(token, name) { var add_mapping = options.source_map ? function(token, name) {
@@ -1630,45 +1628,14 @@ function OutputStream(options) {
} }
}); });
function regexp_safe_literal(code) {
return [
0x5c , // \
0x2f , // /
0x2e , // .
0x2b , // +
0x2a , // *
0x3f , // ?
0x28 , // (
0x29 , // )
0x5b , // [
0x5d , // ]
0x7b , // {
0x7d , // }
0x24 , // $
0x5e , // ^
0x3a , // :
0x7c , // |
0x21 , // !
0x0a , // \n
0x0d , // \r
0x00 , // \0
0xfeff , // Unicode BOM
0x2028 , // unicode "line separator"
0x2029 , // unicode "paragraph separator"
].indexOf(code) < 0;
};
DEFPRINT(AST_RegExp, function(self, output){ DEFPRINT(AST_RegExp, function(self, output){
var str = self.getValue().toString(); var regexp = self.getValue();
var str = regexp.toString();
if (regexp.raw_source) {
str = "/" + regexp.raw_source + str.slice(str.lastIndexOf("/"));
}
if (output.option("ascii_only")) { if (output.option("ascii_only")) {
str = output.to_ascii(str); str = output.to_ascii(str);
} else if (output.option("unescape_regexps")) {
str = str.split("\\\\").map(function(str){
return str.replace(/\\u[0-9a-fA-F]{4}|\\x[0-9a-fA-F]{2}/g, function(s){
var code = parseInt(s.substr(2), 16);
return regexp_safe_literal(code) ? String.fromCharCode(code) : s;
});
}).join("\\\\");
} }
output.print(str); output.print(str);
var p = output.parent(); var p = output.parent();

View File

@@ -611,31 +611,33 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
return name; return name;
}); });
var read_regexp = with_eof_error("Unterminated regular expression", function(regexp){ var read_regexp = with_eof_error("Unterminated regular expression", function(source) {
var prev_backslash = false, ch, in_class = false; var prev_backslash = false, ch, in_class = false;
while ((ch = next(true))) if (NEWLINE_CHARS(ch)) { while ((ch = next(true))) if (NEWLINE_CHARS(ch)) {
parse_error("Unexpected line terminator"); parse_error("Unexpected line terminator");
} else if (prev_backslash) { } else if (prev_backslash) {
regexp += "\\" + ch; source += "\\" + ch;
prev_backslash = false; prev_backslash = false;
} else if (ch == "[") { } else if (ch == "[") {
in_class = true; in_class = true;
regexp += ch; source += ch;
} else if (ch == "]" && in_class) { } else if (ch == "]" && in_class) {
in_class = false; in_class = false;
regexp += ch; source += ch;
} else if (ch == "/" && !in_class) { } else if (ch == "/" && !in_class) {
break; break;
} else if (ch == "\\") { } else if (ch == "\\") {
prev_backslash = true; prev_backslash = true;
} else { } else {
regexp += ch; source += ch;
} }
var mods = read_name(); var mods = read_name();
try { try {
return token("regexp", new RegExp(regexp, mods)); var regexp = new RegExp(source, mods);
regexp.raw_source = source;
return token("regexp", regexp);
} catch(e) { } catch(e) {
parse_error(e.message); parse_error(e.message);
} }
}); });

View File

@@ -4,7 +4,7 @@
"homepage": "https://github.com/mishoo/UglifyJS2/tree/harmony", "homepage": "https://github.com/mishoo/UglifyJS2/tree/harmony",
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)", "author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
"license": "BSD-2-Clause", "license": "BSD-2-Clause",
"version": "3.0.7", "version": "3.0.8",
"engines": { "engines": {
"node": ">=0.8.0" "node": ">=0.8.0"
}, },
@@ -34,7 +34,8 @@
}, },
"devDependencies": { "devDependencies": {
"acorn": "~5.0.3", "acorn": "~5.0.3",
"mocha": "~2.3.4" "mocha": "~2.3.4",
"semver": "~5.3.0"
}, },
"scripts": { "scripts": {
"test": "node test/run-tests.js" "test": "node test/run-tests.js"

View File

@@ -0,0 +1,12 @@
eval_let: {
input: {
eval("let a;");
console.log();
}
expect: {
eval("let a;");
console.log();
}
expect_stdout: ""
node_version: ">=6"
}

View File

@@ -8,3 +8,12 @@ octal_escape_sequence: {
var border_check = "\x20\x30\x38\x30\x00\x30\xc0\x30"; var border_check = "\x20\x30\x38\x30\x00\x30\xc0\x30";
} }
} }
issue_1929: {
input: {
function f(s) {
return s.split(/[\\/]/);
}
}
expect_exact: "function f(s){return s.split(/[\\\\/]/)}"
}

View File

@@ -5,6 +5,7 @@ var path = require("path");
var fs = require("fs"); var fs = require("fs");
var assert = require("assert"); var assert = require("assert");
var sandbox = require("./sandbox"); var sandbox = require("./sandbox");
var semver = require("semver");
var tests_dir = path.dirname(module.filename); var tests_dir = path.dirname(module.filename);
var failures = 0; var failures = 0;
@@ -164,7 +165,8 @@ function run_compress_tests() {
failed_files[file] = 1; failed_files[file] = 1;
} }
} }
if (test.expect_stdout) { if (test.expect_stdout
&& (!test.node_version || semver.satisfies(process.version, test.node_version))) {
var stdout = sandbox.run_code(input_code); var stdout = sandbox.run_code(input_code);
if (test.expect_stdout === true) { if (test.expect_stdout === true) {
test.expect_stdout = stdout; test.expect_stdout = stdout;
@@ -274,7 +276,14 @@ function parse_test(file) {
if (node instanceof U.AST_LabeledStatement) { if (node instanceof U.AST_LabeledStatement) {
var label = node.label; var label = node.label;
assert.ok( assert.ok(
["input", "expect", "expect_exact", "expect_warnings", "expect_stdout"].indexOf(label.name) >= 0, [
"input",
"expect",
"expect_exact",
"expect_warnings",
"expect_stdout",
"node_version",
].indexOf(label.name) >= 0,
tmpl("Unsupported label {name} [{line},{col}]", { tmpl("Unsupported label {name} [{line},{col}]", {
name: label.name, name: label.name,
line: label.start.line, line: label.start.line,
@@ -282,7 +291,7 @@ function parse_test(file) {
}) })
); );
var stat = node.body; var stat = node.body;
if (label.name == "expect_exact") { if (label.name == "expect_exact" || label.name == "node_version") {
test[label.name] = read_string(stat); test[label.name] = read_string(stat);
} else if (label.name == "expect_stdout") { } else if (label.name == "expect_stdout") {
if (stat.TYPE == "SimpleStatement" && stat.body instanceof U.AST_Boolean) { if (stat.TYPE == "SimpleStatement" && stat.body instanceof U.AST_Boolean) {

View File

@@ -1,3 +1,4 @@
var semver = require("semver");
var vm = require("vm"); var vm = require("vm");
function safe_log(arg, level) { function safe_log(arg, level) {
@@ -63,7 +64,7 @@ exports.run_code = function(code) {
process.stdout.write = original_write; process.stdout.write = original_write;
} }
}; };
exports.same_stdout = ~process.version.lastIndexOf("v0.12.", 0) ? function(expected, actual) { exports.same_stdout = semver.satisfies(process.version, "0.12") ? function(expected, actual) {
if (typeof expected != typeof actual) return false; if (typeof expected != typeof actual) return false;
if (typeof expected != "string") { if (typeof expected != "string") {
if (expected.name != actual.name) return false; if (expected.name != actual.name) return false;