Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix declaration-block-no-redundant-longhand-properties autofix with cubic-bezier() #6841

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/soft-islands-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `declaration-block-no-redundant-longhand-properties` `autofix` with `cubic-bezier()`
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ testRule({
description: 'custom resolver for transition - missing transition-delay',
message: messages.expected('transition'),
},
{
code: '.class-name { transition-delay: 25ms; transition-duration: 10ms; transition-property: margin; transition-timing-function: cubic-bezier(0, 1, 1, 1); }',
fixed: '.class-name { transition: margin 10ms cubic-bezier(0, 1, 1, 1) 25ms; }',
description: 'autofixer should not mangle css functions with comma separated values',
message: messages.expected('transition'),
},
{
code: '.class-name { transition-delay: 25ms, 50ms; transition-duration: 10ms, 20ms; transition-property: margin, padding; transition-timing-function: cubic-bezier(0, 1, 1, 1), cubic-bezier(1, 0, 0, 1); }',
fixed:
'.class-name { transition: margin 10ms cubic-bezier(0, 1, 1, 1) 25ms, padding 20ms cubic-bezier(1, 0, 0, 1) 50ms; }',
description: 'autofixer should not mangle css functions with comma separated values',
message: messages.expected('transition'),
},
],
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const valueParser = require('postcss-value-parser');

const arrayEqual = require('../../utils/arrayEqual');
const { basicKeywords } = require('../../reference/keywords');
const eachDeclarationBlock = require('../../utils/eachDeclarationBlock');
Expand Down Expand Up @@ -51,11 +53,39 @@ const customResolvers = new Map([
'transition',
(decls) => {
/** @type {(input: string | undefined) => string[]} */
const commaSeparated = (input = '') =>
input
.split(',')
.map((s) => s.trim())
.filter((s) => s.length > 0);
const commaSeparated = (input = '') => {
let trimmedInput = input.trim();

if (!trimmedInput) return [];

if (trimmedInput.indexOf(',') === -1) return [trimmedInput];

/** @type {import('postcss-value-parser').ParsedValue} */
let parsedValue = valueParser(trimmedInput);
/** @type {Array<Array<import('postcss-value-parser').Node>>} */
let valueParts = [];

{
/** @type {Array<import('postcss-value-parser').Node>} */
let currentListItem = [];

parsedValue.nodes.forEach((node) => {
if (node.type === 'div' && node.value === ',') {
valueParts.push(currentListItem);
currentListItem = [];

return;
}

currentListItem.push(node);
});

valueParts.push(currentListItem);
}

return valueParts.map((s) => valueParser.stringify(s).trim()).filter((s) => s.length > 0);
};

const delays = commaSeparated(decls.get('transition-delay')?.value);
const durations = commaSeparated(decls.get('transition-duration')?.value);
const timingFunctions = commaSeparated(decls.get('transition-timing-function')?.value);
Expand Down