vscode-styled-components icon indicating copy to clipboard operation
vscode-styled-components copied to clipboard

Syntax highlighting broken with typescript type literals and with simple elements like styled.div

Open maartenth opened this issue 5 years ago • 33 comments

screen shot 2019-03-04 at 10 37 53

#26 only added partial typescript support:

const StyledSection = styled.div<{ backgroundColor?: string }>` // no highlighting
const StyledSection = styled.div<Props>` // no highlighting
const StyledSection = styled<{ backgroundColor?: string }>('div')` // no highlighting
const StyledSection = styled<Props>('div')` // works

maartenth avatar Mar 04 '19 09:03 maartenth

I'm seeing the same. To clarify this happens when using the generic version of the an element function. Ie. div<Props>().

chrisui avatar Mar 04 '19 21:03 chrisui

Confirming that highlighting is broken when passing a type argument to the styled function. This behavior is not specific to type literals; passing any kind of type notation will break highlighting:

capture2 capture3

However, one way I got it to work, is to directly notate the Props type when consumed, at least once:

capture8 (works with destructuring as well) capture6

This allows the compiler to infer the type without needing to explicitly pass a type argument as we did the first time, but it probably reduces readability.

Things get messy when we want to use intersection types.

Using two different Props types in the same styled component is obviously not possible: capture9

so we still need to fall back to passing a type argument that is itself an intersection of the desired interfaces: capture10 or use a type alias to combine them, and then notate Props at least once using that alias to get back syntax highlighting: capture11

libekonst avatar Mar 05 '19 20:03 libekonst

Same for me, syntax highlight stopped properly working then typing my styled components.

Before

screenshot 2019-03-06 at 21 56 15

After

screenshot 2019-03-06 at 21 56 36

kevinwolfcr avatar Mar 07 '19 03:03 kevinwolfcr

I'm experiencing the same issue as stated above - syntax highlighting and css code completion is gone when using types.

Also as mentioned above, assigning a type directly to any of the props objects used within the styled component will fix type errors and keep syntax highlighting. However in this example, only textMargin would autocomplete after typing p., and it doesn't seem ideal to have to repeat the ${(p: Types) => p.prop} boilerplate for each rule that uses props.

screen shot 2019-03-07 at 4 17 35 pm

tomellwood avatar Mar 07 '19 16:03 tomellwood

to all: its finally fixed with 0.0.26 released today 🎉

mrGibi avatar Mar 26 '19 19:03 mrGibi

I still have a problem with this when using the styled function even after 0.0.26 because 102f0d87d734f16bb8ec090bcbdfcd281150f817 doesnt take spaces into account

VSCode formatter automatically puts a space after the function, this breaks the highlight

image

vviotto-masti avatar Apr 04 '19 15:04 vviotto-masti

Not sure if this is an error on my end but I still don't get syntax highlighting.

Screenshot 2019-06-07 17 16 58

GU5TAF avatar Jun 07 '19 15:06 GU5TAF

@GU5TAF It doesn't work with inline types like that, so create a type alias:

// Doesn't work
const MyComponent = styled.div<{ color: string }>``

// Works
type Props = { color: string };
const MyComponent = styled.div<Props>``;

It's just using a regex to detect things, so it can't really detect everything properly.

kimroen avatar Jun 07 '19 15:06 kimroen

@GU5TAF It doesn't work with inline types like that, so create a type alias. It's just using a regex to detect things, so it can't really detect everything properly.

Okay, thanks for the reply! 🙏

GU5TAF avatar Jun 07 '19 15:06 GU5TAF

It seems this does work with styled, but not with createGlobalStyle<>.

vitorrd avatar Aug 26 '19 06:08 vitorrd

It seems this does work with styled, but not with createGlobalStyle<>.

Same for css<..>

willem-aart avatar Jan 13 '20 13:01 willem-aart

When the generic type is added also to the attrs the highlighting is still broken:

image

And if you declare the type directly in the props of the attrs call, you would lose access to the other properties, in this case style.

styled.div.attrs(({ size }: ContainerProps) => ({
  size: `${size}px`,
  style: { // <-- 'style' does not exist in type 'ContainerProps'
    height: `${size}px`,
  },
...

jodapomo avatar Jul 26 '20 04:07 jodapomo

I guess this one should be re-opened. Still issues with css<> and createGlobalStyle<>

basslagter avatar Jul 30 '20 10:07 basslagter

Please reopen the issue. I have the same problem with css

pixelkritzel avatar Mar 08 '21 14:03 pixelkritzel

We are looking for contributors, please see https://github.com/styled-components/vscode-styled-components/issues/275

jasonwilliams avatar Mar 08 '21 14:03 jasonwilliams

@jasonwilliams I tried it but I didn't make it. The RegEx was stronger than me.

But my issues seems to be connected to a line break. If there isn't any line break it works as shown in the attached screenshots.

Maybe this will help somebody else.

notworking working

pixelkritzel avatar Mar 08 '21 16:03 pixelkritzel

Thanks @pixelkritzel that was useful

jasonwilliams avatar Mar 08 '21 17:03 jasonwilliams

Hey everyone, so I got the regex to pick up on multi-line inline types (https://regex101.com/r/XxGtKO/1/) but I'm a bit lost on how to proceed. Escaping and switching out the regex in syntaxes/styled-components.json, then building with yarn vscode:prepublishdoesn't really do anything, at least when I install the extension locally. Am I missing something?

johh avatar Mar 13 '21 19:03 johh

Hey everyone, so I got the regex to pick up on multi-line inline types (https://regex101.com/r/XxGtKO/1/) but I'm a bit lost on how to proceed. Escaping and switching out the regex in syntaxes/styled-components.json, then building with yarn vscode:prepublishdoesn't really do anything, at least when I install the extension locally. Am I missing something?

@jasonwilliams, @finnp, do you know how to proceed? 🙂

johh avatar Mar 19 '21 15:03 johh

Hey everyone, so I got the regex to pick up on multi-line inline types (https://regex101.com/r/XxGtKO/1/) but I'm a bit lost on how to proceed. Escaping and switching out the regex in syntaxes/styled-components.json, then building with yarn vscode:prepublishdoesn't really do anything, at least when I install the extension locally. Am I missing something?

@jasonwilliams, @finnp, do you know how to proceed? 🙂

Hmm how are you testing it? It should work. If you have this project checked out locally you should be able to hit “launch extension” in debug view on VSCode and test it on a file. Better yet you can create a test like the others and use that as a test.

Run yarn watch then load the extension, the debug launch option should call “watch” automatically

jasonwilliams avatar Mar 19 '21 20:03 jasonwilliams

Thanks for your reply and for clearing things up! Launching it as you said, I still get no highlighting or autocomplete. Screenshot 2021-03-20 at 22 38 34 I'm not too familiar with vscode extension development, how would I go about debugging this?

johh avatar Mar 20 '21 21:03 johh

@jasonwilliams I tried it but I didn't make it. The RegEx was stronger than me.

But my issues seems to be connected to a line break. If there isn't any line break it works as shown in the attached screenshots.

Maybe this will help somebody else.

notworking working

This has been fixed in v1.5.1 please let me know if it isn't

jasonwilliams avatar Apr 24 '21 00:04 jasonwilliams

Hi, there is still an issue when using styled options

image

I'm using the version 1.5.2

Justkant avatar May 04 '21 09:05 Justkant

@Justkant any help on that one would be appreciated

jasonwilliams avatar May 14 '21 22:05 jasonwilliams

@jasonwilliams sure, how can I help ?

I'm using emotion and I just saw that styled-components and emotion differ on the way you specify the shouldForwardProp option. (styled vs emotion)

I also had a look at the multiline fix (https://github.com/styled-components/vscode-styled-components/pull/281) and I haven't had the time to test some changes to the regex

In the meantime, here is a fixture for my issue:

interface StyledBrokenProps {
    multipleLines: boolean;
}

const StyledBroken = styled('h1', {
  shouldForwardProp: prop => prop !== 'multipleLines'
})<StyledBrokenProps>`
    background: red;
`;

Justkant avatar May 15 '21 09:05 Justkant

@Justkant you should be able to clone this repo, hit “lunch extension” in the debug tab. That will load a second VSCode instance with the fixtures folder loaded.

Go into a typescript fixture (doesn’t matter which one) and replace it with your fixture.

now take one of the regexes (it’s this one https://github.com/styled-components/vscode-styled-components/blob/master/syntaxes/styled-components.json#L159) and tweak it. You can do that by throwing it into regex101 (Unescape it first) and put your fixture in Regex 101 aswell. Then you can tweak there.

once you’re happy load it back into the json file (escaped) and hit refresh on the debug bar.

https://github.com/styled-components/vscode-styled-components/blob/master/CONTRIBUTING.md Goes into more detail

jasonwilliams avatar May 15 '21 10:05 jasonwilliams

@Justkant if you're stuck at any point, let me know.

jasonwilliams avatar May 18 '21 18:05 jasonwilliams

Hi, sorry I hadn't much time to try and fix this issue lately.

I did try today and found a tweak on Regex 101 that helped but couldn't make it work when debugging the extension in vscode. I also found some inconsistencies when debugging with the Scope Inspector

I'll add more infos later when I have some time

Justkant avatar Jul 16 '21 12:07 Justkant

Feel free to share the tweak you did on Regex 101 I can probably take a look

jasonwilliams avatar Jul 16 '21 12:07 jasonwilliams

@jasonwilliams I tried it but I didn't make it. The RegEx was stronger than me. But my issues seems to be connected to a line break. If there isn't any line break it works as shown in the attached screenshots. Maybe this will help somebody else. notworking working

This has been fixed in v1.5.1 please let me know if it isn't

Just saw your comment. Yes, it works for this usecase with styled-components. Thank you!

pixelkritzel avatar Jul 21 '21 22:07 pixelkritzel