Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

super_parameters support for tighten_type_of_initializing_formals #3167

Merged
merged 4 commits into from
Jan 24, 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
11 changes: 10 additions & 1 deletion lib/src/rules/avoid_annotating_with_dynamic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class AvoidAnnotatingWithDynamic extends LintRule {
NodeLintRegistry registry, LinterContext context) {
var visitor = _Visitor(this);
registry.addSimpleFormalParameter(this, visitor);
registry.addSuperFormalParameter(this, visitor);
}
}

Expand All @@ -59,7 +60,15 @@ class _Visitor extends SimpleAstVisitor<void> {

@override
void visitSimpleFormalParameter(SimpleFormalParameter node) {
var type = node.type;
_checkNode(node, node.type);
}

@override
void visitSuperFormalParameter(SuperFormalParameter node) {
_checkNode(node, node.type);
}

void _checkNode(NormalFormalParameter node, TypeAnnotation? type) {
if (type is NamedType && type.name.name == 'dynamic') {
rule.reportLint(node);
}
Expand Down
27 changes: 14 additions & 13 deletions lib/src/rules/tighten_type_of_initializing_formals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ class TightenTypeOfInitializingFormals extends LintRule {
}

class _Visitor extends SimpleAstVisitor<void> {
_Visitor(this.rule, this.context);

final LintRule rule;

final LinterContext context;
_Visitor(this.rule, this.context);

@override
void visitConstructorDeclaration(ConstructorDeclaration node) {
node.initializers
var parameterElements = node.initializers
.whereType<AssertInitializer>()
.map((e) => e.condition)
.whereType<BinaryExpression>()
Expand All @@ -78,15 +78,16 @@ class _Visitor extends SimpleAstVisitor<void> {
: null)
.whereType<Identifier>()
.where((e) {
var staticType = e.staticType;
return staticType != null &&
context.typeSystem.isNullable(staticType);
})
.map((e) => e.staticElement)
.whereType<FieldFormalParameterElement>()
.forEach((e) {
rule.reportLint(node.parameters.parameters
.firstWhere((p) => p.declaredElement == e));
});
var staticType = e.staticType;
return staticType != null && context.typeSystem.isNullable(staticType);
}).map((e) => e.staticElement);

for (var parameterElement in parameterElements) {
if (parameterElement is FieldFormalParameterElement ||
parameterElement is SuperFormalParameterElement) {
rule.reportLint(node.parameters.parameters
.firstWhere((p) => p.declaredElement == parameterElement));
}
}
}
}
5 changes: 5 additions & 0 deletions test/rules/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'avoid_annotating_with_dynamic.dart' as avoid_annotating_with_dynamic;
import 'avoid_function_literals_in_foreach_calls.dart'
as avoid_function_literals_in_foreach_calls;
import 'avoid_init_to_null.dart' as avoid_init_to_null;
Expand Down Expand Up @@ -38,13 +39,16 @@ import 'prefer_generic_function_type_aliases.dart'
as prefer_generic_function_type_aliases;
import 'prefer_spread_collections.dart' as prefer_spread_collections;
import 'super_goes_last.dart' as super_goes_last;
import 'tighten_type_of_initializing_formals.dart'
as tighten_type_of_initializing_formals;
import 'type_init_formals.dart' as type_init_formals;
import 'unawaited_futures.dart' as unawaited_futures;
import 'unnecessary_null_checks.dart' as unnecessary_null_checks;
import 'use_is_even_rather_than_modulo.dart' as use_is_even_rather_than_modulo;
import 'void_checks.dart' as void_checks;

void main() {
avoid_annotating_with_dynamic.main();
avoid_function_literals_in_foreach_calls.main();
avoid_init_to_null.main();
avoid_redundant_argument_values.main();
Expand All @@ -70,6 +74,7 @@ void main() {
prefer_generic_function_type_aliases.main();
prefer_spread_collections.main();
super_goes_last.main();
tighten_type_of_initializing_formals.main();
type_init_formals.main();
unawaited_futures.main();
unnecessary_null_checks.main();
Expand Down
40 changes: 40 additions & 0 deletions test/rules/avoid_annotating_with_dynamic.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:test_reflective_loader/test_reflective_loader.dart';

import '../rule_test_support.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(AvoidAnnotatingWithDynamicTest);
});
}

@reflectiveTest
class AvoidAnnotatingWithDynamicTest extends LintRuleTest {
@override
List<String> get experiments => [
EnableString.super_parameters,
];

@override
String get lintRule => 'avoid_annotating_with_dynamic';

test_super() async {
await assertDiagnostics(r'''
class A {
var a;
var b;
A(this.a, this.b);
}
class B extends A {
B(dynamic super.a, dynamic super.b);
}
''', [
lint('avoid_annotating_with_dynamic', 75, 15),
lint('avoid_annotating_with_dynamic', 92, 15),
]);
}
}
43 changes: 43 additions & 0 deletions test/rules/tighten_type_of_initializing_formals.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:test_reflective_loader/test_reflective_loader.dart';

import '../rule_test_support.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(TightenTypeOfInitializingFormalsTest);
});
}

@reflectiveTest
class TightenTypeOfInitializingFormalsTest extends LintRuleTest {
@override
List<String> get experiments => [
EnableString.super_parameters,
];

@override
String get lintRule => 'tighten_type_of_initializing_formals';

test_superInit() async {
await assertDiagnostics(r'''
class A {
String? a;
A(this.a);
}

class B extends A {
B(String super.a);
}

class C extends A {
C(super.a) : assert(a != null);
}
''', [
lint('tighten_type_of_initializing_formals', 107, 7),
]);
}
}