diff --git a/lib/src/dartdoc_options.dart b/lib/src/dartdoc_options.dart index 7f35160017..79c1a2a59c 100644 --- a/lib/src/dartdoc_options.dart +++ b/lib/src/dartdoc_options.dart @@ -2,8 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// ignore_for_file: analyzer_use_new_elements - /// dartdoc's dartdoc_options.yaml configuration file follows similar loading /// semantics to that of analysis_options.yaml, /// [documented here](https://dart.dev/guides/language/analysis-options). @@ -16,7 +14,7 @@ library; import 'dart:io' show exitCode, stderr, stdout; -import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/dart/element/element2.dart'; import 'package:analyzer/file_system/file_system.dart'; import 'package:args/args.dart'; import 'package:dartdoc/src/dartdoc.dart' show dartdocVersion, programName; @@ -1130,18 +1128,18 @@ class DartdocOptionContext extends DartdocOptionContextBase /// Build a DartdocOptionContext from an analyzer element (using its source /// location). factory DartdocOptionContext.fromElement(DartdocOptionSet optionSet, - LibraryElement libraryElement, ResourceProvider resourceProvider) { + LibraryElement2 libraryElement, ResourceProvider resourceProvider) { return DartdocOptionContext( optionSet, - resourceProvider.getFile(libraryElement.source.fullName), + resourceProvider.getFile(libraryElement.firstFragment.source.fullName), resourceProvider); } /// Build a DartdocOptionContext from an existing [DartdocOptionContext] and a - /// new analyzer [Element]. + /// new analyzer [Element2]. factory DartdocOptionContext.fromContextElement( DartdocOptionContext optionContext, - LibraryElement libraryElement, + LibraryElement2 libraryElement, ResourceProvider resourceProvider) { return DartdocOptionContext.fromElement( optionContext.optionSet, libraryElement, resourceProvider); diff --git a/lib/src/element_type.dart b/lib/src/element_type.dart index 5b56dc7e04..41b62fb657 100644 --- a/lib/src/element_type.dart +++ b/lib/src/element_type.dart @@ -2,14 +2,11 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// ignore_for_file: analyzer_use_new_elements - /// The models used to represent Dart types, all subclasses of [ElementType]. /// /// The only entrypoint for constructing these classes is [ElementType.for_]. library; -import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/element2.dart'; import 'package:analyzer/dart/element/nullability_suffix.dart'; import 'package:analyzer/dart/element/type.dart'; @@ -37,13 +34,13 @@ abstract class ElementType with CommentReferable, Nameable { factory ElementType.for_( DartType type, Library library, PackageGraph packageGraph) { runtimeStats.incrementAccumulator('elementTypeInstantiation'); - var fElement = type.documentableElement; + var fElement = type.documentableElement2; if (fElement == null || fElement.kind == ElementKind.DYNAMIC || fElement.kind == ElementKind.NEVER) { return UndefinedElementType._from(type, library, packageGraph); } - var modelElement = packageGraph.getModelForElement(fElement); + var modelElement = packageGraph.getModelForElement2(fElement); return DefinedElementType._from(type, modelElement, library, packageGraph); } @@ -70,7 +67,7 @@ abstract class ElementType with CommentReferable, Nameable { String toString() => '$type'; } -/// An [ElementType] that isn't pinned to an [Element] (or one that is, but +/// An [ElementType] that isn't pinned to an [Element2] (or one that is, but /// whose element is irrelevant). class UndefinedElementType extends ElementType { UndefinedElementType._(super.type, super.library, super.packageGraph) @@ -105,9 +102,9 @@ class UndefinedElementType extends ElementType { // We can not simply throw here because not all SDK libraries resolve // all types. if (type is InvalidType) return 'dynamic'; - assert(const {'Never'}.contains(type.documentableElement?.name), + assert(const {'Never'}.contains(type.documentableElement2?.name3), 'Unrecognized type for UndefinedElementType: $type'); - return type.documentableElement!.name!; + return type.documentableElement2!.name3!; } @override @@ -132,15 +129,15 @@ class UndefinedElementType extends ElementType { Iterable? get referenceGrandparentOverrides => null; } -/// A [FunctionType] that does not have an underpinning [Element]. +/// A [FunctionType] that does not have an underpinning [Element2]. class FunctionTypeElementType extends UndefinedElementType with Rendered, Callable { FunctionTypeElementType._( FunctionType super.type, super.library, super.packageGraph) : super._(); - List get typeFormals => type.typeFormals - .map((p) => getModelFor(p, library) as TypeParameter) + List get typeFormals => type.typeParameters + .map((p) => getModelFor2(p, library) as TypeParameter) .toList(growable: false); @override @@ -208,18 +205,16 @@ class ParameterizedElementType extends DefinedElementType with Rendered { /// An [ElementType] whose underlying type was referred to by a type alias. mixin Aliased implements ElementType { - Element get typeAliasElement => type.alias!.element; - Element2 get typeAliasElement2 => type.alias!.element2; @override - String get name => typeAliasElement.name!; + String get name => typeAliasElement2.name3!; @override bool get isTypedef => true; late final ModelElement aliasElement = - ModelElement.forElement(typeAliasElement, packageGraph); + ModelElement.forElement2(typeAliasElement2, packageGraph); late final List aliasArguments = type.alias!.typeArguments .map((f) => getTypeFor(f, library)) @@ -258,7 +253,7 @@ class TypeParameterElementType extends DefinedElementType { String get nameWithGenericsPlain => '$name$nullabilitySuffix'; } -/// An [ElementType] associated with an [Element]. +/// An [ElementType] associated with an [Element2]. abstract class DefinedElementType extends ElementType { final ModelElement modelElement; @@ -268,7 +263,7 @@ abstract class DefinedElementType extends ElementType { factory DefinedElementType._from(DartType type, ModelElement modelElement, Library library, PackageGraph packageGraph) { - if (type is! TypeAliasElement && type.alias != null) { + if (type is! TypeAliasElement2 && type.alias != null) { // Here, `alias.element` signals that this is a type referring to an // alias. (`TypeAliasElement.alias.element` has different implications. // In that case it is an actual type alias of some kind (generic or @@ -293,7 +288,7 @@ abstract class DefinedElementType extends ElementType { } @override - String get name => type.documentableElement!.name!; + String get name => type.documentableElement2!.name3!; @override String get fullyQualifiedName => modelElement.fullyQualifiedName; @@ -327,14 +322,14 @@ abstract class DefinedElementType extends ElementType { @internal @override CommentReferable get definingCommentReferable => - ModelElement.forElement(modelElement.element, packageGraph); + ModelElement.forElement2(modelElement.element2, packageGraph); } /// Any callable [ElementType] will mix-in this class, whether anonymous or not, /// unless it is an alias reference. mixin Callable on ElementType { - List get parameters => type.parameters - .map((p) => getModelFor(p, library) as Parameter) + List get parameters => type.formalParameters + .map((p) => getModelFor2(p, library) as Parameter) .toList(growable: false); late final ElementType returnType = getTypeFor(type.returnType, library); diff --git a/lib/src/generator/generator_frontend.dart b/lib/src/generator/generator_frontend.dart index 33c95b847f..9dcd8da136 100644 --- a/lib/src/generator/generator_frontend.dart +++ b/lib/src/generator/generator_frontend.dart @@ -2,8 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// ignore_for_file: analyzer_use_new_elements - import 'package:dartdoc/src/generator/generator.dart'; import 'package:dartdoc/src/generator/generator_backend.dart'; import 'package:dartdoc/src/generator/templates.dart'; @@ -151,7 +149,7 @@ class GeneratorFrontEnd implements Generator { for (var lib in package.libraries.whereDocumented) { if (!multiplePackages) { logInfo('Generating docs for library ${lib.breadcrumbName} from ' - '${lib.element.source.uri}...'); + '${lib.element2.firstFragment.source.uri}...'); } if (!lib.isAnonymous && !lib.hasDocumentation) { packageGraph.warnOnElement(lib, PackageWarning.noLibraryLevelDocs); diff --git a/lib/src/generator/templates.runtime_renderers.dart b/lib/src/generator/templates.runtime_renderers.dart index 1c4933b30a..9cc17d536c 100644 --- a/lib/src/generator/templates.runtime_renderers.dart +++ b/lib/src/generator/templates.runtime_renderers.dart @@ -14112,7 +14112,7 @@ class _Renderer_LibraryContainer extends RendererBase { } } -String renderLibraryRedirect(LibraryTemplateData context, Template template) { +String renderLibrary(LibraryTemplateData context, Template template) { var buffer = StringBuffer(); _render_LibraryTemplateData(context, template.ast, template, buffer); return buffer.toString(); @@ -14358,7 +14358,7 @@ class _Renderer_LibraryTemplateData extends RendererBase { } } -String renderLibrary(LibraryTemplateData context, Template template) { +String renderLibraryRedirect(LibraryTemplateData context, Template template) { var buffer = StringBuffer(); _render_LibraryTemplateData(context, template.ast, template, buffer); return buffer.toString(); @@ -16631,6 +16631,34 @@ class _Renderer_ModelElement extends RendererBase { ); }, ), + 'element2': Property( + getValue: (CT_ c) => c.element2, + renderVariable: + (CT_ c, Property self, List remainingNames) => + self.renderSimpleVariable( + c, + remainingNames, + 'Element2', + ), + + isNullValue: (CT_ c) => false, + + renderValue: ( + CT_ c, + RendererBase r, + List ast, + StringSink sink, + ) { + renderSimple( + c.element2, + ast, + r.template, + sink, + parent: r, + getters: _invisibleGetters['Element2']!, + ); + }, + ), 'enclosingElement': Property( getValue: (CT_ c) => c.enclosingElement, renderVariable: ( @@ -25825,6 +25853,26 @@ const _invisibleGetters = { 'sinceSdkVersion', 'source', }, + 'Element2': { + 'baseElement', + 'children2', + 'displayName', + 'enclosingElement2', + 'firstFragment', + 'fragments', + 'hashCode', + 'id', + 'isPrivate', + 'isPublic', + 'isSynthetic', + 'kind', + 'library2', + 'lookupName', + 'name3', + 'nonSynthetic2', + 'runtimeType', + 'session', + }, 'EnumElement': { 'augmentation', 'augmentationTarget', diff --git a/lib/src/model/library.dart b/lib/src/model/library.dart index 595d7a22d9..1864226681 100644 --- a/lib/src/model/library.dart +++ b/lib/src/model/library.dart @@ -28,6 +28,7 @@ class Library extends ModelElement @override final LibraryElement element; + @override LibraryElement2 get element2 => element as LibraryElementImpl; /// The set of [Element]s declared directly in this library. diff --git a/lib/src/model/model_element.dart b/lib/src/model/model_element.dart index 35c91acdf5..cb26094775 100644 --- a/lib/src/model/model_element.dart +++ b/lib/src/model/model_element.dart @@ -11,11 +11,14 @@ import 'dart:collection'; import 'dart:convert'; import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/dart/element/element2.dart'; import 'package:analyzer/dart/element/type.dart' show FunctionType; import 'package:analyzer/source/line_info.dart'; // ignore: implementation_imports import 'package:analyzer/src/dart/element/member.dart' show ExecutableMember, Member, ParameterMember; +// ignore: implementation_imports +import 'package:analyzer/src/utilities/extensions/element.dart'; import 'package:dartdoc/src/dartdoc_options.dart'; import 'package:dartdoc/src/model/annotation.dart'; import 'package:dartdoc/src/model/attribute.dart'; @@ -109,6 +112,15 @@ abstract class ModelElement return ModelElement.for_(e, library, p); } + /// Returns a [ModelElement] for an [Element2], which can be a + /// property-inducing element or not. + /// + /// This constructor is used when the caller does not know the element's + /// library, or whether it is property-inducing. + factory ModelElement.forElement2(Element2 e, PackageGraph p) { + return ModelElement.forElement(e.asElement!, p); + } + /// Returns a [ModelElement] for a property-inducing element. /// /// Do not construct any [ModelElement]s except from this constructor or @@ -432,7 +444,7 @@ abstract class ModelElement @override late final DartdocOptionContext config = DartdocOptionContext.fromContextElement( - packageGraph.config, library.element, packageGraph.resourceProvider); + packageGraph.config, library.element2, packageGraph.resourceProvider); bool get hasAttributes => attributes.isNotEmpty; @@ -546,6 +558,8 @@ abstract class ModelElement @override Element get element; + Element2 get element2 => element.asElement2!; + @override String get location { // Call nothing from here that can emit warnings or you'll cause stack diff --git a/lib/src/model/nameable.dart b/lib/src/model/nameable.dart index 30895da047..8b685156f7 100644 --- a/lib/src/model/nameable.dart +++ b/lib/src/model/nameable.dart @@ -5,7 +5,10 @@ // ignore_for_file: analyzer_use_new_elements import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/dart/element/element2.dart'; import 'package:analyzer/dart/element/type.dart' show DartType; +// ignore: implementation_imports +import 'package:analyzer/src/utilities/extensions/element.dart'; import 'package:collection/collection.dart'; import 'package:dartdoc/src/element_type.dart'; import 'package:dartdoc/src/model/accessor.dart'; @@ -66,6 +69,21 @@ mixin Nameable { enclosingContainer: enclosingContainer, ); + /// Returns the [ModelElement] for [element], instantiating it if needed. + /// + /// A convenience method for [ModelElement.for_], see its documentation. + ModelElement getModelFor2( + Element2 element, + Library library, { + Container? enclosingContainer, + }) => + ModelElement.for_( + element.asElement!, + library, + packageGraph, + enclosingContainer: enclosingContainer, + ); + /// Returns the [ModelElement] for [element], instantiating it if needed. /// /// A convenience method for [ModelElement.forElement], see its @@ -73,6 +91,13 @@ mixin Nameable { ModelElement getModelForElement(Element element) => ModelElement.forElement(element, packageGraph); + /// Returns the [ModelElement] for [element], instantiating it if needed. + /// + /// A convenience method for [ModelElement.forElement], see its + /// documentation. + ModelElement getModelForElement2(Element2 element) => + ModelElement.forElement(element.asElement!, packageGraph); + /// Returns the [ModelElement] for [element], instantiating it if needed. /// /// A convenience method for [ModelElement.forPropertyInducingElement], see diff --git a/lib/src/model/typedef.dart b/lib/src/model/typedef.dart index 47f3806118..317dcc982b 100644 --- a/lib/src/model/typedef.dart +++ b/lib/src/model/typedef.dart @@ -19,6 +19,7 @@ abstract class Typedef extends ModelElement @override final TypeAliasElement element; + @override TypeAliasElement2 get element2 => element.asElement2; Typedef(this.element, super.library, super.packageGraph); diff --git a/lib/src/type_utils.dart b/lib/src/type_utils.dart index f11b8057d9..d9f56cd648 100644 --- a/lib/src/type_utils.dart +++ b/lib/src/type_utils.dart @@ -5,6 +5,7 @@ // ignore_for_file: analyzer_use_new_elements import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/dart/element/element2.dart'; import 'package:analyzer/dart/element/type.dart'; extension DartTypeExtension on DartType { @@ -22,4 +23,19 @@ extension DartTypeExtension on DartType { _ => null }; } + + /// The static element associataed with this type, where documentable, and + /// `null` otherwise. + /// + /// For example, the documentable element of [DynamicType] is `null`, as there + /// is no documentation for `dynamic` which we can link to. + TypeDefiningElement2? get documentableElement2 { + final self = this; + return switch (self) { + InterfaceType() => self.element3, + NeverType() => self.element3 as TypeDefiningElement2, + TypeParameterType() => self.element3, + _ => null + }; + } } diff --git a/test/mustachio/aot_compiler_builder_test.dart b/test/mustachio/aot_compiler_builder_test.dart index 844932db4b..2925bafa4e 100644 --- a/test/mustachio/aot_compiler_builder_test.dart +++ b/test/mustachio/aot_compiler_builder_test.dart @@ -2,8 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// ignore_for_file: analyzer_use_new_elements - @TestOn('vm && !windows') library; @@ -38,7 +36,7 @@ import 'annotations.dart'; ], ); var renderersLibrary = - await resolveGeneratedLibrary(aotRenderersForHtmlPath); + await resolveGeneratedLibrary2(aotRenderersForHtmlPath); expect(renderersLibrary.getTopLevelFunction('renderFoo'), isNotNull); expect(renderersLibrary.getTopLevelFunction('renderBar'), isNotNull); @@ -107,11 +105,11 @@ class Bar {} class Baz {} '''); var renderersLibrary = - await resolveGeneratedLibrary(aotRenderersForHtmlPath); + await resolveGeneratedLibrary2(aotRenderersForHtmlPath); var fooRenderFunction = renderersLibrary.getTopLevelFunction('renderFoo')!; - expect(fooRenderFunction.typeParameters, hasLength(1)); - var fBound = fooRenderFunction.typeParameters.single.bound!; + expect(fooRenderFunction.typeParameters2, hasLength(1)); + var fBound = fooRenderFunction.typeParameters2.single.bound!; expect(fBound.getDisplayString(), equals('num')); }); diff --git a/test/mustachio/builder_test_base.dart b/test/mustachio/builder_test_base.dart index 37f8587bb7..8eb8395771 100644 --- a/test/mustachio/builder_test_base.dart +++ b/test/mustachio/builder_test_base.dart @@ -6,6 +6,7 @@ import 'package:analyzer/dart/analysis/results.dart'; import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/dart/element/element2.dart'; import 'package:analyzer/file_system/physical_file_system.dart'; import 'package:analyzer/src/dart/analysis/analysis_context_collection.dart' show AnalysisContextCollectionImpl; @@ -102,6 +103,26 @@ Future resolveGeneratedLibrary(String libraryPath) async { return libraryResult.element; } +Future resolveGeneratedLibrary2(String libraryPath) async { + var contextCollection = AnalysisContextCollectionImpl( + includedPaths: [d.sandbox], + // TODO(jcollins-g): should we pass excluded directories here instead of + // handling it ourselves? + resourceProvider: PhysicalResourceProvider.INSTANCE, + sdkPath: sdkPath, + ); + var analysisContext = contextCollection.contextFor(d.sandbox); + final libraryResult = + await analysisContext.currentSession.getResolvedLibrary(libraryPath); + if (libraryResult is! ResolvedLibraryResult) { + throw StateError( + 'Expected library result to be ResolvedLibraryResult, but is ' + '${libraryResult.runtimeType}'); + } + + return libraryResult.element2; +} + extension LibraryExtensions on LibraryElement { /// Returns the top-level function in `this` library, named [name], or `null` /// if no function is found. diff --git a/tool/mustachio/builder.dart b/tool/mustachio/builder.dart index 47fca4d4ce..b2f3febe78 100644 --- a/tool/mustachio/builder.dart +++ b/tool/mustachio/builder.dart @@ -2,8 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// ignore_for_file: analyzer_use_new_elements - import 'dart:io'; import 'package:analyzer/dart/analysis/results.dart'; @@ -48,12 +46,13 @@ Future build( '${libraryResult.runtimeType}'); } - var library = libraryResult.element; + var library = libraryResult.element2; var typeProvider = library.typeProvider; var typeSystem = library.typeSystem; var rendererSpecs = {}; - for (var renderer in library.metadata - .where((e) => e.element!.enclosingElement3!.name == 'Renderer')) { + for (var renderer in library.metadata2 + .annotations + .where((e) => e.element2!.enclosingElement2!.name3 == 'Renderer')) { rendererSpecs.add(_buildRendererSpec(renderer)); }