-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Generics with React TSX Language Leave plain-text
Token Open
#2594
Comments
Another example, this time with more common, real-world code (highlighting on import React, { MouseEvent, ChangeEvent, FormEvent } from 'react';
function handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
}
function handleChange(event: ChangeEvent<HTMLInputElement>) {
console.log(event.target.value);
}
function handleClick(event: MouseEvent) {
console.log(event.button);
}
export default function Form() {
return (
<form onSubmit={handleSubmit}>
<input onChange={handleChange} placeholder="Name" />
<button onClick={handleClick}></button>
</form>
);
} |
I looked into it and I don't see an easy fix for this issue. I'm sorry to say this but this might be open for a while. |
Yeah, I was thinking that it may not be an easy one to fix! Wonder if this fact could help: there is no matching closing tag for generics but JSX requires a matching closing tag. Was thinking in terms of regex lookahead, but maybe that would be expensive...? |
Or, another idea: require a non-word character It's rare to have text up against the left side of a starting tag in JSX - and it can and most often is formatted differently than this. |
Unfortunately, that won't work for two reasons. Firstly, there are some tags that are self-closing as per HTML spec without the self-closing syntax (e.g. Secondly, nested tags. Let's look at
We can actually go a step further here. JSX/TSX sections are compiled down to JS expressions, so know what the character before it has to look like. JS's regex pattern uses the same idea, so we could just copy the lookbehind. However, this means that Prism wouldn't be able highlight beginner-level JSX code like My main idea on how to implement a fix is to not use regexes. Instead of matching individual HTML tags, we match the whole Markup section. We would wrap each Markup section with its own token and then highlight tags within it. This would make a number of JSX things easier to implement, so I quite like the idea. The problem is that this just can't be done with regexes - no way. Hence #2595. |
Yes, JSX demands self closing tags to have the slash.
Yeah, this is why I was thinking it may be prohibitively expensive. |
Actually, the example you posted above would work. There's a space before the opening A broken example would look like: <p>foo<strong>bar</strong></p> In my experience of looking at code examples since years across many platforms (also beginner platforms like Stack Overflow), I have rarely if ever seen this pattern. I think it seems like a good trade-off if the user is using TSX - JSX and TypeScript together (I'm proposing to NOT change it for the JSX or TypeScript or JavaScript languages on their own) that they format this very uncommon pattern to this code to this equivalent code for proper highlighting (JSX strips the whitespace after new lines). Probably if a user is using all of these technologies, they are not a beginner anyway: <p>
foo
<strong>bar</strong>
</p> |
Oh and super interesting about the context-free grammar issue you opened. Regardless of which way this one request goes, I'll be watching #2595 :) |
That's really good. It makes things a lot easier for us.
I can only second that. Formatters like Prettier will automatically insert the line breaks you added, so that we will ever come across the one-liner in the wild is very unlikely indeed. Nevertheless, I'll make a quick PR for TSX with the lookbehind you proposed. The method isn't perfect but I think the tradeoffs are worth it. (Let's keep this issue open even after the PR is merged.) PS. I also want to point out that inferred type parameters of function expressions (e.g. |
Information:
Description
The generic
Foo<string>
in the following code breaks syntax highlighting of the second functionAdd2
with the React TSX language:Example
Screenshots from Test Drive:
https://prismjs.com/test.html#language=tsx
The text was updated successfully, but these errors were encountered: