Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
3 changes: 3 additions & 0 deletions lib/src/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ void main() {
exports.Value = valueClass;
exports.SassBoolean = booleanClass;
exports.SassArgumentList = argumentListClass;
exports.SassCalculation = calculationClass;
exports.CalculationOperation = calculationOperationClass;
exports.CalculationInterpolation = calculationInterpolationClass;
exports.SassColor = colorClass;
exports.SassFunction = functionClass;
exports.SassList = listClass;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/node/exports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class Exports {
// Value APIs
external set Value(JSClass function);
external set SassArgumentList(JSClass function);
external set SassCalculation(JSClass function);
external set CalculationOperation(JSClass function);
external set CalculationInterpolation(JSClass function);
external set SassBoolean(JSClass function);
external set SassColor(JSClass function);
external set SassFunction(JSClass function);
Expand Down
10 changes: 10 additions & 0 deletions lib/src/node/reflection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ extension JSClassExtension on JSClass {
allowInteropCaptureThis((Object self, _, __, [___]) => inspect(self)));
}

/// Defines a static method with the given [name] and [body].
void defineStaticMethod(String name, Function body) {
setProperty(this, name, allowInteropNamed(name, body));
}

/// A shorthand for calling [defineStaticMethod] multiple times.
void defineStaticMethods(Map<String, Function> methods) {
methods.forEach(defineStaticMethod);
}

/// Defines a method with the given [name] and [body].
///
/// The [body] should take an initial `self` parameter, representing the
Expand Down
3 changes: 3 additions & 0 deletions lib/src/node/value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'reflection.dart';

export 'value/argument_list.dart';
export 'value/boolean.dart';
export 'value/calculation.dart';
export 'value/color.dart';
export 'value/function.dart';
export 'value/list.dart';
Expand All @@ -36,6 +37,8 @@ final JSClass valueClass = () {
'get': (Value self, num index) =>
index < 1 && index >= -1 ? self : undefined,
'assertBoolean': (Value self, [String? name]) => self.assertBoolean(name),
'assertCalculation': (Value self, [String? name]) =>
self.assertCalculation(name),
'assertColor': (Value self, [String? name]) => self.assertColor(name),
'assertFunction': (Value self, [String? name]) => self.assertFunction(name),
'assertMap': (Value self, [String? name]) => self.assertMap(name),
Expand Down
128 changes: 128 additions & 0 deletions lib/src/node/value/calculation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright 2023 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'package:collection/collection.dart';
import 'package:node_interop/js.dart';
import 'package:sass/src/node/immutable.dart';
import 'package:sass/src/node/utils.dart';

import '../../value.dart';
import '../reflection.dart';

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

/// The JavaScript `SassCalculation` class.
final JSClass calculationClass = () {
var jsClass =
createJSClass('sass.SassCalculation', (Object self, [Object? _]) {
jsThrow(JsError("new sass.SassCalculation() isn't allowed"));
});

jsClass.defineStaticMethods({
'calc': (Object argument) {
isCalculationValue(argument);
return SassCalculation.unsimplified('calc', [argument]);
},
'min': (Object arguments) {
var argList = jsToDartList(arguments).cast<Object>();
argList.forEach(isCalculationValue);
return SassCalculation.unsimplified('min', argList);
},
'max': (Object arguments) {
var argList = jsToDartList(arguments).cast<Object>();
argList.forEach(isCalculationValue);
return SassCalculation.unsimplified('max', argList);
},
'clamp': (Object min, [Object? value, Object? max]) {
if (value == null && max != null) {
jsThrow(JsError('`value` is undefined and `max` is not undefined'));
}
if ((value == null || max == null) &&
!(min is SassString && min.text.startsWith('var(')) &&
!(value is SassString && value.text.startsWith('var('))) {
jsThrow(JsError(
'`value` or `max` is undefined and neither `min` nor `value` is a SassString that begins with "var("'));
}
[min, value, max].whereNotNull().forEach(isCalculationValue);
return SassCalculation.unsimplified(
'clamp', [min, value, max].whereNotNull());
},
});

jsClass.defineMethods({
'assertCalculation': (SassCalculation self, [String? name]) => self,
});

jsClass.defineGetters({
// The `name` getter is included by default by `createJSClass`
'arguments': (SassCalculation self) => ImmutableList(self.arguments),
});

getJSClass(SassCalculation.unsimplified('calc', [SassNumber(1)]))
.injectSuperclass(jsClass);
return jsClass;
}();

/// The JavaScript CalculationOperation class
final JSClass calculationOperationClass = () {
var jsClass = createJSClass('sass.CalculationOperation',
(Object self, String strOperator, Object left, Object right) {
var operator = CalculationOperator.values
.firstWhereOrNull((value) => value.operator == strOperator);
if (operator == null) {
jsThrow(JsError('Invalid operator: $strOperator'));
}
isCalculationValue(left, checkUnquoted: false);
isCalculationValue(right, checkUnquoted: false);
return SassCalculation.operateInternal(operator, left, right,
inMinMax: false, simplify: false);
});

jsClass.defineMethods({
'equals': (CalculationOperation self, Object other) => self == other,
'hashCode': (CalculationOperation self) => self.hashCode,
});

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

getJSClass(SassCalculation.operateInternal(
CalculationOperator.plus, SassNumber(1), SassNumber(1),
inMinMax: false, simplify: false))
.injectSuperclass(jsClass);
return jsClass;
}();

/// The JavaScript CalculationInterpolation class
final JSClass calculationInterpolationClass = () {
var jsClass = createJSClass('sass.CalculationInterpolation',
(Object self, String value) => CalculationInterpolation(value));

jsClass.defineMethods({
'equals': (CalculationInterpolation self, Object other) => self == other,
'hashCode': (CalculationInterpolation self) => self.hashCode,
});

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

getJSClass(CalculationInterpolation('')).injectSuperclass(jsClass);
return jsClass;
}();