From 78a4f51662acb236f1a5f327b3429d5cd9cff931 Mon Sep 17 00:00:00 2001 From: Jed Fox Date: Wed, 14 Feb 2018 11:19:39 +0000 Subject: [PATCH] Wrap ternaries in Flow typecasts in parens (#3940) * Wrap TypeCastExpression in parens * Tests --- src/common/fast-path.js | 1 + .../__snapshots__/jsfmt.spec.js.snap | 25 +++++++++++++++++++ tests/flow_type_cast/expression.js | 4 +++ tests/flow_type_cast/jsfmt.spec.js | 1 + tests/flow_type_cast/statement.js | 3 +++ 5 files changed, 34 insertions(+) create mode 100644 tests/flow_type_cast/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/flow_type_cast/expression.js create mode 100644 tests/flow_type_cast/jsfmt.spec.js create mode 100644 tests/flow_type_cast/statement.js diff --git a/src/common/fast-path.js b/src/common/fast-path.js index 97e10252..eae670e9 100644 --- a/src/common/fast-path.js +++ b/src/common/fast-path.js @@ -560,6 +560,7 @@ FastPath.prototype.needsParens = function(options) { case "AwaitExpression": case "JSXSpreadAttribute": case "TSTypeAssertionExpression": + case "TypeCastExpression": case "TSAsExpression": case "TSNonNullExpression": return true; diff --git a/tests/flow_type_cast/__snapshots__/jsfmt.spec.js.snap b/tests/flow_type_cast/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..a413b447 --- /dev/null +++ b/tests/flow_type_cast/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,25 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`expression.js 1`] = ` +let x: string = (foo: string); + +// https://github.com/prettier/prettier/issues/3936 +const foo = ((1?2:3): number); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +let x: string = (foo: string); + +// https://github.com/prettier/prettier/issues/3936 +const foo = ((1 ? 2 : 3): number); + +`; + +exports[`statement.js 1`] = ` +foo: string; +bar: number; +(foo.bar: SomeType); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +foo: string; +bar: number; +(foo.bar: SomeType); + +`; diff --git a/tests/flow_type_cast/expression.js b/tests/flow_type_cast/expression.js new file mode 100644 index 00000000..50ecacd5 --- /dev/null +++ b/tests/flow_type_cast/expression.js @@ -0,0 +1,4 @@ +let x: string = (foo: string); + +// https://github.com/prettier/prettier/issues/3936 +const foo = ((1?2:3): number); diff --git a/tests/flow_type_cast/jsfmt.spec.js b/tests/flow_type_cast/jsfmt.spec.js new file mode 100644 index 00000000..c1ba82f4 --- /dev/null +++ b/tests/flow_type_cast/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["flow", "babylon"]); diff --git a/tests/flow_type_cast/statement.js b/tests/flow_type_cast/statement.js new file mode 100644 index 00000000..0dc735b4 --- /dev/null +++ b/tests/flow_type_cast/statement.js @@ -0,0 +1,3 @@ +foo: string; +bar: number; +(foo.bar: SomeType);