Skip to content
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

Syntax error for TypeScript generics #425

Closed
bajtos opened this issue Nov 21, 2020 · 2 comments
Closed

Syntax error for TypeScript generics #425

bajtos opened this issue Nov 21, 2020 · 2 comments
Labels
help wanted lexer bug Highlighting error in a lexer

Comments

@bajtos
Copy link

bajtos commented Nov 21, 2020

Describe the bug

Chroma seems to not understand TypeScript generics with multiple template arguments and/or template arguments using a generic type too.

Consider the following TypeScript code:

export class AuditingRepository<
  T extends Entity,
  ID,
  Relations extends object = {}
> extends DefaultCrudRepository<T, ID, Relations> {
}

Chroma v0.8.2 and Hugo v0.78.2 render the code with a comma (,) characters highlighted in red - perhaps suggesting an error?

chroma-typescript-bug

To Reproduce

Save the following text to bug.ts.

export class AuditingRepository<
  T extends Entity,
  ID,
  Relations extends object = {}
> extends DefaultCrudRepository<T, ID, Relations> {
}

Run chroma as follows:

chroma -s autumn --html --html-lines --html-lines-table --html-inline-styles bug.ts > bug.html

Open the produced HTML file in a Chromium-based browers. You should see the same output as on my screenshot posted above.

@alecthomas alecthomas added help wanted lexer bug Highlighting error in a lexer labels Feb 5, 2021
@fredrare
Copy link
Contributor

fredrare commented Sep 21, 2024

Cause

This happens because the TypeScript lexer also detects JSX. In the case of generics, they are not actually being treated as such, but rather as JSX elements. For this reason, the , is not allowed. Note that this is also an issue in case you want to add something like Required<Pick<T, Key>>, because the < character is not allowed inside a JSX tag for obvious reasons. This may happen with any TypeScript specific syntax that would not be correct in jsx.

Screenshot 2024-09-20 at 9 28 05 PM

Solution

Without needing to modify the lexer, you can bypass this error simply adding a space between < and the generic you want to use. The JSX rules only apply when there are no spaces between that sign and the tag name, so it treats them as 2 separate entities: Operator and NameOther. Of course, given that this solution leaves you with ✨asymmetrical✨ generics, also add an space before > to please your eyes.

Screenshot 2024-09-20 at 9 30 13 PM

alecthomas pushed a commit that referenced this issue Sep 21, 2024
#425 shows that generics are
not correctly identified in general, but they are rather being treated
as JSX elements. I proposed a simple solution in the comments by adding
a space between `<` and the word next to it, but I believe most people
will either not find the solution or some of them will find it rather
unappealing.

For this reason, I made the JSX rules recursive and added a `","`
`Punctuation` token inside so that there can be a number of generics
used, as well as allowing nested generics. While I am not really fond of
this hack, given that generics are already treated as JSX elements, I
think this is a fair and easy enough solution for most cases.

#### Before
<img width="359" alt="Screenshot 2024-09-20 at 9 28 05 PM"
src="https://github.com/user-attachments/assets/b03c2c8a-3278-438b-8803-00eb62cc4a17">

#### With spacing solution
<img width="392" alt="Screenshot 2024-09-20 at 9 30 13 PM"
src="https://github.com/user-attachments/assets/89289476-c92a-41df-b893-5ab289fa96aa">

#### With recursive JSX and `","` `Punctuation` token
<img width="362" alt="Screenshot 2024-09-20 at 9 55 11 PM"
src="https://github.com/user-attachments/assets/d77d892e-667d-4fb4-93cf-8227d5bd4b17">
@alecthomas
Copy link
Owner

alecthomas commented Sep 21, 2024

Fixed in #1002 thanks to @fredrare !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted lexer bug Highlighting error in a lexer
Projects
None yet
Development

No branches or pull requests

3 participants