Skip to content

Commit

Permalink
Modify to wrap TSAsExpression with parens
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Mar 26, 2020
1 parent 289b993 commit 27e67ef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/common/util.js
Expand Up @@ -399,6 +399,11 @@ function shouldFlatten(parentOp, nodeOp) {
return false;
}

// foo as SomeType01 as SomeType02 --> (foo as SomeType01) as SomeType02
if (parentOp === "as" && nodeOp === "as") {
return false;
}

return true;
}

Expand Down
17 changes: 13 additions & 4 deletions src/language-js/printer-estree.js
Expand Up @@ -555,7 +555,7 @@ function printPathNoParens(path, options, print, args) {
case "LogicalExpression":
case "NGPipeExpression":
case "TSAsExpression": {
const { leftNodeName, rightNodeName } = getBinaryishNodeNames(n);
const { leftNodeName, rightNodeName, operator } = getBinaryishNodeNames(n);
const parent = path.getParentNode();
const parentParent = path.getParentNode(1);
const isInsideParenthesis =
Expand Down Expand Up @@ -616,7 +616,7 @@ function printPathNoParens(path, options, print, args) {
parent.type === "ThrowStatement" ||
(parent.type === "JSXExpressionContainer" &&
parentParent.type === "JSXAttribute") ||
(n.operator !== "|" && parent.type === "JsExpressionRoot") ||
(operator !== "|" && parent.type === "JsExpressionRoot") ||
(n.type !== "NGPipeExpression" &&
((parent.type === "NGRoot" && options.parser === "__ng_binding") ||
(parent.type === "NGMicrosyntaxExpression" &&
Expand All @@ -642,7 +642,10 @@ function printPathNoParens(path, options, print, args) {

const samePrecedenceSubExpression =
isBinaryish(n[leftNodeName]) &&
shouldFlatten(n.operator, n[leftNodeName].operator);
shouldFlatten(
operator,
getBinaryishNodeNames(n[leftNodeName]).operator
);

if (
shouldNotIndent ||
Expand Down Expand Up @@ -5740,7 +5743,13 @@ function printBinaryishExpressions(
// precedence level and should be treated as a separate group, so
// print them normally. (This doesn't hold for the `**` operator,
// which is unique in that it is right-associative.)
if (shouldFlatten(node.operator, node[leftNodeName].operator)) {
if (
node.type === "NGPipeExpression" ||
shouldFlatten(
operator,
getBinaryishNodeNames(node[leftNodeName]).operator
)
) {
// Flatten them out by recursively calling this function.
parts = parts.concat(
path.call(
Expand Down
Expand Up @@ -41,7 +41,7 @@ const bar8 = [1,2,3].reduce((carry, value) => {
=====================================output=====================================
const bar1 = [1, 2, 3].reduce((carry, value) => {
return [...carry, value];
}, [] as unknown as number[]);
}, ([] as unknown) as number[]);
const bar2 = [1, 2, 3].reduce((carry, value) => {
return [...carry, value];
Expand All @@ -51,7 +51,7 @@ const bar3 = [1, 2, 3].reduce(
(carry, value) => {
return [...carry, value];
},
[1, 2, 3] as unknown as number[]
([1, 2, 3] as unknown) as number[]
);
const bar4 = [1, 2, 3].reduce(
Expand All @@ -63,7 +63,7 @@ const bar4 = [1, 2, 3].reduce(
const bar5 = [1, 2, 3].reduce((carry, value) => {
return { ...carry, [value]: true };
}, {} as unknown as { [key: number]: boolean });
}, ({} as unknown) as { [key: number]: boolean });
const bar6 = [1, 2, 3].reduce((carry, value) => {
return { ...carry, [value]: true };
Expand All @@ -73,7 +73,7 @@ const bar7 = [1, 2, 3].reduce(
(carry, value) => {
return { ...carry, [value]: true };
},
{ 1: true } as unknown as { [key: number]: boolean }
({ 1: true } as unknown) as { [key: number]: boolean }
);
const bar8 = [1, 2, 3].reduce(
Expand Down

0 comments on commit 27e67ef

Please sign in to comment.