From afc39509537293a2c135fece0eece6b2e8c95ce5 Mon Sep 17 00:00:00 2001 From: awjin Date: Thu, 26 Dec 2019 13:02:46 -0800 Subject: [PATCH] Add built-in variables e and pi. See https://github.com/sass/sass-spec/pull/1501 --- CHANGELOG.md | 2 ++ lib/src/functions/math.dart | 5 ++++- lib/src/module/built_in.dart | 15 +++++++++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a47dc2b31..a1283ad33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ * `hypot()`: given *n* numbers, outputs the length of the *n*-dimensional vector that has components equal to each of the inputs. +* Add the variables `pi` and `e` to the built-in "sass:math" module. + ## 1.24.0 * Add an optional `with` clause to the `@forward` rule. This works like the diff --git a/lib/src/functions/math.dart b/lib/src/functions/math.dart index e0a106183..84d81ff9c 100644 --- a/lib/src/functions/math.dart +++ b/lib/src/functions/math.dart @@ -25,7 +25,10 @@ final global = UnmodifiableListView([ final module = BuiltInModule("math", functions: [ _abs, _ceil, _clamp, _compatible, _floor, _hypot, _isUnitless, _max, _min, // _percentage, _randomFunction, _round, _unit, -]); +], variables: { + "e": SassNumber(math.e), + "pi": SassNumber(math.pi), +}); /// Returns a [Callable] named [name] that transforms a number's value /// using [transform] and preserves its units. diff --git a/lib/src/module/built_in.dart b/lib/src/module/built_in.dart index 63d098687..f1a1f50eb 100644 --- a/lib/src/module/built_in.dart +++ b/lib/src/module/built_in.dart @@ -17,19 +17,21 @@ class BuiltInModule implements Module { final Uri url; final Map functions; final Map mixins; + final Map variables; List> get upstream => const []; - Map get variables => const {}; Map get variableNodes => const {}; Extender get extender => Extender.empty; CssStylesheet get css => CssStylesheet.empty(url: url); bool get transitivelyContainsCss => false; bool get transitivelyContainsExtensions => false; - BuiltInModule(String name, {Iterable functions, Iterable mixins}) + BuiltInModule(String name, + {Iterable functions, Iterable mixins, Map variables}) : url = Uri(scheme: "sass", path: name), functions = _callableMap(functions), - mixins = _callableMap(mixins); + mixins = _callableMap(mixins), + variables = _variableMap(variables); /// Returns a map from [callables]' names to their values. static Map _callableMap( @@ -39,8 +41,13 @@ class BuiltInModule implements Module { : UnmodifiableMapView( {for (var callable in callables) callable.name: callable})); + /// Returns a map from [variables]' names to their values. + static Map _variableMap(Map variables) => + UnmodifiableMapView( + variables == null ? {} : UnmodifiableMapView(variables)); + void setVariable(String name, Value value, AstNode nodeWithSpan) { - throw SassScriptException("Undefined variable."); + throw SassScriptException("Cannot modify built-in variable \$$name."); } Module cloneCss() => this;