From 7306e8f312776f21cc5f4f1e097daebe9989da6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Israel=20Ortiz=20Garc=C3=ADa?= Date: Tue, 16 Aug 2022 14:41:30 -0700 Subject: [PATCH 1/5] Deprecate math.random() when $limit has units --- lib/src/functions/math.dart | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/src/functions/math.dart b/lib/src/functions/math.dart index f9c406103..45ded95cb 100644 --- a/lib/src/functions/math.dart +++ b/lib/src/functions/math.dart @@ -289,11 +289,23 @@ final _random = math.Random(); final _randomFunction = _function("random", r"$limit: null", (arguments) { if (arguments[0] == sassNull) return SassNumber(_random.nextDouble()); - var limit = arguments[0].assertNumber("limit").assertInt("limit"); - if (limit < 1) { + var limit = arguments[0].assertNumber("limit"); + + if (limit.hasUnits) { + warn( + "math.random() will no longer ignore \$limit units ($limit) in a " + "future release.\n" + "\n" + "If you meant to preserve units: " + "math.random($limit) * 1${limit.unitString}", + ); + } + + var limitScalar = limit.assertInt("limit"); + if (limitScalar < 1) { throw SassScriptException("\$limit: Must be greater than 0, was $limit."); } - return SassNumber(_random.nextInt(limit) + 1); + return SassNumber(_random.nextInt(limitScalar) + 1); }); final _div = _function("div", r"$number1, $number2", (arguments) { From 1ad0a73df04dbbd9b4057a661222fc8f90b7ce36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Israel=20Ortiz=20Garc=C3=ADa?= Date: Tue, 16 Aug 2022 18:08:22 -0700 Subject: [PATCH 2/5] improve warning and make recommendation not trigger the same warning --- lib/src/functions/math.dart | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/src/functions/math.dart b/lib/src/functions/math.dart index 45ded95cb..946d9c416 100644 --- a/lib/src/functions/math.dart +++ b/lib/src/functions/math.dart @@ -296,8 +296,11 @@ final _randomFunction = _function("random", r"$limit: null", (arguments) { "math.random() will no longer ignore \$limit units ($limit) in a " "future release.\n" "\n" - "If you meant to preserve units: " - "math.random($limit) * 1${limit.unitString}", + "Recommendation: " + "math.random(\$limit / 1${limit.unitString}) * 1${limit.unitString}\n" + "\n" + "To preserve current behavior: " + "math.random(\$limit / 1${limit.unitString})", ); } From 5273b05f0e9cf63567cf3c35f783fbdc8171e938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Israel=20Ortiz=20Garc=C3=ADa?= Date: Tue, 16 Aug 2022 18:29:15 -0700 Subject: [PATCH 3/5] use math.div instead of slash div --- lib/src/functions/math.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/functions/math.dart b/lib/src/functions/math.dart index 946d9c416..8dafc2f71 100644 --- a/lib/src/functions/math.dart +++ b/lib/src/functions/math.dart @@ -297,10 +297,10 @@ final _randomFunction = _function("random", r"$limit: null", (arguments) { "future release.\n" "\n" "Recommendation: " - "math.random(\$limit / 1${limit.unitString}) * 1${limit.unitString}\n" + "math.random(math.div(\$limit, 1${limit.unitString})) * 1${limit.unitString}\n" "\n" "To preserve current behavior: " - "math.random(\$limit / 1${limit.unitString})", + "math.random(math.div(\$limit, 1${limit.unitString}))", ); } From f823db9ca6e8bab2ca7411403050f8a35b67a3e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Israel=20Ortiz=20Garc=C3=ADa?= Date: Wed, 17 Aug 2022 14:39:10 -0700 Subject: [PATCH 4/5] add changelog for random with units deprecation --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c6c613b0..e81475ea2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ * Properly consider `b > c` to be a superselector of `a > b > c`, and similarly for other combinators. +* Deprecate use of `random($limit)` when `$limit` has units to make it explicit + that `random()` currently ignores units. A future version will no longer + ignore units. + ## 1.54.4 * Improve error messages when passing incorrect units that are also @@ -38,7 +42,7 @@ * Add partial support for new media query syntax from Media Queries Level 4. The only exception are logical operations nested within parentheses, as these were previously interpreted differently as SassScript expressions. - + A parenthesized media condition that begins with `not` or an opening parenthesis now produces a deprecation warning. In a future release, these will be interpreted as plain CSS instead. From 93a36320902e88f4e41cb1d5d168e69f6c37ae55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Israel=20Ortiz=20Garc=C3=ADa?= Date: Wed, 17 Aug 2022 17:38:52 -0700 Subject: [PATCH 5/5] add link to sass-site/d/random-with-units --- CHANGELOG.md | 6 +++--- lib/src/functions/math.dart | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e81475ea2..4e9655233 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,9 @@ * Properly consider `b > c` to be a superselector of `a > b > c`, and similarly for other combinators. -* Deprecate use of `random($limit)` when `$limit` has units to make it explicit - that `random()` currently ignores units. A future version will no longer - ignore units. +* Deprecate use of `random()` when `$limit` has units to make it explicit that + `random()` currently ignores units. A future version will no longer ignore + units. ## 1.54.4 diff --git a/lib/src/functions/math.dart b/lib/src/functions/math.dart index 8dafc2f71..15b7e0474 100644 --- a/lib/src/functions/math.dart +++ b/lib/src/functions/math.dart @@ -300,7 +300,9 @@ final _randomFunction = _function("random", r"$limit: null", (arguments) { "math.random(math.div(\$limit, 1${limit.unitString})) * 1${limit.unitString}\n" "\n" "To preserve current behavior: " - "math.random(math.div(\$limit, 1${limit.unitString}))", + "math.random(math.div(\$limit, 1${limit.unitString}))\n" + "\n" + "More info: https://sass-lang.com/d/random-with-units", ); }