This commit is contained in:
Matt Basta 2014-02-08 10:45:38 -08:00
commit 0689695063
3 changed files with 61 additions and 1 deletions

View File

@ -256,6 +256,9 @@ to set `true`; it's effectively a shortcut for `foo=true`).
- `drop_console` -- default `false`. Pass `true` to discard calls to
`console.*` functions.
- `alphabetize` -- default `false`. Pass `true` to alphabetize keys in object
literals.
### The `unsafe` option
It enables some transformations that *might* break code logic in certain

View File

@ -73,6 +73,7 @@ function Compressor(options, false_by_default) {
screw_ie8 : false,
drop_console : false,
angular : false,
alphabetize : !false_by_default,
warnings : true,
global_defs : {}
@ -2368,7 +2369,28 @@ merge(Compressor.prototype, {
return self;
};
OPT(AST_Array, literals_in_boolean_context);
OPT(AST_Object, literals_in_boolean_context);
OPT(AST_RegExp, literals_in_boolean_context);
OPT(AST_Object, function(self, compressor) {
if (compressor.option("alphabetize") && self.properties.length) {
var sortedProps = self.properties.sort(function(a, b) {
if (a.key < b.key) {
return -1;
}
if (a.key > b.key) {
return 1;
}
return 0;
});
for (var i = self.properties.length; i--;) {
if (self.properties[i] !== sortedProps[i]) {
return make_node(AST_Object, self, {
properties: sortedProps
});
}
}
}
return literals_in_boolean_context(self, compressor);
});
})();

35
test/compress/objects.js Normal file
View File

@ -0,0 +1,35 @@
alphabetize_disabled_by_default: {
options = {};
input: {
foo({
c: 1,
a: 2,
b: 3
});
}
expect: {
foo({
c: 1,
a: 2,
b: 3
});
}
}
alphabetize_orders_keys: {
options = { alphabetize: true };
input: {
foo({
c: 1,
a: 2,
b: 3
});
}
expect: {
foo({
a: 2,
b: 3,
c: 1
});
}
}