Skip to content

Commit 769f9c0

Browse files
committed
Test refactoring to use the patterns syntax suggested in:
dart-lang/language#2137 (comment) In particular: - Instead of if-var statements, use `is <pattern>` as the condition. - Require "var" before variables in cases.
1 parent 76d54b6 commit 769f9c0

13 files changed

+68
-68
lines changed

lib/src/argument_list_visitor.dart

+14-14
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ArgumentListVisitor {
6565
Token leftParenthesis,
6666
Token rightParenthesis,
6767
List<Expression> arguments) {
68-
if (var (start, end)? = _contiguousFunctions(arguments)) {
68+
if (_contiguousFunctions(arguments) is (var start, var end)?) {
6969
// Split the arguments into two independent argument lists with the
7070
// functions in the middle.
7171
var argumentsBefore = arguments.take(start).toList();
@@ -228,30 +228,30 @@ class ArgumentListVisitor {
228228
static bool _isBlockFunction(Expression expression) {
229229
// This is pretty nice:
230230
switch (expression) {
231-
case NamedExpression(expression):
231+
case NamedExpression(var expression):
232232
return _isBlockFunction(expression);
233233

234234
// Allow functions wrapped in dotted method calls like "a.b.c(() { ... })".
235-
case MethodInvocation(target, argumentList):
235+
case MethodInvocation(var target, var argumentList):
236236
if (!_isValidWrappingTarget(target)) return false;
237237
if (argumentList.arguments.length != 1) return false;
238238
return _isBlockFunction(argumentList.arguments.single);
239239

240-
case InstanceCreationExpression(argumentList):
240+
case InstanceCreationExpression(var argumentList):
241241
if (argumentList.arguments.length != 1) return false;
242242
return _isBlockFunction(argumentList.arguments.single);
243243

244244
// Allow immediately-invoked functions like "() { ... }()".
245-
case FunctionExpressionInvocation(function, argumentList):
245+
case FunctionExpressionInvocation(var function, var argumentList):
246246
if (argumentList.arguments.isNotEmpty) return false;
247247
return _isBlockFunction(function);
248248

249249
// Unwrap parenthesized expressions.
250-
case ParenthesizedExpression(expression):
250+
case ParenthesizedExpression(var expression):
251251
return _isBlockFunction(expression);
252252

253253
// Must be a function with a non-empty curly body.
254-
case FunctionExpression(body: BlockFunctionBody(block))
254+
case FunctionExpression(body: BlockFunctionBody(var block))
255255
when block.statements.isNotEmpty ||
256256
block.rightBracket.precedingComments != null:
257257
return true;
@@ -269,7 +269,7 @@ class ArgumentListVisitor {
269269
case null => true;
270270

271271
// Allow property accesses.
272-
case PropertyAccess(target) => _isValidWrappingTarget(target);
272+
case PropertyAccess(var target) => _isValidWrappingTarget(target);
273273

274274
case PrefixedIdentifier() | SimpleIdentifier() => true;
275275

@@ -328,7 +328,7 @@ class ArgumentSublist {
328328
var blocks = <Expression, Token>{};
329329
for (var argument in arguments) {
330330
// Not sure if this is really better:
331-
if (var bracket? = _blockToken(argument)) blocks[argument] = bracket;
331+
if (_blockToken(argument) is var bracket?) blocks[argument] = bracket;
332332
}
333333

334334
// Count the leading arguments that are blocks.
@@ -437,7 +437,7 @@ class ArgumentSublist {
437437
var argumentBlock = _blocks[argument];
438438
// Not really an improvement.
439439
switch (argumentBlock) {
440-
case block?:
440+
case var block?:
441441
rule.disableSplitOnInnerRules();
442442

443443
// Tell it to use the rule we've already created.
@@ -518,12 +518,12 @@ class ArgumentSublist {
518518
static Token? _blockToken(Expression expression) {
519519
// Beautiful!
520520
return switch (expression) {
521-
case NamedExpression(expression) => _blockToken(expression);
521+
case NamedExpression(var expression) => _blockToken(expression);
522522

523523
// TODO(rnystrom): Should we step into parenthesized expressions?
524-
case ListLiteral(leftBracket: token)
525-
| SetOrMapLiteral(leftBracket: token)
526-
| SingleStringLiteral(isMultiline: true, beginToken: token) => token;
524+
case ListLiteral(leftBracket: var token)
525+
| SetOrMapLiteral(leftBracket: var token)
526+
| SingleStringLiteral(isMultiline: true, beginToken: var token) => token;
527527

528528
// Not a collection literal.
529529
default => null;

lib/src/ast_extensions.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ extension ExpressionExtensions on Expression {
9595

9696
// If the target is a call with a trailing comma in the argument list,
9797
// treat it like a collection literal.
98-
case InvocationExpression(argumentList)
99-
| InstanceCreationExpression(argumentList) =>
98+
case InvocationExpression(var argumentList)
99+
| InstanceCreationExpression(var argumentList) =>
100100

101101
// TODO(rnystrom): Do we want to allow an invocation where the last
102102
// argument is a collection literal? Like:
@@ -114,7 +114,7 @@ extension ExpressionExtensions on Expression {
114114
bool get isTrailingCommaArgument {
115115
return switch (this) {
116116
case NamedExpression named => named.isTrailingCommaArgument;
117-
case ArgumentList(arguments) => arguments.hasCommaAfter;
117+
case ArgumentList(var arguments) => arguments.hasCommaAfter;
118118
default => false;
119119
}
120120
}
@@ -147,7 +147,7 @@ extension ExpressionExtensions on Expression {
147147
// A prefixed unnamed constructor call:
148148
//
149149
// prefix.Foo();
150-
case MethodInvocation(SimpleIdentifier target, methodName)
150+
case MethodInvocation(SimpleIdentifier target, var methodName)
151151
when _looksLikeClassName(methodName.name) => true;
152152

153153
// A prefixed or unprefixed named constructor call:
@@ -157,7 +157,7 @@ extension ExpressionExtensions on Expression {
157157
case MethodInvocation(PrefixedIdentifier target) =>
158158
target.looksLikeStaticCall;
159159

160-
case SimpleIdentifier(name) when _looksLikeClassName(name) => true;
160+
case SimpleIdentifier(var name) when _looksLikeClassName(name) => true;
161161

162162
default => false;
163163
}

lib/src/call_chain_visitor.dart

+25-26
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ class CallChainVisitor {
236236

237237
// If there are block calls, end the chain and write those without any
238238
// extra indentation.
239-
if (var blockCalls? _blockCalls) {
239+
if (_blockCalls is var blockCalls?) {
240240
_enableRule();
241241
_visitor.zeroSplit();
242242
_disableRule();
@@ -278,7 +278,7 @@ class CallChainVisitor {
278278
// * The body of a `=>` function.
279279
switch (expression) {
280280
// Unwrap parentheses.
281-
case ParenthesizedExpression(expression):
281+
case ParenthesizedExpression(var expression):
282282
return _forcesSplit(expression);
283283

284284
// Don't split right after a collection literal.
@@ -294,9 +294,9 @@ class CallChainVisitor {
294294

295295
// If the expression ends in an argument list, base the splitting on the
296296
// last argument.
297-
case MethodInvocation(argumentList):
298-
case InstanceCreationExpression(argumentList):
299-
case FunctionExpressionInvocation(argumentList):
297+
case MethodInvocation(var argumentList):
298+
case InstanceCreationExpression(var argumentList):
299+
case FunctionExpressionInvocation(var argumentList):
300300
if (argumentList.arguments.isEmpty) return true;
301301

302302
var argument = argumentList.arguments.last;
@@ -426,8 +426,8 @@ switch class _Selector {
426426
/// Whether this selector is a method call whose arguments are block
427427
/// formatted.
428428
bool isBlockCall(SourceVisitor visitor) => switch (this) {
429-
case _MethodSelector(_node) =>
430-
ArgumentListVisitor(visitor, _node.argumentList).hasBlockArguments;
429+
case _MethodSelector(var node) =>
430+
ArgumentListVisitor(visitor, node.argumentList).hasBlockArguments;
431431

432432
default => false;
433433
}
@@ -449,7 +449,7 @@ switch class _Selector {
449449
visitor._visitor.visitArgumentList(invocation.argumentList);
450450
case IndexExpression index:
451451
visitor._visitor.finishIndexExpression(index);
452-
case PostfixExpression(operator):
452+
case PostfixExpression(var operator):
453453
assert(operator.type == TokenType.BANG);
454454
visitor._visitor.token(operator);
455455
default:
@@ -463,25 +463,25 @@ switch class _Selector {
463463
/// Subclasses implement this to write their selector.
464464
void writeSelector(CallChainVisitor visitor) {
465465
switch (this) {
466-
case _MethodSelector(_node):
467-
visitor._visitor.token(_node.operator);
468-
visitor._visitor.token(_node.methodName.token);
466+
case _MethodSelector(var node):
467+
visitor._visitor.token(node.operator);
468+
visitor._visitor.token(node.methodName.token);
469469

470470
visitor._beforeMethodArguments(this);
471471

472472
visitor._visitor.builder.nestExpression();
473-
visitor._visitor.visit(_node.typeArguments);
473+
visitor._visitor.visit(node.typeArguments);
474474
visitor._visitor
475-
.visitArgumentList(_node.argumentList, nestExpression: false);
475+
.visitArgumentList(node.argumentList, nestExpression: false);
476476
visitor._visitor.builder.unnest();
477477

478-
case _PrefixedSelector(_node):
479-
visitor._visitor.token(_node.period);
480-
visitor._visitor.visit(_node.identifier);
478+
case _PrefixedSelector(var node):
479+
visitor._visitor.token(node.period);
480+
visitor._visitor.visit(node.identifier);
481481

482-
case _PropertySelector(_node):
483-
visitor._visitor.token(_node.operator);
484-
visitor._visitor.visit(_node.propertyName);
482+
case _PropertySelector(var node):
483+
visitor._visitor.token(node.operator);
484+
visitor._visitor.visit(node.propertyName);
485485
}
486486
}
487487
}
@@ -506,8 +506,7 @@ class _PropertySelector extends _Selector {
506506

507507
/// If [expression] is a null-assertion operator, returns its operand.
508508
Expression _unwrapNullAssertion(Expression expression) {
509-
if (expression is PostfixExpression &&
510-
expression.operator.type == TokenType.BANG) {
509+
if (expression is PostfixExpression(operator: Token(type: TokenType.BANG))) {
511510
return expression.operand;
512511
}
513512

@@ -542,19 +541,19 @@ Expression _unwrapTarget(Expression node, List<_Selector> calls) {
542541
case _ when node.looksLikeStaticCall => node;
543542

544543
// Selectors.
545-
case MethodInvocation(target?) =>
544+
case MethodInvocation(var target?) =>
546545
_unwrapSelector(target, _MethodSelector(node), calls);
547546

548-
case PropertyAccess(target?) =>
547+
case PropertyAccess(var target?) =>
549548
_unwrapSelector(target, _PropertySelector(node), calls);
550549

551-
case PrefixedIdentifier(prefix) =>
550+
case PrefixedIdentifier(var prefix) =>
552551
_unwrapSelector(prefix, _PrefixedSelector(node), calls);
553552

554553
// Postfix expressions.
555-
case IndexExpression(target?) => _unwrapPostfix(node, target, calls);
554+
case IndexExpression(var target?) => _unwrapPostfix(node, target, calls);
556555

557-
case FunctionExpressionInvocation(function) =>
556+
case FunctionExpressionInvocation(var function) =>
558557
_unwrapPostfix(node, function, calls);
559558

560559
case PostfixExpression(operator: Token(type: TokenType.BANG)) =>

lib/src/chunk.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class Chunk extends Selection {
201201
if (!isBlock) return false;
202202

203203
// Something like guard-let would be nice here.
204-
if (var argument? = block.argument) {
204+
if (block.argument is var argument?) {
205205
var rule = argument.rule;
206206

207207
// There may be no rule if the block occurs inside a string interpolation.

lib/src/chunk_builder.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ class ChunkBuilder {
383383

384384
// Remove any leading "*" from the middle lines.
385385
if (i > 0 && i < lines.length - 1) {
386-
if (_javaDocLine.firstMatch(line) case var match?) {
386+
if (_javaDocLine.firstMatch(line) is var match?) {
387387
line = match[1]!;
388388
}
389389
}

lib/src/cli/format_command.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class FormatCommand extends Command<int> {
6666
// If the user wants to print the code and didn't indicate how the files
6767
// should be printed, default to only showing the code.
6868
if (!argResults.wasParsed('show') &&
69-
(output == Output.show || output == Output.json)) {
69+
(output is Output.show | Output.json)) {
7070
show = Show.none;
7171
}
7272

lib/src/debug.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void dumpConstraints(List<Chunk> chunks) {
189189
for (var other in rules) {
190190
if (rule == other) continue;
191191

192-
if (var constraint? = rule.constrain(value, other)) {
192+
if (rule.constrain(value, other) is var constraint?) {
193193
constraints.add('$other->$constraint');
194194
}
195195
}

lib/src/line_splitting/rule_set.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class RuleSet {
8989
} else {
9090
onSplitRule(other);
9191
}
92-
case (null, constraint?):
92+
case (null, var constraint?):
9393
// Bind the other rule to its value and recursively propagate its
9494
// constraints.
9595
if (!tryBind(rules, other, constraint, onSplitRule)) return false;
@@ -98,13 +98,13 @@ class RuleSet {
9898
// that value.
9999
case (!= null, Rule.mustSplit): // Instead of `!= null`, could do `_?`.
100100
if (otherValue == Rule.unsplit) return false;
101-
case (!= null, constraint?):
101+
case (!= null, var constraint?):
102102
if (otherValue != constraint) return false;
103103
case (!= null, _):
104104
// See if the other rule's constraint allows us to use this value.
105105
switch (other.constrain(otherValue, rule)) {
106106
case Rule.mustSplit when value == Rule.unsplit:
107-
case constraint? when value != constraint:
107+
case var constraint? when value != constraint:
108108
return false;
109109
default:
110110
}

lib/src/line_writer.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ class LineWriter {
192192
/// Writes [chunk] to the output and updates the selection if the chunk
193193
/// contains a selection marker.
194194
void _writeChunk(Chunk chunk) {
195-
if (var start? = chunk.selectionStart) _selectionStart = length + start;
196-
if (var end? = chunk.selectionEnd) _selectionEnd = length + end;
195+
if (chunk.selectionStart is var start?) _selectionStart = length + start;
196+
if (chunk.selectionEnd is var end?) _selectionEnd = length + end;
197197

198198
_buffer.write(chunk.text);
199199
}

lib/src/nesting_builder.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ class NestingBuilder {
9999
indent ??= Indent.expression;
100100

101101
_pendingNesting = switch (_pendingNesting) {
102-
case nesting? => nesting.nest(indent);
102+
case var nesting? => nesting.nest(indent);
103103
default => _nesting.nest(indent);
104104
}
105105
}
106106

107107
/// Discards the most recent level of expression nesting.
108108
void unnest() {
109109
_pendingNesting = switch (_pendingNesting) {
110-
case nesting? => nesting.parent;
110+
case var nesting? => nesting.parent;
111111
default => _nesting.parent;
112112
}
113113

lib/src/nesting_level.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class NestingLevel extends FastHash {
6565

6666
totalIndent = 0;
6767

68-
if (var parent? = parent) {
68+
if (parent is var parent?) {
6969
parent.refreshTotalUsedIndent(usedNesting);
7070
totalIndent += parent.totalUsedIndent;
7171
}

lib/src/source_code.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ class SourceCode {
6363
throw ArgumentError(
6464
'If selectionStart is provided, selectionLength must be too.');
6565

66-
case (start?, _) when start < 0:
66+
case (var start?, _) when start < 0:
6767
throw ArgumentError('selectionStart must be non-negative.');
6868

69-
case (start?, _) when start > text.length:
69+
case (var start?, _) when start > text.length:
7070
throw ArgumentError('selectionStart must be within text.');
7171

72-
case (_, length?) when length < 0:
72+
case (_, var length?) when length < 0:
7373
throw ArgumentError('selectionLength must be non-negative.');
7474

75-
case (start?, length?) when start + length > text.length:
75+
case (var start?, var length?) when start + length > text.length:
7676
throw ArgumentError('selectionLength must end within text.');
7777
}
7878
}

0 commit comments

Comments
 (0)