Skip to content
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

Expose calculations in JS API #1988

Merged
merged 28 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f5ce090
Expose calculations in JS API
jerivas Jun 6, 2023
fbfe7f1
Merge branch 'main' into js-api-calculations
jgerigmeyer Jun 7, 2023
8c9a7d8
Use strings for CalculationOperator in JS
jerivas Jun 7, 2023
64d50c8
Merge branch 'main' into js-api-calculations
jgerigmeyer Jun 8, 2023
fb9bb04
Update date
jerivas Jun 8, 2023
3f7b3ab
Fix call stack errors in JS
jerivas Jun 9, 2023
bdb6773
Refactor static methods
jerivas Jun 9, 2023
f09910f
Return an immutable list of arguments
jerivas Jun 9, 2023
13b4f33
No need to define left/right
jerivas Jun 13, 2023
71aa9a2
Fix call stack errors on operator access
jerivas Jun 16, 2023
6277580
Export calculations classes to the browser
jerivas Jun 17, 2023
60243ca
Simplify custom function return values
jerivas Jun 22, 2023
c63fa7e
lint
jerivas Jun 22, 2023
70e567e
Parse `value` and `max` from `min`
jerivas Jul 6, 2023
b4fff6c
Merge branch 'main' into js-api-calculations
jerivas Jul 6, 2023
b4e9ade
Address changes to the clamp spec
jerivas Jul 11, 2023
024c239
Improve simplification implementation
jerivas Jul 12, 2023
399dd25
Update after spec changes
jerivas Jul 13, 2023
b292fb8
Streamline simplification
jerivas Jul 13, 2023
e300e41
Check for Value before and after simplification
jerivas Jul 13, 2023
e70e5e3
Address review
jerivas Jul 14, 2023
d30acbd
Uniform error messages
jerivas Jul 18, 2023
cd06089
Address review
jerivas Jul 18, 2023
f3d169e
Clean up
jerivas Jul 18, 2023
6b3b5b8
Test assertCalculation in other types
jerivas Jul 18, 2023
52ab266
Add basic calculation tests
jerivas Jul 19, 2023
2928f3e
Update pubspec and changelog
nex3 Jul 19, 2023
859069e
Merge branch 'main' into js-api-calculations
nex3 Jul 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 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 All @@ -260,18 +263,24 @@ List<AsyncCallable> _parseFunctions(Object? functions, {bool asynch = false}) {
if (!asynch) {
late Callable callable;
callable = Callable.fromSignature(signature, (arguments) {
var result =
simplify((callback as Function)(toJSArray(arguments)) as Object);
if (result is Value) return result;
var result = (callback as Function)(toJSArray(arguments));
if (isPromise(result)) {
throw 'Invalid return value for custom function '
'"${callable.name}":\n'
'Promises may only be returned for sass.compileAsync() and '
'sass.compileStringAsync().';
} else {
throw 'Invalid return value for custom function '
'"${callable.name}": $result is not a sass.Value.';
}
if (result is Value) {
var simplified = simplify(result);
if (simplified is! Value) {
throw 'Custom function "${callable.name}" '
'returned an object that cannot be simplified to a '
'sass.Value.';
jerivas marked this conversation as resolved.
Show resolved Hide resolved
}
return simplified;
}
throw 'Invalid return value for custom function '
'"${callable.name}": $result is not a sass.Value.';
});
result.add(callable);
} else {
Expand All @@ -281,9 +290,15 @@ List<AsyncCallable> _parseFunctions(Object? functions, {bool asynch = false}) {
if (isPromise(result)) {
result = await promiseToFuture<Object>(result as Promise);
}

var simplified = simplify(result as Object);
if (simplified is Value) return simplified;
if (result is Value) {
var simplified = simplify(result);
if (simplified is! Value) {
throw 'Custom function "${callable.name}" '
'returned an object that cannot be simplified to a '
'sass.Value.';
}
return simplified;
}
throw 'Invalid return value for custom function '
'"${callable.name}": $result is not a sass.Value.';
});
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) {
jerivas marked this conversation as resolved.
Show resolved Hide resolved
(SassNumber() ||
SassString(hasQuotes: false) ||
SassCalculation() ||
CalculationOperation() ||
CalculationInterpolation()) =>
jerivas marked this conversation as resolved.
Show resolved Hide resolved
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))) {
jerivas marked this conversation as resolved.
Show resolved Hide resolved
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,
jerivas marked this conversation as resolved.
Show resolved Hide resolved
jerivas marked this conversation as resolved.
Show resolved Hide resolved
'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;
jerivas marked this conversation as resolved.
Show resolved Hide resolved

/// 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
Loading