-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Enable the use of ergonomic rust idioms to construct JSX through macro expansion. #1168
Comments
I think this would be cool (file based components)! |
You might also be interested in taking a look at yew_dsl (still a work in progress). |
This is indeed a limitation of rustfmt, it doesn't format any macro (except a very select few), and AFAIK doesn't have any way to add an 'extension' to it. Thus the issue would persist no matter how the macro looks.
This is something you could fix in your IDE usually! You would have to check and see how it evaluates its indentation and change its behavior whenever it encounters an
Could you expand on what is 'JSX text'? From looking at the link I didn't quite catch that. |
This is objectively untrue. There is no difference between macro and non macro syntax as far as the text editor is concerned. For example: fn main() {
println!(
"my IDE auto-indented my cursor to where this string starts"
);
// cursor for this line was also auto-indented, exactly the same as for non-macro stuff
} To be honest, I have no idea where this notion is coming from.
I personally don't feel there's any merit to expecting users to patch their IDE for it to be compatible with a library. There are some exceptions to this, though I don't see how this could be one of them. At the very least, an ergonomic way to bring in the content of another file so the JSX-like syntax can be edited without being analysed as Rust code.
this should explain. Yew describes the content of the |
@Ben-PH it's not important to pass JSX-like expressions to the compiler (because the result of these expressions have to be further expanded by the Something being considered is an approach to building instances of
I'm not sure this is the case (I also think that the choice of wording in "objectively untrue" was a little harsh and would be better phrased as a statement of experience, e.g. "in my experience I've found that |
The
|
I've never had any trouble with IDE tools recognising macro syntax. The opposite is true of the JSX-like syntax. Analysing text using rust syntax rules on text that follows a completely different rule-set is broken to a fundamental degree. Granted, I'm no expert. The fact remains, though, that the behavior of formatting tools working on use of macro and non-macro code is, for all intents and purposes, indistinguishable. Generally, I make a best-effort to reserve my statements similar to how you suggest. I rarely use the word objectively, but I did so in this case as I thought it was self-evident.
Overall, the auto-formatting limitations that come from bringing non-rust syntax into a rust file essentially mean that formatting tools are pretty much entirely broken. The limitations you linked to seem to be minor edge cases only. Given that the difference is so profound, I fail to see why this is even being discussed. Am I missing something?
That's one way to address the problem. I would argue that offloading compile-time work to run-time risks introducing runtime performance regressions. Given that it's related to DOM element generation, such regressions are likely to impact user-perceived performance to a greater degree. Of course, this could end up not being the case and we have a win-win scenario, or other issues at play that ore more important. |
I agree that doing work at run-time that could be done at compile time seems a waste. Rather than having macros which expand and are then passed into the I like the idea of file-based components though! |
Expanding dirctly into a canonical representation of vdom elements at compile time, with the only run-time concern being the things that cannot be determined statically should be the end goal, I imagine. One possible caveat is binary size, something that is more of an issue when the binary has to be delivered through a series of tubes filled with cats. Whether or not we go for file-based components, go for a rust-based syntax, JSX-like syntax in a |
I think there are simillar arguments about JSX in Javascript. At least, there should be the option to not use JSX. My personal feeling is that Passing the result of macro expansions for specific tags to the |
I like the idea of a file-based syntax because I think it makes it easy for us to handle scoped CSS (in a similar way to Vue). I guess it depends on what @jstarry thinks. |
@Ben-PH are you interested in working on something like this? |
Macro development is way outside my forte. I'm happy to be involved if anything comes u that I can help with, though. |
I have produced a crate which does something which is not exactly this, but is reasonably close (and enables formatting!) It's called Malvolio. |
This problem can also be mitigated by keeping logic outside let onclick = self.link.callback(|_| Msg::AddOne);
let value = self.value;
html! {
<div>
<button onclick=onclick>{ "+1" }</button>
<p>{ value }</p>
</div>
} |
Closing this since this has been adding to Yew (in form of Feel free to comment here/re-open this if you have a better solution that you would like to propose. |
Is your feature request related to a problem? Please describe.
JSX syntax is, in my opinion, not an ideal form to write a markup specification, particularly in the context of a .rs file - it introduces rust rules to a non-rust syntax. For example,
rustfmt
does not recognize the following for correction:Note: The above example was a result of auto-indentation by my IDE.
Ideally, the above could be expressed in a 'rusty' way.
Describe the solution you'd like
Given that there is a limited set of JSX elements, and a formal specification for it is in the works, the use of macros that expand into JSX text will maintain the JSX expressions to be given to the compiler, while using rust syntax. The above example would become this:
or:
Both of these proposals would expand to have the same semantics as the original example
for custom tags, you could add
html::custom_tag!(foo) {bar}
which would expand to<foo>bar</foo>
Describe alternatives you've considered
Alternatively, an ergonomic means to import JSX at compile-time, such as this:
and that path+file contains the JSX text inside the
html!
Additional comment
This would be a good first issue for this project and involved contributers. The implementation ranges from trivial (simple tags such as
to potentially complex (app defined tags, standard, custom attributes, etc), would require productive collaboration, and careful management in order to introduce it into yew.
The text was updated successfully, but these errors were encountered: