Skip to content

Commit a21c495

Browse files
authored
Avoid a few unnecessary href calculations and loops (#2545)
* Avoid a few unnecessary href calculations and loops * Minor other string cleanup * Address feedback
1 parent daeaf48 commit a21c495

File tree

2 files changed

+26
-41
lines changed

2 files changed

+26
-41
lines changed

lib/dartdoc.dart

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,8 @@ class Dartdoc {
273273
referredFromElements.removeWhere((e) => !e.isCanonical);
274274
}
275275
if (warnOnElements != null) {
276-
if (warnOnElements.any((e) => e.isCanonical)) {
277-
warnOnElement = warnOnElements.firstWhere((e) => e.isCanonical);
278-
} else {
279-
// If we don't have a canonical element, just pick one.
280-
warnOnElement = warnOnElements.isEmpty ? null : warnOnElements.first;
281-
}
276+
warnOnElement = warnOnElements.firstWhere((e) => e.isCanonical,
277+
orElse: () => warnOnElements.isEmpty ? null : warnOnElements.first);
282278
}
283279

284280
if (referredFromElements.isEmpty && referredFrom == 'index.html') {
@@ -321,8 +317,7 @@ class Dartdoc {
321317
} else {
322318
// Error messages are orphaned by design and do not appear in the search
323319
// index.
324-
if (<String>['__404error.html', 'categories.json']
325-
.contains(fullPath)) {
320+
if (const {'__404error.html', 'categories.json'}.contains(fullPath)) {
326321
_warn(packageGraph, PackageWarning.orphanedFile, fullPath,
327322
normalOrigin);
328323
}
@@ -342,7 +337,7 @@ class Dartdoc {
342337
// This is extracted to save memory during the check; be careful not to hang
343338
// on to anything referencing the full file and doc tree.
344339
Tuple2<Iterable<String>, String> _getStringLinksAndHref(String fullPath) {
345-
var file = config.resourceProvider.getFile('$fullPath');
340+
var file = config.resourceProvider.getFile(fullPath);
346341
if (!file.exists) {
347342
return null;
348343
}
@@ -368,7 +363,7 @@ class Dartdoc {
368363
PackageGraph packageGraph, String origin, Set<String> visited) {
369364
var fullPath = path.joinAll([origin, 'index.json']);
370365
var indexPath = path.joinAll([origin, 'index.html']);
371-
var file = config.resourceProvider.getFile('$fullPath');
366+
var file = config.resourceProvider.getFile(fullPath);
372367
if (!file.exists) {
373368
return null;
374369
}
@@ -403,10 +398,7 @@ class Dartdoc {
403398
void _doCheck(PackageGraph packageGraph, String origin, Set<String> visited,
404399
String pathToCheck,
405400
[String source, String fullPath]) {
406-
if (fullPath == null) {
407-
fullPath = path.joinAll([origin, pathToCheck]);
408-
fullPath = path.normalize(fullPath);
409-
}
401+
fullPath ??= path.normalize(path.joinAll([origin, pathToCheck]));
410402

411403
var stringLinksAndHref = _getStringLinksAndHref(fullPath);
412404
if (stringLinksAndHref == null) {
@@ -430,14 +422,9 @@ class Dartdoc {
430422
var toVisit = <Tuple2<String, String>>{};
431423

432424
final ignoreHyperlinks = RegExp(r'^(https:|http:|mailto:|ftp:)');
433-
for (var href in stringLinks) {
425+
for (final href in stringLinks) {
434426
if (!href.startsWith(ignoreHyperlinks)) {
435-
Uri uri;
436-
try {
437-
uri = Uri.parse(href);
438-
} on FormatException {
439-
// ignore
440-
}
427+
final uri = Uri.tryParse(href);
441428

442429
if (uri == null || !uri.hasAuthority && !uri.hasFragment) {
443430
String full;
@@ -446,9 +433,10 @@ class Dartdoc {
446433
} else {
447434
full = '${path.dirname(pathToCheck)}/$href';
448435
}
449-
var newPathToCheck = path.normalize(full);
450-
var newFullPath = path.joinAll([origin, newPathToCheck]);
451-
newFullPath = path.normalize(newFullPath);
436+
437+
final newPathToCheck = path.normalize(full);
438+
final newFullPath =
439+
path.normalize(path.joinAll([origin, newPathToCheck]));
452440
if (!visited.contains(newFullPath)) {
453441
toVisit.add(Tuple2(newPathToCheck, newFullPath));
454442
visited.add(newFullPath);
@@ -472,9 +460,8 @@ class Dartdoc {
472460
_hrefs = packageGraph.allHrefs;
473461

474462
final visited = <String>{};
475-
final start = 'index.html';
476463
logInfo('Validating docs...');
477-
_doCheck(packageGraph, origin, visited, start);
464+
_doCheck(packageGraph, origin, visited, 'index.html');
478465
_doOrphanCheck(packageGraph, origin, visited);
479466
_doSearchIndexCheck(packageGraph, origin, visited);
480467
}

lib/src/model/package_graph.dart

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,8 @@ class PackageGraph {
254254

255255
Package _defaultPackage;
256256

257-
Package get defaultPackage {
258-
_defaultPackage ??= Package.fromPackageMeta(packageMeta, this);
259-
return _defaultPackage;
260-
}
257+
Package get defaultPackage =>
258+
_defaultPackage ??= Package.fromPackageMeta(packageMeta, this);
261259

262260
final bool hasEmbedderSdk;
263261

@@ -572,27 +570,27 @@ class PackageGraph {
572570
/// on more than just [allLocalModelElements] to make the error messages
573571
/// comprehensive.
574572
Map<String, Set<ModelElement>> get allHrefs {
575-
var hrefMap = <String, Set<ModelElement>>{};
573+
final hrefMap = <String, Set<ModelElement>>{};
576574
// TODO(jcollins-g ): handle calculating hrefs causing new elements better
577575
// than toList().
578-
for (var modelElement in allConstructedModelElements.values.toList()) {
576+
for (final modelElement in allConstructedModelElements.values.toList()) {
579577
// Technically speaking we should be able to use canonical model elements
580578
// only here, but since the warnings that depend on this debug
581579
// canonicalization problems, don't limit ourselves in case an href is
582580
// generated for something non-canonical.
583581
if (modelElement is Dynamic) continue;
584582
// TODO: see [Accessor.enclosingCombo]
585583
if (modelElement is Accessor) continue;
586-
if (modelElement.href == null) continue;
587-
hrefMap.putIfAbsent(modelElement.href, () => {});
588-
hrefMap[modelElement.href].add(modelElement);
584+
final href = modelElement.href;
585+
if (href == null) continue;
586+
587+
hrefMap.putIfAbsent(href, () => {}).add(modelElement);
589588
}
590-
for (var package in packageMap.values) {
591-
for (var library in package.libraries) {
592-
if (library.href == null) continue;
593-
hrefMap.putIfAbsent(library.href, () => {});
594-
hrefMap[library.href].add(library);
595-
}
589+
590+
for (final library in allLibraries.values) {
591+
final href = library.href;
592+
if (href == null) continue;
593+
hrefMap.putIfAbsent(href, () => {}).add(library);
596594
}
597595
return hrefMap;
598596
}

0 commit comments

Comments
 (0)