Skip to content

Commit fef2bef

Browse files
pqCommit Queue
authored andcommitted
[element model] migrate prefer_const_constructors_in_immutables
Bug:https://github.com/dart-lang/linter/issues/5099 Change-Id: I741e7ac8946a44ff800117c3c1f3657c15aaeb3f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390941 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Auto-Submit: Phil Quitslund <[email protected]>
1 parent 576dbd7 commit fef2bef

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

pkg/analyzer/lib/dart/element/element2.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ abstract class ClassElement2 implements InterfaceElement2 {
162162
/// not extended or mixed in.
163163
bool get isInterface;
164164

165+
/// Whether the class is a macro class.
166+
///
167+
/// A class is a macro class if it has a `macro` modifer.
168+
bool get isMacro;
169+
165170
/// Whether the class is a mixin application.
166171
///
167172
/// A class is a mixin application if it was declared using the syntax

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6839,6 +6839,9 @@ mixin MaybeAugmentedClassElementMixin on MaybeAugmentedInterfaceElementMixin
68396839
@override
68406840
bool get isInterface => declaration.isInterface;
68416841

6842+
@override
6843+
bool get isMacro => declaration.isMacro;
6844+
68426845
@override
68436846
bool get isMixinApplication => declaration.isMixinApplication;
68446847

@@ -6930,8 +6933,7 @@ mixin MaybeAugmentedExtensionTypeElementMixin
69306933
ExtensionTypeElementImpl get firstFragment => declaration;
69316934

69326935
@override
6933-
ConstructorElement2 get primaryConstructor2 =>
6934-
representation as ConstructorElement2;
6936+
ConstructorElement2 get primaryConstructor2 => primaryConstructor.element;
69356937

69366938
@override
69376939
FieldElement2 get representation2 => representation as FieldElement2;

pkg/linter/analyzer_use_new_elements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ lib/src/rules/prefer_bool_in_asserts.dart
112112
lib/src/rules/prefer_collection_literals.dart
113113
lib/src/rules/prefer_conditional_assignment.dart
114114
lib/src/rules/prefer_const_constructors.dart
115+
lib/src/rules/prefer_const_constructors_in_immutables.dart
115116
lib/src/rules/prefer_const_declarations.dart
116117
lib/src/rules/prefer_const_literals_to_create_immutables.dart
117118
lib/src/rules/prefer_constructors_over_static_methods.dart

pkg/linter/lib/src/extensions.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,11 @@ extension ElementExtension2 on Element2? {
382382
self.name == 'print' &&
383383
self.firstFragment.libraryFragment.element.isDartCore;
384384
}
385+
386+
bool get isMacro => switch (this) {
387+
ClassElement2(:var isMacro) => isMacro,
388+
_ => false,
389+
};
385390
}
386391

387392
extension ExpressionExtension on Expression? {

pkg/linter/lib/src/rules/prefer_const_constructors_in_immutables.dart

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/visitor.dart';
7-
import 'package:analyzer/dart/element/element.dart';
7+
import 'package:analyzer/dart/element/element2.dart';
88
import 'package:analyzer/src/lint/linter.dart'; // ignore: implementation_imports
99
import 'package:collection/collection.dart' show IterableExtension;
1010

@@ -40,17 +40,18 @@ class _Visitor extends SimpleAstVisitor<void> {
4040

4141
@override
4242
void visitConstructorDeclaration(ConstructorDeclaration node) {
43-
var element = node.declaredElement;
43+
var element = node.declaredFragment?.element;
4444
if (element == null) return;
4545
if (element.isConst) return;
4646
if (node.body is! EmptyFunctionBody) return;
47-
var enclosingElement = element.enclosingElement3;
47+
var enclosingElement = element.enclosingElement2;
4848
if (enclosingElement.isMacro) return;
49+
4950
if (enclosingElement.mixins.isNotEmpty) return;
5051
if (!_hasImmutableAnnotation(enclosingElement)) return;
5152
var isRedirected =
52-
element.isFactory && element.redirectedConstructor != null;
53-
if (isRedirected && (element.redirectedConstructor?.isConst ?? false)) {
53+
element.isFactory && element.redirectedConstructor2 != null;
54+
if (isRedirected && (element.redirectedConstructor2?.isConst ?? false)) {
5455
rule.reportLintForToken(node.firstTokenAfterCommentAndMetadata);
5556
}
5657
if (!isRedirected &&
@@ -63,56 +64,57 @@ class _Visitor extends SimpleAstVisitor<void> {
6364
@override
6465
void visitExtensionTypeDeclaration(ExtensionTypeDeclaration node) {
6566
if (node.constKeyword != null) return;
66-
var element = node.declaredElement;
67+
var element = node.declaredFragment?.element;
6768
if (element == null) return;
68-
if (element.hasImmutable) {
69+
if (element.metadata2.hasImmutable) {
6970
rule.reportLintForToken(node.name);
7071
}
7172
}
7273

73-
static List<InterfaceElement> _getSelfAndSuperClasses(InterfaceElement self) {
74-
InterfaceElement? current = self;
75-
var seenElements = <InterfaceElement>{};
74+
static List<InterfaceElement2> _getSelfAndSuperClasses(
75+
InterfaceElement2 self) {
76+
InterfaceElement2? current = self;
77+
var seenElements = <InterfaceElement2>{};
7678
while (current != null && seenElements.add(current)) {
77-
current = current.supertype?.element;
79+
current = current.supertype?.element3;
7880
}
7981
return seenElements.toList();
8082
}
8183

8284
static bool _hasConstConstructorInvocation(ConstructorDeclaration node) {
83-
var declaredElement = node.declaredElement;
85+
var declaredElement = node.declaredFragment?.element;
8486
if (declaredElement == null) {
8587
return false;
8688
}
87-
var clazz = declaredElement.enclosingElement3;
89+
var clazz = declaredElement.enclosingElement2;
8890
// Constructor with super-initializer.
8991
var superInvocation =
9092
node.initializers.whereType<SuperConstructorInvocation>().firstOrNull;
9193
if (superInvocation != null) {
92-
return superInvocation.staticElement?.isConst ?? false;
94+
return superInvocation.element?.isConst ?? false;
9395
}
9496
// Constructor with 'this' redirecting initializer.
9597
var redirectInvocation = node.initializers
9698
.whereType<RedirectingConstructorInvocation>()
9799
.firstOrNull;
98100
if (redirectInvocation != null) {
99-
return redirectInvocation.staticElement?.isConst ?? false;
101+
return redirectInvocation.element?.isConst ?? false;
100102
}
101103

102-
if (clazz is ExtensionTypeElement) {
103-
return clazz.primaryConstructor.isConst;
104+
if (clazz is ExtensionTypeElement2) {
105+
return clazz.primaryConstructor2.isConst;
104106
}
105107

106108
// Constructor with implicit `super()` call.
107109
var unnamedSuperConstructor =
108-
clazz.supertype?.constructors.firstWhereOrNull((e) => e.name.isEmpty);
110+
clazz.supertype?.constructors2.firstWhereOrNull((e) => e.name.isEmpty);
109111
return unnamedSuperConstructor != null && unnamedSuperConstructor.isConst;
110112
}
111113

112114
/// Whether [clazz] or any of its super-types are annotated with
113115
/// `@immutable`.
114-
static bool _hasImmutableAnnotation(InterfaceElement clazz) {
116+
static bool _hasImmutableAnnotation(InterfaceElement2 clazz) {
115117
var selfAndInheritedClasses = _getSelfAndSuperClasses(clazz);
116-
return selfAndInheritedClasses.any((cls) => cls.hasImmutable);
118+
return selfAndInheritedClasses.any((cls) => cls.metadata2.hasImmutable);
117119
}
118120
}

0 commit comments

Comments
 (0)