From b3adb46ba985e043fda079f56cb263305c082108 Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Thu, 23 May 2019 01:01:48 +0300 Subject: [PATCH] Keep parens around non-null assertions for new-expressions only (#6140) --- CHANGELOG.unreleased.md | 11 +++++------ src/language-js/needs-parens.js | 12 +++++------- .../__snapshots__/jsfmt.spec.js.snap | 10 ++++++++-- tests/typescript_non_null/parens.ts | 5 ++++- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index c904f9130..5de189bd8 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -325,22 +325,19 @@ export default (function log() {}).toString(); export default (function log() {} as typeof console.log); ``` -### TypeScript: Keep parentheses around a function called with non-null assertion. ([6136] by [@sosukesuzuki]) +### TypeScript: Keep necessary parentheses around non-null assertions. ([#6136] by [@sosukesuzuki], [#6140] by [@thorn0]) -Previously, Prettier removes necessary parentheses around a call expression with non-null assertion. It happens when it's return value is called as function. +Previously, Prettier removed necessary parentheses around non-null assertions if the result of the assertion expression was called as a constructor. ```ts // Input -const a = (b()!)(); const b = new (c()!)(); // Output (Prettier stable) -const a = b()!(); const b = new c()!(); -// Output (prettier master) -const a = (b()!)(); +// Output (Prettier master) const b = new (c()!)(); ``` @@ -359,6 +356,7 @@ const b = new (c()!)(); [#6131]: https://github.com/prettier/prettier/pull/6131 [#6133]: https://github.com/prettier/prettier/pull/6133 [#6136]: https://github.com/prettier/prettier/pull/6136 +[#6140]: https://github.com/prettier/prettier/pull/6140 [@belochub]: https://github.com/belochub [@brainkim]: https://github.com/brainkim [@duailibe]: https://github.com/duailibe @@ -367,3 +365,4 @@ const b = new (c()!)(); [@jridgewell]: https://github.com/jridgewell [@jwbay]: https://github.com/jwbay [@sosukesuzuki]: https://github.com/sosukesuzuki +[@thorn0]: https://github.com/thorn0 diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index 1aade8973..74a3ad0e3 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -725,14 +725,12 @@ function needsParens(path, options) { return false; } return true; + case "TSNonNullExpression": { - if ( - node.expression.type !== "Identifier" && - (parent.type === "CallExpression" || parent.type === "NewExpression") - ) { - return true; - } - return false; + return ( + node.expression.type === "CallExpression" && + parent.type === "NewExpression" + ); } } diff --git a/tests/typescript_non_null/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_non_null/__snapshots__/jsfmt.spec.js.snap index ca951f3e5..62a40c40d 100644 --- a/tests/typescript_non_null/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_non_null/__snapshots__/jsfmt.spec.js.snap @@ -56,9 +56,12 @@ function* g() { return (yield * foo())!; } -const a = (b()!)(); +const a = (b()!)(); // parens aren't necessary const b = c!(); + +// parens are necessary if the expression result is called as a constructor const c = new (d()!)(); +const c = new (d()!); =====================================output===================================== (a ? b : c)![tokenKey]; @@ -73,8 +76,11 @@ function* g() { return (yield* foo())!; } -const a = (b()!)(); +const a = b()!(); // parens aren't necessary const b = c!(); + +// parens are necessary if the expression result is called as a constructor +const c = new (d()!)(); const c = new (d()!)(); ================================================================================ diff --git a/tests/typescript_non_null/parens.ts b/tests/typescript_non_null/parens.ts index b274e867f..b6d1721ec 100644 --- a/tests/typescript_non_null/parens.ts +++ b/tests/typescript_non_null/parens.ts @@ -10,6 +10,9 @@ function* g() { return (yield * foo())!; } -const a = (b()!)(); +const a = (b()!)(); // parens aren't necessary const b = c!(); + +// parens are necessary if the expression result is called as a constructor const c = new (d()!)(); +const c = new (d()!);