Skip to content

Commit 532e467

Browse files
pqCommit Queue
authored and
Commit Queue
committed
extension type support for prefer_const_constructors_in_immutables
Fixes: https://github.com/dart-lang/linter/issues/4720 Change-Id: Ia55289f53b6191e8511664cfa5b05ebcfe42ed6c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/330180 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent ab22dc0 commit 532e467

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class PreferConstConstructorsInImmutables extends LintRule {
5858
NodeLintRegistry registry, LinterContext context) {
5959
var visitor = _Visitor(this, context);
6060
registry.addConstructorDeclaration(this, visitor);
61+
registry.addExtensionTypeDeclaration(this, visitor);
6162
}
6263
}
6364

@@ -88,6 +89,16 @@ class _Visitor extends SimpleAstVisitor<void> {
8889
}
8990
}
9091

92+
@override
93+
void visitExtensionTypeDeclaration(ExtensionTypeDeclaration node) {
94+
if (node.constKeyword != null) return;
95+
var element = node.declaredElement;
96+
if (element == null) return;
97+
if (element.hasImmutable) {
98+
rule.reportLintForToken(node.name);
99+
}
100+
}
101+
91102
bool _hasConstConstructorInvocation(ConstructorDeclaration node) {
92103
var declaredElement = node.declaredElement;
93104
if (declaredElement == null) {
@@ -107,6 +118,11 @@ class _Visitor extends SimpleAstVisitor<void> {
107118
if (redirectInvocation != null) {
108119
return redirectInvocation.staticElement?.isConst ?? false;
109120
}
121+
122+
if (clazz is ExtensionTypeElement) {
123+
return clazz.primaryConstructor.isConst;
124+
}
125+
110126
// Constructor with implicit `super()` call.
111127
var unnamedSuperConstructor =
112128
clazz.supertype?.constructors.firstWhereOrNull((e) => e.name.isEmpty);

pkg/linter/test/rules/prefer_const_constructors_in_immutables_test.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,46 @@ class B extends A {
8383
''');
8484
}
8585

86+
test_extensionType_constConstructor_named() async {
87+
await assertNoDiagnostics(r'''
88+
import 'package:meta/meta.dart';
89+
@immutable
90+
extension type const E(int i) {
91+
const E.e(this.i);
92+
}
93+
''');
94+
}
95+
96+
test_extensionType_constConstructor_primary() async {
97+
await assertNoDiagnostics(r'''
98+
import 'package:meta/meta.dart';
99+
@immutable
100+
extension type const E(int i) { }
101+
''');
102+
}
103+
104+
test_extensionType_nonConstConstructor_named() async {
105+
await assertDiagnostics(r'''
106+
import 'package:meta/meta.dart';
107+
@immutable
108+
extension type const E(int i) {
109+
E.e(this.i);
110+
}
111+
''', [
112+
lint(78, 1),
113+
]);
114+
}
115+
116+
test_extensionType_nonConstConstructor_primary() async {
117+
await assertDiagnostics(r'''
118+
import 'package:meta/meta.dart';
119+
@immutable
120+
extension type E(int i) { }
121+
''', [
122+
lint(59, 1),
123+
]);
124+
}
125+
86126
test_immutable_constConstructor() async {
87127
await assertNoDiagnostics(r'''
88128
import 'package:meta/meta.dart';

0 commit comments

Comments
 (0)