Skip to content

Commit 3f98441

Browse files
authored
Deprecate math.random() when $limit has units (#1779)
* Deprecate math.random() when $limit has units * add changelog for random with units deprecation * add link to sass-site/d/random-with-units
1 parent 0b8a0f6 commit 3f98441

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
* Properly consider `b > c` to be a superselector of `a > b > c`, and similarly
77
for other combinators.
88

9+
* Deprecate use of `random()` when `$limit` has units to make it explicit that
10+
`random()` currently ignores units. A future version will no longer ignore
11+
units.
12+
913
## 1.54.4
1014

1115
* Improve error messages when passing incorrect units that are also
@@ -38,7 +42,7 @@
3842
* Add partial support for new media query syntax from Media Queries Level 4. The
3943
only exception are logical operations nested within parentheses, as these were
4044
previously interpreted differently as SassScript expressions.
41-
45+
4246
A parenthesized media condition that begins with `not` or an opening
4347
parenthesis now produces a deprecation warning. In a future release, these
4448
will be interpreted as plain CSS instead.

lib/src/functions/math.dart

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,28 @@ final _random = math.Random();
289289

290290
final _randomFunction = _function("random", r"$limit: null", (arguments) {
291291
if (arguments[0] == sassNull) return SassNumber(_random.nextDouble());
292-
var limit = arguments[0].assertNumber("limit").assertInt("limit");
293-
if (limit < 1) {
292+
var limit = arguments[0].assertNumber("limit");
293+
294+
if (limit.hasUnits) {
295+
warn(
296+
"math.random() will no longer ignore \$limit units ($limit) in a "
297+
"future release.\n"
298+
"\n"
299+
"Recommendation: "
300+
"math.random(math.div(\$limit, 1${limit.unitString})) * 1${limit.unitString}\n"
301+
"\n"
302+
"To preserve current behavior: "
303+
"math.random(math.div(\$limit, 1${limit.unitString}))\n"
304+
"\n"
305+
"More info: https://sass-lang.com/d/random-with-units",
306+
);
307+
}
308+
309+
var limitScalar = limit.assertInt("limit");
310+
if (limitScalar < 1) {
294311
throw SassScriptException("\$limit: Must be greater than 0, was $limit.");
295312
}
296-
return SassNumber(_random.nextInt(limit) + 1);
313+
return SassNumber(_random.nextInt(limitScalar) + 1);
297314
});
298315

299316
final _div = _function("div", r"$number1, $number2", (arguments) {

0 commit comments

Comments
 (0)