Skip to content

Commit

Permalink
Address review
Browse files Browse the repository at this point in the history
  • Loading branch information
jerivas committed Jul 14, 2023
1 parent e300e41 commit e70e5e3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
7 changes: 5 additions & 2 deletions lib/src/node/compile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,17 @@ Object simplify(Object value) => switch (value) {
value.name, // ...the calculation name
value.arguments.map(simplify).toList() // ...and simplified arguments
)) {
('calc', [var first, ...]) => first,
('calc', [var first]) => first,
('calc', _) =>
throw ArgumentError('calc() must contain a single argument.'),
('clamp', [var min, var value, var max]) =>
SassCalculation.clamp(min, value, max),
('clamp', _) =>
throw ArgumentError('clamp() requires exactly 3 arguments.'),
('min', var args) => SassCalculation.min(args),
('max', var args) => SassCalculation.max(args),
(var name, var args) => SassCalculation.unsimplified(name, args)
(var name, _) =>
throw ArgumentError('Unknown calculation function "$name".'),
},
CalculationOperation() => SassCalculation.operate(
value.operator, simplify(value.left), simplify(value.right)),
Expand Down
43 changes: 24 additions & 19 deletions lib/src/node/value/calculation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@ import '../../value.dart';
import '../reflection.dart';

/// Check that [arg] is a valid argument to a calculation function.
void assertCalculationValue(Object arg) {
if (arg is! SassNumber &&
arg is! SassString &&
arg is! SassCalculation &&
arg is! CalculationOperation &&
arg is! CalculationInterpolation) {
jsThrow(JsError('Argument `$arg` must be one of '
'SassNumber, SassString, SassCalculation, CalculationOperation, '
'CalculationInterpolation'));
}
if (arg is SassString && arg.hasQuotes) {
jsThrow(JsError('Argument `$arg` must be unquoted SassString'));
}
}

/// Check that [arg] is an unquoted string or interpolation
bool isValidClampArg(Object? arg) => ((arg is CalculationInterpolation) ||
(arg is SassString && !arg.hasQuotes));
void assertCalculationValue(Object arg) => switch (arg) {
(SassNumber() ||
SassString(hasQuotes: false) ||
SassCalculation() ||
CalculationOperation() ||
CalculationInterpolation()) =>
null,
_ => jsThrow(JsError(
'Argument `$arg` must be one of SassNumber, unquoted SassString, '
'SassCalculation, CalculationOperation, CalculationInterpolation')),
};

/// Check that [arg] is an unquoted string or interpolation.
bool isValidClampArg(Object? arg) => switch (arg) {
(CalculationInterpolation() || SassString(hasQuotes: false)) => true,
_ => false,
};

/// The JavaScript `SassCalculation` class.
final JSClass calculationClass = () {
Expand All @@ -54,7 +53,7 @@ final JSClass calculationClass = () {
},
'clamp': (Object min, [Object? value, Object? max]) {
if ((value == null && !isValidClampArg(min)) ||
(max == null) && !([min, value]).any(isValidClampArg)) {
(max == null) && !([min, value].any(isValidClampArg))) {
jsThrow(JsError('Expected at least one SassString or '
'CalculationInterpolation in `${[
min,
Expand Down Expand Up @@ -104,6 +103,8 @@ final JSClass calculationOperationClass = () {

jsClass.defineGetters({
'operator': (CalculationOperation self) => self.operator.operator,
'left': (CalculationOperation self) => self.left,
'right': (CalculationOperation self) => self.right,
});

getJSClass(SassCalculation.operateInternal(
Expand All @@ -123,6 +124,10 @@ final JSClass calculationInterpolationClass = () {
'hashCode': (CalculationInterpolation self) => self.hashCode,
});

jsClass.defineGetters({
'value': (CalculationInterpolation self) => self.value,
});

getJSClass(CalculationInterpolation('')).injectSuperclass(jsClass);
return jsClass;
}();
30 changes: 23 additions & 7 deletions lib/src/value/calculation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,11 @@ class SassCalculation extends Value {
/// {@category Value}
@sealed
class CalculationOperation {
/// We use a getters to allow overriding the logic in the JS API
/// implementation.
/// The operator.
CalculationOperator get operator {
// We use a getter to allow overriding the logic in the JS API
// implementation
return _operator;
}

Expand All @@ -341,15 +342,23 @@ class CalculationOperation {
///
/// This is either a [SassNumber], a [SassCalculation], an unquoted
/// [SassString], a [CalculationOperation], or a [CalculationInterpolation].
final Object left;
Object get left {
return _left;
}

final Object _left;

/// The right-hand operand.
///
/// This is either a [SassNumber], a [SassCalculation], an unquoted
/// [SassString], a [CalculationOperation], or a [CalculationInterpolation].
final Object right;
Object get right {
return _right;
}

CalculationOperation._(this._operator, this.left, this.right);
final Object _right;

CalculationOperation._(this._operator, this._left, this._right);

bool operator ==(Object other) =>
other is CalculationOperation &&
Expand Down Expand Up @@ -409,9 +418,16 @@ enum CalculationOperator {
/// {@category Value}
@sealed
class CalculationInterpolation {
final String value;
/// We use a getters to allow overriding the logic in the JS API
/// implementation.
String get value {
return _value;
}

final String _value;

CalculationInterpolation(this.value);
CalculationInterpolation(this._value);

bool operator ==(Object other) =>
other is CalculationInterpolation && value == other.value;
Expand Down

0 comments on commit e70e5e3

Please sign in to comment.