Skip to content

Commit 5447d07

Browse files
pqCommit Queue
authored andcommitted
[element model] migrate use_super_parameters
Bug: https://github.com/dart-lang/linter/issues/5099 Change-Id: I0b664c7a0ebeb5e71e526cf88e8d2361bf2efd11 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391496 Auto-Submit: Phil Quitslund <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 38a3b21 commit 5447d07

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

pkg/linter/analyzer_use_new_elements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ lib/src/rules/avoid_relative_lib_imports.dart
3434
lib/src/rules/avoid_return_types_on_setters.dart
3535
lib/src/rules/avoid_returning_null.dart
3636
lib/src/rules/avoid_returning_null_for_future.dart
37+
lib/src/rules/avoid_returning_null_for_void.dart
3738
lib/src/rules/avoid_returning_this.dart
3839
lib/src/rules/avoid_shadowing_type_parameters.dart
3940
lib/src/rules/avoid_single_cascade_in_expression_statements.dart
@@ -201,6 +202,7 @@ lib/src/rules/use_named_constants.dart
201202
lib/src/rules/use_raw_strings.dart
202203
lib/src/rules/use_rethrow_when_possible.dart
203204
lib/src/rules/use_string_in_part_of_directives.dart
205+
lib/src/rules/use_super_parameters.dart
204206
lib/src/rules/use_test_throws_matchers.dart
205207
lib/src/rules/use_to_and_as_if_applicable.dart
206208
lib/src/rules/use_truncating_division.dart

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

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'package:analyzer/dart/analysis/features.dart';
66
import 'package:analyzer/dart/ast/ast.dart';
77
import 'package:analyzer/dart/ast/visitor.dart';
8-
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/dart/element/element2.dart';
99
import 'package:analyzer/src/utilities/extensions/string.dart'; // ignore: implementation_imports
1010

1111
import '../analyzer.dart';
@@ -15,7 +15,7 @@ const _desc = r'Use super-initializer parameters where possible.';
1515

1616
/// Return a set containing the elements of all of the parameters that are
1717
/// referenced in the body of the [constructor].
18-
Set<ParameterElement> _referencedParameters(
18+
Set<FormalParameterElement> _referencedParameters(
1919
ConstructorDeclaration constructor) {
2020
var collector = _ReferencedParameterCollector();
2121
constructor.body.accept(collector);
@@ -47,12 +47,12 @@ class UseSuperParameters extends LintRule {
4747
}
4848

4949
class _ReferencedParameterCollector extends RecursiveAstVisitor<void> {
50-
final Set<ParameterElement> foundParameters = {};
50+
final Set<FormalParameterElement> foundParameters = {};
5151

5252
@override
5353
void visitSimpleIdentifier(SimpleIdentifier node) {
54-
var element = node.staticElement;
55-
if (element is ParameterElement) {
54+
var element = node.element;
55+
if (element is FormalParameterElement) {
5656
foundParameters.add(element);
5757
}
5858
}
@@ -68,7 +68,7 @@ class _Visitor extends SimpleAstVisitor<void> {
6868
ConstructorDeclaration node,
6969
SuperConstructorInvocation superInvocation,
7070
FormalParameterList parameters) {
71-
var constructorElement = superInvocation.staticElement;
71+
var constructorElement = superInvocation.element;
7272
if (constructorElement == null) return;
7373

7474
// TODO(pq): consolidate logic shared w/ server
@@ -83,9 +83,9 @@ class _Visitor extends SimpleAstVisitor<void> {
8383
if (identifiers == null) return;
8484

8585
for (var parameter in parameters.parameters) {
86-
var parameterElement = parameter.declaredElement;
86+
var parameterElement = parameter.declaredFragment?.element;
8787
if (parameterElement == null) continue;
88-
if (parameterElement is FieldFormalParameterElement) continue;
88+
if (parameterElement is FieldFormalParameterElement2) continue;
8989
if (parameterElement.isNamed &&
9090
!referencedParameters.contains(parameterElement)) {
9191
if (_checkNamedParameter(
@@ -116,10 +116,10 @@ class _Visitor extends SimpleAstVisitor<void> {
116116
/// there are parameters that can't be converted since this will short-circuit
117117
/// the lint.
118118
List<String>? _checkForConvertiblePositionalParams(
119-
ConstructorElement constructorElement,
119+
ConstructorElement2 constructorElement,
120120
SuperConstructorInvocation superInvocation,
121121
FormalParameterList parameters,
122-
Set<ParameterElement> referencedParameters) {
122+
Set<FormalParameterElement> referencedParameters) {
123123
var positionalSuperArgs = <SimpleIdentifier>[];
124124
for (var arg in superInvocation.argumentList.arguments) {
125125
if (arg is SimpleIdentifier) {
@@ -135,14 +135,14 @@ class _Visitor extends SimpleAstVisitor<void> {
135135
var convertibleConstructorParams = <String>[];
136136
var matchedConstructorParamIndex = 0;
137137

138-
var seenSuperParams = <Element>{};
138+
var seenSuperParams = <Element2>{};
139139

140140
// For each super arg, ensure there is a constructor param (in the right
141141
// order).
142142
for (var i = 0; i < positionalSuperArgs.length; ++i) {
143143
var superArg = positionalSuperArgs[i];
144-
var superParam = superArg.staticElement;
145-
if (superParam is! ParameterElement) return null;
144+
var superParam = superArg.element;
145+
if (superParam is! FormalParameterElement) return null;
146146
if (superParam.isNamed) return null;
147147

148148
// Check for the case where a super param is used more than once.
@@ -153,8 +153,8 @@ class _Visitor extends SimpleAstVisitor<void> {
153153
var constructorParam = constructorParams[i];
154154
if (constructorParam is FieldFormalParameter) return null;
155155
if (constructorParam is SuperFormalParameter) return null;
156-
var constructorElement = constructorParam.declaredElement;
157-
if (constructorElement == null) continue;
156+
var constructorElement = constructorParam.declaredFragment?.element;
157+
if (constructorElement == null) return null;
158158
if (referencedParameters.contains(constructorElement)) return null;
159159
if (constructorElement == superParam) {
160160
// Compare the types.
@@ -166,9 +166,8 @@ class _Visitor extends SimpleAstVisitor<void> {
166166

167167
match = true;
168168
var identifier = constructorParam.name?.lexeme;
169-
if (identifier != null) {
170-
convertibleConstructorParams.add(identifier);
171-
}
169+
if (identifier == null) return null;
170+
convertibleConstructorParams.add(identifier);
172171
// Ensure we're not out of order.
173172
if (i < matchedConstructorParamIndex) return null;
174173
matchedConstructorParamIndex = i;
@@ -183,8 +182,8 @@ class _Visitor extends SimpleAstVisitor<void> {
183182
/// initializing formal parameter.
184183
bool _checkNamedParameter(
185184
FormalParameter parameter,
186-
ParameterElement parameterElement,
187-
ConstructorElement superConstructor,
185+
FormalParameterElement parameterElement,
186+
ConstructorElement2 superConstructor,
188187
SuperConstructorInvocation superInvocation) {
189188
var superParameter =
190189
_correspondingNamedParameter(superConstructor, parameterElement);
@@ -194,10 +193,10 @@ class _Visitor extends SimpleAstVisitor<void> {
194193
var arguments = superInvocation.argumentList.arguments;
195194
for (var argument in arguments) {
196195
if (argument is NamedExpression &&
197-
argument.name.label.name == parameterElement.name) {
196+
argument.name.label.name == parameterElement.name3) {
198197
var expression = argument.expression;
199198
if (expression is SimpleIdentifier &&
200-
expression.staticElement == parameterElement) {
199+
expression.element == parameterElement) {
201200
matchingArgument = true;
202201
break;
203202
}
@@ -221,10 +220,12 @@ class _Visitor extends SimpleAstVisitor<void> {
221220
return true;
222221
}
223222

224-
ParameterElement? _correspondingNamedParameter(
225-
ConstructorElement superConstructor, ParameterElement thisParameter) {
226-
for (var superParameter in superConstructor.parameters) {
227-
if (superParameter.isNamed && superParameter.name == thisParameter.name) {
223+
FormalParameterElement? _correspondingNamedParameter(
224+
ConstructorElement2 superConstructor,
225+
FormalParameterElement thisParameter) {
226+
for (var superParameter in superConstructor.formalParameters) {
227+
if (superParameter.isNamed &&
228+
superParameter.name3 == thisParameter.name3) {
228229
return superParameter;
229230
}
230231
}

0 commit comments

Comments
 (0)