Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polish: align optional chain whitespace behavior to their sibiling #11328

Merged
merged 1 commit into from Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;