Merge branch 'master' into harmony
This commit is contained in:
@@ -346,7 +346,7 @@ merge(Compressor.prototype, {
|
|||||||
if (ref.scope.uses_eval || ref.scope.uses_with) break;
|
if (ref.scope.uses_eval || ref.scope.uses_with) break;
|
||||||
|
|
||||||
// Constant single use vars can be replaced in any scope.
|
// Constant single use vars can be replaced in any scope.
|
||||||
if (var_decl.value.is_constant(compressor)) {
|
if (!(var_decl.value instanceof AST_RegExp) && var_decl.value.is_constant(compressor)) {
|
||||||
var ctt = new TreeTransformer(function(node) {
|
var ctt = new TreeTransformer(function(node) {
|
||||||
if (node === ref)
|
if (node === ref)
|
||||||
return replace_var(node, ctt.parent(), true);
|
return replace_var(node, ctt.parent(), true);
|
||||||
|
|||||||
@@ -520,7 +520,8 @@ function OutputStream(options) {
|
|||||||
|
|
||||||
PARENS([ AST_Unary, AST_Undefined ], function(output){
|
PARENS([ AST_Unary, AST_Undefined ], function(output){
|
||||||
var p = output.parent();
|
var p = output.parent();
|
||||||
return p instanceof AST_PropAccess && p.expression === this;
|
return p instanceof AST_PropAccess && p.expression === this
|
||||||
|
|| p instanceof AST_New;
|
||||||
});
|
});
|
||||||
|
|
||||||
PARENS(AST_Seq, function(output){
|
PARENS(AST_Seq, function(output){
|
||||||
@@ -1531,7 +1532,12 @@ function OutputStream(options) {
|
|||||||
|
|
||||||
// self should be AST_New. decide if we want to show parens or not.
|
// self should be AST_New. decide if we want to show parens or not.
|
||||||
function no_constructor_parens(self, output) {
|
function no_constructor_parens(self, output) {
|
||||||
return self.args.length == 0 && !output.option("beautify");
|
return self.args.length == 0 && !output.option("beautify") ||
|
||||||
|
!(self.expression instanceof AST_SymbolRef ||
|
||||||
|
self.expression instanceof AST_Call ||
|
||||||
|
self.expression instanceof AST_Function ||
|
||||||
|
self.expression instanceof AST_Assign
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
function best_of(a) {
|
function best_of(a) {
|
||||||
|
|||||||
@@ -501,6 +501,8 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
|
|||||||
break;
|
break;
|
||||||
} else if (ch == "\\") {
|
} else if (ch == "\\") {
|
||||||
prev_backslash = true;
|
prev_backslash = true;
|
||||||
|
} else if ("\r\n\u2028\u2029".indexOf(ch) >= 0) {
|
||||||
|
parse_error("Unexpected line terminator");
|
||||||
} else {
|
} else {
|
||||||
regexp += ch;
|
regexp += ch;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1153,3 +1153,59 @@ collapse_vars_short_circuited_conditions: {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
collapse_vars_regexp: {
|
||||||
|
options = {
|
||||||
|
collapse_vars: true,
|
||||||
|
loops: false,
|
||||||
|
sequences: true,
|
||||||
|
dead_code: true,
|
||||||
|
conditionals: true,
|
||||||
|
comparisons: true,
|
||||||
|
evaluate: true,
|
||||||
|
booleans: true,
|
||||||
|
unused: true,
|
||||||
|
hoist_funs: true,
|
||||||
|
keep_fargs: true,
|
||||||
|
if_return: true,
|
||||||
|
join_vars: true,
|
||||||
|
cascade: true,
|
||||||
|
side_effects: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f1() {
|
||||||
|
var k = 9;
|
||||||
|
var rx = /[A-Z]+/;
|
||||||
|
return [rx, k];
|
||||||
|
}
|
||||||
|
function f2() {
|
||||||
|
var rx = /[abc123]+/;
|
||||||
|
return function(s) {
|
||||||
|
return rx.exec(s);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
(function(){
|
||||||
|
var result;
|
||||||
|
var s = 'acdabcdeabbb';
|
||||||
|
var rx = /ab*/g;
|
||||||
|
while (result = rx.exec(s)) {
|
||||||
|
console.log(result[0]);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f1() {
|
||||||
|
return [/[A-Z]+/, 9];
|
||||||
|
}
|
||||||
|
function f2() {
|
||||||
|
var rx = /[abc123]+/;
|
||||||
|
return function(s) {
|
||||||
|
return rx.exec(s);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
(function(){
|
||||||
|
var result, rx = /ab*/g;
|
||||||
|
while (result = rx.exec('acdabcdeabbb'))
|
||||||
|
console.log(result[0]);
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,3 +10,11 @@ new_statement: {
|
|||||||
}
|
}
|
||||||
expect_exact: "new x(1);new x(1)(2);new x(1)(2)(3);new new x(1);new new x(1)(2);new new x(1)(2);(new new x(1))(2);"
|
expect_exact: "new x(1);new x(1)(2);new x(1)(2)(3);new new x(1);new new x(1)(2);new new x(1)(2);(new new x(1))(2);"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
new_with_rewritten_true_value: {
|
||||||
|
options = { booleans: true }
|
||||||
|
input: {
|
||||||
|
new true;
|
||||||
|
}
|
||||||
|
expect_exact: "new(!0);"
|
||||||
|
}
|
||||||
|
|||||||
@@ -30,5 +30,27 @@ describe("line-endings", function() {
|
|||||||
var result = Uglify.minify(js, options);
|
var result = Uglify.minify(js, options);
|
||||||
assert.strictEqual(result.code, expected_code);
|
assert.strictEqual(result.code, expected_code);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should not allow line terminators in regexp", function() {
|
||||||
|
var inputs = [
|
||||||
|
"/\n/",
|
||||||
|
"/\r/",
|
||||||
|
"/\u2028/",
|
||||||
|
"/\u2029/",
|
||||||
|
"/someRandomTextLike[]()*AndThen\n/"
|
||||||
|
]
|
||||||
|
var test = function(input) {
|
||||||
|
return function() {
|
||||||
|
Uglify.parse(input);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var fail = function(e) {
|
||||||
|
return e instanceof Uglify.JS_Parse_Error &&
|
||||||
|
e.message === "Unexpected line terminator";
|
||||||
|
}
|
||||||
|
for (var i = 0; i < inputs.length; i++) {
|
||||||
|
assert.throws(test(inputs[i]), fail);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
34
test/mocha/new.js
Normal file
34
test/mocha/new.js
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
var assert = require("assert");
|
||||||
|
var uglify = require("../../");
|
||||||
|
|
||||||
|
describe("New", function() {
|
||||||
|
it("Should attach callback parens after some tokens", function() {
|
||||||
|
var tests = [
|
||||||
|
"new x(1);",
|
||||||
|
"new (function(foo){this.foo=foo;})(1);",
|
||||||
|
"new true;",
|
||||||
|
"new (0);",
|
||||||
|
"new (!0);",
|
||||||
|
"new (bar = function(foo) {this.foo=foo;})(123);"
|
||||||
|
];
|
||||||
|
var expected = [
|
||||||
|
"new x(1);",
|
||||||
|
"new function(foo) {\n this.foo = foo;\n}(1);",
|
||||||
|
"new true;",
|
||||||
|
"new 0;",
|
||||||
|
"new (!0);",
|
||||||
|
"new (bar = function(foo) {\n this.foo = foo;\n})(123);"
|
||||||
|
];
|
||||||
|
for (var i = 0; i < tests.length; i++) {
|
||||||
|
assert.strictEqual(
|
||||||
|
uglify.minify(tests[i], {
|
||||||
|
fromString: true,
|
||||||
|
output: {beautify: true},
|
||||||
|
compress: false,
|
||||||
|
mangle: false
|
||||||
|
}).code,
|
||||||
|
expected[i]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -163,9 +163,16 @@ function run_compress_tests() {
|
|||||||
|
|
||||||
function parse_test(file) {
|
function parse_test(file) {
|
||||||
var script = fs.readFileSync(file, "utf8");
|
var script = fs.readFileSync(file, "utf8");
|
||||||
|
// TODO try/catch can be removed after fixing https://github.com/mishoo/UglifyJS2/issues/348
|
||||||
|
try {
|
||||||
var ast = U.parse(script, {
|
var ast = U.parse(script, {
|
||||||
filename: file
|
filename: file
|
||||||
});
|
});
|
||||||
|
} catch (e) {
|
||||||
|
console.log("Caught error while parsing tests in " + file + "\n");
|
||||||
|
console.log(e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
var tests = {};
|
var tests = {};
|
||||||
var tw = new U.TreeWalker(function(node, descend){
|
var tw = new U.TreeWalker(function(node, descend){
|
||||||
if (node instanceof U.AST_LabeledStatement
|
if (node instanceof U.AST_LabeledStatement
|
||||||
|
|||||||
@@ -14,5 +14,6 @@ exports["merge"] = merge;
|
|||||||
exports["parse"] = parse;
|
exports["parse"] = parse;
|
||||||
exports["push_uniq"] = push_uniq;
|
exports["push_uniq"] = push_uniq;
|
||||||
exports["string_template"] = string_template;
|
exports["string_template"] = string_template;
|
||||||
|
exports["tokenizer"] = tokenizer;
|
||||||
exports["is_identifier"] = is_identifier;
|
exports["is_identifier"] = is_identifier;
|
||||||
exports["SymbolDef"] = SymbolDef;
|
exports["SymbolDef"] = SymbolDef;
|
||||||
|
|||||||
Reference in New Issue
Block a user