Comments (18)
Hi, @sheerun @sQVe @davidroeca @tsuyoshicho This issue should have been fixed in #99. Update this plugin and take a look, Thanks.
from vim-jsx-pretty.
Created a simple repo that reproduces this issue both for flow
and typescript
https://github.com/davidroeca/vim-jsx-pretty-issue-86-repro
from vim-jsx-pretty.
Then it's at least good clue :)
from vim-jsx-pretty.
@sQVe If that's issue with yats.vim maybe you'll open an issue there?
from vim-jsx-pretty.
@davidroeca Thanks for your investigation. This regex is indeed complex because we have to use it to detect the jsx tag start boundaries and the boundaries vary greatly.
Currently, it cannot be simplified because this plugin has no assumption on which plugin (vim-javascript
or typescript-vim
) is used.
I will try to fix this issue in a few days.
from vim-jsx-pretty.
Thanks for your feedback, will take a look in my spare time.
from vim-jsx-pretty.
@sheerun I can reproduce this issue without vim-jsx-pretty
. So this should be an issue of yats.vim
.
from vim-jsx-pretty.
@sheerun Closing this issue since it's not an issue of this plugin.
from vim-jsx-pretty.
@yuezk, @sheerun: The indenting is an issue with yats.vim
. The issue (sheerun/vim-polyglot#428) referred to highlighting. As soon as I add vim-jsx-pretty
the following breaks typescript (both yats.vim
and typescript-vim
) highlighting for all code below it:
const head = <T>(arr: T[]): T => arr[0]
I suggest reopening and renaming this issue or alternatively creating a new issue that tracks the problematic highlighting.
from vim-jsx-pretty.
@sQVe OK, I see. I'll reopen this issue.
from vim-jsx-pretty.
I think the highlighting of generic functions is an issue here around how a JSX
group is defined. I've encountered the same issue in https://github.com/leafgarland/typescript-vim and also in https://github.com/pangloss/vim-javascript with flow.
from vim-jsx-pretty.
My best guess at a fix would be an edit in the following lines:
vim-jsx-pretty/after/syntax/jsx_pretty.vim
Lines 38 to 41 in c3dc920
But it's tough for me to be sure; the regex is giving me a bit of a headache š
from vim-jsx-pretty.
I think this issue is hard to fix and I'm trying to explain it.
Vim syntax highlighting has its limitation because it uses the regex to match the syntax and it's a static match, for most of the cases, this mechanism works well since most of the language syntax is unambiguous.
But the test case in this issue is ambiguous.
const head = <T>(arr: T[]): T => arr[0]
<T>
can be the start element of a jsx element, it can also be the generic type definition, it's based on the syntax after it. But when regex syntax matches, it can not parse the syntax after it, so it treats the <T>
as the start element of the jsx element.
A possible solution is that we make an assumption that if <T>
is followed by a (
, we treat it as the generic type. But this may break the highlight of const head = <T>(...)</T>
, which is a valid jsx element.
The workaround is to use the function expression:
const head = function <T>(arr: T[]): T { return arr[0]; }
or use the trailing comma after the type
const head = <T,>(arr: T[]): T => arr[0]
Reference: microsoft/TypeScript#15713
from vim-jsx-pretty.
@yuezk I understand. Thank you for diving deep into this issue š
I mostly write JavaScript / TypeScript in a functional manner so the likelyhood of me writing functions like const head = <T>(arr: T[]): T => arr[0]
is quite a bit larger than using a <T>(...)</T>
JSX block. The JSX counterpart is also easier to repair with newlines etc.
It would be interesting to know how common the <T>(...)</T>
case really is. I'm unsure on what that specific use case solves.
I vote to fixing the generic type case even though it might introduce a very specific issue with JSX.
from vim-jsx-pretty.
@sQVe You can update this plugin and use the workaround by adding a trailing comma after T
.
const head = <T,>(arr: T[]): T => arr[0]
from vim-jsx-pretty.
I also agree that const head = <T>(arr: T[]): T => arr[0]
is probably more popular than <T>(...)</T>
, so probably checking if <T>
is followed by a (
is fair indicator (or checking if </T>
is present in the same line).
Also good indicator is that it's capital case e.g. in my codebase there's <span>({photos.length})</span>
but it cannot be generic type because it's lower case.
from vim-jsx-pretty.
Also good indicator is that it's capital case e.g. in my codebase there's
<span>({photos.length})</span>
but it cannot be generic type because it's lower case.
Using a capital letter for a generic type is considered good practice but it is not required. The following snippet is valid TypeScript:
const head = <t>(arr: t[]): t => arr[0]
// or even
const head = <span>(arr: span[]): span => arr[0]
from vim-jsx-pretty.
@yuezk thanks for taking the time here!
Iām good with either approach as long as what correctly highlights is compatible with what linters and prettier expect. I know prettier removes unnecessary parens. Iād assume most standard linter configs would complain about lower-case type variables.
from vim-jsx-pretty.
Related Issues (20)
- [Question] Using Emmet with vim-jsx-pretty HOT 2
- typescript: misaligned indentation when using function shorthand with generic HOT 4
- typescript: misaligned indentation for `=` in template literal HOT 4
- Not matching JSX with dot separator HOT 1
- Great documentation HOT 1
- Dealing with tags interspersed in blocks of text
- Typescript indent failing for multiline properties
- jsxTagName should be same as htmlTagName
- Different formatting than expected HOT 1
- Function declaration's closing bracket highlighted as error HOT 3
- Highlight type arguments after jsxComponentName
- Highlighting issue with colorscheme HOT 2
- Closing tag doesn't get colored - Gruvbox HOT 2
- [Feature] Highlight when call function HOT 2
- highlighting fails on closing parens HOT 3
- Matchit support has problems with unclosed tags
- Weird highlight using vim-polygot HOT 1
- Broken .tsx highlighting for arrow functions using generics
- Problem syntax HOT 2
- Is there a reason why Line Comments are being redefined?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vim-jsx-pretty.