Skip to content

Commit d14c680

Browse files
authored
Make many Strings in DocumentationComment non-nullable (#3243)
1 parent 92afb9b commit d14c680

File tree

6 files changed

+50
-65
lines changed

6 files changed

+50
-65
lines changed

lib/src/generator/templates.runtime_renderers.dart

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3824,10 +3824,10 @@ class _Renderer_Documentable extends RendererBase<Documentable> {
38243824
nextProperty,
38253825
[...remainingNames.skip(1)]);
38263826
},
3827-
isNullValue: (CT_ c) => c.oneLineDoc == null,
3827+
isNullValue: (CT_ c) => false,
38283828
renderValue: (CT_ c, RendererBase<CT_> r,
38293829
List<MustachioNode> ast, StringSink sink) {
3830-
_render_String(c.oneLineDoc!, ast, r.template, sink,
3830+
_render_String(c.oneLineDoc, ast, r.template, sink,
38313831
parent: r);
38323832
},
38333833
),
@@ -3942,13 +3942,25 @@ class _Renderer_DocumentationComment
39423942
nextProperty,
39433943
[...remainingNames.skip(1)]);
39443944
},
3945-
isNullValue: (CT_ c) => c.documentationLocal == null,
3945+
isNullValue: (CT_ c) => false,
39463946
renderValue: (CT_ c, RendererBase<CT_> r,
39473947
List<MustachioNode> ast, StringSink sink) {
3948-
_render_String(c.documentationLocal!, ast, r.template, sink,
3948+
_render_String(c.documentationLocal, ast, r.template, sink,
39493949
parent: r);
39503950
},
39513951
),
3952+
'element': Property(
3953+
getValue: (CT_ c) => c.element,
3954+
renderVariable: (CT_ c, Property<CT_> self,
3955+
List<String> remainingNames) =>
3956+
self.renderSimpleVariable(c, remainingNames, 'Element'),
3957+
isNullValue: (CT_ c) => false,
3958+
renderValue: (CT_ c, RendererBase<CT_> r,
3959+
List<MustachioNode> ast, StringSink sink) {
3960+
renderSimple(c.element, ast, r.template, sink,
3961+
parent: r, getters: _invisibleGetters['Element']!);
3962+
},
3963+
),
39523964
'elementDocumentation': Property(
39533965
getValue: (CT_ c) => c.elementDocumentation,
39543966
renderVariable: (CT_ c, Property<CT_> self,
@@ -9776,29 +9788,6 @@ class _Renderer_ModelElement extends RendererBase<ModelElement> {
97769788
parent: r);
97779789
},
97789790
),
9779-
'documentationComment': Property(
9780-
getValue: (CT_ c) => c.documentationComment,
9781-
renderVariable:
9782-
(CT_ c, Property<CT_> self, List<String> remainingNames) {
9783-
if (remainingNames.isEmpty) {
9784-
return self.getValue(c).toString();
9785-
}
9786-
var name = remainingNames.first;
9787-
var nextProperty =
9788-
_Renderer_String.propertyMap().getValue(name);
9789-
return nextProperty.renderVariable(
9790-
self.getValue(c) as String,
9791-
nextProperty,
9792-
[...remainingNames.skip(1)]);
9793-
},
9794-
isNullValue: (CT_ c) => false,
9795-
renderValue: (CT_ c, RendererBase<CT_> r,
9796-
List<MustachioNode> ast, StringSink sink) {
9797-
_render_String(
9798-
c.documentationComment, ast, r.template, sink,
9799-
parent: r);
9800-
},
9801-
),
98029791
'element': Property(
98039792
getValue: (CT_ c) => c.element,
98049793
renderVariable: (CT_ c, Property<CT_> self,
@@ -10014,13 +10003,6 @@ class _Renderer_ModelElement extends RendererBase<ModelElement> {
1001410003
self.renderSimpleVariable(c, remainingNames, 'bool'),
1001510004
getBool: (CT_ c) => c.hasDocumentation == true,
1001610005
),
10017-
'hasDocumentationComment': Property(
10018-
getValue: (CT_ c) => c.hasDocumentationComment,
10019-
renderVariable: (CT_ c, Property<CT_> self,
10020-
List<String> remainingNames) =>
10021-
self.renderSimpleVariable(c, remainingNames, 'bool'),
10022-
getBool: (CT_ c) => c.hasDocumentationComment == true,
10023-
),
1002410006
'hasFeatures': Property(
1002510007
getValue: (CT_ c) => c.hasFeatures,
1002610008
renderVariable: (CT_ c, Property<CT_> self,
@@ -15798,6 +15780,7 @@ const _invisibleGetters = {
1579815780
'documentationComment',
1579915781
'documentationFrom',
1580015782
'documentationLocal',
15783+
'element',
1580115784
'elementDocumentation',
1580215785
'fullyQualifiedNameWithoutLibrary',
1580315786
'hasDocumentationComment',

lib/src/model/documentable.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ abstract class Documentable extends Nameable {
1717

1818
bool get hasDocumentation;
1919

20-
String? get oneLineDoc;
20+
String get oneLineDoc;
2121

2222
PackageGraph get packageGraph;
2323

lib/src/model/documentation_comment.dart

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:analyzer/dart/element/element.dart';
12
import 'package:args/args.dart';
23
import 'package:crypto/crypto.dart' as crypto;
34
import 'package:dartdoc/src/model/documentable.dart';
@@ -37,6 +38,9 @@ final _htmlInjectRegExp = RegExp(r'<dartdoc-html>([a-f0-9]+)</dartdoc-html>');
3738
/// entrypoints.
3839
mixin DocumentationComment
3940
on Documentable, Warnable, Locatable, SourceCodeMixin {
41+
@override
42+
Element get element;
43+
4044
List<DocumentationComment>? _documentationFrom;
4145

4246
/// The [ModelElement](s) from which we will get documentation.
@@ -56,7 +60,7 @@ mixin DocumentationComment
5660
if (!hasDocumentationComment && self.overriddenElement != null) {
5761
return self.overriddenElement!.documentationFrom;
5862
} else if (self.isInherited) {
59-
return modelBuilder.fromElement(element!).documentationFrom;
63+
return modelBuilder.fromElement(element).documentationFrom;
6064
} else {
6165
return [this];
6266
}
@@ -71,15 +75,15 @@ mixin DocumentationComment
7175

7276
/// The rawest form of the documentation comment, including comment delimiters
7377
/// like `///`, `//`, `/*`, `*/`.
74-
String get documentationComment;
78+
String get documentationComment => element.documentationComment ?? '';
7579

7680
/// True if [this] has a synthetic/inherited or local documentation
7781
/// comment. False otherwise.
78-
bool get hasDocumentationComment;
82+
bool get hasDocumentationComment => element.documentationComment != null;
7983

8084
/// Returns true if the raw documentation comment has a 'nodoc' indication.
8185
late final bool hasNodoc = () {
82-
if (packageGraph.configSetsNodocFor(element!.source!.fullName)) {
86+
if (packageGraph.configSetsNodocFor(element.source!.fullName)) {
8387
return true;
8488
}
8589
if (!hasDocumentationComment) {
@@ -331,7 +335,7 @@ mixin DocumentationComment
331335
replacement = replacement.replaceFirst('```', '```$lang');
332336
}
333337
} else {
334-
var filePath = element!.source!.fullName.substring(dirPath.length + 1);
338+
var filePath = element.source!.fullName.substring(dirPath.length + 1);
335339

336340
// TODO(srawlins): If a file exists at the location without the
337341
// appended 'md' extension, note this.
@@ -753,20 +757,28 @@ mixin DocumentationComment
753757
}
754758
}
755759

756-
/// Returns the documentation for this literal element unless
757-
/// [config.dropTextFrom] indicates it should not be returned. Macro
758-
/// definitions are stripped, but macros themselves are not injected. This
759-
/// is a two stage process to avoid ordering problems.
760-
String? _documentationLocal;
760+
bool _documentationLocalIsSet = false;
761761

762-
String? get documentationLocal =>
763-
_documentationLocal ??= _buildDocumentationLocal();
762+
/// Returns the documentation for this literal element unless
763+
/// `config.dropTextFrom` indicates it should not be returned. Macro
764+
/// definitions are stripped, but macros themselves are not injected. This is
765+
/// a two stage process to avoid ordering problems.
766+
late final String _documentationLocal;
767+
768+
String get documentationLocal {
769+
if (!_documentationLocalIsSet) {
770+
_documentationLocal = _buildDocumentationBaseSync();
771+
_documentationLocalIsSet = true;
772+
}
773+
return _documentationLocal;
774+
}
764775

765776
/// Unconditionally precache local documentation.
766777
///
767778
/// Use only in factory for [PackageGraph].
768779
Future<void> precacheLocalDocs() async {
769780
_documentationLocal = await _buildDocumentationBase();
781+
_documentationLocalIsSet = true;
770782
}
771783

772784
late final bool _hasNodoc;
@@ -833,35 +845,31 @@ mixin DocumentationComment
833845

834846
String? _rawDocs;
835847

836-
String? _buildDocumentationLocal() => _buildDocumentationBaseSync();
837-
838848
/// Override this to add more features to the documentation builder in a
839849
/// subclass.
840850
String buildDocumentationAddition(String docs) => docs;
841851

842-
/// Separate from [_buildDocumentationLocal] for overriding.
843-
String? _buildDocumentationBaseSync() {
852+
String _buildDocumentationBaseSync() {
844853
assert(_rawDocs == null,
845854
'reentrant calls to _buildDocumentation* not allowed');
846855
// Do not use the sync method if we need to evaluate tools or templates.
847856
assert(!isCanonical || !needsPrecache);
848857
String rawDocs;
849-
if (config.dropTextFrom.contains(element!.library!.name)) {
858+
if (config.dropTextFrom.contains(element.library!.name)) {
850859
rawDocs = '';
851860
} else {
852861
rawDocs = _processCommentWithoutTools(documentationComment);
853862
}
854863
return _rawDocs = buildDocumentationAddition(rawDocs);
855864
}
856865

857-
/// Separate from [_buildDocumentationLocal] for overriding. Can only be
858-
/// used as part of [PackageGraph.setUpPackageGraph].
859-
Future<String?> _buildDocumentationBase() async {
866+
/// Can only be used as part of `PackageGraph.setUpPackageGraph`.
867+
Future<String> _buildDocumentationBase() async {
860868
assert(_rawDocs == null,
861869
'reentrant calls to _buildDocumentation* not allowed');
862870
String rawDocs;
863871
// Do not use the sync method if we need to evaluate tools or templates.
864-
if (config.dropTextFrom.contains(element!.library!.name)) {
872+
if (config.dropTextFrom.contains(element.library!.name)) {
865873
rawDocs = '';
866874
} else {
867875
rawDocs = await processComment(documentationComment);

lib/src/model/getter_setter_combo.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ mixin GetterSetterCombo on ModelElement {
190190
// We have to check against `dropTextFrom` here since
191191
// `documentationFrom` doesn't yield the real elements for
192192
// [GetterSetterCombo]s.
193-
if (!config.dropTextFrom.contains(fromGetter.element!.library!.name)) {
193+
if (!config.dropTextFrom.contains(fromGetter.element.library!.name)) {
194194
if (fromGetter.hasDocumentationComment) {
195195
getterComment = fromGetter.documentationComment;
196196
}
@@ -206,7 +206,7 @@ mixin GetterSetterCombo on ModelElement {
206206
if (!setter.isSynthetic && setter.isPublic) {
207207
assert(setter.documentationFrom.length == 1);
208208
var fromSetter = setter.documentationFrom.first;
209-
if (!config.dropTextFrom.contains(fromSetter.element!.library!.name)) {
209+
if (!config.dropTextFrom.contains(fromSetter.element.library!.name)) {
210210
if (fromSetter.hasDocumentationComment) {
211211
return getterComment.isEmpty
212212
? fromSetter.documentationComment

lib/src/model/model_element.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -879,12 +879,6 @@ abstract class ModelElement extends Canonicalization
879879
);
880880
}();
881881

882-
@override
883-
String get documentationComment => element.documentationComment ?? '';
884-
885-
@override
886-
bool get hasDocumentationComment => element.documentationComment != null;
887-
888882
@override
889883
late final String sourceCode =
890884
_sourceCodeRenderer.renderSourceCode(super.sourceCode);

test/end2end/model_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3796,7 +3796,7 @@ String? topLevelFunction(int param1, bool param2, Cool coolBeans,
37963796
test('inheritance of docs from SDK works for getter/setter combos', () {
37973797
expect(
37983798
ExtraSpecialListLength
3799-
.getter!.documentationFrom.first.element!.library!.name,
3799+
.getter!.documentationFrom.first.element.library!.name,
38003800
equals('dart.core'));
38013801
expect(ExtraSpecialListLength.oneLineDoc == '', isFalse);
38023802
});

0 commit comments

Comments
 (0)