From 74f0d3979b6bcd8d3b0ebf74279b4f062253158d Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Fri, 2 Jun 2017 15:10:02 -0700 Subject: [PATCH] Add parenthesis for yield and await inside of `as` (#1915) Fixes #1913 --- src/fast-path.js | 4 +++- tests/typescript_as/__snapshots__/jsfmt.spec.js.snap | 12 ++++++++++++ tests/typescript_as/as.js | 6 ++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/fast-path.js b/src/fast-path.js index 7a0d263a..c59f9de6 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -354,7 +354,8 @@ FastPath.prototype.needsParens = function() { case "YieldExpression": if ( parent.type === "UnaryExpression" || - parent.type === "AwaitExpression" + parent.type === "AwaitExpression" || + parent.type === "TSAsExpression" ) { return true; } @@ -366,6 +367,7 @@ FastPath.prototype.needsParens = function() { case "LogicalExpression": case "SpreadElement": case "SpreadProperty": + case "TSAsExpression": return true; case "MemberExpression": diff --git a/tests/typescript_as/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_as/__snapshots__/jsfmt.spec.js.snap index 5f9b56ce..cf63b81f 100644 --- a/tests/typescript_as/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_as/__snapshots__/jsfmt.spec.js.snap @@ -9,6 +9,12 @@ start + (yearSelectTotal as number) scrollTop > (visibilityHeight as number) export default class Column extends (RcTable.Column as React.ComponentClass>) {} ({}) as {}; +function*g() { + const test = (yield 'foo') as number; +} +async function g() { + const test = (await 'foo') as number; +} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ const name = (description as DescriptionObject).name || (description as string); this.isTabActionBar((e.target || e.srcElement) as HTMLElement); @@ -22,5 +28,11 @@ export default class Column extends (RcTable.Column as React.ComponentClass< ColumnProps >) {} ({}) as {}; +function* g() { + const test = (yield "foo") as number; +} +async function g() { + const test = (await "foo") as number; +} `; diff --git a/tests/typescript_as/as.js b/tests/typescript_as/as.js index 112e38b9..7bd17f2c 100644 --- a/tests/typescript_as/as.js +++ b/tests/typescript_as/as.js @@ -6,3 +6,9 @@ start + (yearSelectTotal as number) scrollTop > (visibilityHeight as number) export default class Column extends (RcTable.Column as React.ComponentClass>) {} ({}) as {}; +function*g() { + const test = (yield 'foo') as number; +} +async function g() { + const test = (await 'foo') as number; +}