From a2328edced1fcd94802874598cfd79fbc6665510 Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Thu, 15 Jun 2017 09:34:12 +1000 Subject: [PATCH] TypeScript: keep parens around with yield/await non-null assertion, fixes #2137 (#2149) --- src/fast-path.js | 4 +++- .../__snapshots__/jsfmt.spec.js.snap | 16 ++++++++++++++++ tests/typescript_non_null/parens.ts | 8 ++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/fast-path.js b/src/fast-path.js index fd41810d..c734de4d 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -385,7 +385,8 @@ FastPath.prototype.needsParens = function(options) { if ( parent.type === "UnaryExpression" || parent.type === "AwaitExpression" || - parent.type === "TSAsExpression" + parent.type === "TSAsExpression" || + parent.type === "TSNonNullExpression" ) { return true; } @@ -398,6 +399,7 @@ FastPath.prototype.needsParens = function(options) { case "SpreadElement": case "SpreadProperty": case "TSAsExpression": + case "TSNonNullExpression": return true; case "MemberExpression": diff --git a/tests/typescript_non_null/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_non_null/__snapshots__/jsfmt.spec.js.snap index b5900514..6a27e7eb 100644 --- a/tests/typescript_non_null/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_non_null/__snapshots__/jsfmt.spec.js.snap @@ -3,8 +3,24 @@ exports[`parens.ts 1`] = ` (a ? b : c) ![tokenKey]; (a || b) ![tokenKey]; + +async function f() { + return (await foo())!; +} + +function* g() { + return (yield * foo())!; +} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (a ? b : c)![tokenKey]; (a || b)![tokenKey]; +async function f() { + return (await foo())!; +} + +function* g() { + return (yield* foo())!; +} + `; diff --git a/tests/typescript_non_null/parens.ts b/tests/typescript_non_null/parens.ts index 2f72c110..24d3d108 100644 --- a/tests/typescript_non_null/parens.ts +++ b/tests/typescript_non_null/parens.ts @@ -1,2 +1,10 @@ (a ? b : c) ![tokenKey]; (a || b) ![tokenKey]; + +async function f() { + return (await foo())!; +} + +function* g() { + return (yield * foo())!; +}