Skip to content

Commit

Permalink
feat: migrate from bs.* to mel.* attributes (#755)
Browse files Browse the repository at this point in the history
* feat: migrate from `bs.*` to `mel.*` attributes

* melange2: remove mel.val

* update tests

* ci: update opam to use 5.1.x

* add melange.dom to libraries

* test(ci): fix flakes / OCaml version / add melange.dom

* rm dupe

---------

Co-authored-by: Javier Chavarri <[email protected]>
  • Loading branch information
anmonteiro and jchavarri committed Sep 6, 2023
1 parent dbd608b commit d3869f9
Show file tree
Hide file tree
Showing 28 changed files with 2,026 additions and 1,998 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ocamlVersion: [4_14, 5_0]
ocamlVersion: [5_1]
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v21
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/opam.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
# disabling this for now

ocaml-compiler:
- 4.14.x
- ocaml-variants.5.1.0+trunk

runs-on: ${{ matrix.os }}

Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ format-check: ## Checks if format is correct

.PHONY: install
install: ## Update the package dependencies when new deps are added to dune-project
@opam pin add -y melange --dev-repo
@opam pin add -y melange.dev --dev-repo
@opam pin add -y reason.dev --dev-repo
@opam install . --deps-only --with-test
@npm install

.PHONY: init
create-switch: ## Create a local opam switch
@opam switch create . 4.14.1 --no-install
@opam switch create . 5.1.0~alpha2 --no-install

.PHONY: init
init: create-switch install ## Create a local opam switch, install deps
39 changes: 19 additions & 20 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages."${system}".appendOverlays [
(self: super: { ocamlPackages = super.ocaml-ng.ocamlPackages_4_14; })
(self: super: { ocamlPackages = super.ocaml-ng.ocamlPackages_5_1; })
melange.overlays.default
];
in
Expand Down
50 changes: 25 additions & 25 deletions ppx/src/reactjs_jsx_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let constantString ~loc str =
let safeTypeFromValue valueStr =
let valueStr = getLabel valueStr in
match String.sub valueStr 0 1 with "_" -> "T" ^ valueStr | _ -> valueStr
[@@raises Invalid_argument]
[@@raises Invalid_argument]

let keyType loc =
Typ.constr ~loc { loc; txt = optionIdent }
Expand Down Expand Up @@ -84,11 +84,11 @@ let extractChildren ?(removeLastPositionUnit = false) propsAndChildren =
(Invalid_argument
"JSX: found non-labelled argument before the last position")
| arg :: rest -> allButLast_ rest (arg :: acc)
[@@raises Invalid_argument]
[@@raises Invalid_argument]
in
let allButLast lst =
allButLast_ lst [] |> List.rev
[@@raises Invalid_argument]
[@@raises Invalid_argument]
in
match
List.partition
Expand All @@ -111,7 +111,7 @@ let extractChildren ?(removeLastPositionUnit = false) propsAndChildren =
| _ ->
raise
(Invalid_argument "JSX: somehow there's more than one `children` label")
[@@raises Invalid_argument]
[@@raises Invalid_argument]

let unerasableIgnore loc =
{
Expand Down Expand Up @@ -158,7 +158,7 @@ let getFnName binding =
| { pvb_pat = { ppat_desc = Ppat_var { txt } } } -> txt
| _ ->
raise (Invalid_argument "react.component calls cannot be destructured.")
[@@raises Invalid_argument]
[@@raises Invalid_argument]

let makeNewBinding binding expression newName =
match binding with
Expand All @@ -172,7 +172,7 @@ let makeNewBinding binding expression newName =
}
| _ ->
raise (Invalid_argument "react.component calls cannot be destructured.")
[@@raises Invalid_argument]
[@@raises Invalid_argument]

(* Lookup the value of `props` otherwise raise Invalid_argument error *)
let getPropsNameValue _acc (loc, exp) =
Expand All @@ -184,7 +184,7 @@ let getPropsNameValue _acc (loc, exp) =
(Invalid_argument
("react.component only accepts props as an option, given: "
^ Longident.last_exn txt))
[@@raises Invalid_argument]
[@@raises Invalid_argument]

(* Lookup the `props` record or string as part of [@react.component] and store
the name for use when rewriting *)
Expand Down Expand Up @@ -212,7 +212,7 @@ let getPropsAttr payload =
(Invalid_argument
"react.component accepts a record config with props as an options.")
| _ -> defaultProps
[@@raises Invalid_argument]
[@@raises Invalid_argument]

(* Plucks the label, loc, and type_ from an AST node *)
let pluckLabelDefaultLocType (label, default, _, _, loc, type_) =
Expand Down Expand Up @@ -309,7 +309,7 @@ let rec recursivelyMakeNamedArgsForExternal list args =
| _label, Some type_, _ -> type_)
args)
| [] -> args
[@@raises Invalid_argument]
[@@raises Invalid_argument]

(* Build an AST node for the [@bs.obj] representing props for a component *)
let makePropsValue fnName loc namedArgListWithKeyAndRef propsType =
Expand All @@ -330,14 +330,14 @@ let makePropsValue fnName loc namedArgListWithKeyAndRef propsType =
pval_attributes =
[
{
attr_name = { txt = "bs.obj"; loc };
attr_name = { txt = "mel.obj"; loc };
attr_payload = PStr [];
attr_loc = loc;
};
];
pval_loc = loc;
}
[@@raises Invalid_argument]
[@@raises Invalid_argument]

(* Build an AST node representing an `external` with the definition of the
[@bs.obj] *)
Expand All @@ -348,7 +348,7 @@ let makePropsExternal fnName loc namedArgListWithKeyAndRef propsType =
Pstr_primitive
(makePropsValue fnName loc namedArgListWithKeyAndRef propsType);
}
[@@raises Invalid_argument]
[@@raises Invalid_argument]

(* Build an AST node for the signature of the `external` definition *)
let makePropsExternalSig fnName loc namedArgListWithKeyAndRef propsType =
Expand All @@ -357,7 +357,7 @@ let makePropsExternalSig fnName loc namedArgListWithKeyAndRef propsType =
psig_desc =
Psig_value (makePropsValue fnName loc namedArgListWithKeyAndRef propsType);
}
[@@raises Invalid_argument]
[@@raises Invalid_argument]

(* Build an AST node for the props name when converted to an object inside the
function signature *)
Expand Down Expand Up @@ -453,7 +453,7 @@ let makeExternalDecl fnName loc namedArgListWithKeyAndRef namedTypeList =
makePropsExternal fnName loc
(List.map pluckLabelDefaultLocType namedArgListWithKeyAndRef)
(makePropsType ~loc namedTypeList)
[@@raises Invalid_argument]
[@@raises Invalid_argument]

(* TODO: some line number might still be wrong *)
let jsxMapper =
Expand Down Expand Up @@ -481,7 +481,7 @@ let jsxMapper =
let first = String.sub str 0 1 [@@raises Invalid_argument] in
let capped = String.uppercase_ascii first in
first = capped
[@@raises Invalid_argument]
[@@raises Invalid_argument]
in
let ident =
match modulePath with
Expand Down Expand Up @@ -625,7 +625,7 @@ let jsxMapper =
"react-jsx-ppx: react.component refs only support plain arguments \
and type annotations."
| _ -> (list, None)
[@@raises Invalid_argument]
[@@raises Invalid_argument]
in

let argToType types (name, default, _noLabelName, _alias, loc, type_) =
Expand Down Expand Up @@ -685,7 +685,7 @@ let jsxMapper =
} )
:: types
| _ -> types
[@@raises Invalid_argument]
[@@raises Invalid_argument]
in

let argToConcreteType types (name, loc, type_) =
Expand All @@ -706,7 +706,7 @@ let jsxMapper =
pstr_desc =
Pstr_primitive
({ pval_name = { txt = fnName }; pval_attributes; pval_type } as
value_description);
value_description);
} as pstr -> (
match List.filter hasAttr pval_attributes with
| [] -> structure :: returnStructures
Expand Down Expand Up @@ -1113,7 +1113,7 @@ let jsxMapper =
in
(Some externalDecl, bindings, newBinding)
else (None, [ binding ], None)
[@@raises Invalid_argument]
[@@raises Invalid_argument]
in
let structuresAndBinding = List.map mapBinding valueBindings in
let otherStructures (extern, binding, newBinding)
Expand Down Expand Up @@ -1146,12 +1146,12 @@ let jsxMapper =
])
@ returnStructures
| structure -> structure :: returnStructures
[@@raises Invalid_argument]
[@@raises Invalid_argument]
in

let reactComponentTransform ~ctxt mapper structures =
List.fold_right (transformComponentDefinition ~ctxt mapper) structures []
[@@raises Invalid_argument]
[@@raises Invalid_argument]
in

let transformComponentSignature _mapper signature returnSignatures =
Expand All @@ -1161,7 +1161,7 @@ let jsxMapper =
psig_desc =
Psig_value
({ pval_name = { txt = fnName }; pval_attributes; pval_type } as
psig_desc);
psig_desc);
} as psig -> (
match List.filter hasAttr pval_attributes with
| [] -> signature :: returnSignatures
Expand Down Expand Up @@ -1218,12 +1218,12 @@ let jsxMapper =
"Only one react.component call can exist on a component at \
one time"))
| signature -> signature :: returnSignatures
[@@raises Invalid_argument]
[@@raises Invalid_argument]
in

let reactComponentSignatureTransform mapper signatures =
List.fold_right (transformComponentSignature mapper) signatures []
[@@raises Invalid_argument]
[@@raises Invalid_argument]
in

let transformJsxCall ~ctxt parentExpLoc mapper callExpression callArguments
Expand Down Expand Up @@ -1263,7 +1263,7 @@ let jsxMapper =
(Invalid_argument
"JSX: `createElement` should be preceeded by a simple, direct \
module name.")
[@@raises Invalid_argument]
[@@raises Invalid_argument]
in

object (self)
Expand Down
Loading

0 comments on commit d3869f9

Please sign in to comment.