Sorting classes inside variables
See original GitHub issueIs there any way to make the plugin sort classes inside variables? Consider the following code:
const ClassVariable: React.FC = (props) => {
const klass = "flex flex-col min-h-0";
return <div className={klass} />;
};
const ClassName: React.FC = (props) => {
return <div className="flex flex-col min-h-0" />;
};
After formatting, this is the result:
const ClassVariable: React.FC = (props) => {
- const klass = "flex flex-col min-h-0"; // not changed
return <div className={klass} />;
};
const ClassName: React.FC = (props) => {
+ return <div className="flex min-h-0 flex-col" />; // changed
};
I can imagine trying to sort all string variables by default would not be feasible, but some kind of workaround would be nice. klass
is a pretty common variable name for CSS classes from what I’ve seen, so trying to sort strings assigned to klass
(as well as strings inside an array assigned to klass
, like klass = ["some class strings", bool ? "this" : "that"]
) could be one possibility. I imagine this would be one thing that could use a config option if provided though, like a variable name string/regex that is used for matching what variables to try to sort.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:11
- Comments:5 (1 by maintainers)
Top Results From Across the Web
[Prettier Plugin] Sorting classes inside variables #7558 - GitHub
In general, the plugin works by identifying specific AST nodes and sorting all strings that appear inside said nodes. So in theory, all...
Read more >Sorting an array list of custom java classes based on variable
I have a custom Java class which contains two variables: username and score . For example, below shows the ArrayList containing multiple ...
Read more >Sorting objects of user defined class in Python - GeeksforGeeks
The following article discusses how objects of a user-defined class can be arranged based on any of the variables of the class, which...
Read more >Using the BY Statement with Class Variables - SAS Help Center
Sort the GRADE data set. PROC SORT sorts the observations by the variable Section. Sorting is required in order to use Section as...
Read more >20.9. Sorting Lists of Instances - Runestone Academy
There is a way to define a default sort order for instances, right in the class definition, but it requires defining a bunch...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@gustavopch The plugin actually already sorts the strings in your example if you put it inside a
<div className={/* complex array/ternary/whatever goes here */}>
. In general, the plugin works by identifying specific AST nodes and sorting all strings that appear inside said nodes. So in theory, all we should need is some way to signal to the plugin when a variable is a class name variable and it should be able to handle things from there.Leaving a more complex use case here with ternaries and maps to be considered too:
I haven’t put a lot of thought into it, but I’m not sure if the
tailwindCSS.experimental.classRegex
approach would work here (unless perhaps if I added the comment to each line, which would not be ideal IMO).