Skip to content

Commit 4c15895

Browse files
committed
Stop using 'astFactory', use specialized API.
1 parent 6f894c0 commit 4c15895

File tree

3 files changed

+21
-93
lines changed

3 files changed

+21
-93
lines changed

lib/src/source_visitor.dart

Lines changed: 18 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
// ignore_for_file: avoid_dynamic_calls
55

66
import 'package:analyzer/dart/ast/ast.dart';
7-
import 'package:analyzer/dart/ast/standard_ast_factory.dart';
87
import 'package:analyzer/dart/ast/token.dart';
98
import 'package:analyzer/dart/ast/visitor.dart';
109
import 'package:analyzer/source/line_info.dart';
10+
// ignore: implementation_imports
11+
import 'package:analyzer/src/clients/dart_style/rewrite_cascade.dart';
1112

1213
import 'argument_list_visitor.dart';
1314
import 'call_chain_visitor.dart';
@@ -1245,74 +1246,6 @@ class SourceVisitor extends ThrowingAstVisitor {
12451246
token(node.semicolon);
12461247
}
12471248

1248-
/// Synthesize a token with [type] to replace the given [operator].
1249-
///
1250-
/// Offset, comments, and previous/next links are all preserved.
1251-
static Token _synthesizeToken(TokenType type, Token operator) =>
1252-
Token(type, operator.offset, operator.precedingComments)
1253-
..previous = operator.previous
1254-
..next = operator.next;
1255-
1256-
static Expression _realTargetOf(Expression expression) {
1257-
if (expression is PropertyAccess) {
1258-
return expression.realTarget;
1259-
} else if (expression is MethodInvocation) {
1260-
return expression.realTarget!;
1261-
} else if (expression is IndexExpression) {
1262-
return expression.realTarget;
1263-
}
1264-
throw UnimplementedError('Unhandled ${expression.runtimeType}'
1265-
'($expression)');
1266-
}
1267-
1268-
/// Recursively insert [cascadeTarget] (the LHS of the cascade) into the
1269-
/// LHS of the assignment expression that used to be the cascade's RHS.
1270-
static Expression _insertCascadeTargetIntoExpression(
1271-
Expression expression, Expression cascadeTarget) {
1272-
// Base case: We've recursed as deep as possible.
1273-
if (expression == cascadeTarget) return cascadeTarget;
1274-
1275-
// Otherwise, copy `expression` and recurse into its LHS.
1276-
var expressionTarget = _realTargetOf(expression);
1277-
if (expression is PropertyAccess) {
1278-
return astFactory.propertyAccess(
1279-
_insertCascadeTargetIntoExpression(expressionTarget, cascadeTarget),
1280-
// If we've reached the end, replace the `..` operator with `.`
1281-
expressionTarget == cascadeTarget
1282-
? _synthesizeToken(TokenType.PERIOD, expression.operator)
1283-
: expression.operator,
1284-
expression.propertyName);
1285-
} else if (expression is MethodInvocation) {
1286-
return astFactory.methodInvocation(
1287-
_insertCascadeTargetIntoExpression(expressionTarget, cascadeTarget),
1288-
// If we've reached the end, replace the `..` operator with `.`
1289-
expressionTarget == cascadeTarget
1290-
? _synthesizeToken(TokenType.PERIOD, expression.operator!)
1291-
: expression.operator,
1292-
expression.methodName,
1293-
expression.typeArguments,
1294-
expression.argumentList);
1295-
} else if (expression is IndexExpression) {
1296-
var question = expression.question;
1297-
1298-
// A null-aware cascade treats the `?` in `?..` as part of the token, but
1299-
// for a non-cascade index, it is a separate `?` token.
1300-
if (expression.period?.type == TokenType.QUESTION_PERIOD_PERIOD) {
1301-
question = _synthesizeToken(TokenType.QUESTION, expression.period!);
1302-
}
1303-
1304-
return astFactory.indexExpressionForTarget2(
1305-
target: _insertCascadeTargetIntoExpression(
1306-
expressionTarget, cascadeTarget),
1307-
question: question,
1308-
leftBracket: expression.leftBracket,
1309-
index: expression.index,
1310-
rightBracket: expression.rightBracket);
1311-
}
1312-
throw UnimplementedError('Unhandled ${expression.runtimeType}'
1313-
'($expression)');
1314-
}
1315-
13161249
/// Parenthesize the target of the given statement's expression (assumed to
13171250
/// be a CascadeExpression) before removing the cascade.
13181251
void _fixCascadeByParenthesizingTarget(ExpressionStatement statement) {
@@ -1325,28 +1258,24 @@ class SourceVisitor extends ThrowingAstVisitor {
13251258
writePrecedingCommentsAndNewlines(cascade.target.beginToken);
13261259
_suppressPrecedingCommentsAndNewLines.add(cascade.target.beginToken);
13271260

1328-
var newTarget = astFactory.parenthesizedExpression(
1329-
Token(TokenType.OPEN_PAREN, 0)
1330-
..previous = statement.beginToken.previous
1331-
..next = cascade.target.beginToken,
1332-
cascade.target,
1333-
Token(TokenType.CLOSE_PAREN, 0)
1334-
..previous = cascade.target.endToken
1335-
..next = statement.semicolon);
1336-
13371261
// Finally, we can revisit a clone of this ExpressionStatement to actually
13381262
// remove the cascade.
1339-
visit(astFactory.expressionStatement(
1340-
astFactory.cascadeExpression(newTarget, cascade.cascadeSections),
1341-
statement.semicolon));
1263+
visit(
1264+
fixCascadeByParenthesizingTarget(
1265+
expressionStatement: statement,
1266+
cascadeExpression: cascade,
1267+
),
1268+
);
13421269
}
13431270

13441271
void _removeCascade(ExpressionStatement statement) {
13451272
var cascade = statement.expression as CascadeExpression;
13461273
var subexpression = cascade.cascadeSections.single;
13471274
builder.nestExpression();
13481275

1349-
if (subexpression is AssignmentExpression) {
1276+
if (subexpression is AssignmentExpression ||
1277+
subexpression is MethodInvocation ||
1278+
subexpression is PropertyAccess) {
13501279
// CascadeExpression("leftHandSide", "..",
13511280
// AssignmentExpression("target", "=", "rightHandSide"))
13521281
//
@@ -1356,13 +1285,7 @@ class SourceVisitor extends ThrowingAstVisitor {
13561285
// PropertyAccess("leftHandSide", ".", "target"),
13571286
// "=",
13581287
// "rightHandSide")
1359-
visit(astFactory.assignmentExpression(
1360-
_insertCascadeTargetIntoExpression(
1361-
subexpression.leftHandSide, cascade.target),
1362-
subexpression.operator,
1363-
subexpression.rightHandSide));
1364-
} else if (subexpression is MethodInvocation ||
1365-
subexpression is PropertyAccess) {
1288+
//
13661289
// CascadeExpression("leftHandSide", "..",
13671290
// MethodInvocation("target", ".", "methodName", ...))
13681291
//
@@ -1374,7 +1297,12 @@ class SourceVisitor extends ThrowingAstVisitor {
13741297
// "methodName", ...)
13751298
//
13761299
// And similarly for PropertyAccess expressions.
1377-
visit(_insertCascadeTargetIntoExpression(subexpression, cascade.target));
1300+
visit(
1301+
insertCascadeTargetIntoExpression(
1302+
expression: subexpression,
1303+
cascadeTarget: cascade.target,
1304+
),
1305+
);
13781306
} else {
13791307
throw UnsupportedError(
13801308
'--fix-single-cascade-statements: subexpression of cascade '

pubspec.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ packages:
77
name: _fe_analyzer_shared
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "32.0.0"
10+
version: "34.0.0"
1111
analyzer:
1212
dependency: "direct main"
1313
description:
1414
name: analyzer
1515
url: "https://pub.dartlang.org"
1616
source: hosted
17-
version: "3.0.0"
17+
version: "3.2.0"
1818
args:
1919
dependency: "direct main"
2020
description:

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ environment:
99
sdk: ">=2.12.0-0 <3.0.0"
1010

1111
dependencies:
12-
analyzer: ">=2.6.0 <4.0.0"
12+
analyzer: ">=3.2.0 <4.0.0"
1313
args: ">=1.0.0 <3.0.0"
1414
path: ^1.0.0
1515
pub_semver: ">=1.4.4 <3.0.0"

0 commit comments

Comments
 (0)