def(AST_Lambda, return_this);
def(AST_Node, return_this);
def(AST_Constant, function() {
- return this.getValue();
+ return this.value;
});
def(AST_Function, function(compressor) {
if (compressor.option("unsafe")) {
var sym = condition.right.expression;
if (!is_undeclared_ref(sym)) return;
var body;
- var undef = condition.left.getValue() == "undefined";
+ var undef = condition.left.value == "undefined";
switch (condition.operator) {
case "==":
body = undef ? alternative : consequent;
if (self.args.length == 1) {
var first = self.args[0];
if (first instanceof AST_Number) try {
- var length = first.getValue();
+ var length = first.value;
if (length > 6) break;
var elements = Array(length);
for (var i = 0; i < length; i++) elements[i] = make_node(AST_Hole, self);
// "undefined" == typeof x => undefined === x
else if (compressor.option("typeofs")
&& self.left instanceof AST_String
- && self.left.getValue() == "undefined"
+ && self.left.value == "undefined"
&& self.right instanceof AST_UnaryPrefix
&& self.right.operator == "typeof") {
var expr = self.right.expression;
}
break;
case "==":
- if (self.left instanceof AST_String && self.left.getValue() == "" && self.right.is_string(compressor)) {
+ if (self.left instanceof AST_String && self.left.value == "" && self.right.is_string(compressor)) {
return make_node(AST_UnaryPrefix, self, {
operator: "!",
expression: self.right
}
break;
case "!=":
- if (self.left instanceof AST_String && self.left.getValue() == "" && self.right.is_string(compressor)) {
+ if (self.left instanceof AST_String && self.left.value == "" && self.right.is_string(compressor)) {
return self.right.optimize(compressor);
}
break;
}
if (self.operator == "+") {
if (self.right instanceof AST_String
- && self.right.getValue() == ""
+ && self.right.value == ""
&& self.left.is_string(compressor)) {
return self.left.optimize(compressor);
}
if (self.left instanceof AST_String
- && self.left.getValue() == ""
+ && self.left.value == ""
&& self.right.is_string(compressor)) {
return self.right.optimize(compressor);
}
if (self.left instanceof AST_Binary
&& self.left.operator == "+"
&& self.left.left instanceof AST_String
- && self.left.left.getValue() == ""
+ && self.left.left.value == ""
&& self.right.is_string(compressor)) {
self.left = self.left.right;
return self.optimize(compressor);
self = make_node(AST_Binary, self, {
operator: "+",
left: make_node(AST_String, self.left, {
- value: "" + self.left.getValue() + self.right.left.getValue(),
+ value: "" + self.left.value + self.right.left.value,
start: self.left.start,
end: self.right.left.end
}),
operator: "+",
left: self.left.left,
right: make_node(AST_String, self.right, {
- value: "" + self.left.right.getValue() + self.right.getValue(),
+ value: "" + self.left.right.value + self.right.value,
start: self.left.right.start,
end: self.right.end
})
operator: "+",
left: self.left.left,
right: make_node(AST_String, self.left.right, {
- value: "" + self.left.right.getValue() + self.right.left.getValue(),
+ value: "" + self.left.right.value + self.right.left.value,
start: self.left.right.start,
end: self.right.left.end
})
// a + +b => +b + a
if (self.operator != "-"
&& self.operator != "/"
- && self.left.is_number(compressor)
- && self.right.is_number(compressor)
+ && (self.left.is_boolean(compressor) || self.left.is_number(compressor))
+ && (self.right.is_boolean(compressor) || self.right.is_number(compressor))
&& reversible()
&& !(self.left instanceof AST_Binary
&& self.left.operator != self.operator
}
break;
}
+ if (self.left instanceof AST_Number && !self.right.is_constant()) switch (self.operator) {
+ // 0 + n => n
+ case "+":
+ if (self.left.value == 0) {
+ if (self.right.is_number(compressor)) return self.right;
+ if (self.right.is_boolean(compressor)) return make_node(AST_UnaryPrefix, self, {
+ operator: "+",
+ expression: self.right
+ }).optimize(compressor);
+ }
+ break;
+ // 0 - n => -n
+ case "-":
+ if (self.left.value == 0) return make_node(AST_UnaryPrefix, self, {
+ operator: "-",
+ expression: self.right
+ }).optimize(compressor);
+ break;
+ // 1 * n => n
+ case "*":
+ if (self.left.value == 1) {
+ return self.right.is_number(compressor) ? self.right : make_node(AST_UnaryPrefix, self, {
+ operator: "+",
+ expression: self.right
+ }).optimize(compressor);
+ }
+ break;
+ }
+ // n - 0 => n
+ // n / 1 => n
+ if (self.right instanceof AST_Number && !self.left.is_constant() && self.right.value == {
+ "-": 0,
+ "/": 1,
+ }[self.operator]) return self.left.is_number(compressor) ? self.left : make_node(AST_UnaryPrefix, self, {
+ operator: "+",
+ expression: self.left
+ }).optimize(compressor);
}
if (compressor.option("typeofs")) switch (self.operator) {
case "&&":
&& indexRight
&& (self.operator == "==" || self.operator == "!=")
&& self.left instanceof AST_Number
- && self.left.getValue() == 0) {
+ && self.left.value == 0) {
return (self.operator == "==" ? make_node(AST_UnaryPrefix, self, {
operator: "!",
expression: self.right
switch (self.operator) {
case "<=":
// 0 <= array.indexOf(string) => !!~array.indexOf(string)
- return indexRight && self.left instanceof AST_Number && self.left.getValue() == 0;
+ return indexRight && self.left instanceof AST_Number && self.left.value == 0;
case "<":
// array.indexOf(string) < 0 => !~array.indexOf(string)
- if (indexLeft && self.right instanceof AST_Number && self.right.getValue() == 0) return true;
+ if (indexLeft && self.right instanceof AST_Number && self.right.value == 0) return true;
// -1 < array.indexOf(string) => !!~array.indexOf(string)
case "==":
case "!=":
// -1 == array.indexOf(string) => !~array.indexOf(string)
// -1 != array.indexOf(string) => !!~array.indexOf(string)
if (!indexRight) return false;
- return self.left instanceof AST_Number && self.left.getValue() == -1
+ return self.left instanceof AST_Number && self.left.value == -1
|| self.left instanceof AST_UnaryPrefix && self.left.operator == "-"
- && self.left.expression instanceof AST_Number && self.left.expression.getValue() == 1;
+ && self.left.expression instanceof AST_Number && self.left.expression.value == 1;
}
}
});
if ((self.operator == "-=" || self.operator == "+="
&& (self.left.is_boolean(compressor) || self.left.is_number(compressor)))
&& self.right instanceof AST_Number
- && self.right.getValue() === 1) {
+ && self.right.value == 1) {
var op = self.operator.slice(0, -1);
return make_node(AST_UnaryPrefix, self, {
operator: op + op,
return node instanceof AST_True
|| in_bool
&& node instanceof AST_Constant
- && node.getValue()
+ && node.value
|| (node instanceof AST_UnaryPrefix
&& node.operator == "!"
&& node.expression instanceof AST_Constant
- && !node.expression.getValue());
+ && !node.expression.value);
}
// AST_False or !1
function is_false(node) {
return node instanceof AST_False
|| in_bool
&& node instanceof AST_Constant
- && !node.getValue()
+ && !node.value
|| (node instanceof AST_UnaryPrefix
&& node.operator == "!"
&& node.expression instanceof AST_Constant
- && node.expression.getValue());
+ && node.expression.value);
}
function arg_diff() {
&& is_arguments(def = expr.definition())
&& prop instanceof AST_Number
&& (fn = expr.scope) === find_lambda()) {
- var index = prop.getValue();
+ var index = prop.value;
if (parent instanceof AST_UnaryPrefix && parent.operator == "delete") {
if (!def.deleted) def.deleted = [];
def.deleted[index] = true;
}
if (compressor.option("properties") && compressor.option("side_effects")
&& prop instanceof AST_Number && expr instanceof AST_Array) {
- var index = prop.getValue();
+ var index = prop.value;
var elements = expr.elements;
var retValue = elements[index];
if (safe_to_flatten(retValue, compressor)) {