Skip to content

Commit

Permalink
polish: align optional chain whitespace behavior to their sibiling (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Mar 24, 2020
1 parent 025e4ae commit 8e7ef30
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/babel-generator/src/node/whitespace.js
Expand Up @@ -14,13 +14,13 @@ type WhitespaceObject = {
*/

function crawl(node, state = {}) {
if (t.isMemberExpression(node)) {
if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) {
crawl(node.object, state);
if (node.computed) crawl(node.property, state);
} else if (t.isBinary(node) || t.isAssignmentExpression(node)) {
crawl(node.left, state);
crawl(node.right, state);
} else if (t.isCallExpression(node)) {
} else if (t.isCallExpression(node) || t.isOptionalCallExpression(node)) {
state.hasCall = true;
crawl(node.callee, state);
} else if (t.isFunction(node)) {
Expand Down Expand Up @@ -119,7 +119,7 @@ export const nodes = {
},

/**
* Test if CallExpression needs whitespace.
* Test if CallExpressionish needs whitespace.
*/

CallExpression(node: Object): ?WhitespaceObject {
Expand All @@ -131,6 +131,15 @@ export const nodes = {
}
},

OptionalCallExpression(node: Object): ?WhitespaceObject {
if (t.isFunction(node.callee)) {
return {
before: true,
after: true,
};
}
},

/**
* Test if VariableDeclaration needs whitespace.
*/
Expand Down
@@ -0,0 +1,5 @@
(() => {})();
foo();

(() => {})?.();
foo();
@@ -0,0 +1,7 @@
(() => {})();

foo();

(() => {})?.();

foo();
@@ -0,0 +1,5 @@
foo = (() => {}).call();
foo;

foo = (() => {})?.call();
foo;
@@ -0,0 +1,7 @@
foo = (() => {}).call();

foo;

foo = (() => {})?.call();

foo;

0 comments on commit 8e7ef30

Please sign in to comment.