Add new option to preserve quoted props

Also added a couple of tests.
This commit is contained in:
Shrey Banga 2016-05-09 21:48:36 -07:00
parent 7bafacd236
commit a84003d156
2 changed files with 34 additions and 3 deletions

View File

@ -64,7 +64,8 @@ function OutputStream(options) {
preserve_line : false,
screw_ie8 : false,
preamble : null,
quote_style : 0
quote_style : 0,
keep_quoted_props: false
}, true);
var indentation = 0;
@ -1142,7 +1143,7 @@ function OutputStream(options) {
&& parseFloat(key) >= 0) {
output.print(make_num(key));
} else if (RESERVED_WORDS(key) ? output.option("screw_ie8") : is_identifier_string(key)) {
if (quote && output.option("quote_style")) {
if (quote && output.option("keep_quoted_props")) {
output.print_string(key, quote);
} else {
output.print_name(key);

View File

@ -7,5 +7,35 @@ describe("minify", function() {
var result = Uglify.minify(js, {fromString: true});
assert.strictEqual(result.code, 'function foo(n){return n?3:7}');
});
});
describe("keep_quoted_props", function() {
it("Should preserve quotes in object literals", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
var result = Uglify.minify(js, {
fromString: true, output: {
keep_quoted_props: true
}});
assert.strictEqual(result.code, 'var foo={"x":1,y:2,"z":3};');
});
it("Should preserve quote styles when quote_style is 3", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
var result = Uglify.minify(js, {
fromString: true, output: {
keep_quoted_props: true,
quote_style: 3
}});
assert.strictEqual(result.code, 'var foo={"x":1,y:2,\'z\':3};');
});
it("Should not preserve quotes in object literals when disabled", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
var result = Uglify.minify(js, {
fromString: true, output: {
keep_quoted_props: false,
quote_style: 3
}});
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
});
});
});