Skip to content

Commit

Permalink
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 4f197a6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 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
6 changes: 5 additions & 1 deletion src/language-js/printer-estree.js
Expand Up @@ -5740,7 +5740,11 @@ 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)) {
const leftNodeOprator = getBinaryishNodeNames(node[leftNodeName]).operator;
if (
node.type === "NGPipeExpression" ||
shouldFlatten(operator, leftNodeOprator)
) {
// 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 4f197a6

Please sign in to comment.