Skip to content

Commit f36ecbf

Browse files
authored
[record_use] Refactor (de)serialization order (dart-lang#3108)
1 parent 71b7a66 commit f36ecbf

1 file changed

Lines changed: 72 additions & 55 deletions

File tree

‎pkgs/record_use/lib/src/recordings.dart‎

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,7 @@ Error: $e
6666
}
6767

6868
factory Recordings._fromSyntax(RecordedUsesSyntax syntax) {
69-
final constants = <Constant>[];
70-
final context = DeserializationContext(constants);
71-
for (final constantSyntax in syntax.constants ?? <ConstantSyntax>[]) {
72-
final constant = ConstantProtected.fromSyntax(constantSyntax, context);
73-
if (!constants.contains(constant)) {
74-
constants.add(constant);
75-
}
76-
}
69+
final context = _deserializeConstants(syntax);
7770

7871
final callsForDefinition = <Definition, List<CallReference>>{};
7972
final instancesForDefinition = <Definition, List<InstanceReference>>{};
@@ -117,52 +110,32 @@ Error: $e
117110
);
118111
}
119112

113+
static DeserializationContext _deserializeConstants(
114+
RecordedUsesSyntax syntax,
115+
) {
116+
final constants = <Constant>[];
117+
// Create a context that includes an empty list for the constants. This
118+
// list will be populated by [_deserializeConstants], providing the
119+
// self-referential access needed to resolve recursive constants (e.g.
120+
// list and map constants).
121+
final context = DeserializationContext(constants);
122+
for (final constantSyntax in syntax.constants ?? <ConstantSyntax>[]) {
123+
final constant = ConstantProtected.fromSyntax(constantSyntax, context);
124+
if (!constants.contains(constant)) {
125+
constants.add(constant);
126+
}
127+
}
128+
return context;
129+
}
130+
120131
/// Encodes this object into a JSON representation.
121132
///
122133
/// This method normalizes identifiers and constants for storage efficiency.
123134
Map<String, Object?> toJson() => _toSyntax().json;
124135

125136
RecordedUsesSyntax _toSyntax() {
126-
final constantsIndex = <Constant>{
127-
...calls.values
128-
.expand((calls) => calls)
129-
.whereType<CallWithArguments>()
130-
.expand(
131-
(call) => [
132-
...call.positionalArguments,
133-
...call.namedArguments.values,
134-
],
135-
)
136-
.expand(
137-
(argument) => switch (argument) {
138-
final Constant c => [c],
139-
NonConstant() => <Constant>[],
140-
},
141-
),
142-
...instances.values
143-
.expand((instances) => instances)
144-
.expand(
145-
(instance) => switch (instance) {
146-
InstanceConstantReference(:final instanceConstant) => {
147-
...instanceConstant.fields.values,
148-
instanceConstant,
149-
},
150-
InstanceCreationReference(
151-
:final positionalArguments,
152-
:final namedArguments,
153-
) =>
154-
[...positionalArguments, ...namedArguments.values].expand(
155-
(argument) => switch (argument) {
156-
final Constant c => [c],
157-
NonConstant() => <Constant>[],
158-
},
159-
),
160-
ConstructorTearoffReference() => <Constant>[],
161-
},
162-
),
163-
}.flatten().asMapToIndices;
164-
165-
final context = SerializationContext(constantsIndex);
137+
final allConstants = _collectConstants();
138+
final (context, constantSyntax) = _serializeConstants(allConstants);
166139

167140
final recordings = <RecordingSyntax>[];
168141
final allDefinitions = {
@@ -187,17 +160,61 @@ Error: $e
187160

188161
return RecordedUsesSyntax(
189162
metadata: metadata.toSyntax(),
190-
constants: constantsIndex.isEmpty
191-
? null
192-
: constantsIndex.keys
193-
.map(
194-
(Constant constant) => constant.toSyntax(context),
195-
)
196-
.toList(),
163+
constants: constantSyntax.isEmpty ? null : constantSyntax,
197164
recordings: recordings.isEmpty ? null : recordings,
198165
);
199166
}
200167

168+
List<Constant> _collectConstants() => {
169+
...calls.values
170+
.expand((calls) => calls)
171+
.whereType<CallWithArguments>()
172+
.expand(
173+
(call) => [
174+
...call.positionalArguments,
175+
...call.namedArguments.values,
176+
],
177+
)
178+
.expand(
179+
(argument) => switch (argument) {
180+
final Constant c => [c],
181+
NonConstant() => <Constant>[],
182+
},
183+
),
184+
...instances.values
185+
.expand((instances) => instances)
186+
.expand(
187+
(instance) => switch (instance) {
188+
InstanceConstantReference(:final instanceConstant) => {
189+
...instanceConstant.fields.values,
190+
instanceConstant,
191+
},
192+
InstanceCreationReference(
193+
:final positionalArguments,
194+
:final namedArguments,
195+
) =>
196+
[...positionalArguments, ...namedArguments.values].expand(
197+
(argument) => switch (argument) {
198+
final Constant c => [c],
199+
NonConstant() => <Constant>[],
200+
},
201+
),
202+
ConstructorTearoffReference() => <Constant>[],
203+
},
204+
),
205+
}.flatten().toList();
206+
207+
(SerializationContext, List<ConstantSyntax>) _serializeConstants(
208+
List<Constant> allConstants,
209+
) {
210+
final constantsMap = allConstants.asMapToIndices;
211+
final context = SerializationContext(constantsMap);
212+
final constantSyntax = [
213+
for (final constant in allConstants) constant.toSyntax(context),
214+
];
215+
return (context, constantSyntax);
216+
}
217+
201218
@override
202219
bool operator ==(covariant Recordings other) {
203220
if (identical(this, other)) return true;

0 commit comments

Comments
 (0)