Skip to content

Commit e428411

Browse files
authored
Minor cleanup to generator utils (#2534)
* Minor cleanup to generator utils * Shuffle around encoder declaration * Only run if there are categories defined * Address PR feedback * Reformat after changes * Switch to predefined var
1 parent 1d9ad44 commit e428411

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

lib/src/generator/dartdoc_generator_backend.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,15 @@ abstract class DartdocGeneratorBackend implements GeneratorBackend {
8686
@override
8787
void generateCategoryJson(
8888
FileWriter writer, List<Categorization> categories) {
89-
var json = generator_util.generateCategoryJson(
90-
categories, options.prettyIndexJson);
91-
if (!options.useBaseHref) {
92-
json = json.replaceAll(htmlBasePlaceholder, '');
89+
var json = '';
90+
if (categories.isNotEmpty) {
91+
json = generator_util.generateCategoryJson(
92+
categories, options.prettyIndexJson);
93+
if (!options.useBaseHref) {
94+
json = json.replaceAll(htmlBasePlaceholder, '');
95+
}
9396
}
97+
9498
writer.write(_pathContext.join('categories.json'), '${json}\n');
9599
}
96100

lib/src/generator/generator_utils.dart

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,33 @@ import 'package:dartdoc/src/model/model_element.dart';
1313
/// Convenience function to generate category JSON since different generators
1414
/// will likely want the same content for this.
1515
String generateCategoryJson(Iterable<Categorization> categories, bool pretty) {
16-
var encoder = pretty ? JsonEncoder.withIndent(' ') : JsonEncoder();
1716
// ignore: omit_local_variable_types
1817
final List<Map<String, Object>> indexItems =
19-
categories.map((Categorization e) {
20-
var data = <String, Object>{
21-
'name': e.name,
22-
'qualifiedName': e.fullyQualifiedName,
23-
'href': e.href,
24-
'type': e.kind,
18+
categories.map((Categorization categorization) {
19+
final data = <String, Object>{
20+
'name': categorization.name,
21+
'qualifiedName': categorization.fullyQualifiedName,
22+
'href': categorization.href,
23+
'type': categorization.kind,
2524
};
2625

27-
if (e.hasCategoryNames) data['categories'] = e.categoryNames;
28-
if (e.hasSubCategoryNames) data['subcategories'] = e.subCategoryNames;
29-
if (e.hasImage) data['image'] = e.image;
30-
if (e.hasSamples) data['samples'] = e.samples;
26+
if (categorization.hasCategoryNames) {
27+
data['categories'] = categorization.categoryNames;
28+
}
29+
if (categorization.hasSubCategoryNames) {
30+
data['subcategories'] = categorization.subCategoryNames;
31+
}
32+
if (categorization.hasImage) {
33+
data['image'] = categorization.image;
34+
}
35+
if (categorization.hasSamples) {
36+
data['samples'] = categorization.samples;
37+
}
3138
return data;
32-
}).toList();
39+
}).sorted(_sortElementRepresentations);
3340

34-
indexItems.sort((a, b) {
35-
var value = compareNatural(a['qualifiedName'], b['qualifiedName']);
36-
if (value == 0) {
37-
value = compareNatural(a['type'], b['type']);
38-
}
39-
return value;
40-
});
41+
final encoder =
42+
pretty ? const JsonEncoder.withIndent(' ') : const JsonEncoder();
4143

4244
return encoder.convert(indexItems);
4345
}
@@ -46,37 +48,39 @@ String generateCategoryJson(Iterable<Categorization> categories, bool pretty) {
4648
/// generators will likely want the same content for this.
4749
String generateSearchIndexJson(
4850
Iterable<Indexable> indexedElements, bool pretty) {
49-
var encoder = pretty ? JsonEncoder.withIndent(' ') : JsonEncoder();
50-
final indexItems = indexedElements.map((Indexable e) {
51-
var data = <String, Object>{
52-
'name': e.name,
53-
'qualifiedName': e.fullyQualifiedName,
54-
'href': e.href,
55-
'type': e.kind,
56-
'overriddenDepth': e.overriddenDepth,
51+
final indexItems = indexedElements.map((Indexable indexable) {
52+
final data = <String, Object>{
53+
'name': indexable.name,
54+
'qualifiedName': indexable.fullyQualifiedName,
55+
'href': indexable.href,
56+
'type': indexable.kind,
57+
'overriddenDepth': indexable.overriddenDepth,
5758
};
58-
if (e is ModelElement) {
59-
data['packageName'] = e.package.name;
59+
if (indexable is ModelElement) {
60+
data['packageName'] = indexable.package.name;
6061
}
61-
if (e is EnclosedElement) {
62-
var ee = e as EnclosedElement;
62+
if (indexable is EnclosedElement) {
63+
final ee = indexable as EnclosedElement;
6364
data['enclosedBy'] = {
6465
'name': ee.enclosingElement.name,
6566
'type': ee.enclosingElement.kind
6667
};
6768

68-
data['qualifiedName'] = e.fullyQualifiedName;
69+
data['qualifiedName'] = indexable.fullyQualifiedName;
6970
}
7071
return data;
71-
}).toList();
72+
}).sorted(_sortElementRepresentations);
7273

73-
indexItems.sort((a, b) {
74-
var value = compareNatural(a['qualifiedName'], b['qualifiedName']);
75-
if (value == 0) {
76-
value = compareNatural(a['type'], b['type']);
77-
}
78-
return value;
79-
});
74+
final encoder =
75+
pretty ? const JsonEncoder.withIndent(' ') : const JsonEncoder();
8076

8177
return encoder.convert(indexItems);
8278
}
79+
80+
int _sortElementRepresentations(Map<String, Object> a, Map<String, Object> b) {
81+
final value = compareNatural(a['qualifiedName'], b['qualifiedName']);
82+
if (value == 0) {
83+
return compareNatural(a['type'], b['type']);
84+
}
85+
return value;
86+
}

0 commit comments

Comments
 (0)