Skip to content

Commit a3e4026

Browse files
parloughsrawlins
andauthored
Future and annotation null safety cleanup (#2911)
* Future and annotation null safety cleanup * Add back missing comment Co-authored-by: Sam Rawlins <[email protected]>
1 parent fb7b92e commit a3e4026

File tree

3 files changed

+40
-49
lines changed

3 files changed

+40
-49
lines changed

lib/src/generator/templates.runtime_renderers.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -529,10 +529,10 @@ class _Renderer_Annotation extends RendererBase<Annotation> {
529529
nextProperty,
530530
[...remainingNames.skip(1)]);
531531
},
532-
isNullValue: (CT_ c) => c.modelType == null,
532+
isNullValue: (CT_ c) => false,
533533
renderValue: (CT_ c, RendererBase<CT_> r,
534534
List<MustachioNode> ast, StringSink sink) {
535-
_render_ElementType(c.modelType!, ast, r.template, sink,
535+
_render_ElementType(c.modelType, ast, r.template, sink,
536536
parent: r);
537537
},
538538
),
@@ -5358,10 +5358,10 @@ class _Renderer_Feature extends RendererBase<Feature> {
53585358
nextProperty,
53595359
[...remainingNames.skip(1)]);
53605360
},
5361-
isNullValue: (CT_ c) => c.linkedName == null,
5361+
isNullValue: (CT_ c) => false,
53625362
renderValue: (CT_ c, RendererBase<CT_> r,
53635363
List<MustachioNode> ast, StringSink sink) {
5364-
_render_String(c.linkedName!, ast, r.template, sink,
5364+
_render_String(c.linkedName, ast, r.template, sink,
53655365
parent: r);
53665366
},
53675367
),
@@ -5380,11 +5380,11 @@ class _Renderer_Feature extends RendererBase<Feature> {
53805380
nextProperty,
53815381
[...remainingNames.skip(1)]);
53825382
},
5383-
isNullValue: (CT_ c) => c.linkedNameWithParameters == null,
5383+
isNullValue: (CT_ c) => false,
53845384
renderValue: (CT_ c, RendererBase<CT_> r,
53855385
List<MustachioNode> ast, StringSink sink) {
53865386
_render_String(
5387-
c.linkedNameWithParameters!, ast, r.template, sink,
5387+
c.linkedNameWithParameters, ast, r.template, sink,
53885388
parent: r);
53895389
},
53905390
),
@@ -5403,10 +5403,10 @@ class _Renderer_Feature extends RendererBase<Feature> {
54035403
nextProperty,
54045404
[...remainingNames.skip(1)]);
54055405
},
5406-
isNullValue: (CT_ c) => c.name == null,
5406+
isNullValue: (CT_ c) => false,
54075407
renderValue: (CT_ c, RendererBase<CT_> r,
54085408
List<MustachioNode> ast, StringSink sink) {
5409-
_render_String(c.name!, ast, r.template, sink, parent: r);
5409+
_render_String(c.name, ast, r.template, sink, parent: r);
54105410
},
54115411
),
54125412
'sortGroup': Property(

lib/src/model/annotation.dart

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,56 +19,47 @@ class Annotation extends Feature with ModelBuilder {
1919
final PackageGraph packageGraph;
2020

2121
Annotation(this.annotation, this.library, this.packageGraph)
22-
: super(annotation.element!.name);
22+
: super(annotation.element!.name!);
2323

24-
String? _linkedNameWithParameters;
2524
@override
26-
String get linkedNameWithParameters => _linkedNameWithParameters ??=
25+
late final String linkedNameWithParameters =
2726
packageGraph.rendererFactory.featureRenderer.renderAnnotation(this);
2827

2928
/// Return the linked name of the annotation.
3029
@override
3130
String get linkedName => annotation.element is PropertyAccessorElement
3231
? modelBuilder.fromElement(annotation.element!).linkedName
3332
// TODO(jcollins-g): consider linking to constructor instead of type?
34-
: modelType!.linkedName;
33+
: modelType.linkedName;
3534

36-
ElementType? _modelType;
37-
ElementType? get modelType {
38-
if (_modelType == null) {
39-
var annotatedWith = annotation.element;
40-
if (annotatedWith is ConstructorElement) {
41-
_modelType = modelBuilder.typeFrom(annotatedWith.returnType, library!);
42-
} else if (annotatedWith is PropertyAccessorElement) {
43-
_modelType = (modelBuilder.fromElement(annotatedWith.variable)
44-
as GetterSetterCombo)
45-
.modelType;
46-
} else {
47-
assert(false,
48-
'non-callable element used as annotation?: ${annotation.element}');
49-
}
35+
late final ElementType modelType = () {
36+
var annotatedWith = annotation.element;
37+
if (annotatedWith is ConstructorElement) {
38+
return modelBuilder.typeFrom(annotatedWith.returnType, library!);
39+
} else if (annotatedWith is PropertyAccessorElement) {
40+
return (modelBuilder.fromElement(annotatedWith.variable)
41+
as GetterSetterCombo)
42+
.modelType;
43+
} else {
44+
throw StateError(
45+
'non-callable element used as annotation?: ${annotation.element}');
5046
}
51-
return _modelType;
52-
}
47+
}();
5348

54-
String? _parameterText;
55-
String get parameterText {
56-
// TODO(srawlins): Attempt to revive constructor arguments in an annotation,
57-
// akin to source_gen's Reviver, in order to link to inner components. For
58-
// example, in `@Foo(const Bar(), baz: <Baz>[Baz.one, Baz.two])`, link to
59-
// `Foo`, `Bar`, `Baz`, `Baz.one`, and `Baz.two`.
60-
if (_parameterText == null) {
61-
var source = annotation.toSource();
62-
var startIndex = source.indexOf('(');
63-
_parameterText =
64-
source.substring(startIndex == -1 ? source.length : startIndex);
65-
}
66-
return _parameterText!;
67-
}
49+
// TODO(srawlins): Attempt to revive constructor arguments in an annotation,
50+
// akin to source_gen's Reviver, in order to link to inner components. For
51+
// example, in `@Foo(const Bar(), baz: <Baz>[Baz.one, Baz.two])`, link to
52+
// `Foo`, `Bar`, `Baz`, `Baz.one`, and `Baz.two`.
53+
/// The textual representation of the argument(s) supplied to the annotation.
54+
late final String parameterText = () {
55+
var source = annotation.toSource();
56+
var startIndex = source.indexOf('(');
57+
return source.substring(startIndex == -1 ? source.length : startIndex);
58+
}();
6859

6960
@override
7061
bool get isPublic =>
71-
modelType!.isPublic &&
62+
modelType.isPublic &&
7263
modelType is DefinedElementType &&
7364
!packageGraph.invisibleAnnotations
7465
.contains((modelType as DefinedElementType).modelElement);

lib/src/model/feature.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:dartdoc/src/model/privacy.dart';
88
int byFeatureOrdering(Feature a, Feature b) {
99
if (a.sortGroup < b.sortGroup) return -1;
1010
if (a.sortGroup > b.sortGroup) return 1;
11-
return compareAsciiLowerCaseNatural(a.name!, b.name!);
11+
return compareAsciiLowerCaseNatural(a.name, b.name);
1212
}
1313

1414
class ElementFeatureNotFoundError extends Error {
@@ -23,22 +23,22 @@ class ElementFeatureNotFoundError extends Error {
2323
/// A "feature" includes both explicit annotations in code (e.g. `deprecated`)
2424
/// as well as others added by the documentation system (`read-write`);
2525
class Feature implements Privacy {
26-
final String? _name;
26+
final String _name;
2727

2828
/// Do not use this except in subclasses, prefer const members of this
2929
/// class instead.
3030
const Feature(this._name, [this.sortGroup = 0]);
3131

3232
final String featurePrefix = '';
3333

34-
String? get name => _name;
34+
String get name => _name;
3535

36-
String? get linkedName => name;
36+
String get linkedName => name;
3737

38-
String? get linkedNameWithParameters => linkedName;
38+
String get linkedNameWithParameters => linkedName;
3939

4040
@override
41-
bool get isPublic => !name!.startsWith('_');
41+
bool get isPublic => !name.startsWith('_');
4242

4343
/// Numerical sort group for this feature.
4444
/// Less than zero will sort before custom annotations.

0 commit comments

Comments
 (0)