Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disambiguate between named constructor and field #2331

Merged
merged 1 commit into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions lib/src/markdown_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -446,15 +446,22 @@ class _MarkdownCommentReference {
// If a result is accessible in this library, prefer that.
_reducePreferResultsAccessibleInSameLibrary,
// This may refer to an element with the same name in multiple libraries
// in an external package, e.g. Matrix4 in vector_math and vector_math_64.
// Disambiguate by attempting to figure out which of them our package
// is actually using by checking the import/export graph.
// in an external package, e.g. Matrix4 in vector_math and
// vector_math_64. Disambiguate by attempting to figure out which of
// them our package is actually using by checking the import/export
// graph.
_reducePreferLibrariesInLocalImportExportGraph,
// If a result's fully qualified name has pieces of the comment reference,
// prefer that.
// If a result's fully qualified name has pieces of the comment
// reference, prefer that.
_reducePreferReferencesIncludingFullyQualifiedName,
// Prefer the Dart analyzer's resolution of comment references. We can't
// start from this because of the differences in Dartdoc canonicalization.
// If the reference is indicated to be a constructor, prefer
// constructors. This is not as generic as it sounds; very few naming
// conflicts are allowed, but an instance field is allowed to have the
// same name as a named constructor.
_reducePreferConstructorViaIndicators,
// Prefer the Dart analyzer's resolution of comment references. We
// can't start from this because of the differences in Dartdoc
// canonicalization.
_reducePreferAnalyzerResolution,
]) {
reduceMethod();
Expand Down Expand Up @@ -497,6 +504,13 @@ class _MarkdownCommentReference {
}
}

void _reducePreferConstructorViaIndicators() {
if (codeRef.contains(_constructorIndicationPattern) &&
codeRefChompedParts.length >= 2) {
results.removeWhere((r) => r is! Constructor);
}
}

void _reducePreferReferencesIncludingFullyQualifiedName() {
var startName = '${element.fullyQualifiedName}.';
var realName = '${element.fullyQualifiedName}.${codeRefChomped}';
Expand Down
13 changes: 12 additions & 1 deletion test/end2end/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,17 @@ void main() {
'null-checked variable <a href="${HTMLBASE_PLACEHOLDER}ex/myNumber.html">myNumber!</a>'));
});

test('reference to constructor named the same as a field', () {
var FieldAndCtorWithSameName = exLibrary.classes
.firstWhere((c) => c.name == 'FieldAndCtorWithSameName');
var comment = FieldAndCtorWithSameName.documentationAsHtml;
expect(
comment,
contains('Reference to '
'<a href="${HTMLBASE_PLACEHOLDER}ex/FieldAndCtorWithSameName/FieldAndCtorWithSameName.named.html">'
'FieldAndCtorWithSameName.named()</a>'));
});

test('reference to class from another library', () {
var comment = superAwesomeClass.documentationAsHtml;
expect(
Expand Down Expand Up @@ -1641,7 +1652,7 @@ void main() {
});

test('correctly finds all the classes', () {
expect(classes, hasLength(35));
expect(classes, hasLength(36));
});

test('abstract', () {
Expand Down
7 changes: 7 additions & 0 deletions testing/test_package/lib/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ class B extends Apple with Cat {
/// Reference to nullable type: [Apple?] and null-checked variable [myNumber!].
class RefsWithQsAndBangs {}

/// Reference to [FieldAndCtorWithSameName.named()].
class FieldAndCtorWithSameName {
FieldAndCtorWithSameName.named();

int named;
}

// Do NOT add a doc comment to C. Testing blank comments.

abstract class Cat {
Expand Down