Skip to content

Commit f08a85b

Browse files
authored
[record_use] Definition rework JSON encoding (dart-lang#3091)
1 parent a44ec6c commit f08a85b

44 files changed

Lines changed: 1004 additions & 196 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎pkgs/record_use/doc/schema/record_use.schema.json‎

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,20 @@
240240
"Definition": {
241241
"type": "object",
242242
"properties": {
243-
"name": {
244-
"type": "string"
245-
},
246-
"scope": {
247-
"type": "string"
243+
"path": {
244+
"type": "array",
245+
"items": {
246+
"$ref": "#/definitions/Name"
247+
},
248+
"minItems": 1
248249
},
249250
"uri": {
250251
"type": "string",
251252
"pattern": "^package:"
252253
}
253254
},
254255
"required": [
255-
"name",
256+
"path",
256257
"uri"
257258
]
258259
},
@@ -350,6 +351,56 @@
350351
"value"
351352
]
352353
},
354+
"Name": {
355+
"type": "object",
356+
"properties": {
357+
"disambiguators": {
358+
"type": "array",
359+
"items": {
360+
"type": "string",
361+
"anyOf": [
362+
{
363+
"enum": [
364+
"instance",
365+
"static"
366+
]
367+
},
368+
{
369+
"type": "string"
370+
}
371+
]
372+
}
373+
},
374+
"kind": {
375+
"type": "string",
376+
"anyOf": [
377+
{
378+
"enum": [
379+
"class",
380+
"constructor",
381+
"enum",
382+
"extension",
383+
"extension_type",
384+
"getter",
385+
"method",
386+
"mixin",
387+
"operator",
388+
"setter"
389+
]
390+
},
391+
{
392+
"type": "string"
393+
}
394+
]
395+
},
396+
"name": {
397+
"type": "string"
398+
}
399+
},
400+
"required": [
401+
"name"
402+
]
403+
},
353404
"RecordedUses": {
354405
"type": "object",
355406
"properties": {
@@ -394,7 +445,7 @@
394445
"$ref": "#/definitions/Call"
395446
}
396447
},
397-
"identifier": {
448+
"definition": {
398449
"$ref": "#/definitions/Definition"
399450
},
400451
"instances": {
@@ -405,7 +456,7 @@
405456
}
406457
},
407458
"required": [
408-
"identifier"
459+
"definition"
409460
]
410461
}
411462
}

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

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,40 @@ class Definition {
3030
const Definition(this.library, this.path);
3131

3232
/// Creates a [Definition] object from its syntax representation.
33-
static Definition _fromSyntax(DefinitionSyntax syntax) {
34-
final path = <Name>[];
35-
if (syntax.scope != null) {
36-
path.add(Name(syntax.scope!));
37-
}
38-
path.add(Name(syntax.name));
39-
return Definition(syntax.uri, path);
40-
}
33+
static Definition _fromSyntax(DefinitionSyntax syntax) => Definition(
34+
syntax.uri,
35+
syntax.definitionPath
36+
.map(
37+
(nameSyntax) => Name(
38+
nameSyntax.name,
39+
kind: nameSyntax.kind != null
40+
? DefinitionKind._fromName(nameSyntax.kind!)
41+
: null,
42+
disambiguators:
43+
nameSyntax.disambiguators
44+
?.map(DefinitionDisambiguator._fromName)
45+
.toSet() ??
46+
{},
47+
),
48+
)
49+
.toList(),
50+
);
4151

4252
/// Converts this [Definition] object to a syntax representation.
43-
DefinitionSyntax _toSyntax() {
44-
if (path.length > 2) {
45-
throw StateError(
46-
'DefinitionSyntax only supports up to two levels of nesting. '
47-
'Found ${path.length} levels: $path',
48-
);
49-
}
50-
String? scope;
51-
if (path.length > 1) {
52-
scope = path.first.name;
53-
}
54-
final name = path.last.name;
55-
return DefinitionSyntax(uri: library, scope: scope, name: name);
56-
}
53+
DefinitionSyntax _toSyntax() => DefinitionSyntax(
54+
uri: library,
55+
definitionPath: path
56+
.map(
57+
(name) => NameSyntax(
58+
name: name.name,
59+
kind: name.kind?.toString(),
60+
disambiguators: name.disambiguators.isEmpty
61+
? null
62+
: name.disambiguators.map((d) => d.toString()).toList(),
63+
),
64+
)
65+
.toList(),
66+
);
5767

5868
/// The parent, if it exists.
5969
Definition? get parent => path.length > 1
@@ -163,6 +173,10 @@ class Name {
163173
}
164174

165175
/// The kind of code element represented by a [Name].
176+
///
177+
/// This is not an enum because adding new elements to an enum is a breaking
178+
/// change for switch statements. By using a class with static const instances,
179+
/// we can add new kinds without breaking existing code.
166180
final class DefinitionKind {
167181
final String _name;
168182
const DefinitionKind._(this._name);
@@ -178,12 +192,41 @@ final class DefinitionKind {
178192
static const operatorKind = DefinitionKind._('operator');
179193
static const constructorKind = DefinitionKind._('constructor');
180194

195+
static const _knownValues = [
196+
classKind,
197+
mixinKind,
198+
enumKind,
199+
extensionKind,
200+
extensionTypeKind,
201+
methodKind,
202+
getterKind,
203+
setterKind,
204+
operatorKind,
205+
constructorKind,
206+
];
207+
208+
static DefinitionKind _fromName(String name) => _knownValues.firstWhere(
209+
(v) => v._name == name,
210+
orElse: () => DefinitionKind._(name),
211+
);
212+
213+
@override
214+
bool operator ==(Object other) =>
215+
other is DefinitionKind && other._name == _name;
216+
217+
@override
218+
int get hashCode => _name.hashCode;
219+
181220
@override
182221
String toString() => _name;
183222
}
184223

185224
/// Extra metadata to disambiguate between elements that might have the same
186225
/// name and kind.
226+
///
227+
/// This is not an enum because adding new elements to an enum is a breaking
228+
/// change for switch statements. By using a class with static const instances,
229+
/// we can add new kinds without breaking existing code.
187230
final class DefinitionDisambiguator {
188231
final String _name;
189232
const DefinitionDisambiguator._(this._name);
@@ -201,6 +244,24 @@ final class DefinitionDisambiguator {
201244
/// [DefinitionKind.setterKind], and [DefinitionKind.operatorKind].
202245
static const instanceDisambiguator = DefinitionDisambiguator._('instance');
203246

247+
static const _knownValues = [
248+
staticDisambiguator,
249+
instanceDisambiguator,
250+
];
251+
252+
static DefinitionDisambiguator _fromName(String name) =>
253+
_knownValues.firstWhere(
254+
(v) => v._name == name,
255+
orElse: () => DefinitionDisambiguator._(name),
256+
);
257+
258+
@override
259+
bool operator ==(Object other) =>
260+
other is DefinitionDisambiguator && other._name == _name;
261+
262+
@override
263+
int get hashCode => _name.hashCode;
264+
204265
@override
205266
String toString() => _name;
206267
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Error: $e
7878

7979
for (final recordingSyntax in syntax.recordings ?? <RecordingSyntax>[]) {
8080
final definition = DefinitionProtected.fromSyntax(
81-
recordingSyntax.identifier,
81+
recordingSyntax.definition,
8282
);
8383
if (recordingSyntax.calls case final callSyntaxes?) {
8484
final callReferences = callSyntaxes
@@ -170,7 +170,7 @@ Error: $e
170170
final instancesForDefinition = instances[definition];
171171
recordings.add(
172172
RecordingSyntax(
173-
identifier: definition.toSyntax(),
173+
definition: definition.toSyntax(),
174174
calls: callsForDefinition
175175
?.map((call) => call.toSyntax(constantsIndex))
176176
.toList(),

0 commit comments

Comments
 (0)