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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate math.random() when $limit has units #1779

Merged
merged 5 commits into from Aug 18, 2022
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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()` 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
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 20 additions & 3 deletions lib/src/functions/math.dart
Expand Up @@ -289,11 +289,28 @@ 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"
"Recommendation: "
"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}))\n"
"\n"
"More info: https://sass-lang.com/d/random-with-units",
);
}

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) {
Expand Down