-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Support xml namespace prefix for JSX elements and attributes #37421
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
Conversation
hey man GJ do you know if it'll fix this |
@4r7d3c0 It will not. Sorry! |
Just for curious, what will the type check semantics for JSX namespace? Does it checked as a whole identifier like |
@Jack-Works Good thoughts. My guess would be that it checks the whole identifier, but I didn't include any tests for that. I'll look into the details of this when I have some time. |
Hi! My PR #37455 has been merged and it changed the identifier scanner to add JSX hyphen and JSX namespace support. (By a second parameter |
Why close it? 🤔 |
Oh, I thought you meant your PR had fixed the same issue. I'll reopen it, then. |
The reviewers list isn't working, but @weswigham and @DanielRosenwasser , you might want to look at this too. |
As it stands this PR needs
I think @RyanCavanaugh is a better person to speak to the expectations of this PR, and I would hold off on further work until there's further clarity. |
Unless I'm mistaken, it should get compiled to something like
based on how we parse it? Which seems.... ok. Even if there's no spec stating as such, I'm not sure what else would be reasonable. |
Does react ok with this emit behavior? For example have a try to use it to render svg with svg ns |
The Babel React transform issues the message:
|
@andrewbranch How do you think the type of |
I really doubt that it makes sense to use a nested property. I assume the property name on the element’s props type would just be |
By making it nested, it is possible to reuse attributes. for example: interface IntrinsicElements {
element: {
namespace1: attributes // type reference
namespace2: attributes // type reference
}
} Without this, I have to write |
@Jack-Works What would you propose namespaced tags should emit or typecheck against? IMHO, understanding that I'm not sure what can be emitted (or typechecked against) except plain identifiers. Wouldn't nested types require nested props ( I'll note that namespaced JSX is useful to generate XML from JSX, using a package like jsx-xml. I would like this to work: import { JSXXML } from 'jsx-xml';
const xml = <ns:tag ns:attr="foobar">foobaz</ns:tag>; With import { JSXXML } from 'jsx-xml';
const xml = JSXXML("ns:tag", { "ns:attr": "foobar" }, "foobaz"); |
Maybe we should leave the type check behavior until the real world usage |
Is there any work I can do on this PR regarding the discussions above? It is difficult for me to discern a consenus about what is needed. |
I have removed the check that the code is up to date with master since it has been long enough there are conflicts now. I still would be willing to move forward on this PR in my free time if there is a clear path. |
I’d like to hear some more detailed use cases for namespaced attributes so we can make sure we’re not boxing ourselves into a type checking behavior we’ll be unhappy with. We can’t just ignore the type checking behavior for now, because if I understand correctly, merging this as-is would allow libraries to augment |
We can ship the syntax first, not allowing it when |
I've been using JSX in Solid Framework, and I was hoping to use it not only for SVG namespaces, but for custom binding syntax like you find in Vue or Svelte. For example: https://vuejs.org/v2/guide/syntax.html#Arguments To be fair this is custom JSX compilation so just unbreaking this for Vue actually supports JSX but can't use its normal syntax with it I wonder if @yyx990803 and co would be interested in this feature so that they can use their directives in JSX. EDIT: I guess it has never been mentioned before in this thread but looking at the linked issues this use case has been brought up several times(Vue, Adobe, and Solid related). From reading the threads I'd think this is a primary use case. I think SVG namespace support to mirror how normal markup work is important even if libraries like React came up with special JSX attributes as a workaround. So there are definitely real-world use cases. |
@tomjw64 sorry this has taken so long, but we’ve decided this is looking good for the 4.2 release, which means we could merge as soon as 4.1-rc is out. The type checking behavior should behave just like there are colons in the element names and element attributes—no nested objects. This means the type checking should already work, but it would be good to see a test or two: // @noImplicitAny: true
// @jsx: preserve
declare namespace JSX {
interface IntrinsicElements {
"ns:element": {
"ns:attribute": string;
}
}
}
const e = <ns:element ns:attribute="yep" />; As an aside:
I think this should be possible with #40336 now 😄 We also decided that there’s not a great reason to limit this to We also need to make sure that we gracefully error on Thanks for your patience! I’ll be keeping an eye out to make sure this is ready for 4.2 and to help with whatever you need. |
@andrewbranch great to hear. I'll do what I can to add those tests, and reach out if I hit a snag. |
Just as with the `-` character, `:` is now also treated specially in JSX element and attribute names, but is only allowed a single time, and not at the beginning or end of the name, as is specified in the JSX spec. All tests in jsxInvalidEsprimaTestSuite still fail, but for slightly different reasons now. Two lines in jsxEsprimaFbTestSuite were uncommented as they included elements with namespaces, and they now pass without error.
3f809c8
to
c3d75d1
Compare
c3d75d1
to
6292eaa
Compare
@andrewbranch Added all the aforementioned tests, and it looks pretty decent to my eyes. |
This looks great! The only questions I’d like to get feedback on from the team are:
|
Great thoughts. This work parallels the work to allow the
which special-cases jsx names with the |
While looking around, I also found this in
I may have to look into how this might affect things if this no longer holds, although I'm not really sure what a "known property" is. |
Great find on
|
9a7d384
to
079e82f
Compare
I made the adjustment on isIntrinsicJsxName. Now we get parseable JS in the React test case even when using an capitalized identifier that includes a colon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 and now we wait until the release-4.1 branch is cut from master and hope there aren’t too many merge conflicts in the meantime
src/compiler/checker.ts
Outdated
* Returns true if the hyphen character is not in the JSX element name. The hyphen character, if present, | ||
* would prevent it from being a valid JS identifier, ignoring restrictions about keywords not being identifiers | ||
*/ | ||
function isUnhyphenatedJsxName(name: string | __String) { | ||
// - is the only character supported in JSX attribute names that isn't valid in JavaScript identifiers | ||
// - is one of only two characters supported in JSX attribute names that isn't valid in JavaScript identifiers, : being the other |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my quick glance at this function’s usage, it seemed to me like the note about identifier validity isn’t actually relevant. On second thought, I would probably just remove these comments since they’re subtly misleading about the purpose of the function. The name and one-line implementation of the function are sufficiently self-explanatory.
@SoloJiang it is / will be in 4.2. |
I got tired of the VSCode TypeScript checker complaining about JSX that React, Babel, and the JSX spec consider perfectly fine, so I thought I'd check to see this feature has been requested in the past and make a PR if so. Looked to be two issues requesting the support. I haven't contributed to TypeScript before, so I've probably done this all wrong, but I'm willing to put some more of my personal time into this to get it right. Thanks for your time.
Checks
Backlog
milestone (required)No, but here's a quote saying there's a chance that a PR could be taken (@RyanCavanaugh on March 7, 2016):
master
branchgulp runtests
locallyFixes #11833, #7411
Commit message: