Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(813)

Unified Diff: lib/src/call_chain_visitor.dart

Issue 1355203002: Handle index expressions in the middle of call chains. (Closed) Base URL: https://github.com/dart-lang/dart_style.git@master
Patch Set: Handle chained indexes. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « example/format.dart ('k') | lib/src/source_visitor.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/call_chain_visitor.dart
diff --git a/lib/src/call_chain_visitor.dart b/lib/src/call_chain_visitor.dart
index 36faccaab5a9289b25f471e7a1f5adcd0efa1832..a01e6ec6d2d378f027fc1c719467927ec551c4f5 100644
--- a/lib/src/call_chain_visitor.dart
+++ b/lib/src/call_chain_visitor.dart
@@ -51,14 +51,25 @@ class CallChainVisitor {
flatten(expression) {
target = expression;
- if (expression is MethodInvocation && expression.target != null) {
- flatten(expression.target);
+ // Treat index expressions where the target is a valid call in a method
+ // chain as being part of the call. Handles cases like:
+ //
+ // receiver
+ // .property
+ // .property[0]
+ // .property
+ // .property;
+ var call = expression;
+ while (call is IndexExpression) call = call.target;
+
+ if (call is MethodInvocation && call.target != null) {
+ flatten(call.target);
calls.add(expression);
- } else if (expression is PropertyAccess && expression.target != null) {
- flatten(expression.target);
+ } else if (call is PropertyAccess && call.target != null) {
+ flatten(call.target);
calls.add(expression);
- } else if (expression is PrefixedIdentifier) {
- flatten(expression.prefix);
+ } else if (call is PrefixedIdentifier) {
+ flatten(call.prefix);
calls.add(expression);
}
}
@@ -135,12 +146,19 @@ class CallChainVisitor {
/// Writes [call], which must be one of the supported expression types.
void _writeCall(Expression call) {
- if (call is MethodInvocation) {
+ if (call is IndexExpression) {
+ _visitor.builder.nestExpression();
+ _writeCall(call.target);
+ _visitor.finishIndexExpression(call);
+ _visitor.builder.unnest();
+ } else if (call is MethodInvocation) {
_writeInvocation(call);
} else if (call is PropertyAccess) {
- _writePropertyAccess(call);
+ _visitor.token(call.operator);
+ _visitor.visit(call.propertyName);
} else if (call is PrefixedIdentifier) {
- _writePrefixedIdentifier(call);
+ _visitor.token(call.period);
+ _visitor.visit(call.identifier);
} else {
// Unexpected type.
assert(false);
@@ -211,16 +229,6 @@ class CallChainVisitor {
if (args.nestMethodArguments) _visitor.builder.endBlockArgumentNesting();
}
- void _writePropertyAccess(PropertyAccess property) {
- _visitor.token(property.operator);
- _visitor.visit(property.propertyName);
- }
-
- void _writePrefixedIdentifier(PrefixedIdentifier prefix) {
- _visitor.token(prefix.period);
- _visitor.visit(prefix.identifier);
- }
-
/// If a [Rule] for the method chain is currently active, ends it.
void _disableRule() {
if (_ruleEnabled == false) return;
« no previous file with comments | « example/format.dart ('k') | lib/src/source_visitor.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698