/
use_rule.dart
67 lines (54 loc) · 2.2 KB
/
use_rule.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2016 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:source_span/source_span.dart';
import 'package:tuple/tuple.dart';
import '../../../logger.dart';
import '../../../parse/scss.dart';
import '../../../visitor/interface/statement.dart';
import '../expression.dart';
import '../expression/string.dart';
import '../statement.dart';
/// A `@use` rule.
class UseRule implements Statement {
/// The URI of the module to use.
///
/// If this is relative, it's relative to the containing file.
final Uri url;
/// The namespace for members of the used module, or `null` if the members
/// can be accessed without a namespace.
final String namespace;
/// A map from variable names to their values and the spans for those
/// variables, used to configure the loaded modules.
final Map<String, Tuple2<Expression, FileSpan>> configuration;
final FileSpan span;
UseRule(this.url, this.namespace, this.span,
{Map<String, Tuple2<Expression, FileSpan>> configuration})
: configuration = Map.unmodifiable(configuration ?? const {});
/// Parses a `@use` rule from [contents].
///
/// If passed, [url] is the name of the file from which [contents] comes.
///
/// Throws a [SassFormatException] if parsing fails.
factory UseRule.parse(String contents, {url, Logger logger}) =>
ScssParser(contents, url: url, logger: logger).parseUseRule();
T accept<T>(StatementVisitor<T> visitor) => visitor.visitUseRule(this);
String toString() {
var buffer =
StringBuffer("@use ${StringExpression.quoteText(url.toString())}");
var basename = url.pathSegments.isEmpty ? "" : url.pathSegments.last;
var dot = basename.indexOf(".");
if (namespace != basename.substring(0, dot == -1 ? basename.length : dot)) {
buffer.write(" as ${namespace ?? "*"}");
}
if (configuration.isNotEmpty) {
buffer.write(" with (");
buffer.write(configuration.entries
.map((entry) => "\$${entry.key}: ${entry.value.item1}")
.join(", "));
buffer.write(")");
}
buffer.write(";");
return buffer.toString();
}
}