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 3 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
50 changes: 23 additions & 27 deletions lib/src/node/compile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,29 +227,25 @@ Importer _parseImporter(Object? importer) {

/// Implements the simplification algorithm for custom function return values.
/// {@link https://github.com/sass/sass/blob/main/spec/types/calculation.md#simplifying-a-calculationvalue}
dynamic simplify(dynamic value) {
if (value is SassCalculation) {
var simplifiedArgs = value.arguments.map(simplify).toList().cast<Object>();
if (value.name == 'calc') {
return simplifiedArgs[0];
}
if (value.name == 'clamp') {
return simplify(Function.apply(SassCalculation.clamp, simplifiedArgs));
}
if (value.name == 'min') {
return simplify(Function.apply(SassCalculation.min, [simplifiedArgs]));
}
if (value.name == 'max') {
return simplify(Function.apply(SassCalculation.max, [simplifiedArgs]));
}
return SassCalculation.unsimplified(value.name, simplifiedArgs);
}
if (value is CalculationOperation) {
return simplify(SassCalculation.operate(value.operator,
simplify(value.left) as Object, simplify(value.right) as Object));
}
return value;
}
Object simplify(Object value) => switch (value) {
jerivas marked this conversation as resolved.
Show resolved Hide resolved
SassCalculation() => switch ((
// Match against...
value.name, // ...the calculation name
value.arguments.map(simplify).toList() // ...and simplified arguments
)) {
('calc', [var first, ...]) => first,
jerivas marked this conversation as resolved.
Show resolved Hide resolved
('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)
jerivas marked this conversation as resolved.
Show resolved Hide resolved
},
CalculationOperation() => SassCalculation.operate(
value.operator, simplify(value.left), simplify(value.right)),
_ => value,
};

/// Parses `functions` from [record] into a list of [Callable]s or
/// [AsyncCallable]s.
Expand All @@ -264,8 +260,8 @@ List<AsyncCallable> _parseFunctions(Object? functions, {bool asynch = false}) {
if (!asynch) {
late Callable callable;
callable = Callable.fromSignature(signature, (arguments) {
var result = (callback as Function)(toJSArray(arguments));
result = simplify(result);
var result =
simplify((callback as Function)(toJSArray(arguments)) as Object);
if (result is Value) return result;
if (isPromise(result)) {
throw 'Invalid return value for custom function '
Expand All @@ -286,8 +282,8 @@ List<AsyncCallable> _parseFunctions(Object? functions, {bool asynch = false}) {
result = await promiseToFuture<Object>(result as Promise);
}

result = simplify(result);
if (result is Value) return result;
var simplified = simplify(result as Object);
if (simplified is Value) return simplified;
throw 'Invalid return value for custom function '
'"${callable.name}": $result is not a sass.Value.';
});
Expand Down
38 changes: 16 additions & 22 deletions lib/src/node/value/calculation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,24 @@ import '../../value.dart';
import '../reflection.dart';

/// Check that [arg] is a valid argument to a calculation function.
void assertCalculationValue(Object arg, {bool checkUnquoted = true}) {
void assertCalculationValue(Object arg) {
if (arg is! SassNumber &&
arg is! SassString &&
arg is! SassCalculation &&
arg is! CalculationOperation &&
arg is! CalculationInterpolation) {
jsThrow(JsError('Argument must be one of '
jsThrow(JsError('Argument `$arg` must be one of '
'SassNumber, SassString, SassCalculation, CalculationOperation, '
'CalculationInterpolation'));
}
if (checkUnquoted && arg is SassString && arg.hasQuotes) {
jsThrow(JsError('Argument must be unquoted SassString'));
}
if (arg is CalculationOperation) {
assertCalculationValue(arg.left, checkUnquoted: checkUnquoted);
assertCalculationValue(arg.right, checkUnquoted: checkUnquoted);
if (arg is SassString && arg.hasQuotes) {
jerivas marked this conversation as resolved.
Show resolved Hide resolved
jsThrow(JsError('Argument `$arg` must be unquoted SassString'));
}
}

/// Check that [arg] is an unquoted string or interpolation
void assertUnquotedStringOrInterpolation(Object arg) {
if ((arg is! SassString && arg is! CalculationInterpolation) ||
(arg is SassString && arg.hasQuotes)) {
jsThrow(JsError(
'Argument must be an unquoted SassString or CalculationInterpolation'));
}
}
bool isValidClampArg(Object? arg) => ((arg is CalculationInterpolation) ||
(arg is SassString && !arg.hasQuotes));

/// The JavaScript `SassCalculation` class.
final JSClass calculationClass = () {
Expand All @@ -62,11 +53,14 @@ final JSClass calculationClass = () {
return SassCalculation.unsimplified('max', argList);
},
'clamp': (Object min, [Object? value, Object? max]) {
if (value == null) {
assertUnquotedStringOrInterpolation(min);
} else if (max == null) {
assertUnquotedStringOrInterpolation(min);
assertUnquotedStringOrInterpolation(value);
if ((value == null && !isValidClampArg(min)) ||
(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,
value,
max
].whereNotNull()}`'));
}
[min, value, max].whereNotNull().forEach(assertCalculationValue);
return SassCalculation.unsimplified(
Expand Down Expand Up @@ -97,8 +91,8 @@ final JSClass calculationOperationClass = () {
if (operator == null) {
jsThrow(JsError('Invalid operator: $strOperator'));
}
assertCalculationValue(left, checkUnquoted: false);
assertCalculationValue(right, checkUnquoted: false);
assertCalculationValue(left);
assertCalculationValue(right);
return SassCalculation.operateInternal(operator, left, right,
inMinMax: false, simplify: false);
});
Expand Down