refactor brackets to braces (#3005)

This commit is contained in:
Alex Lam S.L
2018-03-15 15:46:45 +08:00
committed by GitHub
parent 90585e29c2
commit b29d435bb5
11 changed files with 32 additions and 32 deletions

View File

@@ -824,7 +824,7 @@ can pass additional arguments that control the code output:
when you want to generate minified code, in order to specify additional when you want to generate minified code, in order to specify additional
arguments, so you can use `-b beautify=false` to override it. arguments, so you can use `-b beautify=false` to override it.
- `bracketize` (default `false`) -- always insert brackets in `if`, `for`, - `braces` (default `false`) -- always insert braces in `if`, `for`,
`do`, `while` or `with` statements, even if their body is a single `do`, `while` or `with` statements, even if their body is a single
statement. statement.

View File

@@ -165,7 +165,7 @@ function walk_body(node, visitor) {
}; };
var AST_Block = DEFNODE("Block", "body", { var AST_Block = DEFNODE("Block", "body", {
$documentation: "A body of statements (usually bracketed)", $documentation: "A body of statements (usually braced)",
$propdoc: { $propdoc: {
body: "[AST_Statement*] an array of statements" body: "[AST_Statement*] an array of statements"
}, },

View File

@@ -56,7 +56,7 @@ function OutputStream(options) {
options = defaults(options, { options = defaults(options, {
ascii_only : false, ascii_only : false,
beautify : false, beautify : false,
bracketize : false, braces : false,
comments : false, comments : false,
ie8 : false, ie8 : false,
indent_level : 4, indent_level : 4,
@@ -886,22 +886,22 @@ function OutputStream(options) {
self.body.print(output); self.body.print(output);
output.semicolon(); output.semicolon();
}); });
function print_bracketed_empty(self, output) { function print_braced_empty(self, output) {
output.print("{"); output.print("{");
output.with_indent(output.next_indent(), function() { output.with_indent(output.next_indent(), function() {
output.append_comments(self, true); output.append_comments(self, true);
}); });
output.print("}"); output.print("}");
} }
function print_bracketed(self, output, allow_directives) { function print_braced(self, output, allow_directives) {
if (self.body.length > 0) { if (self.body.length > 0) {
output.with_block(function() { output.with_block(function() {
display_body(self.body, false, output, allow_directives); display_body(self.body, false, output, allow_directives);
}); });
} else print_bracketed_empty(self, output); } else print_braced_empty(self, output);
}; };
DEFPRINT(AST_BlockStatement, function(self, output){ DEFPRINT(AST_BlockStatement, function(self, output){
print_bracketed(self, output); print_braced(self, output);
}); });
DEFPRINT(AST_EmptyStatement, function(self, output){ DEFPRINT(AST_EmptyStatement, function(self, output){
output.semicolon(); output.semicolon();
@@ -996,7 +996,7 @@ function OutputStream(options) {
}); });
}); });
output.space(); output.space();
print_bracketed(self, output, true); print_braced(self, output, true);
}); });
DEFPRINT(AST_Lambda, function(self, output){ DEFPRINT(AST_Lambda, function(self, output){
self._do_print(output); self._do_print(output);
@@ -1037,7 +1037,7 @@ function OutputStream(options) {
/* -----[ if ]----- */ /* -----[ if ]----- */
function make_then(self, output) { function make_then(self, output) {
var b = self.body; var b = self.body;
if (output.option("bracketize") if (output.option("braces")
|| output.option("ie8") && b instanceof AST_Do) || output.option("ie8") && b instanceof AST_Do)
return make_block(b, output); return make_block(b, output);
// The squeezer replaces "block"-s that contain only a single // The squeezer replaces "block"-s that contain only a single
@@ -1046,7 +1046,7 @@ function OutputStream(options) {
// IF having an ELSE clause where the THEN clause ends in an // IF having an ELSE clause where the THEN clause ends in an
// IF *without* an ELSE block (then the outer ELSE would refer // IF *without* an ELSE block (then the outer ELSE would refer
// to the inner IF). This function checks for this case and // to the inner IF). This function checks for this case and
// adds the block brackets if needed. // adds the block braces if needed.
if (!b) return output.force_semicolon(); if (!b) return output.force_semicolon();
while (true) { while (true) {
if (b instanceof AST_If) { if (b instanceof AST_If) {
@@ -1093,7 +1093,7 @@ function OutputStream(options) {
}); });
output.space(); output.space();
var last = self.body.length - 1; var last = self.body.length - 1;
if (last < 0) print_bracketed_empty(self, output); if (last < 0) print_braced_empty(self, output);
else output.with_block(function(){ else output.with_block(function(){
self.body.forEach(function(branch, i){ self.body.forEach(function(branch, i){
output.indent(true); output.indent(true);
@@ -1127,7 +1127,7 @@ function OutputStream(options) {
DEFPRINT(AST_Try, function(self, output){ DEFPRINT(AST_Try, function(self, output){
output.print("try"); output.print("try");
output.space(); output.space();
print_bracketed(self, output); print_braced(self, output);
if (self.bcatch) { if (self.bcatch) {
output.space(); output.space();
self.bcatch.print(output); self.bcatch.print(output);
@@ -1144,12 +1144,12 @@ function OutputStream(options) {
self.argname.print(output); self.argname.print(output);
}); });
output.space(); output.space();
print_bracketed(self, output); print_braced(self, output);
}); });
DEFPRINT(AST_Finally, function(self, output){ DEFPRINT(AST_Finally, function(self, output){
output.print("finally"); output.print("finally");
output.space(); output.space();
print_bracketed(self, output); print_braced(self, output);
}); });
/* -----[ var/const ]----- */ /* -----[ var/const ]----- */
@@ -1348,7 +1348,7 @@ function OutputStream(options) {
}); });
output.newline(); output.newline();
}); });
else print_bracketed_empty(self, output); else print_braced_empty(self, output);
}); });
function print_property_name(key, quote, output) { function print_property_name(key, quote, output) {
@@ -1420,7 +1420,7 @@ function OutputStream(options) {
}); });
function force_statement(stat, output) { function force_statement(stat, output) {
if (output.option("bracketize")) { if (output.option("braces")) {
make_block(stat, output); make_block(stat, output);
} else { } else {
if (!stat || stat instanceof AST_EmptyStatement) if (!stat || stat instanceof AST_EmptyStatement)

View File

@@ -294,10 +294,10 @@ issue_186_beautify_ie8: {
] ]
} }
issue_186_bracketize: { issue_186_braces: {
beautify = { beautify = {
beautify: false, beautify: false,
bracketize: true, braces: true,
ie8: false, ie8: false,
} }
input: { input: {
@@ -314,10 +314,10 @@ issue_186_bracketize: {
expect_exact: 'var x=3;if(foo()){do{do{alert(x)}while(--x)}while(x)}else{bar()}' expect_exact: 'var x=3;if(foo()){do{do{alert(x)}while(--x)}while(x)}else{bar()}'
} }
issue_186_bracketize_ie8: { issue_186_braces_ie8: {
beautify = { beautify = {
beautify: false, beautify: false,
bracketize: true, braces: true,
ie8: true, ie8: true,
} }
input: { input: {
@@ -334,10 +334,10 @@ issue_186_bracketize_ie8: {
expect_exact: 'var x=3;if(foo()){do{do{alert(x)}while(--x)}while(x)}else{bar()}' expect_exact: 'var x=3;if(foo()){do{do{alert(x)}while(--x)}while(x)}else{bar()}'
} }
issue_186_beautify_bracketize: { issue_186_beautify_braces: {
beautify = { beautify = {
beautify: true, beautify: true,
bracketize: true, braces: true,
ie8: false, ie8: false,
} }
input: { input: {
@@ -366,10 +366,10 @@ issue_186_beautify_bracketize: {
] ]
} }
issue_186_beautify_bracketize_ie8: { issue_186_beautify_braces_ie8: {
beautify = { beautify = {
beautify: true, beautify: true,
bracketize: true, braces: true,
ie8: true, ie8: true,
} }
input: { input: {

View File

@@ -164,13 +164,13 @@ describe("bin/uglifyjs", function () {
done(); done();
}); });
}); });
it("Should work with `--beautify bracketize`", function (done) { it("Should work with `--beautify braces`", function (done) {
var command = uglifyjscmd + ' test/input/issue-1482/input.js -b bracketize'; var command = uglifyjscmd + ' test/input/issue-1482/input.js -b braces';
exec(command, function (err, stdout) { exec(command, function (err, stdout) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, read("test/input/issue-1482/bracketize.js")); assert.strictEqual(stdout, read("test/input/issue-1482/braces.js"));
done(); done();
}); });
}); });

View File

@@ -139,7 +139,7 @@ describe("Comment", function() {
assert.strictEqual(result.code, code); assert.strictEqual(result.code, code);
}); });
it("Should retain comments within brackets", function() { it("Should retain comments within braces", function() {
var code = [ var code = [
"{/* foo */}", "{/* foo */}",
"a({/* foo */});", "a({/* foo */});",

View File

@@ -17,7 +17,7 @@ describe("test/benchmark.js", function() {
this.timeout(10 * 60 * 1000); this.timeout(10 * 60 * 1000);
[ [
"-b", "-b",
"-b bracketize", "-b braces",
"-m", "-m",
"-mc passes=3", "-mc passes=3",
"-mc passes=3,toplevel", "-mc passes=3,toplevel",

View File

@@ -11,7 +11,7 @@ function try_beautify(code) {
mangle: false, mangle: false,
output: { output: {
beautify: true, beautify: true,
bracketize: true braces: true
} }
}); });
if (beautified.error) { if (beautified.error) {

View File

@@ -975,7 +975,7 @@ function try_beautify(code, result, printfn) {
mangle: false, mangle: false,
output: { output: {
beautify: true, beautify: true,
bracketize: true, braces: true,
}, },
}); });
if (beautified.error) { if (beautified.error) {

View File

@@ -4,7 +4,7 @@
"mangle": false, "mangle": false,
"output": { "output": {
"beautify": true, "beautify": true,
"bracketize": true "braces": true
}, },
"rename": true "rename": true
}, },