Skip to content

Commit

Permalink
Remove isCurlyBracket and isEmptyBlock (#1221)
Browse files Browse the repository at this point in the history
We shouldn't be reading from the printed document but instead look at the AST. This was really easy, it can either be EmptyStatement, BlockStatement or anything else.
  • Loading branch information
vjeux committed Apr 13, 2017
1 parent f68531a commit 33fdcae
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/printer.js
Expand Up @@ -973,10 +973,10 @@ function genericPrintNoParens(path, options, print, args) {
"with (",
path.call(print, "object"),
")",
adjustClause(path.call(print, "body"), options)
adjustClause(n.body, path.call(print, "body"))
]);
case "IfStatement":
const con = adjustClause(path.call(print, "consequent"), options);
const con = adjustClause(n.consequent, path.call(print, "consequent"));
const opening = group(
concat([
"if (",
Expand All @@ -994,10 +994,7 @@ function genericPrintNoParens(path, options, print, args) {
parts.push(opening);

if (n.alternate) {
const hasBraces = isCurlyBracket(con);
const isEmpty = isEmptyBlock(con);

if (hasBraces && !isEmpty) {
if (n.consequent.type === "BlockStatement") {
parts.push(" else");
} else {
parts.push(hardline, "else");
Expand All @@ -1006,8 +1003,8 @@ function genericPrintNoParens(path, options, print, args) {
parts.push(
group(
adjustClause(
n.alternate,
path.call(print, "alternate"),
options,
n.alternate.type === "IfStatement"
)
)
Expand All @@ -1016,7 +1013,7 @@ function genericPrintNoParens(path, options, print, args) {

return concat(parts);
case "ForStatement": {
const body = adjustClause(path.call(print, "body"), options);
const body = adjustClause(n.body, path.call(print, "body"));

// We want to keep dangling comments above the loop to stay consistent.
// Any comment positioned between the for statement and the parentheses
Expand Down Expand Up @@ -1066,7 +1063,7 @@ function genericPrintNoParens(path, options, print, args) {
])
),
")",
adjustClause(path.call(print, "body"), options)
adjustClause(n.body, path.call(print, "body"))
]);
case "ForInStatement":
// Note: esprima can't actually parse "for each (".
Expand All @@ -1076,7 +1073,7 @@ function genericPrintNoParens(path, options, print, args) {
" in ",
path.call(print, "right"),
")",
adjustClause(path.call(print, "body"), options)
adjustClause(n.body, path.call(print, "body"))
]);

case "ForOfStatement":
Expand All @@ -1094,17 +1091,20 @@ function genericPrintNoParens(path, options, print, args) {
" of ",
path.call(print, "right"),
")",
adjustClause(path.call(print, "body"), options)
adjustClause(n.body, path.call(print, "body"))
]);

case "DoWhileStatement":
var clause = adjustClause(path.call(print, "body"), options);
var clause = adjustClause(n.body, path.call(print, "body"));
var doBody = concat(["do", clause]);
var parts = [doBody];
const hasBraces = isCurlyBracket(clause);

if (hasBraces) parts.push(" while");
else parts.push(concat([hardline, "while"]));
if (n.body.type === "BlockStatement") {
parts.push(" ");
} else {
parts.push(hardline);
}
parts.push("while");

parts.push(" (", path.call(print, "test"), ")", semi);

Expand Down Expand Up @@ -1208,7 +1208,7 @@ function genericPrintNoParens(path, options, print, args) {
);
}, "consequent");
parts.push(
isCurlyBracket(cons)
n.consequent.length === 1 && n.consequent[0].type === "BlockStatement"
? concat([" ", cons])
: indent(concat([hardline, cons]))
);
Expand Down Expand Up @@ -3185,12 +3185,12 @@ function printAssignment(
return group(concat([printedLeft, " ", operator, printed]));
}

function adjustClause(clause, options, forceSpace) {
if (clause === "") {
function adjustClause(node, clause, forceSpace) {
if (node.type === "EmptyStatement") {
return ";";
}

if (isCurlyBracket(clause) || forceSpace) {
if (node.type === "BlockStatement" || forceSpace) {
return concat([" ", clause]);
}

Expand Down

0 comments on commit 33fdcae

Please sign in to comment.