Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0af80eca22 | ||
|
|
23876a84a5 | ||
|
|
092d0275a0 | ||
|
|
06296bee7f | ||
|
|
3818a9e9c1 | ||
|
|
75e2748b16 | ||
|
|
a7c987ad2b | ||
|
|
957c54bc87 | ||
|
|
4cbf5a7821 | ||
|
|
93d4224072 | ||
|
|
6cd580dc23 | ||
|
|
ecb63ad8bc | ||
|
|
1ca43bcca1 | ||
|
|
3ee1464aa4 | ||
|
|
24967b8be8 | ||
|
|
a8c67ea353 | ||
|
|
d0b0aecfc5 | ||
|
|
9f5a6029a3 | ||
|
|
4027a0c962 |
@@ -1,5 +1,4 @@
|
|||||||
language: node_js
|
language: node_js
|
||||||
before_install: "npm install -g npm"
|
|
||||||
node_js:
|
node_js:
|
||||||
- "0.10"
|
- "0.10"
|
||||||
- "0.12"
|
- "0.12"
|
||||||
|
|||||||
16
README.md
16
README.md
@@ -353,6 +353,9 @@ to set `true`; it's effectively a shortcut for `foo=true`).
|
|||||||
- `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)`
|
||||||
|
|
||||||
|
- `unsafe_regexp` (default: false) -- enable substitutions of variables with
|
||||||
|
`RegExp` values the same way as if they are constants.
|
||||||
|
|
||||||
- `conditionals` -- apply optimizations for `if`-s and conditional
|
- `conditionals` -- apply optimizations for `if`-s and conditional
|
||||||
expressions
|
expressions
|
||||||
|
|
||||||
@@ -436,16 +439,17 @@ to set `true`; it's effectively a shortcut for `foo=true`).
|
|||||||
compressor from discarding function names. Useful for code relying on
|
compressor from discarding function names. Useful for code relying on
|
||||||
`Function.prototype.name`. See also: the `keep_fnames` [mangle option](#mangle).
|
`Function.prototype.name`. See also: the `keep_fnames` [mangle option](#mangle).
|
||||||
|
|
||||||
- `passes` -- default `1`. Number of times to run compress. Use an
|
- `passes` -- default `1`. Number of times to run compress with a maximum of 3.
|
||||||
integer argument larger than 1 to further reduce code size in some cases.
|
In some cases more than one pass leads to further compressed code. Keep in
|
||||||
Note: raising the number of passes will increase uglify compress time.
|
mind more passes will take more time.
|
||||||
|
|
||||||
- `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 is marked as "pure" via the comment annotation
|
functions marked as "pure". A function call is marked as "pure" if a comment
|
||||||
`/* @__PURE__ */` or `/* #__PURE__ */`)
|
annotation `/*@__PURE__*/` or `/*#__PURE__*/` immediately precedes the call. For
|
||||||
|
example: `/*@__PURE__*/foo();`
|
||||||
|
|
||||||
|
|
||||||
### The `unsafe` option
|
### The `unsafe` option
|
||||||
|
|||||||
16
bin/uglifyjs
16
bin/uglifyjs
@@ -367,19 +367,19 @@ var index = 0;
|
|||||||
if (ex instanceof UglifyJS.JS_Parse_Error) {
|
if (ex instanceof UglifyJS.JS_Parse_Error) {
|
||||||
print_error("Parse error at " + file + ":" + ex.line + "," + ex.col);
|
print_error("Parse error at " + file + ":" + ex.line + "," + ex.col);
|
||||||
var col = ex.col;
|
var col = ex.col;
|
||||||
var line = code.split(/\r?\n/)[ex.line - (col ? 1 : 2)];
|
var lines = code.split(/\r?\n/);
|
||||||
|
var line = lines[ex.line - 1];
|
||||||
|
if (!line && !col) {
|
||||||
|
line = lines[ex.line - 2];
|
||||||
|
col = line.length;
|
||||||
|
}
|
||||||
if (line) {
|
if (line) {
|
||||||
if (col > 40) {
|
if (col > 40) {
|
||||||
line = line.slice(col - 40);
|
line = line.slice(col - 40);
|
||||||
col = 40;
|
col = 40;
|
||||||
}
|
}
|
||||||
if (col) {
|
print_error(line.slice(0, 80));
|
||||||
print_error(line.slice(0, 80));
|
print_error(line.slice(0, col).replace(/\S/g, " ") + "^");
|
||||||
print_error(line.slice(0, col).replace(/\S/g, " ") + "^");
|
|
||||||
} else {
|
|
||||||
print_error(line.slice(-40));
|
|
||||||
print_error(line.slice(-40).replace(/\S/g, " ") + "^");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
print_error(ex.stack);
|
print_error(ex.stack);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
|||||||
@@ -798,8 +798,8 @@ var AST_Object = DEFNODE("Object", "properties", {
|
|||||||
var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", {
|
var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", {
|
||||||
$documentation: "Base class for literal object properties",
|
$documentation: "Base class for literal object properties",
|
||||||
$propdoc: {
|
$propdoc: {
|
||||||
key: "[string] the property name converted to a string for ObjectKeyVal. For setters and getters this is an arbitrary AST_Node.",
|
key: "[string] the property name converted to a string for ObjectKeyVal. For setters and getters this is an AST_SymbolAccessor.",
|
||||||
value: "[AST_Node] property value. For setters and getters this is an AST_Function."
|
value: "[AST_Node] property value. For setters and getters this is an AST_Accessor."
|
||||||
},
|
},
|
||||||
_walk: function(visitor) {
|
_walk: function(visitor) {
|
||||||
return visitor._visit(this, function(){
|
return visitor._visit(this, function(){
|
||||||
|
|||||||
113
lib/compress.js
113
lib/compress.js
@@ -84,6 +84,7 @@ function Compressor(options, false_by_default) {
|
|||||||
unsafe_comps : false,
|
unsafe_comps : false,
|
||||||
unsafe_math : false,
|
unsafe_math : false,
|
||||||
unsafe_proto : false,
|
unsafe_proto : false,
|
||||||
|
unsafe_regexp : false,
|
||||||
unused : !false_by_default,
|
unused : !false_by_default,
|
||||||
warnings : true,
|
warnings : true,
|
||||||
}, true);
|
}, true);
|
||||||
@@ -316,25 +317,55 @@ merge(Compressor.prototype, {
|
|||||||
safe_ids = save_ids;
|
safe_ids = save_ids;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
var iife;
|
if (node instanceof AST_Function) {
|
||||||
if (node instanceof AST_Function
|
push();
|
||||||
&& !node.name
|
var iife;
|
||||||
&& (iife = tw.parent()) instanceof AST_Call
|
if (!node.name
|
||||||
&& iife.expression === node) {
|
&& (iife = tw.parent()) instanceof AST_Call
|
||||||
// Virtually turn IIFE parameters into variable definitions:
|
&& iife.expression === node) {
|
||||||
// (function(a,b) {...})(c,d) => (function() {var a=c,b=d; ...})()
|
// Virtually turn IIFE parameters into variable definitions:
|
||||||
// So existing transformation rules can work on them.
|
// (function(a,b) {...})(c,d) => (function() {var a=c,b=d; ...})()
|
||||||
node.argnames.forEach(function(arg, i) {
|
// So existing transformation rules can work on them.
|
||||||
var d = arg.definition();
|
node.argnames.forEach(function(arg, i) {
|
||||||
if (!node.uses_arguments && d.fixed === undefined) {
|
var d = arg.definition();
|
||||||
d.fixed = function() {
|
if (!node.uses_arguments && d.fixed === undefined) {
|
||||||
return iife.args[i] || make_node(AST_Undefined, iife);
|
d.fixed = function() {
|
||||||
};
|
return iife.args[i] || make_node(AST_Undefined, iife);
|
||||||
mark(d, true);
|
};
|
||||||
} else {
|
mark(d, true);
|
||||||
d.fixed = false;
|
} else {
|
||||||
}
|
d.fixed = false;
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
descend();
|
||||||
|
pop();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (node instanceof AST_Accessor) {
|
||||||
|
var save_ids = safe_ids;
|
||||||
|
safe_ids = Object.create(null);
|
||||||
|
descend();
|
||||||
|
safe_ids = save_ids;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (node instanceof AST_Binary
|
||||||
|
&& (node.operator == "&&" || node.operator == "||")) {
|
||||||
|
node.left.walk(tw);
|
||||||
|
push();
|
||||||
|
node.right.walk(tw);
|
||||||
|
pop();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (node instanceof AST_Conditional) {
|
||||||
|
node.condition.walk(tw);
|
||||||
|
push();
|
||||||
|
node.consequent.walk(tw);
|
||||||
|
pop();
|
||||||
|
push();
|
||||||
|
node.alternative.walk(tw);
|
||||||
|
pop();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
if (node instanceof AST_If || node instanceof AST_DWLoop) {
|
if (node instanceof AST_If || node instanceof AST_DWLoop) {
|
||||||
node.condition.walk(tw);
|
node.condition.walk(tw);
|
||||||
@@ -1195,12 +1226,12 @@ merge(Compressor.prototype, {
|
|||||||
&& !node.expression.has_side_effects(compressor);
|
&& !node.expression.has_side_effects(compressor);
|
||||||
}
|
}
|
||||||
|
|
||||||
// may_eq_null()
|
// may_throw_on_access()
|
||||||
// returns true if this node may evaluate to null or undefined
|
// returns true if this node may be null, undefined or contain `AST_Accessor`
|
||||||
(function(def) {
|
(function(def) {
|
||||||
AST_Node.DEFMETHOD("may_eq_null", function(compressor) {
|
AST_Node.DEFMETHOD("may_throw_on_access", function(compressor) {
|
||||||
var pure_getters = compressor.option("pure_getters");
|
var pure_getters = compressor.option("pure_getters");
|
||||||
return !pure_getters || this._eq_null(pure_getters);
|
return !pure_getters || this._throw_on_access(pure_getters);
|
||||||
});
|
});
|
||||||
|
|
||||||
function is_strict(pure_getters) {
|
function is_strict(pure_getters) {
|
||||||
@@ -1212,7 +1243,12 @@ merge(Compressor.prototype, {
|
|||||||
def(AST_Undefined, return_true);
|
def(AST_Undefined, return_true);
|
||||||
def(AST_Constant, return_false);
|
def(AST_Constant, return_false);
|
||||||
def(AST_Array, return_false);
|
def(AST_Array, return_false);
|
||||||
def(AST_Object, return_false);
|
def(AST_Object, function(pure_getters) {
|
||||||
|
if (!is_strict(pure_getters)) return false;
|
||||||
|
for (var i = this.properties.length; --i >=0;)
|
||||||
|
if (this.properties[i].value instanceof AST_Accessor) return true;
|
||||||
|
return false;
|
||||||
|
});
|
||||||
def(AST_Function, return_false);
|
def(AST_Function, return_false);
|
||||||
def(AST_UnaryPostfix, return_false);
|
def(AST_UnaryPostfix, return_false);
|
||||||
def(AST_UnaryPrefix, function() {
|
def(AST_UnaryPrefix, function() {
|
||||||
@@ -1221,33 +1257,33 @@ merge(Compressor.prototype, {
|
|||||||
def(AST_Binary, function(pure_getters) {
|
def(AST_Binary, function(pure_getters) {
|
||||||
switch (this.operator) {
|
switch (this.operator) {
|
||||||
case "&&":
|
case "&&":
|
||||||
return this.left._eq_null(pure_getters);
|
return this.left._throw_on_access(pure_getters);
|
||||||
case "||":
|
case "||":
|
||||||
return this.left._eq_null(pure_getters)
|
return this.left._throw_on_access(pure_getters)
|
||||||
&& this.right._eq_null(pure_getters);
|
&& this.right._throw_on_access(pure_getters);
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
def(AST_Assign, function(pure_getters) {
|
def(AST_Assign, function(pure_getters) {
|
||||||
return this.operator == "="
|
return this.operator == "="
|
||||||
&& this.right._eq_null(pure_getters);
|
&& this.right._throw_on_access(pure_getters);
|
||||||
})
|
})
|
||||||
def(AST_Conditional, function(pure_getters) {
|
def(AST_Conditional, function(pure_getters) {
|
||||||
return this.consequent._eq_null(pure_getters)
|
return this.consequent._throw_on_access(pure_getters)
|
||||||
|| this.alternative._eq_null(pure_getters);
|
|| this.alternative._throw_on_access(pure_getters);
|
||||||
})
|
})
|
||||||
def(AST_Seq, function(pure_getters) {
|
def(AST_Seq, function(pure_getters) {
|
||||||
return this.cdr._eq_null(pure_getters);
|
return this.cdr._throw_on_access(pure_getters);
|
||||||
});
|
});
|
||||||
def(AST_SymbolRef, function(pure_getters) {
|
def(AST_SymbolRef, function(pure_getters) {
|
||||||
if (this.is_undefined) return true;
|
if (this.is_undefined) return true;
|
||||||
if (!is_strict(pure_getters)) return false;
|
if (!is_strict(pure_getters)) return false;
|
||||||
var fixed = this.fixed_value();
|
var fixed = this.fixed_value();
|
||||||
return !fixed || fixed._eq_null(pure_getters);
|
return !fixed || fixed._throw_on_access(pure_getters);
|
||||||
});
|
});
|
||||||
})(function(node, func) {
|
})(function(node, func) {
|
||||||
node.DEFMETHOD("_eq_null", func);
|
node.DEFMETHOD("_throw_on_access", func);
|
||||||
});
|
});
|
||||||
|
|
||||||
/* -----[ boolean/negation helpers ]----- */
|
/* -----[ boolean/negation helpers ]----- */
|
||||||
@@ -1787,11 +1823,11 @@ merge(Compressor.prototype, {
|
|||||||
return any(this.elements, compressor);
|
return any(this.elements, compressor);
|
||||||
});
|
});
|
||||||
def(AST_Dot, function(compressor){
|
def(AST_Dot, function(compressor){
|
||||||
return this.expression.may_eq_null(compressor)
|
return this.expression.may_throw_on_access(compressor)
|
||||||
|| this.expression.has_side_effects(compressor);
|
|| this.expression.has_side_effects(compressor);
|
||||||
});
|
});
|
||||||
def(AST_Sub, function(compressor){
|
def(AST_Sub, function(compressor){
|
||||||
return this.expression.may_eq_null(compressor)
|
return this.expression.may_throw_on_access(compressor)
|
||||||
|| this.expression.has_side_effects(compressor)
|
|| this.expression.has_side_effects(compressor)
|
||||||
|| this.property.has_side_effects(compressor);
|
|| this.property.has_side_effects(compressor);
|
||||||
});
|
});
|
||||||
@@ -2295,6 +2331,7 @@ merge(Compressor.prototype, {
|
|||||||
var args = trim(this.args, compressor, first_in_statement);
|
var args = trim(this.args, compressor, first_in_statement);
|
||||||
return args && AST_Seq.from_array(args);
|
return args && AST_Seq.from_array(args);
|
||||||
});
|
});
|
||||||
|
def(AST_Accessor, return_null);
|
||||||
def(AST_Function, return_null);
|
def(AST_Function, return_null);
|
||||||
def(AST_Binary, function(compressor, first_in_statement){
|
def(AST_Binary, function(compressor, first_in_statement){
|
||||||
var right = this.right.drop_side_effect_free(compressor);
|
var right = this.right.drop_side_effect_free(compressor);
|
||||||
@@ -2365,11 +2402,11 @@ merge(Compressor.prototype, {
|
|||||||
return values && AST_Seq.from_array(values);
|
return values && AST_Seq.from_array(values);
|
||||||
});
|
});
|
||||||
def(AST_Dot, function(compressor, first_in_statement){
|
def(AST_Dot, function(compressor, first_in_statement){
|
||||||
if (this.expression.may_eq_null(compressor)) return this;
|
if (this.expression.may_throw_on_access(compressor)) return this;
|
||||||
return this.expression.drop_side_effect_free(compressor, first_in_statement);
|
return this.expression.drop_side_effect_free(compressor, first_in_statement);
|
||||||
});
|
});
|
||||||
def(AST_Sub, function(compressor, first_in_statement){
|
def(AST_Sub, function(compressor, first_in_statement){
|
||||||
if (this.expression.may_eq_null(compressor)) return this;
|
if (this.expression.may_throw_on_access(compressor)) return this;
|
||||||
var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
|
var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
|
||||||
if (!expression) return this.property.drop_side_effect_free(compressor, first_in_statement);
|
if (!expression) return this.property.drop_side_effect_free(compressor, first_in_statement);
|
||||||
var property = this.property.drop_side_effect_free(compressor);
|
var property = this.property.drop_side_effect_free(compressor);
|
||||||
@@ -3644,7 +3681,7 @@ merge(Compressor.prototype, {
|
|||||||
if (fixed) {
|
if (fixed) {
|
||||||
if (d.should_replace === undefined) {
|
if (d.should_replace === undefined) {
|
||||||
var init = fixed.evaluate(compressor);
|
var init = fixed.evaluate(compressor);
|
||||||
if (init !== fixed) {
|
if (init !== fixed && (compressor.option("unsafe_regexp") || !(init instanceof RegExp))) {
|
||||||
init = make_node_from_constant(init, fixed);
|
init = make_node_from_constant(init, fixed);
|
||||||
var value = init.optimize(compressor).print_to_string().length;
|
var value = init.optimize(compressor).print_to_string().length;
|
||||||
var fn;
|
var fn;
|
||||||
|
|||||||
@@ -111,23 +111,19 @@
|
|||||||
},
|
},
|
||||||
Property: function(M) {
|
Property: function(M) {
|
||||||
var key = M.key;
|
var key = M.key;
|
||||||
var name = key.type == "Identifier" ? key.name : key.value;
|
|
||||||
var args = {
|
var args = {
|
||||||
start : my_start_token(key),
|
start : my_start_token(key),
|
||||||
end : my_end_token(M.value),
|
end : my_end_token(M.value),
|
||||||
key : name,
|
key : key.type == "Identifier" ? key.name : key.value,
|
||||||
value : from_moz(M.value)
|
value : from_moz(M.value)
|
||||||
};
|
};
|
||||||
switch (M.kind) {
|
if (M.kind == "init") return new AST_ObjectKeyVal(args);
|
||||||
case "init":
|
args.key = new AST_SymbolAccessor({
|
||||||
return new AST_ObjectKeyVal(args);
|
name: args.key
|
||||||
case "set":
|
});
|
||||||
args.value.name = from_moz(key);
|
args.value = new AST_Accessor(args.value);
|
||||||
return new AST_ObjectSetter(args);
|
if (M.kind == "get") return new AST_ObjectGetter(args);
|
||||||
case "get":
|
if (M.kind == "set") return new AST_ObjectSetter(args);
|
||||||
args.value.name = from_moz(key);
|
|
||||||
return new AST_ObjectGetter(args);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
ArrayExpression: function(M) {
|
ArrayExpression: function(M) {
|
||||||
return new AST_Array({
|
return new AST_Array({
|
||||||
@@ -256,10 +252,7 @@
|
|||||||
map("CallExpression", AST_Call, "callee>expression, arguments@args");
|
map("CallExpression", AST_Call, "callee>expression, arguments@args");
|
||||||
|
|
||||||
def_to_moz(AST_Toplevel, function To_Moz_Program(M) {
|
def_to_moz(AST_Toplevel, function To_Moz_Program(M) {
|
||||||
return {
|
return to_moz_scope("Program", M);
|
||||||
type: "Program",
|
|
||||||
body: M.body.map(to_moz)
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
def_to_moz(AST_Defun, function To_Moz_FunctionDeclaration(M) {
|
def_to_moz(AST_Defun, function To_Moz_FunctionDeclaration(M) {
|
||||||
@@ -267,7 +260,7 @@
|
|||||||
type: "FunctionDeclaration",
|
type: "FunctionDeclaration",
|
||||||
id: to_moz(M.name),
|
id: to_moz(M.name),
|
||||||
params: M.argnames.map(to_moz),
|
params: M.argnames.map(to_moz),
|
||||||
body: to_moz_block(M)
|
body: to_moz_scope("BlockStatement", M)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -276,7 +269,7 @@
|
|||||||
type: "FunctionExpression",
|
type: "FunctionExpression",
|
||||||
id: to_moz(M.name),
|
id: to_moz(M.name),
|
||||||
params: M.argnames.map(to_moz),
|
params: M.argnames.map(to_moz),
|
||||||
body: to_moz_block(M)
|
body: to_moz_scope("BlockStatement", M)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -382,11 +375,10 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
def_to_moz(AST_ObjectProperty, function To_Moz_Property(M) {
|
def_to_moz(AST_ObjectProperty, function To_Moz_Property(M) {
|
||||||
var key = (
|
var key = {
|
||||||
is_identifier(M.key)
|
type: "Literal",
|
||||||
? {type: "Identifier", name: M.key}
|
value: M.key instanceof AST_SymbolAccessor ? M.key.name : M.key
|
||||||
: {type: "Literal", value: M.key}
|
};
|
||||||
);
|
|
||||||
var kind;
|
var kind;
|
||||||
if (M instanceof AST_ObjectKeyVal) {
|
if (M instanceof AST_ObjectKeyVal) {
|
||||||
kind = "init";
|
kind = "init";
|
||||||
@@ -547,8 +539,8 @@
|
|||||||
moz_to_me = new Function("U2", "my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")(
|
moz_to_me = new Function("U2", "my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")(
|
||||||
exports, my_start_token, my_end_token, from_moz
|
exports, my_start_token, my_end_token, from_moz
|
||||||
);
|
);
|
||||||
me_to_moz = new Function("to_moz", "to_moz_block", "return(" + me_to_moz + ")")(
|
me_to_moz = new Function("to_moz", "to_moz_block", "to_moz_scope", "return(" + me_to_moz + ")")(
|
||||||
to_moz, to_moz_block
|
to_moz, to_moz_block, to_moz_scope
|
||||||
);
|
);
|
||||||
MOZ_TO_ME[moztype] = moz_to_me;
|
MOZ_TO_ME[moztype] = moz_to_me;
|
||||||
def_to_moz(mytype, me_to_moz);
|
def_to_moz(mytype, me_to_moz);
|
||||||
@@ -606,4 +598,14 @@
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function to_moz_scope(type, node) {
|
||||||
|
var body = node.body.map(to_moz);
|
||||||
|
if (node.body[0] instanceof AST_SimpleStatement && node.body[0].body instanceof AST_String) {
|
||||||
|
body.unshift(to_moz(new AST_EmptyStatement(node.body[0])));
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
type: type,
|
||||||
|
body: body
|
||||||
|
};
|
||||||
|
};
|
||||||
})();
|
})();
|
||||||
|
|||||||
@@ -1207,9 +1207,8 @@ function OutputStream(options) {
|
|||||||
});
|
});
|
||||||
else output.print("{}");
|
else output.print("{}");
|
||||||
});
|
});
|
||||||
DEFPRINT(AST_ObjectKeyVal, function(self, output){
|
|
||||||
var key = self.key;
|
function print_property_name(key, quote, output) {
|
||||||
var quote = self.quote;
|
|
||||||
if (output.option("quote_keys")) {
|
if (output.option("quote_keys")) {
|
||||||
output.print_string(key + "");
|
output.print_string(key + "");
|
||||||
} else if ((typeof key == "number"
|
} else if ((typeof key == "number"
|
||||||
@@ -1226,20 +1225,24 @@ function OutputStream(options) {
|
|||||||
} else {
|
} else {
|
||||||
output.print_string(key, quote);
|
output.print_string(key, quote);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DEFPRINT(AST_ObjectKeyVal, function(self, output){
|
||||||
|
print_property_name(self.key, self.quote, output);
|
||||||
output.colon();
|
output.colon();
|
||||||
self.value.print(output);
|
self.value.print(output);
|
||||||
});
|
});
|
||||||
DEFPRINT(AST_ObjectSetter, function(self, output){
|
AST_ObjectProperty.DEFMETHOD("_print_getter_setter", function(type, output) {
|
||||||
output.print("set");
|
output.print(type);
|
||||||
output.space();
|
output.space();
|
||||||
self.key.print(output);
|
print_property_name(this.key.name, this.quote, output);
|
||||||
self.value._do_print(output, true);
|
this.value._do_print(output, true);
|
||||||
|
});
|
||||||
|
DEFPRINT(AST_ObjectSetter, function(self, output){
|
||||||
|
self._print_getter_setter("set", output);
|
||||||
});
|
});
|
||||||
DEFPRINT(AST_ObjectGetter, function(self, output){
|
DEFPRINT(AST_ObjectGetter, function(self, output){
|
||||||
output.print("get");
|
self._print_getter_setter("get", output);
|
||||||
output.space();
|
|
||||||
self.key.print(output);
|
|
||||||
self.value._do_print(output, true);
|
|
||||||
});
|
});
|
||||||
DEFPRINT(AST_Symbol, function(self, output){
|
DEFPRINT(AST_Symbol, function(self, output){
|
||||||
var def = self.definition();
|
var def = self.definition();
|
||||||
|
|||||||
113
lib/parse.js
113
lib/parse.js
@@ -111,7 +111,7 @@ var WHITESPACE_CHARS = makePredicate(characters(" \u00a0\n\r\t\f\u000b\u200b\u20
|
|||||||
|
|
||||||
var NEWLINE_CHARS = makePredicate(characters("\n\r\u2028\u2029"));
|
var NEWLINE_CHARS = makePredicate(characters("\n\r\u2028\u2029"));
|
||||||
|
|
||||||
var PUNC_BEFORE_EXPRESSION = makePredicate(characters("[{(,.;:"));
|
var PUNC_BEFORE_EXPRESSION = makePredicate(characters("[{(,;:"));
|
||||||
|
|
||||||
var PUNC_CHARS = makePredicate(characters("[]{}(),;:"));
|
var PUNC_CHARS = makePredicate(characters("[]{}(),;:"));
|
||||||
|
|
||||||
@@ -285,7 +285,11 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
|
|||||||
S.regex_allowed = ((type == "operator" && !UNARY_POSTFIX(value)) ||
|
S.regex_allowed = ((type == "operator" && !UNARY_POSTFIX(value)) ||
|
||||||
(type == "keyword" && KEYWORDS_BEFORE_EXPRESSION(value)) ||
|
(type == "keyword" && KEYWORDS_BEFORE_EXPRESSION(value)) ||
|
||||||
(type == "punc" && PUNC_BEFORE_EXPRESSION(value)));
|
(type == "punc" && PUNC_BEFORE_EXPRESSION(value)));
|
||||||
prev_was_dot = (type == "punc" && value == ".");
|
if (type == "punc" && value == ".") {
|
||||||
|
prev_was_dot = true;
|
||||||
|
} else if (!is_comment) {
|
||||||
|
prev_was_dot = false;
|
||||||
|
}
|
||||||
var ret = {
|
var ret = {
|
||||||
type : type,
|
type : type,
|
||||||
value : value,
|
value : value,
|
||||||
@@ -803,28 +807,23 @@ function parse($TEXT, options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var statement = embed_tokens(function() {
|
var statement = embed_tokens(function() {
|
||||||
var tmp;
|
|
||||||
handle_regexp();
|
handle_regexp();
|
||||||
switch (S.token.type) {
|
switch (S.token.type) {
|
||||||
case "string":
|
case "string":
|
||||||
var dir = false;
|
if (S.in_directives) {
|
||||||
if (S.in_directives === true) {
|
var token = peek();
|
||||||
if ((is_token(peek(), "punc", ";") || peek().nlb) && S.token.raw.indexOf("\\") === -1) {
|
if (S.token.raw.indexOf("\\") == -1
|
||||||
|
&& (token.nlb
|
||||||
|
|| is_token(token, "eof")
|
||||||
|
|| is_token(token, "punc", ";")
|
||||||
|
|| is_token(token, "punc", "}"))) {
|
||||||
S.input.add_directive(S.token.value);
|
S.input.add_directive(S.token.value);
|
||||||
} else {
|
} else {
|
||||||
S.in_directives = false;
|
S.in_directives = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var dir = S.in_directives, stat = simple_statement();
|
var dir = S.in_directives, stat = simple_statement();
|
||||||
if (dir) {
|
return dir ? new AST_Directive(stat.body) : stat;
|
||||||
return new AST_Directive({
|
|
||||||
start : stat.body.start,
|
|
||||||
end : stat.body.end,
|
|
||||||
quote : stat.body.quote,
|
|
||||||
value : stat.body.value,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return stat;
|
|
||||||
case "num":
|
case "num":
|
||||||
case "regexp":
|
case "regexp":
|
||||||
case "operator":
|
case "operator":
|
||||||
@@ -856,75 +855,103 @@ function parse($TEXT, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "keyword":
|
case "keyword":
|
||||||
switch (tmp = S.token.value, next(), tmp) {
|
switch (S.token.value) {
|
||||||
case "break":
|
case "break":
|
||||||
|
next();
|
||||||
return break_cont(AST_Break);
|
return break_cont(AST_Break);
|
||||||
|
|
||||||
case "continue":
|
case "continue":
|
||||||
|
next();
|
||||||
return break_cont(AST_Continue);
|
return break_cont(AST_Continue);
|
||||||
|
|
||||||
case "debugger":
|
case "debugger":
|
||||||
|
next();
|
||||||
semicolon();
|
semicolon();
|
||||||
return new AST_Debugger();
|
return new AST_Debugger();
|
||||||
|
|
||||||
case "do":
|
case "do":
|
||||||
|
next();
|
||||||
|
var body = in_loop(statement);
|
||||||
|
expect_token("keyword", "while");
|
||||||
|
var condition = parenthesised();
|
||||||
|
semicolon(true);
|
||||||
return new AST_Do({
|
return new AST_Do({
|
||||||
body : in_loop(statement),
|
body : body,
|
||||||
condition : (expect_token("keyword", "while"), tmp = parenthesised(), semicolon(true), tmp)
|
condition : condition
|
||||||
});
|
});
|
||||||
|
|
||||||
case "while":
|
case "while":
|
||||||
|
next();
|
||||||
return new AST_While({
|
return new AST_While({
|
||||||
condition : parenthesised(),
|
condition : parenthesised(),
|
||||||
body : in_loop(statement)
|
body : in_loop(statement)
|
||||||
});
|
});
|
||||||
|
|
||||||
case "for":
|
case "for":
|
||||||
|
next();
|
||||||
return for_();
|
return for_();
|
||||||
|
|
||||||
case "function":
|
case "function":
|
||||||
|
next();
|
||||||
return function_(AST_Defun);
|
return function_(AST_Defun);
|
||||||
|
|
||||||
case "if":
|
case "if":
|
||||||
|
next();
|
||||||
return if_();
|
return if_();
|
||||||
|
|
||||||
case "return":
|
case "return":
|
||||||
if (S.in_function == 0 && !options.bare_returns)
|
if (S.in_function == 0 && !options.bare_returns)
|
||||||
croak("'return' outside of function");
|
croak("'return' outside of function");
|
||||||
|
next();
|
||||||
|
var value = null;
|
||||||
|
if (is("punc", ";")) {
|
||||||
|
next();
|
||||||
|
} else if (!can_insert_semicolon()) {
|
||||||
|
value = expression(true);
|
||||||
|
semicolon();
|
||||||
|
}
|
||||||
return new AST_Return({
|
return new AST_Return({
|
||||||
value: ( is("punc", ";")
|
value: value
|
||||||
? (next(), null)
|
|
||||||
: can_insert_semicolon()
|
|
||||||
? null
|
|
||||||
: (tmp = expression(true), semicolon(), tmp) )
|
|
||||||
});
|
});
|
||||||
|
|
||||||
case "switch":
|
case "switch":
|
||||||
|
next();
|
||||||
return new AST_Switch({
|
return new AST_Switch({
|
||||||
expression : parenthesised(),
|
expression : parenthesised(),
|
||||||
body : in_loop(switch_body_)
|
body : in_loop(switch_body_)
|
||||||
});
|
});
|
||||||
|
|
||||||
case "throw":
|
case "throw":
|
||||||
|
next();
|
||||||
if (S.token.nlb)
|
if (S.token.nlb)
|
||||||
croak("Illegal newline after 'throw'");
|
croak("Illegal newline after 'throw'");
|
||||||
|
var value = expression(true);
|
||||||
|
semicolon();
|
||||||
return new AST_Throw({
|
return new AST_Throw({
|
||||||
value: (tmp = expression(true), semicolon(), tmp)
|
value: value
|
||||||
});
|
});
|
||||||
|
|
||||||
case "try":
|
case "try":
|
||||||
|
next();
|
||||||
return try_();
|
return try_();
|
||||||
|
|
||||||
case "var":
|
case "var":
|
||||||
return tmp = var_(), semicolon(), tmp;
|
next();
|
||||||
|
var node = var_();
|
||||||
|
semicolon();
|
||||||
|
return node;
|
||||||
|
|
||||||
case "const":
|
case "const":
|
||||||
return tmp = const_(), semicolon(), tmp;
|
next();
|
||||||
|
var node = const_();
|
||||||
|
semicolon();
|
||||||
|
return node;
|
||||||
|
|
||||||
case "with":
|
case "with":
|
||||||
if (S.input.has_directive("use strict")) {
|
if (S.input.has_directive("use strict")) {
|
||||||
croak("Strict mode may not include a with statement");
|
croak("Strict mode may not include a with statement");
|
||||||
}
|
}
|
||||||
|
next();
|
||||||
return new AST_With({
|
return new AST_With({
|
||||||
expression : parenthesised(),
|
expression : parenthesised(),
|
||||||
body : statement()
|
body : statement()
|
||||||
@@ -1320,10 +1347,15 @@ function parse($TEXT, options) {
|
|||||||
var type = start.type;
|
var type = start.type;
|
||||||
var name = as_property_name();
|
var name = as_property_name();
|
||||||
if (type == "name" && !is("punc", ":")) {
|
if (type == "name" && !is("punc", ":")) {
|
||||||
|
var key = new AST_SymbolAccessor({
|
||||||
|
start: S.token,
|
||||||
|
name: as_property_name(),
|
||||||
|
end: prev()
|
||||||
|
});
|
||||||
if (name == "get") {
|
if (name == "get") {
|
||||||
a.push(new AST_ObjectGetter({
|
a.push(new AST_ObjectGetter({
|
||||||
start : start,
|
start : start,
|
||||||
key : as_atom_node(),
|
key : key,
|
||||||
value : create_accessor(),
|
value : create_accessor(),
|
||||||
end : prev()
|
end : prev()
|
||||||
}));
|
}));
|
||||||
@@ -1332,7 +1364,7 @@ function parse($TEXT, options) {
|
|||||||
if (name == "set") {
|
if (name == "set") {
|
||||||
a.push(new AST_ObjectSetter({
|
a.push(new AST_ObjectSetter({
|
||||||
start : start,
|
start : start,
|
||||||
key : as_atom_node(),
|
key : key,
|
||||||
value : create_accessor(),
|
value : create_accessor(),
|
||||||
end : prev()
|
end : prev()
|
||||||
}));
|
}));
|
||||||
@@ -1354,14 +1386,15 @@ function parse($TEXT, options) {
|
|||||||
|
|
||||||
function as_property_name() {
|
function as_property_name() {
|
||||||
var tmp = S.token;
|
var tmp = S.token;
|
||||||
next();
|
|
||||||
switch (tmp.type) {
|
switch (tmp.type) {
|
||||||
|
case "operator":
|
||||||
|
if (!KEYWORDS(tmp.value)) unexpected();
|
||||||
case "num":
|
case "num":
|
||||||
case "string":
|
case "string":
|
||||||
case "name":
|
case "name":
|
||||||
case "operator":
|
|
||||||
case "keyword":
|
case "keyword":
|
||||||
case "atom":
|
case "atom":
|
||||||
|
next();
|
||||||
return tmp.value;
|
return tmp.value;
|
||||||
default:
|
default:
|
||||||
unexpected();
|
unexpected();
|
||||||
@@ -1370,16 +1403,9 @@ function parse($TEXT, options) {
|
|||||||
|
|
||||||
function as_name() {
|
function as_name() {
|
||||||
var tmp = S.token;
|
var tmp = S.token;
|
||||||
|
if (tmp.type != "name") unexpected();
|
||||||
next();
|
next();
|
||||||
switch (tmp.type) {
|
return tmp.value;
|
||||||
case "name":
|
|
||||||
case "operator":
|
|
||||||
case "keyword":
|
|
||||||
case "atom":
|
|
||||||
return tmp.value;
|
|
||||||
default:
|
|
||||||
unexpected();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function _make_symbol(type) {
|
function _make_symbol(type) {
|
||||||
@@ -1440,14 +1466,14 @@ function parse($TEXT, options) {
|
|||||||
if (is("operator") && UNARY_PREFIX(start.value)) {
|
if (is("operator") && UNARY_PREFIX(start.value)) {
|
||||||
next();
|
next();
|
||||||
handle_regexp();
|
handle_regexp();
|
||||||
var ex = make_unary(AST_UnaryPrefix, start.value, maybe_unary(allow_calls));
|
var ex = make_unary(AST_UnaryPrefix, start, maybe_unary(allow_calls));
|
||||||
ex.start = start;
|
ex.start = start;
|
||||||
ex.end = prev();
|
ex.end = prev();
|
||||||
return ex;
|
return ex;
|
||||||
}
|
}
|
||||||
var val = expr_atom(allow_calls);
|
var val = expr_atom(allow_calls);
|
||||||
while (is("operator") && UNARY_POSTFIX(S.token.value) && !S.token.nlb) {
|
while (is("operator") && UNARY_POSTFIX(S.token.value) && !S.token.nlb) {
|
||||||
val = make_unary(AST_UnaryPostfix, S.token.value, val);
|
val = make_unary(AST_UnaryPostfix, S.token, val);
|
||||||
val.start = start;
|
val.start = start;
|
||||||
val.end = S.token;
|
val.end = S.token;
|
||||||
next();
|
next();
|
||||||
@@ -1455,9 +1481,10 @@ function parse($TEXT, options) {
|
|||||||
return val;
|
return val;
|
||||||
};
|
};
|
||||||
|
|
||||||
function make_unary(ctor, op, expr) {
|
function make_unary(ctor, token, expr) {
|
||||||
|
var op = token.value;
|
||||||
if ((op == "++" || op == "--") && !is_assignable(expr))
|
if ((op == "++" || op == "--") && !is_assignable(expr))
|
||||||
croak("Invalid use of " + op + " operator", null, ctor === AST_UnaryPrefix ? expr.start.col - 1 : null);
|
croak("Invalid use of " + op + " operator", token.line, token.col, token.pos);
|
||||||
return new ctor({ operator: op, expression: expr });
|
return new ctor({ operator: op, expression: expr });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -361,11 +361,6 @@ AST_Symbol.DEFMETHOD("unmangleable", function(options){
|
|||||||
return this.definition().unmangleable(options);
|
return this.definition().unmangleable(options);
|
||||||
});
|
});
|
||||||
|
|
||||||
// property accessors are not mangleable
|
|
||||||
AST_SymbolAccessor.DEFMETHOD("unmangleable", function(){
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
|
|
||||||
// labels are always mangleable
|
// labels are always mangleable
|
||||||
AST_Label.DEFMETHOD("unmangleable", function(){
|
AST_Label.DEFMETHOD("unmangleable", function(){
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
"homepage": "http://lisperator.net/uglifyjs",
|
"homepage": "http://lisperator.net/uglifyjs",
|
||||||
"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": "2.8.24",
|
"version": "2.8.29",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=0.8.0"
|
"node": ">=0.8.0"
|
||||||
},
|
},
|
||||||
@@ -33,10 +33,7 @@
|
|||||||
"yargs": "~3.10.0"
|
"yargs": "~3.10.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"acorn": "~0.6.0",
|
"acorn": "~5.0.3",
|
||||||
"escodegen": "~1.3.3",
|
|
||||||
"esfuzz": "~0.3.1",
|
|
||||||
"estraverse": "~1.5.1",
|
|
||||||
"mocha": "~2.3.4"
|
"mocha": "~2.3.4"
|
||||||
},
|
},
|
||||||
"optionalDependencies": {
|
"optionalDependencies": {
|
||||||
|
|||||||
@@ -256,3 +256,19 @@ try_catch_finally: {
|
|||||||
"1",
|
"1",
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
accessor: {
|
||||||
|
options = {
|
||||||
|
side_effects: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
({
|
||||||
|
get a() {},
|
||||||
|
set a(v){
|
||||||
|
this.b = 2;
|
||||||
|
},
|
||||||
|
b: 1
|
||||||
|
});
|
||||||
|
}
|
||||||
|
expect: {}
|
||||||
|
}
|
||||||
|
|||||||
@@ -989,3 +989,50 @@ Infinity_NaN_undefined_LHS: {
|
|||||||
"}",
|
"}",
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
issue_1964_1: {
|
||||||
|
options = {
|
||||||
|
evaluate: true,
|
||||||
|
reduce_vars: true,
|
||||||
|
unsafe_regexp: false,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f() {
|
||||||
|
var long_variable_name = /\s/;
|
||||||
|
return "a b c".split(long_variable_name)[1];
|
||||||
|
}
|
||||||
|
console.log(f());
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f() {
|
||||||
|
var long_variable_name = /\s/;
|
||||||
|
return "a b c".split(long_variable_name)[1];
|
||||||
|
}
|
||||||
|
console.log(f());
|
||||||
|
}
|
||||||
|
expect_stdout: "b"
|
||||||
|
}
|
||||||
|
|
||||||
|
issue_1964_2: {
|
||||||
|
options = {
|
||||||
|
evaluate: true,
|
||||||
|
reduce_vars: true,
|
||||||
|
unsafe_regexp: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f() {
|
||||||
|
var long_variable_name = /\s/;
|
||||||
|
return "a b c".split(long_variable_name)[1];
|
||||||
|
}
|
||||||
|
console.log(f());
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f() {
|
||||||
|
return "a b c".split(/\s/)[1];
|
||||||
|
}
|
||||||
|
console.log(f());
|
||||||
|
}
|
||||||
|
expect_stdout: "b"
|
||||||
|
}
|
||||||
|
|||||||
31
test/compress/issue-1943.js
Normal file
31
test/compress/issue-1943.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
operator: {
|
||||||
|
input: {
|
||||||
|
a. //comment
|
||||||
|
typeof
|
||||||
|
}
|
||||||
|
expect_exact: "a.typeof;"
|
||||||
|
}
|
||||||
|
|
||||||
|
name: {
|
||||||
|
input: {
|
||||||
|
a. //comment
|
||||||
|
b
|
||||||
|
}
|
||||||
|
expect_exact: "a.b;"
|
||||||
|
}
|
||||||
|
|
||||||
|
keyword: {
|
||||||
|
input: {
|
||||||
|
a. //comment
|
||||||
|
default
|
||||||
|
}
|
||||||
|
expect_exact: "a.default;"
|
||||||
|
}
|
||||||
|
|
||||||
|
atom: {
|
||||||
|
input: {
|
||||||
|
a. //comment
|
||||||
|
true
|
||||||
|
}
|
||||||
|
expect_exact: "a.true;"
|
||||||
|
}
|
||||||
@@ -555,3 +555,105 @@ native_prototype: {
|
|||||||
"".indexOf.call(e, "bar");
|
"".indexOf.call(e, "bar");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
accessor_boolean: {
|
||||||
|
input: {
|
||||||
|
var a = 1;
|
||||||
|
var b = {
|
||||||
|
get true() {
|
||||||
|
return a;
|
||||||
|
},
|
||||||
|
set false(c) {
|
||||||
|
a = c;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
console.log(b.true, b.false = 2, b.true);
|
||||||
|
}
|
||||||
|
expect_exact: 'var a=1;var b={get true(){return a},set false(c){a=c}};console.log(b.true,b.false=2,b.true);'
|
||||||
|
expect_stdout: "1 2 2"
|
||||||
|
}
|
||||||
|
|
||||||
|
accessor_get_set: {
|
||||||
|
input: {
|
||||||
|
var a = 1;
|
||||||
|
var b = {
|
||||||
|
get set() {
|
||||||
|
return a;
|
||||||
|
},
|
||||||
|
set get(c) {
|
||||||
|
a = c;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
console.log(b.set, b.get = 2, b.set);
|
||||||
|
}
|
||||||
|
expect_exact: 'var a=1;var b={get set(){return a},set get(c){a=c}};console.log(b.set,b.get=2,b.set);'
|
||||||
|
expect_stdout: "1 2 2"
|
||||||
|
}
|
||||||
|
|
||||||
|
accessor_null_undefined: {
|
||||||
|
input: {
|
||||||
|
var a = 1;
|
||||||
|
var b = {
|
||||||
|
get null() {
|
||||||
|
return a;
|
||||||
|
},
|
||||||
|
set undefined(c) {
|
||||||
|
a = c;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
console.log(b.null, b.undefined = 2, b.null);
|
||||||
|
}
|
||||||
|
expect_exact: 'var a=1;var b={get null(){return a},set undefined(c){a=c}};console.log(b.null,b.undefined=2,b.null);'
|
||||||
|
expect_stdout: "1 2 2"
|
||||||
|
}
|
||||||
|
|
||||||
|
accessor_number: {
|
||||||
|
input: {
|
||||||
|
var a = 1;
|
||||||
|
var b = {
|
||||||
|
get 42() {
|
||||||
|
return a;
|
||||||
|
},
|
||||||
|
set 42(c) {
|
||||||
|
a = c;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
console.log(b[42], b[42] = 2, b[42]);
|
||||||
|
}
|
||||||
|
expect_exact: 'var a=1;var b={get 42(){return a},set 42(c){a=c}};console.log(b[42],b[42]=2,b[42]);'
|
||||||
|
expect_stdout: "1 2 2"
|
||||||
|
}
|
||||||
|
|
||||||
|
accessor_string: {
|
||||||
|
input: {
|
||||||
|
var a = 1;
|
||||||
|
var b = {
|
||||||
|
get "a-b"() {
|
||||||
|
return a;
|
||||||
|
},
|
||||||
|
set "a-b"(c) {
|
||||||
|
a = c;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
console.log(b["a-b"], b["a-b"] = 2, b["a-b"]);
|
||||||
|
}
|
||||||
|
expect_exact: 'var a=1;var b={get"a-b"(){return a},set"a-b"(c){a=c}};console.log(b["a-b"],b["a-b"]=2,b["a-b"]);'
|
||||||
|
expect_stdout: "1 2 2"
|
||||||
|
}
|
||||||
|
|
||||||
|
accessor_this: {
|
||||||
|
input: {
|
||||||
|
var a = 1;
|
||||||
|
var b = {
|
||||||
|
get this() {
|
||||||
|
return a;
|
||||||
|
},
|
||||||
|
set this(c) {
|
||||||
|
a = c;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
console.log(b.this, b.this = 2, b.this);
|
||||||
|
}
|
||||||
|
expect_exact: 'var a=1;var b={get this(){return a},set this(c){a=c}};console.log(b.this,b.this=2,b.this);'
|
||||||
|
expect_stdout: "1 2 2"
|
||||||
|
}
|
||||||
|
|||||||
@@ -119,3 +119,62 @@ chained: {
|
|||||||
a.b.c;
|
a.b.c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impure_getter_1: {
|
||||||
|
options = {
|
||||||
|
pure_getters: "strict",
|
||||||
|
side_effects: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
({
|
||||||
|
get a() {
|
||||||
|
console.log(1);
|
||||||
|
},
|
||||||
|
b: 1
|
||||||
|
}).a;
|
||||||
|
({
|
||||||
|
get a() {
|
||||||
|
console.log(1);
|
||||||
|
},
|
||||||
|
b: 1
|
||||||
|
}).b;
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
({
|
||||||
|
get a() {
|
||||||
|
console.log(1);
|
||||||
|
},
|
||||||
|
b: 1
|
||||||
|
}).a;
|
||||||
|
({
|
||||||
|
get a() {
|
||||||
|
console.log(1);
|
||||||
|
},
|
||||||
|
b: 1
|
||||||
|
}).b;
|
||||||
|
}
|
||||||
|
expect_stdout: "1"
|
||||||
|
}
|
||||||
|
|
||||||
|
impure_getter_2: {
|
||||||
|
options = {
|
||||||
|
pure_getters: true,
|
||||||
|
side_effects: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
// will produce incorrect output because getter is not pure
|
||||||
|
({
|
||||||
|
get a() {
|
||||||
|
console.log(1);
|
||||||
|
},
|
||||||
|
b: 1
|
||||||
|
}).a;
|
||||||
|
({
|
||||||
|
get a() {
|
||||||
|
console.log(1);
|
||||||
|
},
|
||||||
|
b: 1
|
||||||
|
}).b;
|
||||||
|
}
|
||||||
|
expect: {}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2169,3 +2169,32 @@ issue_1922_2: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "1"
|
expect_stdout: "1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
accessor: {
|
||||||
|
options = {
|
||||||
|
evaluate: true,
|
||||||
|
reduce_vars: true,
|
||||||
|
toplevel: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a = 1;
|
||||||
|
console.log({
|
||||||
|
get a() {
|
||||||
|
a = 2;
|
||||||
|
return a;
|
||||||
|
},
|
||||||
|
b: 1
|
||||||
|
}.b, a);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var a = 1;
|
||||||
|
console.log({
|
||||||
|
get a() {
|
||||||
|
a = 2;
|
||||||
|
return a;
|
||||||
|
},
|
||||||
|
b: 1
|
||||||
|
}.b, a);
|
||||||
|
}
|
||||||
|
expect_stdout: "1 1"
|
||||||
|
}
|
||||||
|
|||||||
1
test/input/invalid/assign_4.js
Normal file
1
test/input/invalid/assign_4.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
++null
|
||||||
1
test/input/invalid/dot_1.js
Normal file
1
test/input/invalid/dot_1.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
a.=
|
||||||
1
test/input/invalid/dot_2.js
Normal file
1
test/input/invalid/dot_2.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
%.a;
|
||||||
1
test/input/invalid/dot_3.js
Normal file
1
test/input/invalid/dot_3.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
a./();
|
||||||
1
test/input/invalid/else.js
Normal file
1
test/input/invalid/else.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
if (0) else 1;
|
||||||
1
test/input/invalid/object.js
Normal file
1
test/input/invalid/object.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
console.log({%: 1});
|
||||||
1
test/input/invalid/return.js
Normal file
1
test/input/invalid/return.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
return 42;
|
||||||
@@ -16,7 +16,7 @@ describe("Accessor tokens", function() {
|
|||||||
assert.equal(node.start.pos, 12);
|
assert.equal(node.start.pos, 12);
|
||||||
assert.equal(node.end.endpos, 46);
|
assert.equal(node.end.endpos, 46);
|
||||||
|
|
||||||
assert(node.key instanceof UglifyJS.AST_SymbolRef);
|
assert(node.key instanceof UglifyJS.AST_SymbolAccessor);
|
||||||
assert.equal(node.key.start.pos, 16);
|
assert.equal(node.key.start.pos, 16);
|
||||||
assert.equal(node.key.end.endpos, 22);
|
assert.equal(node.key.end.endpos, 22);
|
||||||
|
|
||||||
|
|||||||
@@ -51,15 +51,15 @@ describe("bin/uglifyjs", function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should append source map to output when using --source-map-inline", function (done) {
|
it("Should append source map to output when using --source-map-inline", function (done) {
|
||||||
var command = uglifyjscmd + ' test/input/issue-1323/sample.js --source-map-inline';
|
var command = uglifyjscmd + ' test/input/issue-1323/sample.js --source-map-inline';
|
||||||
|
|
||||||
exec(command, function (err, stdout) {
|
exec(command, function (err, stdout) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
assert.strictEqual(stdout, "var bar=function(){function foo(bar){return bar}return foo}();\n" +
|
assert.strictEqual(stdout, "var bar=function(){function foo(bar){return bar}return foo}();\n" +
|
||||||
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMTMyMy9zYW1wbGUuanMiXSwibmFtZXMiOlsiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxHQUFJQSxLQUFNLFdBQ04sUUFBU0MsS0FBS0QsS0FDVixNQUFPQSxLQUdYLE1BQU9DIn0=\n");
|
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMTMyMy9zYW1wbGUuanMiXSwibmFtZXMiOlsiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxHQUFJQSxLQUFNLFdBQ04sUUFBU0MsS0FBS0QsS0FDVixNQUFPQSxLQUdYLE1BQU9DIn0=\n");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("should not append source map to output when not using --source-map-inline", function (done) {
|
it("should not append source map to output when not using --source-map-inline", function (done) {
|
||||||
var command = uglifyjscmd + ' test/input/issue-1323/sample.js';
|
var command = uglifyjscmd + ' test/input/issue-1323/sample.js';
|
||||||
@@ -72,84 +72,84 @@ describe("bin/uglifyjs", function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should work with --keep-fnames (mangle only)", function (done) {
|
it("Should work with --keep-fnames (mangle only)", function (done) {
|
||||||
var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m';
|
var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m';
|
||||||
|
|
||||||
exec(command, function (err, stdout) {
|
exec(command, function (err, stdout) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n");
|
assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should work with --keep-fnames (mangle & compress)", function (done) {
|
it("Should work with --keep-fnames (mangle & compress)", function (done) {
|
||||||
var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m -c unused=false';
|
var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m -c unused=false';
|
||||||
|
|
||||||
exec(command, function (err, stdout) {
|
exec(command, function (err, stdout) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(5==f(g)());\n");
|
assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(5==f(g)());\n");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should work with keep_fnames under mangler options", function (done) {
|
it("Should work with keep_fnames under mangler options", function (done) {
|
||||||
var command = uglifyjscmd + ' test/input/issue-1431/sample.js -m keep_fnames=true';
|
var command = uglifyjscmd + ' test/input/issue-1431/sample.js -m keep_fnames=true';
|
||||||
|
|
||||||
exec(command, function (err, stdout) {
|
exec(command, function (err, stdout) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n");
|
assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should work with --define (simple)", function (done) {
|
it("Should work with --define (simple)", function (done) {
|
||||||
var command = uglifyjscmd + ' test/input/global_defs/simple.js --define D=5 -c';
|
var command = uglifyjscmd + ' test/input/global_defs/simple.js --define D=5 -c';
|
||||||
|
|
||||||
exec(command, function (err, stdout) {
|
exec(command, function (err, stdout) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
assert.strictEqual(stdout, "console.log(5);\n");
|
assert.strictEqual(stdout, "console.log(5);\n");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should work with --define (nested)", function (done) {
|
it("Should work with --define (nested)", function (done) {
|
||||||
var command = uglifyjscmd + ' test/input/global_defs/nested.js --define C.D=5,C.V=3 -c';
|
var command = uglifyjscmd + ' test/input/global_defs/nested.js --define C.D=5,C.V=3 -c';
|
||||||
|
|
||||||
exec(command, function (err, stdout) {
|
exec(command, function (err, stdout) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
assert.strictEqual(stdout, "console.log(3,5);\n");
|
assert.strictEqual(stdout, "console.log(3,5);\n");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should work with --define (AST_Node)", function (done) {
|
it("Should work with --define (AST_Node)", function (done) {
|
||||||
var command = uglifyjscmd + ' test/input/global_defs/simple.js --define console.log=stdout.println -c';
|
var command = uglifyjscmd + ' test/input/global_defs/simple.js --define console.log=stdout.println -c';
|
||||||
|
|
||||||
exec(command, function (err, stdout) {
|
exec(command, function (err, stdout) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
assert.strictEqual(stdout, "stdout.println(D);\n");
|
assert.strictEqual(stdout, "stdout.println(D);\n");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should work with `--beautify`", function (done) {
|
it("Should work with `--beautify`", function (done) {
|
||||||
var command = uglifyjscmd + ' test/input/issue-1482/input.js -b';
|
var command = uglifyjscmd + ' test/input/issue-1482/input.js -b';
|
||||||
|
|
||||||
exec(command, function (err, stdout) {
|
exec(command, function (err, stdout) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
assert.strictEqual(stdout, readFileSync("test/input/issue-1482/default.js", "utf8"));
|
assert.strictEqual(stdout, readFileSync("test/input/issue-1482/default.js", "utf8"));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should work with `--beautify bracketize`", function (done) {
|
it("Should work with `--beautify bracketize`", function (done) {
|
||||||
var command = uglifyjscmd + ' test/input/issue-1482/input.js -b bracketize';
|
var command = uglifyjscmd + ' test/input/issue-1482/input.js -b bracketize';
|
||||||
|
|
||||||
exec(command, function (err, stdout) {
|
exec(command, function (err, stdout) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
assert.strictEqual(stdout, readFileSync("test/input/issue-1482/bracketize.js", "utf8"));
|
assert.strictEqual(stdout, readFileSync("test/input/issue-1482/bracketize.js", "utf8"));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should process inline source map", function(done) {
|
it("Should process inline source map", function(done) {
|
||||||
var command = uglifyjscmd + ' test/input/issue-520/input.js -mc toplevel --in-source-map inline --source-map-inline';
|
var command = uglifyjscmd + ' test/input/issue-520/input.js -mc toplevel --in-source-map inline --source-map-inline';
|
||||||
@@ -252,58 +252,163 @@ describe("bin/uglifyjs", function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should support hyphen as shorthand", function(done) {
|
it("Should support hyphen as shorthand", function(done) {
|
||||||
var command = uglifyjscmd + ' test/input/issue-1431/sample.js -m keep-fnames=true';
|
var command = uglifyjscmd + ' test/input/issue-1431/sample.js -m keep-fnames=true';
|
||||||
|
|
||||||
exec(command, function (err, stdout) {
|
exec(command, function (err, stdout) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n");
|
assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should throw syntax error (5--)", function(done) {
|
it("Should throw syntax error (5--)", function(done) {
|
||||||
var command = uglifyjscmd + ' test/input/invalid/assign_1.js';
|
var command = uglifyjscmd + ' test/input/invalid/assign_1.js';
|
||||||
|
|
||||||
exec(command, function (err, stdout, stderr) {
|
exec(command, function (err, stdout, stderr) {
|
||||||
assert.ok(err);
|
assert.ok(err);
|
||||||
assert.strictEqual(stdout, "");
|
assert.strictEqual(stdout, "");
|
||||||
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
||||||
"Parse error at test/input/invalid/assign_1.js:1,18",
|
"Parse error at test/input/invalid/assign_1.js:1,18",
|
||||||
"console.log(1 || 5--);",
|
"console.log(1 || 5--);",
|
||||||
" ^",
|
" ^",
|
||||||
"SyntaxError: Invalid use of -- operator"
|
"SyntaxError: Invalid use of -- operator"
|
||||||
].join("\n"));
|
].join("\n"));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should throw syntax error (Math.random() /= 2)", function(done) {
|
it("Should throw syntax error (Math.random() /= 2)", function(done) {
|
||||||
var command = uglifyjscmd + ' test/input/invalid/assign_2.js';
|
var command = uglifyjscmd + ' test/input/invalid/assign_2.js';
|
||||||
|
|
||||||
exec(command, function (err, stdout, stderr) {
|
exec(command, function (err, stdout, stderr) {
|
||||||
assert.ok(err);
|
assert.ok(err);
|
||||||
assert.strictEqual(stdout, "");
|
assert.strictEqual(stdout, "");
|
||||||
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
||||||
"Parse error at test/input/invalid/assign_2.js:1,32",
|
"Parse error at test/input/invalid/assign_2.js:1,32",
|
||||||
"console.log(2 || (Math.random() /= 2));",
|
"console.log(2 || (Math.random() /= 2));",
|
||||||
" ^",
|
" ^",
|
||||||
"SyntaxError: Invalid assignment"
|
"SyntaxError: Invalid assignment"
|
||||||
].join("\n"));
|
].join("\n"));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should throw syntax error (++this)", function(done) {
|
it("Should throw syntax error (++this)", function(done) {
|
||||||
var command = uglifyjscmd + ' test/input/invalid/assign_3.js';
|
var command = uglifyjscmd + ' test/input/invalid/assign_3.js';
|
||||||
|
|
||||||
exec(command, function (err, stdout, stderr) {
|
exec(command, function (err, stdout, stderr) {
|
||||||
assert.ok(err);
|
assert.ok(err);
|
||||||
assert.strictEqual(stdout, "");
|
assert.strictEqual(stdout, "");
|
||||||
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
||||||
"Parse error at test/input/invalid/assign_3.js:1,18",
|
"Parse error at test/input/invalid/assign_3.js:1,17",
|
||||||
"console.log(3 || ++this);",
|
"console.log(3 || ++this);",
|
||||||
" ^",
|
" ^",
|
||||||
"SyntaxError: Invalid use of ++ operator"
|
"SyntaxError: Invalid use of ++ operator"
|
||||||
].join("\n"));
|
].join("\n"));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
it("Should throw syntax error (++null)", function(done) {
|
||||||
|
var command = uglifyjscmd + ' test/input/invalid/assign_4.js';
|
||||||
|
|
||||||
|
exec(command, function (err, stdout, stderr) {
|
||||||
|
assert.ok(err);
|
||||||
|
assert.strictEqual(stdout, "");
|
||||||
|
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
||||||
|
"Parse error at test/input/invalid/assign_4.js:1,0",
|
||||||
|
"++null",
|
||||||
|
"^",
|
||||||
|
"SyntaxError: Invalid use of ++ operator"
|
||||||
|
].join("\n"));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it("Should throw syntax error (a.=)", function(done) {
|
||||||
|
var command = uglifyjscmd + ' test/input/invalid/dot_1.js';
|
||||||
|
|
||||||
|
exec(command, function (err, stdout, stderr) {
|
||||||
|
assert.ok(err);
|
||||||
|
assert.strictEqual(stdout, "");
|
||||||
|
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
||||||
|
"Parse error at test/input/invalid/dot_1.js:1,2",
|
||||||
|
"a.=",
|
||||||
|
" ^",
|
||||||
|
"SyntaxError: Unexpected token: operator (=)"
|
||||||
|
].join("\n"));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it("Should throw syntax error (%.a)", function(done) {
|
||||||
|
var command = uglifyjscmd + ' test/input/invalid/dot_2.js';
|
||||||
|
|
||||||
|
exec(command, function (err, stdout, stderr) {
|
||||||
|
assert.ok(err);
|
||||||
|
assert.strictEqual(stdout, "");
|
||||||
|
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
||||||
|
"Parse error at test/input/invalid/dot_2.js:1,0",
|
||||||
|
"%.a;",
|
||||||
|
"^",
|
||||||
|
"SyntaxError: Unexpected token: operator (%)"
|
||||||
|
].join("\n"));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it("Should throw syntax error (a./();)", function(done) {
|
||||||
|
var command = uglifyjscmd + ' test/input/invalid/dot_3.js';
|
||||||
|
|
||||||
|
exec(command, function (err, stdout, stderr) {
|
||||||
|
assert.ok(err);
|
||||||
|
assert.strictEqual(stdout, "");
|
||||||
|
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
||||||
|
"Parse error at test/input/invalid/dot_3.js:1,2",
|
||||||
|
"a./();",
|
||||||
|
" ^",
|
||||||
|
"SyntaxError: Unexpected token: operator (/)"
|
||||||
|
].join("\n"));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it("Should throw syntax error ({%: 1})", function(done) {
|
||||||
|
var command = uglifyjscmd + ' test/input/invalid/object.js';
|
||||||
|
|
||||||
|
exec(command, function (err, stdout, stderr) {
|
||||||
|
assert.ok(err);
|
||||||
|
assert.strictEqual(stdout, "");
|
||||||
|
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
||||||
|
"Parse error at test/input/invalid/object.js:1,13",
|
||||||
|
"console.log({%: 1});",
|
||||||
|
" ^",
|
||||||
|
"SyntaxError: Unexpected token: operator (%)"
|
||||||
|
].join("\n"));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it("Should throw syntax error (else)", function(done) {
|
||||||
|
var command = uglifyjscmd + ' test/input/invalid/else.js';
|
||||||
|
|
||||||
|
exec(command, function (err, stdout, stderr) {
|
||||||
|
assert.ok(err);
|
||||||
|
assert.strictEqual(stdout, "");
|
||||||
|
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
||||||
|
"Parse error at test/input/invalid/else.js:1,7",
|
||||||
|
"if (0) else 1;",
|
||||||
|
" ^",
|
||||||
|
"SyntaxError: Unexpected token: keyword (else)"
|
||||||
|
].join("\n"));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it("Should throw syntax error (return)", function(done) {
|
||||||
|
var command = uglifyjscmd + ' test/input/invalid/return.js';
|
||||||
|
|
||||||
|
exec(command, function (err, stdout, stderr) {
|
||||||
|
assert.ok(err);
|
||||||
|
assert.strictEqual(stdout, "");
|
||||||
|
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
||||||
|
"Parse error at test/input/invalid/return.js:1,0",
|
||||||
|
"return 42;",
|
||||||
|
"^",
|
||||||
|
"SyntaxError: 'return' outside of function"
|
||||||
|
].join("\n"));
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -351,18 +351,28 @@ describe("Directives", function() {
|
|||||||
var tests = [
|
var tests = [
|
||||||
[
|
[
|
||||||
'"use strict";"use strict";"use strict";"use foo";"use strict";;"use sloppy";doSomething("foo");',
|
'"use strict";"use strict";"use strict";"use foo";"use strict";;"use sloppy";doSomething("foo");',
|
||||||
'"use strict";"use foo";doSomething("foo");'
|
'"use strict";"use foo";doSomething("foo");',
|
||||||
|
'function f(){ "use strict" }',
|
||||||
|
'function f(){ "use asm" }',
|
||||||
|
'function f(){ "use nondirective" }',
|
||||||
|
'function f(){ ;"use strict" }',
|
||||||
|
'function f(){ "use \n"; }',
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
// Nothing gets optimised in the compressor because "use asm" is the first statement
|
// Nothing gets optimised in the compressor because "use asm" is the first statement
|
||||||
'"use asm";"use\\x20strict";1+1;',
|
'"use asm";"use\\x20strict";1+1;',
|
||||||
'"use asm";;"use strict";1+1;' // Yet, the parser noticed that "use strict" wasn't a directive
|
'"use asm";;"use strict";1+1;', // Yet, the parser noticed that "use strict" wasn't a directive
|
||||||
|
'function f(){"use strict"}',
|
||||||
|
'function f(){"use asm"}',
|
||||||
|
'function f(){"use nondirective"}',
|
||||||
|
'function f(){}',
|
||||||
|
'function f(){}',
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
||||||
for (var i = 0; i < tests.length; i++) {
|
for (var i = 0; i < tests.length; i++) {
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
uglify.minify(tests[i][0], {fromString: true, compress: {collapse_vars: true, side_effects: true}}).code,
|
uglify.minify(tests[i][0], {fromString: true}).code,
|
||||||
tests[i][1],
|
tests[i][1],
|
||||||
tests[i][0]
|
tests[i][0]
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ describe("Getters and setters", function() {
|
|||||||
var fail = function(data) {
|
var fail = function(data) {
|
||||||
return function (e) {
|
return function (e) {
|
||||||
return e instanceof UglifyJS.JS_Parse_Error &&
|
return e instanceof UglifyJS.JS_Parse_Error &&
|
||||||
e.message === "Invalid getter/setter name: " + data.operator;
|
e.message === "Unexpected token: operator (" + data.operator + ")";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,103 +1,87 @@
|
|||||||
// Testing UglifyJS <-> SpiderMonkey AST conversion
|
// Testing UglifyJS <-> SpiderMonkey AST conversion
|
||||||
// through generative testing.
|
"use strict";
|
||||||
|
|
||||||
var UglifyJS = require(".."),
|
var acorn = require("acorn");
|
||||||
escodegen = require("escodegen"),
|
var ufuzz = require("./ufuzz");
|
||||||
esfuzz = require("esfuzz"),
|
var UglifyJS = require("..");
|
||||||
estraverse = require("estraverse"),
|
|
||||||
prefix = "\r ";
|
|
||||||
|
|
||||||
// Normalizes input AST for UglifyJS in order to get correct comparison.
|
function try_beautify(code) {
|
||||||
|
var beautified;
|
||||||
function normalizeInput(ast) {
|
try {
|
||||||
return estraverse.replace(ast, {
|
beautified = UglifyJS.minify(code, {
|
||||||
enter: function(node, parent) {
|
fromString: true,
|
||||||
switch (node.type) {
|
compress: false,
|
||||||
// Internally mark all the properties with semi-standard type "Property".
|
mangle: false,
|
||||||
case "ObjectExpression":
|
output: {
|
||||||
node.properties.forEach(function (property) {
|
beautify: true,
|
||||||
property.type = "Property";
|
bracketize: true
|
||||||
});
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Since UglifyJS doesn"t recognize different types of property keys,
|
|
||||||
// decision on SpiderMonkey node type is based on check whether key
|
|
||||||
// can be valid identifier or not - so we do in input AST.
|
|
||||||
case "Property":
|
|
||||||
var key = node.key;
|
|
||||||
if (key.type === "Literal" && typeof key.value === "string" && UglifyJS.is_identifier(key.value)) {
|
|
||||||
node.key = {
|
|
||||||
type: "Identifier",
|
|
||||||
name: key.value
|
|
||||||
};
|
|
||||||
} else if (key.type === "Identifier" && !UglifyJS.is_identifier(key.name)) {
|
|
||||||
node.key = {
|
|
||||||
type: "Literal",
|
|
||||||
value: key.name
|
|
||||||
};
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
// UglifyJS internally flattens all the expression sequences - either
|
|
||||||
// to one element (if sequence contains only one element) or flat list.
|
|
||||||
case "SequenceExpression":
|
|
||||||
node.expressions = node.expressions.reduce(function flatten(list, expr) {
|
|
||||||
return list.concat(expr.type === "SequenceExpression" ? expr.expressions.reduce(flatten, []) : [expr]);
|
|
||||||
}, []);
|
|
||||||
if (node.expressions.length === 1) {
|
|
||||||
return node.expressions[0];
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = function(options) {
|
|
||||||
console.log("--- UglifyJS <-> Mozilla AST conversion");
|
|
||||||
|
|
||||||
for (var counter = 0; counter < options.iterations; counter++) {
|
|
||||||
process.stdout.write(prefix + counter + "/" + options.iterations);
|
|
||||||
|
|
||||||
var ast1 = normalizeInput(esfuzz.generate({
|
|
||||||
maxDepth: options.maxDepth
|
|
||||||
}));
|
|
||||||
|
|
||||||
var ast2 =
|
|
||||||
UglifyJS
|
|
||||||
.AST_Node
|
|
||||||
.from_mozilla_ast(ast1)
|
|
||||||
.to_mozilla_ast();
|
|
||||||
|
|
||||||
var astPair = [
|
|
||||||
{name: 'expected', value: ast1},
|
|
||||||
{name: 'actual', value: ast2}
|
|
||||||
];
|
|
||||||
|
|
||||||
var jsPair = astPair.map(function(item) {
|
|
||||||
return {
|
|
||||||
name: item.name,
|
|
||||||
value: escodegen.generate(item.value)
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
} catch (ex) {
|
||||||
if (jsPair[0].value !== jsPair[1].value) {
|
beautified = { error: ex };
|
||||||
var fs = require("fs");
|
|
||||||
var acorn = require("acorn");
|
|
||||||
|
|
||||||
fs.existsSync("tmp") || fs.mkdirSync("tmp");
|
|
||||||
|
|
||||||
jsPair.forEach(function (item) {
|
|
||||||
var fileName = "tmp/dump_" + item.name;
|
|
||||||
var ast = acorn.parse(item.value);
|
|
||||||
fs.writeFileSync(fileName + ".js", item.value);
|
|
||||||
fs.writeFileSync(fileName + ".json", JSON.stringify(ast, null, 2));
|
|
||||||
});
|
|
||||||
|
|
||||||
process.stdout.write("\n");
|
|
||||||
throw new Error("Got different outputs, check out tmp/dump_*.{js,json} for codes and ASTs.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (beautified.error) {
|
||||||
|
console.log("// !!! beautify failed !!!");
|
||||||
|
console.log(beautified.error.stack);
|
||||||
|
console.log(code);
|
||||||
|
} else {
|
||||||
|
console.log("// (beautified)");
|
||||||
|
console.log(beautified.code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
process.stdout.write(prefix + "Probability of error is less than " + (100 / options.iterations) + "%, stopping.\n");
|
function test(original, estree, description) {
|
||||||
};
|
var transformed;
|
||||||
|
try {
|
||||||
|
transformed = UglifyJS.minify(estree, {
|
||||||
|
fromString: true,
|
||||||
|
compress: false,
|
||||||
|
mangle: false,
|
||||||
|
spidermonkey: true
|
||||||
|
});
|
||||||
|
} catch (ex) {
|
||||||
|
transformed = { error: ex };
|
||||||
|
}
|
||||||
|
if (transformed.error || original !== transformed.code) {
|
||||||
|
console.log("//=============================================================");
|
||||||
|
console.log("// !!!!!! Failed... round", round);
|
||||||
|
console.log("// original code");
|
||||||
|
try_beautify(original);
|
||||||
|
console.log();
|
||||||
|
console.log();
|
||||||
|
console.log("//-------------------------------------------------------------");
|
||||||
|
console.log("//", description);
|
||||||
|
if (transformed.error) {
|
||||||
|
console.log(transformed.error.stack);
|
||||||
|
} else {
|
||||||
|
try_beautify(transformed.code);
|
||||||
|
}
|
||||||
|
console.log("!!!!!! Failed... round", round);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var num_iterations = ufuzz.num_iterations;
|
||||||
|
for (var round = 1; round <= num_iterations; round++) {
|
||||||
|
process.stdout.write(round + " of " + num_iterations + "\r");
|
||||||
|
var code = ufuzz.createTopLevelCode();
|
||||||
|
var uglified = {
|
||||||
|
ast: UglifyJS.parse(code),
|
||||||
|
code: UglifyJS.minify(code, {
|
||||||
|
fromString: true,
|
||||||
|
compress: false,
|
||||||
|
mangle: false
|
||||||
|
}).code
|
||||||
|
};
|
||||||
|
test(uglified.code, uglified.ast.to_mozilla_ast(), "AST_Node.to_mozilla_ast()");
|
||||||
|
try {
|
||||||
|
test(uglified.code, acorn.parse(code), "acorn.parse()");
|
||||||
|
} catch (e) {
|
||||||
|
console.log("//=============================================================");
|
||||||
|
console.log("// acorn parser failed... round", round);
|
||||||
|
console.log(e);
|
||||||
|
console.log("// original code");
|
||||||
|
console.log(code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log();
|
||||||
|
|||||||
@@ -23,12 +23,6 @@ mocha_tests();
|
|||||||
var run_sourcemaps_tests = require('./sourcemaps');
|
var run_sourcemaps_tests = require('./sourcemaps');
|
||||||
run_sourcemaps_tests();
|
run_sourcemaps_tests();
|
||||||
|
|
||||||
var run_ast_conversion_tests = require("./mozilla-ast");
|
|
||||||
|
|
||||||
run_ast_conversion_tests({
|
|
||||||
iterations: 1000
|
|
||||||
});
|
|
||||||
|
|
||||||
/* -----[ utils ]----- */
|
/* -----[ utils ]----- */
|
||||||
|
|
||||||
function tmpl() {
|
function tmpl() {
|
||||||
|
|||||||
@@ -1,14 +1,16 @@
|
|||||||
var vm = require("vm");
|
var vm = require("vm");
|
||||||
|
|
||||||
function safe_log(arg) {
|
function safe_log(arg, level) {
|
||||||
if (arg) switch (typeof arg) {
|
if (arg) switch (typeof arg) {
|
||||||
case "function":
|
case "function":
|
||||||
return arg.toString();
|
return arg.toString();
|
||||||
case "object":
|
case "object":
|
||||||
if (/Error$/.test(arg.name)) return arg.toString();
|
if (/Error$/.test(arg.name)) return arg.toString();
|
||||||
arg.constructor.toString();
|
arg.constructor.toString();
|
||||||
for (var key in arg) {
|
if (level--) for (var key in arg) {
|
||||||
arg[key] = safe_log(arg[key]);
|
if (!Object.getOwnPropertyDescriptor(arg, key).get) {
|
||||||
|
arg[key] = safe_log(arg[key], level);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return arg;
|
return arg;
|
||||||
@@ -48,7 +50,9 @@ exports.run_code = function(code) {
|
|||||||
].join("\n"), {
|
].join("\n"), {
|
||||||
console: {
|
console: {
|
||||||
log: function() {
|
log: function() {
|
||||||
return console.log.apply(console, [].map.call(arguments, safe_log));
|
return console.log.apply(console, [].map.call(arguments, function(arg) {
|
||||||
|
return safe_log(arg, 3);
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, { timeout: 5000 });
|
}, { timeout: 5000 });
|
||||||
|
|||||||
218
test/ufuzz.js
218
test/ufuzz.js
@@ -48,8 +48,9 @@ var STMT_COUNT_FROM_GLOBAL = true; // count statement depth from nearest functio
|
|||||||
var num_iterations = +process.argv[2] || 1/0;
|
var num_iterations = +process.argv[2] || 1/0;
|
||||||
var verbose = false; // log every generated test
|
var verbose = false; // log every generated test
|
||||||
var verbose_interval = false; // log every 100 generated tests
|
var verbose_interval = false; // log every 100 generated tests
|
||||||
var verbose_error = false;
|
|
||||||
var use_strict = false;
|
var use_strict = false;
|
||||||
|
var catch_redef = require.main === module;
|
||||||
|
var generate_directive = require.main === module;
|
||||||
for (var i = 2; i < process.argv.length; ++i) {
|
for (var i = 2; i < process.argv.length; ++i) {
|
||||||
switch (process.argv[i]) {
|
switch (process.argv[i]) {
|
||||||
case '-v':
|
case '-v':
|
||||||
@@ -58,9 +59,6 @@ for (var i = 2; i < process.argv.length; ++i) {
|
|||||||
case '-V':
|
case '-V':
|
||||||
verbose_interval = true;
|
verbose_interval = true;
|
||||||
break;
|
break;
|
||||||
case '-E':
|
|
||||||
verbose_error = true;
|
|
||||||
break;
|
|
||||||
case '-t':
|
case '-t':
|
||||||
MAX_GENERATED_TOPLEVELS_PER_RUN = +process.argv[++i];
|
MAX_GENERATED_TOPLEVELS_PER_RUN = +process.argv[++i];
|
||||||
if (!MAX_GENERATED_TOPLEVELS_PER_RUN) throw new Error('Must generate at least one toplevel per run');
|
if (!MAX_GENERATED_TOPLEVELS_PER_RUN) throw new Error('Must generate at least one toplevel per run');
|
||||||
@@ -79,6 +77,12 @@ for (var i = 2; i < process.argv.length; ++i) {
|
|||||||
STMT_SECOND_LEVEL_OVERRIDE = STMT_ARG_TO_ID[name];
|
STMT_SECOND_LEVEL_OVERRIDE = STMT_ARG_TO_ID[name];
|
||||||
if (!(STMT_SECOND_LEVEL_OVERRIDE >= 0)) throw new Error('Unknown statement name; use -? to get a list');
|
if (!(STMT_SECOND_LEVEL_OVERRIDE >= 0)) throw new Error('Unknown statement name; use -? to get a list');
|
||||||
break;
|
break;
|
||||||
|
case '--no-catch-redef':
|
||||||
|
catch_redef = false;
|
||||||
|
break;
|
||||||
|
case '--no-directive':
|
||||||
|
generate_directive = false;
|
||||||
|
break;
|
||||||
case '--use-strict':
|
case '--use-strict':
|
||||||
use_strict = true;
|
use_strict = true;
|
||||||
break;
|
break;
|
||||||
@@ -103,11 +107,12 @@ for (var i = 2; i < process.argv.length; ++i) {
|
|||||||
console.log('<number>: generate this many cases (if used must be first arg)');
|
console.log('<number>: generate this many cases (if used must be first arg)');
|
||||||
console.log('-v: print every generated test case');
|
console.log('-v: print every generated test case');
|
||||||
console.log('-V: print every 100th generated test case');
|
console.log('-V: print every 100th generated test case');
|
||||||
console.log('-E: print generated test case with runtime error');
|
|
||||||
console.log('-t <int>: generate this many toplevels per run (more take longer)');
|
console.log('-t <int>: generate this many toplevels per run (more take longer)');
|
||||||
console.log('-r <int>: maximum recursion depth for generator (higher takes longer)');
|
console.log('-r <int>: maximum recursion depth for generator (higher takes longer)');
|
||||||
console.log('-s1 <statement name>: force the first level statement to be this one (see list below)');
|
console.log('-s1 <statement name>: force the first level statement to be this one (see list below)');
|
||||||
console.log('-s2 <statement name>: force the second level statement to be this one (see list below)');
|
console.log('-s2 <statement name>: force the second level statement to be this one (see list below)');
|
||||||
|
console.log('--no-catch-redef: do not redefine catch variables');
|
||||||
|
console.log('--no-directive: do not generate directives');
|
||||||
console.log('--use-strict: generate "use strict"');
|
console.log('--use-strict: generate "use strict"');
|
||||||
console.log('--stmt-depth-from-func: reset statement depth counter at each function, counts from global otherwise');
|
console.log('--stmt-depth-from-func: reset statement depth counter at each function, counts from global otherwise');
|
||||||
console.log('--only-stmt <statement names>: a comma delimited white list of statements that may be generated');
|
console.log('--only-stmt <statement names>: a comma delimited white list of statements that may be generated');
|
||||||
@@ -192,12 +197,33 @@ var ASSIGNMENTS = [
|
|||||||
'=',
|
'=',
|
||||||
'=',
|
'=',
|
||||||
'=',
|
'=',
|
||||||
|
'=',
|
||||||
|
'=',
|
||||||
|
'=',
|
||||||
|
'=',
|
||||||
|
|
||||||
|
'=',
|
||||||
|
'=',
|
||||||
|
'=',
|
||||||
|
'=',
|
||||||
|
'=',
|
||||||
|
'=',
|
||||||
|
'=',
|
||||||
|
'=',
|
||||||
|
'=',
|
||||||
|
'=',
|
||||||
|
|
||||||
'==',
|
|
||||||
'!=',
|
|
||||||
'===',
|
|
||||||
'!==',
|
|
||||||
'+=',
|
'+=',
|
||||||
|
'+=',
|
||||||
|
'+=',
|
||||||
|
'+=',
|
||||||
|
'+=',
|
||||||
|
'+=',
|
||||||
|
'+=',
|
||||||
|
'+=',
|
||||||
|
'+=',
|
||||||
|
'+=',
|
||||||
|
|
||||||
'-=',
|
'-=',
|
||||||
'*=',
|
'*=',
|
||||||
'/=',
|
'/=',
|
||||||
@@ -207,7 +233,8 @@ var ASSIGNMENTS = [
|
|||||||
'<<=',
|
'<<=',
|
||||||
'>>=',
|
'>>=',
|
||||||
'>>>=',
|
'>>>=',
|
||||||
'%=' ];
|
'%=',
|
||||||
|
];
|
||||||
|
|
||||||
var UNARY_SAFE = [
|
var UNARY_SAFE = [
|
||||||
'+',
|
'+',
|
||||||
@@ -276,6 +303,7 @@ var TYPEOF_OUTCOMES = [
|
|||||||
'symbol',
|
'symbol',
|
||||||
'crap' ];
|
'crap' ];
|
||||||
|
|
||||||
|
var unique_vars = [];
|
||||||
var loops = 0;
|
var loops = 0;
|
||||||
var funcs = 0;
|
var funcs = 0;
|
||||||
var labels = 10000;
|
var labels = 10000;
|
||||||
@@ -290,6 +318,10 @@ function strictMode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function createTopLevelCode() {
|
function createTopLevelCode() {
|
||||||
|
VAR_NAMES.length = INITIAL_NAMES_LEN; // prune any previous names still in the list
|
||||||
|
unique_vars.length = 0;
|
||||||
|
loops = 0;
|
||||||
|
funcs = 0;
|
||||||
return [
|
return [
|
||||||
strictMode(),
|
strictMode(),
|
||||||
'var a = 100, b = 10, c = 0;',
|
'var a = 100, b = 10, c = 0;',
|
||||||
@@ -325,33 +357,36 @@ function createArgs() {
|
|||||||
return args.join(', ');
|
return args.join(', ');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function filterDirective(s) {
|
||||||
|
if (!generate_directive && !s[1] && /\("/.test(s[2])) s[2] = ';' + s[2];
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
function createFunction(recurmax, inGlobal, noDecl, canThrow, stmtDepth) {
|
function createFunction(recurmax, inGlobal, noDecl, canThrow, stmtDepth) {
|
||||||
if (--recurmax < 0) { return ';'; }
|
if (--recurmax < 0) { return ';'; }
|
||||||
if (!STMT_COUNT_FROM_GLOBAL) stmtDepth = 0;
|
if (!STMT_COUNT_FROM_GLOBAL) stmtDepth = 0;
|
||||||
var func = funcs++;
|
var func = funcs++;
|
||||||
var namesLenBefore = VAR_NAMES.length;
|
var namesLenBefore = VAR_NAMES.length;
|
||||||
var name = (inGlobal || rng(5) > 0) ? 'f' + func : createVarName(MANDATORY, noDecl);
|
var name;
|
||||||
if (name === 'a' || name === 'b' || name === 'c') name = 'f' + func; // quick hack to prevent assignment to func names of being called
|
if (inGlobal || rng(5) > 0) name = 'f' + func;
|
||||||
var s = '';
|
else {
|
||||||
|
unique_vars.push('a', 'b', 'c');
|
||||||
|
name = createVarName(MANDATORY, noDecl);
|
||||||
|
unique_vars.length -= 3;
|
||||||
|
}
|
||||||
|
var s = [
|
||||||
|
'function ' + name + '(' + createParams() + '){',
|
||||||
|
strictMode()
|
||||||
|
];
|
||||||
if (rng(5) === 0) {
|
if (rng(5) === 0) {
|
||||||
// functions with functions. lower the recursion to prevent a mess.
|
// functions with functions. lower the recursion to prevent a mess.
|
||||||
s = [
|
s.push(createFunctions(rng(5) + 1, Math.ceil(recurmax * 0.7), NOT_GLOBAL, ANY_TYPE, canThrow, stmtDepth));
|
||||||
'function ' + name + '(' + createParams() + '){',
|
|
||||||
strictMode(),
|
|
||||||
createFunctions(rng(5) + 1, Math.ceil(recurmax * 0.7), NOT_GLOBAL, ANY_TYPE, canThrow, stmtDepth),
|
|
||||||
'}',
|
|
||||||
''
|
|
||||||
].join('\n');
|
|
||||||
} else {
|
} else {
|
||||||
// functions with statements
|
// functions with statements
|
||||||
s = [
|
s.push(createStatements(3, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth));
|
||||||
'function ' + name + '(' + createParams() + '){',
|
|
||||||
strictMode(),
|
|
||||||
createStatements(3, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth),
|
|
||||||
'}',
|
|
||||||
''
|
|
||||||
].join('\n');
|
|
||||||
}
|
}
|
||||||
|
s.push('}', '');
|
||||||
|
s = filterDirective(s).join('\n');
|
||||||
|
|
||||||
VAR_NAMES.length = namesLenBefore;
|
VAR_NAMES.length = namesLenBefore;
|
||||||
|
|
||||||
@@ -359,7 +394,6 @@ function createFunction(recurmax, inGlobal, noDecl, canThrow, stmtDepth) {
|
|||||||
// avoid "function statements" (decl inside statements)
|
// avoid "function statements" (decl inside statements)
|
||||||
else if (inGlobal || rng(10) > 0) s += 'var ' + createVarName(MANDATORY) + ' = ' + name + '(' + createArgs() + ');';
|
else if (inGlobal || rng(10) > 0) s += 'var ' + createVarName(MANDATORY) + ' = ' + name + '(' + createArgs() + ');';
|
||||||
|
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -406,7 +440,7 @@ function getLabel(label) {
|
|||||||
return label && " L" + label;
|
return label && " L" + label;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) {
|
function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth, target) {
|
||||||
++stmtDepth;
|
++stmtDepth;
|
||||||
var loop = ++loops;
|
var loop = ++loops;
|
||||||
if (--recurmax < 0) {
|
if (--recurmax < 0) {
|
||||||
@@ -414,10 +448,11 @@ function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn
|
|||||||
}
|
}
|
||||||
|
|
||||||
// allow to forcefully generate certain structures at first or second recursion level
|
// allow to forcefully generate certain structures at first or second recursion level
|
||||||
var target = 0;
|
if (target === undefined) {
|
||||||
if (stmtDepth === 1 && STMT_FIRST_LEVEL_OVERRIDE >= 0) target = STMT_FIRST_LEVEL_OVERRIDE;
|
if (stmtDepth === 1 && STMT_FIRST_LEVEL_OVERRIDE >= 0) target = STMT_FIRST_LEVEL_OVERRIDE;
|
||||||
else if (stmtDepth === 2 && STMT_SECOND_LEVEL_OVERRIDE >= 0) target = STMT_SECOND_LEVEL_OVERRIDE;
|
else if (stmtDepth === 2 && STMT_SECOND_LEVEL_OVERRIDE >= 0) target = STMT_SECOND_LEVEL_OVERRIDE;
|
||||||
else target = STMTS_TO_USE[rng(STMTS_TO_USE.length)];
|
else target = STMTS_TO_USE[rng(STMTS_TO_USE.length)];
|
||||||
|
}
|
||||||
|
|
||||||
switch (target) {
|
switch (target) {
|
||||||
case STMT_BLOCK:
|
case STMT_BLOCK:
|
||||||
@@ -460,20 +495,22 @@ function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn
|
|||||||
case STMT_VAR:
|
case STMT_VAR:
|
||||||
switch (rng(3)) {
|
switch (rng(3)) {
|
||||||
case 0:
|
case 0:
|
||||||
|
unique_vars.push('c');
|
||||||
var name = createVarName(MANDATORY);
|
var name = createVarName(MANDATORY);
|
||||||
if (name === 'c') name = 'a';
|
unique_vars.pop();
|
||||||
return 'var ' + name + ';';
|
return 'var ' + name + ';';
|
||||||
case 1:
|
case 1:
|
||||||
// initializer can only have one expression
|
// initializer can only have one expression
|
||||||
|
unique_vars.push('c');
|
||||||
var name = createVarName(MANDATORY);
|
var name = createVarName(MANDATORY);
|
||||||
if (name === 'c') name = 'b';
|
unique_vars.pop();
|
||||||
return 'var ' + name + ' = ' + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ';';
|
return 'var ' + name + ' = ' + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ';';
|
||||||
default:
|
default:
|
||||||
// initializer can only have one expression
|
// initializer can only have one expression
|
||||||
|
unique_vars.push('c');
|
||||||
var n1 = createVarName(MANDATORY);
|
var n1 = createVarName(MANDATORY);
|
||||||
if (n1 === 'c') n1 = 'b';
|
|
||||||
var n2 = createVarName(MANDATORY);
|
var n2 = createVarName(MANDATORY);
|
||||||
if (n2 === 'c') n2 = 'b';
|
unique_vars.pop();
|
||||||
return 'var ' + n1 + ' = ' + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ', ' + n2 + ' = ' + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ';';
|
return 'var ' + n1 + ' = ' + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ', ' + n2 + ' = ' + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ';';
|
||||||
}
|
}
|
||||||
case STMT_RETURN_ETC:
|
case STMT_RETURN_ETC:
|
||||||
@@ -514,8 +551,11 @@ function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn
|
|||||||
var nameLenBefore = VAR_NAMES.length;
|
var nameLenBefore = VAR_NAMES.length;
|
||||||
var catchName = createVarName(MANDATORY);
|
var catchName = createVarName(MANDATORY);
|
||||||
var freshCatchName = VAR_NAMES.length !== nameLenBefore;
|
var freshCatchName = VAR_NAMES.length !== nameLenBefore;
|
||||||
|
if (!catch_redef) unique_vars.push(catchName);
|
||||||
s += ' catch (' + catchName + ') { ' + createStatements(3, recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + ' }';
|
s += ' catch (' + catchName + ') { ' + createStatements(3, recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + ' }';
|
||||||
if (freshCatchName) VAR_NAMES.splice(nameLenBefore, 1); // remove catch name
|
// remove catch name
|
||||||
|
if (!catch_redef) unique_vars.pop();
|
||||||
|
if (freshCatchName) VAR_NAMES.splice(nameLenBefore, 1);
|
||||||
}
|
}
|
||||||
if (n !== 0) s += ' finally { ' + createStatements(3, recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + ' }';
|
if (n !== 0) s += ' finally { ' + createStatements(3, recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + ' }';
|
||||||
return s;
|
return s;
|
||||||
@@ -593,8 +633,9 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
|
|||||||
case p++:
|
case p++:
|
||||||
case p++:
|
case p++:
|
||||||
var nameLenBefore = VAR_NAMES.length;
|
var nameLenBefore = VAR_NAMES.length;
|
||||||
|
unique_vars.push('c');
|
||||||
var name = createVarName(MAYBE); // note: this name is only accessible from _within_ the function. and immutable at that.
|
var name = createVarName(MAYBE); // note: this name is only accessible from _within_ the function. and immutable at that.
|
||||||
if (name == 'c') name = 'a';
|
unique_vars.pop();
|
||||||
var s = [];
|
var s = [];
|
||||||
switch (rng(5)) {
|
switch (rng(5)) {
|
||||||
case 0:
|
case 0:
|
||||||
@@ -636,7 +677,7 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
|
|||||||
strictMode()
|
strictMode()
|
||||||
);
|
);
|
||||||
if (instantiate) for (var i = rng(4); --i >= 0;) {
|
if (instantiate) for (var i = rng(4); --i >= 0;) {
|
||||||
if (rng(2)) s.push('this.' + getDotKey() + createAssignment() + _createBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ';');
|
if (rng(2)) s.push('this.' + getDotKey(true) + createAssignment() + _createBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ';');
|
||||||
else s.push('this[' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ']' + createAssignment() + _createBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ';');
|
else s.push('this[' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ']' + createAssignment() + _createBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ';');
|
||||||
}
|
}
|
||||||
s.push(
|
s.push(
|
||||||
@@ -646,7 +687,7 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
VAR_NAMES.length = nameLenBefore;
|
VAR_NAMES.length = nameLenBefore;
|
||||||
return s.join('\n');
|
return filterDirective(s).join('\n');
|
||||||
case p++:
|
case p++:
|
||||||
case p++:
|
case p++:
|
||||||
return createTypeofExpr(recurmax, stmtDepth, canThrow);
|
return createTypeofExpr(recurmax, stmtDepth, canThrow);
|
||||||
@@ -689,19 +730,19 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
|
|||||||
") || " + rng(10) + ").toString()[" +
|
") || " + rng(10) + ").toString()[" +
|
||||||
createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + "] ";
|
createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + "] ";
|
||||||
case p++:
|
case p++:
|
||||||
return createArrayLiteral(recurmax, COMMA_OK, stmtDepth, canThrow);
|
return createArrayLiteral(recurmax, stmtDepth, canThrow);
|
||||||
case p++:
|
case p++:
|
||||||
return createObjectLiteral(recurmax, COMMA_OK, stmtDepth, canThrow);
|
return createObjectLiteral(recurmax, stmtDepth, canThrow);
|
||||||
case p++:
|
case p++:
|
||||||
return createArrayLiteral(recurmax, COMMA_OK, stmtDepth, canThrow) + '[' +
|
return createArrayLiteral(recurmax, stmtDepth, canThrow) + '[' +
|
||||||
createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ']';
|
createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ']';
|
||||||
case p++:
|
case p++:
|
||||||
return createObjectLiteral(recurmax, COMMA_OK, stmtDepth, canThrow) + '[' +
|
return createObjectLiteral(recurmax, stmtDepth, canThrow) + '[' +
|
||||||
createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ']';
|
createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ']';
|
||||||
case p++:
|
case p++:
|
||||||
return createArrayLiteral(recurmax, COMMA_OK, stmtDepth, canThrow) + '.' + getDotKey();
|
return createArrayLiteral(recurmax, stmtDepth, canThrow) + '.' + getDotKey();
|
||||||
case p++:
|
case p++:
|
||||||
return createObjectLiteral(recurmax, COMMA_OK, stmtDepth, canThrow) + '.' + getDotKey();
|
return createObjectLiteral(recurmax, stmtDepth, canThrow) + '.' + getDotKey();
|
||||||
case p++:
|
case p++:
|
||||||
var name = getVarName();
|
var name = getVarName();
|
||||||
return name + ' && ' + name + '[' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ']';
|
return name + ' && ' + name + '[' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ']';
|
||||||
@@ -713,7 +754,7 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
|
|||||||
return _createExpression(recurmax, noComma, stmtDepth, canThrow);
|
return _createExpression(recurmax, noComma, stmtDepth, canThrow);
|
||||||
}
|
}
|
||||||
|
|
||||||
function createArrayLiteral(recurmax, noComma, stmtDepth, canThrow) {
|
function createArrayLiteral(recurmax, stmtDepth, canThrow) {
|
||||||
recurmax--;
|
recurmax--;
|
||||||
var arr = "[";
|
var arr = "[";
|
||||||
for (var i = rng(6); --i >= 0;) {
|
for (var i = rng(6); --i >= 0;) {
|
||||||
@@ -746,18 +787,56 @@ var KEYS = [
|
|||||||
"3",
|
"3",
|
||||||
].concat(SAFE_KEYS);
|
].concat(SAFE_KEYS);
|
||||||
|
|
||||||
function getDotKey() {
|
function getDotKey(assign) {
|
||||||
return SAFE_KEYS[rng(SAFE_KEYS.length)];
|
var key;
|
||||||
|
do {
|
||||||
|
key = SAFE_KEYS[rng(SAFE_KEYS.length)];
|
||||||
|
} while (assign && key == "length");
|
||||||
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createObjectLiteral(recurmax, noComma, stmtDepth, canThrow) {
|
function createAccessor(recurmax, stmtDepth, canThrow) {
|
||||||
recurmax--;
|
var namesLenBefore = VAR_NAMES.length;
|
||||||
var obj = "({";
|
var s;
|
||||||
for (var i = rng(6); --i >= 0;) {
|
var prop1 = getDotKey();
|
||||||
var key = KEYS[rng(KEYS.length)];
|
if (rng(2) == 0) {
|
||||||
obj += key + ":(" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + "), ";
|
s = [
|
||||||
|
'get ' + prop1 + '(){',
|
||||||
|
strictMode(),
|
||||||
|
createStatements(2, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth),
|
||||||
|
createStatement(recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth, STMT_RETURN_ETC),
|
||||||
|
'},'
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
var prop2;
|
||||||
|
do {
|
||||||
|
prop2 = getDotKey();
|
||||||
|
} while (prop1 == prop2);
|
||||||
|
s = [
|
||||||
|
'set ' + prop1 + '(' + createVarName(MANDATORY) + '){',
|
||||||
|
strictMode(),
|
||||||
|
createStatements(2, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth),
|
||||||
|
'this.' + prop2 + createAssignment() + _createBinaryExpr(recurmax, COMMA_OK, stmtDepth, canThrow) + ';',
|
||||||
|
'},'
|
||||||
|
];
|
||||||
}
|
}
|
||||||
return obj + "})";
|
VAR_NAMES.length = namesLenBefore;
|
||||||
|
return filterDirective(s).join('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
function createObjectLiteral(recurmax, stmtDepth, canThrow) {
|
||||||
|
recurmax--;
|
||||||
|
var obj = ['({'];
|
||||||
|
for (var i = rng(6); --i >= 0;) {
|
||||||
|
if (rng(20) == 0) {
|
||||||
|
obj.push(createAccessor(recurmax, stmtDepth, canThrow));
|
||||||
|
} else {
|
||||||
|
var key = KEYS[rng(KEYS.length)];
|
||||||
|
obj.push(key + ':(' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + '),');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
obj.push('})');
|
||||||
|
return obj.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
function createNestedBinaryExpr(recurmax, noComma, stmtDepth, canThrow) {
|
function createNestedBinaryExpr(recurmax, noComma, stmtDepth, canThrow) {
|
||||||
@@ -787,7 +866,7 @@ function _createSimpleBinaryExpr(recurmax, noComma, stmtDepth, canThrow) {
|
|||||||
return canThrow && rng(10) == 0 ? expr : '(' + assignee + ' && ' + expr + ')';
|
return canThrow && rng(10) == 0 ? expr : '(' + assignee + ' && ' + expr + ')';
|
||||||
case 4:
|
case 4:
|
||||||
assignee = getVarName();
|
assignee = getVarName();
|
||||||
expr = '(' + assignee + '.' + getDotKey() + createAssignment()
|
expr = '(' + assignee + '.' + getDotKey(true) + createAssignment()
|
||||||
+ _createBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ')';
|
+ _createBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ')';
|
||||||
return canThrow && rng(10) == 0 ? expr : '(' + assignee + ' && ' + expr + ')';
|
return canThrow && rng(10) == 0 ? expr : '(' + assignee + ' && ' + expr + ')';
|
||||||
default:
|
default:
|
||||||
@@ -844,17 +923,24 @@ function getVarName() {
|
|||||||
|
|
||||||
function createVarName(maybe, dontStore) {
|
function createVarName(maybe, dontStore) {
|
||||||
if (!maybe || rng(2)) {
|
if (!maybe || rng(2)) {
|
||||||
var name = VAR_NAMES[rng(VAR_NAMES.length)];
|
|
||||||
var suffix = rng(3);
|
var suffix = rng(3);
|
||||||
if (suffix) {
|
var name;
|
||||||
name += '_' + suffix;
|
do {
|
||||||
if (!dontStore) VAR_NAMES.push(name);
|
name = VAR_NAMES[rng(VAR_NAMES.length)];
|
||||||
}
|
if (suffix) name += '_' + suffix;
|
||||||
|
} while (unique_vars.indexOf(name) >= 0);
|
||||||
|
if (suffix && !dontStore) VAR_NAMES.push(name);
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (require.main !== module) {
|
||||||
|
exports.createTopLevelCode = createTopLevelCode;
|
||||||
|
exports.num_iterations = num_iterations;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
function try_beautify(code, result) {
|
function try_beautify(code, result) {
|
||||||
try {
|
try {
|
||||||
var beautified = UglifyJS.minify(code, {
|
var beautified = UglifyJS.minify(code, {
|
||||||
@@ -978,10 +1064,6 @@ var uglify_code, uglify_result, ok;
|
|||||||
for (var round = 1; round <= num_iterations; round++) {
|
for (var round = 1; round <= num_iterations; round++) {
|
||||||
process.stdout.write(round + " of " + num_iterations + "\r");
|
process.stdout.write(round + " of " + num_iterations + "\r");
|
||||||
|
|
||||||
VAR_NAMES.length = INITIAL_NAMES_LEN; // prune any previous names still in the list
|
|
||||||
loops = 0;
|
|
||||||
funcs = 0;
|
|
||||||
|
|
||||||
original_code = createTopLevelCode();
|
original_code = createTopLevelCode();
|
||||||
original_result = sandbox.run_code(original_code);
|
original_result = sandbox.run_code(original_code);
|
||||||
(typeof original_result != "string" ? fallback_options : minify_options).forEach(function(options) {
|
(typeof original_result != "string" ? fallback_options : minify_options).forEach(function(options) {
|
||||||
@@ -999,7 +1081,7 @@ for (var round = 1; round <= num_iterations; round++) {
|
|||||||
ok = uglify_code.name == original_result.name;
|
ok = uglify_code.name == original_result.name;
|
||||||
}
|
}
|
||||||
if (verbose || (verbose_interval && !(round % INTERVAL_COUNT)) || !ok) log(options);
|
if (verbose || (verbose_interval && !(round % INTERVAL_COUNT)) || !ok) log(options);
|
||||||
else if (verbose_error && typeof original_result != "string") {
|
else if (typeof original_result != "string") {
|
||||||
console.log("//=============================================================");
|
console.log("//=============================================================");
|
||||||
console.log("// original code");
|
console.log("// original code");
|
||||||
try_beautify(original_code, original_result);
|
try_beautify(original_code, original_result);
|
||||||
|
|||||||
Reference in New Issue
Block a user