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

Rename some fields which erroneously reference "default" constructors. #2330

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
48 changes: 29 additions & 19 deletions lib/src/markdown_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -345,37 +345,41 @@ class _MarkdownCommentReference {
packageGraph = library.packageGraph;
}

String __impliedDefaultConstructor;
bool __impliedDefaultConstructorIsSet = false;
String __impliedUnnamedConstructor;

/// Returns the name of the implied default constructor if there is one, or
/// [_impliedUnnamedConstructor] is memoized in [__impliedUnnamedConstructor],
/// but even after it is initialized, it may be null. This bool represents the
/// initializiation state.
bool __impliedUnnamedConstructorIsSet = false;

/// Returns the name of the implied unnamed constructor if there is one, or
/// null if not.
///
/// Default constructors are a special case in dartdoc. If we look up a name
/// Unnamed constructors are a special case in dartdoc. If we look up a name
/// within a class of that class itself, the first thing we find is the
/// default constructor. But we determine whether that's what they actually
/// unnamed constructor. But we determine whether that's what they actually
/// intended (vs. the enclosing class) by context -- whether they seem
/// to be calling it with () or have a 'new' in front of it, or
/// whether the name is repeated.
///
/// Similarly, referencing a class by itself might actually refer to its
/// constructor based on these same heuristics.
/// unnamed constructor based on these same heuristics.
///
/// With the name of the implied default constructor, other methods can
/// With the name of the implied unnamed constructor, other methods can
/// determine whether or not the constructor and/or class we resolved to
/// is actually matching the user's intent.
String get _impliedDefaultConstructor {
if (!__impliedDefaultConstructorIsSet) {
__impliedDefaultConstructorIsSet = true;
String get _impliedUnnamedConstructor {
if (!__impliedUnnamedConstructorIsSet) {
__impliedUnnamedConstructorIsSet = true;
if (codeRef.contains(_constructorIndicationPattern) ||
(codeRefChompedParts.length >= 2 &&
codeRefChompedParts[codeRefChompedParts.length - 1] ==
codeRefChompedParts[codeRefChompedParts.length - 2])) {
// If the last two parts of the code reference are equal, this is probably a default constructor.
__impliedDefaultConstructor = codeRefChompedParts.last;
__impliedUnnamedConstructor = codeRefChompedParts.last;
}
}
return __impliedDefaultConstructor;
return __impliedUnnamedConstructor;
}

/// Calculate reference to a ModelElement.
Expand Down Expand Up @@ -593,18 +597,24 @@ class _MarkdownCommentReference {
}
}

/// Transform members of [toConvert] that are classes to their default constructor,
/// if a constructor is implied. If not, do the reverse conversion for default
/// constructors.
/// Returns the unnamed constructor for class [toConvert] or the class for
/// constructor [toConvert], or just [toConvert], based on hueristics.
///
/// * If an unnamed constructor is implied in the comment reference, and
/// [toConvert] is a class with the same name, the class's unnamed
/// constructor is returned.
/// * Otherwise, if [toConvert] is an unnamed constructor, its enclosing
/// class is returned.
/// * Othwerwise, [toConvert] is returned.
ModelElement _convertConstructors(ModelElement toConvert) {
if (_impliedDefaultConstructor != null) {
if (toConvert is Class && toConvert.name == _impliedDefaultConstructor) {
return toConvert.defaultConstructor;
if (_impliedUnnamedConstructor != null) {
if (toConvert is Class && toConvert.name == _impliedUnnamedConstructor) {
return toConvert.unnamedConstructor;
}
return toConvert;
} else {
if (toConvert is Constructor &&
(toConvert.enclosingElement as Class).defaultConstructor ==
(toConvert.enclosingElement as Class).unnamedConstructor ==
toConvert) {
return toConvert.enclosingElement;
}
Expand Down
15 changes: 10 additions & 5 deletions lib/src/model/class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,19 @@ class Class extends Container
packageGraph.specialClasses.addSpecial(this);
}

Constructor _defaultConstructor;
Constructor _unnamedConstructor;

Constructor get defaultConstructor {
_defaultConstructor ??= constructors
.firstWhere((c) => c.isDefaultConstructor, orElse: () => null);
return _defaultConstructor;
Constructor get unnamedConstructor {
_unnamedConstructor ??= constructors
.firstWhere((c) => c.isUnnamedConstructor, orElse: () => null);
return _unnamedConstructor;
}

@Deprecated(
'Renamed to `unnamedConstructor`; this getter with the old name will be '
'removed as early as Dartdoc 1.0.0')
Constructor get defaultConstructor => unnamedConstructor;

@override
Iterable<Method> get instanceMethods =>
quiver.concat([super.instanceMethods, inheritedMethods]);
Expand Down
9 changes: 7 additions & 2 deletions lib/src/model/constructor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Constructor extends ModelElement

@override
String get fullyQualifiedName {
if (isDefaultConstructor) return super.fullyQualifiedName;
if (isUnnamedConstructor) return super.fullyQualifiedName;
return '${library.name}.$name';
}

Expand All @@ -63,7 +63,12 @@ class Constructor extends ModelElement
@override
bool get isConst => _constructor.isConst;

bool get isDefaultConstructor => name == enclosingElement.name;
bool get isUnnamedConstructor => name == enclosingElement.name;

@Deprecated(
'Renamed to `isUnnamedConstructor`; this getter with the old name will '
'be removed as early as Dartdoc 1.0.0')
bool get isDefaultConstructor => isUnnamedConstructor;

bool get isFactory => _constructor.isFactory;

Expand Down
2 changes: 1 addition & 1 deletion test/end2end/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3353,7 +3353,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
.firstWhere((c) => c.name == 'ReferToADefaultConstructor');
withSyntheticConstructor = exLibrary.classes
.firstWhere((c) => c.name == 'WithSyntheticConstructor');
syntheticConstructor = withSyntheticConstructor.defaultConstructor;
syntheticConstructor = withSyntheticConstructor.unnamedConstructor;
});

test('calculates comment references to classes vs. constructors correctly',
Expand Down