Skip to content

Commit 4b30add

Browse files
authored
Disambiguate between named constructor and field (#2331)
1 parent 8b1e6bf commit 4b30add

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

lib/src/markdown_processor.dart

+21-7
Original file line numberDiff line numberDiff line change
@@ -446,15 +446,22 @@ class _MarkdownCommentReference {
446446
// If a result is accessible in this library, prefer that.
447447
_reducePreferResultsAccessibleInSameLibrary,
448448
// This may refer to an element with the same name in multiple libraries
449-
// in an external package, e.g. Matrix4 in vector_math and vector_math_64.
450-
// Disambiguate by attempting to figure out which of them our package
451-
// is actually using by checking the import/export graph.
449+
// in an external package, e.g. Matrix4 in vector_math and
450+
// vector_math_64. Disambiguate by attempting to figure out which of
451+
// them our package is actually using by checking the import/export
452+
// graph.
452453
_reducePreferLibrariesInLocalImportExportGraph,
453-
// If a result's fully qualified name has pieces of the comment reference,
454-
// prefer that.
454+
// If a result's fully qualified name has pieces of the comment
455+
// reference, prefer that.
455456
_reducePreferReferencesIncludingFullyQualifiedName,
456-
// Prefer the Dart analyzer's resolution of comment references. We can't
457-
// start from this because of the differences in Dartdoc canonicalization.
457+
// If the reference is indicated to be a constructor, prefer
458+
// constructors. This is not as generic as it sounds; very few naming
459+
// conflicts are allowed, but an instance field is allowed to have the
460+
// same name as a named constructor.
461+
_reducePreferConstructorViaIndicators,
462+
// Prefer the Dart analyzer's resolution of comment references. We
463+
// can't start from this because of the differences in Dartdoc
464+
// canonicalization.
458465
_reducePreferAnalyzerResolution,
459466
]) {
460467
reduceMethod();
@@ -497,6 +504,13 @@ class _MarkdownCommentReference {
497504
}
498505
}
499506

507+
void _reducePreferConstructorViaIndicators() {
508+
if (codeRef.contains(_constructorIndicationPattern) &&
509+
codeRefChompedParts.length >= 2) {
510+
results.removeWhere((r) => r is! Constructor);
511+
}
512+
}
513+
500514
void _reducePreferReferencesIncludingFullyQualifiedName() {
501515
var startName = '${element.fullyQualifiedName}.';
502516
var realName = '${element.fullyQualifiedName}.${codeRefChomped}';

test/end2end/model_test.dart

+12-1
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,17 @@ void main() {
13361336
'null-checked variable <a href="${HTMLBASE_PLACEHOLDER}ex/myNumber.html">myNumber!</a>'));
13371337
});
13381338

1339+
test('reference to constructor named the same as a field', () {
1340+
var FieldAndCtorWithSameName = exLibrary.classes
1341+
.firstWhere((c) => c.name == 'FieldAndCtorWithSameName');
1342+
var comment = FieldAndCtorWithSameName.documentationAsHtml;
1343+
expect(
1344+
comment,
1345+
contains('Reference to '
1346+
'<a href="${HTMLBASE_PLACEHOLDER}ex/FieldAndCtorWithSameName/FieldAndCtorWithSameName.named.html">'
1347+
'FieldAndCtorWithSameName.named()</a>'));
1348+
});
1349+
13391350
test('reference to class from another library', () {
13401351
var comment = superAwesomeClass.documentationAsHtml;
13411352
expect(
@@ -1641,7 +1652,7 @@ void main() {
16411652
});
16421653

16431654
test('correctly finds all the classes', () {
1644-
expect(classes, hasLength(35));
1655+
expect(classes, hasLength(36));
16451656
});
16461657

16471658
test('abstract', () {

testing/test_package/lib/example.dart

+7
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@ class B extends Apple with Cat {
262262
/// Reference to nullable type: [Apple?] and null-checked variable [myNumber!].
263263
class RefsWithQsAndBangs {}
264264

265+
/// Reference to [FieldAndCtorWithSameName.named()].
266+
class FieldAndCtorWithSameName {
267+
FieldAndCtorWithSameName.named();
268+
269+
int named;
270+
}
271+
265272
// Do NOT add a doc comment to C. Testing blank comments.
266273

267274
abstract class Cat {

0 commit comments

Comments
 (0)