From 53124e55d474846f7bcdd84394166ecc14e5b3d9 Mon Sep 17 00:00:00 2001 From: Matt Wang Date: Fri, 22 May 2020 19:23:38 -0700 Subject: [PATCH 1/2] preliminary adjustments to asPercentage, asNumber possible issues with very small numbers & specifying precision --- .../alpha-value-notation/__tests__/index.js | 48 +++++++++++++++++++ lib/rules/alpha-value-notation/index.js | 4 +- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/rules/alpha-value-notation/__tests__/index.js b/lib/rules/alpha-value-notation/__tests__/index.js index 497a6d1d90..5785659c86 100644 --- a/lib/rules/alpha-value-notation/__tests__/index.js +++ b/lib/rules/alpha-value-notation/__tests__/index.js @@ -271,3 +271,51 @@ testRule({ }, ], }); + +// testing that floating point-rounded numbers-to-percentages are handled properly +testRule({ + ruleName, + config: ['percentage'], + fix: true, + + reject: [ + { + code: 'a { opacity: 0.14 }', + fixed: 'a { opacity: 14% }', + message: messages.expected('0.14', '14%'), + line: 1, + column: 14, + }, + { + code: 'a { opacity: 0.003 }', + fixed: 'a { opacity: 0.3% }', + message: messages.expected('0.003', '0.3%'), + line: 1, + column: 14, + }, + ], +}); + +// testing that floating point-rounded percentages-to-numbers are handled properly +testRule({ + ruleName, + config: ['number'], + fix: true, + + reject: [ + { + code: 'a { opacity: 14% }', + fixed: 'a { opacity: 0.14 }', + message: messages.expected('14%', '0.14'), + line: 1, + column: 14, + }, + { + code: 'a { opacity: 0.3% }', + fixed: 'a { opacity: 0.003 }', + message: messages.expected('0.3%', '0.003'), + line: 1, + column: 14, + }, + ], +}); diff --git a/lib/rules/alpha-value-notation/index.js b/lib/rules/alpha-value-notation/index.js index c8c2798f50..45c270c1fb 100644 --- a/lib/rules/alpha-value-notation/index.js +++ b/lib/rules/alpha-value-notation/index.js @@ -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) { From e598e45e058920e5152152afe8a28ae9f2898961 Mon Sep 17 00:00:00 2001 From: Matt Wang Date: Tue, 26 May 2020 18:06:10 -0700 Subject: [PATCH 2/2] updating test locations & description --- .../alpha-value-notation/__tests__/index.js | 80 ++++++++----------- 1 file changed, 32 insertions(+), 48 deletions(-) diff --git a/lib/rules/alpha-value-notation/__tests__/index.js b/lib/rules/alpha-value-notation/__tests__/index.js index 5785659c86..abee5d8730 100644 --- a/lib/rules/alpha-value-notation/__tests__/index.js +++ b/lib/rules/alpha-value-notation/__tests__/index.js @@ -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', + }, ], }); @@ -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', + }, ], }); @@ -271,51 +303,3 @@ testRule({ }, ], }); - -// testing that floating point-rounded numbers-to-percentages are handled properly -testRule({ - ruleName, - config: ['percentage'], - fix: true, - - reject: [ - { - code: 'a { opacity: 0.14 }', - fixed: 'a { opacity: 14% }', - message: messages.expected('0.14', '14%'), - line: 1, - column: 14, - }, - { - code: 'a { opacity: 0.003 }', - fixed: 'a { opacity: 0.3% }', - message: messages.expected('0.003', '0.3%'), - line: 1, - column: 14, - }, - ], -}); - -// testing that floating point-rounded percentages-to-numbers are handled properly -testRule({ - ruleName, - config: ['number'], - fix: true, - - reject: [ - { - code: 'a { opacity: 14% }', - fixed: 'a { opacity: 0.14 }', - message: messages.expected('14%', '0.14'), - line: 1, - column: 14, - }, - { - code: 'a { opacity: 0.3% }', - fixed: 'a { opacity: 0.003 }', - message: messages.expected('0.3%', '0.003'), - line: 1, - column: 14, - }, - ], -});