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

Add built-in variables e and pi. #907

Merged
merged 4 commits into from Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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.
Awjin marked this conversation as resolved.
Show resolved Hide resolved

## 1.24.0

* Add an optional `with` clause to the `@forward` rule. This works like the
Expand Down
5 changes: 4 additions & 1 deletion lib/src/functions/math.dart
Expand Up @@ -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.
Expand Down
18 changes: 14 additions & 4 deletions lib/src/module/built_in.dart
Expand Up @@ -17,19 +17,21 @@ class BuiltInModule<T extends AsyncCallable> implements Module<T> {
final Uri url;
final Map<String, T> functions;
final Map<String, T> mixins;
final Map<String, Value> variables;

List<Module<T>> get upstream => const [];
Map<String, Value> get variables => const {};
Map<String, AstNode> 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<T> functions, Iterable<T> mixins})
BuiltInModule(String name,
{Iterable<T> functions, Iterable<T> mixins, Map<String, Value> 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<String, T> _callableMap<T extends AsyncCallable>(
Expand All @@ -39,8 +41,16 @@ class BuiltInModule<T extends AsyncCallable> implements Module<T> {
: UnmodifiableMapView(
{for (var callable in callables) callable.name: callable}));

/// Returns a map from [variables]' names to their values.
static Map<String, V> _variableMap<V>(Map<String, V> variables) =>
Awjin marked this conversation as resolved.
Show resolved Hide resolved
UnmodifiableMapView(
variables == null ? {} : UnmodifiableMapView(variables));

void setVariable(String name, Value value, AstNode nodeWithSpan) {
throw SassScriptException("Undefined variable.");
if (!variables.containsKey(name)) {
throw SassScriptException("Undefined variable.");
}
throw SassScriptException("Cannot modify built-in variable \$$name.");
Awjin marked this conversation as resolved.
Show resolved Hide resolved
}

Module<T> cloneCss() => this;
Expand Down