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

This commit is contained in:
Huáng Jùnliàng 2020-03-24 15:52:36 -04:00 committed by GitHub
parent 025e4ae44c
commit 8e7ef30968
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 3 deletions

View File

@ -14,13 +14,13 @@ type WhitespaceObject = {
*/ */
function crawl(node, state = {}) { function crawl(node, state = {}) {
if (t.isMemberExpression(node)) { if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) {
crawl(node.object, state); crawl(node.object, state);
if (node.computed) crawl(node.property, state); if (node.computed) crawl(node.property, state);
} else if (t.isBinary(node) || t.isAssignmentExpression(node)) { } else if (t.isBinary(node) || t.isAssignmentExpression(node)) {
crawl(node.left, state); crawl(node.left, state);
crawl(node.right, state); crawl(node.right, state);
} else if (t.isCallExpression(node)) { } else if (t.isCallExpression(node) || t.isOptionalCallExpression(node)) {
state.hasCall = true; state.hasCall = true;
crawl(node.callee, state); crawl(node.callee, state);
} else if (t.isFunction(node)) { } else if (t.isFunction(node)) {
@ -119,7 +119,7 @@ export const nodes = {
}, },
/** /**
* Test if CallExpression needs whitespace. * Test if CallExpressionish needs whitespace.
*/ */
CallExpression(node: Object): ?WhitespaceObject { CallExpression(node: Object): ?WhitespaceObject {
@ -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. * Test if VariableDeclaration needs whitespace.
*/ */

View File

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

View File

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

View File

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

View File

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