Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e2b80c7

Browse files
committedSep 3, 2020
Rename some fields which erroneously reference "default" constructors.
A "default constructor" is an unnamed, zero-arg constructor. But the constructors being referenced in these fields are just "unnamed." * Deprecated `Class.defaultConstructor`: use `Class.unnamedConstructor`. * Deprecated `Constructor.isDefaultConstructor`: use `Constructor.isUnnamedConstructor`.
1 parent 9849d74 commit e2b80c7

File tree

4 files changed

+47
-27
lines changed

4 files changed

+47
-27
lines changed
 

‎lib/src/markdown_processor.dart

+29-19
Original file line numberDiff line numberDiff line change
@@ -320,37 +320,41 @@ class _MarkdownCommentReference {
320320
packageGraph = library.packageGraph;
321321
}
322322

323-
String __impliedDefaultConstructor;
324-
bool __impliedDefaultConstructorIsSet = false;
323+
String __impliedUnnamedConstructor;
325324

326-
/// Returns the name of the implied default constructor if there is one, or
325+
/// [_impliedUnnamedConstructor] is memoized in [__impliedUnnamedConstructor],
326+
/// but even after it is initialized, it may be null. This bool represents the
327+
/// initializiation state.
328+
bool __impliedUnnamedConstructorIsSet = false;
329+
330+
/// Returns the name of the implied unnamed constructor if there is one, or
327331
/// null if not.
328332
///
329-
/// Default constructors are a special case in dartdoc. If we look up a name
333+
/// Unnamed constructors are a special case in dartdoc. If we look up a name
330334
/// within a class of that class itself, the first thing we find is the
331-
/// default constructor. But we determine whether that's what they actually
335+
/// unnamed constructor. But we determine whether that's what they actually
332336
/// intended (vs. the enclosing class) by context -- whether they seem
333337
/// to be calling it with () or have a 'new' in front of it, or
334338
/// whether the name is repeated.
335339
///
336340
/// Similarly, referencing a class by itself might actually refer to its
337-
/// constructor based on these same heuristics.
341+
/// unnamed constructor based on these same heuristics.
338342
///
339-
/// With the name of the implied default constructor, other methods can
343+
/// With the name of the implied unnamed constructor, other methods can
340344
/// determine whether or not the constructor and/or class we resolved to
341345
/// is actually matching the user's intent.
342-
String get _impliedDefaultConstructor {
343-
if (!__impliedDefaultConstructorIsSet) {
344-
__impliedDefaultConstructorIsSet = true;
346+
String get _impliedUnnamedConstructor {
347+
if (!__impliedUnnamedConstructorIsSet) {
348+
__impliedUnnamedConstructorIsSet = true;
345349
if (codeRef.contains(isConstructor) ||
346350
(codeRefChompedParts.length >= 2 &&
347351
codeRefChompedParts[codeRefChompedParts.length - 1] ==
348352
codeRefChompedParts[codeRefChompedParts.length - 2])) {
349353
// If the last two parts of the code reference are equal, this is probably a default constructor.
350-
__impliedDefaultConstructor = codeRefChompedParts.last;
354+
__impliedUnnamedConstructor = codeRefChompedParts.last;
351355
}
352356
}
353-
return __impliedDefaultConstructor;
357+
return __impliedUnnamedConstructor;
354358
}
355359

356360
/// Calculate reference to a ModelElement.
@@ -563,18 +567,24 @@ class _MarkdownCommentReference {
563567
}
564568
}
565569

566-
/// Transform members of [toConvert] that are classes to their default constructor,
567-
/// if a constructor is implied. If not, do the reverse conversion for default
568-
/// constructors.
570+
/// Returns the unnamed constructor for class [toConvert] or the class for
571+
/// constructor [toConvert], or just [toConvert], based on hueristics.
572+
///
573+
/// * If an unnamed constructor is implied in the comment reference, and
574+
/// [toConvert] is a class with the same name, the class's unnamed
575+
/// constructor is returned.
576+
/// * Otherwise, if [toConvert] is an unnamed constructor, its enclosing
577+
/// class is returned.
578+
/// * Othwerwise, [toConvert] is returned.
569579
ModelElement _convertConstructors(ModelElement toConvert) {
570-
if (_impliedDefaultConstructor != null) {
571-
if (toConvert is Class && toConvert.name == _impliedDefaultConstructor) {
572-
return toConvert.defaultConstructor;
580+
if (_impliedUnnamedConstructor != null) {
581+
if (toConvert is Class && toConvert.name == _impliedUnnamedConstructor) {
582+
return toConvert.unnamedConstructor;
573583
}
574584
return toConvert;
575585
} else {
576586
if (toConvert is Constructor &&
577-
(toConvert.enclosingElement as Class).defaultConstructor ==
587+
(toConvert.enclosingElement as Class).unnamedConstructor ==
578588
toConvert) {
579589
return toConvert.enclosingElement;
580590
}

‎lib/src/model/class.dart

+10-5
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,19 @@ class Class extends Container
6060
packageGraph.specialClasses.addSpecial(this);
6161
}
6262

63-
Constructor _defaultConstructor;
63+
Constructor _unnamedConstructor;
6464

65-
Constructor get defaultConstructor {
66-
_defaultConstructor ??= constructors
67-
.firstWhere((c) => c.isDefaultConstructor, orElse: () => null);
68-
return _defaultConstructor;
65+
Constructor get unnamedConstructor {
66+
_unnamedConstructor ??= constructors
67+
.firstWhere((c) => c.isUnnamedConstructor, orElse: () => null);
68+
return _unnamedConstructor;
6969
}
7070

71+
@Deprecated(
72+
'Renamed to `unnamedConstructor`; this getter with the old name will be '
73+
'removed as early as Dartdoc 1.0.0')
74+
Constructor get defaultConstructor => unnamedConstructor;
75+
7176
@override
7277
Iterable<Method> get instanceMethods =>
7378
quiver.concat([super.instanceMethods, inheritedMethods]);

‎lib/src/model/constructor.dart

+7-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Constructor extends ModelElement
4646

4747
@override
4848
String get fullyQualifiedName {
49-
if (isDefaultConstructor) return super.fullyQualifiedName;
49+
if (isUnnamedConstructor) return super.fullyQualifiedName;
5050
return '${library.name}.$name';
5151
}
5252

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

66-
bool get isDefaultConstructor => name == enclosingElement.name;
66+
bool get isUnnamedConstructor => name == enclosingElement.name;
67+
68+
@Deprecated(
69+
'Renamed to `isUnnamedConstructor`; this getter with the old name will '
70+
'be removed as early as Dartdoc 1.0.0')
71+
bool get isDefaultConstructor => isUnnamedConstructor;
6772

6873
bool get isFactory => _constructor.isFactory;
6974

‎test/end2end/model_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -3339,7 +3339,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
33393339
.firstWhere((c) => c.name == 'ReferToADefaultConstructor');
33403340
withSyntheticConstructor = exLibrary.classes
33413341
.firstWhere((c) => c.name == 'WithSyntheticConstructor');
3342-
syntheticConstructor = withSyntheticConstructor.defaultConstructor;
3342+
syntheticConstructor = withSyntheticConstructor.unnamedConstructor;
33433343
});
33443344

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

0 commit comments

Comments
 (0)
Please sign in to comment.