@@ -2,8 +2,6 @@ import 'package:analyzer/dart/ast/ast.dart';
2
2
import 'package:analyzer/dart/constant/value.dart' ;
3
3
import 'package:collection/collection.dart' ;
4
4
import 'package:freezed/src/templates/copy_with.dart' ;
5
- import 'package:freezed/src/templates/properties.dart' ;
6
- import 'package:freezed/src/tools/type.dart' ;
7
5
import 'package:freezed_annotation/freezed_annotation.dart' show Freezed;
8
6
import 'package:meta/meta.dart' ;
9
7
import 'package:source_gen/source_gen.dart' ;
@@ -21,14 +19,6 @@ extension StringX on String {
21
19
}
22
20
}
23
21
24
- class CommonProperties {
25
- /// Properties that have a getter in the abstract class
26
- final List <Property > readableProperties = [];
27
-
28
- /// Properties that are visible on `copyWith`
29
- final List <Property > cloneableProperties = [];
30
- }
31
-
32
22
@immutable
33
23
class FreezedGenerator extends ParserGenerator <Freezed > {
34
24
FreezedGenerator (this ._buildYamlConfigs);
@@ -57,141 +47,14 @@ class FreezedGenerator extends ParserGenerator<Freezed> {
57
47
return Class .from (declaration, configs, globalConfigs: _buildYamlConfigs);
58
48
}
59
49
60
- CommonProperties _commonParametersBetweenAllConstructors (Class data) {
61
- final constructorsNeedsGeneration = data.constructors;
62
-
63
- final result = CommonProperties ();
64
- if (constructorsNeedsGeneration case [final ctor]) {
65
- result.cloneableProperties.addAll (
66
- constructorsNeedsGeneration.first.parameters.allParameters
67
- .map (Property .fromParameter),
68
- );
69
- result.readableProperties.addAll (result.cloneableProperties
70
- .where ((p) => ctor.isSynthetic (param: p.name)));
71
- return result;
72
- }
73
-
74
- parameterLoop:
75
- for (final parameter
76
- in constructorsNeedsGeneration.first.parameters.allParameters) {
77
- final isSynthetic =
78
- constructorsNeedsGeneration.first.isSynthetic (param: parameter.name);
79
-
80
- final library = parameter.parameterElement! .library! ;
81
-
82
- var commonTypeBetweenAllUnionConstructors =
83
- parameter.parameterElement! .type;
84
-
85
- for (final constructor in constructorsNeedsGeneration) {
86
- final matchingParameter = constructor.parameters.allParameters
87
- .firstWhereOrNull ((p) => p.name == parameter.name);
88
- // The property is not present in one of the union cases, so shouldn't
89
- // be present in the abstract class.
90
- if (matchingParameter == null ) continue parameterLoop;
91
-
92
- commonTypeBetweenAllUnionConstructors =
93
- library.typeSystem.leastUpperBound (
94
- commonTypeBetweenAllUnionConstructors,
95
- matchingParameter.parameterElement! .type,
96
- );
97
- }
98
-
99
- final matchingParameters = constructorsNeedsGeneration
100
- .expand ((element) => element.parameters.allParameters)
101
- .where ((element) => element.name == parameter.name)
102
- .toList ();
103
-
104
- final isFinal = matchingParameters.any (
105
- (element) =>
106
- element.isFinal ||
107
- element.parameterElement? .type !=
108
- commonTypeBetweenAllUnionConstructors,
109
- );
110
-
111
- final nonNullableCommonType = library.typeSystem
112
- .promoteToNonNull (commonTypeBetweenAllUnionConstructors);
113
-
114
- final didDowncast = matchingParameters.any (
115
- (element) =>
116
- element.parameterElement? .type !=
117
- commonTypeBetweenAllUnionConstructors,
118
- );
119
- final didNonNullDowncast = matchingParameters.any (
120
- (element) =>
121
- element.parameterElement? .type !=
122
- commonTypeBetweenAllUnionConstructors &&
123
- element.parameterElement? .type != nonNullableCommonType,
124
- );
125
- final didNullDowncast = ! didNonNullDowncast && didDowncast;
126
-
127
- final commonTypeString = resolveFullTypeStringFrom (
128
- library,
129
- commonTypeBetweenAllUnionConstructors,
130
- );
131
-
132
- final commonProperty = Property (
133
- isFinal: isFinal,
134
- type: commonTypeString,
135
- isNullable: commonTypeBetweenAllUnionConstructors.isNullable,
136
- isDartList: commonTypeBetweenAllUnionConstructors.isDartCoreList,
137
- isDartMap: commonTypeBetweenAllUnionConstructors.isDartCoreMap,
138
- isDartSet: commonTypeBetweenAllUnionConstructors.isDartCoreSet,
139
- isPossiblyDartCollection:
140
- commonTypeBetweenAllUnionConstructors.isPossiblyDartCollection,
141
- name: parameter.name,
142
- decorators: parameter.decorators,
143
- defaultValueSource: parameter.defaultValueSource,
144
- doc: parameter.doc,
145
- // TODO support JsonKey
146
- hasJsonKey: false ,
147
- );
148
-
149
- if (isSynthetic) result.readableProperties.add (commonProperty);
150
-
151
- // For {int a, int b, int c} | {int a, int? b, double c}, allows:
152
- // copyWith({int a, int b})
153
- // - int? b is not allowed because `null` is not compatible with the
154
- // first union case.
155
- // - num c is not allowed because num is not assignable int/double
156
- if (! didNonNullDowncast) {
157
- final copyWithType = didNullDowncast
158
- ? nonNullableCommonType
159
- : commonTypeBetweenAllUnionConstructors;
160
-
161
- result.cloneableProperties.add (
162
- Property (
163
- isFinal: isFinal,
164
- type: resolveFullTypeStringFrom (
165
- library,
166
- copyWithType,
167
- ),
168
- isNullable: copyWithType.isNullable,
169
- isDartList: copyWithType.isDartCoreList,
170
- isDartMap: copyWithType.isDartCoreMap,
171
- isDartSet: copyWithType.isDartCoreSet,
172
- isPossiblyDartCollection: copyWithType.isPossiblyDartCollection,
173
- name: parameter.name,
174
- decorators: parameter.decorators,
175
- defaultValueSource: parameter.defaultValueSource,
176
- doc: parameter.doc,
177
- // TODO support JsonKey
178
- hasJsonKey: false ,
179
- ),
180
- );
181
- }
182
- }
183
-
184
- return result;
185
- }
186
-
187
50
Iterable <DeepCloneableProperty > _getCommonDeepCloneableProperties (
188
51
List <ConstructorDetails > constructors,
189
- CommonProperties commonProperties,
52
+ PropertyList commonProperties,
190
53
) sync * {
191
54
for (final commonProperty in commonProperties.cloneableProperties) {
192
55
final commonGetter = commonProperties.readableProperties
193
56
.firstWhereOrNull ((e) => e.name == commonProperty.name);
194
- final deepCopyProperty = constructors.first .deepCloneableProperties
57
+ final deepCopyProperty = constructors.firstOrNull ? .deepCloneableProperties
195
58
.firstWhereOrNull ((e) => e.name == commonProperty.name);
196
59
197
60
if (deepCopyProperty == null || commonGetter == null ) continue ;
@@ -239,17 +102,15 @@ class FreezedGenerator extends ParserGenerator<Freezed> {
239
102
) sync * {
240
103
if (data.options.fromJson) yield FromJson (data);
241
104
242
- final commonProperties = _commonParametersBetweenAllConstructors (data);
243
-
244
105
final commonCopyWith = data.options.annotation.copyWith ??
245
- commonProperties .cloneableProperties.isNotEmpty
106
+ data.properties .cloneableProperties.isNotEmpty
246
107
? CopyWith (
247
108
clonedClassName: data.name,
248
- readableProperties: commonProperties .readableProperties,
249
- cloneableProperties: commonProperties .cloneableProperties,
109
+ readableProperties: data.properties .readableProperties,
110
+ cloneableProperties: data.properties .cloneableProperties,
250
111
deepCloneableProperties: _getCommonDeepCloneableProperties (
251
112
data.constructors,
252
- commonProperties ,
113
+ data.properties ,
253
114
).toList (),
254
115
genericsDefinition: data.genericsDefinitionTemplate,
255
116
genericsParameter: data.genericsParameterTemplate,
@@ -260,25 +121,23 @@ class FreezedGenerator extends ParserGenerator<Freezed> {
260
121
yield Abstract (
261
122
data: data,
262
123
copyWith: commonCopyWith,
263
- commonProperties: commonProperties.readableProperties,
124
+ commonProperties: data.properties.readableProperties,
125
+ globalData: globalData,
264
126
);
265
127
266
128
for (final constructor in data.constructors) {
267
129
yield Concrete (
268
130
data: data,
269
131
constructor: constructor,
270
- commonProperties: commonProperties .readableProperties,
132
+ commonProperties: data.properties .readableProperties,
271
133
globalData: globalData,
272
134
copyWith: data.options.annotation.copyWith ??
273
135
constructor.parameters.allParameters.isNotEmpty
274
136
? CopyWith (
275
137
clonedClassName: constructor.redirectedName,
276
- cloneableProperties:
277
- constructor.properties.map ((e) => e.value).toList (),
278
- readableProperties: constructor.properties
279
- .where ((e) => e.isSynthetic)
280
- .map ((e) => e.value)
281
- .toList (),
138
+ cloneableProperties: constructor.properties.toList (),
139
+ readableProperties:
140
+ constructor.properties.where ((e) => e.isSynthetic).toList (),
282
141
deepCloneableProperties: constructor.deepCloneableProperties,
283
142
genericsDefinition: data.genericsDefinitionTemplate,
284
143
genericsParameter: data.genericsParameterTemplate,
0 commit comments