Skip to content

Commit 43389c9

Browse files
authored
Cleanup pipe: remove |.u and change "|." to "->" to represent pipe. (#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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 10 additions & 15 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 10 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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)))

0 commit comments

Comments
 (0)