forked from sass/dart-sass
/
variable_declaration.dart
98 lines (82 loc) · 2.97 KB
/
variable_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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:meta/meta.dart';
import 'package:source_span/source_span.dart';
import '../../../exception.dart';
import '../../../logger.dart';
import '../../../parse/scss.dart';
import '../../../utils.dart';
import '../../../util/span.dart';
import '../../../visitor/interface/statement.dart';
import '../expression.dart';
import '../declaration.dart';
import '../statement.dart';
import 'silent_comment.dart';
/// A variable declaration.
///
/// This defines or sets a variable.
///
/// {@category AST}
@sealed
class VariableDeclaration implements Statement, SassDeclaration {
/// The namespace of the variable being set, or `null` if it's defined or set
/// without a namespace.
final String? namespace;
/// The name of the variable, with underscores converted to hyphens.
final String name;
/// The comment immediately preceding this declaration.
SilentComment? comment;
/// The value the variable is being assigned to.
final Expression expression;
/// Whether this is a guarded assignment.
///
/// Guarded assignments only happen if the variable is undefined or `null`.
final bool isGuarded;
/// Whether this is a global assignment.
///
/// Global assignments always affect only the global scope.
final bool isGlobal;
final FileSpan span;
/// The variable name as written in the document, without underscores
/// converted to hyphens and including the leading `$`.
///
/// This isn't particularly efficient, and should only be used for error
/// messages.
String get originalName => declarationName(span);
FileSpan get nameSpan {
var span = this.span;
if (namespace != null) span = span.withoutNamespace();
return span.initialIdentifier(includeLeading: 1);
}
FileSpan? get namespaceSpan =>
namespace == null ? null : span.initialIdentifier();
VariableDeclaration(this.name, this.expression, this.span,
{this.namespace, bool guarded = false, bool global = false, this.comment})
: isGuarded = guarded,
isGlobal = global {
if (namespace != null && global) {
throw ArgumentError(
"Other modules' members can't be defined with !global.");
}
}
/// Parses a variable declaration from [contents].
///
/// If passed, [url] is the name of the file from which [contents] comes.
///
/// Throws a [SassFormatException] if parsing fails.
///
/// @nodoc
@internal
factory VariableDeclaration.parse(String contents,
{Object? url, Logger? logger}) =>
ScssParser(contents, url: url, logger: logger).parseVariableDeclaration();
T accept<T>(StatementVisitor<T> visitor) =>
visitor.visitVariableDeclaration(this);
String toString() {
var buffer = StringBuffer();
if (namespace != null) buffer.write("$namespace.");
buffer.write("\$$name: $expression;");
return buffer.toString();
}
}