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

fix: support tear-off methods for check-unnecessary-nullable #1154

Merged
merged 1 commit into from
Jan 19, 2023
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 @@ -11,6 +11,7 @@
* fix: ignore method invocations in a variable declaration for [`prefer-moving-to-variable`](https://dartcodemetrics.dev/docs/rules/common/prefer-moving-to-variable).
* feat: add `allow-initialized` option to [`avoid-late-keyword`](https://dartcodemetrics.dev/docs/rules/common/avoid-late-keyword).
* feat: add `ignored-types` option to [`avoid-late-keyword`](https://dartcodemetrics.dev/docs/rules/common/avoid-late-keyword).
* fix: support tear-off methods for `check-unnecessary-nullable`.

## 5.4.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';

import 'models/invocations_usage.dart';

Expand All @@ -20,6 +21,15 @@ class InvocationsVisitor extends RecursiveAstVisitor<void> {
}
}

@override
void visitPrefixedIdentifier(PrefixedIdentifier node) {
super.visitPrefixedIdentifier(node);

if (node.identifier.staticType is FunctionType) {
_recordUsedElement(node.identifier.staticElement, null);
}
}

@override
void visitMethodInvocation(MethodInvocation node) {
super.visitMethodInvocation(node);
Expand All @@ -42,7 +52,7 @@ class InvocationsVisitor extends RecursiveAstVisitor<void> {
}

/// Records use of a not prefixed [element].
void _recordUsedElement(Element? element, ArgumentList arguments) {
void _recordUsedElement(Element? element, ArgumentList? arguments) {
if (element == null) {
return;
}
Expand All @@ -52,6 +62,7 @@ class InvocationsVisitor extends RecursiveAstVisitor<void> {
return;
}
// Remember the element.
invocationsUsages.addElementUsage(element, {arguments});
final usedArguments = arguments == null ? null : {arguments};
invocationsUsages.addElementUsage(element, usedArguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import 'package:analyzer/dart/element/element.dart';
/// elements.
class InvocationsUsage {
/// The set of referenced top-level elements.
final Map<Element, Set<ArgumentList>> elements = {};
final Map<Element, Set<ArgumentList>?> elements = {};

final Set<String> exports = {};

void addElementUsage(Element element, Set<ArgumentList> expressions) {
void addElementUsage(Element element, Set<ArgumentList>? expressions) {
elements.update(
element,
(value) => value..addAll(expressions),
(value) {
if (expressions == null || value == null) {
return null;
}

return value..addAll(expressions);
},
ifAbsent: () => expressions,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ class ClassWithMethods {

// ignore: unnecessary-nullable
void ignoredAlwaysNonNullable(String? anotherValue) {}

void tearOff(String? anotherValue) {}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class MyWidget extends StatelessWidget {
const MyWidget(Key? key) : super(key);
const MyWidget(super.key);
}

class Widget {
Expand All @@ -9,9 +9,15 @@ class Widget {
}

class StatelessWidget extends Widget {
const StatelessWidget(Key? key) : super(key);
const StatelessWidget(super.key);
}

class Key {}

class GlobalKey extends Key {}

class AnotherWidget extends Widget {
final void Function(String?) onSubmit;

const AnotherWidget({required this.onSubmit}) : super(null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ void main() {

withMethods
..someMethod(null)
..someMethod('value');
..someMethod('value')
..tearOff('str');

withMethods.alwaysNonNullable('anotherValue');
withMethods.ignoredAlwaysNonNullable('anotherValue');
Expand Down Expand Up @@ -44,6 +45,8 @@ void main() {
multipleParametersWithOptional('name', 1, 'secondName');

MyWidget(GlobalKey());

AnotherWidget(onSubmit: withMethods.tearOff);
}

class _Test {
Expand Down