Skip to content

Commit 09903f9

Browse files
authored
Allow skipping leading pipe in definitions of variants with attribute on leading constructor (#7782)
* allow skipping leading pipe in definitions of variants with attribute on first constructor * changelog
1 parent 9efa7e2 commit 09903f9

File tree

6 files changed

+40
-1
lines changed

6 files changed

+40
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#### :nail_care: Polish
3131

32+
- Allow skipping the leading pipe in variant definition with a leading constructor with an attribute. https://github.com/rescript-lang/rescript/pull/7782
33+
3234
#### :house: Internal
3335

3436
# 12.0.0-beta.6

compiler/syntax/src/res_core.ml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5044,7 +5044,7 @@ and parse_type_representation ?current_type_name_path ?inline_types_context p =
50445044
in
50455045
let kind =
50465046
match p.Parser.token with
5047-
| Bar | Uident _ | DocComment _ ->
5047+
| Bar | Uident _ | DocComment _ | At ->
50485048
Parsetree.Ptype_variant (parse_type_constructor_declarations p)
50495049
| Lbrace ->
50505050
Parsetree.Ptype_record
@@ -5602,6 +5602,36 @@ and parse_type_equation_and_representation ?current_type_name_path
56025602
| Bar | DotDot | DocComment _ ->
56035603
let priv, kind = parse_type_representation p in
56045604
(None, priv, kind)
5605+
| At -> (
5606+
(* Attribute can start a variant constructor or a type manifest.
5607+
Look ahead past attributes; if a constructor-like token follows (Uident not immediately
5608+
followed by a Dot, or DotDotDot/Bar/DocComment), treat as variant; otherwise manifest *)
5609+
let is_variant_after_attrs =
5610+
Parser.lookahead p (fun state ->
5611+
ignore (parse_attributes state);
5612+
match state.Parser.token with
5613+
| Uident _ -> (
5614+
Parser.next state;
5615+
match state.Parser.token with
5616+
| Dot -> false
5617+
| _ -> true)
5618+
| DotDotDot | Bar | DocComment _ -> true
5619+
| _ -> false)
5620+
in
5621+
if is_variant_after_attrs then
5622+
let priv, kind = parse_type_representation p in
5623+
(None, priv, kind)
5624+
else
5625+
let manifest = Some (parse_typ_expr p) in
5626+
match p.Parser.token with
5627+
| Equal ->
5628+
Parser.next p;
5629+
let priv, kind =
5630+
parse_type_representation ?current_type_name_path
5631+
?inline_types_context p
5632+
in
5633+
(manifest, priv, kind)
5634+
| _ -> (manifest, Public, Parsetree.Ptype_abstract))
56055635
| _ -> (
56065636
let manifest = Some (parse_typ_expr p) in
56075637
match p.Parser.token with
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type nonrec t =
2+
| One [@as {js|one|js}]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type t = @as("one") One
2+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type t = | @as("one") One
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type t = @as("one") One
2+

0 commit comments

Comments
 (0)