Skip to content

Commit

Permalink
fix: size-* is based on spacing config
Browse files Browse the repository at this point in the history
  • Loading branch information
francoismassart committed Feb 3, 2024
1 parent 4f2a833 commit 228f3b7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/rules/enforces-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,10 @@ module.exports = {
if (mode === 'value') {
const bodyMatch = inputSet.some((inputClassPattern) => inputClassPattern === remainingClass.body);
// w-screen + h-screen ≠ size-screen (Issue #307)
const validValue =
bodyMatch && !(['w-', 'h-'].includes(remainingClass.body) && ['screen'].includes(remainingClass.value));
const spacingKeys = Object.keys(mergedConfig.theme.spacing);
const isSize = ['w-', 'h-'].includes(remainingClass.body);
const isValidSize = spacingKeys.includes(remainingClass.value);
const validValue = bodyMatch && !(isSize && !isValidSize);
return validValue;
}
});
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/rules/enforces-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,33 @@ const skipClassAttributeOptions = [
},
];

const customWidthHeightOptions = [
{
config: {
theme: {
extend: {
width: { custom: "100px" },
height: { custom: "100px" },
},
},
plugins: [],
},
},
];

const customSpacingOptions = [
{
config: {
theme: {
extend: {
spacing: { custom: "100px" },
},
},
plugins: [],
},
},
];

var generateError = (classnames, shorthand) => {
return {
messageId: "shorthandCandidateDetected",
Expand Down Expand Up @@ -132,6 +159,10 @@ ruleTester.run("shorthands", rule, {
{
code: "<div className={'w-screen h-screen'}>issue #307</div>",
},
{
code: `<div class="h-custom w-custom">size-* is based on spacing</div>`,
options: customWidthHeightOptions,
},
],

invalid: [
Expand Down Expand Up @@ -727,5 +758,11 @@ ruleTester.run("shorthands", rule, {
output: `<div class="h-10 md:size-5 lg:w-10">New size-* utilities</div>`,
errors: [generateError(["md:h-5", "md:w-5"], "md:size-5")],
},
{
code: `<div class="h-custom w-custom">size-* is based on spacing</div>`,
output: `<div class="size-custom">size-* is based on spacing</div>`,
errors: [generateError(["h-custom", "w-custom"], "size-custom")],
options: customSpacingOptions,
},
],
});

0 comments on commit 228f3b7

Please sign in to comment.