Skip to content

Commit

Permalink
Update modulo function to return SassNumber and corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
pamelalozano16 committed Jul 14, 2023
1 parent 2496ed1 commit 91d8272
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
47 changes: 25 additions & 22 deletions lib/src/value/calculation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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].
///
Expand All @@ -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].
///
Expand All @@ -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].
///
Expand All @@ -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].
///
Expand All @@ -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].
///
Expand All @@ -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].
///
Expand All @@ -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].
///
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]);

Expand Down Expand Up @@ -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].
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/value/number.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/value/number/unitless.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/visitor/evaluate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 91d8272

Please sign in to comment.