forked from sass/dart-sass
/
declaration.dart
82 lines (70 loc) · 2.48 KB
/
declaration.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
72
73
74
75
76
77
78
79
80
81
82
// 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:charcode/charcode.dart';
import 'package:meta/meta.dart';
import 'package:source_span/source_span.dart';
import '../../../visitor/interface/statement.dart';
import '../expression.dart';
import '../expression/string.dart';
import '../interpolation.dart';
import '../statement.dart';
import 'parent.dart';
/// A declaration (that is, a `name: value` pair).
///
/// {@category AST}
@sealed
class Declaration extends ParentStatement {
/// The name of this declaration.
final Interpolation name;
/// The value of this declaration.
///
/// If [children] is `null`, this is never `null`. Otherwise, it may or may
/// not be `null`.
final Expression? value;
final FileSpan span;
/// Returns whether this is a CSS Custom Property declaration.
///
/// Note that this can return `false` for declarations that will ultimately be
/// serialized as custom properties if they aren't *parsed as* custom
/// properties, such as `#{--foo}: ...`.
///
/// If this is `true`, then `value` will be a [StringExpression].
///
/// @nodoc
@internal
bool get isCustomProperty => name.initialPlain.startsWith('--');
/// Creates a declaration with no children.
Declaration(this.name, this.value, this.span) : super(null) {
if (isCustomProperty && value is! StringExpression) {
throw ArgumentError(
'Declarations whose names begin with "--" must have StringExpression '
'values (was `$value` of type ${value.runtimeType}).');
}
}
/// Creates a declaration with children.
///
/// For these declarations, a value is optional.
Declaration.nested(this.name, Iterable<Statement> children, this.span,
{this.value})
: super(List.unmodifiable(children)) {
if (isCustomProperty && value is! StringExpression) {
throw ArgumentError(
'Declarations whose names begin with "--" may not be nested.');
}
}
T accept<T>(StatementVisitor<T> visitor) => visitor.visitDeclaration(this);
String toString() {
var buffer = StringBuffer();
buffer.write(name);
buffer.writeCharCode($colon);
if (value != null) {
if (!isCustomProperty) {
buffer.writeCharCode($space);
}
buffer.write("$value");
}
var children = this.children;
return children == null ? "$buffer;" : "$buffer {${children.join(" ")}}";
}
}