Generics with React TSX Language Leave `plain-text` Token Open
See original GitHub issueInformation:
- Prism version: Test Drive version
- Plugins: Test Drive plugins
- Environment: Test Drive page
Description
The generic Foo<string>
in the following code breaks syntax highlighting of the second function Add2
with the React TSX language:
function Add1(a, b) { return <div>a + b</div> }
type Bar = Foo<string>;
function Add2(a, b) { return <div>a + b</div> }
Example
Screenshots from Test Drive:
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Make your react component generic with Typescript - Medium
Using Typescript generics turns out to be a way to make the component cleaner and keep it open for extension, but closed for...
Read more >VS Code API | Visual Studio Code Extension API
VS Code API. VS Code API is a set of JavaScript APIs that you can invoke in your Visual Studio Code extension. This...
Read more >How To Use Generics in TypeScript - DigitalOcean
Generics are a fundamental feature of statically-typed languages, allowing developers to pass types as parameters to another type, function, or ...
Read more >React app with Typescript, Using generic spread expressions ...
You should use typescript eslint parser: // eslintrc.json ... "parser": "@typescript-eslint/parser", ... And also extends typescript babel ...
Read more >Language Server Protocol Specification - 3.17
The following TypeScript definitions describe the base JSON-RPC protocol: ... version: integer; /** * The content of the opened text document.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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. The main reason I’m somewhat against it is that it means that Prism’s highlighting will be very whitespace-sensitive. The whitespace-sensitivity goes both ways:
Foo <Bar>
should be a simple generic type but it won’t be highlighted as such.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.
type Foo = <T>(x: T) => T
) will most likely still be highlighted incorrectly even with a CF grammar.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.
<br>
). I don’t know if JSX/TSX have some restrictions here and actually demand valid XML but I doubt it.Secondly, nested tags. Let’s look at
<div><div></div></div>
. Obviously, we can’t stop after the first</div>
but that means that we have to count the number of opening and closing divs which is impossible for regexes.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
<p>foo <strong>bar</strong></p>
which is just sad.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.