Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ppx: use Location.raise_errorf instead of invalid_arg in a few more p…
Browse files Browse the repository at this point in the history
…laces
glennsl committed Feb 25, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent de7b6c4 commit c9c3864
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions ppx/ppx.ml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c9c3864

Please sign in to comment.