Skip to content

Commit 645e9e0

Browse files
authored
Simplify parsing and printing attributes of function and arguments (#5783)
1 parent 3bc159f commit 645e9e0

File tree

14 files changed

+244
-242
lines changed

14 files changed

+244
-242
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- `npm i -g rescript@9`
1919
- `rescript convert <reason files>`
2020
- Remove obsolete built-in project templates and the "rescript init" functionality. This will be replaced by the create-rescript-app project that is maintained separately.
21+
- Parse the attributes of labelled argument to the pattern attributes of argument instead of function.
2122

2223
#### :boom: Breaking Change
2324

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -49677,7 +49677,7 @@ let funExpr expr =
4967749677
collectNewTypes (stringLoc :: acc) returnExpr
4967849678
| returnExpr -> (List.rev acc, returnExpr)
4967949679
in
49680-
let rec collect n attrsBefore acc expr =
49680+
let rec collect attrsBefore acc expr =
4968149681
match expr with
4968249682
| {
4968349683
pexp_desc =
@@ -49688,48 +49688,26 @@ let funExpr expr =
4968849688
{pexp_desc = Pexp_apply _} );
4968949689
} ->
4969049690
(attrsBefore, List.rev acc, rewriteUnderscoreApply expr)
49691-
| {
49692-
pexp_desc = Pexp_fun (lbl, defaultExpr, pattern, returnExpr);
49693-
pexp_attributes = [];
49694-
} ->
49695-
let parameter = Parameter {attrs = []; lbl; defaultExpr; pat = pattern} in
49696-
collect (n + 1) attrsBefore (parameter :: acc) returnExpr
4969749691
| {pexp_desc = Pexp_newtype (stringLoc, rest); pexp_attributes = attrs} ->
4969849692
let stringLocs, returnExpr = collectNewTypes [stringLoc] rest in
4969949693
let param = NewTypes {attrs; locs = stringLocs} in
49700-
collect (n + 1) attrsBefore (param :: acc) returnExpr
49701-
| {pexp_desc = Pexp_fun _; pexp_attributes}
49702-
when pexp_attributes
49703-
|> List.exists (fun ({Location.txt}, _) ->
49704-
txt = "bs" || txt = "res.async")
49705-
&& n > 0 ->
49706-
(* stop here, the uncurried or async attribute always indicates the beginning of an arrow function
49707-
* e.g. `(. a) => (. b)` instead of `(. a, . b)` *)
49708-
(attrsBefore, List.rev acc, expr)
49694+
collect attrsBefore (param :: acc) returnExpr
4970949695
| {
49710-
pexp_desc =
49711-
Pexp_fun
49712-
(((Labelled _ | Optional _) as lbl), defaultExpr, pattern, returnExpr);
49713-
pexp_attributes = attrs;
49696+
pexp_desc = Pexp_fun (lbl, defaultExpr, pattern, returnExpr);
49697+
pexp_attributes = [];
4971449698
} ->
49715-
(* Normally attributes are attached to the labelled argument, e.g. (@foo ~x) => ...
49716-
In the case of `@res.async`, pass the attribute to the outside *)
49717-
let attrs_async, attrs_other =
49718-
attrs |> List.partition (fun ({Location.txt}, _) -> txt = "res.async")
49719-
in
49720-
let parameter =
49721-
Parameter {attrs = attrs_other; lbl; defaultExpr; pat = pattern}
49722-
in
49723-
collect (n + 1) (attrs_async @ attrsBefore) (parameter :: acc) returnExpr
49699+
let parameter = Parameter {attrs = []; lbl; defaultExpr; pat = pattern} in
49700+
collect attrsBefore (parameter :: acc) returnExpr
49701+
| {pexp_desc = Pexp_fun _} -> (attrsBefore, List.rev acc, expr)
4972449702
| expr -> (attrsBefore, List.rev acc, expr)
4972549703
in
4972649704
match expr with
4972749705
| {
49728-
pexp_desc = Pexp_fun (Nolabel, _defaultExpr, _pattern, _returnExpr);
49706+
pexp_desc = Pexp_fun (_, _defaultExpr, _pattern, _returnExpr);
4972949707
pexp_attributes = attrs;
4973049708
} as expr ->
49731-
collect 0 attrs [] {expr with pexp_attributes = []}
49732-
| expr -> collect 0 [] [] expr
49709+
collect attrs [] {expr with pexp_attributes = []}
49710+
| expr -> collect [] [] expr
4973349711

4973449712
let processBracesAttr expr =
4973549713
match expr.pexp_attributes with
@@ -57920,13 +57898,23 @@ and printExprFunParameters ~customLayout ~inCallback ~async ~uncurried
5792057898
attrs = [];
5792157899
lbl = Asttypes.Nolabel;
5792257900
defaultExpr = None;
57923-
pat = {Parsetree.ppat_desc = Ppat_var stringLoc};
57901+
pat =
57902+
{
57903+
Parsetree.ppat_desc = Ppat_var stringLoc;
57904+
Parsetree.ppat_attributes = attrs;
57905+
};
5792457906
};
5792557907
]
5792657908
when not uncurried ->
5792757909
let txtDoc =
5792857910
let var = printIdentLike stringLoc.txt in
57929-
let var = if hasConstraint then addParens var else var in
57911+
let var =
57912+
match attrs with
57913+
| [] -> if hasConstraint then addParens var else var
57914+
| attrs ->
57915+
let attrs = printAttributes ~customLayout attrs cmtTbl in
57916+
addParens (Doc.concat [attrs; var])
57917+
in
5793057918
if async then addAsync var else var
5793157919
in
5793257920
printComments txtDoc cmtTbl stringLoc.loc
@@ -58017,22 +58005,25 @@ and printExpFunParameter ~customLayout parameter cmtTbl =
5801758005
match (lbl, pattern) with
5801858006
| Asttypes.Nolabel, pattern -> printPattern ~customLayout pattern cmtTbl
5801958007
| ( (Asttypes.Labelled lbl | Optional lbl),
58020-
{
58021-
ppat_desc = Ppat_var stringLoc;
58022-
ppat_attributes = [] | [({Location.txt = "ns.namedArgLoc"}, _)];
58023-
} )
58008+
{ppat_desc = Ppat_var stringLoc; ppat_attributes} )
5802458009
when lbl = stringLoc.txt ->
5802558010
(* ~d *)
58026-
Doc.concat [Doc.text "~"; printIdentLike lbl]
58011+
Doc.concat
58012+
[
58013+
printAttributes ~customLayout ppat_attributes cmtTbl;
58014+
Doc.text "~";
58015+
printIdentLike lbl;
58016+
]
5802758017
| ( (Asttypes.Labelled lbl | Optional lbl),
5802858018
{
5802958019
ppat_desc = Ppat_constraint ({ppat_desc = Ppat_var {txt}}, typ);
58030-
ppat_attributes = [] | [({Location.txt = "ns.namedArgLoc"}, _)];
58020+
ppat_attributes;
5803158021
} )
5803258022
when lbl = txt ->
5803358023
(* ~d: e *)
5803458024
Doc.concat
5803558025
[
58026+
printAttributes ~customLayout ppat_attributes cmtTbl;
5803658027
Doc.text "~";
5803758028
printIdentLike lbl;
5803858029
Doc.text ": ";

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -49677,7 +49677,7 @@ let funExpr expr =
4967749677
collectNewTypes (stringLoc :: acc) returnExpr
4967849678
| returnExpr -> (List.rev acc, returnExpr)
4967949679
in
49680-
let rec collect n attrsBefore acc expr =
49680+
let rec collect attrsBefore acc expr =
4968149681
match expr with
4968249682
| {
4968349683
pexp_desc =
@@ -49688,48 +49688,26 @@ let funExpr expr =
4968849688
{pexp_desc = Pexp_apply _} );
4968949689
} ->
4969049690
(attrsBefore, List.rev acc, rewriteUnderscoreApply expr)
49691-
| {
49692-
pexp_desc = Pexp_fun (lbl, defaultExpr, pattern, returnExpr);
49693-
pexp_attributes = [];
49694-
} ->
49695-
let parameter = Parameter {attrs = []; lbl; defaultExpr; pat = pattern} in
49696-
collect (n + 1) attrsBefore (parameter :: acc) returnExpr
4969749691
| {pexp_desc = Pexp_newtype (stringLoc, rest); pexp_attributes = attrs} ->
4969849692
let stringLocs, returnExpr = collectNewTypes [stringLoc] rest in
4969949693
let param = NewTypes {attrs; locs = stringLocs} in
49700-
collect (n + 1) attrsBefore (param :: acc) returnExpr
49701-
| {pexp_desc = Pexp_fun _; pexp_attributes}
49702-
when pexp_attributes
49703-
|> List.exists (fun ({Location.txt}, _) ->
49704-
txt = "bs" || txt = "res.async")
49705-
&& n > 0 ->
49706-
(* stop here, the uncurried or async attribute always indicates the beginning of an arrow function
49707-
* e.g. `(. a) => (. b)` instead of `(. a, . b)` *)
49708-
(attrsBefore, List.rev acc, expr)
49694+
collect attrsBefore (param :: acc) returnExpr
4970949695
| {
49710-
pexp_desc =
49711-
Pexp_fun
49712-
(((Labelled _ | Optional _) as lbl), defaultExpr, pattern, returnExpr);
49713-
pexp_attributes = attrs;
49696+
pexp_desc = Pexp_fun (lbl, defaultExpr, pattern, returnExpr);
49697+
pexp_attributes = [];
4971449698
} ->
49715-
(* Normally attributes are attached to the labelled argument, e.g. (@foo ~x) => ...
49716-
In the case of `@res.async`, pass the attribute to the outside *)
49717-
let attrs_async, attrs_other =
49718-
attrs |> List.partition (fun ({Location.txt}, _) -> txt = "res.async")
49719-
in
49720-
let parameter =
49721-
Parameter {attrs = attrs_other; lbl; defaultExpr; pat = pattern}
49722-
in
49723-
collect (n + 1) (attrs_async @ attrsBefore) (parameter :: acc) returnExpr
49699+
let parameter = Parameter {attrs = []; lbl; defaultExpr; pat = pattern} in
49700+
collect attrsBefore (parameter :: acc) returnExpr
49701+
| {pexp_desc = Pexp_fun _} -> (attrsBefore, List.rev acc, expr)
4972449702
| expr -> (attrsBefore, List.rev acc, expr)
4972549703
in
4972649704
match expr with
4972749705
| {
49728-
pexp_desc = Pexp_fun (Nolabel, _defaultExpr, _pattern, _returnExpr);
49706+
pexp_desc = Pexp_fun (_, _defaultExpr, _pattern, _returnExpr);
4972949707
pexp_attributes = attrs;
4973049708
} as expr ->
49731-
collect 0 attrs [] {expr with pexp_attributes = []}
49732-
| expr -> collect 0 [] [] expr
49709+
collect attrs [] {expr with pexp_attributes = []}
49710+
| expr -> collect [] [] expr
4973349711

4973449712
let processBracesAttr expr =
4973549713
match expr.pexp_attributes with
@@ -57920,13 +57898,23 @@ and printExprFunParameters ~customLayout ~inCallback ~async ~uncurried
5792057898
attrs = [];
5792157899
lbl = Asttypes.Nolabel;
5792257900
defaultExpr = None;
57923-
pat = {Parsetree.ppat_desc = Ppat_var stringLoc};
57901+
pat =
57902+
{
57903+
Parsetree.ppat_desc = Ppat_var stringLoc;
57904+
Parsetree.ppat_attributes = attrs;
57905+
};
5792457906
};
5792557907
]
5792657908
when not uncurried ->
5792757909
let txtDoc =
5792857910
let var = printIdentLike stringLoc.txt in
57929-
let var = if hasConstraint then addParens var else var in
57911+
let var =
57912+
match attrs with
57913+
| [] -> if hasConstraint then addParens var else var
57914+
| attrs ->
57915+
let attrs = printAttributes ~customLayout attrs cmtTbl in
57916+
addParens (Doc.concat [attrs; var])
57917+
in
5793057918
if async then addAsync var else var
5793157919
in
5793257920
printComments txtDoc cmtTbl stringLoc.loc
@@ -58017,22 +58005,25 @@ and printExpFunParameter ~customLayout parameter cmtTbl =
5801758005
match (lbl, pattern) with
5801858006
| Asttypes.Nolabel, pattern -> printPattern ~customLayout pattern cmtTbl
5801958007
| ( (Asttypes.Labelled lbl | Optional lbl),
58020-
{
58021-
ppat_desc = Ppat_var stringLoc;
58022-
ppat_attributes = [] | [({Location.txt = "ns.namedArgLoc"}, _)];
58023-
} )
58008+
{ppat_desc = Ppat_var stringLoc; ppat_attributes} )
5802458009
when lbl = stringLoc.txt ->
5802558010
(* ~d *)
58026-
Doc.concat [Doc.text "~"; printIdentLike lbl]
58011+
Doc.concat
58012+
[
58013+
printAttributes ~customLayout ppat_attributes cmtTbl;
58014+
Doc.text "~";
58015+
printIdentLike lbl;
58016+
]
5802758017
| ( (Asttypes.Labelled lbl | Optional lbl),
5802858018
{
5802958019
ppat_desc = Ppat_constraint ({ppat_desc = Ppat_var {txt}}, typ);
58030-
ppat_attributes = [] | [({Location.txt = "ns.namedArgLoc"}, _)];
58020+
ppat_attributes;
5803158021
} )
5803258022
when lbl = txt ->
5803358023
(* ~d: e *)
5803458024
Doc.concat
5803558025
[
58026+
printAttributes ~customLayout ppat_attributes cmtTbl;
5803658027
Doc.text "~";
5803758028
printIdentLike lbl;
5803858029
Doc.text ": ";
@@ -284712,6 +284703,12 @@ and parseTernaryExpr leftOperand p =
284712284703
and parseEs6ArrowExpression ?context ?parameters p =
284713284704
let startPos = p.Parser.startPos in
284714284705
Parser.leaveBreadcrumb p Grammar.Es6ArrowExpr;
284706+
(* Parsing function parameters and attributes:
284707+
1. Basically, attributes outside of `(...)` are added to the function, except
284708+
the uncurried attribute `(.)` is added to the function. e.g. async, uncurried
284709+
284710+
2. Attributes inside `(...)` are added to the arguments regardless of whether
284711+
labeled, optional or nolabeled *)
284715284712
let parameters =
284716284713
match parameters with
284717284714
| Some params -> params
@@ -284785,12 +284782,6 @@ and parseParameter p =
284785284782
then
284786284783
let startPos = p.Parser.startPos in
284787284784
let uncurried = Parser.optional p Token.Dot in
284788-
(* two scenarios:
284789-
* attrs ~lbl ...
284790-
* attrs pattern
284791-
* Attributes before a labelled arg, indicate that it's on the whole arrow expr
284792-
* Otherwise it's part of the pattern
284793-
* *)
284794284785
let attrs = parseAttributes p in
284795284786
if p.Parser.token = Typ then (
284796284787
Parser.next p;
@@ -284808,9 +284799,9 @@ and parseParameter p =
284808284799
match p.Parser.token with
284809284800
| Comma | Equal | Rparen ->
284810284801
let loc = mkLoc startPos p.prevEndPos in
284811-
( attrs,
284802+
( [],
284812284803
Asttypes.Labelled lblName,
284813-
Ast_helper.Pat.var ~attrs:[propLocAttr] ~loc
284804+
Ast_helper.Pat.var ~attrs:(propLocAttr :: attrs) ~loc
284814284805
(Location.mkloc lblName loc) )
284815284806
| Colon ->
284816284807
let lblEnd = p.prevEndPos in
@@ -284820,25 +284811,29 @@ and parseParameter p =
284820284811
let pat =
284821284812
let pat = Ast_helper.Pat.var ~loc (Location.mkloc lblName loc) in
284822284813
let loc = mkLoc startPos p.prevEndPos in
284823-
Ast_helper.Pat.constraint_ ~attrs:[propLocAttr] ~loc pat typ
284814+
Ast_helper.Pat.constraint_ ~attrs:(propLocAttr :: attrs) ~loc pat
284815+
typ
284824284816
in
284825284817
(attrs, Asttypes.Labelled lblName, pat)
284826284818
| As ->
284827284819
Parser.next p;
284828284820
let pat =
284829284821
let pat = parseConstrainedPattern p in
284830-
{pat with ppat_attributes = propLocAttr :: pat.ppat_attributes}
284822+
{
284823+
pat with
284824+
ppat_attributes = (propLocAttr :: attrs) @ pat.ppat_attributes;
284825+
}
284831284826
in
284832-
(attrs, Asttypes.Labelled lblName, pat)
284827+
([], Asttypes.Labelled lblName, pat)
284833284828
| t ->
284834284829
Parser.err p (Diagnostics.unexpected t p.breadcrumbs);
284835284830
let loc = mkLoc startPos p.prevEndPos in
284836-
( attrs,
284831+
( [],
284837284832
Asttypes.Labelled lblName,
284838284833
Ast_helper.Pat.var ~loc (Location.mkloc lblName loc) ))
284839284834
| _ ->
284840284835
let pattern = parseConstrainedPattern p in
284841-
let attrs = List.concat [attrs; pattern.ppat_attributes] in
284836+
let attrs = List.concat [pattern.ppat_attributes; attrs] in
284842284837
([], Asttypes.Nolabel, {pattern with ppat_attributes = attrs})
284843284838
in
284844284839
match p.Parser.token with

0 commit comments

Comments
 (0)