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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error message percentage/number precision for alpha-value-notation #4802

Merged
merged 2 commits into from Jun 2, 2020
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
32 changes: 32 additions & 0 deletions lib/rules/alpha-value-notation/__tests__/index.js
Expand Up @@ -124,6 +124,22 @@ testRule({
{ message: messages.expected('40%', '0.4'), line: 5, column: 29 },
],
},
{
code: 'a { opacity: 14% }',
fixed: 'a { opacity: 0.14 }',
message: messages.expected('14%', '0.14'),
line: 1,
column: 14,
description: 'properly deals with floating-point conversions',
},
{
code: 'a { opacity: 0.3% }',
fixed: 'a { opacity: 0.003 }',
message: messages.expected('0.3%', '0.003'),
line: 1,
column: 14,
description: 'properly deals with floating-point conversions',
},
],
});

Expand Down Expand Up @@ -199,6 +215,22 @@ testRule({
{ message: messages.expected('0.40', '40%'), line: 5, column: 29 },
],
},
{
code: 'a { opacity: 0.14 }',
fixed: 'a { opacity: 14% }',
message: messages.expected('0.14', '14%'),
line: 1,
column: 14,
description: 'properly deals with floating-point conversions',
},
{
code: 'a { opacity: 0.003 }',
fixed: 'a { opacity: 0.3% }',
message: messages.expected('0.003', '0.3%'),
line: 1,
column: 14,
description: 'properly deals with floating-point conversions',
},
],
});

Expand Down
4 changes: 2 additions & 2 deletions lib/rules/alpha-value-notation/index.js
Expand Up @@ -115,13 +115,13 @@ function rule(primary, options, context) {
}

function asPercentage(value) {
return `${value * 100}%`;
return `${Number((value * 100).toPrecision(3))}%`;
}

function asNumber(value) {
const { number } = valueParser.unit(value);

return number / 100;
return Number((number / 100).toPrecision(3));
}

function findAlphaInValue(node) {
Expand Down