Skip to content

Commit a4706ef

Browse files
cknittclaude
andcommitted
Remove obsolete attributes from the printer's filters and merge them
`res.ternary` and `JSX` are now only part of the v0 PPX wire format: the parser produces `Pexp_ternary` and `Pexp_jsx_element`, and the v0 bridge converts both markers back into those nodes. Drop them from `is_parsing_attr`, `has_attributes` and `is_printable_attribute`. `is_printable_attribute` then only differed from `is_parsing_attr` in `res.patVariantSpread` and `res.dictPattern`, which the parser only puts on patterns, while the printable helpers are only used on expressions and module expressions. Define the printable helpers in terms of `is_parsing_attr`, so the printer has one list of internal attributes instead of two that disagreed (the cause of the `@JSX` review finding in #8735). `filter_printable_attributes` was the same as `filter_parsing_attrs` and is removed. A `@JSX` attribute written in source now gets the same parens as any other attribute, e.g. `(@jsx x) + 1`. Formatting of the repo's sources is unchanged. Signed-off-by: Christoph Knittel <christoph@knittel.cc> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 51c9a0d commit a4706ef

3 files changed

Lines changed: 10 additions & 21 deletions

File tree

‎compiler/syntax/src/res_parsetree_viewer.ml‎

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ let is_parsing_attr (attr : Parsetree.attribute) =
223223
match attr with
224224
| ( {
225225
Location.txt =
226-
( "res.iflet" | "res.ternary" | "res.patVariantSpread"
227-
| "res.dictPattern" | "res.dictSpread" );
226+
( "res.iflet" | "res.patVariantSpread" | "res.dictPattern"
227+
| "res.dictSpread" );
228228
},
229229
_ ) ->
230230
true
@@ -382,7 +382,7 @@ let has_attributes attrs =
382382
List.exists
383383
(fun attr ->
384384
match attr with
385-
| {Location.txt = "res.iflet" | "res.ternary"}, _ -> false
385+
| {Location.txt = "res.iflet"}, _ -> false
386386
(* Remove the fragile pattern warning for iflet expressions *)
387387
| ( {Location.txt = "warning"},
388388
PStr
@@ -530,25 +530,16 @@ let should_inline_rhs_binary_expr rhs =
530530
true
531531
| _ -> false
532532

533-
let is_printable_attribute attr =
534-
match attr with
535-
| {Location.txt = "res.iflet" | "JSX" | "res.ternary" | "res.dictSpread"}, _
536-
->
537-
false
538-
| _ -> true
539-
540-
let has_printable_attributes attrs = List.exists is_printable_attribute attrs
533+
let has_printable_attributes attrs =
534+
List.exists (fun attr -> not (is_parsing_attr attr)) attrs
541535

542-
(* Attributes on a module expression, the ones [Res_printer.print_attributes]
543-
prints: they print before it and bind less tightly than an application or
544-
a constraint *)
536+
(* Attributes on a module expression print before it and bind less tightly
537+
than an application or a constraint *)
545538
let mod_expr_has_attributes (mod_expr : Parsetree.module_expr) =
546-
List.exists (fun attr -> not (is_parsing_attr attr)) mod_expr.pmod_attributes
547-
548-
let filter_printable_attributes attrs = List.filter is_printable_attribute attrs
539+
has_printable_attributes mod_expr.pmod_attributes
549540

550541
let partition_printable_attributes attrs =
551-
List.partition is_printable_attribute attrs
542+
List.partition (fun attr -> not (is_parsing_attr attr)) attrs
552543

553544
let is_doc_comment_attribute ((id, payload) : Parsetree.attribute) =
554545
match (id, payload) with

‎compiler/syntax/src/res_parsetree_viewer.mli‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ val should_indent_binary_expr : Parsetree.expression -> bool
9696
val should_inline_rhs_binary_expr : Parsetree.expression -> bool
9797
val has_printable_attributes : Parsetree.attributes -> bool
9898
val mod_expr_has_attributes : Parsetree.module_expr -> bool
99-
val filter_printable_attributes : Parsetree.attributes -> Parsetree.attributes
10099
val partition_printable_attributes :
101100
Parsetree.attributes -> Parsetree.attributes * Parsetree.attributes
102101
val is_doc_comment_attribute : Parsetree.attribute -> bool

‎compiler/syntax/src/res_printer.ml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4291,8 +4291,7 @@ and print_binary_expression ~state ~force_pipe_breaks
42914291
{
42924292
expr with
42934293
pexp_attributes =
4294-
Parsetree_viewer.filter_printable_attributes
4295-
expr.pexp_attributes;
4294+
Parsetree_viewer.filter_parsing_attrs expr.pexp_attributes;
42964295
}
42974296
with
42984297
| Braced braces_loc -> print_braces doc expr braces_loc

0 commit comments

Comments
 (0)