Skip to content

Commit 0504019

Browse files
fix: check all size, width and height for size-*
1 parent c6e87c6 commit 0504019

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ You can can the same information on your favorite command line software as well.
4343
- fix: [composable touch action classnames](https://github.com/francoismassart/eslint-plugin-tailwindcss/issues/293)
4444
- fix: [`shadow-md` + `shadow-[#color]`can be used together 🤝](https://github.com/francoismassart/eslint-plugin-tailwindcss/issues/298)
4545
- fix: [`tabular-nums` and `slashed-zero` can be used together 🤝](https://github.com/francoismassart/eslint-plugin-tailwindcss/issues/316)
46-
- fix: [`size-*` based `size` 🤓](https://github.com/francoismassart/eslint-plugin-tailwindcss/issues/315)
46+
- fix: [`size-*` based `size`, `spacing`, `width` and `height` 🤓](https://github.com/francoismassart/eslint-plugin-tailwindcss/issues/315)
4747
- fix: [there is no `size-screen` 😅](https://github.com/francoismassart/eslint-plugin-tailwindcss/issues/307)
4848
- fix: [edge cases with whitespace in `enforces-shorthand`](https://github.com/francoismassart/eslint-plugin-tailwindcss/pull/308)(by [kachkaev](https://github.com/kachkaev) 🙏)
4949
- fix: [parsing spreads in function call returns](https://github.com/francoismassart/eslint-plugin-tailwindcss/pull/285)(by [egorpavlikhin](https://github.com/egorpavlikhin) 🙏)

lib/rules/enforces-shorthand.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,11 @@ module.exports = {
248248
const sizeKeys = Object.keys(mergedConfig.theme.size);
249249
const isSize = ['w-', 'h-'].includes(remainingClass.body);
250250
const isValidSize = sizeKeys.includes(remainingClass.value);
251-
const validValue = bodyMatch && !(isSize && !isValidSize);
252-
return validValue;
251+
const wValue = mergedConfig.theme.width[remainingClass.value];
252+
const hValue = mergedConfig.theme.height[remainingClass.value];
253+
const sizeValue = mergedConfig.theme.size[remainingClass.value];
254+
const fullMatch = wValue === hValue && wValue === sizeValue;
255+
return bodyMatch && !(isSize && !isValidSize && !fullMatch);
253256
}
254257
});
255258

tests/lib/rules/enforces-shorthand.js

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const skipClassAttributeOptions = [
3434
},
3535
];
3636

37-
const customWidthHeightOptions = [
37+
const incompleteCustomWidthHeightOptions = [
3838
{
3939
config: {
4040
theme: {
@@ -48,7 +48,7 @@ const customWidthHeightOptions = [
4848
},
4949
];
5050

51-
const customSpacingOptions = [
51+
const customSpacingOnlyOptions = [
5252
{
5353
config: {
5454
theme: {
@@ -61,6 +61,34 @@ const customSpacingOptions = [
6161
},
6262
];
6363

64+
const customSizeOnlyOptions = [
65+
{
66+
config: {
67+
theme: {
68+
extend: {
69+
size: { size: "100px" },
70+
},
71+
},
72+
plugins: [],
73+
},
74+
},
75+
];
76+
77+
const ambiguousOptions = [
78+
{
79+
config: {
80+
theme: {
81+
extend: {
82+
width: { ambiguous: "75px" },
83+
height: { ambiguous: "120px" },
84+
size: { ambiguous: "100px" },
85+
},
86+
},
87+
plugins: [],
88+
},
89+
},
90+
];
91+
6492
var generateError = (classnames, shorthand) => {
6593
return {
6694
messageId: "shorthandCandidateDetected",
@@ -160,8 +188,16 @@ ruleTester.run("shorthands", rule, {
160188
code: "<div className={'w-screen h-screen'}>issue #307</div>",
161189
},
162190
{
163-
code: `<div class="h-custom w-custom">size-* is based on spacing</div>`,
164-
options: customWidthHeightOptions,
191+
code: `<div class="h-custom w-custom">Incomplete config should not use size-*</div>`,
192+
options: incompleteCustomWidthHeightOptions,
193+
},
194+
{
195+
code: `<div class="h-custom w-custom">Ambiguous cannot size-*</div>`,
196+
options: ambiguousOptions,
197+
},
198+
{
199+
code: `<div class="h-custom w-custom">h-custom & w-custom don't exist... no size-*</div>`,
200+
options: customSizeOnlyOptions,
165201
},
166202
],
167203

@@ -759,10 +795,10 @@ ruleTester.run("shorthands", rule, {
759795
errors: [generateError(["md:h-5", "md:w-5"], "md:size-5")],
760796
},
761797
{
762-
code: `<div class="h-custom w-custom">size-* is based on spacing</div>`,
763-
output: `<div class="size-custom">size-* is based on spacing</div>`,
798+
code: `<div class="h-custom w-custom">size-*</div>`,
799+
output: `<div class="size-custom">size-*</div>`,
764800
errors: [generateError(["h-custom", "w-custom"], "size-custom")],
765-
options: customSpacingOptions,
801+
options: customSpacingOnlyOptions,
766802
},
767803
],
768804
});

0 commit comments

Comments
 (0)