Skip to content

Commit 4cbd204

Browse files
authored
[record_use] Public API deal with non-const (dart-lang#3147)
This is not the final API yet, but the current internal API addresses some issues with the public API. So lets remove the old public API. Closes: * dart-lang#2718 * dart-lang#2938 The main design approach of using the new API, deep destructuring in a switch, and giving an error that you cannot tree-shake on failing to destructure: ```dart switch (call) { case CallWithArguments( positionalArguments: [StringConstant(value: final english), ...], ): // Shrink a translations file based on all the different translation // keys. print('Translating to pirate: $english'); case _: throw UnsupportError('Cannot determine which translations are used.'); } ``` ```dart switch (ship) { case InstanceConstantReference( instanceConstant: InstanceConstant( fields: {'name': StringConstant(value: final name)}, ), ): // Include the 3d model for this ship in the application but not // bundle the other ships. print('Pirate ship found: $name'); case _: throw UnsupportedError('Cannot determine which ships are used.'); } ```
1 parent 258d382 commit 4cbd204

14 files changed

Lines changed: 185 additions & 438 deletions

File tree

‎pkgs/hooks/example/link/package_with_assets/hook/link.dart‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ void main(List<String> args) async {
3232

3333
final usedAssets = [
3434
for (final entry in assetMapping.entries)
35-
if (usages.constArgumentsFor(entry.key).isNotEmpty ||
36-
usages.hasNonConstArguments(entry.key))
37-
entry.value,
35+
if (usages.calls.containsKey(entry.key)) entry.value,
3836
];
3937

4038
output.assets.data.addAll(
@@ -46,11 +44,11 @@ void main(List<String> args) async {
4644
}
4745

4846
extension on LinkInput {
49-
RecordedUsages get usages {
47+
Recordings get usages {
5048
final usagesFile = recordedUsagesFile;
5149
final usagesContent = File.fromUri(usagesFile!).readAsStringSync();
5250
final usagesJson = jsonDecode(usagesContent) as Map<String, Object?>;
53-
final usages = RecordedUsages.fromJson(usagesJson);
51+
final usages = Recordings.fromJson(usagesJson);
5452
return usages;
5553
}
5654
}

‎pkgs/hooks_runner/test_data/pirate_speak/hook/link.dart‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ Future<Recordings> _loadRecordings(Uri file) async {
5050
}
5151

5252
Set<String> _extractUsedPhrases(Recordings recordings) {
53-
final usages = RecordedUsages.fromJson(recordings.toJson());
5453
final usedPhrases = <String>{};
5554
final pirateSpeakDef = Definition(
5655
'package:pirate_speak/src/definitions.dart',
@@ -63,11 +62,14 @@ Set<String> _extractUsedPhrases(Recordings recordings) {
6362
],
6463
);
6564

66-
for (final call in usages.constArgumentsFor(pirateSpeakDef)) {
67-
if (call.positional.isNotEmpty) {
68-
if (call.positional.first case StringConstant(:final value)) {
65+
for (final call in recordings.calls[pirateSpeakDef] ?? const []) {
66+
switch (call) {
67+
case CallWithArguments(
68+
positionalArguments: [StringConstant(:final value), ...],
69+
):
6970
usedPhrases.add(value);
70-
}
71+
case _:
72+
throw UnsupportedError('Cannot determine which translations are used.');
7173
}
7274
}
7375
return usedPhrases;

‎pkgs/record_use/README.md‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
This package provides the data classes for the usage recording feature in the
66
Dart SDK.
77

8-
Dart objects with the `@RecordUse` annotation are being recorded at compile
8+
Dart objects with the `@RecordUse()` annotation are being recorded at compile
99
time, providing the user with information. The information depends on the object
1010
being recorded.
1111

@@ -15,7 +15,7 @@ the method will be recorded, as far as they can be inferred at compile time.
1515
any constant instance of the class will be recorded.
1616

1717
> [!NOTE]
18-
> The `@RecordUse` annotation is only allowed on definitions within a package's
18+
> The `@RecordUse()` annotation is only allowed on definitions within a package's
1919
> `lib/` directory. This includes definitions that are members of a class, such
2020
> as static methods.
2121
@@ -53,25 +53,37 @@ void main(List<String> arguments) {
5353
final usesUri = input.recordedUsagesFile;
5454
if (usesUri == null) return;
5555
final usesJson = await File.fromUri(usesUri).readAsString();
56-
final uses = RecordedUsages.fromJson(
56+
final uses = Recordings.fromJson(
5757
jsonDecode(usesJson) as Map<String, Object?>,
5858
);
5959
60-
final args = uses.constArgumentsFor(methodId);
61-
for (final arg in args) {
62-
if (arg.positional[0] case StringConstant(value: final english)) {
63-
print('Translating to pirate: $english');
64-
// Shrink a translations file based on all the different translation
65-
// keys.
60+
final calls = uses.calls[methodId] ?? [];
61+
for (final call in calls) {
62+
switch (call) {
63+
case CallWithArguments(
64+
positionalArguments: [StringConstant(value: final english), ...],
65+
):
66+
// Shrink a translations file based on all the different translation
67+
// keys.
68+
print('Translating to pirate: $english');
69+
case _:
70+
print('Cannot determine which translations are used.');
6671
}
6772
}
6873
69-
final ships = uses.constantsOf(classId);
74+
final ships = uses.instances[classId] ?? [];
7075
for (final ship in ships) {
71-
if (ship.fields['name'] case StringConstant(value: final name)) {
72-
print('Pirate ship found: $name');
73-
// Include the 3d model for this ship in the application but not
74-
// bundle the other ships.
76+
switch (ship) {
77+
case InstanceConstantReference(
78+
instanceConstant: InstanceConstant(
79+
fields: {'name': StringConstant(value: final name)},
80+
),
81+
):
82+
// Include the 3d model for this ship in the application but not
83+
// bundle the other ships.
84+
print('Pirate ship found: $name');
85+
case _:
86+
print('Cannot determine which ships are used.');
7587
}
7688
}
7789
});

‎pkgs/record_use/example/api/usage_link.dart‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,40 @@ void main(List<String> arguments) {
4444
final usesUri = input.recordedUsagesFile;
4545
if (usesUri == null) return;
4646
final usesJson = await File.fromUri(usesUri).readAsString();
47-
final uses = RecordedUsages.fromJson(
47+
final uses = Recordings.fromJson(
4848
jsonDecode(usesJson) as Map<String, Object?>,
4949
);
5050

5151
// snippet-start#static-call
52-
final args = uses.constArgumentsFor(methodId);
53-
for (final arg in args) {
54-
if (arg.positional[0] case StringConstant(value: final english)) {
55-
print('Translating to pirate: $english');
56-
// Shrink a translations file based on all the different translation
57-
// keys.
52+
final calls = uses.calls[methodId] ?? [];
53+
for (final call in calls) {
54+
switch (call) {
55+
case CallWithArguments(
56+
positionalArguments: [StringConstant(value: final english), ...],
57+
):
58+
// Shrink a translations file based on all the different translation
59+
// keys.
60+
print('Translating to pirate: $english');
61+
case _:
62+
print('Cannot determine which translations are used.');
5863
}
5964
}
6065
// snippet-end#static-call
6166

6267
// snippet-start#const-instance
63-
final ships = uses.constantsOf(classId);
68+
final ships = uses.instances[classId] ?? [];
6469
for (final ship in ships) {
65-
if (ship.fields['name'] case StringConstant(value: final name)) {
66-
print('Pirate ship found: $name');
67-
// Include the 3d model for this ship in the application but not
68-
// bundle the other ships.
70+
switch (ship) {
71+
case InstanceConstantReference(
72+
instanceConstant: InstanceConstant(
73+
fields: {'name': StringConstant(value: final name)},
74+
),
75+
):
76+
// Include the 3d model for this ship in the application but not
77+
// bundle the other ships.
78+
print('Pirate ship found: $name');
79+
case _:
80+
print('Cannot determine which ships are used.');
6981
}
7082
}
7183
// snippet-end#const-instance

‎pkgs/record_use/lib/record_use.dart‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,29 @@ export 'src/constant.dart'
66
show
77
BoolConstant,
88
Constant,
9+
EnumConstant,
910
InstanceConstant,
1011
IntConstant,
1112
ListConstant,
1213
MapConstant,
1314
MaybeConstant,
1415
NonConstant,
1516
NullConstant,
17+
RecordConstant,
1618
StringConstant,
1719
SymbolConstant,
1820
UnsupportedConstant;
1921
export 'src/definition.dart'
2022
show Definition, DefinitionDisambiguator, DefinitionKind, Name;
2123
export 'src/loading_unit.dart' show LoadingUnit;
2224
export 'src/metadata.dart' show Metadata;
23-
export 'src/record_use.dart' show RecordedUsages;
24-
export 'src/recorded_usage_from_file.dart' show parseFromFile;
25+
export 'src/recordings.dart' show Recordings;
26+
export 'src/reference.dart'
27+
show
28+
CallReference,
29+
CallTearoff,
30+
CallWithArguments,
31+
ConstructorTearoffReference,
32+
InstanceConstantReference,
33+
InstanceCreationReference,
34+
InstanceReference;

‎pkgs/record_use/lib/record_use_internal.dart‎

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,5 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
export 'src/constant.dart'
6-
show
7-
BoolConstant,
8-
Constant,
9-
EnumConstant,
10-
InstanceConstant,
11-
IntConstant,
12-
ListConstant,
13-
MapConstant,
14-
MaybeConstant,
15-
NonConstant,
16-
NullConstant,
17-
RecordConstant,
18-
StringConstant,
19-
SymbolConstant,
20-
UnsupportedConstant;
21-
export 'src/definition.dart'
22-
show Definition, DefinitionDisambiguator, DefinitionKind, Name;
23-
export 'src/loading_unit.dart' show LoadingUnit;
24-
export 'src/metadata.dart' show Metadata;
25-
export 'src/record_use.dart' show RecordedUsages;
26-
export 'src/recordings.dart'
27-
show FlattenConstantsExtension, MapifyIterableExtension, Recordings;
28-
export 'src/reference.dart'
29-
show
30-
CallReference,
31-
CallTearoff,
32-
CallWithArguments,
33-
ConstructorTearoffReference,
34-
InstanceConstantReference,
35-
InstanceCreationReference,
36-
InstanceReference;
5+
// TODO(dcharkes): Completely remove after migration.
6+
export 'record_use.dart';

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

Lines changed: 0 additions & 133 deletions
This file was deleted.

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

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)