diff --git a/CHANGELOG.md b/CHANGELOG.md index a54f7615f..c295c9d25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,10 @@ * Fix a bug where an interpolation in a custom property name crashed if the file was loaded by a `@use` nested in an `@import`. -* All functions defined in CSS Values and Units 4 are now parsed as calculation objects: -round(), mod(), rem(), sin(), cos(), tan(), asin(), acos(), atan(), atan2(), pow(), sqrt(), hypot(), log(), exp(), abs(), and sign(). +* All functions defined in CSS Values and Units 4 are now parsed as calculation + objects: `round()`, `mod()`, `rem()`, `sin()`, `cos()`, `tan()`, `asin()`, + `acos()`, `atan()`, `atan2()`, `pow()`, `sqrt()`, `hypot()`, `log()`, `exp()`, + `abs()`, and `sign()`. ### JavaScript API diff --git a/lib/src/value/calculation.dart b/lib/src/value/calculation.dart index bd67115fe..b89599e2f 100644 --- a/lib/src/value/calculation.dart +++ b/lib/src/value/calculation.dart @@ -170,7 +170,7 @@ class SassCalculation extends Value { /// [SassNumber] rather than a [SassCalculation]. It throws an exception if it /// can determine that the calculation will definitely produce invalid CSS. static Value sqrt(Object argument) => - _singleArgument("sqrt", argument, number_lib.sqrt, false); + _singleArgument("sqrt", argument, number_lib.sqrt, forbidUnits: true); /// Creates a `sin()` calculation with the given [argument]. /// @@ -182,7 +182,7 @@ class SassCalculation extends Value { /// [SassNumber] rather than a [SassCalculation]. It throws an exception if it /// can determine that the calculation will definitely produce invalid CSS. static Value sin(Object argument) => - _singleArgument("sin", argument, number_lib.sin, true); + _singleArgument("sin", argument, number_lib.sin); /// Creates a `cos()` calculation with the given [argument]. /// @@ -194,7 +194,7 @@ class SassCalculation extends Value { /// [SassNumber] rather than a [SassCalculation]. It throws an exception if it /// can determine that the calculation will definitely produce invalid CSS. static Value cos(Object argument) => - _singleArgument("cos", argument, number_lib.cos, true); + _singleArgument("cos", argument, number_lib.cos); /// Creates a `tan()` calculation with the given [argument]. /// @@ -206,7 +206,7 @@ class SassCalculation extends Value { /// [SassNumber] rather than a [SassCalculation]. It throws an exception if it /// can determine that the calculation will definitely produce invalid CSS. static Value tan(Object argument) => - _singleArgument("tan", argument, number_lib.tan, true); + _singleArgument("tan", argument, number_lib.tan); /// Creates an `atan()` calculation with the given [argument]. /// @@ -218,7 +218,7 @@ class SassCalculation extends Value { /// [SassNumber] rather than a [SassCalculation]. It throws an exception if it /// can determine that the calculation will definitely produce invalid CSS. static Value atan(Object argument) => - _singleArgument("atan", argument, number_lib.atan, false); + _singleArgument("atan", argument, number_lib.atan, forbidUnits: true); /// Creates an `asin()` calculation with the given [argument]. /// @@ -230,7 +230,7 @@ class SassCalculation extends Value { /// [SassNumber] rather than a [SassCalculation]. It throws an exception if it /// can determine that the calculation will definitely produce invalid CSS. static Value asin(Object argument) => - _singleArgument("asin", argument, number_lib.asin, false); + _singleArgument("asin", argument, number_lib.asin, forbidUnits: true); /// Creates an `acos()` calculation with the given [argument]. /// @@ -242,7 +242,7 @@ class SassCalculation extends Value { /// [SassNumber] rather than a [SassCalculation]. It throws an exception if it /// can determine that the calculation will definitely produce invalid CSS. static Value acos(Object argument) => - _singleArgument("acos", argument, number_lib.acos, false); + _singleArgument("acos", argument, number_lib.acos, forbidUnits: true); /// Creates an `abs()` calculation with the given [argument]. /// @@ -449,7 +449,7 @@ class SassCalculation extends Value { var result = dividend.modulo(modulus); if (modulus.value.signIncludingZero != dividend.value.signIncludingZero) { if (modulus.value.isInfinite) return dividend; - if (result is SassNumber && result.value == 0) { + if (result.value == 0) { return result.unaryMinus(); } return result.minus(dividend); @@ -543,25 +543,25 @@ class SassCalculation extends Value { "Number to round and step arguments are required."); case ( - (SassString() || CalculationInterpolation()) && var strategy, + (SassString() || CalculationInterpolation()) && var rest, null, null ): - return SassCalculation._("round", [strategy]); + return SassCalculation._("round", [rest]); case (var number, null, null): throw SassScriptException( "Single argument $number expected to be simplifiable."); - case (var strategy, var number?, null): - return SassCalculation._("round", [strategy, number]); + case (var number, var step?, null): + return SassCalculation._("round", [number, step]); case ( (SassString(text: 'nearest' || 'up' || 'down' || 'to-zero') || SassString(isVar: true)) && var strategy, - Object number, - Object step + var number?, + var step? ): return SassCalculation._("round", [strategy, number, step]); @@ -644,9 +644,10 @@ class SassCalculation extends Value { SassCalculation._(this.name, this.arguments); // Returns [value] coerced to [number]'s units. - static SassNumber _matchUnits(double value, SassNumber number) { - return SassNumber.withUnits(value, numeratorUnits: number.numeratorUnits); - } + static SassNumber _matchUnits(double value, SassNumber number) => + SassNumber.withUnits(value, + numeratorUnits: number.numeratorUnits, + denominatorUnits: number.denominatorUnits); /// Returns a rounded [number] based on a selected rounding [strategy], /// to the nearest integer multiple of [step]. @@ -766,14 +767,16 @@ class SassCalculation extends Value { /// Returns a [Callable] named [name] that calls a single argument /// math function. - static Value _singleArgument(String name, Object argument, - SassNumber mathFunc(SassNumber value), bool acceptsKnownUnits) { + /// + /// If [forbidUnits] is `true` it will throw an error if [argument] has units. + static Value _singleArgument( + String name, Object argument, SassNumber mathFunc(SassNumber value), + {bool forbidUnits = false}) { argument = _simplify(argument); - if (argument is! SassNumber || - (acceptsKnownUnits && argument.hasUnit('%'))) { + if (argument is! SassNumber) { return SassCalculation._(name, [argument]); } - if (!acceptsKnownUnits) argument.assertNoUnits(); + if (forbidUnits) argument.assertNoUnits(); return mathFunc(argument); } diff --git a/lib/src/value/number.dart b/lib/src/value/number.dart index 9a5821bfd..cf4804318 100644 --- a/lib/src/value/number.dart +++ b/lib/src/value/number.dart @@ -711,7 +711,7 @@ abstract class SassNumber extends Value { /// @nodoc @internal - Value modulo(Value other) { + SassNumber modulo(Value other) { if (other is SassNumber) { return withValue(_coerceUnits(other, moduloLikeSass)); } diff --git a/lib/src/value/number/unitless.dart b/lib/src/value/number/unitless.dart index 506ef6d5b..24d716402 100644 --- a/lib/src/value/number/unitless.dart +++ b/lib/src/value/number/unitless.dart @@ -98,7 +98,7 @@ class UnitlessSassNumber extends SassNumber { return super.lessThanOrEquals(other); } - Value modulo(Value other) { + SassNumber modulo(Value other) { if (other is SassNumber) { return other.withValue(moduloLikeSass(value, other.value)); } diff --git a/lib/src/visitor/evaluate.dart b/lib/src/visitor/evaluate.dart index 3de0c52aa..737644eb2 100644 --- a/lib/src/visitor/evaluate.dart +++ b/lib/src/visitor/evaluate.dart @@ -5,7 +5,7 @@ // DO NOT EDIT. This file was generated from async_evaluate.dart. // See tool/grind/synchronize.dart for details. // -// Checksum: 959805706b0be90eba25f04979b411dac85e648d +// Checksum: 3170c459abe92e4ef49982a797b2be4209d57616 // // ignore_for_file: unused_import