@@ -6,6 +6,7 @@ import 'dart:math' as math;
6
6
7
7
import 'package:charcode/charcode.dart' ;
8
8
import 'package:meta/meta.dart' ;
9
+ import 'package:source_span/source_span.dart' ;
9
10
10
11
import '../deprecation.dart' ;
11
12
import '../evaluation_context.dart' ;
@@ -482,13 +483,47 @@ final class SassCalculation extends Value {
482
483
/// This may be passed fewer than two arguments, but only if one of the
483
484
/// arguments is an unquoted `var()` string.
484
485
static Value round (Object strategyOrNumber,
485
- [Object ? numberOrStep, Object ? step]) {
486
+ [Object ? numberOrStep, Object ? step]) =>
487
+ roundInternal (strategyOrNumber, numberOrStep, step,
488
+ span: null , inLegacySassFunction: null , warn: null );
489
+
490
+ /// Like [round] , but with the internal-only [inLegacySassFunction] and
491
+ /// [warn] parameters.
492
+ ///
493
+ /// If [inLegacySassFunction] isn't null, this allows unitless numbers to be
494
+ /// added and subtracted with numbers with units, for backwards-compatibility
495
+ /// with the old global `min()` and `max()` functions. This emits a
496
+ /// deprecation warning using the string as the function's name.
497
+ ///
498
+ /// If [simplify] is `false` , no simplification will be done.
499
+ ///
500
+ /// The [warn] callback is used to surface deprecation warnings.
501
+ ///
502
+ /// @nodoc
503
+ @internal
504
+ static Value roundInternal (
505
+ Object strategyOrNumber, Object ? numberOrStep, Object ? step,
506
+ {required FileSpan ? span,
507
+ required String ? inLegacySassFunction,
508
+ required void Function (String message, [Deprecation ? deprecation])?
509
+ warn}) {
486
510
switch ((
487
511
_simplify (strategyOrNumber),
488
512
numberOrStep.andThen (_simplify),
489
513
step.andThen (_simplify)
490
514
)) {
491
- case (SassNumber number, null , null ):
515
+ case (SassNumber (hasUnits: false ) && var number, null , null ):
516
+ return SassNumber (number.value.round ());
517
+
518
+ case (SassNumber number, null , null ) when inLegacySassFunction != null :
519
+ warn !(
520
+ "In future versions of Sass, round() will be interpreted as a CSS "
521
+ "round() calculation. This requires an explicit modulus when "
522
+ "rounding numbers with units. If you want to use the Sass "
523
+ "function, call math.round() instead.\n "
524
+ "\n "
525
+ "See https://sass-lang.com/d/import" ,
526
+ Deprecation .globalBuiltin);
492
527
return _matchUnits (number.value.round ().toDouble (), number);
493
528
494
529
case (SassNumber number, SassNumber step, null )
@@ -542,12 +577,8 @@ final class SassCalculation extends Value {
542
577
throw SassScriptException (
543
578
"Number to round and step arguments are required." );
544
579
545
- case (SassString rest, null , null ):
546
- return SassCalculation ._("round" , [rest]);
547
-
548
580
case (var number, null , null ):
549
- throw SassScriptException (
550
- "Single argument $number expected to be simplifiable." );
581
+ return SassCalculation ._("round" , [number]);
551
582
552
583
case (var number, var step? , null ):
553
584
return SassCalculation ._("round" , [number, step]);
@@ -584,32 +615,54 @@ final class SassCalculation extends Value {
584
615
static Object operate (
585
616
CalculationOperator operator , Object left, Object right) =>
586
617
operateInternal (operator , left, right,
587
- inLegacySassFunction: false , simplify: true );
618
+ inLegacySassFunction: null , simplify: true , warn : null );
588
619
589
- /// Like [operate] , but with the internal-only [inLegacySassFunction] parameter.
620
+ /// Like [operate] , but with the internal-only [inLegacySassFunction] and
621
+ /// [warn] parameters.
590
622
///
591
- /// If [inLegacySassFunction] is `true` , this allows unitless numbers to be added and
592
- /// subtracted with numbers with units, for backwards-compatibility with the
593
- /// old global `min()` and `max()` functions.
623
+ /// If [inLegacySassFunction] isn't null, this allows unitless numbers to be
624
+ /// added and subtracted with numbers with units, for backwards-compatibility
625
+ /// with the old global `min()` and `max()` functions. This emits a
626
+ /// deprecation warning using the string as the function's name.
594
627
///
595
628
/// If [simplify] is `false` , no simplification will be done.
629
+ ///
630
+ /// The [warn] callback is used to surface deprecation warnings.
631
+ ///
632
+ /// @nodoc
596
633
@internal
597
634
static Object operateInternal (
598
635
CalculationOperator operator , Object left, Object right,
599
- {required bool inLegacySassFunction, required bool simplify}) {
636
+ {required String ? inLegacySassFunction,
637
+ required bool simplify,
638
+ required void Function (String message, [Deprecation ? deprecation])?
639
+ warn}) {
600
640
if (! simplify) return CalculationOperation ._(operator , left, right);
601
641
left = _simplify (left);
602
642
right = _simplify (right);
603
643
604
644
if (operator case CalculationOperator .plus || CalculationOperator .minus) {
605
- if (left is SassNumber &&
606
- right is SassNumber &&
607
- (inLegacySassFunction
608
- ? left.isComparableTo (right)
609
- : left.hasCompatibleUnits (right))) {
610
- return operator == CalculationOperator .plus
611
- ? left.plus (right)
612
- : left.minus (right);
645
+ if (left is SassNumber && right is SassNumber ) {
646
+ var compatible = left.hasCompatibleUnits (right);
647
+ if (! compatible &&
648
+ inLegacySassFunction != null &&
649
+ left.isComparableTo (right)) {
650
+ warn !(
651
+ "In future versions of Sass, $inLegacySassFunction () will be "
652
+ "interpreted as the CSS $inLegacySassFunction () calculation. "
653
+ "This doesn't allow unitless numbers to be mixed with numbers "
654
+ "with units. If you want to use the Sass function, call "
655
+ "math.$inLegacySassFunction () instead.\n "
656
+ "\n "
657
+ "See https://sass-lang.com/d/import" ,
658
+ Deprecation .globalBuiltin);
659
+ compatible = true ;
660
+ }
661
+ if (compatible) {
662
+ return operator == CalculationOperator .plus
663
+ ? left.plus (right)
664
+ : left.minus (right);
665
+ }
613
666
}
614
667
615
668
_verifyCompatibleNumbers ([left, right]);
0 commit comments