-
Notifications
You must be signed in to change notification settings - Fork 4
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
Dynamic Selector Resolution #88
Comments
Great ideas! I too think it's best to continue silently ignoring the dynamic string case. Mainly because this is also the behavior of CSS modules at build time (with type checking): non-existing selectors are silently ignored, which is consistent with TS behavior. Dynamic property access using strings silently returns undefined, except maybe for TS template string types or cases where
I think I'd expect the extension to warn about non-existing selectors if the dynamic selector consists of a single identifier, but suggesting identifiers in scope that resolve to a valid selector probably isn't needed and might even be hard to understand. I'm not sure if I will be able to provide a useful PR for this soon, but I really like the idea to validate statically analyzable dynamic selectors instead of just ignoring, given they only consist of a single identifier. Again, this would be somewhat consistent with TS behavior for regular objects, although of course this extension cannot modify behavior of actual build setups using CSS modules. |
Thanks for the valuable insights.
No worries . I will try to find some time for this soon. |
As mentioned in #92, this works pretty well in the For example, here the class Unfortunately it seems like this approach is hard to integrate with the refactoring actions that this extension provides. |
@strlns I guess, this feature won't be possible without interacting with the typescript language server and letting typescript language server take control over it. This could be what the typescript css modules plugin in doing |
Yes I see. I'm not sure if I'm able to code that. If I am, I probably wouldn't do it better than that plugin and/or without breaking features that this extension provides. I just tested what happens if I rename a class from the point of usage in a project with Vite, Might be fundamentally difficult to provide both things at once (TypeScript IntelliSense for CSS modules and editor actions) |
Interesting. To speak against the feature, I believe this feature is a little outside the context of this extension. I wasn't very convinced to introduce typescript language service after requested by @karlhorky in #70 , however it seemed like a nice addon to the extension. However type checking dynamic selectors seems like a problem of different context. I don't do a lot of dynamic selectors in my projects , I feel like its a bit cumbersome to tie selectors with type definitions. They do not give any clear idea to my colleague what the selector does on first glance. That said, I guess we can treat this issue as a no op . WDYT ? |
I think you are right. I'll take the liberty to close this then, hope you don't mind? |
Is your feature request related to a problem? Please describe.
As mentioned in #86 dynamic selectors are not supported by the extension and were considered for future releases but eventually marked as trivial since there were no proper brainstorming or ideas.
dynamic selectors usually are common in UI libraries , Component frameworks etc. It's not a bad feature to atleast brainstorm.
Describe the solution you'd like
First solution mentioned in #86 can be elaborated as follows.
When accessing a dynamic selector one of the following patterns is probable to occur
At least In Typescript/Typescript react
Dynamic selectors in typescript can be accessed through the following type of syntaxes
styles["mx_w"+"_"+width]
-> can be evaluated tostyles["max_w_320"]
for instancestyles[
max_w_${width}]
styles[width]
-> width can be a prop that can hold values in the form of string or a named union.After determining the type of dynamic selector , not all providers can be give useful intellisense.
The possible providers are
Definition Provider.
Definition provider can provide multiple definitions for a given position in the document.
In the case of dynamic selectors each inferred value at the given position can be collected by the definition provider
for instance in the use case mentioned in #86 the
size
prop can hold three values.sm | md | lg
.Now if the classes that correspond to these values are defined in the accessing style module , then three definition references will be provided.
Hover Provider
Hover takes the same approach as definitions , but will combine all the three code blocks into one large hover content.
for instance
It can get messy if the possibilities are long.
Diagnostics
Diagnostics should warn about missing selectors, the same way it does for static selectors except the fact it should consider the inferred types.
If the types are not able to be inferred , then the accessor at the position should be ignored.
Describe alternatives you've considered
Treat dynamic selectors as trivial and not support them.
Additional context
The probable solutions mentioned here in this ticket and in #86 are only feasible if one of the two approaches are realistic to implement
The text was updated successfully, but these errors were encountered: