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

fix: check of constructor exist for prefer-iterable-of #1050

Merged
merged 1 commit into from
Oct 23, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* feat: add static code diagnostic [`avoid-cascade-after-if-null`](https://dartcodemetrics.dev/docs/rules/common/avoid-cascade-after-if-null).
* feat: **Breaking change** handle widget members order separately in [`member-ordering`](https://dartcodemetrics.dev/docs/rules/common/member-ordering).
* feat: support dynamic method names for [`member-ordering`](https://dartcodemetrics.dev/docs/rules/common/member-ordering).
* fix: check `of` constructor exist for [`prefer-iterable-of`](https://dartcodemetrics.dev/docs/rules/common/prefer-iterable-of)

## 4.21.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class _Visitor extends RecursiveAstVisitor<void> {
super.visitInstanceCreationExpression(node);

if (isIterableOrSubclass(node.staticType) &&
node.constructorName.name?.name == 'from') {
node.constructorName.name?.name == 'from' &&
_hasConstructorOf(node.staticType)) {
final arg = node.argumentList.arguments.first;

final argumentType = _getType(arg.staticType);
final castedType = _getType(node.staticType);

if (argumentType != null &&
!argumentType.isDartCoreObject &&
!argumentType.isDynamic &&
Expand All @@ -25,6 +25,14 @@ class _Visitor extends RecursiveAstVisitor<void> {
}
}

bool _hasConstructorOf(DartType? type) {
if (type is InterfaceType) {
return type.constructors.any((element) => element.name == 'of');
}

return false;
}

DartType? _getType(DartType? type) {
if (type == null || type is! InterfaceType) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies:
analyzer_plugin: ">=0.11.0 <0.12.0"
ansicolor: ^2.0.1
args: ^2.0.0
collection: ^1.15.0
collection: ^1.16.0
crypto: ^3.0.0
file: ^6.0.0
glob: ^2.0.1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:collection/collection.dart';

void main() {
const queue = QueueList.of([1, 2, 3, 4, 5, 6, 7, 8, 9]);

final copy = QueueList<int>.from(queue);
final numQueue = QueueList<num>.from(queue);

final intQueue = QueueList<int>.from(numQueue);

final unspecifedQueue = QueueList.from(queue);

final dynamicQueue = QueueList<dynamic>.from([1, 2, 3]);
final copy = QueueList<int>.from(dynamicQueue);
final dynamicCopy = QueueList.from(dynamicQueue);

final objectQueue = QueueList<Object>.from([1, 2, 3]);
final copy = QueueList<int>.from(objectQueue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const _linkedHashSetExamplePath =
const _listQueueExamplePath =
'prefer_iterable_of/examples/list_queue_example.dart';
const _queueExamplePath = 'prefer_iterable_of/examples/queue_example.dart';
const _queueListExamplePath =
'prefer_iterable_of/examples/queue_list_example.dart';
const _splayTreeSetExamplePath =
'prefer_iterable_of/examples/splay_tree_set_example.dart';

Expand Down Expand Up @@ -249,6 +251,21 @@ void main() {
);
});

test('reports about found issues for queue list', () async {
final unit = await RuleTestHelper.resolveFromFile(_queueListExamplePath);
final issues = PreferIterableOfRule().check(unit);

RuleTestHelper.verifyIssues(
issues: issues,
startLines: [],
startColumns: [],
locationTexts: [],
messages: [],
replacements: [],
replacementComments: [],
);
});

test('reports about found issues for splay tree sets', () async {
final unit =
await RuleTestHelper.resolveFromFile(_splayTreeSetExamplePath);
Expand Down