Skip to content

Commit 43389c9

Browse files
authored
Cleanup pipe: remove |.u and change "|." to "->" to represent pipe. (rescript-lang#7244)
* Cleanup pipe: remove `|.u` which is effectively treated the same as `|.`. * Rename binary operator "|." to "->" in the internal representation. * Add ppx test for "->" representation. * Update CHANGELOG.md * Map between `->` and `|.` in the ppx conversion.
1 parent 1338ea2 commit 43389c9

37 files changed

+160
-148
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- AST cleanup: Remove `expression_desc.Pexp_new`, `expression_desc.Pexp_setinstvar`, `expression_desc.Pexp_override`, `expression_desc.Pexp_poly`, `exp_extra.Texp_poly`, `expression_desc.Texp_new`, `expression_desc.Texp_setinstvar`, `expression_desc.Texp_override` & `expression_desc.Texp_instvar` from AST as it is unused. https://github.com/rescript-lang/rescript/pull/7239
2828
- AST cleanup: Remove `@res.partial` attribute from the internal representation, and add a flag to untyped and typed ASTs instead. https://github.com/rescript-lang/rescript/pull/7238 https://github.com/rescript-lang/rescript/pull/7240
2929
- AST cleanup: Remove `structure_item_desc.Pstr_class`, `signature_item_desc.Psig_class`, `structure_item_desc.Pstr_class_type`, `signature_item_desc.Psig_class_type`, `structure_item_desc.Tstr_class`, `structure_item_desc.Tstr_class_type`, `signature_item_desc.Tsig_class`, `signature_item_desc.Tsig_class_type` from AST as it is unused. https://github.com/rescript-lang/rescript/pull/7242
30+
- AST cleanup: remove "|." and rename "|." to "->" in the internal representation for the pipe operator. https://github.com/rescript-lang/rescript/pull/7244
3031

3132
# 12.0.0-alpha.7
3233

analysis/examples/larger-project/src/res_comments_table.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ and walkExpr = (expr, t, comments) => {
12561256
| "/"
12571257
| "/."
12581258
| "**"
1259-
| "|."
1259+
| "->"
12601260
| "<>",
12611261
),
12621262
}),

analysis/examples/larger-project/src/res_parsetree_viewer.res

+4-4
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ let operatorPrecedence = operator =>
270270
| "+" | "+." | "-" | "-." | "^" => 5
271271
| "*" | "*." | "/" | "/." => 6
272272
| "**" => 7
273-
| "#" | "##" | "|." => 8
273+
| "#" | "##" | "->" => 8
274274
| _ => 0
275275
}
276276

@@ -312,7 +312,7 @@ let isBinaryOperator = operator =>
312312
| "/"
313313
| "/."
314314
| "**"
315-
| "|."
315+
| "->"
316316
| "<>" => true
317317
| _ => false
318318
}
@@ -649,15 +649,15 @@ let isSinglePipeExpr = expr => {
649649
let isPipeExpr = expr =>
650650
switch expr.pexp_desc {
651651
| Pexp_apply(
652-
{pexp_desc: Pexp_ident({txt: Longident.Lident("|." | "|>")})},
652+
{pexp_desc: Pexp_ident({txt: Longident.Lident("->" | "|>")})},
653653
list{(Nolabel, _operand1), (Nolabel, _operand2)},
654654
) => true
655655
| _ => false
656656
}
657657

658658
switch expr.pexp_desc {
659659
| Pexp_apply(
660-
{pexp_desc: Pexp_ident({txt: Longident.Lident("|." | "|>")})},
660+
{pexp_desc: Pexp_ident({txt: Longident.Lident("->" | "|>")})},
661661
list{(Nolabel, operand1), (Nolabel, _operand2)},
662662
) if !isPipeExpr(operand1) => true
663663
| _ => false

analysis/examples/larger-project/src/res_printer.res

-1
Original file line numberDiff line numberDiff line change
@@ -3495,7 +3495,6 @@ and printUnaryExpression = (expr, cmtTbl) => {
34953495
and printBinaryExpression = (expr: Parsetree.expression, cmtTbl) => {
34963496
let printBinaryOperator = (~inlineRhs, operator) => {
34973497
let operatorTxt = switch operator {
3498-
| "|." => "->"
34993498
| "^" => "++"
35003499
| "=" => "=="
35013500
| "==" => "==="

analysis/src/CompletionFrontEnd.ml

+10-15
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
218218
(match exprs with
219219
| [] -> None
220220
| exp :: _ -> exprToContextPath exp))
221-
| Pexp_ident {txt = Lident ("|." | "|.u")} -> None
221+
| Pexp_ident {txt = Lident "->"} -> None
222222
| Pexp_ident {txt; loc} ->
223223
Some
224224
(CPId {path = Utils.flattenLongIdent txt; completionContext = Value; loc})
@@ -258,7 +258,7 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
258258
{
259259
funct =
260260
{
261-
pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")};
261+
pexp_desc = Pexp_ident {txt = Lident "->"};
262262
pexp_loc;
263263
pexp_attributes;
264264
};
@@ -275,7 +275,7 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
275275
}
276276
| Pexp_apply
277277
{
278-
funct = {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}};
278+
funct = {pexp_desc = Pexp_ident {txt = Lident "->"}};
279279
args =
280280
[
281281
(_, lhs); (_, {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes});
@@ -328,15 +328,15 @@ let completePipeChain (exp : Parsetree.expression) =
328328
Example: someArray->Js.Array2.map(v => v + 2)-> *)
329329
| Pexp_apply
330330
{
331-
funct = {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}};
331+
funct = {pexp_desc = Pexp_ident {txt = Lident "->"}};
332332
args = [_; (_, {pexp_desc = Pexp_apply {funct = d}})];
333333
} ->
334334
exprToContextPath exp |> Option.map (fun ctxPath -> (ctxPath, d.pexp_loc))
335335
(* When the left side of the pipe we're completing is an identifier application.
336336
Example: someArray->filterAllTheGoodStuff-> *)
337337
| Pexp_apply
338338
{
339-
funct = {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}};
339+
funct = {pexp_desc = Pexp_ident {txt = Lident "->"}};
340340
args = [_; (_, {pexp_desc = Pexp_ident _; pexp_loc})];
341341
} ->
342342
exprToContextPath exp |> Option.map (fun ctxPath -> (ctxPath, pexp_loc))
@@ -1113,8 +1113,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
11131113
resetCurrentCtxPath oldCtxPath
11141114
| Pexp_apply
11151115
{
1116-
funct =
1117-
{pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u"); loc = opLoc}};
1116+
funct = {pexp_desc = Pexp_ident {txt = Lident "->"; loc = opLoc}};
11181117
args =
11191118
[
11201119
(_, lhs);
@@ -1292,7 +1291,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
12921291
else iterateJsxProps ~iterator jsxProps
12931292
| Pexp_apply
12941293
{
1295-
funct = {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}};
1294+
funct = {pexp_desc = Pexp_ident {txt = Lident "->"}};
12961295
args =
12971296
[
12981297
(_, lhs);
@@ -1304,19 +1303,15 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
13041303
setPipeResult ~lhs ~id |> ignore
13051304
| Pexp_apply
13061305
{
1307-
funct =
1308-
{
1309-
pexp_desc =
1310-
Pexp_ident {txt = Lident ("|." | "|.u"); loc = opLoc};
1311-
};
1306+
funct = {pexp_desc = Pexp_ident {txt = Lident "->"; loc = opLoc}};
13121307
args = [(_, lhs); _];
13131308
}
13141309
when Loc.end_ opLoc = posCursor ->
13151310
if Debug.verbose () then print_endline "[expr_iter] Case foo->";
13161311
setPipeResult ~lhs ~id:"" |> ignore
13171312
| Pexp_apply
13181313
{
1319-
funct = {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}};
1314+
funct = {pexp_desc = Pexp_ident {txt = Lident "->"}};
13201315
args = [_; (_, {pexp_desc = Pexp_apply {funct = funExpr; args}})];
13211316
}
13221317
when (* Normally named arg completion fires when the cursor is right after the expression.
@@ -1352,7 +1347,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
13521347
| Some argCompletable -> setResult argCompletable)
13531348
| Pexp_apply
13541349
{
1355-
funct = {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}};
1350+
funct = {pexp_desc = Pexp_ident {txt = Lident "->"}};
13561351
args = [_; _];
13571352
} ->
13581353
(* Ignore any other pipe. *)

analysis/src/SignatureHelp.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
365365
pexp_desc =
366366
Pexp_apply
367367
{
368-
funct = {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}};
368+
funct = {pexp_desc = Pexp_ident {txt = Lident "->"}};
369369
args =
370370
[
371371
_;

compiler/frontend/ast_exp_apply.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ let view_as_app (fn : exp) (s : string list) : app_pattern option =
6969
Some {op; loc = fn.pexp_loc; args = check_and_discard args}
7070
| _ -> None
7171

72-
let infix_ops = ["|."; "|.u"; "#="; "##"]
72+
let infix_ops = ["->"; "#="; "##"]
7373

7474
let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) : exp =
7575
match view_as_app e infix_ops with
76-
| Some {op = "|." | "|.u"; args = [a_; f_]; loc} -> (
76+
| Some {op = "->"; args = [a_; f_]; loc} -> (
7777
(*
7878
a |. f
7979
a |. f b c [@bs] --> f a b c [@bs]
@@ -193,7 +193,7 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) : exp =
193193
} ->
194194
gen_assignment obj name name_loc
195195
| _ -> Location.raise_errorf ~loc "invalid #= assignment"))
196-
| Some {op = "|."; loc} ->
196+
| Some {op = "->"; loc} ->
197197
Location.raise_errorf ~loc
198198
"invalid |. syntax, it can only be used as binary operator"
199199
| Some {op = "##"; loc} ->

compiler/ml/ast_mapper_from0.ml

+7
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,13 @@ module E = struct
311311
(sub.pat sub p) (sub.expr sub e)
312312
| Pexp_function _ -> assert false
313313
| Pexp_apply (e, l) ->
314+
let e =
315+
match (e.pexp_desc, l) with
316+
| ( Pexp_ident ({txt = Longident.Lident "|."} as lid),
317+
[(Nolabel, _); (Nolabel, _)] ) ->
318+
{e with pexp_desc = Pexp_ident {lid with txt = Longident.Lident "->"}}
319+
| _ -> e
320+
in
314321
let process_partial_app_attribute attrs =
315322
let rec process partial_app acc attrs =
316323
match attrs with

compiler/ml/ast_mapper_to0.ml

+10-2
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,20 @@ module E = struct
321321
~attrs:(arity_to_attributes arity)
322322
(Location.mkloc (Longident.Lident "Function$") e.pexp_loc)
323323
(Some e))
324-
| Pexp_apply {funct = e; args = l; partial} ->
324+
| Pexp_apply {funct = e; args; partial} ->
325+
let e =
326+
match (e.pexp_desc, args) with
327+
| ( Pexp_ident ({txt = Longident.Lident "->"} as lid),
328+
[(Nolabel, _); (Nolabel, _)] ) ->
329+
{e with pexp_desc = Pexp_ident {lid with txt = Longident.Lident "|."}}
330+
| _ -> e
331+
in
325332
let attrs =
326333
if partial then (Location.mknoloc "res.partial", Pt.PStr []) :: attrs
327334
else []
328335
in
329-
apply ~loc ~attrs (sub.expr sub e) (List.map (map_snd (sub.expr sub)) l)
336+
apply ~loc ~attrs (sub.expr sub e)
337+
(List.map (map_snd (sub.expr sub)) args)
330338
| Pexp_match (e, pel) ->
331339
match_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
332340
| Pexp_try (e, pel) -> try_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)

compiler/ml/env.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,7 @@ and check_value_name name loc =
16241624
(* Note: we could also check here general validity of the
16251625
identifier, to protect against bad identifiers forged by -pp or
16261626
-ppx preprocessors. *)
1627-
if name = "|." then raise (Error (Illegal_value_name (loc, name)))
1627+
if name = "->" then raise (Error (Illegal_value_name (loc, name)))
16281628
else if String.length name > 0 && name.[0] = '#' then
16291629
for i = 1 to String.length name - 1 do
16301630
if name.[i] = '#' then raise (Error (Illegal_value_name (loc, name)))

compiler/syntax/src/res_comments_table.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1340,8 +1340,8 @@ and walk_expression expr t comments =
13401340
Longident.Lident
13411341
( ":=" | "||" | "&&" | "=" | "==" | "<" | ">" | "!="
13421342
| "!==" | "<=" | ">=" | "|>" | "+" | "+." | "-" | "-."
1343-
| "++" | "^" | "*" | "*." | "/" | "/." | "**" | "|."
1344-
| "|.u" | "<>" );
1343+
| "++" | "^" | "*" | "*." | "/" | "/." | "**" | "->"
1344+
| "<>" );
13451345
};
13461346
};
13471347
args = [(Nolabel, operand1); (Nolabel, operand2)];

compiler/syntax/src/res_core.ml

+1-2
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,7 @@ let build_longident words =
391391

392392
let make_infix_operator (p : Parser.t) token start_pos end_pos =
393393
let stringified_token =
394-
if token = Token.MinusGreater then "|.u"
395-
else if token = Token.PlusPlus then "^"
394+
if token = Token.PlusPlus then "^"
396395
else if token = Token.BangEqual then "<>"
397396
else if token = Token.BangEqualEqual then "!="
398397
else if token = Token.Equal then (

compiler/syntax/src/res_parens.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ let flatten_operand_rhs parent_operator rhs =
192192

193193
let binary_operator_inside_await_needs_parens operator =
194194
ParsetreeViewer.operator_precedence operator
195-
< ParsetreeViewer.operator_precedence "|."
195+
< ParsetreeViewer.operator_precedence "->"
196196

197197
let lazy_or_assert_or_await_expr_rhs ?(in_await = false) expr =
198198
let opt_braces, _ = ParsetreeViewer.process_braces_attr expr in

compiler/syntax/src/res_parsetree_viewer.ml

+5-12
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ let operator_precedence operator =
274274
| "+" | "+." | "-" | "-." | "^" -> 5
275275
| "*" | "*." | "/" | "/." | "%" -> 6
276276
| "**" -> 7
277-
| "#" | "##" | "|." | "|.u" -> 8
277+
| "#" | "##" | "->" -> 8
278278
| _ -> 0
279279

280280
let is_unary_operator operator =
@@ -297,8 +297,8 @@ let is_unary_expression expr =
297297
let is_binary_operator operator =
298298
match operator with
299299
| ":=" | "||" | "&&" | "=" | "==" | "<" | ">" | "!=" | "!==" | "<=" | ">="
300-
| "|>" | "+" | "+." | "-" | "-." | "^" | "*" | "*." | "/" | "/." | "**" | "|."
301-
| "|.u" | "<>" | "%" ->
300+
| "|>" | "+" | "+." | "-" | "-." | "^" | "*" | "*." | "/" | "/." | "**" | "->"
301+
| "<>" | "%" ->
302302
true
303303
| _ -> false
304304

@@ -714,11 +714,7 @@ let is_single_pipe_expr expr =
714714
match expr.pexp_desc with
715715
| Pexp_apply
716716
{
717-
funct =
718-
{
719-
pexp_desc =
720-
Pexp_ident {txt = Longident.Lident ("|." | "|.u" | "|>")};
721-
};
717+
funct = {pexp_desc = Pexp_ident {txt = Longident.Lident ("->" | "|>")}};
722718
args = [(Nolabel, _operand1); (Nolabel, _operand2)];
723719
} ->
724720
true
@@ -727,10 +723,7 @@ let is_single_pipe_expr expr =
727723
match expr.pexp_desc with
728724
| Pexp_apply
729725
{
730-
funct =
731-
{
732-
pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|.u" | "|>")};
733-
};
726+
funct = {pexp_desc = Pexp_ident {txt = Longident.Lident ("->" | "|>")}};
734727
args = [(Nolabel, operand1); (Nolabel, _operand2)];
735728
}
736729
when not (is_pipe_expr operand1) ->

compiler/syntax/src/res_printer.ml

+6-8
Original file line numberDiff line numberDiff line change
@@ -3664,7 +3664,6 @@ and print_binary_expression ~state (expr : Parsetree.expression) cmt_tbl =
36643664
let print_binary_operator ~inline_rhs operator =
36653665
let operator_txt =
36663666
match operator with
3667-
| "|." | "|.u" -> "->"
36683667
| "^" -> "++"
36693668
| "=" -> "=="
36703669
| "==" -> "==="
@@ -3673,12 +3672,12 @@ and print_binary_expression ~state (expr : Parsetree.expression) cmt_tbl =
36733672
| txt -> txt
36743673
in
36753674
let spacing_before_operator =
3676-
if operator = "|." || operator = "|.u" then Doc.soft_line
3675+
if operator = "->" then Doc.soft_line
36773676
else if operator = "|>" then Doc.line
36783677
else Doc.space
36793678
in
36803679
let spacing_after_operator =
3681-
if operator = "|." || operator = "|.u" then Doc.nil
3680+
if operator = "->" then Doc.nil
36823681
else if operator = "|>" then Doc.space
36833682
else if inline_rhs then Doc.space
36843683
else Doc.line
@@ -3749,7 +3748,7 @@ and print_binary_expression ~state (expr : Parsetree.expression) cmt_tbl =
37493748
]
37503749
else
37513750
match operator with
3752-
| ("|." | "|.u") when is_multiline ->
3751+
| "->" when is_multiline ->
37533752
(* If the pipe-chain is written over multiple lines, break automatically
37543753
* `let x = a->b->c -> same line, break when line-width exceeded
37553754
* `let x = a->
@@ -3855,8 +3854,7 @@ and print_binary_expression ~state (expr : Parsetree.expression) cmt_tbl =
38553854
{
38563855
funct =
38573856
{
3858-
pexp_desc =
3859-
Pexp_ident {txt = Longident.Lident (("|." | "|.u" | "|>") as op)};
3857+
pexp_desc = Pexp_ident {txt = Longident.Lident (("->" | "|>") as op)};
38603858
};
38613859
args = [(Nolabel, lhs); (Nolabel, rhs)];
38623860
}
@@ -3874,8 +3872,8 @@ and print_binary_expression ~state (expr : Parsetree.expression) cmt_tbl =
38743872
print_attributes ~state expr.pexp_attributes cmt_tbl;
38753873
lhs_doc;
38763874
(match (lhs_has_comment_below, op) with
3877-
| true, ("|." | "|.u") -> Doc.concat [Doc.soft_line; Doc.text "->"]
3878-
| false, ("|." | "|.u") -> Doc.text "->"
3875+
| true, "->" -> Doc.concat [Doc.soft_line; Doc.text "->"]
3876+
| false, "->" -> Doc.text "->"
38793877
| true, "|>" -> Doc.concat [Doc.line; Doc.text "|> "]
38803878
| false, "|>" -> Doc.text " |> "
38813879
| _ -> Doc.nil);

tests/syntax_tests/data/parsing/errors/expressions/expected/block.res.txt

+3-4
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,14 @@ let findThreadByIdLinearScan [arity:2]~threads:((threads)[@res.namedArgLoc ])
7474
otherPersonIDWhichIsAlsoThreadID
7575
| Group { id } -> id
7676
| Unknown { id } ->
77-
(unknown.id |.u Js.String.make) |.u FBID.ofStringUnsafe in
77+
(unknown.id -> Js.String.make) -> FBID.ofStringUnsafe in
7878
thisId == id)
7979
[@res.braces ])))
8080
[@res.braces ])
81-
let x = ((loop 0 (Nil |.u (push doc)))[@res.braces ])
81+
let x = ((loop 0 (Nil -> (push doc)))[@res.braces ])
8282
;;match stack with
8383
| Empty -> [%rescript.exprhole ]
8484
| Cons (doc, rest) -> ()
85-
| Join (doc1, doc2) ->
86-
(buffer |.u (Buffer.add_string indentation); loop ())
85+
| Join (doc1, doc2) -> (buffer -> (Buffer.add_string indentation); loop ())
8786
let pipeline =
8887
match scheduler with | Some -> [%rescript.exprhole ] | None -> ()

0 commit comments

Comments
 (0)