Skip to content

Commit 8b1e6bf

Browse files
authored
Rename some fields which erroneously reference "default" constructors. (#2330)
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 84de3a1 commit 8b1e6bf

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
@@ -345,37 +345,41 @@ class _MarkdownCommentReference {
345345
packageGraph = library.packageGraph;
346346
}
347347

348-
String __impliedDefaultConstructor;
349-
bool __impliedDefaultConstructorIsSet = false;
348+
String __impliedUnnamedConstructor;
350349

351-
/// Returns the name of the implied default constructor if there is one, or
350+
/// [_impliedUnnamedConstructor] is memoized in [__impliedUnnamedConstructor],
351+
/// but even after it is initialized, it may be null. This bool represents the
352+
/// initializiation state.
353+
bool __impliedUnnamedConstructorIsSet = false;
354+
355+
/// Returns the name of the implied unnamed constructor if there is one, or
352356
/// null if not.
353357
///
354-
/// Default constructors are a special case in dartdoc. If we look up a name
358+
/// Unnamed constructors are a special case in dartdoc. If we look up a name
355359
/// within a class of that class itself, the first thing we find is the
356-
/// default constructor. But we determine whether that's what they actually
360+
/// unnamed constructor. But we determine whether that's what they actually
357361
/// intended (vs. the enclosing class) by context -- whether they seem
358362
/// to be calling it with () or have a 'new' in front of it, or
359363
/// whether the name is repeated.
360364
///
361365
/// Similarly, referencing a class by itself might actually refer to its
362-
/// constructor based on these same heuristics.
366+
/// unnamed constructor based on these same heuristics.
363367
///
364-
/// With the name of the implied default constructor, other methods can
368+
/// With the name of the implied unnamed constructor, other methods can
365369
/// determine whether or not the constructor and/or class we resolved to
366370
/// is actually matching the user's intent.
367-
String get _impliedDefaultConstructor {
368-
if (!__impliedDefaultConstructorIsSet) {
369-
__impliedDefaultConstructorIsSet = true;
371+
String get _impliedUnnamedConstructor {
372+
if (!__impliedUnnamedConstructorIsSet) {
373+
__impliedUnnamedConstructorIsSet = true;
370374
if (codeRef.contains(_constructorIndicationPattern) ||
371375
(codeRefChompedParts.length >= 2 &&
372376
codeRefChompedParts[codeRefChompedParts.length - 1] ==
373377
codeRefChompedParts[codeRefChompedParts.length - 2])) {
374378
// If the last two parts of the code reference are equal, this is probably a default constructor.
375-
__impliedDefaultConstructor = codeRefChompedParts.last;
379+
__impliedUnnamedConstructor = codeRefChompedParts.last;
376380
}
377381
}
378-
return __impliedDefaultConstructor;
382+
return __impliedUnnamedConstructor;
379383
}
380384

381385
/// Calculate reference to a ModelElement.
@@ -593,18 +597,24 @@ class _MarkdownCommentReference {
593597
}
594598
}
595599

596-
/// Transform members of [toConvert] that are classes to their default constructor,
597-
/// if a constructor is implied. If not, do the reverse conversion for default
598-
/// constructors.
600+
/// Returns the unnamed constructor for class [toConvert] or the class for
601+
/// constructor [toConvert], or just [toConvert], based on hueristics.
602+
///
603+
/// * If an unnamed constructor is implied in the comment reference, and
604+
/// [toConvert] is a class with the same name, the class's unnamed
605+
/// constructor is returned.
606+
/// * Otherwise, if [toConvert] is an unnamed constructor, its enclosing
607+
/// class is returned.
608+
/// * Othwerwise, [toConvert] is returned.
599609
ModelElement _convertConstructors(ModelElement toConvert) {
600-
if (_impliedDefaultConstructor != null) {
601-
if (toConvert is Class && toConvert.name == _impliedDefaultConstructor) {
602-
return toConvert.defaultConstructor;
610+
if (_impliedUnnamedConstructor != null) {
611+
if (toConvert is Class && toConvert.name == _impliedUnnamedConstructor) {
612+
return toConvert.unnamedConstructor;
603613
}
604614
return toConvert;
605615
} else {
606616
if (toConvert is Constructor &&
607-
(toConvert.enclosingElement as Class).defaultConstructor ==
617+
(toConvert.enclosingElement as Class).unnamedConstructor ==
608618
toConvert) {
609619
return toConvert.enclosingElement;
610620
}

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
@@ -3359,7 +3359,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
33593359
.firstWhere((c) => c.name == 'ReferToADefaultConstructor');
33603360
withSyntheticConstructor = exLibrary.classes
33613361
.firstWhere((c) => c.name == 'WithSyntheticConstructor');
3362-
syntheticConstructor = withSyntheticConstructor.defaultConstructor;
3362+
syntheticConstructor = withSyntheticConstructor.unnamedConstructor;
33633363
});
33643364

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

0 commit comments

Comments
 (0)