@@ -415154,13 +415154,13 @@ let shouldInlineRhsBinaryExpr rhs = match rhs.pexp_desc with
415154
415154
415155
415155
let filterPrinteableAttributes attrs =
415156
415156
List.filter (fun attr -> match attr with
415157
- | ({Location.txt="bs" | "ns.ternary" | "ns.iflet"}, _) -> false
415157
+ | ({Location.txt="bs" | "ns.ternary" | "ns.iflet" | "JSX" }, _) -> false
415158
415158
| _ -> true
415159
415159
) attrs
415160
415160
415161
415161
let partitionPrinteableAttributes attrs =
415162
415162
List.partition (fun attr -> match attr with
415163
- | ({Location.txt="bs" | "ns.ternary" | "ns.iflet"}, _) -> false
415163
+ | ({Location.txt="bs" | "ns.ternary" | "ns.iflet" | "JSX" }, _) -> false
415164
415164
| _ -> true
415165
415165
) attrs
415166
415166
@@ -416933,7 +416933,11 @@ and walkExprArgument (_argLabel, expr) t comments =
416933
416933
| Ppat_construct (constr, Some pat) ->
416934
416934
let (leading, trailing) = partitionLeadingTrailing comments constr.loc in
416935
416935
attach t.leading constr.loc leading;
416936
- let (leading, inside, trailing) = partitionByLoc trailing pat.ppat_loc in
416936
+ let (afterConstructor, rest) =
416937
+ partitionAdjacentTrailing constr.loc trailing
416938
+ in
416939
+ attach t.trailing constr.loc afterConstructor;
416940
+ let (leading, inside, trailing) = partitionByLoc rest pat.ppat_loc in
416937
416941
attach t.leading pat.ppat_loc leading;
416938
416942
walkPattern pat t inside;
416939
416943
attach t.trailing pat.ppat_loc trailing
@@ -420405,15 +420409,41 @@ let printIdentLike ?allowUident txt =
420405
420409
]
420406
420410
| NormalIdent -> Doc.text txt
420407
420411
420412
+ let rec unsafe_for_all_range s ~start ~finish p =
420413
+ start > finish ||
420414
+ p (String.unsafe_get s start) &&
420415
+ unsafe_for_all_range s ~start:(start + 1) ~finish p
420416
+
420417
+ let for_all_from s start p =
420418
+ let len = String.length s in
420419
+ unsafe_for_all_range s ~start ~finish:(len - 1) p
420420
+
420421
+ (* See https://github.com/rescript-lang/rescript-compiler/blob/726cfa534314b586e5b5734471bc2023ad99ebd9/jscomp/ext/ext_string.ml#L510 *)
420422
+ let isValidNumericPolyvarNumber (x : string) =
420423
+ let len = String.length x in
420424
+ len > 0 && (
420425
+ let a = Char.code (String.unsafe_get x 0) in
420426
+ a <= 57 &&
420427
+ (if len > 1 then
420428
+ a > 48 &&
420429
+ for_all_from x 1 (function '0' .. '9' -> true | _ -> false)
420430
+ else
420431
+ a >= 48 )
420432
+ )
420433
+
420408
420434
(* Exotic identifiers in poly-vars have a "lighter" syntax: #"ease-in" *)
420409
420435
let printPolyVarIdent txt =
420410
- match classifyIdentContent ~allowUident:true txt with
420411
- | ExoticIdent -> Doc.concat [
420412
- Doc.text "\"";
420413
- Doc.text txt;
420414
- Doc.text"\""
420415
- ]
420416
- | NormalIdent -> Doc.text txt
420436
+ (* numeric poly-vars don't need quotes: #644 *)
420437
+ if isValidNumericPolyvarNumber txt then
420438
+ Doc.text txt
420439
+ else
420440
+ match classifyIdentContent ~allowUident:true txt with
420441
+ | ExoticIdent -> Doc.concat [
420442
+ Doc.text "\"";
420443
+ Doc.text txt;
420444
+ Doc.text"\""
420445
+ ]
420446
+ | NormalIdent -> Doc.text txt
420417
420447
420418
420448
420419
420449
let printLident l = match l with
@@ -422118,7 +422148,7 @@ and printPattern (p : Parsetree.pattern) cmtTbl =
422118
422148
])
422119
422149
)
422120
422150
| Ppat_construct(constrName, constructorArgs) ->
422121
- let constrName = printLongident constrName.txt in
422151
+ let constrName = printLongidentLocation constrName cmtTbl in
422122
422152
let argsDoc = match constructorArgs with
422123
422153
| None -> Doc.nil
422124
422154
| Some({ppat_loc; ppat_desc = Ppat_construct ({txt = Longident.Lident "()"}, _)}) ->
@@ -423814,7 +423844,11 @@ and printJsxExpression lident args cmtTbl =
423814
423844
let name = printJsxName lident in
423815
423845
let (formattedProps, children) = printJsxProps args cmtTbl in
423816
423846
(* <div className="test" /> *)
423817
- let isSelfClosing = match children with | [] -> true | _ -> false in
423847
+ let isSelfClosing =
423848
+ match children with
423849
+ | Some ({Parsetree.pexp_desc = Pexp_construct ({txt = Longident.Lident "[]"}, None)}) -> true
423850
+ | _ -> false
423851
+ in
423818
423852
Doc.group (
423819
423853
Doc.concat [
423820
423854
Doc.group (
@@ -423831,7 +423865,10 @@ and printJsxExpression lident args cmtTbl =
423831
423865
Doc.indent (
423832
423866
Doc.concat [
423833
423867
Doc.line;
423834
- printJsxChildren children cmtTbl;
423868
+ (match children with
423869
+ | Some childrenExpression -> printJsxChildren childrenExpression cmtTbl
423870
+ | None -> Doc.nil
423871
+ );
423835
423872
]
423836
423873
);
423837
423874
Doc.line;
@@ -423845,17 +423882,17 @@ and printJsxExpression lident args cmtTbl =
423845
423882
and printJsxFragment expr cmtTbl =
423846
423883
let opening = Doc.text "<>" in
423847
423884
let closing = Doc.text "</>" in
423848
- let (children, _) = ParsetreeViewer.collectListExpressions expr in
423885
+ (* let (children, _) = ParsetreeViewer.collectListExpressions expr in *)
423849
423886
Doc.group (
423850
423887
Doc.concat [
423851
423888
opening;
423852
- begin match children with
423853
- | [] -> Doc.nil
423854
- | children ->
423889
+ begin match expr.pexp_desc with
423890
+ | Pexp_construct ({txt = Longident.Lident "[]"}, None) -> Doc.nil
423891
+ | _ ->
423855
423892
Doc.indent (
423856
423893
Doc.concat [
423857
423894
Doc.line;
423858
- printJsxChildren children cmtTbl;
423895
+ printJsxChildren expr cmtTbl;
423859
423896
]
423860
423897
)
423861
423898
end;
@@ -423864,29 +423901,46 @@ and printJsxFragment expr cmtTbl =
423864
423901
]
423865
423902
)
423866
423903
423867
- and printJsxChildren (children: Parsetree.expression list) cmtTbl =
423868
- Doc.group (
423869
- Doc.join ~sep:Doc.line (
423870
- List.map (fun (expr : Parsetree.expression) ->
423871
- let leadingLineCommentPresent = hasLeadingLineComment cmtTbl expr.pexp_loc in
423872
- let exprDoc = printExpressionWithComments expr cmtTbl in
423873
- match Parens.jsxChildExpr expr with
423874
- | Parenthesized | Braced _ ->
423875
- (* {(20: int)} make sure that we also protect the expression inside *)
423876
- let innerDoc = if Parens.bracedExpr expr then addParens exprDoc else exprDoc in
423877
- if leadingLineCommentPresent then
423878
- addBraces innerDoc
423879
- else
423880
- Doc.concat [Doc.lbrace; innerDoc; Doc.rbrace]
423881
- | Nothing -> exprDoc
423882
- ) children
423904
+ and printJsxChildren (childrenExpr : Parsetree.expression) cmtTbl =
423905
+ match childrenExpr.pexp_desc with
423906
+ | Pexp_construct ({txt = Longident.Lident "::"}, _) ->
423907
+ let (children, _) = ParsetreeViewer.collectListExpressions childrenExpr in
423908
+ Doc.group (
423909
+ Doc.join ~sep:Doc.line (
423910
+ List.map (fun (expr : Parsetree.expression) ->
423911
+ let leadingLineCommentPresent = hasLeadingLineComment cmtTbl expr.pexp_loc in
423912
+ let exprDoc = printExpressionWithComments expr cmtTbl in
423913
+ match Parens.jsxChildExpr expr with
423914
+ | Parenthesized | Braced _ ->
423915
+ (* {(20: int)} make sure that we also protect the expression inside *)
423916
+ let innerDoc = if Parens.bracedExpr expr then addParens exprDoc else exprDoc in
423917
+ if leadingLineCommentPresent then
423918
+ addBraces innerDoc
423919
+ else
423920
+ Doc.concat [Doc.lbrace; innerDoc; Doc.rbrace]
423921
+ | Nothing -> exprDoc
423922
+ ) children
423923
+ )
423883
423924
)
423884
- )
423925
+ | _ ->
423926
+ let leadingLineCommentPresent = hasLeadingLineComment cmtTbl childrenExpr.pexp_loc in
423927
+ let exprDoc = printExpressionWithComments childrenExpr cmtTbl in
423928
+ Doc.concat [
423929
+ Doc.dotdotdot;
423930
+ match Parens.jsxChildExpr childrenExpr with
423931
+ | Parenthesized | Braced _ ->
423932
+ let innerDoc = if Parens.bracedExpr childrenExpr then addParens exprDoc else exprDoc in
423933
+ if leadingLineCommentPresent then
423934
+ addBraces innerDoc
423935
+ else
423936
+ Doc.concat [Doc.lbrace; innerDoc; Doc.rbrace]
423937
+ | Nothing -> exprDoc
423938
+ ]
423885
423939
423886
- and printJsxProps args cmtTbl =
423940
+ and printJsxProps args cmtTbl :(Doc.t * Parsetree.expression option) =
423887
423941
let rec loop props args =
423888
423942
match args with
423889
- | [] -> (Doc.nil, [] )
423943
+ | [] -> (Doc.nil, None )
423890
423944
| [
423891
423945
(Asttypes.Labelled "children", children);
423892
423946
(
@@ -423905,8 +423959,7 @@ and printJsxProps args cmtTbl =
423905
423959
)
423906
423960
]
423907
423961
) in
423908
- let (children, _) = ParsetreeViewer.collectListExpressions children in
423909
- (formattedProps, children)
423962
+ (formattedProps, Some children)
423910
423963
| arg::args ->
423911
423964
let propDoc = printJsxProp arg cmtTbl in
423912
423965
loop (propDoc::props) args
@@ -425863,6 +425916,9 @@ let parseHashIdent ~startPos p =
425863
425916
Parser.next p;
425864
425917
let text = if p.mode = ParseForTypeChecker then parseStringLiteral text else text in
425865
425918
(text, mkLoc startPos p.prevEndPos)
425919
+ | Int {i} ->
425920
+ Parser.next p;
425921
+ (i, mkLoc startPos p.prevEndPos)
425866
425922
| _ ->
425867
425923
parseIdent ~startPos ~msg:ErrorMessages.variantIdent p
425868
425924
@@ -426366,6 +426422,9 @@ let rec parsePattern ?(alias=true) ?(or_=true) p =
426366
426422
Parser.next p;
426367
426423
let text = if p.mode = ParseForTypeChecker then parseStringLiteral text else text in
426368
426424
(text, mkLoc startPos p.prevEndPos)
426425
+ | Int {i} ->
426426
+ Parser.next p;
426427
+ (i, mkLoc startPos p.prevEndPos)
426369
426428
| _ ->
426370
426429
parseIdent ~msg:ErrorMessages.variantIdent ~startPos p
426371
426430
in
@@ -426766,7 +426825,7 @@ and parseTernaryExpr leftOperand p =
426766
426825
| _ ->
426767
426826
leftOperand
426768
426827
426769
- and parseEs6ArrowExpression ?parameters p =
426828
+ and parseEs6ArrowExpression ?context ? parameters p =
426770
426829
let startPos = p.Parser.startPos in
426771
426830
Parser.leaveBreadcrumb p Grammar.Es6ArrowExpr;
426772
426831
let parameters = match parameters with
@@ -426782,7 +426841,7 @@ and parseEs6ArrowExpression ?parameters p =
426782
426841
in
426783
426842
Parser.expect EqualGreater p;
426784
426843
let body =
426785
- let expr = parseExpr p in
426844
+ let expr = parseExpr ?context p in
426786
426845
match returnType with
426787
426846
| Some typ ->
426788
426847
Ast_helper.Exp.constraint_
@@ -427298,7 +427357,7 @@ and parseOperandExpr ~context p =
427298
427357
if (context != WhenExpr) &&
427299
427358
isEs6ArrowExpression ~inTernary:(context=TernaryTrueBranchExpr) p
427300
427359
then
427301
- parseEs6ArrowExpression p
427360
+ parseEs6ArrowExpression ~context p
427302
427361
else
427303
427362
parseUnaryExpr p
427304
427363
in
@@ -429852,7 +429911,7 @@ and parseTypeEquationOrConstrDecl p =
429852
429911
| _ -> (Some typ, Asttypes.Public, Parsetree.Ptype_abstract)
429853
429912
end
429854
429913
| _ ->
429855
- let uidentEndPos = p.endPos in
429914
+ let uidentEndPos = p.prevEndPos in
429856
429915
let (args, res) = parseConstrDeclArgs p in
429857
429916
let first = Some (
429858
429917
let uidentLoc = mkLoc uidentStartPos uidentEndPos in
@@ -432746,6 +432805,28 @@ end = struct
432746
432805
module Doc = Res_doc
432747
432806
module Token = Res_token
432748
432807
432808
+ let rec unsafe_for_all_range s ~start ~finish p =
432809
+ start > finish ||
432810
+ p (String.unsafe_get s start) &&
432811
+ unsafe_for_all_range s ~start:(start + 1) ~finish p
432812
+
432813
+ let for_all_from s start p =
432814
+ let len = String.length s in
432815
+ unsafe_for_all_range s ~start ~finish:(len - 1) p
432816
+
432817
+ (* See https://github.com/rescript-lang/rescript-compiler/blob/726cfa534314b586e5b5734471bc2023ad99ebd9/jscomp/ext/ext_string.ml#L510 *)
432818
+ let isValidNumericPolyvarNumber (x : string) =
432819
+ let len = String.length x in
432820
+ len > 0 && (
432821
+ let a = Char.code (String.unsafe_get x 0) in
432822
+ a <= 57 &&
432823
+ (if len > 1 then
432824
+ a > 48 &&
432825
+ for_all_from x 1 (function '0' .. '9' -> true | _ -> false)
432826
+ else
432827
+ a >= 48 )
432828
+ )
432829
+
432749
432830
(* checks if ident contains "arity", like in "arity1", "arity2", "arity3" etc. *)
432750
432831
let isArityIdent ident =
432751
432832
if String.length ident >= 6 then
@@ -432793,13 +432874,17 @@ let printIdentLike ~allowUident txt =
432793
432874
| NormalIdent -> Doc.text txt
432794
432875
432795
432876
let printPolyVarIdent txt =
432796
- match classifyIdentContent ~allowUident:true txt with
432797
- | ExoticIdent -> Doc.concat [
432798
- Doc.text "\"";
432799
- Doc.text txt;
432800
- Doc.text"\""
432801
- ]
432802
- | NormalIdent -> Doc.text txt
432877
+ (* numeric poly-vars don't need quotes: #644 *)
432878
+ if isValidNumericPolyvarNumber txt then
432879
+ Doc.text txt
432880
+ else
432881
+ match classifyIdentContent ~allowUident:true txt with
432882
+ | ExoticIdent -> Doc.concat [
432883
+ Doc.text "\"";
432884
+ Doc.text txt;
432885
+ Doc.text"\""
432886
+ ]
432887
+ | NormalIdent -> Doc.text txt
432803
432888
432804
432889
(* ReScript doesn't have parenthesized identifiers.
432805
432890
* We don't support custom operators. *)
0 commit comments