Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor
  • Loading branch information
ryo-manba committed Apr 24, 2025
commit c9b5ec8162ccbb08fad9f392684a9ce1ee227b69
111 changes: 65 additions & 46 deletions lib/rules/shorthand-property-no-redundant-values/index.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 65 additions & 46 deletions lib/rules/shorthand-property-no-redundant-values/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,63 @@ function splitBorderRadius(value) {
return { horizontal, vertical, sawSlash, horizontalHasVar, verticalHasVar };
}

/**
* Process the `border-radius` value when a slash is present.
*
* @param {import('stylelint').PostcssResult} result
* @param {import('postcss').Declaration} decl
* @param {string[]} horizontal
* @param {string[]} vertical
* @param {boolean} horizontalHasVar
* @param {boolean} verticalHasVar
*/
function processBorderRadiusValue(
result,
decl,
horizontal,
vertical,
horizontalHasVar,
verticalHasVar,
) {
const [h1 = '', h2 = '', h3 = '', h4 = ''] = horizontal;
const [v1 = '', v2 = '', v3 = '', v4 = ''] = vertical;

// Skip condensation on the side containing `var()`, condense the other.
const condensedHorizontal = horizontalHasVar
? horizontal.join(' ')
: canCondense(h1, h2, h3, h4).filter(Boolean).join(' ');
const condensedVertical = verticalHasVar
? vertical.join(' ')
: canCondense(v1, v2, v3, v4).filter(Boolean).join(' ');

const shortestFormString = `${condensedHorizontal} / ${condensedVertical}`.trim();
const valueLower = decl.value.trim().toLowerCase().replace(/\s+/g, ' ');
const shortestLower = shortestFormString.toLowerCase();

if (valueLower === shortestLower) return; // Already optimal

const fix = () => {
decl.value = shortestFormString;
};

const index = declarationValueIndex(decl);
const endIndex = index + decl.value.length;

report({
message: messages.expected,
messageArgs: [decl.value, shortestFormString],
node: decl,
index,
endIndex,
result,
ruleName,
fix: {
apply: fix,
node: decl,
},
});
}

/** @type {import('stylelint').CoreRules[ruleName]} */
const rule = (primary) => {
return (root, result) => {
Expand Down Expand Up @@ -196,52 +253,14 @@ const rule = (primary) => {
splitBorderRadius(value);

if (sawSlash) {
if (
horizontal.length >= 1 &&
horizontal.length <= 4 &&
vertical.length >= 1 &&
vertical.length <= 4
) {
const [h1 = '', h2 = '', h3 = '', h4 = ''] = horizontal;
const [v1 = '', v2 = '', v3 = '', v4 = ''] = vertical;

// varFunction is not supported in shorthand
const condensedHorizontal = horizontalHasVar
? horizontal.join(' ')
: canCondense(h1, h2, h3, h4).filter(Boolean).join(' ');
const condensedVertical = verticalHasVar
? vertical.join(' ')
: canCondense(v1, v2, v3, v4).filter(Boolean).join(' ');

const shortestFormString = `${condensedHorizontal} / ${condensedVertical}`.trim();
const valueLower = value.trim().toLowerCase().replace(/\s+/g, ' ');
const shortestLower = shortestFormString.toLowerCase();

if (valueLower === shortestLower) return; // Already optimal

const fix = () => {
decl.value = shortestFormString;
};

const index = declarationValueIndex(decl);
const endIndex = index + decl.value.length;

report({
message: messages.expected,
messageArgs: [value, shortestFormString],
node: decl,
index,
endIndex,
result,
ruleName,
fix: {
apply: fix,
node: decl,
},
});

return;
}
processBorderRadiusValue(
result,
decl,
horizontal,
vertical,
horizontalHasVar,
verticalHasVar,
);

return;
}
Expand Down