Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/printer-constraint-attribute.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix the formatter moving an attribute from a type constraint, such as `@foo (x: int)`, to the first operand of the expression.
5 changes: 5 additions & 0 deletions syntax/printer/res_parens.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ let expr_with_coercion_kind coercion_kind expr =
Pexp_constraint ({pexp_desc = Pexp_pack _}, {ptyp_desc = Ptyp_package _});
} ->
Nothing
(* With attributes, the printer puts the constraint or coercion in
parentheses itself, inside the attributes. *)
| {pexp_desc = Pexp_constraint _ | Pexp_coerce _; pexp_attributes = _ :: _}
->
Nothing
| {pexp_desc = Pexp_coerce _} -> coercion_kind
| {pexp_desc = Pexp_constraint _} -> Parenthesized
| _ -> Nothing)
Expand Down
6 changes: 5 additions & 1 deletion syntax/printer/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3771,7 +3771,11 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
| Braced braces -> print_braces doc expr braces
| Nothing -> doc
in
Doc.concat [expr_doc; Doc.text ": "; print_typ_expr ~state typ cmt_tbl]
let doc =
Doc.concat [expr_doc; Doc.text ": "; print_typ_expr ~state typ cmt_tbl]
in
(* Keep attributes on the constraint rather than its operand. *)
if e.pexp_attributes <> [] then add_parens doc else doc
| Pexp_letmodule ({txt = _modName}, _modExpr, _expr) ->
print_expression_block ~state ~braces:true e cmt_tbl
| Pexp_letexception (_extensionConstructor, _expr) ->
Expand Down
5 changes: 5 additions & 0 deletions tests/syntax_tests/data/printer/expr/constraint.res
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ let x = (street["number"]: int)
let x = (streets[0]: int)

let x = (true ? 1 : 0: int)

let constraintAttribute = @foo (v: b)
let constraintAttributeArgument = f(@foo (v: b))
let constraintAttributeOperand = @foo (v: int) + 1
let constraintAttributeBraced = @foo {(v: b)}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ let jsx = <Component value={v :> b}>{v :> b}</Component>
foo(/* before */ v /* operand */ :> /* type */ b /* after */)
foo(v :> b) // trailing line comment
let attributed = (@foo v :> b)
let coercionAttribute = (@foo (v :> b))
let coercionAttribute = @foo (v :> b)
let long = functionWithAVeryLongName(valueWithAVeryLongName :> typeWithAVeryLongName)

let arrayIndex = values[index :> int]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
let x = (Numbers.ten: int)

let x = (@attr 10: int)
let x = @attr (10: int)
let x = (@attr 10: int)

let x = (street["number"]: int)
let x = (streets[0]: int)

let x = (true ? 1 : 0: int)

let constraintAttribute = @foo (v: b)
let constraintAttributeArgument = f(@foo (v: b))
let constraintAttributeOperand = (@foo (v: int)) + 1
let constraintAttributeBraced = {(@foo (v: b))}
Loading