Skip to content

Commit 7cfc5e5

Browse files
authored
Fix lightness-notation crash with "number" option and single-digit percentage (#8661)
1 parent 4a7c5e5 commit 7cfc5e5

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

.changeset/major-seals-bathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"stylelint": patch
3+
---
4+
5+
Fixed: `lightness-notation` crash with `"number"` option and single-digit percentage

lib/rules/lightness-notation/__tests__/index.mjs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,19 @@ testRule({
214214
},
215215
],
216216
},
217+
{
218+
code: 'a { color: oklab(0 0.1 241) }',
219+
fixed: 'a { color: oklab(0% 0.1 241) }',
220+
fix: {
221+
range: [17, 18],
222+
text: '0%',
223+
},
224+
message: messages.expected('0', '0%'),
225+
line: 1,
226+
column: 18,
227+
endLine: 1,
228+
endColumn: 19,
229+
},
217230
],
218231
});
219232

@@ -423,5 +436,31 @@ testRule({
423436
},
424437
],
425438
},
439+
{
440+
code: 'a { color: oklab(5% -100% 0.4) }',
441+
fixed: 'a { color: oklab(0.05 -100% 0.4) }',
442+
fix: {
443+
range: [17, 19],
444+
text: '0.05',
445+
},
446+
message: messages.expected('5%', '0.05'),
447+
line: 1,
448+
column: 18,
449+
endLine: 1,
450+
endColumn: 20,
451+
},
452+
{
453+
code: 'a { color: oklab(0% -100% 0.4) }',
454+
fixed: 'a { color: oklab(0 -100% 0.4) }',
455+
fix: {
456+
range: [18, 19],
457+
text: '',
458+
},
459+
message: messages.expected('0%', '0'),
460+
line: 1,
461+
column: 18,
462+
endLine: 1,
463+
endColumn: 20,
464+
},
426465
],
427466
});

lib/rules/lightness-notation/index.cjs

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/rules/lightness-notation/index.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,16 @@ function asNumber(value, func) {
139139
/**
140140
* @param {number} num
141141
* @param {string} value
142+
* @returns {string}
142143
*/
143144
function roundToNumberOfDigits(num, value) {
144-
return num.toPrecision(value.length - 2);
145+
if (num === 0) return '0';
146+
147+
const precision = value.replaceAll('%', '').replaceAll('.', '').length;
148+
149+
if (precision === 0) return `${num}`;
150+
151+
return num.toPrecision(precision).replace(/0+$/, ''); // trim trailing zeros
145152
}
146153

147154
/**

0 commit comments

Comments
 (0)