Skip to content

Future and annotation null safety cleanup #2911

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

Merged
merged 4 commits into from
Jan 20, 2022
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
16 changes: 8 additions & 8 deletions lib/src/generator/templates.runtime_renderers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,10 @@ class _Renderer_Annotation extends RendererBase<Annotation> {
nextProperty,
[...remainingNames.skip(1)]);
},
isNullValue: (CT_ c) => c.modelType == null,
isNullValue: (CT_ c) => false,
renderValue: (CT_ c, RendererBase<CT_> r,
List<MustachioNode> ast, StringSink sink) {
_render_ElementType(c.modelType!, ast, r.template, sink,
_render_ElementType(c.modelType, ast, r.template, sink,
parent: r);
},
),
Expand Down Expand Up @@ -5358,10 +5358,10 @@ class _Renderer_Feature extends RendererBase<Feature> {
nextProperty,
[...remainingNames.skip(1)]);
},
isNullValue: (CT_ c) => c.linkedName == null,
isNullValue: (CT_ c) => false,
renderValue: (CT_ c, RendererBase<CT_> r,
List<MustachioNode> ast, StringSink sink) {
_render_String(c.linkedName!, ast, r.template, sink,
_render_String(c.linkedName, ast, r.template, sink,
parent: r);
},
),
Expand All @@ -5380,11 +5380,11 @@ class _Renderer_Feature extends RendererBase<Feature> {
nextProperty,
[...remainingNames.skip(1)]);
},
isNullValue: (CT_ c) => c.linkedNameWithParameters == null,
isNullValue: (CT_ c) => false,
renderValue: (CT_ c, RendererBase<CT_> r,
List<MustachioNode> ast, StringSink sink) {
_render_String(
c.linkedNameWithParameters!, ast, r.template, sink,
c.linkedNameWithParameters, ast, r.template, sink,
parent: r);
},
),
Expand All @@ -5403,10 +5403,10 @@ class _Renderer_Feature extends RendererBase<Feature> {
nextProperty,
[...remainingNames.skip(1)]);
},
isNullValue: (CT_ c) => c.name == null,
isNullValue: (CT_ c) => false,
renderValue: (CT_ c, RendererBase<CT_> r,
List<MustachioNode> ast, StringSink sink) {
_render_String(c.name!, ast, r.template, sink, parent: r);
_render_String(c.name, ast, r.template, sink, parent: r);
},
),
'sortGroup': Property(
Expand Down
61 changes: 26 additions & 35 deletions lib/src/model/annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,47 @@ class Annotation extends Feature with ModelBuilder {
final PackageGraph packageGraph;

Annotation(this.annotation, this.library, this.packageGraph)
: super(annotation.element!.name);
: super(annotation.element!.name!);

String? _linkedNameWithParameters;
@override
String get linkedNameWithParameters => _linkedNameWithParameters ??=
late final String linkedNameWithParameters =
packageGraph.rendererFactory.featureRenderer.renderAnnotation(this);

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

ElementType? _modelType;
ElementType? get modelType {
if (_modelType == null) {
var annotatedWith = annotation.element;
if (annotatedWith is ConstructorElement) {
_modelType = modelBuilder.typeFrom(annotatedWith.returnType, library!);
} else if (annotatedWith is PropertyAccessorElement) {
_modelType = (modelBuilder.fromElement(annotatedWith.variable)
as GetterSetterCombo)
.modelType;
} else {
assert(false,
'non-callable element used as annotation?: ${annotation.element}');
}
late final ElementType modelType = () {
var annotatedWith = annotation.element;
if (annotatedWith is ConstructorElement) {
return modelBuilder.typeFrom(annotatedWith.returnType, library!);
} else if (annotatedWith is PropertyAccessorElement) {
return (modelBuilder.fromElement(annotatedWith.variable)
as GetterSetterCombo)
.modelType;
} else {
throw StateError(
'non-callable element used as annotation?: ${annotation.element}');
}
return _modelType;
}
}();

String? _parameterText;
String get parameterText {
// TODO(srawlins): Attempt to revive constructor arguments in an annotation,
// akin to source_gen's Reviver, in order to link to inner components. For
// example, in `@Foo(const Bar(), baz: <Baz>[Baz.one, Baz.two])`, link to
// `Foo`, `Bar`, `Baz`, `Baz.one`, and `Baz.two`.
if (_parameterText == null) {
var source = annotation.toSource();
var startIndex = source.indexOf('(');
_parameterText =
source.substring(startIndex == -1 ? source.length : startIndex);
}
return _parameterText!;
}
// TODO(srawlins): Attempt to revive constructor arguments in an annotation,
// akin to source_gen's Reviver, in order to link to inner components. For
// example, in `@Foo(const Bar(), baz: <Baz>[Baz.one, Baz.two])`, link to
// `Foo`, `Bar`, `Baz`, `Baz.one`, and `Baz.two`.
/// The textual representation of the argument(s) supplied to the annotation.
late final String parameterText = () {
var source = annotation.toSource();
var startIndex = source.indexOf('(');
return source.substring(startIndex == -1 ? source.length : startIndex);
}();

@override
bool get isPublic =>
modelType!.isPublic &&
modelType.isPublic &&
modelType is DefinedElementType &&
!packageGraph.invisibleAnnotations
.contains((modelType as DefinedElementType).modelElement);
Expand Down
12 changes: 6 additions & 6 deletions lib/src/model/feature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:dartdoc/src/model/privacy.dart';
int byFeatureOrdering(Feature a, Feature b) {
if (a.sortGroup < b.sortGroup) return -1;
if (a.sortGroup > b.sortGroup) return 1;
return compareAsciiLowerCaseNatural(a.name!, b.name!);
return compareAsciiLowerCaseNatural(a.name, b.name);
}

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

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

final String featurePrefix = '';

String? get name => _name;
String get name => _name;

String? get linkedName => name;
String get linkedName => name;

String? get linkedNameWithParameters => linkedName;
String get linkedNameWithParameters => linkedName;

@override
bool get isPublic => !name!.startsWith('_');
bool get isPublic => !name.startsWith('_');

/// Numerical sort group for this feature.
/// Less than zero will sort before custom annotations.
Expand Down