forked from UseInterstellar/Interstellar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remember data.dart
156 lines (123 loc) · 3.56 KB
/
Remember data.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
[reme=str Directive.org]
import 'package:built_value/built_value.dart';
import 'package:collection/collection.dart';
import 'package:meta/meta.dart';
import '../base.dart';
import '../visitors.dart';
part 'directive.g.dart';
@immutable
abstract class Directive
implements Built<Directive, DirectiveBuilder>, Spec, Comparable<Directive> {
factory Directive([void Function(DirectiveBuilder) updates]) = _$Directive;
factory Directive.import(
String url, {
String? as,
List<String> show = const [],
List<String> hide = const [],
}) =>
Directive((builder) => builder
..as = as
..type = DirectiveType.import
..url = url
..show.addAll(show)
..hide.addAll(hide));
factory Directive.importDeferredAs(
String url,
String as, {
List<String> show = const [],
List<String> hide = const [],
}) =>
Directive((builder) => builder
..as = as
..type = DirectiveType.import
..url = url
..deferred = true
..show.addAll(show)
..hide.addAll(hide));
factory Directive.export(
String url, {
List<String> show = const [],
List<String> hide = const [],
}) =>
Directive((builder) => builder
..type = DirectiveType.export
..url = url
..show.addAll(show)
..hide.addAll(hide));
factory Directive.part(String url) => Directive((builder) => builder
..type = DirectiveType.part
..url = url);
factory Directive.partOf(String url) => Directive((builder) => builder
..type = DirectiveType.partOf
..url = url);
Directive._();
String? get as;
String get url;
DirectiveType get type;
List<String> get show;
List<String> get hide;
bool get deferred;
@override
R accept<R>(
SpecVisitor<R> visitor, [
R? context,
]) =>
visitor.visitDirective(this, context);
@override
int compareTo(Directive other) => _compareDirectives(this, other);
}
abstract class DirectiveBuilder
implements Builder<Directive, DirectiveBuilder> {
factory DirectiveBuilder() = _$DirectiveBuilder;
DirectiveBuilder._();
bool deferred = false;
String? as;
String? url;
List<String> show = <String>[];
List<String> hide = <String>[];
DirectiveType? type;
}
enum DirectiveType {
import,
export,
part,
partOf,
}
/// Sort import URIs represented by [a] and [b] to honor the
/// "Effective Dart" ordering rules which are enforced by the
/// `directives_ordering` lint.
///
/// 1. `import`s before `export`s
/// 2. `dart:`
/// 3. `package:`
/// 4. relative
/// 5. `part`s
int _compareDirectives(Directive a, Directive b) {
// NOTE: using the fact that `import` is before `export` in the
// `DirectiveType` enum – which allows us to compare using `indexOf`.
var value = DirectiveType.values
.indexOf(a.type)
.compareTo(DirectiveType.values.indexOf(b.type));
if (value == 0) {
final uriA = Uri.parse(a.url);
final uriB = Uri.parse(b.url);
if (uriA.hasScheme) {
if (uriB.hasScheme) {
// If both import URIs have schemes, compare them based on scheme
// `dart` will sort before `package` which is what we want
// schemes are case-insensitive, so compare accordingly
value = compareAsciiLowerCase(uriA.scheme, uriB.scheme);
} else {
value = -1;
}
} else if (uriB.hasScheme) {
value = 1;
}
// If both schemes are the same, compare based on path
if (value == 0) {
value = compareAsciiLowerCase(uriA.path, uriB.path);
}
assert((value == 0) == (a.url == b.url));
}
return value;
}