Skip to content

Commit 56a0148

Browse files
authored
Warnings enhancements from #1524 (#1539)
* first piece * Finish warnings breakout * dartfmt * Review comment
1 parent b299577 commit 56a0148

File tree

5 files changed

+53
-20
lines changed

5 files changed

+53
-20
lines changed

bin/dartdoc.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ main(List<String> arguments) async {
174174
assert(message.isNotEmpty);
175175

176176
if (record.level < logging.Level.WARNING) {
177-
if (message.endsWith('...')) {
177+
if (showProgress && message.endsWith('...')) {
178178
// Assume there may be more progress to print, so omit the trailing
179179
// newline
180180
writingProgress = true;

lib/src/markdown_processor.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ final RegExp isConstructor = new RegExp(r'^new[\s]+', multiLine: true);
137137
// Covers anything with leading digits/symbols, empty string, weird punctuation, spaces.
138138
final RegExp notARealDocReference = new RegExp(r'''(^[^\w]|^[\d]|[,"'/]|^$)''');
139139

140+
final RegExp operatorPrefix = new RegExp(r'^operator[ ]*');
141+
140142
final HtmlEscape htmlEscape = const HtmlEscape(HtmlEscapeMode.ELEMENT);
141143

142144
final List<md.InlineSyntax> _markdown_syntaxes = [
@@ -397,8 +399,8 @@ Element _findRefElementInLibrary(String codeRef, ModelElement element,
397399
final Set<ModelElement> results = new Set();
398400

399401
// This might be an operator. Strip the operator prefix and try again.
400-
if (results.isEmpty && codeRef.startsWith('operator')) {
401-
String newCodeRef = codeRef.replaceFirst('operator', '');
402+
if (results.isEmpty && codeRef.startsWith(operatorPrefix)) {
403+
String newCodeRef = codeRef.replaceFirst(operatorPrefix, '');
402404
return _findRefElementInLibrary(
403405
newCodeRef, element, commentRefs, preferredClass);
404406
}
@@ -594,9 +596,13 @@ Element _findRefElementInLibrary(String codeRef, ModelElement element,
594596
} else if (results.length == 1) {
595597
result = results.first.element;
596598
} else {
597-
element.warn(PackageWarning.ambiguousDocReference,
598-
message:
599-
"[$codeRef] => ${results.map((r) => "'${r.fullyQualifiedName}'").join(", ")}");
599+
// Squelch ambiguous doc reference warnings for parameters, because we
600+
// don't link those anyway.
601+
if (!results.every((r) => r is Parameter)) {
602+
element.warn(PackageWarning.ambiguousDocReference,
603+
message:
604+
"[$codeRef] => ${results.map((r) => "'${r.fullyQualifiedName}'").join(", ")}");
605+
}
600606
result = results.first.element;
601607
}
602608
return result;
@@ -635,6 +641,11 @@ void _getResultsForClass(Class tryClass, String codeRefChomped,
635641
for (final modelElement in c.allModelElements) {
636642
if (!_ConsiderIfConstructor(codeRef, modelElement)) continue;
637643
String namePart = modelElement.fullyQualifiedName.split('.').last;
644+
if (modelElement is Accessor) {
645+
// TODO(jcollins-g): Individual classes should be responsible for
646+
// this name comparison munging.
647+
namePart = namePart.split('=').first;
648+
}
638649
// TODO(jcollins-g): fix operators so we can use 'name' here or similar.
639650
if (codeRefChomped == namePart) {
640651
results.add(package.findCanonicalModelElementFor(

lib/src/model.dart

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int byFeatureOrdering(String a, String b) {
8585
return compareAsciiLowerCaseNatural(a, b);
8686
}
8787

88-
final RegExp _locationSplitter = new RegExp(r"(package:|[\\/;.])");
88+
final RegExp locationSplitter = new RegExp(r"(package:|[\\/;.])");
8989

9090
/// Mixin for subclasses of ModelElement representing Elements that can be
9191
/// inherited from one class to another.
@@ -2152,6 +2152,7 @@ ModelElement resolveMultiplyInheritedElement(
21522152
/// ModelElement will reference itself as part of the "wrong" [Library]
21532153
/// from the public interface perspective.
21542154
abstract class ModelElement extends Nameable
2155+
with Warnable
21552156
implements Comparable, Documentable {
21562157
final Element _element;
21572158
final Library _library;
@@ -2325,14 +2326,6 @@ abstract class ModelElement extends Nameable
23252326
return library.package.libraryElementReexportedBy[this.element.library];
23262327
}
23272328

2328-
Set<String> get locationPieces {
2329-
return new Set()
2330-
..addAll(element.location
2331-
.toString()
2332-
.split(_locationSplitter)
2333-
.where((s) => s.isNotEmpty));
2334-
}
2335-
23362329
// Use components of this element's location to return a score for library
23372330
// location.
23382331
ScoredCandidate scoreElementWithLibrary(Library lib) {
@@ -3318,7 +3311,7 @@ abstract class Nameable {
33183311
String get name;
33193312

33203313
Set<String> get namePieces => new Set()
3321-
..addAll(name.split(_locationSplitter).where((s) => s.isNotEmpty));
3314+
..addAll(name.split(locationSplitter).where((s) => s.isNotEmpty));
33223315
}
33233316

33243317
class Operator extends Method {
@@ -3379,7 +3372,7 @@ class Operator extends Method {
33793372
String get typeName => 'operator';
33803373
}
33813374

3382-
class Package extends Nameable implements Documentable {
3375+
class Package extends Nameable with Documentable, Warnable {
33833376
// Library objects serving as entry points for documentation.
33843377
final List<Library> _libraries = [];
33853378

@@ -3483,10 +3476,30 @@ class Package extends Nameable implements Documentable {
34833476
extendedDebug: extendedDebug);
34843477
}
34853478

3479+
final Set<Tuple3<Element, PackageWarning, String>> _warnAlreadySeen =
3480+
new Set();
34863481
void warnOnElement(Warnable warnable, PackageWarning kind,
34873482
{String message,
34883483
Iterable<Locatable> referredFrom,
34893484
Iterable<String> extendedDebug}) {
3485+
var newEntry = new Tuple3(warnable?.element, kind, message);
3486+
if (_warnAlreadySeen.contains(newEntry)) {
3487+
return;
3488+
}
3489+
// Warnings can cause other warnings. Queue them up via the stack but
3490+
// don't allow warnings we're already working on to get in there.
3491+
_warnAlreadySeen.add(newEntry);
3492+
_warnOnElement(warnable, kind,
3493+
message: message,
3494+
referredFrom: referredFrom,
3495+
extendedDebug: extendedDebug);
3496+
_warnAlreadySeen.remove(newEntry);
3497+
}
3498+
3499+
void _warnOnElement(Warnable warnable, PackageWarning kind,
3500+
{String message,
3501+
Iterable<Locatable> referredFrom,
3502+
Iterable<String> extendedDebug}) {
34903503
if (warnable != null) {
34913504
// This sort of warning is only applicable to top level elements.
34923505
if (kind == PackageWarning.ambiguousReexport) {

lib/src/warnings.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/element/element.dart';
6+
import 'package:dartdoc/src/model.dart';
67
import 'package:tuple/tuple.dart';
78

89
import 'config.dart';
@@ -82,10 +83,18 @@ final Map<PackageWarning, PackageWarningHelpText> packageWarningText = const {
8283
};
8384

8485
/// Something that package warnings can be called on.
86+
/// TODO(jcollins-g): Complete object model refactoring for #1524.
8587
abstract class Warnable implements Locatable {
8688
void warn(PackageWarning warning,
8789
{String message, Iterable<Locatable> referredFrom});
8890
Warnable get enclosingElement;
91+
92+
Set<String> get locationPieces {
93+
return new Set.from(element.location
94+
.toString()
95+
.split(locationSplitter)
96+
.where((s) => s.isNotEmpty));
97+
}
8998
}
9099

91100
/// Something that can be located for warning purposes.
@@ -240,7 +249,7 @@ class PackageWarningCounter {
240249
int get errorCount {
241250
return _warningCounts.keys
242251
.map((w) => options.asErrors.contains(w) ? _warningCounts[w] : 0)
243-
.reduce((a, b) => a + b);
252+
.fold(0, (a, b) => a + b);
244253
}
245254

246255
int get warningCount {
@@ -249,7 +258,7 @@ class PackageWarningCounter {
249258
options.asWarnings.contains(w) && !options.asErrors.contains(w)
250259
? _warningCounts[w]
251260
: 0)
252-
.reduce((a, b) => a + b);
261+
.fold(0, (a, b) => a + b);
253262
}
254263

255264
@override

pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,4 +350,4 @@ packages:
350350
source: hosted
351351
version: "2.1.13"
352352
sdks:
353-
dart: ">=1.23.0 <=2.0.0-dev.6.0"
353+
dart: ">=1.23.0 <=2.0.0-dev.7.0"

0 commit comments

Comments
 (0)