Skip to content

Commit

Permalink
[flow] Fix printer output of casting syntax when LHS of as or `as c…
Browse files Browse the repository at this point in the history
…onst` has low precedence

Summary: Changelog: [ide] Fix printer output of casting syntax when LHS of `as` or `as const` has low precedence

Reviewed By: panagosg7

Differential Revision: D54875911

fbshipit-source-id: 2ff2b71ecdad45a5db8b506436191199e75ddf36
  • Loading branch information
gkz authored and facebook-github-bot committed Mar 14, 2024
1 parent 72d7cce commit 7a6d3fd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
6 changes: 6 additions & 0 deletions src/parser_utils/output/__tests__/js_layout_generator_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1980,7 +1980,13 @@ let tests =
assert_statement_string ~ctxt "type T<in out A>=A;"
);
("as_expression" >:: fun ctxt -> assert_expression_string ~ctxt "a as T");
( "as_expression_precedence" >:: fun ctxt ->
assert_expression_string ~ctxt ~pretty:true "(x ?? a) as T"
);
("as_const_expression" >:: fun ctxt -> assert_expression_string ~ctxt "a as const");
( "as_const_expression_precedence" >:: fun ctxt ->
assert_expression_string ~ctxt ~pretty:true "(x ?? a) as const"
);
("satisfies_expression" >:: fun ctxt -> assert_expression_string ~ctxt "a satisfies T");
( "type_parameter" >:: fun ctxt ->
assert_statement_string ~ctxt "type a<a>=a;";
Expand Down
19 changes: 13 additions & 6 deletions src/parser_utils/output/js_layout_generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1263,8 +1263,8 @@ and expression ?(ctxt = normal_context) ~opts (root_expr : (Loc.t, Loc.t) Ast.Ex
| E.JSXElement el -> jsx_element ~opts loc el
| E.JSXFragment fr -> jsx_fragment ~opts loc fr
| E.TypeCast cast -> type_cast ~opts loc cast
| E.AsConstExpression cast -> as_const_expression ~opts loc cast
| E.AsExpression cast -> as_expression ~opts loc cast
| E.AsConstExpression cast -> as_const_expression ~ctxt ~opts ~precedence loc cast
| E.AsExpression cast -> as_expression ~ctxt ~opts ~precedence loc cast
| E.TSSatisfies cast -> ts_satisfies ~opts loc cast
| E.Import { E.Import.argument; comments } ->
layout_node_with_comments_opt loc comments
Expand Down Expand Up @@ -1487,18 +1487,25 @@ and type_cast ~opts loc cast =
layout_node_with_comments_opt loc comments
@@ wrap_in_parens (fuse [expr_layout; type_annotation ~opts annot])

and as_const_expression ~opts loc cast =
and as_const_expression ~ctxt ~opts ~precedence loc cast =
let open Ast.Expression.AsConstExpression in
let { expression = expr; comments } = cast in
let expr_layout = expression ~opts expr in
let expr_layout = expression_with_parens ~ctxt ~opts ~precedence expr in
let rhs = [Atom "as"; space; Atom "const"] in
layout_node_with_comments_opt loc comments @@ fuse [expr_layout; space; fuse rhs]

and as_expression ~opts loc cast =
and as_expression ~ctxt ~opts ~precedence loc cast =
let open Ast.Expression.AsExpression in
let { expression = expr; annot = (_, annot); comments } = cast in
layout_node_with_comments_opt loc comments
@@ fuse [expression ~opts expr; space; Atom "as"; space; type_ ~opts annot]
@@ fuse
[
expression_with_parens ~ctxt ~opts ~precedence expr;
space;
Atom "as";
space;
type_ ~opts annot;
]

and ts_satisfies ~opts loc cast =
let open Ast.Expression.TSSatisfies in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type { E, C } from "./lib";import { foo, baz } from "./lib";
const a = require("./a");

module.exports = [
0 < 1 ? foo() : baz() as C | E
(0 < 1 ? foo() : baz()) as C | E
];

>>> Launching report...
Expand Down Expand Up @@ -86,7 +86,7 @@ import type { E, C } from "./lib";import { foo, baz } from "./lib";
const a = require("./a");

module.exports = [
0 < 1 ? foo() : baz() as C | E
(0 < 1 ? foo() : baz()) as C | E
];

>>> ./error-a.js
Expand All @@ -100,8 +100,8 @@ import { error_foo, error_bar } from "./error-lib";
const b = require("./error-b");

module.exports = [
0 < 1 ? foo() : error_foo() as C | C,
0 < 1 ? bar() : error_bar() as D | D,
(0 < 1 ? foo() : error_foo()) as C | C,
(0 < 1 ? bar() : error_bar()) as D | D,
];

>>> ./error-b.js
Expand All @@ -113,8 +113,8 @@ import { error_foo, error_baz } from "./error-lib";
const a = require("./error-a");

module.exports = [
0 < 1 ? foo() : error_foo() as C | C,
0 < 1 ? baz() : error_baz() as E | E,
(0 < 1 ? foo() : error_foo()) as C | C,
(0 < 1 ? baz() : error_baz()) as E | E,
];


Expand All @@ -124,14 +124,14 @@ module.exports = [
< 0 < 1 ? foo() : error_foo(),
< 0 < 1 ? bar() : error_bar(),
---
> 0 < 1 ? foo() : error_foo() as C | C,
> 0 < 1 ? bar() : error_bar() as D | D,
> (0 < 1 ? foo() : error_foo()) as C | C,
> (0 < 1 ? bar() : error_bar()) as D | D,

>>> ./error-b.js
9,10c9,10
< 0 < 1 ? foo() : error_foo(),
< 0 < 1 ? baz() : error_baz(),
---
> 0 < 1 ? foo() : error_foo() as C | C,
> 0 < 1 ? baz() : error_baz() as E | E,
> (0 < 1 ? foo() : error_foo()) as C | C,
> (0 < 1 ? baz() : error_baz()) as E | E,

Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = (foo() as C); // error: C is not exported
import type { D } from "./lib-2";import { foo, bar } from "./lib-2";

module.exports._1 = (foo() as C); // error: C is not exported
module.exports._2 = (0 < 1 ? foo() : bar() as C | D); // error: C is not exported, even though D is okay
module.exports._2 = ((0 < 1 ? foo() : bar()) as C | D); // error: C is not exported, even though D is okay
module.exports._3 = (bar() as D); // okay


Expand All @@ -70,5 +70,5 @@ module.exports._3 = (bar() as D); // okay
< module.exports._2 = 0 < 1 ? foo() : bar(); // error: C is not exported, even though D is okay
---
> module.exports._1 = (foo() as C); // error: C is not exported
> module.exports._2 = (0 < 1 ? foo() : bar() as C | D); // error: C is not exported, even though D is okay
> module.exports._2 = ((0 < 1 ? foo() : bar()) as C | D); // error: C is not exported, even though D is okay

0 comments on commit 7a6d3fd

Please sign in to comment.