Skip to content

Why negative sign before a variable inside round function is a syntax error #2523

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

Closed
Sesna opened this issue Feb 24, 2025 · 3 comments · Fixed by #2524
Closed

Why negative sign before a variable inside round function is a syntax error #2523

Sesna opened this issue Feb 24, 2025 · 3 comments · Fixed by #2524
Assignees
Labels

Comments

@Sesna
Copy link

Sesna commented Feb 24, 2025

I'm migrating node-sass to dart-sass, this code works in node-sass, but got a syntax error in dart-sass.

.abc {
  $sqrt: 1.41;
  $dim: 30px;
  top: round(-$dim / $sqrt);
}
Image

https://sass-lang.com/playground/#eJwzNNRLTEpWqOZSUFApLiwqsVIw1DMxtAZxUzJzrRSMDQoqQLyS/AIrhaL80rwUDV2QjII+RL2mNVctAGRnEoU=

After looking through the official docs, I found that in the early version, the round is always parsed as a sass function
see docs: https://sass-lang.com/documentation/values/calculations/#round

But I look through the docs of variables and operators, I still don't know why -$dim / $sqrt inside round can't be compiled to round(-30px / 1.41)

@nex3
Copy link
Contributor

nex3 commented Feb 24, 2025

There are three overlapping things going on here:

  1. Node Sass doesn't have support for modern math functions like round(), clamp(), sin(), and so on. Dart Sass does, and so in some circumstances, it will evaluate these expressions using CSS's math function semantics rather than the normal SassScript expression semantics.

  2. For reasons that aren't entirely clear to me (possibly because - can begin an identifier in CSS?), CSS math functions don't support a unary minus operator like you're using in -$dim. See the specification for details.

  3. round() in particular is a global Sass function in addition to being a CSS math function, and for backwards-compatibility it's supposed to be evaluated as SassScript if it uses any features that aren't available in plain CSS. The fact that it's not doing so here is a bug that I will fix.

Since global Sass functions are deprecated anyway, the best solution here is probably to add @use 'sass:math'; to the top of your stylesheet and write math.round() instead, which will never be interpreted as a CSS function.

@nex3 nex3 added the bug label Feb 24, 2025
@nex3 nex3 self-assigned this Feb 24, 2025
nex3 added a commit to sass/sass-spec that referenced this issue Feb 25, 2025
nex3 added a commit that referenced this issue Feb 25, 2025
This was previously checking whether *either* operator was
calculation-safe, when in fact it should check that *both* are.

Closes #2523
nex3 added a commit to sass/sass-spec that referenced this issue Feb 25, 2025
nex3 added a commit that referenced this issue Feb 25, 2025
This was previously checking whether *either* operator was
calculation-safe, when in fact it should check that *both* are.

Closes #2523
nex3 added a commit to sass/sass-spec that referenced this issue Feb 25, 2025
@nex3 nex3 closed this as completed in 87ef19a Feb 25, 2025
@nex3
Copy link
Contributor

nex3 commented Feb 25, 2025

The fix has been released in Sass 1.85.1. You'll notice that your playground link is now working as intended.

@Sesna
Copy link
Author

Sesna commented Mar 10, 2025

Thank you. I agree that we should use sass math to clarify, and we've done that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants