Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add package:record_use with JSON storage #1479

Closed
wants to merge 13 commits into from
Prev Previous commit
Next Next commit
Restructure classname of instances
mosuem committed Sep 3, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 728fb3d096554bd2a659f7141caa56efd6453181
6 changes: 1 addition & 5 deletions pkgs/record_use/lib/src/public/field.dart
Original file line number Diff line number Diff line change
@@ -5,32 +5,28 @@
import 'package:equatable/equatable.dart';

class Field extends Equatable {
final String className;
final String name;
final Object? value;

Field({
required this.className,
required this.name,
required this.value,
});

Map<String, dynamic> toJson() {
return {
'className': className,
'name': name,
'value': value,
};
}

factory Field.fromJson(Map<String, dynamic> map) {
return Field(
className: map['className'] as String,
name: map['name'] as String,
value: map['value'],
);
}

@override
List<Object?> get props => [className, name, value];
List<Object?> get props => [name, value];
}
7 changes: 3 additions & 4 deletions pkgs/record_use/lib/src/public/instance.dart
Original file line number Diff line number Diff line change
@@ -4,12 +4,11 @@

import 'package:equatable/equatable.dart';

import 'field.dart';

class Instance extends Equatable {
final List<Field> fields;
final String className;
final Map<String, Object?> fields;

Instance({required this.fields});
Instance({required this.className, required this.fields});

@override
List<Object?> get props => [fields];
6 changes: 5 additions & 1 deletion pkgs/record_use/lib/src/public/reference.dart
Original file line number Diff line number Diff line change
@@ -59,17 +59,20 @@ final class CallReference extends Reference {
}

final class InstanceReference extends Reference {
final String className;
final List<Field> fields;

InstanceReference({
super.loadingUnit,
required super.location,
required this.fields,
required this.className,
});

factory InstanceReference.fromJson(
Map<String, dynamic> json, List<String> uris) {
return InstanceReference(
className: json['className'] as String,
loadingUnit: json['loadingUnit'] as String?,
location:
Location.fromJson(json['@'] as Map<String, dynamic>, null, uris),
@@ -83,8 +86,9 @@ final class InstanceReference extends Reference {
Map<String, dynamic> toJson(List<String> uris) => {
if (fields.isNotEmpty)
'fields': fields.map((field) => field.toJson()).toList(),
'className': className,
...super.toJson(uris),
};
@override
List<Object?> get props => super.props..add(fields);
List<Object?> get props => super.props..add([className, fields]);
}
7 changes: 5 additions & 2 deletions pkgs/record_use/lib/src/record_use.dart
Original file line number Diff line number Diff line change
@@ -104,8 +104,11 @@ extension type RecordedUsages._(UsageRecord _usages) {
.firstWhereOrNull(
(instance) => instance.definition.identifier == classIdentifier)
?.references
.map((reference) => reference.fields)
.map((fields) => Instance(fields: fields));
.map((reference) => Instance(
className: reference.className,
fields: Map.fromEntries(reference.fields
.map((field) => MapEntry(field.name, field.value))),
));

/// Checks if any call to [method] has non-const arguments.
///
4 changes: 2 additions & 2 deletions pkgs/record_use/test/storage_test.dart
Original file line number Diff line number Diff line change
@@ -47,6 +47,7 @@ final recordedUses = UsageRecord(
),
references: [
InstanceReference(
className: 'className',
location: Location(
uri: Uri.parse('file://lib/_internal/js_runtime/lib/js_helper.dart')
.toString(),
@@ -55,7 +56,6 @@ final recordedUses = UsageRecord(
),
fields: [
Field(
className: 'className',
name: 'a',
value: 42,
),
@@ -192,9 +192,9 @@ final recordedUsesJson = {
},
'references': [
{
'className': 'className',
'fields': [
{
'className': 'className',
'name': 'a',
'value': 42,
}
15 changes: 10 additions & 5 deletions pkgs/record_use/test/usage_test.dart
Original file line number Diff line number Diff line change
@@ -15,11 +15,16 @@ void main() {
);
});
test('All API instances', () {
final references = recordedUses.instances.expand((e) => e.references);
final instances =
RecordedUsages.fromJson(recordedUsesJson).instancesOf(instanceId);
expect(
RecordedUsages.fromJson(recordedUsesJson).instancesOf(instanceId),
recordedUses.instances
.expand((e) => e.references)
.map((e) => Instance(fields: e.fields)),
instances!.map((e) => e.className),
references.map((e) => e.className),
);
expect(
instances.map((e) => e.fields.entries.map((e) => (e.key, e.value))),
references.map((e) => e.fields.map((e) => (e.name, e.value))),
);
});
test('Specific API calls', () {
@@ -58,7 +63,7 @@ void main() {
);
expect(
RecordedUsages.fromJson(recordedUsesJson).instancesOf(instanceId)?.first,
Instance(fields: [Field(name: 'a', className: 'className', value: 42)]),
Instance(className: 'className', fields: {'a': 42}),
);
});
}