Skip to content
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
50 changes: 50 additions & 0 deletions lib/src/generator/templates.runtime_renderers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20472,6 +20472,34 @@ class _Renderer_Parameter extends RendererBase<Parameter> {
);
},
),
'element2': Property(
getValue: (CT_ c) => c.element2,
renderVariable:
(CT_ c, Property<CT_> self, List<String> remainingNames) =>
self.renderSimpleVariable(
c,
remainingNames,
'FormalParameterElement',
),

isNullValue: (CT_ c) => false,

renderValue: (
CT_ c,
RendererBase<CT_> r,
List<MustachioNode> ast,
StringSink sink,
) {
renderSimple(
c.element2,
ast,
r.template,
sink,
parent: r,
getters: _invisibleGetters['FormalParameterElement']!,
);
},
),
'enclosingElement': Property(
getValue: (CT_ c) => c.enclosingElement,
renderVariable: (
Expand Down Expand Up @@ -26105,6 +26133,28 @@ const _invisibleGetters = {
'runtimeType',
},
'File': {'hashCode', 'lengthSync', 'modificationStamp', 'runtimeType'},
'FormalParameterElement': {
'baseElement',
'defaultValueCode',
'firstFragment',
'formalParameters',
'fragments',
'hasDefaultValue',
'hashCode',
'isCovariant',
'isInitializingFormal',
'isNamed',
'isOptional',
'isOptionalNamed',
'isOptionalPositional',
'isPositional',
'isRequired',
'isRequiredNamed',
'isRequiredPositional',
'isSuperFormal',
'runtimeType',
'typeParameters2',
},
'FunctionElement': {
'augmentation',
'augmentationTarget',
Expand Down
8 changes: 8 additions & 0 deletions lib/src/model/container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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/scope.dart';
import 'package:dartdoc/src/model/comment_referable.dart';
import 'package:dartdoc/src/model/model.dart';
Expand Down Expand Up @@ -158,9 +159,16 @@ abstract class Container extends ModelElement
/// See [Inheritable.canonicalEnclosingContainer].
bool containsElement(Element? element) => _allElements.contains(element);

/// This container might be canonical for elements it does not contain.
/// See [Inheritable.canonicalEnclosingContainer].
bool containsElement2(Element2? element) => _allElements2.contains(element);

late final Set<Element> _allElements =
allModelElements.map((e) => e.element).toSet();

late final Set<Element2> _allElements2 =
allModelElements.map((e) => e.element2).toSet();

bool get hasPublicStaticFields => staticFields.any((e) => e.isPublic);

List<Field> get publicStaticFieldsSorted =>
Expand Down
18 changes: 8 additions & 10 deletions lib/src/model/inheritable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// 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:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:collection/collection.dart' show IterableExtension;
import 'package:dartdoc/src/model/attribute.dart';
import 'package:dartdoc/src/model/comment_referable.dart';
Expand All @@ -16,7 +14,7 @@ import 'package:dartdoc/src/model/model.dart';
/// We can search the inheritance chain between this instance and
/// [definingEnclosingContainer] in [Inheritable.canonicalEnclosingContainer],
/// for the canonical [Class] closest to where this member was defined. We
/// can then know that when we find [Inheritable.element] inside that [Class]'s
/// can then know that when we find [Inheritable.element2] inside that [Class]'s
/// namespace, that's the one we should treat as canonical and implementors of
/// this class can use that knowledge to determine canonicalization.
///
Expand Down Expand Up @@ -51,19 +49,19 @@ mixin Inheritable on ContainerMember {
?.allCanonicalModelElements
.firstWhereOrNull((m) =>
m.name == name &&
m is PropertyAccessorElement == this is PropertyAccessorElement);
m is PropertyAccessorElement2 == this is PropertyAccessorElement2);

@override
Container? computeCanonicalEnclosingContainer() {
if (isInherited) {
var searchElement = element.declaration;
var searchElement = element2.baseElement;
// TODO(jcollins-g): generate warning if an inherited element's definition
// is in an intermediate non-canonical class in the inheritance chain?
Container? found;
var reverseInheritance = _inheritance.reversed.toList();
for (var i = 0; i < reverseInheritance.length; i++) {
var container = reverseInheritance[i];
if (container.containsElement(searchElement)) {
if (container.containsElement2(searchElement)) {
var previousIsHiddenAndNotDefining = i > 0 &&
_isHiddenInterface(reverseInheritance[i - 1]) &&
container != definingEnclosingContainer;
Expand Down Expand Up @@ -97,7 +95,7 @@ mixin Inheritable on ContainerMember {
// starting from the ModelElement.
if (canonicalContainer != null) {
assert(canonicalContainer.isCanonical);
assert(canonicalContainer.containsElement(searchElement));
assert(canonicalContainer.containsElement2(searchElement));
found = canonicalContainer;
break;
}
Expand Down Expand Up @@ -125,8 +123,8 @@ mixin Inheritable on ContainerMember {
/// implementation.
bool _isHiddenInterface(Container? c) =>
c != null &&
c.element.name == 'Interceptor' &&
c.element.library?.name == '_interceptors';
c.element2.name3 == 'Interceptor' &&
c.element2.library2?.name3 == '_interceptors';

/// A roughly ordered list of this element's enclosing container's inheritance
/// chain.
Expand Down
13 changes: 7 additions & 6 deletions lib/src/model/model_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ abstract class ModelElement
}
if (e.kind == ElementKind.NEVER) {
return packageGraph.allConstructedModelElements[key] =
NeverType(e, packageGraph);
NeverType(e.asElement2!, packageGraph);
}

var newModelElement = ModelElement._constructFromElementDeclaration(
Expand Down Expand Up @@ -299,7 +299,7 @@ abstract class ModelElement
}) {
return switch (e) {
LibraryElement() => packageGraph.findButDoNotCreateLibraryFor(e)!,
PrefixElement() => Prefix(e, library, packageGraph),
PrefixElement() => Prefix(e.asElement2, library, packageGraph),
EnumElement() => Enum(e, library, packageGraph),
MixinElement() => Mixin(e, library, packageGraph),
ClassElement() => Class(e, library, packageGraph),
Expand All @@ -310,12 +310,13 @@ abstract class ModelElement
GenericFunctionTypeElement() =>
ModelFunctionTypedef(e, library, packageGraph),
TypeAliasElement(aliasedType: FunctionType()) =>
FunctionTypedef(e, library, packageGraph),
FunctionTypedef(e.asElement2, library, packageGraph),
TypeAliasElement()
when e.aliasedType.documentableElement2.asElement
is InterfaceElement =>
ClassTypedef(e, library, packageGraph),
TypeAliasElement() => GeneralizedTypedef(e, library, packageGraph),
ClassTypedef(e.asElement2, library, packageGraph),
TypeAliasElement() =>
GeneralizedTypedef(e.asElement2, library, packageGraph),
MethodElement(isOperator: true) when enclosingContainer == null =>
Operator(e.asElement2, library, packageGraph),
MethodElement(isOperator: true)
Expand All @@ -334,7 +335,7 @@ abstract class ModelElement
MethodElement(isOperator: false) => Method.inherited(
e.asElement2, enclosingContainer, library, packageGraph,
originalMember: originalMember as ExecutableMember?),
ParameterElement() => Parameter(e, library, packageGraph,
ParameterElement() => Parameter(e.asElement2, library, packageGraph,
originalMember: originalMember as ParameterMember?),
PropertyAccessorElement() => _constructFromPropertyAccessor(
e,
Expand Down
13 changes: 9 additions & 4 deletions lib/src/model/never.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@
// 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:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
// ignore: implementation_imports
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to remember to search for imports of these extensions before we declare the package fully converted.

Alternatively, you could remove element and use .element2.asElement in any unconverted files. That might be less error prone.

Copy link
Collaborator Author

@keertip keertip Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Will use that moving forward and change these in a follow up CL.

import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:dartdoc/src/model/comment_referable.dart';
import 'package:dartdoc/src/model/kind.dart';
import 'package:dartdoc/src/model/model.dart';

class NeverType extends ModelElement with HasNoPage {
@override
final Element element;
// ignore: analyzer_use_new_elements
Element get element => element2.asElement!;

@override
final Element2 element2;

NeverType(this.element, PackageGraph packageGraph)
NeverType(this.element2, PackageGraph packageGraph)
: super(Library.sentinel, packageGraph);

/// `Never` is not a real object, and so we can't document it, so there
Expand Down
57 changes: 33 additions & 24 deletions lib/src/model/parameter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,88 @@
// 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:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
// ignore: implementation_imports
import 'package:analyzer/src/dart/element/member.dart' show ParameterMember;
// ignore: implementation_imports
import 'package:analyzer/src/utilities/extensions/element.dart';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

import 'package:dartdoc/src/element_type.dart';
import 'package:dartdoc/src/model/comment_referable.dart';
import 'package:dartdoc/src/model/kind.dart';
import 'package:dartdoc/src/model/model.dart';

class Parameter extends ModelElement with HasNoPage {
@override
final ParameterElement element;
// ignore: analyzer_use_new_elements
ParameterElement get element => element2.asElement;

Parameter(this.element, super.library, super.packageGraph,
@override
final FormalParameterElement element2;

Parameter(this.element2, super.library, super.packageGraph,
{ParameterMember? super.originalMember});

String? get defaultValue => hasDefaultValue ? element.defaultValueCode : null;
String? get defaultValue =>
hasDefaultValue ? element2.defaultValueCode : null;

@override
ModelElement? get enclosingElement {
final enclosingElement = element.enclosingElement3;
final enclosingElement = element2.enclosingElement2;
return enclosingElement == null
? null
: getModelFor(enclosingElement, library);
: getModelFor2(enclosingElement, library);
}

bool get hasDefaultValue {
return element.defaultValueCode != null &&
element.defaultValueCode!.isNotEmpty;
return element2.defaultValueCode != null &&
element2.defaultValueCode!.isNotEmpty;
}

@override
String? get href => null;

@override
String get htmlId {
final enclosingElement = element.enclosingElement3;
final enclosingElement = element2.enclosingElement2;
if (enclosingElement == null) {
return 'param-$name';
}
var enclosingName = enclosingElement.name;
if (enclosingElement is GenericFunctionTypeElement) {
var enclosingName = enclosingElement.lookupName;
if (enclosingName == 'new') {
enclosingName = '';
}
if (enclosingElement is GenericFunctionTypeElement2) {
// TODO(jcollins-g): Drop when GenericFunctionTypeElement populates
// name. Also, allowing null here is allowed as a workaround for
// dart-lang/sdk#32005.
for (Element e = enclosingElement;
e.enclosingElement3 != null;
e = e.enclosingElement3!) {
enclosingName = e.name;
for (Element2 e = enclosingElement;
e.enclosingElement2 != null;
e = e.enclosingElement2!) {
enclosingName = e.lookupName;
if (enclosingName != null && enclosingName.isNotEmpty) break;
}
}
return '$enclosingName-param-$name';
}

@override
int get hashCode => element.hashCode;
int get hashCode => element2.hashCode;

@override
bool operator ==(Object other) =>
other is Parameter && (element.type == other.element.type);
other is Parameter && (element2.type == other.element2.type);

bool get isCovariant => element.isCovariant;
bool get isCovariant => element2.isCovariant;

bool get isRequiredPositional => element.isRequiredPositional;
bool get isRequiredPositional => element2.isRequiredPositional;

bool get isNamed => element.isNamed;
bool get isNamed => element2.isNamed;

bool get isOptionalPositional => element.isOptionalPositional;
bool get isOptionalPositional => element2.isOptionalPositional;

/// Only true if this is a required named parameter.
bool get isRequiredNamed => element.isRequiredNamed;
bool get isRequiredNamed => element2.isRequiredNamed;

@override
Kind get kind => Kind.parameter;
Expand Down Expand Up @@ -102,5 +111,5 @@ class Parameter extends ModelElement with HasNoPage {
super.originalMember as ParameterMember?;

late final ElementType modelType =
getTypeFor((originalMember ?? element).type, library);
getTypeFor((originalMember ?? element2).type, library);
}
Loading