Skip to content

Commit

Permalink
fix jsx printers
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim committed Mar 26, 2024
1 parent f334190 commit 406746c
Show file tree
Hide file tree
Showing 26 changed files with 188 additions and 185 deletions.
4 changes: 4 additions & 0 deletions jscomp/ext/ext_ident.ml
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,17 @@ let [@inline] no_escape (c : char) =
| _ -> false

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

let wrap_exotic name = "\\\"" ^ name ^ "\""

let unwrap_exotic name =
if is_exotic name then
let len = String.length name in
(* Exotic idents should always wrapped by \"..." *)
String.sub name 2 (len - 3)
else name

Expand Down
2 changes: 2 additions & 0 deletions jscomp/ext/ext_ident.mli
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ val make_unused : unit -> Ident.t

val is_exotic : string -> bool

val wrap_exotic : string -> string

val unwrap_exotic : string -> string

(**
Expand Down
3 changes: 2 additions & 1 deletion jscomp/syntax/src/jsx_v4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ let mapBinding ~config ~emptyLoc ~pstr_loc ~fileName ~recFlag binding =
}
in
let fnName = getFnName binding.pvb_pat in
let internalFnName = fnName ^ "$Internal" in
let internalFnName = Ext_ident.wrap_exotic (fnName ^ "$Internal") in
let fullModuleName = makeModuleName fileName config.nestedModules fnName in
let bindingWrapper, hasForwardRef, expression =
modifiedBinding ~bindingLoc ~bindingPatLoc ~fnName binding
Expand Down Expand Up @@ -986,6 +986,7 @@ let mapBinding ~config ~emptyLoc ~pstr_loc ~fileName ~recFlag binding =
match fullModuleName with
| "" -> fullExpression
| txt ->
let txt = Ext_ident.wrap_exotic txt in
Exp.let_ Nonrecursive
[
Vb.mk ~loc:emptyLoc
Expand Down
3 changes: 2 additions & 1 deletion jscomp/syntax/src/reactjs_jsx_v3.ml
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ let getPropsNameValue _acc (loc, exp) =

(* Lookup the `props` record or string as part of [@react.component] and store the name for use when rewriting *)
let getPropsAttr payload =
let defaultProps = {propsName = "Props"} in
let defaultProps = {propsName = Ext_ident.wrap_exotic "Props"} in
match payload with
| Some
(PStr
Expand Down Expand Up @@ -970,6 +970,7 @@ let jsxMapper ~config =
match fullModuleName with
| "" -> fullExpression
| txt ->
let txt = Ext_ident.wrap_exotic txt in
Exp.let_ Nonrecursive
[
Vb.mk ~loc:emptyLoc
Expand Down
24 changes: 8 additions & 16 deletions jscomp/syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,9 @@ let printPolyVarIdent txt =
if isValidNumericPolyvarNumber txt then Doc.text txt
else
match classifyIdentContent ~allowUident:true txt with
| ExoticIdent -> Doc.concat [Doc.text "\""; Doc.text (Ext_ident.unwrap_exotic txt); Doc.text "\""]
| ExoticIdent ->
Doc.concat
[Doc.text "\""; Doc.text (Ext_ident.unwrap_exotic txt); Doc.text "\""]
| NormalIdent -> (
match txt with
| "" -> Doc.concat [Doc.text "\""; Doc.text txt; Doc.text "\""]
Expand All @@ -455,9 +457,7 @@ let printLident l =
| Some txts ->
Doc.concat
[
Doc.join ~sep:Doc.dot (List.map Doc.text txts);
Doc.dot;
Doc.text txt;
Doc.join ~sep:Doc.dot (List.map Doc.text txts); Doc.dot; Doc.text txt;
]
| None -> Doc.text "printLident: Longident.Lapply is not supported"
in
Expand Down Expand Up @@ -1597,8 +1597,7 @@ and printTypExpr ~(state : State.t) (typExpr : Parsetree.core_type) cmtTbl =
let renderedType =
match typExpr.ptyp_desc with
| Ptyp_any -> Doc.text "_"
| Ptyp_var var ->
Doc.concat [Doc.text "'"; Doc.text var]
| Ptyp_var var -> Doc.concat [Doc.text "'"; Doc.text var]
| Ptyp_extension extension ->
printExtension ~state ~atModuleLvl:false extension cmtTbl
| Ptyp_alias (typ, alias) ->
Expand Down Expand Up @@ -1873,10 +1872,8 @@ and printTypeParameter ~state (attrs, lbl, typ) cmtTbl =
let label =
match lbl with
| Asttypes.Nolabel -> Doc.nil
| Labelled lbl ->
Doc.concat [Doc.text "~"; Doc.text lbl; Doc.text ": "]
| Optional lbl ->
Doc.concat [Doc.text "~"; Doc.text lbl; Doc.text ": "]
| Labelled lbl -> Doc.concat [Doc.text "~"; Doc.text lbl; Doc.text ": "]
| Optional lbl -> Doc.concat [Doc.text "~"; Doc.text lbl; Doc.text ": "]
in
let optionalIndicator =
match lbl with
Expand Down Expand Up @@ -4719,12 +4716,7 @@ and printArgument ~state (argLbl, arg) cmtTbl =
in
let doc =
Doc.concat
[
Doc.tilde;
Doc.text lbl;
Doc.text ": ";
printTypExpr ~state typ cmtTbl;
]
[Doc.tilde; Doc.text lbl; Doc.text ": "; printTypExpr ~state typ cmtTbl]
in
printComments doc cmtTbl loc
(* ~a? (optional lbl punned)*)
Expand Down
28 changes: 14 additions & 14 deletions jscomp/syntax/tests/ppx/react/expected/aliasProps.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ module C0 = {
React.string(text)
}
let make = {
let AliasProps$C0 = (props: props<_>) => make(props)
let \"AliasProps$C0" = (props: props<_>) => make(props)

AliasProps$C0
\"AliasProps$C0"
}
}

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

AliasProps$C1
\"AliasProps$C1"
}
}

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

AliasProps$C2
\"AliasProps$C2"
}
}

Expand All @@ -72,9 +72,9 @@ module C3 = {
}
}
let make = {
let AliasProps$C3 = (props: props<_>) => make(props)
let \"AliasProps$C3" = (props: props<_>) => make(props)

AliasProps$C3
\"AliasProps$C3"
}
}

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

AliasProps$C4
\"AliasProps$C4"
}
}

Expand All @@ -108,9 +108,9 @@ module C5 = {
x + y + z
}
let make = {
let AliasProps$C5 = (props: props<_>) => make(props)
let \"AliasProps$C5" = (props: props<_>) => make(props)

AliasProps$C5
\"AliasProps$C5"
}
}

Expand All @@ -124,8 +124,8 @@ module C6 = {

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

AliasProps$C6
\"AliasProps$C6"
}
}
8 changes: 4 additions & 4 deletions jscomp/syntax/tests/ppx/react/expected/asyncAwait.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module C0 = {
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.int(a)})})
}
let make = {
let AsyncAwait$C0 = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))
let \"AsyncAwait$C0" = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))

AsyncAwait$C0
\"AsyncAwait$C0"
}
}

Expand All @@ -24,8 +24,8 @@ module C1 = {
}
}
let make = {
let AsyncAwait$C1 = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))
let \"AsyncAwait$C1" = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))

AsyncAwait$C1
\"AsyncAwait$C1"
}
}
4 changes: 2 additions & 2 deletions jscomp/syntax/tests/ppx/react/expected/commentAtTop.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let make = ({msg, _}: props<_>) => {
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({msg->React.string})})
}
let make = {
let CommentAtTop = (props: props<_>) => make(props)
let \"CommentAtTop" = (props: props<_>) => make(props)

CommentAtTop
\"CommentAtTop"
}
16 changes: 8 additions & 8 deletions jscomp/syntax/tests/ppx/react/expected/defaultValueProp.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module C0 = {
React.int(a + b)
}
let make = {
let DefaultValueProp$C0 = (props: props<_>) => make(props)
DefaultValueProp$C0
let \"DefaultValueProp$C0" = (props: props<_>) => make(props)
\"DefaultValueProp$C0"
}
}

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

DefaultValueProp$C1
\"DefaultValueProp$C1"
}
}

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

DefaultValueProp$C2
\"DefaultValueProp$C2"
}
}

Expand All @@ -69,8 +69,8 @@ module C3 = {
}
}
let make = {
let DefaultValueProp$C3 = (props: props<_>) => make(props)
let \"DefaultValueProp$C3" = (props: props<_>) => make(props)

DefaultValueProp$C3
\"DefaultValueProp$C3"
}
}
12 changes: 6 additions & 6 deletions jscomp/syntax/tests/ppx/react/expected/fileLevelConfig.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module V3 = {
ReactDOMRe.createDOMElementVariadic("div", [{msg->React.string}])
}
let make = {
let FileLevelConfig$V3 = (Props: {"msg": 'msg}) => make(~msg=Props["msg"])
FileLevelConfig$V3
let \"FileLevelConfig$V3" = (\"Props": {"msg": 'msg}) => make(~msg=\"Props"["msg"])
\"FileLevelConfig$V3"
}
}

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

FileLevelConfig$V4C
\"FileLevelConfig$V4C"
}
}

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

FileLevelConfig$V4A
\"FileLevelConfig$V4A"
}
}
18 changes: 9 additions & 9 deletions jscomp/syntax/tests/ppx/react/expected/firstClassModules.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ module Select = {
ReactDOMRe.createDOMElementVariadic("div", [])
}
let make = {
let FirstClassModules$Select = (
Props: {
let \"FirstClassModules$Select" = (
\"Props": {
"model": module(T with type t = 'type-a and type key = 'type-key),
"selected": option<'type-key>,
"onChange": option<'type-key> => unit,
"items": array<'type-a>,
},
) =>
make(
~items=Props["items"],
~onChange=Props["onChange"],
~selected=Props["selected"],
~model=Props["model"],
~items=\"Props"["items"],
~onChange=\"Props"["onChange"],
~selected=\"Props"["selected"],
~model=\"Props"["model"],
)
FirstClassModules$Select
\"FirstClassModules$Select"
}
}

Expand Down Expand Up @@ -77,9 +77,9 @@ module Select = {
ReactDOM.createDOMElementVariadic("div", [])
}
let make = {
let FirstClassModules$Select = (props: props<_>) => make(props)
let \"FirstClassModules$Select" = (props: props<_>) => make(props)

FirstClassModules$Select
\"FirstClassModules$Select"
}
}

Expand Down
Loading

0 comments on commit 406746c

Please sign in to comment.