/
utils.dart
493 lines (430 loc) · 16.5 KB
/
utils.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// 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 'dart:math' as math;
import 'package:charcode/charcode.dart';
import 'package:collection/collection.dart';
import 'package:source_span/source_span.dart';
import 'package:stack_trace/stack_trace.dart';
import 'package:string_scanner/string_scanner.dart';
import 'package:term_glyph/term_glyph.dart' as glyph;
import 'package:tuple/tuple.dart';
import 'ast/sass.dart';
import 'exception.dart';
import 'parse/scss.dart';
import 'util/character.dart';
/// The URL used in stack traces when no source URL is available.
final _noSourceUrl = Uri.parse("-");
/// Stack traces associated with exceptions thrown with [throwWithTrace].
final _traces = Expando<StackTrace>();
/// Converts [iter] into a sentence, separating each word with [conjunction].
String toSentence(Iterable<Object> iter, [String? conjunction]) {
conjunction ??= "and";
if (iter.length == 1) return iter.first.toString();
return iter.exceptLast.join(", ") + " $conjunction ${iter.last}";
}
/// Returns [string] with every line indented [indentation] spaces.
String indent(String string, int indentation) =>
string.split("\n").map((line) => (" " * indentation) + line).join("\n");
/// Returns [name] if [number] is 1, or the plural of [name] otherwise.
///
/// By default, this just adds "s" to the end of [name] to get the plural. If
/// [plural] is passed, that's used instead.
String pluralize(String name, int number, {String? plural}) {
if (number == 1) return name;
if (plural != null) return plural;
return '${name}s';
}
/// Returns `a $word` or `an $word` depending on whether [word] starts with a
/// vowel.
String a(String word) =>
[$a, $e, $i, $o, $u].contains(word.codeUnitAt(0)) ? "an $word" : "a $word";
/// Returns a bulleted list of items in [bullets].
String bulletedList(Iterable<String> bullets) {
return bullets.map((element) {
var lines = element.split("\n");
return "${glyph.bullet} ${lines.first}" +
(lines.length > 1 ? "\n" + indent(lines.skip(1).join("\n"), 2) : "");
}).join("\n");
}
/// Returns the number of times [codeUnit] appears in [string].
int countOccurrences(String string, int codeUnit) {
var count = 0;
for (var i = 0; i < string.length; i++) {
if (string.codeUnitAt(i) == codeUnit) count++;
}
return count;
}
/// Like [String.trim], but only trims ASCII whitespace.
///
/// If [excludeEscape] is `true`, this doesn't trim whitespace included in a CSS
/// escape.
String trimAscii(String string, {bool excludeEscape = false}) {
var start = _firstNonWhitespace(string);
return start == null
? ""
: string.substring(
start, _lastNonWhitespace(string, excludeEscape: excludeEscape)! + 1);
}
/// Like [String.trimLeft], but only trims ASCII whitespace.
String trimAsciiLeft(String string) {
var start = _firstNonWhitespace(string);
return start == null ? "" : string.substring(start);
}
/// Like [String.trimRight], but only trims ASCII whitespace.
///
/// If [excludeEscape] is `true`, this doesn't trim whitespace included in a CSS
/// escape.
String trimAsciiRight(String string, {bool excludeEscape = false}) {
var end = _lastNonWhitespace(string, excludeEscape: excludeEscape);
return end == null ? "" : string.substring(0, end + 1);
}
/// Returns the index of the first character in [string] that's not ASCII
/// whitespace, or [null] if [string] is entirely spaces.
int? _firstNonWhitespace(String string) {
for (var i = 0; i < string.length; i++) {
if (!isWhitespace(string.codeUnitAt(i))) return i;
}
return null;
}
/// Returns the index of the last character in [string] that's not ASCII
/// whitespace, or [null] if [string] is entirely spaces.
///
/// If [excludeEscape] is `true`, this doesn't move past whitespace that's
/// included in a CSS escape.
int? _lastNonWhitespace(String string, {bool excludeEscape = false}) {
for (var i = string.length - 1; i >= 0; i--) {
var codeUnit = string.codeUnitAt(i);
if (!isWhitespace(codeUnit)) {
if (excludeEscape &&
i != 0 &&
i != string.length &&
codeUnit == $backslash) {
return i + 1;
} else {
return i;
}
}
}
return null;
}
/// Returns whether [member] is a public member name.
///
/// Assumes that [member] is a valid Sass identifier.
bool isPublic(String member) {
var start = member.codeUnitAt(0);
return start != $dash && start != $underscore;
}
/// Flattens the first level of nested arrays in [iterable].
///
/// The return value is ordered first by index in the nested iterable, then by
/// the index *of* that iterable in [iterable]. For example,
/// `flattenVertically([["1a", "1b"], ["2a", "2b"]])` returns `["1a", "2a",
/// "1b", "2b"]`.
List<T> flattenVertically<T>(Iterable<Iterable<T>> iterable) {
var queues = iterable.map((inner) => QueueList.from(inner)).toList();
if (queues.length == 1) return queues.first;
var result = <T>[];
while (queues.isNotEmpty) {
queues.removeWhere((queue) {
result.add(queue.removeFirst());
return queue.isEmpty;
});
}
return result;
}
/// Returns the first element of [iterable], or `null` if the iterable is empty.
// TODO(nweiz): Use package:collection
T? firstOrNull<T>(Iterable<T> iterable) {
var iterator = iterable.iterator;
return iterator.moveNext() ? iterator.current : null;
}
/// Converts [codepointIndex] to a code unit index, relative to [string].
///
/// A codepoint index is the index in pure Unicode codepoints; a code unit index
/// is an index into a UTF-16 string.
int codepointIndexToCodeUnitIndex(String string, int codepointIndex) {
var codeUnitIndex = 0;
for (var i = 0; i < codepointIndex; i++) {
if (isHighSurrogate(string.codeUnitAt(codeUnitIndex++))) codeUnitIndex++;
}
return codeUnitIndex;
}
/// Converts [codeUnitIndex] to a codepoint index, relative to [string].
///
/// A codepoint index is the index in pure Unicode codepoints; a code unit index
/// is an index into a UTF-16 string.
int codeUnitIndexToCodepointIndex(String string, int codeUnitIndex) {
var codepointIndex = 0;
for (var i = 0; i < codeUnitIndex; i++) {
codepointIndex++;
if (isHighSurrogate(string.codeUnitAt(i))) i++;
}
return codepointIndex;
}
/// Returns whether [iterable1] and [iterable2] have the same contents.
bool iterableEquals(Iterable<Object> iterable1, Iterable<Object> iterable2) =>
const IterableEquality<Object>().equals(iterable1, iterable2);
/// Returns a hash code for [iterable] that matches [iterableEquals].
int iterableHash(Iterable<Object> iterable) =>
const IterableEquality<Object>().hash(iterable);
/// Returns whether [list1] and [list2] have the same contents.
bool listEquals(List<Object?>? list1, List<Object?>? list2) =>
const ListEquality<Object?>().equals(list1, list2);
/// Returns a hash code for [list] that matches [listEquals].
int listHash(List<Object> list) => const ListEquality<Object>().hash(list);
/// Returns whether [map1] and [map2] have the same contents.
bool mapEquals(Map<Object, Object> map1, Map<Object, Object> map2) =>
const MapEquality<Object, Object>().equals(map1, map2);
/// Returns a hash code for [map] that matches [mapEquals].
int mapHash(Map<Object, Object> map) =>
const MapEquality<Object, Object>().hash(map);
/// Returns a stack frame for the given [span] with the given [member] name.
///
/// By default, the frame's URL is set to `span.sourceUrl`. However, if [url] is
/// passed, it's used instead.
Frame frameForSpan(SourceSpan span, String member, {Uri? url}) => Frame(
url ?? span.sourceUrl ?? _noSourceUrl,
span.start.line + 1,
span.start.column + 1,
member);
/// Returns the variable name (including the leading `$`) from a [span] that
/// covers a variable declaration, which includes the variable name as well as
/// the colon and expression following it.
///
/// This isn't particularly efficient, and should only be used for error
/// messages.
String declarationName(FileSpan span) {
var text = span.text;
return trimAsciiRight(text.substring(0, text.indexOf(":")));
}
/// Returns [name] without a vendor prefix.
///
/// If [name] has no vendor prefix, it's returned as-is.
String unvendor(String name) {
if (name.length < 2) return name;
if (name.codeUnitAt(0) != $dash) return name;
if (name.codeUnitAt(1) == $dash) return name;
for (var i = 2; i < name.length; i++) {
if (name.codeUnitAt(i) == $dash) return name.substring(i + 1);
}
return name;
}
/// Returns whether [string1] and [string2] are equal, ignoring ASCII case.
bool equalsIgnoreCase(String? string1, String? string2) {
if (identical(string1, string2)) return true;
if (string1 == null || string2 == null) return false;
if (string1.length != string2.length) return false;
for (var i = 0; i < string1.length; i++) {
if (!characterEqualsIgnoreCase(
string1.codeUnitAt(i), string2.codeUnitAt(i))) {
return false;
}
}
return true;
}
/// Returns whether [string] starts with [prefix], ignoring ASCII case.
bool startsWithIgnoreCase(String string, String prefix) {
if (string.length < prefix.length) return false;
for (var i = 0; i < prefix.length; i++) {
if (!characterEqualsIgnoreCase(
string.codeUnitAt(i), prefix.codeUnitAt(i))) {
return false;
}
}
return true;
}
/// Destructively updates every element of [list] with the result of [function].
void mapInPlace<T>(List<T> list, T function(T element)) {
for (var i = 0; i < list.length; i++) {
list[i] = function(list[i]);
}
}
/// Returns the longest common subsequence between [list1] and [list2].
///
/// If there are more than one equally long common subsequence, returns the one
/// which starts first in [list1].
///
/// If [select] is passed, it's used to check equality between elements in each
/// list. If it returns `null`, the elements are considered unequal; otherwise,
/// it should return the element to include in the return value.
List<T> longestCommonSubsequence<T>(List<T> list1, List<T> list2,
{T? select(T element1, T element2)?}) {
select ??= (element1, element2) => element1 == element2 ? element1 : null;
var lengths = List.generate(
list1.length + 1, (_) => List.filled(list2.length + 1, 0),
growable: false);
var selections = List<List<T?>>.generate(
list1.length, (_) => List<T?>.filled(list2.length, null),
growable: false);
for (var i = 0; i < list1.length; i++) {
for (var j = 0; j < list2.length; j++) {
var selection = select(list1[i], list2[j]);
selections[i][j] = selection;
lengths[i + 1][j + 1] = selection == null
? math.max(lengths[i + 1][j], lengths[i][j + 1])
: lengths[i][j] + 1;
}
}
List<T> backtrack(int i, int j) {
if (i == -1 || j == -1) return [];
var selection = selections[i][j];
if (selection != null) return backtrack(i - 1, j - 1)..add(selection);
return lengths[i + 1][j] > lengths[i][j + 1]
? backtrack(i, j - 1)
: backtrack(i - 1, j);
}
return backtrack(list1.length - 1, list2.length - 1);
}
/// Removes the first value in [list] that matches [test].
///
/// If [orElse] is passed, calls it if no value matches.
void removeFirstWhere<T>(List<T> list, bool test(T value), {void orElse()?}) {
for (var i = 0; i < list.length; i++) {
if (!test(list[i])) continue;
list.removeAt(i);
return;
}
if (orElse != null) orElse();
}
/// Like [Map.addAll], but for two-layer maps.
///
/// This avoids copying inner maps from [source] if possible.
void mapAddAll2<K1, K2, V>(
Map<K1, Map<K2, V>> destination, Map<K1, Map<K2, V>> source) {
source.forEach((key, inner) {
var innerDestination = destination[key];
if (innerDestination != null) {
innerDestination.addAll(inner);
} else {
destination[key] = inner;
}
});
}
/// Sets all [keys] in [map] to [value].
void setAll<K, V>(Map<K, V> map, Iterable<K> keys, V value) {
for (var key in keys) {
map[key] = value;
}
}
/// Rotates the element in list from [start] (inclusive) to [end] (exclusive)
/// one index higher, looping the final element back to [start].
void rotateSlice(List<Object> list, int start, int end) {
var element = list[end - 1];
for (var i = start; i < end; i++) {
var next = list[i];
list[i] = element;
element = next;
}
}
/// Like [Iterable.map] but for an asynchronous [callback].
Future<Iterable<F>> mapAsync<E, F>(
Iterable<E> iterable, Future<F> callback(E value)) async =>
[for (var element in iterable) await callback(element)];
/// Like [Map.putIfAbsent], but for an asynchronous [ifAbsent].
///
/// Note that this is *not* safe to call in parallel on the same map with the
/// same key.
Future<V> putIfAbsentAsync<K, V>(
Map<K, V> map, K key, Future<V> ifAbsent()) async {
if (map.containsKey(key)) return map[key] as V;
var value = await ifAbsent();
map[key] = value;
return value;
}
/// Returns a deep copy of a map that contains maps.
Map<K1, Map<K2, V>> copyMapOfMap<K1, K2, V>(Map<K1, Map<K2, V>> map) =>
{for (var entry in map.entries) entry.key: Map.of(entry.value)};
/// Returns a deep copy of a map that contains lists.
Map<K, List<E>> copyMapOfList<K, E>(Map<K, List<E>> map) =>
{for (var entry in map.entries) entry.key: entry.value.toList()};
/// Consumes an escape sequence from [scanner] and returns the character it
/// represents.
int consumeEscapedCharacter(StringScanner scanner) {
// See https://drafts.csswg.org/css-syntax-3/#consume-escaped-code-point.
scanner.expectChar($backslash);
var first = scanner.peekChar();
if (first == null) {
return 0xFFFD;
} else if (isNewline(first)) {
scanner.error("Expected escape sequence.");
} else if (isHex(first)) {
var value = 0;
for (var i = 0; i < 6; i++) {
var next = scanner.peekChar();
if (next == null || !isHex(next)) break;
value = (value << 4) + asHex(scanner.readChar());
}
if (isWhitespace(scanner.peekChar())) scanner.readChar();
if (value == 0 ||
(value >= 0xD800 && value <= 0xDFFF) ||
value >= 0x10FFFF) {
return 0xFFFD;
} else {
return value;
}
} else {
return scanner.readChar();
}
}
// TODO(nweiz): Use a built-in solution for this when dart-lang/sdk#10297 is
// fixed.
/// Throws [error] with [trace] stored as its stack trace.
///
/// Note that [trace] is only accessible via [getTrace].
Never throwWithTrace(Object error, StackTrace trace) {
attachTrace(error, trace);
throw error;
}
/// Attaches [trace] to [error] so that it may be retrieved using [getTrace].
///
/// In most cases, [throwWithTrace] should be used instead of this.
void attachTrace(Object error, StackTrace trace) {
if (error is String || error is num || error is bool) return;
// Non-`Error` objects thrown in Node will have empty stack traces. We don't
// want to store these because they don't have any useful information.
if (trace.toString().isEmpty) return;
_traces[error] ??= trace;
}
/// Returns the stack trace associated with error using [throwWithTrace], or
/// [defaultTrace] if it was thrown normally.
StackTrace? getTrace(Object error) =>
error is String || error is num || error is bool ? null : _traces[error];
extension MapExtension<K, V> on Map<K, V> {
/// If [this] doesn't contain the given [key], sets that key to [value] and
/// returns it.
///
/// Otherwise, calls [merge] with the existing value and [value] and sets
/// [key] to the result.
V putOrMerge(K key, V value, V Function(V oldValue, V newValue) merge) =>
containsKey(key)
? this[key] = merge(this[key] as V, value)
: this[key] = value;
}
extension IterableExtension<E> on Iterable<E> {
/// Returns a view of this list that covers all elements except the last.
///
/// Note this is only efficient for an iterable with a known length.
Iterable<E> get exceptLast {
var size = length - 1;
if (size < 0) throw StateError('Iterable may not be empty');
return take(size);
}
}
/// Parses a function signature of the format allowed by Node Sass's functions
/// option and returns its name and declaration.
///
/// If [requireParens] is `false`, this allows parentheses to be omitted.
///
/// Throws a [SassFormatException] if parsing fails.
Tuple2<String, ArgumentDeclaration> parseSignature(String signature,
{bool requireParens = true}) {
try {
return ScssParser(signature).parseSignature(requireParens: requireParens);
} on SassFormatException catch (error, stackTrace) {
throwWithTrace(
SassFormatException(
'Invalid signature "$signature": ${error.message}', error.span),
stackTrace);
}
}