Skip to content

Commit 2cb69e7

Browse files
author
Awjin Ahn
authored
Add built-in variables e and pi. (#907)
1 parent 24f84e2 commit 2cb69e7

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* `hypot()`: given *n* numbers, outputs the length of the *n*-dimensional
77
vector that has components equal to each of the inputs.
88

9+
* Add the variables `$pi` and `$e` to the built-in "sass:math" module.
10+
911
## 1.24.0
1012

1113
* Add an optional `with` clause to the `@forward` rule. This works like the

lib/src/functions/math.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ final global = UnmodifiableListView([
2525
final module = BuiltInModule("math", functions: [
2626
_abs, _ceil, _clamp, _compatible, _floor, _hypot, _isUnitless, _max, _min, //
2727
_percentage, _randomFunction, _round, _unit,
28-
]);
28+
], variables: {
29+
"e": SassNumber(math.e),
30+
"pi": SassNumber(math.pi),
31+
});
2932

3033
/// Returns a [Callable] named [name] that transforms a number's value
3134
/// using [transform] and preserves its units.

lib/src/module/built_in.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,22 @@ class BuiltInModule<T extends AsyncCallable> implements Module<T> {
1717
final Uri url;
1818
final Map<String, T> functions;
1919
final Map<String, T> mixins;
20+
final Map<String, Value> variables;
2021

2122
List<Module<T>> get upstream => const [];
22-
Map<String, Value> get variables => const {};
2323
Map<String, AstNode> get variableNodes => const {};
2424
Extender get extender => Extender.empty;
2525
CssStylesheet get css => CssStylesheet.empty(url: url);
2626
bool get transitivelyContainsCss => false;
2727
bool get transitivelyContainsExtensions => false;
2828

29-
BuiltInModule(String name, {Iterable<T> functions, Iterable<T> mixins})
29+
BuiltInModule(String name,
30+
{Iterable<T> functions, Iterable<T> mixins, Map<String, Value> variables})
3031
: url = Uri(scheme: "sass", path: name),
3132
functions = _callableMap(functions),
32-
mixins = _callableMap(mixins);
33+
mixins = _callableMap(mixins),
34+
variables =
35+
variables == null ? const {} : UnmodifiableMapView(variables);
3336

3437
/// Returns a map from [callables]' names to their values.
3538
static Map<String, T> _callableMap<T extends AsyncCallable>(
@@ -40,7 +43,10 @@ class BuiltInModule<T extends AsyncCallable> implements Module<T> {
4043
{for (var callable in callables) callable.name: callable}));
4144

4245
void setVariable(String name, Value value, AstNode nodeWithSpan) {
43-
throw SassScriptException("Undefined variable.");
46+
if (!variables.containsKey(name)) {
47+
throw SassScriptException("Undefined variable.");
48+
}
49+
throw SassScriptException("Cannot modify built-in variable.");
4450
}
4551

4652
Module<T> cloneCss() => this;

0 commit comments

Comments
 (0)