Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit 6608751

Browse files
committed
fix: check of constructor exist for prefer-iterable-of
1 parent 1b6a848 commit 6608751

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* feat: add static code diagnostic [`avoid-cascade-after-if-null`](https://dartcodemetrics.dev/docs/rules/common/avoid-cascade-after-if-null).
99
* feat: **Breaking change** handle widget members order separately in [`member-ordering`](https://dartcodemetrics.dev/docs/rules/common/member-ordering).
1010
* feat: support dynamic method names for [`member-ordering`](https://dartcodemetrics.dev/docs/rules/common/member-ordering).
11+
* fix: check `of` constructor exist for [`prefer-iterable-of`](https://dartcodemetrics.dev/docs/rules/common/prefer-iterable-of)
1112

1213
## 4.21.2
1314

lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_iterable_of/visitor.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class _Visitor extends RecursiveAstVisitor<void> {
1010
super.visitInstanceCreationExpression(node);
1111

1212
if (isIterableOrSubclass(node.staticType) &&
13-
node.constructorName.name?.name == 'from') {
13+
node.constructorName.name?.name == 'from' &&
14+
_hasConstructorOf(node.staticType)) {
1415
final arg = node.argumentList.arguments.first;
1516

1617
final argumentType = _getType(arg.staticType);
1718
final castedType = _getType(node.staticType);
18-
1919
if (argumentType != null &&
2020
!argumentType.isDartCoreObject &&
2121
!argumentType.isDynamic &&
@@ -25,6 +25,14 @@ class _Visitor extends RecursiveAstVisitor<void> {
2525
}
2626
}
2727

28+
bool _hasConstructorOf(DartType? type) {
29+
if (type is InterfaceType) {
30+
return type.constructors.any((element) => element.name == 'of');
31+
}
32+
33+
return false;
34+
}
35+
2836
DartType? _getType(DartType? type) {
2937
if (type == null || type is! InterfaceType) {
3038
return null;

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ dependencies:
1414
analyzer_plugin: ">=0.11.0 <0.12.0"
1515
ansicolor: ^2.0.1
1616
args: ^2.0.0
17-
collection: ^1.15.0
17+
collection: ^1.16.0
1818
crypto: ^3.0.0
1919
file: ^6.0.0
2020
glob: ^2.0.1
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'package:collection/collection.dart';
2+
3+
void main() {
4+
const queue = QueueList.of([1, 2, 3, 4, 5, 6, 7, 8, 9]);
5+
6+
final copy = QueueList<int>.from(queue);
7+
final numQueue = QueueList<num>.from(queue);
8+
9+
final intQueue = QueueList<int>.from(numQueue);
10+
11+
final unspecifedQueue = QueueList.from(queue);
12+
13+
final dynamicQueue = QueueList<dynamic>.from([1, 2, 3]);
14+
final copy = QueueList<int>.from(dynamicQueue);
15+
final dynamicCopy = QueueList.from(dynamicQueue);
16+
17+
final objectQueue = QueueList<Object>.from([1, 2, 3]);
18+
final copy = QueueList<int>.from(objectQueue);
19+
}

test/src/analyzers/lint_analyzer/rules/rules_list/prefer_iterable_of/prefer_iterable_of_rule_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const _linkedHashSetExamplePath =
1414
const _listQueueExamplePath =
1515
'prefer_iterable_of/examples/list_queue_example.dart';
1616
const _queueExamplePath = 'prefer_iterable_of/examples/queue_example.dart';
17+
const _queueListExamplePath =
18+
'prefer_iterable_of/examples/queue_list_example.dart';
1719
const _splayTreeSetExamplePath =
1820
'prefer_iterable_of/examples/splay_tree_set_example.dart';
1921

@@ -249,6 +251,21 @@ void main() {
249251
);
250252
});
251253

254+
test('reports about found issues for queue list', () async {
255+
final unit = await RuleTestHelper.resolveFromFile(_queueListExamplePath);
256+
final issues = PreferIterableOfRule().check(unit);
257+
258+
RuleTestHelper.verifyIssues(
259+
issues: issues,
260+
startLines: [],
261+
startColumns: [],
262+
locationTexts: [],
263+
messages: [],
264+
replacements: [],
265+
replacementComments: [],
266+
);
267+
});
268+
252269
test('reports about found issues for splay tree sets', () async {
253270
final unit =
254271
await RuleTestHelper.resolveFromFile(_splayTreeSetExamplePath);

0 commit comments

Comments
 (0)