Skip to content

Commit e4f9451

Browse files
authored
Fix duplicate entries of elements in a category when re-exported. (#4043)
If a package has two libraries `foo.dart` and `bar.dart` where * `foo.dart` contains `Foo` with an `/// {@category ...}` annotation, and, * `bar.dart` re-exports `foo.dart`. Then the category page shall not list `Foo` twice, which is simply a bug. This change fixes this issue. -------- There is an example of the issue in production here: https://pub.dev/documentation/typed_sql/latest/topics/Exceptions-topic.html ![image](https://github.com/user-attachments/assets/04c05459-114b-4f9b-b79c-7c0c480e877f)
1 parent 876180b commit e4f9451

File tree

5 files changed

+58
-19
lines changed

5 files changed

+58
-19
lines changed

lib/src/generator/templates.runtime_renderers.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,7 @@ class _Renderer_Category extends RendererBase<Category> {
15411541
self.renderSimpleVariable(
15421542
c,
15431543
remainingNames,
1544-
'List<TopLevelVariable>',
1544+
'Set<TopLevelVariable>',
15451545
),
15461546

15471547
renderIterable: (
@@ -1735,7 +1735,7 @@ class _Renderer_Category extends RendererBase<Category> {
17351735
self.renderSimpleVariable(
17361736
c,
17371737
remainingNames,
1738-
'List<Enum>',
1738+
'Set<Enum>',
17391739
),
17401740

17411741
renderIterable: (
@@ -1777,7 +1777,7 @@ class _Renderer_Category extends RendererBase<Category> {
17771777
self.renderSimpleVariable(
17781778
c,
17791779
remainingNames,
1780-
'List<ExtensionType>',
1780+
'Set<ExtensionType>',
17811781
),
17821782

17831783
renderIterable: (
@@ -1804,7 +1804,7 @@ class _Renderer_Category extends RendererBase<Category> {
18041804
self.renderSimpleVariable(
18051805
c,
18061806
remainingNames,
1807-
'List<Extension>',
1807+
'Set<Extension>',
18081808
),
18091809

18101810
renderIterable: (
@@ -1917,7 +1917,7 @@ class _Renderer_Category extends RendererBase<Category> {
19171917
self.renderSimpleVariable(
19181918
c,
19191919
remainingNames,
1920-
'List<ModelFunction>',
1920+
'Set<ModelFunction>',
19211921
),
19221922

19231923
renderIterable: (
@@ -2054,7 +2054,7 @@ class _Renderer_Category extends RendererBase<Category> {
20542054
self.renderSimpleVariable(
20552055
c,
20562056
remainingNames,
2057-
'List<Mixin>',
2057+
'Set<Mixin>',
20582058
),
20592059

20602060
renderIterable: (
@@ -2167,7 +2167,7 @@ class _Renderer_Category extends RendererBase<Category> {
21672167
self.renderSimpleVariable(
21682168
c,
21692169
remainingNames,
2170-
'List<TopLevelVariable>',
2170+
'Set<TopLevelVariable>',
21712171
),
21722172

21732173
renderIterable: (
@@ -2320,7 +2320,7 @@ class _Renderer_Category extends RendererBase<Category> {
23202320
self.renderSimpleVariable(
23212321
c,
23222322
remainingNames,
2323-
'List<Typedef>',
2323+
'Set<Typedef>',
23242324
),
23252325

23262326
renderIterable: (

lib/src/model/category.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,33 @@ class Category
3232
@override
3333
final DartdocOptionContext config;
3434

35-
final List<Class> _classes = [];
35+
final Set<Class> _classes = {};
3636

37-
final List<Class> _exceptions = [];
37+
final Set<Class> _exceptions = {};
3838

3939
@override
40-
final List<TopLevelVariable> constants = [];
40+
final Set<TopLevelVariable> constants = {};
4141

4242
@override
43-
final List<Extension> extensions = [];
43+
final Set<Extension> extensions = {};
4444

4545
@override
46-
final List<ExtensionType> extensionTypes = [];
46+
final Set<ExtensionType> extensionTypes = {};
4747

4848
@override
49-
final List<Enum> enums = [];
49+
final Set<Enum> enums = {};
5050

5151
@override
52-
final List<ModelFunction> functions = [];
52+
final Set<ModelFunction> functions = {};
5353

5454
@override
55-
final List<Mixin> mixins = [];
55+
final Set<Mixin> mixins = {};
5656

5757
@override
58-
final List<TopLevelVariable> properties = [];
58+
final Set<TopLevelVariable> properties = {};
5959

6060
@override
61-
final List<Typedef> typedefs = [];
61+
final Set<Typedef> typedefs = {};
6262

6363
final CategoryDefinition _categoryDefinition;
6464

test/end2end/model_special_cases_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,25 @@ void main() {
393393
expect(SubForDocComments.categories.first.isDocumented, isFalse);
394394
expect(SubForDocComments.displayedCategories, isEmpty);
395395
});
396+
397+
test('No duplicate entries', () {
398+
final categories = ginormousPackageGraph.localPackages
399+
.firstWhere((p) => p.name == 'test_package')
400+
.categories;
401+
for (final c in categories) {
402+
expect(c.classes.length, equals(c.classes.toSet().length));
403+
expect(c.exceptions.length, equals(c.exceptions.toSet().length));
404+
expect(c.extensions.length, equals(c.extensions.toSet().length));
405+
expect(
406+
c.extensionTypes.length, equals(c.extensionTypes.toSet().length));
407+
expect(c.enums.length, equals(c.enums.toSet().length));
408+
expect(c.mixins.length, equals(c.mixins.toSet().length));
409+
expect(c.constants.length, equals(c.constants.toSet().length));
410+
expect(c.properties.length, equals(c.properties.toSet().length));
411+
expect(c.functions.length, equals(c.functions.toSet().length));
412+
expect(c.typedefs.length, equals(c.typedefs.toSet().length));
413+
}
414+
});
396415
});
397416

398417
group('Package', () {

test/templates/category_test.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ extension type ExType(int it) {}
125125
'''),
126126
d.file('other.dart', '''
127127
/// {@category Documented}
128-
library;
128+
library;
129+
130+
export 'lib.dart' show C1, E1;
129131
'''),
130132
],
131133
files: [
@@ -229,6 +231,15 @@ library;
229231
);
230232
});
231233

234+
test('classes are not duplicated', () async {
235+
expect(
236+
topicOneLines
237+
.where((l) => l.contains('<a href="../lib/C1-class.html">C1</a>')),
238+
// Once in the sidebar and once in the main body
239+
hasLength(2),
240+
);
241+
});
242+
232243
test('sidebar contains enums', () async {
233244
expect(
234245
topicOneLines,
@@ -240,6 +251,14 @@ library;
240251
);
241252
});
242253

254+
test('enums are not duplicated', () async {
255+
expect(
256+
topicOneLines.where((l) => l.contains('<a href="../lib/E1.html">E1</a>')),
257+
// Once in the sidebar and once in the main body
258+
hasLength(2),
259+
);
260+
});
261+
243262
test('sidebar contains mixins', () async {
244263
expect(
245264
topicOneLines,

testing/test_package/lib/src/somelib.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
library reexport.somelib;
22

3+
/// {@category Unreal}
34
class SomeClass {}
45

56
class SomeOtherClass {}

0 commit comments

Comments
 (0)