Skip to content

Commit e2a1010

Browse files
no-custom-classname plugin support (#132)
* Use `generateRules` for `no-custom-classname` * chore: updating `groups.js` * tests: add tests for listed issues * Speeding up the rule using @mpsijm ’s code * fix: support for v3.1.0 * Drop support for `officialSorting` & `prependCustom` (`classnames-order`) * Include fix from #144 (by @mpsijm) * chore: removing the `groups` option * chore: remove unused code
1 parent 74975d5 commit e2a1010

25 files changed

+4408
-1213
lines changed

README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ If you enjoy my work you can:
3737

3838
## Latest changelog
3939

40+
- ADD: support for [new features from Tailwind CSS v3.1.0](https://github.com/tailwindlabs/tailwindcss/releases/tag/v3.1.0)
41+
> Custom `dark` class, `.grid-flow-dense`, `.text-start`, `.text-end`, `.mix-blend-plus-lighter`, `.border-spacing...`
42+
- **BREAKING CHANGE:** `groups`, `groupByResponsive`, `officialSorting` and `prependCustom` are deprecated ☠️.
43+
The official sorting from `prettier-plugin-tailwindcss` is always used by `classnames-order`.
44+
> This was required in order to support classnames generated by plugins.
45+
- FIX: [Many fixes](https://github.com/francoismassart/eslint-plugin-tailwindcss/pull/132) including support for classnames generated by plugins.
4046
- FIX: [speeds up `enforces-shorthand` and `classnames-order`](https://github.com/francoismassart/eslint-plugin-tailwindcss/issues/136) with `officialSorting: true` by introducing WeakMap caches to reduce duplicate calculations (by [mpsijm](https://github.com/mpsijm) 🙏)
4147
- New strategy for whitespaces and linebreaks: the plugin will attempt to leave them intact
4248
- New option `officialSorting` for [`classnames-order`](docs/rules/classnames-order.md#officialsorting-default-false) can be set to `true` in order to use the same ordering order as the official [`prettier-plugin-tailwindcss`](https://www.npmjs.com/package/prettier-plugin-tailwindcss)
@@ -152,21 +158,23 @@ All these settings have nice default values that are explained in each rules' do
152158

153159
```json5
154160
{
155-
"settings": {
156-
"tailwindcss": {
161+
settings: {
162+
tailwindcss: {
157163
// These are the default values but feel free to customize
158-
"callees": ["classnames", "clsx", "ctl"],
159-
"config": "tailwind.config.js",
160-
"cssFiles": ["**/*.css", "!**/node_modules", "!**/.*", "!**/dist", "!**/build"],
161-
"cssFilesRefreshRate": 5_000,
162-
"groupByResponsive": true,
163-
"groups": defaultGroups, // imported from groups.js
164-
"officialSorting": false,
165-
"prependCustom": false,
166-
"removeDuplicates": true,
167-
"whitelist": []
168-
}
169-
}
164+
callees: ["classnames", "clsx", "ctl"],
165+
config: "tailwind.config.js",
166+
cssFiles: [
167+
"**/*.css",
168+
"!**/node_modules",
169+
"!**/.*",
170+
"!**/dist",
171+
"!**/build",
172+
],
173+
cssFilesRefreshRate: 5_000,
174+
removeDuplicates: true,
175+
whitelist: [],
176+
},
177+
},
170178
}
171179
```
172180

docs/rules/classnames-order.md

Lines changed: 4 additions & 279 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
# Use a consistent orders for the Tailwind CSS classnames, based on property then on variants (tailwindcss/classnames-order)
1+
# Use a consistent orders for the Tailwind CSS classnames, based on the official order (tailwindcss/classnames-order)
22

33
Enforces a consistent order of the Tailwind CSS classnames and its variants.
44

5-
> **Note**: By default, it uses the same order as the official [Tailwind CSS documentation](https://tailwindcss.com/docs/container) **v2.1.1**
5+
> **Note**: Since version `3.6.0`, the ordering is solely done using the [order process from the official `prettier-plugin-tailwindcss`](https://tailwindcss.com/blog/automatic-class-sorting-with-prettier#how-classes-are-sorted)
66
77
## Rule Details
88

99
Examples of **incorrect** code for this rule:
1010

1111
```html
12-
<div class="sm:w-6 custom relative w-12"></div>
12+
<div class="p-3 border-gray-300 m-4 h-24 lg:p-4 flex border-2 lg:m-4"></div>
1313
```
1414

1515
Examples of **correct** code for this rule:
1616

1717
```html
18-
<div class="relative w-12 sm:w-6 custom"></div>
18+
<div class="m-4 flex h-24 border-2 border-gray-300 p-3 lg:m-4 lg:p-4"></div>
1919
```
2020

2121
### Options
@@ -25,10 +25,6 @@ Examples of **correct** code for this rule:
2525
"tailwindcss/classnames-order": [<enabled>, {
2626
"callees": Array<string>,
2727
"config": <string>|<object>,
28-
"groupByResponsive": <boolean>,
29-
"groups": Array<object>,
30-
"officialSorting": <boolean>,
31-
"prependCustom": <boolean>,
3228
"removeDuplicates": <boolean>,
3329
"tags": Array<string>,
3430
}]
@@ -55,281 +51,10 @@ It is also possible to directly inject a configuration as plain `object` like `{
5551

5652
Finally, the plugin will [merge the provided configuration](https://tailwindcss.com/docs/configuration#referencing-in-java-script) with [Tailwind CSS's default configuration](https://github.com/tailwindlabs/tailwindcss/blob/master/stubs/defaultConfig.stub.js).
5753

58-
### `groupByResponsive` (default: `true`)
59-
60-
When this option was introduced in version 2.x.x of the plugin, this setting was set to `false` to avoid a tsunami of reorder in the classnames.
61-
You had to set it to `true` intentionally.
62-
63-
Since version 3 of the plugin, the default value is now `true`, grouping by responsive modifier in priority vs. grouping by property.
64-
65-
Linting this code:
66-
67-
`<div class="rounded sm:rounded-lg lg:rounded-2xl p-4 sm:p-6 lg:p-8">...</div>`
68-
69-
By default, the ordering process will group the classnames by variants then by properties:
70-
71-
`<div class="p-4 rounded sm:p-6 sm:rounded-lg lg:p-8 lg:rounded-2xl">...</div>`
72-
73-
Set `groupByResponsive` to `false` and the ordering will work by properties, then by variants:
74-
75-
`<div class="p-4 sm:p-6 lg:p-8 rounded sm:rounded-lg lg:rounded-2xl">...</div>`
76-
77-
### `groups` (default defined in [groups.js](../../lib/config/groups.js))
78-
79-
If you really need to, you can write your own configuration.
80-
81-
`groups` is a fairly long configuration array which defines the hierarchical orders to apply to your classnames prior to variants sorting.
82-
83-
I would recommend you to duplicate the `groups.js` file, move it in your own config location and move items as you wish. At its deepest, it is a list of patterns that will be used inside a regular expression.
84-
85-
```js
86-
// custom-groups.js
87-
module.exports.groups = [
88-
{
89-
type: 'Layout',
90-
members: [
91-
{
92-
type: 'Floats',
93-
members: 'float\\-(right|left|none)',
94-
},
95-
{
96-
type: 'Box Sizing',
97-
members: 'box\\-(border|content)',
98-
},
99-
...
100-
],
101-
},
102-
...
103-
];
104-
```
105-
106-
Import your own file then set it in your rules config.
107-
108-
```js
109-
// .eslintrc.js
110-
...
111-
const customGroups = require('custom-groups').groups;
112-
...
113-
"tailwindcss/classnames-order": [2, {
114-
"groups": customGroups
115-
}]
116-
...
117-
```
118-
119-
### `officialSorting` (default: `false`)
120-
121-
Set `officialSorting` to `true` if you want to use the same ordering rules as the official plugin `prettier-plugin-tailwindcss`. Enabling this settings will cause `groupByResponsive`, `groups`, `prependCustom` and `removeDuplicates` options to be ignored.
122-
123-
### `prependCustom` (default: `false`)
124-
125-
By default, classnames which doesn't belong to Tailwind CSS will be pushed at the end. Set `prependCustom` to `true` if you prefer to move them at the beginning.
126-
12754
### `removeDuplicates` (default: `true`)
12855

12956
Duplicate classnames are automatically removed but you can always disable this behavior by setting `removeDuplicates` to `false`.
13057

13158
### `tags` (default: `[]`)
13259

13360
Optional, if you are using tagged templates, you should provide the tags in this array.
134-
135-
## Further Reading
136-
137-
### How it works
138-
139-
1. Groups classnames by property
140-
2. Sorts each group based on configuration
141-
3. Within each group, it sorts the variants:
142-
1. Responsive (e.g. `sm:` ... `2xl:`, the order is based on the breakpoints defined in `theme.screens`)
143-
2. Theme
144-
1. (empty)
145-
2. `dark:`
146-
3. State (e.g. `hover:`... the order is based on `variantOrder`)
147-
148-
### Default groups
149-
150-
The [default `groups` configuration](../../lib/config/groups.js) will apply the following order, as in the official [Tailwind CSS documentation](https://tailwindcss.com/docs/container).
151-
152-
Each line represents a group (based on property).
153-
154-
```
155-
Core Concepts
156-
Hover, Focus, & Other States
157-
Dark Mode
158-
Arbitrary properties
159-
Layout
160-
Aspect Ratio
161-
Container
162-
Columns
163-
Break After
164-
Break Before
165-
Break Inside
166-
Box Decoration Break
167-
Box Sizing
168-
Display
169-
Floats
170-
Clear
171-
Isolation
172-
Object Fit
173-
Object Position
174-
Overflow
175-
Overscroll Behavior
176-
Position
177-
Top / Right / Bottom / Left
178-
Visibility
179-
Z-Index
180-
Flexbox & Grid
181-
Flex Basis
182-
Flex Direction
183-
Flex Wrap
184-
Flex
185-
Flex Grow
186-
Flex Shrink
187-
Order
188-
Grid Template Columns
189-
Grid Column Start / End
190-
Grid Template Rows
191-
Grid Row Start / End
192-
Grid Auto Flow
193-
Grid Auto Columns
194-
Grid Auto Rows
195-
Gap
196-
Justify Content
197-
Justify Items
198-
Justify Self
199-
Align Content
200-
Align Items
201-
Align Self
202-
Place Content
203-
Place Items
204-
Place Self
205-
Spacing
206-
Padding
207-
Margin
208-
Space Between
209-
Sizing
210-
Width
211-
Min-Width
212-
Max-Width
213-
Height
214-
Min-Height
215-
Max-Height
216-
Typography
217-
Font Family
218-
Font Size
219-
Font Smoothing
220-
Font Style
221-
Font Weight
222-
Font Variant Numeric
223-
Letter Spacing
224-
Line Height
225-
List Style Type
226-
List Style Position
227-
Text Alignment
228-
Text Color
229-
Text Decoration
230-
Text Decoration Color
231-
Text Decoration Style
232-
Text Decoration Thickness
233-
Text Underline Offset
234-
Text Transform
235-
Text Overflow
236-
Text Indent
237-
Vertical Alignment
238-
Whitespace
239-
Word Break
240-
Content
241-
Backgrounds
242-
Background Attachment
243-
Background Clip
244-
Background Color
245-
Background Origin
246-
Background Position
247-
Background Repeat
248-
Background Size
249-
Background Image
250-
Gradient Color Stops
251-
Borders
252-
Border Radius
253-
Border Width
254-
Border Color
255-
Border Style
256-
Divide Width
257-
Divide Color
258-
Divide Style
259-
Outline Width
260-
Outline Color
261-
Outline Style
262-
Outline Offset
263-
Ring Width
264-
Ring Inset
265-
Ring Color
266-
Ring Offset Width
267-
Ring Offset Color
268-
Effects
269-
Box Shadow
270-
Box Shadow Color
271-
Opacity
272-
Mix Blend Mode
273-
Background Blend Mode
274-
Filters
275-
Blur
276-
Brightness
277-
Contrast
278-
Drop Shadow
279-
Grayscale
280-
Hue Rotate
281-
Invert
282-
Saturate
283-
Sepia
284-
Backdrop Blur
285-
Backdrop Brightness
286-
Backdrop Contrast
287-
Backdrop Grayscale
288-
Backdrop Hue Rotate
289-
Backdrop Invert
290-
Backdrop Opacity
291-
Backdrop Saturate
292-
Backdrop Sepia
293-
Tables
294-
Border Collapse
295-
Table Layout
296-
Transitions & Animation
297-
Transition Property
298-
Transition Duration
299-
Transition Timing Function
300-
Transition Delay
301-
Animation
302-
Transforms
303-
Transform GPU
304-
Scale
305-
Rotate
306-
Translate
307-
Skew
308-
Transform Origin
309-
Interactivity
310-
Accent Color
311-
Appearance
312-
Cursor
313-
Caret Color
314-
Pointer Events
315-
Resize
316-
Scroll Behavior
317-
Scroll Margin
318-
Scroll Padding
319-
Scroll Snap Align
320-
Scroll Snap Stop
321-
Scroll Snap Type
322-
Touch Action
323-
User Select
324-
Will Change
325-
SVG
326-
Fill
327-
Stroke
328-
Stroke Width
329-
Accessibility
330-
Screen Readers
331-
Official Plugins
332-
Typography
333-
Aspect Ratio
334-
Line Clamp
335-
```

0 commit comments

Comments
 (0)