Skip to content

Commit bfd1fd4

Browse files
committed
Bring syntax to master.
1 parent 75f3fa0 commit bfd1fd4

File tree

5 files changed

+230
-286
lines changed

5 files changed

+230
-286
lines changed

jscomp/main/rescript_compiler_main.ml

-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ let format_file input =
183183
match ext with
184184
| Ml | Mli -> `ml
185185
| Res | Resi -> `res
186-
| Re | Rei -> `refmt (Filename.concat (Filename.dirname Sys.executable_name) "refmt.exe")
187186
| _ -> Bsc_args.bad_arg ("don't know what to do with " ^ input) in
188187
let formatted = Res_multi_printer.print syntax ~input in
189188
match !Clflags.output_name with

lib/4.06.1/unstable/js_compiler.ml

+50-13
Original file line numberDiff line numberDiff line change
@@ -261670,9 +261670,10 @@ let otherAttrsPure (loc, _) = loc.txt <> "react.component"
261670261670
let hasAttrOnBinding { pvb_attributes } = find_opt hasAttr pvb_attributes <> None
261671261671

261672261672
(* Finds the name of the variable the binding is assigned to, otherwise raises Invalid_argument *)
261673-
let getFnName binding =
261673+
let rec getFnName binding =
261674261674
match binding with
261675-
| { pvb_pat = { ppat_desc = Ppat_var { txt } } } -> txt
261675+
| { ppat_desc = Ppat_var { txt } } -> txt
261676+
| { ppat_desc = Ppat_constraint (pat, _) } -> getFnName pat
261676261677
| _ -> raise (Invalid_argument "react.component calls cannot be destructured.")
261677261678
[@@raises Invalid_argument]
261678261679

@@ -261809,6 +261810,17 @@ let makeExternalDecl fnName loc namedArgListWithKeyAndRef namedTypeList =
261809261810
(makePropsType ~loc namedTypeList)
261810261811
[@@raises Invalid_argument]
261811261812

261813+
let newtypeToVar newtype type_ =
261814+
let var_desc = Ptyp_var ("type-" ^ newtype) in
261815+
let typ (mapper : Ast_mapper.mapper) typ =
261816+
match typ.ptyp_desc with
261817+
| Ptyp_constr ({txt = Lident name}, _) when name = newtype ->
261818+
{typ with ptyp_desc = var_desc}
261819+
| _ -> Ast_mapper.default_mapper.typ mapper typ
261820+
in
261821+
let mapper = {Ast_mapper.default_mapper with typ} in
261822+
mapper.typ mapper type_
261823+
261812261824
(* TODO: some line number might still be wrong *)
261813261825
let jsxMapper () =
261814261826
let jsxVersion = ref None in
@@ -261911,7 +261923,7 @@ let jsxMapper () =
261911261923
[@@raises Invalid_argument]
261912261924
in
261913261925

261914-
let rec recursivelyTransformNamedArgsForMake mapper expr list =
261926+
let rec recursivelyTransformNamedArgsForMake mapper expr args newtypes =
261915261927
let expr = mapper.expr mapper expr in
261916261928
match expr.pexp_desc with
261917261929
(* TODO: make this show up with a loc. *)
@@ -261952,19 +261964,23 @@ let jsxMapper () =
261952261964
let type_ = match pattern with { ppat_desc = Ppat_constraint (_, type_) } -> Some type_ | _ -> None in
261953261965

261954261966
recursivelyTransformNamedArgsForMake mapper expression
261955-
((arg, default, pattern, alias, pattern.ppat_loc, type_) :: list)
261967+
((arg, default, pattern, alias, pattern.ppat_loc, type_) :: args) newtypes
261956261968
| Pexp_fun (Nolabel, _, { ppat_desc = Ppat_construct ({ txt = Lident "()" }, _) | Ppat_any }, _expression) ->
261957-
(list, None)
261969+
(args, newtypes, None)
261958261970
| Pexp_fun
261959261971
( Nolabel,
261960261972
_,
261961261973
{ ppat_desc = Ppat_var { txt } | Ppat_constraint ({ ppat_desc = Ppat_var { txt } }, _) },
261962261974
_expression ) ->
261963-
(list, Some txt)
261975+
(args, newtypes, Some txt)
261964261976
| Pexp_fun (Nolabel, _, pattern, _expression) ->
261965261977
Location.raise_errorf ~loc:pattern.ppat_loc
261966261978
"React: react.component refs only support plain arguments and type annotations."
261967-
| _ -> (list, None)
261979+
| Pexp_newtype (label, expression) ->
261980+
recursivelyTransformNamedArgsForMake mapper expression args (label :: newtypes)
261981+
| Pexp_constraint (expression, _typ) ->
261982+
recursivelyTransformNamedArgsForMake mapper expression args newtypes
261983+
| _ -> (args, newtypes, None)
261968261984
[@@raises Invalid_argument]
261969261985
in
261970261986

@@ -262064,7 +262080,7 @@ let jsxMapper () =
262064262080
let bindingLoc = binding.pvb_loc in
262065262081
let bindingPatLoc = binding.pvb_pat.ppat_loc in
262066262082
let binding = { binding with pvb_pat = { binding.pvb_pat with ppat_loc = emptyLoc }; pvb_loc = emptyLoc } in
262067-
let fnName = getFnName binding in
262083+
let fnName = getFnName binding.pvb_pat in
262068262084
let internalFnName = fnName ^ "$Internal" in
262069262085
let fullModuleName = makeModuleName fileName !nestedModules fnName in
262070262086
let modifiedBindingOld binding =
@@ -262073,7 +262089,8 @@ let jsxMapper () =
262073262089
let rec spelunkForFunExpression expression =
262074262090
match expression with
262075262091
(* let make = (~prop) => ... *)
262076-
| { pexp_desc = Pexp_fun _ } -> expression
262092+
| { pexp_desc = Pexp_fun _ }
262093+
| { pexp_desc = Pexp_newtype _ } -> expression
262077262094
(* let make = {let foo = bar in (~prop) => ...} *)
262078262095
| { pexp_desc = Pexp_let (_recursive, _vbs, returnExpression) } ->
262079262096
(* here's where we spelunk! *)
@@ -262083,6 +262100,8 @@ let jsxMapper () =
262083262100
spelunkForFunExpression innerFunctionExpression
262084262101
| { pexp_desc = Pexp_sequence (_wrapperExpression, innerFunctionExpression) } ->
262085262102
spelunkForFunExpression innerFunctionExpression
262103+
| { pexp_desc = Pexp_constraint (innerFunctionExpression, _typ) } ->
262104+
spelunkForFunExpression innerFunctionExpression
262086262105
| _ ->
262087262106
raise
262088262107
(Invalid_argument
@@ -262171,8 +262190,8 @@ let jsxMapper () =
262171262190
in
262172262191
let props = getPropsAttr payload in
262173262192
(* do stuff here! *)
262174-
let namedArgList, forwardRef =
262175-
recursivelyTransformNamedArgsForMake mapper (modifiedBindingOld binding) []
262193+
let namedArgList, newtypes, forwardRef =
262194+
recursivelyTransformNamedArgsForMake mapper (modifiedBindingOld binding) [] []
262176262195
in
262177262196
let namedArgListWithKeyAndRef =
262178262197
(optional "key", None, Pat.var { txt = "key"; loc = emptyLoc }, "key", emptyLoc, Some (keyType emptyLoc))
@@ -262207,7 +262226,25 @@ let jsxMapper () =
262207262226
in
262208262227
let namedTypeList = List.fold_left argToType [] namedArgList in
262209262228
let loc = emptyLoc in
262210-
let externalDecl = makeExternalDecl fnName loc namedArgListWithKeyAndRef namedTypeList in
262229+
let externalArgs = (* translate newtypes to type variables *)
262230+
List.fold_left
262231+
(fun args newtype ->
262232+
List.map (fun (a, b, c, d, e, maybeTyp) ->
262233+
match maybeTyp with
262234+
| Some typ -> (a, b, c, d, e, Some (newtypeToVar newtype.txt typ))
262235+
| None -> (a, b, c, d, e, None))
262236+
args)
262237+
namedArgListWithKeyAndRef
262238+
newtypes
262239+
in
262240+
let externalTypes = (* translate newtypes to type variables *)
262241+
List.fold_left
262242+
(fun args newtype ->
262243+
List.map (fun (a, b, typ) -> (a, b, newtypeToVar newtype.txt typ)) args)
262244+
namedTypeList
262245+
newtypes
262246+
in
262247+
let externalDecl = makeExternalDecl fnName loc externalArgs externalTypes in
262211262248
let innerExpressionArgs =
262212262249
List.map pluckArg namedArgListWithKeyAndRefForNew
262213262250
@ if hasUnit then [ (Nolabel, Exp.construct { loc; txt = Lident "()" } None) ] else []
@@ -262237,7 +262274,7 @@ let jsxMapper () =
262237262274
{
262238262275
ppat_desc =
262239262276
Ppat_constraint
262240-
(makePropsName ~loc:emptyLoc props.propsName, makePropsType ~loc:emptyLoc namedTypeList);
262277+
(makePropsName ~loc:emptyLoc props.propsName, makePropsType ~loc:emptyLoc externalTypes);
262241262278
ppat_loc = emptyLoc;
262242262279
ppat_attributes = [];
262243262280
}

0 commit comments

Comments
 (0)