Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Generic JSX transform #6565

Merged
merged 8 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

#### :rocket: New Feature

- experimental support of tagged template literals, eg ```sql`select * from ${table}```. https://github.com/rescript-lang/rescript-compiler/pull/6250
- Experimental support of tagged template literals, eg ```sql`select * from ${table}```. https://github.com/rescript-lang/rescript-compiler/pull/6250
- Experimental support for generic/custom JSX transforms. https://github.com/rescript-lang/rescript-compiler/pull/6565

#### :bug: Bug Fix

Expand Down
8 changes: 4 additions & 4 deletions jscomp/bsb/bsb_jsx.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type version = Jsx_v3 | Jsx_v4
type module_ = React
type module_ = React | Generic of {moduleName: string}
type mode = Classic | Automatic
type dependencies = string list

Expand All @@ -15,7 +15,7 @@ let encode_no_nl jsx =
| None -> ""
| Some Jsx_v3 -> "3"
| Some Jsx_v4 -> "4")
^ (match jsx.module_ with None -> "" | Some React -> "React")
^ (match jsx.module_ with None -> "" | Some React -> "React" | Some Generic {moduleName} -> moduleName)
^
match jsx.mode with
| None -> ""
Expand Down Expand Up @@ -55,10 +55,10 @@ let from_map map =
`Obj
(fun m ->
match m.?(Bsb_build_schemas.jsx_module) with
| Some (Str { loc; str }) -> (
| Some (Str { str }) -> (
match str with
| "react" -> module_ := Some React
| _ -> Bsb_exception.errorf ~loc "Unsupported jsx module %s" str)
| moduleName -> module_ := Some (Generic {moduleName}))
| Some x ->
Bsb_exception.config_error x
"Unexpected input (jsx module name) for jsx module"
Expand Down
3 changes: 2 additions & 1 deletion jscomp/bsb/bsb_ninja_rule.ml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config)
| None, None -> ());
(match jsx.module_ with
| None -> ()
| Some React -> Ext_buffer.add_string buf " -bs-jsx-module react");
| Some React -> Ext_buffer.add_string buf " -bs-jsx-module react"
| Some Generic {moduleName} -> Ext_buffer.add_string buf (" -bs-jsx-module " ^ moduleName));
(match jsx.mode with
| None -> ()
| Some Classic -> Ext_buffer.add_string buf " -bs-jsx-mode classic"
Expand Down
1 change: 0 additions & 1 deletion jscomp/bsc/rescript_compiler_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ let buckle_script_flags : (string * Bsc_args.spec * string) array =
"*internal* Set jsx version";

"-bs-jsx-module", string_call (fun i ->
(if i <> "react" then Bsc_args.bad_arg (" Not supported jsx-module : " ^ i));
Js_config.jsx_module := Js_config.jsx_module_of_string i),
"*internal* Set jsx module";

Expand Down
5 changes: 3 additions & 2 deletions jscomp/common/js_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
(** Browser is not set via command line only for internal use *)

type jsx_version = Jsx_v3 | Jsx_v4
type jsx_module = React
type jsx_module = React | Generic of {moduleName: string}
type jsx_mode = Classic | Automatic

let no_version_header = ref false
Expand Down Expand Up @@ -64,6 +64,7 @@ let int_of_jsx_version = function

let string_of_jsx_module = function
| React -> "react"
| Generic {moduleName} -> moduleName

let string_of_jsx_mode = function
| Classic -> "classic"
Expand All @@ -76,7 +77,7 @@ let jsx_version_of_int = function

let jsx_module_of_string = function
| "react" -> React
| _ -> React
| moduleName -> Generic {moduleName}

let jsx_mode_of_string = function
| "classic" -> Classic
Expand Down
2 changes: 1 addition & 1 deletion jscomp/common/js_config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

type jsx_version = Jsx_v3 | Jsx_v4
type jsx_module = React
type jsx_module = React | Generic of {moduleName: string}
type jsx_mode = Classic | Automatic

(* val get_packages_info :
Expand Down
4 changes: 2 additions & 2 deletions jscomp/frontend/ppx_entry.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let rewrite_signature (ast : Parsetree.signature) : Parsetree.signature =
let jsxVersion = int_of_jsx_version jsxVersion in
let jsxModule = string_of_jsx_module !jsx_module in
let jsxMode = string_of_jsx_mode !jsx_mode in
Reactjs_jsx_ppx.rewrite_signature ~jsxVersion ~jsxModule ~jsxMode ast
Jsx_ppx.rewrite_signature ~jsxVersion ~jsxModule ~jsxMode ast
in
if !Js_config.no_builtin_ppx then ast
else
Expand All @@ -55,7 +55,7 @@ let rewrite_implementation (ast : Parsetree.structure) : Parsetree.structure =
let jsxVersion = int_of_jsx_version jsxVersion in
let jsxModule = string_of_jsx_module !jsx_module in
let jsxMode = string_of_jsx_mode !jsx_mode in
Reactjs_jsx_ppx.rewrite_implementation ~jsxVersion ~jsxModule ~jsxMode ast
Jsx_ppx.rewrite_implementation ~jsxVersion ~jsxModule ~jsxMode ast
in
if !Js_config.no_builtin_ppx then ast
else
Expand Down
4 changes: 2 additions & 2 deletions jscomp/syntax/cli/res_cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ module CliArgProcessor = struct
else exit 1)
else
let parsetree =
Reactjs_jsx_ppx.rewrite_signature ~jsxVersion ~jsxModule ~jsxMode
Jsx_ppx.rewrite_signature ~jsxVersion ~jsxModule ~jsxMode
parseResult.parsetree
in
printEngine.printInterface ~width ~filename
Expand All @@ -300,7 +300,7 @@ module CliArgProcessor = struct
else exit 1)
else
let parsetree =
Reactjs_jsx_ppx.rewrite_implementation ~jsxVersion ~jsxModule ~jsxMode
Jsx_ppx.rewrite_implementation ~jsxVersion ~jsxModule ~jsxMode
parseResult.parsetree
in
printEngine.printImplementation ~width ~filename
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ type jsxConfig = {
mutable module_: string;
mutable mode: string;
mutable nestedModules: string list;
mutable hasReactComponent: bool;
mutable hasComponent: bool;
}

(* Helper method to look up the [@react.component] attribute *)
let hasAttr (loc, _) = loc.txt = "react.component"
let hasAttr (loc, _) =
match loc.txt with
| "react.component" | "jsx.component" -> true
| _ -> false

(* Iterate over the attributes and try to find the [@react.component] attribute *)
let hasAttrOnBinding {pvb_attributes} =
Expand All @@ -20,7 +23,7 @@ let coreTypeOfAttrs attributes =
List.find_map
(fun ({txt}, payload) ->
match (txt, payload) with
| "react.component", PTyp coreType -> Some coreType
| ("react.component" | "jsx.component"), PTyp coreType -> Some coreType
| _ -> None)
attributes

Expand All @@ -37,7 +40,7 @@ let typVarsOfCoreType {ptyp_desc} =

let raiseError ~loc msg = Location.raise_errorf ~loc msg

let raiseErrorMultipleReactComponent ~loc =
let raiseErrorMultipleComponent ~loc =
raiseError ~loc
"Only one component definition is allowed for each module. Move to a \
submodule or other file if necessary."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ let updateConfig config payload =
let fields = getPayloadFields payload in
(match getInt ~key:"version" fields with
| None -> ()
| Some i -> config.React_jsx_common.version <- i);
(match getString ~key:"module" fields with
| Some i -> config.Jsx_common.version <- i);
(match getString ~key:"module_" fields with
Comment on lines +53 to +54
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old mechanism didn't actually work, because module is a reserved keyword.

| None -> ()
| Some s -> config.module_ <- s);
match getString ~key:"mode" fields with
Expand All @@ -68,7 +68,7 @@ let getMapper ~config =
Reactjs_jsx_v3.jsxMapper ~config
in
let expr4, module_binding4, transformSignatureItem4, transformStructureItem4 =
Reactjs_jsx_v4.jsxMapper ~config
Jsx_v4.jsxMapper ~config
in

let expr mapper e =
Expand All @@ -89,18 +89,18 @@ let getMapper ~config =
version = config.version;
module_ = config.module_;
mode = config.mode;
hasReactComponent = config.hasReactComponent;
hasComponent = config.hasComponent;
}
in
let restoreConfig oldConfig =
config.version <- oldConfig.React_jsx_common.version;
config.version <- oldConfig.Jsx_common.version;
config.module_ <- oldConfig.module_;
config.mode <- oldConfig.mode;
config.hasReactComponent <- oldConfig.hasReactComponent
config.hasComponent <- oldConfig.hasComponent
in
let signature mapper items =
let oldConfig = saveConfig () in
config.hasReactComponent <- false;
config.hasComponent <- false;
let result =
List.map
(fun item ->
Expand All @@ -119,7 +119,7 @@ let getMapper ~config =
in
let structure mapper items =
let oldConfig = saveConfig () in
config.hasReactComponent <- false;
config.hasComponent <- false;
let result =
List.map
(fun item ->
Expand All @@ -143,11 +143,11 @@ let rewrite_implementation ~jsxVersion ~jsxModule ~jsxMode
(code : Parsetree.structure) : Parsetree.structure =
let config =
{
React_jsx_common.version = jsxVersion;
Jsx_common.version = jsxVersion;
module_ = jsxModule;
mode = jsxMode;
nestedModules = [];
hasReactComponent = false;
hasComponent = false;
}
in
let mapper = getMapper ~config in
Expand All @@ -157,11 +157,11 @@ let rewrite_signature ~jsxVersion ~jsxModule ~jsxMode
(code : Parsetree.signature) : Parsetree.signature =
let config =
{
React_jsx_common.version = jsxVersion;
Jsx_common.version = jsxVersion;
module_ = jsxModule;
mode = jsxMode;
nestedModules = [];
hasReactComponent = false;
hasComponent = false;
}
in
let mapper = getMapper ~config in
Expand Down
File renamed without changes.
Loading