Skip to content

Commit 2f33e1a

Browse files
authored
[record_use] Remove internal fromJson and toJson (dart-lang#2985)
This PR only leaves the top level `Recordings` and `RecordedUsages` JSON APIs. All internal from and to json have been removed. The format uses normalization (with constant indices), which means the to and from JSON of parts of the format are not useful by themselves. Users should not try to use JSON serialization of parts of the format, only the full format. Closes: dart-lang#2901 This will require a manual roll into the Dart SDK to migrate `Metadata.fromJson` constructor calls.
1 parent d5ff8ed commit 2f33e1a

8 files changed

Lines changed: 38 additions & 73 deletions

File tree

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ sealed class Constant {
1616
/// Creates a [Constant] object.
1717
const Constant();
1818

19-
/// Converts this [Constant] object to a JSON representation.
20-
///
21-
/// [constants] needs to be passed, as the [Constant]s are normalized and
22-
/// stored separately in the JSON.
23-
Map<String, Object?> toJson(Map<Constant, int> constants) =>
24-
_toSyntax(constants).json;
25-
2619
/// Converts this [Constant] object to a syntax representation.
2720
ConstantSyntax _toSyntax(Map<Constant, int> constants);
2821

@@ -39,15 +32,6 @@ sealed class Constant {
3932
),
4033
};
4134

42-
/// Creates a [Constant] object from its JSON representation.
43-
///
44-
/// [constants] needs to be passed, as the [Constant]s are normalized and
45-
/// stored separately in the JSON.
46-
static Constant fromJson(
47-
Map<String, Object?> value,
48-
List<Constant> constants,
49-
) => _fromSyntax(ConstantSyntax.fromJson(value), constants);
50-
5135
/// Creates a [Constant] object from its syntax representation.
5236
static Constant _fromSyntax(
5337
ConstantSyntax syntax,

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,11 @@ class Definition {
1414

1515
const Definition({required this.identifier, this.loadingUnit});
1616

17-
factory Definition.fromJson(Map<String, Object?> json) =>
18-
Definition._fromSyntax(DefinitionSyntax.fromJson(json));
19-
2017
factory Definition._fromSyntax(DefinitionSyntax syntax) => Definition(
2118
identifier: IdentifierProtected.fromSyntax(syntax.identifier),
2219
loadingUnit: syntax.loadingUnit,
2320
);
2421

25-
Map<String, Object?> toJson() => _toSyntax().json;
26-
2722
DefinitionSyntax _toSyntax() => DefinitionSyntax(
2823
identifier: identifier.toSyntax(),
2924
loadingUnit: loadingUnit,

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,10 @@ class Identifier {
3939
/// [name] is the name of the element.
4040
const Identifier({required this.importUri, this.scope, required this.name});
4141

42-
/// Creates an [Identifier] object from its JSON representation.
43-
factory Identifier.fromJson(Map<String, Object?> json) =>
44-
Identifier._fromSyntax(IdentifierSyntax.fromJson(json));
45-
4642
/// Creates an [Identifier] object from its syntax representation.
4743
factory Identifier._fromSyntax(IdentifierSyntax syntax) =>
4844
Identifier(importUri: syntax.uri, scope: syntax.scope, name: syntax.name);
4945

50-
/// Converts this [Identifier] object to a JSON representation.
51-
Map<String, Object?> toJson() => _toSyntax().json;
52-
5346
/// Converts this [Identifier] object to a syntax representation.
5447
IdentifierSyntax _toSyntax() =>
5548
IdentifierSyntax(uri: importUri, scope: scope, name: name);

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@ class Location {
1313

1414
const Location({required this.uri, this.line, this.column});
1515

16-
factory Location.fromJson(Map<String, Object?> map) =>
17-
Location._fromSyntax(LocationSyntax.fromJson(map));
18-
1916
factory Location._fromSyntax(LocationSyntax syntax) =>
2017
Location(uri: syntax.uri, line: syntax.line, column: syntax.column);
2118

22-
Map<String, Object?> toJson() => _toSyntax().json;
23-
2419
LocationSyntax _toSyntax() =>
2520
LocationSyntax(uri: uri, line: line, column: column);
2621

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,21 @@ class Metadata {
1717

1818
const Metadata._(this._syntax);
1919

20-
factory Metadata.fromJson(Map<String, Object?> json) =>
21-
Metadata._(MetadataSyntax.fromJson(json));
20+
factory Metadata({
21+
required Version version,
22+
required String comment,
23+
Map<String, Object?>? extension,
24+
}) {
25+
final syntax = MetadataSyntax(
26+
comment: comment,
27+
version: version.toString(),
28+
);
29+
// TODO(https://github.com/dart-lang/native/issues/2984): Nest extension
30+
// fields.
31+
return Metadata._(
32+
MetadataSyntax.fromJson({...syntax.json, ...?extension}),
33+
);
34+
}
2235

2336
/// The underlying data.
2437
///
@@ -27,11 +40,20 @@ class Metadata {
2740
/// VM.
2841
Map<String, Object?> get json => _syntax.json;
2942

30-
Map<String, Object?> toJson() => _syntax.json;
31-
3243
Version get version => Version.parse(_syntax.version);
44+
3345
String get comment => _syntax.comment;
3446

47+
Map<String, Object?>? get extension {
48+
// TODO(https://github.com/dart-lang/native/issues/2984): Nest extension
49+
// fields.
50+
final dummy = MetadataSyntax(comment: comment, version: version.toString());
51+
return {
52+
for (final entry in _syntax.json.entries)
53+
if (!dummy.json.keys.contains(entry.key)) entry.key: entry.value,
54+
};
55+
}
56+
3557
@override
3658
bool operator ==(covariant Metadata other) {
3759
if (identical(this, other)) return true;

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ sealed class Reference {
3535
@override
3636
int get hashCode => Object.hash(loadingUnit, location);
3737

38-
Map<String, Object?> toJson(
39-
Map<Constant, int> constants,
40-
Map<Location, int> locations,
41-
) => _toSyntax(constants, locations).json;
42-
43-
JsonObjectSyntax _toSyntax(
44-
Map<Constant, int> constants,
45-
Map<Location, int> locations,
46-
);
47-
4838
bool _semanticEqualsShared(
4939
Reference other,
5040
bool allowLocationNull, {
@@ -81,12 +71,6 @@ sealed class Reference {
8171
sealed class CallReference extends Reference {
8272
const CallReference({required super.loadingUnit, required super.location});
8373

84-
static CallReference fromJson(
85-
Map<String, Object?> json,
86-
List<Constant> constants,
87-
List<Location> locations,
88-
) => _fromSyntax(CallSyntax.fromJson(json), constants, locations);
89-
9074
static CallReference _fromSyntax(
9175
CallSyntax syntax,
9276
List<Constant> constants,
@@ -121,7 +105,6 @@ sealed class CallReference extends Reference {
121105
};
122106
}
123107

124-
@override
125108
CallSyntax _toSyntax(
126109
Map<Constant, int> constants,
127110
Map<Location, int> locations,
@@ -296,12 +279,6 @@ final class InstanceReference extends Reference {
296279
required super.location,
297280
});
298281

299-
factory InstanceReference.fromJson(
300-
Map<String, Object?> json,
301-
List<Constant> constants,
302-
List<Location> locations,
303-
) => _fromSyntax(InstanceSyntax.fromJson(json), constants, locations);
304-
305282
static InstanceReference _fromSyntax(
306283
InstanceSyntax syntax,
307284
List<Constant> constants,
@@ -316,7 +293,6 @@ final class InstanceReference extends Reference {
316293
);
317294
}
318295

319-
@override
320296
InstanceSyntax _toSyntax(
321297
Map<Constant, int> constants,
322298
Map<Location, int> locations,

‎pkgs/record_use/test/semantic_equality_test.dart‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ void main() {
6363
loadingUnit: null,
6464
location: Location(uri: 'memory:a/a.dart', line: 1, column: 1),
6565
);
66-
final metadata = Metadata.fromJson({
67-
'version': Version(1, 0, 0).toString(),
68-
'comment': '',
69-
});
66+
final metadata = Metadata(
67+
version: Version(1, 0, 0),
68+
comment: '',
69+
);
7070

7171
test('Definition semantic equality', () {
7272
expect(definition1.semanticEquals(definition1), isTrue);

‎pkgs/record_use/test/test_data.dart‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ final instanceId = Identifier(
2020
);
2121

2222
final recordedUses = Recordings(
23-
metadata: Metadata.fromJson({
24-
'version': Version(1, 6, 2, pre: 'wip', build: '5.-.2.z').toString(),
25-
'comment':
23+
metadata: Metadata(
24+
version: Version(1, 6, 2, pre: 'wip', build: '5.-.2.z'),
25+
comment:
2626
'Recorded references at compile time and their argument values, as'
2727
' far as known, to definitions annotated with @RecordUse',
28-
}),
28+
),
2929
callsForDefinition: {
3030
Definition(identifier: callId, loadingUnit: 'part_15.js'): [
3131
const CallWithArguments(
@@ -83,12 +83,12 @@ final recordedUses = Recordings(
8383
);
8484

8585
final recordedUses2 = Recordings(
86-
metadata: Metadata.fromJson({
87-
'version': Version(1, 6, 2, pre: 'wip', build: '5.-.2.z').toString(),
88-
'comment':
86+
metadata: Metadata(
87+
version: Version(1, 6, 2, pre: 'wip', build: '5.-.2.z'),
88+
comment:
8989
'Recorded references at compile time and their argument values, as'
9090
' far as known, to definitions annotated with @RecordUse',
91-
}),
91+
),
9292
callsForDefinition: {
9393
Definition(identifier: callId, loadingUnit: 'part_15.js'): [
9494
const CallWithArguments(

0 commit comments

Comments
 (0)