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 time-min-milliseconds end positions #6273

Merged
merged 1 commit into from Aug 17, 2022
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
56 changes: 56 additions & 0 deletions lib/rules/time-min-milliseconds/__tests__/index.js
Expand Up @@ -152,168 +152,224 @@ testRule({
message: messages.expected(MIN_VALUE),
line: 1,
column: 23,
endLine: 1,
endColumn: 29,
},
{
code: 'a { -webkit-transition-delay: 0.006s; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 31,
endLine: 1,
endColumn: 37,
},
{
code: 'a { tRaNsItIoN-dElAy: 0.006s; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 23,
endLine: 1,
endColumn: 29,
},
{
code: 'a { TRANSITION-DELAY: 0.006s; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 23,
endLine: 1,
endColumn: 29,
},
{
code: 'a { transition-delay: 0.006S; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 23,
endLine: 1,
endColumn: 29,
},
{
code: 'a { transition-delay: 3ms; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 23,
endLine: 1,
endColumn: 26,
},
{
code: 'a { transition-delay: 3MS; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 23,
endLine: 1,
endColumn: 26,
},
{
code: 'a { transition-duration: 0.009s; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 26,
endLine: 1,
endColumn: 32,
},
{
code: 'a { -webkit-transition-duration: 0.009s; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 34,
endLine: 1,
endColumn: 40,
},
{
code: 'a { transition-duration: 80ms; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 26,
endLine: 1,
endColumn: 30,
},
{
code: 'a { transition: foo 0.008s linear; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 21,
endLine: 1,
endColumn: 27,
},
{
code: 'a { -webkit-transition: foo 0.008s linear; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 29,
endLine: 1,
endColumn: 35,
},
{
code: 'a { tRaNsItIoN: foo 0.008s linear; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 21,
endLine: 1,
endColumn: 27,
},
{
code: 'a { TRANSITION: foo 0.008s linear; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 21,
endLine: 1,
endColumn: 27,
},
{
code: 'a { transition: foo 0.8s ease-in-out 20ms; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 38,
endLine: 1,
endColumn: 42,
},
{
code: 'a { transition: foo 0.8s ease 200ms, bar 0.8s ease 20ms; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 52,
endLine: 1,
endColumn: 56,
},
{
code: 'a { transition: foo 0.8s ease, bar 30ms ease; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 36,
endLine: 1,
endColumn: 40,
},
{
code: 'a { animation-delay: 0.006s; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 22,
endLine: 1,
endColumn: 28,
},
{
code: 'a { -webkit-animation-delay: 0.006s; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 30,
endLine: 1,
endColumn: 36,
},
{
code: 'a { animation-delay: 3ms; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 22,
endLine: 1,
endColumn: 25,
},
{
code: 'a { animation-duration: 0.009s; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 25,
endLine: 1,
endColumn: 31,
},
{
code: 'a { -webkit-animation-duration: 0.009s; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 33,
endLine: 1,
endColumn: 39,
},
{
code: 'a { animation-duration: 80ms; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 25,
endLine: 1,
endColumn: 29,
},
{
code: 'a { animation: foo 0.008s linear; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 20,
endLine: 1,
endColumn: 26,
},
{
code: 'a { -webkit-animation: foo 0.008s linear; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 28,
endLine: 1,
endColumn: 34,
},
{
code: 'a { animation: foo 0.8s ease-in-out 20ms; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 37,
endLine: 1,
endColumn: 41,
},
{
code: 'a { animation: foo 0.8s ease, bar 20ms ease; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 35,
endLine: 1,
endColumn: 39,
},
{
code: 'a { animation: foo 0.8s ease 0.1s, bar 0.8s ease 0.01s; }',
message: messages.expected(MIN_VALUE),
line: 1,
column: 50,
endLine: 1,
endColumn: 55,
},
],
});
Expand Down
34 changes: 19 additions & 15 deletions lib/rules/time-min-milliseconds/index.js
Expand Up @@ -23,7 +23,7 @@ const meta = {

const DELAY_PROPERTIES = new Set(['animation-delay', 'transition-delay']);

/** @type {import('stylelint').Rule} */
/** @type {import('stylelint').Rule<number>} */
const rule = (primary, secondaryOptions) => {
return (root, result) => {
const validOptions = validateOptions(
Expand All @@ -46,37 +46,39 @@ const rule = (primary, secondaryOptions) => {
return;
}

const minimum = /** @type {number} */ (primary);
const minimum = primary;
const ignoreDelay = optionsMatches(secondaryOptions, 'ignore', 'delay');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] Avoid repeated evaluation.


root.walkDecls((decl) => {
const propertyName = vendor.unprefixed(decl.prop.toLowerCase());
const propertyValue = decl.value;

if (
longhandTimeProperties.has(propertyName) &&
!isIgnoredProperty(propertyName) &&
!isAcceptableTime(decl.value)
!isAcceptableTime(propertyValue)
) {
complain(decl);
complain(decl, 0, propertyValue.length);
}

if (shorthandTimeProperties.has(propertyName)) {
const valueListList = postcss.list.comma(decl.value);
const valueListList = postcss.list.comma(propertyValue);

for (const valueListString of valueListList) {
const valueList = postcss.list.space(valueListString);

if (optionsMatches(secondaryOptions, 'ignore', 'delay')) {
if (ignoreDelay) {
// Check only duration time values
const duration = getDuration(valueList);

if (duration && !isAcceptableTime(duration)) {
complain(decl, decl.value.indexOf(duration));
complain(decl, propertyValue.indexOf(duration), duration.length);
}
} else {
// Check all time values
for (const value of valueList) {
if (!isAcceptableTime(value)) {
complain(decl, decl.value.indexOf(value));
complain(decl, propertyValue.indexOf(value), value.length);
}
}
}
Expand Down Expand Up @@ -108,10 +110,7 @@ const rule = (primary, secondaryOptions) => {
* @returns {boolean}
*/
function isIgnoredProperty(propertyName) {
if (
optionsMatches(secondaryOptions, 'ignore', 'delay') &&
DELAY_PROPERTIES.has(propertyName)
) {
if (ignoreDelay && DELAY_PROPERTIES.has(propertyName)) {
return true;
}

Expand Down Expand Up @@ -148,15 +147,20 @@ const rule = (primary, secondaryOptions) => {

/**
* @param {import('postcss').Declaration} decl
* @param {number} [offset]
* @param {number} offset
* @param {number} length
* @returns {void}
*/
function complain(decl, offset = 0) {
function complain(decl, offset, length) {
const index = declarationValueIndex(decl) + offset;
const endIndex = index + length;

report({
result,
ruleName,
message: messages.expected(minimum),
index: declarationValueIndex(decl) + offset,
index,
endIndex,
node: decl,
});
}
Expand Down