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

Split JSX into separate grammar? #244

Closed
HiPhish opened this issue Mar 19, 2023 · 7 comments
Closed

Split JSX into separate grammar? #244

HiPhish opened this issue Mar 19, 2023 · 7 comments

Comments

@HiPhish
Copy link

HiPhish commented Mar 19, 2023

As a user I want to be able to have separate settings for Javascript and JSX in my configuration. Currently support for JSX files (React.js extension for Javascript) is included in the Javascript grammar. However, Javascript and JSX are different languages, and I want to have separate per-filetype settings in my text editor.

I have recently added support for JSX in nvim-ts-rainbow2 where I have two sets of queries for JSX files: one matches the angle brackets of tags, other other matches entire tags. This means a lot of copy-pasting between both queries.

If the two file types were separate I could make the JSX query inherit the Javascript query and add only the queries for the JSX extension. Example in TSX, note the first line which Neovim understands:


(jsx_element
  open_tag: (jsx_opening_element) @opening
  close_tag: (jsx_closing_element) @closing) @container

This is only possible because the https://github.com/tree-sitter/
tree-sitter-typescript repo splits the two languages this way as well. The result is a very terse query. If I want to change how I highlight tags I only need to write a query for that part and can piggy-back off the Typescript query for everything else.

@amaanq
Copy link
Member

amaanq commented Aug 11, 2023

PR Welcome - though I do find having it as one language simpler, but I can see the benefits of splitting it.

@calebdw
Copy link

calebdw commented Jan 24, 2024

PR submitted 😄

@maxbrunsfeld
Copy link
Contributor

Hi, I don't think we want a separate grammar for JSX, because in most codebases, you can't tell whether a file is going to use JSX statically. They often just use the .js extension.

@calebdw
Copy link

calebdw commented Jan 24, 2024

@maxbrunsfeld

How is splitting javascript/jsx any different than splitting typescript/tsx? Since the ts parser is split it makes sense to split this one, otherwise the same argument for keeping this parser combined could apply to the ts parser, could it not?

They often just use the .js extension.

That may be, however, I would argue that jsx is not javascript and therefore should not be included in .js files. Perhaps splitting the parser would provide the motivation to separate those concerns?

@HiPhish
Copy link
Author

HiPhish commented Jan 24, 2024

They often just use the .js extension.

Do they really? I have only worked with React and Typescript, and the file extension was always .tsx. Wouldn't React and Javascript projects use .jsx?

@maxbrunsfeld
Copy link
Contributor

How is splitting javascript/jsx any different than splitting typescript/tsx? Since the ts parser is split it makes sense to split this one, otherwise the same argument for keeping this parser combined could apply to the ts parser, could it not?

TypeScript and TSX must use different file extensions, because the two languages are mutually incompatible. So the TypeScript compiler needs to know up front which is which.

In practice, JavaScript is not that way. People just use the .js extension for files that contain JSX. As the maintainers of this repo, we don't have any control over this. With the current system, the JavaScript grammar can be applied to JS files. If we were to split the grammars, it would never be clear when we should use the plain JS grammar. So that would be a strictly less useful state of affairs for consumers of this library.

@calebdw
Copy link

calebdw commented Jan 26, 2024

Should this issue be closed then?

@amaanq amaanq closed this as not planned Won't fix, can't repro, duplicate, stale Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
@maxbrunsfeld @calebdw @HiPhish @amaanq and others