Skip to content

Commit c582cf4

Browse files
committed
fix jsx printers
1 parent f334190 commit c582cf4

26 files changed

+206
-201
lines changed

jscomp/ext/ext_ident.ml

+4
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,17 @@ let [@inline] no_escape (c : char) =
133133
| _ -> false
134134

135135
let is_exotic name =
136+
(* Exotic idents should always wrapped by \"..." *)
136137
match String.unsafe_get name 0 with
137138
| '\\' -> true
138139
| _ -> false
139140

141+
let wrap_exotic name = "\\\"" ^ name ^ "\""
142+
140143
let unwrap_exotic name =
141144
if is_exotic name then
142145
let len = String.length name in
146+
(* Exotic idents should always wrapped by \"..." *)
143147
String.sub name 2 (len - 3)
144148
else name
145149

jscomp/ext/ext_ident.mli

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ val make_unused : unit -> Ident.t
5050

5151
val is_exotic : string -> bool
5252

53+
val wrap_exotic : string -> string
54+
5355
val unwrap_exotic : string -> string
5456

5557
(**

jscomp/syntax/src/jsx_v4.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ let mapBinding ~config ~emptyLoc ~pstr_loc ~fileName ~recFlag binding =
914914
}
915915
in
916916
let fnName = getFnName binding.pvb_pat in
917-
let internalFnName = fnName ^ "$Internal" in
917+
let internalFnName = Ext_ident.wrap_exotic (fnName ^ "$Internal") in
918918
let fullModuleName = makeModuleName fileName config.nestedModules fnName in
919919
let bindingWrapper, hasForwardRef, expression =
920920
modifiedBinding ~bindingLoc ~bindingPatLoc ~fnName binding
@@ -986,6 +986,7 @@ let mapBinding ~config ~emptyLoc ~pstr_loc ~fileName ~recFlag binding =
986986
match fullModuleName with
987987
| "" -> fullExpression
988988
| txt ->
989+
let txt = Ext_ident.wrap_exotic txt in
989990
Exp.let_ Nonrecursive
990991
[
991992
Vb.mk ~loc:emptyLoc

jscomp/syntax/src/reactjs_jsx_v3.ml

+4-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ let getPropsNameValue _acc (loc, exp) =
151151

152152
(* Lookup the `props` record or string as part of [@react.component] and store the name for use when rewriting *)
153153
let getPropsAttr payload =
154-
let defaultProps = {propsName = "Props"} in
154+
let defaultProps = {propsName = Ext_ident.wrap_exotic "Props"} in
155155
match payload with
156156
| Some
157157
(PStr
@@ -317,7 +317,8 @@ let makeExternalDecl fnName loc namedArgListWithKeyAndRef namedTypeList =
317317
(makePropsType ~loc namedTypeList)
318318

319319
let newtypeToVar newtype type_ =
320-
let var_desc = Ptyp_var ("type-" ^ newtype) in
320+
let newtype_label = Ext_ident.wrap_exotic ("type-" ^ newtype) in
321+
let var_desc = Ptyp_var newtype_label in
321322
let typ (mapper : Ast_mapper.mapper) typ =
322323
match typ.ptyp_desc with
323324
| Ptyp_constr ({txt = Lident name}, _) when name = newtype ->
@@ -970,6 +971,7 @@ let jsxMapper ~config =
970971
match fullModuleName with
971972
| "" -> fullExpression
972973
| txt ->
974+
let txt = Ext_ident.wrap_exotic txt in
973975
Exp.let_ Nonrecursive
974976
[
975977
Vb.mk ~loc:emptyLoc

jscomp/syntax/src/res_printer.ml

+8-16
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,9 @@ let printPolyVarIdent txt =
428428
if isValidNumericPolyvarNumber txt then Doc.text txt
429429
else
430430
match classifyIdentContent ~allowUident:true txt with
431-
| ExoticIdent -> Doc.concat [Doc.text "\""; Doc.text (Ext_ident.unwrap_exotic txt); Doc.text "\""]
431+
| ExoticIdent ->
432+
Doc.concat
433+
[Doc.text "\""; Doc.text (Ext_ident.unwrap_exotic txt); Doc.text "\""]
432434
| NormalIdent -> (
433435
match txt with
434436
| "" -> Doc.concat [Doc.text "\""; Doc.text txt; Doc.text "\""]
@@ -455,9 +457,7 @@ let printLident l =
455457
| Some txts ->
456458
Doc.concat
457459
[
458-
Doc.join ~sep:Doc.dot (List.map Doc.text txts);
459-
Doc.dot;
460-
Doc.text txt;
460+
Doc.join ~sep:Doc.dot (List.map Doc.text txts); Doc.dot; Doc.text txt;
461461
]
462462
| None -> Doc.text "printLident: Longident.Lapply is not supported"
463463
in
@@ -1597,8 +1597,7 @@ and printTypExpr ~(state : State.t) (typExpr : Parsetree.core_type) cmtTbl =
15971597
let renderedType =
15981598
match typExpr.ptyp_desc with
15991599
| Ptyp_any -> Doc.text "_"
1600-
| Ptyp_var var ->
1601-
Doc.concat [Doc.text "'"; Doc.text var]
1600+
| Ptyp_var var -> Doc.concat [Doc.text "'"; Doc.text var]
16021601
| Ptyp_extension extension ->
16031602
printExtension ~state ~atModuleLvl:false extension cmtTbl
16041603
| Ptyp_alias (typ, alias) ->
@@ -1873,10 +1872,8 @@ and printTypeParameter ~state (attrs, lbl, typ) cmtTbl =
18731872
let label =
18741873
match lbl with
18751874
| Asttypes.Nolabel -> Doc.nil
1876-
| Labelled lbl ->
1877-
Doc.concat [Doc.text "~"; Doc.text lbl; Doc.text ": "]
1878-
| Optional lbl ->
1879-
Doc.concat [Doc.text "~"; Doc.text lbl; Doc.text ": "]
1875+
| Labelled lbl -> Doc.concat [Doc.text "~"; Doc.text lbl; Doc.text ": "]
1876+
| Optional lbl -> Doc.concat [Doc.text "~"; Doc.text lbl; Doc.text ": "]
18801877
in
18811878
let optionalIndicator =
18821879
match lbl with
@@ -4719,12 +4716,7 @@ and printArgument ~state (argLbl, arg) cmtTbl =
47194716
in
47204717
let doc =
47214718
Doc.concat
4722-
[
4723-
Doc.tilde;
4724-
Doc.text lbl;
4725-
Doc.text ": ";
4726-
printTypExpr ~state typ cmtTbl;
4727-
]
4719+
[Doc.tilde; Doc.text lbl; Doc.text ": "; printTypExpr ~state typ cmtTbl]
47284720
in
47294721
printComments doc cmtTbl loc
47304722
(* ~a? (optional lbl punned)*)

jscomp/syntax/tests/ppx/react/expected/aliasProps.res.txt

+14-14
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ module C0 = {
1212
React.string(text)
1313
}
1414
let make = {
15-
let AliasProps$C0 = (props: props<_>) => make(props)
15+
let \"AliasProps$C0" = (props: props<_>) => make(props)
1616

17-
AliasProps$C0
17+
\"AliasProps$C0"
1818
}
1919
}
2020

@@ -30,9 +30,9 @@ module C1 = {
3030
React.string(p ++ text)
3131
}
3232
let make = {
33-
let AliasProps$C1 = (props: props<_>) => make(props)
33+
let \"AliasProps$C1" = (props: props<_>) => make(props)
3434

35-
AliasProps$C1
35+
\"AliasProps$C1"
3636
}
3737
}
3838

@@ -48,9 +48,9 @@ module C2 = {
4848
React.string(bar)
4949
}
5050
let make = {
51-
let AliasProps$C2 = (props: props<_>) => make(props)
51+
let \"AliasProps$C2" = (props: props<_>) => make(props)
5252

53-
AliasProps$C2
53+
\"AliasProps$C2"
5454
}
5555
}
5656

@@ -72,9 +72,9 @@ module C3 = {
7272
}
7373
}
7474
let make = {
75-
let AliasProps$C3 = (props: props<_>) => make(props)
75+
let \"AliasProps$C3" = (props: props<_>) => make(props)
7676

77-
AliasProps$C3
77+
\"AliasProps$C3"
7878
}
7979
}
8080

@@ -90,9 +90,9 @@ module C4 = {
9090
ReactDOM.jsx("div", {children: ?ReactDOM.someElement(b)})
9191
}
9292
let make = {
93-
let AliasProps$C4 = (props: props<_>) => make(props)
93+
let \"AliasProps$C4" = (props: props<_>) => make(props)
9494

95-
AliasProps$C4
95+
\"AliasProps$C4"
9696
}
9797
}
9898

@@ -108,9 +108,9 @@ module C5 = {
108108
x + y + z
109109
}
110110
let make = {
111-
let AliasProps$C5 = (props: props<_>) => make(props)
111+
let \"AliasProps$C5" = (props: props<_>) => make(props)
112112

113-
AliasProps$C5
113+
\"AliasProps$C5"
114114
}
115115
}
116116

@@ -124,8 +124,8 @@ module C6 = {
124124

125125
let make = ({comp: module(Comp: Comp), x: (a, b), _}: props<_, _>) => React.jsx(Comp.make, {})
126126
let make = {
127-
let AliasProps$C6 = (props: props<_>) => make(props)
127+
let \"AliasProps$C6" = (props: props<_>) => make(props)
128128

129-
AliasProps$C6
129+
\"AliasProps$C6"
130130
}
131131
}

jscomp/syntax/tests/ppx/react/expected/asyncAwait.res.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ module C0 = {
88
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.int(a)})})
99
}
1010
let make = {
11-
let AsyncAwait$C0 = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))
11+
let \"AsyncAwait$C0" = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))
1212

13-
AsyncAwait$C0
13+
\"AsyncAwait$C0"
1414
}
1515
}
1616

@@ -24,8 +24,8 @@ module C1 = {
2424
}
2525
}
2626
let make = {
27-
let AsyncAwait$C1 = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))
27+
let \"AsyncAwait$C1" = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))
2828

29-
AsyncAwait$C1
29+
\"AsyncAwait$C1"
3030
}
3131
}

jscomp/syntax/tests/ppx/react/expected/commentAtTop.res.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let make = ({msg, _}: props<_>) => {
44
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({msg->React.string})})
55
}
66
let make = {
7-
let CommentAtTop = (props: props<_>) => make(props)
7+
let \"CommentAtTop" = (props: props<_>) => make(props)
88

9-
CommentAtTop
9+
\"CommentAtTop"
1010
}

jscomp/syntax/tests/ppx/react/expected/defaultValueProp.res.txt

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ module C0 = {
1313
React.int(a + b)
1414
}
1515
let make = {
16-
let DefaultValueProp$C0 = (props: props<_>) => make(props)
17-
DefaultValueProp$C0
16+
let \"DefaultValueProp$C0" = (props: props<_>) => make(props)
17+
\"DefaultValueProp$C0"
1818
}
1919
}
2020

@@ -30,9 +30,9 @@ module C1 = {
3030
React.int(a + b)
3131
}
3232
let make = {
33-
let DefaultValueProp$C1 = (props: props<_>) => make(props)
33+
let \"DefaultValueProp$C1" = (props: props<_>) => make(props)
3434

35-
DefaultValueProp$C1
35+
\"DefaultValueProp$C1"
3636
}
3737
}
3838

@@ -49,9 +49,9 @@ module C2 = {
4949
React.string(a)
5050
}
5151
let make = {
52-
let DefaultValueProp$C2 = (props: props<_>) => make(props)
52+
let \"DefaultValueProp$C2" = (props: props<_>) => make(props)
5353

54-
DefaultValueProp$C2
54+
\"DefaultValueProp$C2"
5555
}
5656
}
5757

@@ -69,8 +69,8 @@ module C3 = {
6969
}
7070
}
7171
let make = {
72-
let DefaultValueProp$C3 = (props: props<_>) => make(props)
72+
let \"DefaultValueProp$C3" = (props: props<_>) => make(props)
7373

74-
DefaultValueProp$C3
74+
\"DefaultValueProp$C3"
7575
}
7676
}

jscomp/syntax/tests/ppx/react/expected/fileLevelConfig.res.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ module V3 = {
1010
ReactDOMRe.createDOMElementVariadic("div", [{msg->React.string}])
1111
}
1212
let make = {
13-
let FileLevelConfig$V3 = (Props: {"msg": 'msg}) => make(~msg=Props["msg"])
14-
FileLevelConfig$V3
13+
let \"FileLevelConfig$V3" = (\"Props": {"msg": 'msg}) => make(~msg=\"Props"["msg"])
14+
\"FileLevelConfig$V3"
1515
}
1616
}
1717

@@ -24,9 +24,9 @@ module V4C = {
2424
ReactDOM.createDOMElementVariadic("div", [{msg->React.string}])
2525
}
2626
let make = {
27-
let FileLevelConfig$V4C = (props: props<_>) => make(props)
27+
let \"FileLevelConfig$V4C" = (props: props<_>) => make(props)
2828

29-
FileLevelConfig$V4C
29+
\"FileLevelConfig$V4C"
3030
}
3131
}
3232

@@ -39,8 +39,8 @@ module V4A = {
3939
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({msg->React.string})})
4040
}
4141
let make = {
42-
let FileLevelConfig$V4A = (props: props<_>) => make(props)
42+
let \"FileLevelConfig$V4A" = (props: props<_>) => make(props)
4343

44-
FileLevelConfig$V4A
44+
\"FileLevelConfig$V4A"
4545
}
4646
}

jscomp/syntax/tests/ppx/react/expected/firstClassModules.res.txt

+21-21
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ module Select = {
77
}
88
@obj
99
external makeProps: (
10-
~model: module(T with type t = 'type-a and type key = 'type-key),
11-
~selected: option<'type-key>,
12-
~onChange: option<'type-key> => unit,
13-
~items: array<'type-a>,
10+
~model: module(T with type t = '\"type-a" and type key = '\"type-key"),
11+
~selected: option<'\"type-key">,
12+
~onChange: option<'\"type-key"> => unit,
13+
~items: array<'\"type-a">,
1414
~key: string=?,
1515
unit,
1616
) => {
17-
"model": module(T with type t = 'type-a and type key = 'type-key),
18-
"selected": option<'type-key>,
19-
"onChange": option<'type-key> => unit,
20-
"items": array<'type-a>,
17+
"model": module(T with type t = '\"type-a" and type key = '\"type-key"),
18+
"selected": option<'\"type-key">,
19+
"onChange": option<'\"type-key"> => unit,
20+
"items": array<'\"type-a">,
2121
} = ""
2222

2323
@react.component
@@ -32,21 +32,21 @@ module Select = {
3232
ReactDOMRe.createDOMElementVariadic("div", [])
3333
}
3434
let make = {
35-
let FirstClassModules$Select = (
36-
Props: {
37-
"model": module(T with type t = 'type-a and type key = 'type-key),
38-
"selected": option<'type-key>,
39-
"onChange": option<'type-key> => unit,
40-
"items": array<'type-a>,
35+
let \"FirstClassModules$Select" = (
36+
\"Props": {
37+
"model": module(T with type t = '\"type-a" and type key = '\"type-key"),
38+
"selected": option<'\"type-key">,
39+
"onChange": option<'\"type-key"> => unit,
40+
"items": array<'\"type-a">,
4141
},
4242
) =>
4343
make(
44-
~items=Props["items"],
45-
~onChange=Props["onChange"],
46-
~selected=Props["selected"],
47-
~model=Props["model"],
44+
~items=\"Props"["items"],
45+
~onChange=\"Props"["onChange"],
46+
~selected=\"Props"["selected"],
47+
~model=\"Props"["model"],
4848
)
49-
FirstClassModules$Select
49+
\"FirstClassModules$Select"
5050
}
5151
}
5252

@@ -77,9 +77,9 @@ module Select = {
7777
ReactDOM.createDOMElementVariadic("div", [])
7878
}
7979
let make = {
80-
let FirstClassModules$Select = (props: props<_>) => make(props)
80+
let \"FirstClassModules$Select" = (props: props<_>) => make(props)
8181

82-
FirstClassModules$Select
82+
\"FirstClassModules$Select"
8383
}
8484
}
8585

0 commit comments

Comments
 (0)