This repository was archived by the owner on Jun 15, 2023. It is now read-only.
Jsx4 automatic mode: suspicious Caml_option.some #667
Copy link
Copy link
Closed
Description
Context
Dependencies:
"@rescript/react": "^0.11.0-alpha.1",
"rescript": "^10.1.0-alpha.2",
bsconfig.json
{
"name": "speedycal",
"version": "0.0.1",
"sources": {
"dir": "src"
},
"package-specs": {
"module": "es6",
"in-source": true
},
"suffix": ".js",
"jsx": { "version": 4, "mode": "automatic" },
"bs-dependencies": ["@rescript/react"]
}
What am I seeing?
// Doctor.res
module User = {
type t = {firstName: string, lastName: string}
let format = user => "Dr." ++ user.lastName
@react.component
let make = (~doctor) => {
<h1> {React.string(format(doctor))} </h1>
}
}Compiles to:
function Doctor$User(props) {
return JsxRuntime.jsx("h1", {
children: Caml_option.some("Dr." + props.doctor.lastName)
});
}The Caml_option.some seems suspicious in the output. The type of ~doctor is t, it isn't optional.
If you compare this with what's running in the playground right now: link to working code
// Generated by ReScript, PLEASE EDIT WITH CARE
'use strict';
var React = require("react");
function format(user) {
return "Dr." + user.lastName;
}
function Playground$User(Props) {
var doctor = Props.doctor;
return React.createElement("h1", undefined, "Dr." + doctor.lastName);
}
var User = {
format: format,
make: Playground$User
};
exports.User = User;
/* react Not a pure module */We don't have any Caml_option.some here.
Any pointers on why I might be seeing Caml_option.some in the output? cc @mattdamon108 @cristianoc
Debug info
test file:
type t = {firstName: string, lastName: string}
let format = user => "Dr." ++ user.lastName
@react.component
let make = (~doctor) => {
<h1> {React.string(format(doctor))} </h1>
}With dune exec -- rescript -jsx-mode automatic -jsx-version 4 -print res test.res:
type t = {firstName: string, lastName: string}
let format = user => "Dr." ++ user.lastName
type props<'doctor> = {
doctor: 'doctor,
}
@react.component
let make = ({doctor, _}: props<'doctor>) => {
ReactDOM.jsx("h1", {children: {React.string(format(doctor))}})
}
let make = {
let \"Test" = (props: props<_>) => make(props)
\"Test"
}
With dune exec -- rescript -jsx-version 3 -print res test.res:
type t = {firstName: string, lastName: string}
let format = user => "Dr." ++ user.lastName
@obj external makeProps: (~doctor: 'doctor, ~key: string=?, unit) => {"doctor": 'doctor} = ""
@react.component
let make =
(@warning("-16") ~doctor) => {
ReactDOMRe.createDOMElementVariadic("h1", [{React.string(format(doctor))}])
}
let make = {
let \"Test" = (\"Props": {"doctor": 'doctor}) => make(~doctor=\"Props"["doctor"])
\"Test"
}Metadata
Metadata
Assignees
Labels
No labels
Activity
cristianoc commentedon Oct 2, 2022
Simple example: children has an optional abstract type, so the optimisation does not kick in.
mununki commentedon Oct 2, 2022
If the poc of record type extension would be landed later, we might be able to use two different type
which are children field as optional and not.which are having the children field and not having it.cristianoc commentedon Oct 4, 2022
Probably easier just to use a cast from element to opt element.
mununki commentedon Oct 4, 2022
Can you explain more detail? I don't get it. Do you mean this?
cristianoc commentedon Oct 4, 2022
This:
can be used instead of
Somemununki commentedon Oct 4, 2022
I ran with compiler#rewrite-jsxdom, syntax#jsx-v4-lowercase and rescript-react#master
It compiles and generates the output:
Caml_option.somseems not using it.mununki commentedon Oct 4, 2022
I guess the PR #665 will generated more cleaner output #665 (comment)
cristianoc commentedon Oct 4, 2022
Is that in jsx mode? What changed?
mununki commentedon Oct 4, 2022
My mistake. I was confused. I ran the example component in the sample project:
It compiled okay, so I thought it would be compiled in
automaticmode successfully. But, I changed bsconfig.json and ran it again it fails.The error message is correct.
JsxDOM.domPropsdoesn't have a children field now. I should add a children field intoJsxDOM.domPropsor make new function e.g.addChildrenProplikeaddKeyProp.At the same time, I'm gonna check why the
jsxConfig{{version:4, mode:"automatic"}seems not working under global config of "classic".mununki commentedon Oct 4, 2022
It works! How does this work? What makes it different from
?Some?cristianoc commentedon Oct 4, 2022
It prevents the compiler from knowing it's an option, and using a conservative approach.