deduplicate parenthesis around object and function literals (#2953)

This commit is contained in:
Alex Lam S.L
2018-02-25 02:14:33 +08:00
committed by GitHub
parent 455790202a
commit 52de64cf16
2 changed files with 15 additions and 2 deletions

View File

@@ -576,6 +576,7 @@ function OutputStream(options) {
indentation : function() { return indentation }, indentation : function() { return indentation },
current_width : function() { return current_col - indentation }, current_width : function() { return current_col - indentation },
should_break : function() { return options.width && this.current_width() >= options.width }, should_break : function() { return options.width && this.current_width() >= options.width },
has_parens : function() { return OUTPUT.slice(-1) == "(" },
newline : newline, newline : newline,
print : print, print : print,
space : space, space : space,
@@ -683,7 +684,7 @@ function OutputStream(options) {
// a function expression needs parens around it when it's provably // a function expression needs parens around it when it's provably
// the first token to appear in a statement. // the first token to appear in a statement.
PARENS(AST_Function, function(output){ PARENS(AST_Function, function(output){
if (first_in_statement(output)) { if (!output.has_parens() && first_in_statement(output)) {
return true; return true;
} }
@@ -704,7 +705,9 @@ function OutputStream(options) {
// same goes for an object literal, because otherwise it would be // same goes for an object literal, because otherwise it would be
// interpreted as a block of code. // interpreted as a block of code.
PARENS(AST_Object, first_in_statement); PARENS(AST_Object, function(output){
return !output.has_parens() && first_in_statement(output);
});
PARENS(AST_Unary, function(output){ PARENS(AST_Unary, function(output){
var p = output.parent(); var p = output.parent();

View File

@@ -2012,3 +2012,13 @@ issue_2898: {
} }
expect_stdout: "2" expect_stdout: "2"
} }
deduplicate_parenthesis: {
input: {
({}).a = b;
(({}).a = b)();
(function() {}).a = b;
((function() {}).a = b)();
}
expect_exact: "({}).a=b;({}.a=b)();(function(){}).a=b;(function(){}.a=b)();"
}