From 1d35ecd624c65f91e42b06e4134511fd50139da3 Mon Sep 17 00:00:00 2001 From: Parker Lougheed Date: Thu, 20 Jan 2022 10:45:04 -0600 Subject: [PATCH 1/2] Future and annotation null safety cleanup --- .../templates.runtime_renderers.dart | 17 +++--- lib/src/model/annotation.dart | 56 +++++++------------ lib/src/model/feature.dart | 12 ++-- 3 files changed, 36 insertions(+), 49 deletions(-) diff --git a/lib/src/generator/templates.runtime_renderers.dart b/lib/src/generator/templates.runtime_renderers.dart index 948a353461..d2195f66dc 100644 --- a/lib/src/generator/templates.runtime_renderers.dart +++ b/lib/src/generator/templates.runtime_renderers.dart @@ -529,10 +529,10 @@ class _Renderer_Annotation extends RendererBase { nextProperty, [...remainingNames.skip(1)]); }, - isNullValue: (CT_ c) => c.modelType == null, + isNullValue: (CT_ c) => false, renderValue: (CT_ c, RendererBase r, List ast, StringSink sink) { - _render_ElementType(c.modelType!, ast, r.template, sink, + _render_ElementType(c.modelType, ast, r.template, sink, parent: r); }, ), @@ -5358,10 +5358,10 @@ class _Renderer_Feature extends RendererBase { nextProperty, [...remainingNames.skip(1)]); }, - isNullValue: (CT_ c) => c.linkedName == null, + isNullValue: (CT_ c) => false, renderValue: (CT_ c, RendererBase r, List ast, StringSink sink) { - _render_String(c.linkedName!, ast, r.template, sink, + _render_String(c.linkedName, ast, r.template, sink, parent: r); }, ), @@ -5380,11 +5380,11 @@ class _Renderer_Feature extends RendererBase { nextProperty, [...remainingNames.skip(1)]); }, - isNullValue: (CT_ c) => c.linkedNameWithParameters == null, + isNullValue: (CT_ c) => false, renderValue: (CT_ c, RendererBase r, List ast, StringSink sink) { _render_String( - c.linkedNameWithParameters!, ast, r.template, sink, + c.linkedNameWithParameters, ast, r.template, sink, parent: r); }, ), @@ -5403,10 +5403,10 @@ class _Renderer_Feature extends RendererBase { nextProperty, [...remainingNames.skip(1)]); }, - isNullValue: (CT_ c) => c.name == null, + isNullValue: (CT_ c) => false, renderValue: (CT_ c, RendererBase r, List 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( @@ -16628,6 +16628,7 @@ const _invisibleGetters = { 'packageWarningCounter', 'publicPackages', 'libraries', + 'libraryCount', 'publicLibraries', 'localPublicLibraries', 'dartCoreObject', diff --git a/lib/src/model/annotation.dart b/lib/src/model/annotation.dart index 8a4a203a8d..eb7f64cd31 100644 --- a/lib/src/model/annotation.dart +++ b/lib/src/model/annotation.dart @@ -19,11 +19,10 @@ 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. @@ -31,44 +30,31 @@ class Annotation extends Feature with ModelBuilder { 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.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!; - } + 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); diff --git a/lib/src/model/feature.dart b/lib/src/model/feature.dart index 5529ce0cc8..2c7fc4196d 100644 --- a/lib/src/model/feature.dart +++ b/lib/src/model/feature.dart @@ -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 { @@ -23,7 +23,7 @@ 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. @@ -31,14 +31,14 @@ class Feature implements Privacy { 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. From 0f394e4a2742929e4b29c4b2e81ae95ef6a6d991 Mon Sep 17 00:00:00 2001 From: Parker Lougheed Date: Thu, 20 Jan 2022 13:02:21 -0600 Subject: [PATCH 2/2] Add back missing comment --- lib/src/model/annotation.dart | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/src/model/annotation.dart b/lib/src/model/annotation.dart index eb7f64cd31..3aa1a34a40 100644 --- a/lib/src/model/annotation.dart +++ b/lib/src/model/annotation.dart @@ -46,6 +46,11 @@ class Annotation extends Feature with ModelBuilder { } }(); + // 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.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('(');