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

Broken .tsx highlighting for arrow functions using generics #154

Open
noahbrenner opened this issue Nov 3, 2021 · 1 comment
Open

Broken .tsx highlighting for arrow functions using generics #154

noahbrenner opened this issue Nov 3, 2021 · 1 comment

Comments

@noahbrenner
Copy link

I'm running into inconsistencies in highlighting between ft=typescript and ft=typescriptreact.

I'm using yats.vim, vim-javascript, and of course vim-jsx-pretty, all just updated using vim-plug.

Some example code:

const doFoo = <K extends keyof Foo>(key: K, value: Foo[K], foos: Foo[]) => {
  foos[0][key] = value;
};

const doBar = (bar: string) => {
  return bar;
};
  • When filetype is typescript:
    image

  • When filetype is typescriptreact:
    image

    • Highlighting is broken for the generic parameter, which looks like it's treated as a JSX element.
    • Highlighting is broken for the function parameters, function body, and for the next function (it's broken for all following functions, I just included 1 in the example).
  • Surprisingly (to me 😄), the behavior is affected by the name of the generic variable. If I use T instead of K, I get this:
    image

    • Highlighting for the generic parameter is still broken, but in a different way than above. Now it's all the default text color.
    • Highlighting is correct for the function parameters, function body, and the following function(s).

For simple generics, I can get away with just using T for now, but that's not always the best type variable to use, doesn't cover functions that need more than one generic parameter, and is still missing highlighting for the type parameters themselves.

@wchargin
Copy link

I'm also running into this.

Here's a hacky one-line patch to broaden it to at least accept [A-Z] instead of just T:

diff --git a/after/syntax/jsx_pretty.vim b/after/syntax/jsx_pretty.vim
index 74b200c..71e97d3 100644
--- a/after/syntax/jsx_pretty.vim
+++ b/after/syntax/jsx_pretty.vim
@@ -3,5 +3,5 @@ let s:highlight_close_tag = get(g:, 'vim_jsx_pretty_highlight_close_tag', 0)
 " detect jsx region
 syntax region jsxRegion
-      \ start=+\%(\%(\_[([,?:=+\-*/>{}]\|<\s\+\|&&\|||\|=>\|\<return\|\<default\|\<await\|\<yield\)\_s*\)\@<=<\_s*\%(>\|\z(\%(script\|\s*\<T\>\)\@!\<[_$A-Za-z][-:._$A-Za-z0-9]*\>\)\%(\_s*\%([-+*)\]}&|?,]\|/\%([/*]\|\_s*>\)\@!\)\)\@!\)+
+      \ start=+\%(\%(\_[([,?:=+\-*/>{}]\|<\s\+\|&&\|||\|=>\|\<return\|\<default\|\<await\|\<yield\)\_s*\)\@<=<\_s*\%(>\|\z(\%(script\|\s*\<[A-Z]\>\)\@!\<[_$A-Za-z][-:._$A-Za-z0-9]*\>\)\%(\_s*\%([-+*)\]}&|?,]\|/\%([/*]\|\_s*>\)\@!\)\)\@!\)+
       \ end=++
       \ contains=jsxElement

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

No branches or pull requests

2 participants