forked from rrousselGit/freezed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
properties.dart
255 lines (231 loc) · 7.14 KB
/
properties.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
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:build/build.dart';
import 'package:freezed/src/templates/parameter_template.dart';
import 'package:source_gen/source_gen.dart';
import '../utils.dart';
import 'concrete_template.dart';
import 'prototypes.dart';
const privConstUsedErrorString =
'It seems like you constructed your class using `MyClass._()`. '
'This constructor is only meant to be used by freezed '
'and you are not supposed to need it nor use it.'
'\\nPlease check the documentation here for more information: '
'https://github.com/rrousselGit/freezed#custom-getters-and-methods';
const privConstUsedErrorVarName = '_privateConstructorUsedError';
class Property {
Property({
required String? type,
required this.name,
required this.decorators,
required this.defaultValueSource,
required this.hasJsonKey,
required this.doc,
required this.isFinal,
required this.isNullable,
required this.isDartList,
required this.isDartMap,
required this.isDartSet,
required this.isPossiblyDartCollection,
required this.isCommonWithDifferentNullability,
required this.parameterElement,
}) : type = type ?? 'dynamic';
Property.fromParameter(Parameter p)
: this(
decorators: p.decorators,
name: p.name,
isFinal: p.isFinal,
doc: p.doc,
type: p.type,
defaultValueSource: p.defaultValueSource,
isNullable: p.isNullable,
isDartList: p.isDartList,
isDartMap: p.isDartMap,
isDartSet: p.isDartSet,
isPossiblyDartCollection: p.isPossiblyDartCollection,
// TODO: support hasJsonKey
hasJsonKey: false,
isCommonWithDifferentNullability: p.isCommonWithDifferentNullability,
parameterElement: p.parameterElement,
);
static Future<Property> fromParameterElement(
ParameterElement element,
BuildStep buildStep, {
required bool addImplicitFinal,
}) async {
final defaultValue = element.defaultValue;
if (defaultValue != null &&
(element.hasRequired || element.isRequiredPositional)) {
throw InvalidGenerationSourceError(
'@Default cannot be used on non-optional parameters',
element: element,
);
}
return Property(
name: element.name,
isNullable: element.type.nullabilitySuffix == NullabilitySuffix.question,
isDartList: element.type.isDartCoreList,
isDartMap: element.type.isDartCoreMap,
isDartSet: element.type.isDartCoreSet,
isFinal: addImplicitFinal || element.isFinal,
doc: await documentationOfParameter(element, buildStep),
type: parseTypeSource(element),
decorators: parseDecorators(element.metadata),
defaultValueSource: defaultValue,
hasJsonKey: element.hasJsonKey,
isPossiblyDartCollection: element.type.isPossiblyDartCollection,
isCommonWithDifferentNullability: false,
parameterElement: element,
);
}
final String type;
final String name;
final bool isNullable;
final bool isDartList;
final bool isDartMap;
final bool isDartSet;
final bool isFinal;
final List<String> decorators;
final String? defaultValueSource;
final bool hasJsonKey;
final String doc;
final bool isPossiblyDartCollection;
final bool isCommonWithDifferentNullability;
final ParameterElement? parameterElement;
@override
String toString() {
final leading = isFinal ? 'final ' : '';
return '$doc${decorators.join()} $leading $type $name;';
}
Getter get unimplementedGetter => Getter(
name: name,
type: type,
decorators: decorators,
doc: doc,
body: ' => throw $privConstUsedErrorVarName;',
);
Getter get abstractGetter => Getter(
name: name,
type: type,
decorators: decorators,
doc: doc,
body: ';',
);
Getter asGetter(String body) => Getter(
name: name,
type: type,
decorators: decorators,
doc: doc,
body: body,
);
Setter get unimplementedSetter => Setter(
name: name,
type: type,
decorators: decorators,
doc: doc,
body: ' => throw $privConstUsedErrorVarName;',
);
Setter get abstractSetter => Setter(
name: name,
type: type,
decorators: decorators,
doc: doc,
body: ';',
);
Property copyWith({
String? type,
String? name,
bool? isNullable,
bool? isDartList,
bool? isDartMap,
bool? isDartSet,
bool? isFinal,
List<String>? decorators,
String? defaultValueSource,
bool? hasJsonKey,
String? doc,
bool? isPossiblyDartCollection,
bool? isCommonWithDifferentNullability,
ParameterElement? parameterElement,
}) {
return Property(
type: type ?? this.type,
name: name ?? this.name,
isNullable: isNullable ?? this.isNullable,
decorators: decorators ?? this.decorators,
defaultValueSource: defaultValueSource ?? this.defaultValueSource,
hasJsonKey: hasJsonKey ?? this.hasJsonKey,
doc: doc ?? this.doc,
isFinal: isFinal ?? this.isFinal,
isDartList: isDartList ?? this.isDartList,
isDartMap: isDartMap ?? this.isDartMap,
isDartSet: isDartSet ?? this.isDartSet,
isPossiblyDartCollection:
isPossiblyDartCollection ?? this.isPossiblyDartCollection,
isCommonWithDifferentNullability: isCommonWithDifferentNullability ??
this.isCommonWithDifferentNullability,
parameterElement: parameterElement ?? this.parameterElement,
);
}
}
class Getter {
Getter({
required String? type,
required this.name,
required this.decorators,
required this.doc,
required this.body,
}) : type = type ?? 'dynamic';
final String type;
final String name;
final List<String> decorators;
final String doc;
final String body;
@override
String toString() {
return '$doc${decorators.join()} $type get $name$body';
}
}
class Setter {
Setter({
required String? type,
required this.name,
required this.decorators,
required this.doc,
required this.body,
}) : type = type ?? 'dynamic';
final String type;
final String name;
final List<String> decorators;
final String doc;
final String body;
@override
String toString() {
return '$doc${decorators.join()} set $name($type value)$body';
}
}
extension IsDartCollection on DartType {
/// Whether this type can potentially contain a [List], [Map], [Set] or [Iterable].
///
/// This includes types such as [dynamic], [Object] and generics
bool get isPossiblyDartCollection {
final interface = safeCast<InterfaceType>();
return isDartCoreMap ||
isDartCoreIterable ||
isDartCoreSet ||
isDartCoreList ||
isDynamic ||
isDartCoreObject ||
this is TypeParameterType ||
(interface != null &&
interface.allSupertypes.any((e) => e.isPossiblyDartCollection));
}
}
extension on Object? {
T? safeCast<T>() {
final that = this;
if (that is T) return that;
return null;
}
}