Skip to content

Commit

Permalink
[element model] migrate cascade_invocations
Browse files Browse the repository at this point in the history
Bug: https://github.com/dart-lang/linter/issues/5099
Change-Id: Ic70839e4c56da27fbc7b3e336d790605e1cf97c2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394600
Commit-Queue: Brian Wilkerson <[email protected]>
Auto-Submit: Phil Quitslund <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
pq authored and Commit Queue committed Nov 11, 2024
1 parent 598de0a commit 9a76ff8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
1 change: 0 additions & 1 deletion pkg/linter/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ lib/src/rules/avoid_renaming_method_parameters.dart
lib/src/rules/avoid_setters_without_getters.dart
lib/src/rules/avoid_types_as_parameter_names.dart
lib/src/rules/avoid_void_async.dart
lib/src/rules/cascade_invocations.dart
lib/src/rules/deprecated_member_use_from_same_package.dart
lib/src/rules/invalid_runtime_check_with_js_interop_types.dart
lib/src/rules/prefer_asserts_in_initializer_lists.dart
Expand Down
3 changes: 1 addition & 2 deletions pkg/linter/lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ extension AstNodeNullableExtension on AstNode? {
if (node is Identifier) {
return node.element;
} else if (node is PropertyAccess) {
// TODO(pq): implement.
return null;
return node.propertyName.element;
}
}
return null;
Expand Down
38 changes: 19 additions & 19 deletions pkg/linter/lib/src/rules/cascade_invocations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';

import '../analyzer.dart';
import '../extensions.dart';

const _desc = r'Cascade consecutive method invocations on the same reference.';

Element? _getElementFromVariableDeclarationStatement(
Element2? _getElementFromVariableDeclarationStatement(
VariableDeclarationStatement statement) {
var variables = statement.variables.variables;
if (variables.length == 1) {
Expand All @@ -25,39 +25,39 @@ Element? _getElementFromVariableDeclarationStatement(
// In such a case, we should not return any cascadable element here.
return null;
}
return variable.declaredElement;
return variable.declaredElement2 ?? variable.declaredFragment?.element;
}
return null;
}

ExecutableElement? _getExecutableElementFromMethodInvocation(
ExecutableElement2? _getExecutableElementFromMethodInvocation(
MethodInvocation node) {
if (_isInvokedWithoutNullAwareOperator(node.operator)) {
var executableElement = node.methodName.canonicalElement;
if (executableElement is ExecutableElement) {
var executableElement = node.methodName.canonicalElement2;
if (executableElement is ExecutableElement2) {
return executableElement;
}
}
return null;
}

Element? _getPrefixElementFromExpression(Expression rawExpression) {
Element2? _getPrefixElementFromExpression(Expression rawExpression) {
var expression = rawExpression.unParenthesized;
if (expression is PrefixedIdentifier) {
return expression.prefix.canonicalElement;
return expression.prefix.canonicalElement2;
} else if (expression is PropertyAccess &&
_isInvokedWithoutNullAwareOperator(expression.operator) &&
expression.target is SimpleIdentifier) {
return expression.target.canonicalElement;
return expression.target.canonicalElement2;
}
return null;
}

Element? _getTargetElementFromCascadeExpression(CascadeExpression node) =>
node.target.canonicalElement;
Element2? _getTargetElementFromCascadeExpression(CascadeExpression node) =>
node.target.canonicalElement2;

Element? _getTargetElementFromMethodInvocation(MethodInvocation node) =>
node.target.canonicalElement;
Element2? _getTargetElementFromMethodInvocation(MethodInvocation node) =>
node.target.canonicalElement2;

bool _isInvokedWithoutNullAwareOperator(Token? token) =>
token?.type == TokenType.PERIOD;
Expand Down Expand Up @@ -126,7 +126,7 @@ class _CascadableExpression {
/// in the right part of an assignment in a following expression that we would
/// like to join to this.
final bool isCritical;
final Element? element;
final Element2? element;
final List<AstNode> criticalNodes;

factory _CascadableExpression.fromExpressionStatement(
Expand Down Expand Up @@ -163,14 +163,14 @@ class _CascadableExpression {
var leftExpression = node.leftHandSide.unParenthesized;
if (leftExpression is SimpleIdentifier) {
return _CascadableExpression._internal(
leftExpression.staticElement?.canonicalElement, [node.rightHandSide],
leftExpression.element, [node.rightHandSide],
canReceive: node.operator.type != TokenType.QUESTION_QUESTION_EQ,
isCritical: true);
}
// setters
var variable = _getPrefixElementFromExpression(leftExpression);
var canReceive = node.operator.type != TokenType.QUESTION_QUESTION_EQ &&
variable is VariableElement &&
variable is VariableElement2 &&
!variable.isStatic;
return _CascadableExpression._internal(variable, [node.rightHandSide],
canJoin: true, canReceive: canReceive, canBeCascaded: true);
Expand Down Expand Up @@ -201,12 +201,12 @@ class _CascadableExpression {

factory _CascadableExpression._fromPrefixedIdentifier(
PrefixedIdentifier node) =>
_CascadableExpression._internal(node.prefix.canonicalElement, [],
_CascadableExpression._internal(node.prefix.canonicalElement2, [],
canJoin: true, canReceive: true, canBeCascaded: true);

factory _CascadableExpression._fromPropertyAccess(PropertyAccess node) {
var targetIsSimple = node.target is SimpleIdentifier;
return _CascadableExpression._internal(node.target.canonicalElement, [],
return _CascadableExpression._internal(node.target.canonicalElement2, [],
canJoin: targetIsSimple,
canReceive: targetIsSimple,
canBeCascaded: true);
Expand Down Expand Up @@ -248,7 +248,7 @@ class _NodeVisitor extends UnifyingAstVisitor<void> {
_NodeVisitor(this.expressionBox);

bool isCriticalNode(AstNode node) =>
node.canonicalElement == expressionBox.element;
node.canonicalElement2 == expressionBox.element;

bool isOrHasCriticalNode(AstNode node) {
node.accept(this);
Expand Down

0 comments on commit 9a76ff8

Please sign in to comment.