/
use_rule.dart
71 lines (58 loc) · 2.29 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
68
69
70
71
// 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 '../../../exception.dart';
import '../../../logger.dart';
import '../../../parse/scss.dart';
import '../../../visitor/interface/statement.dart';
import '../configured_variable.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 list of variable assignments used to configure the loaded modules.
final List<ConfiguredVariable> configuration;
final FileSpan span;
UseRule(this.url, this.namespace, this.span,
{Iterable<ConfiguredVariable>? configuration})
: configuration = configuration == null
? const []
: List<ConfiguredVariable>.unmodifiable(configuration) {
for (var variable in this.configuration) {
if (variable.isGuarded) {
throw ArgumentError.value(variable, "configured variable",
"can't be guarded in a @use rule.");
}
}
}
/// 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, {Object? 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 (${configuration.join(", ")})");
}
buffer.write(";");
return buffer.toString();
}
}