diff --git a/ppx/ppx.ml b/ppx/ppx.ml index 127a79e9..c2e29a9b 100644 --- a/ppx/ppx.ml +++ b/ppx/ppx.ml @@ -88,10 +88,9 @@ let extractChildren ?(removeLastPositionUnit = false) ~loc propsAndChildren = [] | [(Nolabel, {pexp_desc= Pexp_construct ({txt= Lident "()"}, None)})] -> acc - | (Nolabel, _) :: _rest -> - raise - (Invalid_argument - "JSX: found non-labelled argument before the last position" ) + | (Nolabel, {pexp_loc}) :: _rest -> + Location.raise_errorf ~loc:pexp_loc + "JSX: found non-labelled argument before the last position" | arg :: rest -> allButLast_ rest (arg :: acc) in @@ -107,9 +106,9 @@ let extractChildren ?(removeLastPositionUnit = false) ~loc propsAndChildren = , if removeLastPositionUnit then allButLast props else props ) | [(_, childrenExpr)], props -> (childrenExpr, if removeLastPositionUnit then allButLast props else props) - | _ -> - raise - (Invalid_argument "JSX: somehow there's more than one `children` label") + | _first :: (_, {pexp_loc}) :: _rest, _props -> + Location.raise_errorf ~loc:pexp_loc + "JSX: somehow there's more than one `children` label" let unerasableIgnore loc = { attr_name= {txt= "warning"; loc} @@ -148,19 +147,19 @@ let rec getFnName = function txt | {ppat_desc= Ppat_constraint (pat, _)} -> getFnName pat - | _ -> - raise (Invalid_argument "react.component calls cannot be destructured.") + | {ppat_loc} -> + Location.raise_errorf ~loc:ppat_loc + "react.component calls cannot be destructured." (* Lookup the value of `props` otherwise raise Invalid_argument error *) let getPropsNameValue _acc (loc, exp) = match (loc, exp) with | {txt= Lident "props"}, {pexp_desc= Pexp_ident {txt= Lident str}} -> {propsName= str} - | {txt}, _ -> - raise - (Invalid_argument - ( "react.component only accepts props as an option, given: " - ^ Longident.last_exn txt ) ) + | {txt; loc}, _ -> + Location.raise_errorf ~loc + "react.component only accepts props as an option, given: %s" + (Longident.last_exn txt) (* Lookup the `props` record or string as part of [@react.component] and store the name for use when rewriting *) let get_props_attr payload = @@ -178,11 +177,9 @@ let get_props_attr payload = Pstr_eval ({pexp_desc= Pexp_ident {txt= Lident "props"}}, _) } :: _rest ) ) -> {propsName= "props"} - | Some (PStr ({pstr_desc= Pstr_eval (_, _)} :: _rest)) -> - raise - (Invalid_argument - "react.component accepts a record config with props as an options." - ) + | Some (PStr ({pstr_desc= Pstr_eval (_, _); pstr_loc} :: _rest)) -> + Location.raise_errorf ~loc:pstr_loc + "react.component accepts a record config with props as an options." | _ -> default_props