Skip to content

Commit

Permalink
Add spaces after flow control statements (flutter#126320)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgucio authored and Casey Hillers committed May 24, 2023
1 parent 3bf6ec9 commit bb6454e
Show file tree
Hide file tree
Showing 116 changed files with 264 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ResampleFlagVariant extends TestVariant<TestScenario> {

late TestScenario currentValue;
bool get resample {
switch(currentValue) {
switch (currentValue) {
case TestScenario.resampleOn90Hz:
case TestScenario.resampleOn59Hz:
return true;
Expand All @@ -78,7 +78,7 @@ class ResampleFlagVariant extends TestVariant<TestScenario> {
}
}
double get frequency {
switch(currentValue) {
switch (currentValue) {
case TestScenario.resampleOn90Hz:
case TestScenario.resampleOff90Hz:
return 90.0;
Expand All @@ -92,7 +92,7 @@ class ResampleFlagVariant extends TestVariant<TestScenario> {

@override
String describeValue(TestScenario value) {
switch(value) {
switch (value) {
case TestScenario.resampleOn90Hz:
return 'resample on with 90Hz input';
case TestScenario.resampleOn59Hz:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Future<void> main() => driver.integrationDriver(
final Map<String, dynamic> fullyLiveResult =
data?['fullyLive'] as Map<String,dynamic>;

if(benchmarkLiveResult['frame_count'] as int < 10
if (benchmarkLiveResult['frame_count'] as int < 10
|| fullyLiveResult['frame_count'] as int < 10) {
print('Failure Details:\nNot Enough frames collected: '
'benchmarkLive ${benchmarkLiveResult['frameCount']}, '
Expand Down
35 changes: 35 additions & 0 deletions dev/bots/analyze.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ Future<void> run(List<String> arguments) async {
printProgress('Trailing spaces...');
await verifyNoTrailingSpaces(flutterRoot); // assumes no unexpected binaries, so should be after verifyNoBinaries

printProgress('Spaces after flow control statements...');
await verifySpacesAfterFlowControlStatements(flutterRoot);

printProgress('Deprecations...');
await verifyDeprecations(flutterRoot);

Expand Down Expand Up @@ -1040,6 +1043,38 @@ Future<void> verifyNoTrailingSpaces(String workingDirectory, { int minimumMatche
}
}

final RegExp _flowControlStatementWithoutSpace = RegExp(r'(^|[ \t])(if|switch|for|do|while|catch)\(', multiLine: true);

Future<void> verifySpacesAfterFlowControlStatements(String workingDirectory, { int minimumMatches = 4000 }) async {
const Set<String> extensions = <String>{
'.dart',
'.java',
'.js',
'.kt',
'.swift',
'.c',
'.cc',
'.cpp',
'.h',
'.m',
};
final List<File> files = await _allFiles(workingDirectory, null, minimumMatches: minimumMatches)
.where((File file) => extensions.contains(path.extension(file.path)))
.toList();
final List<String> problems = <String>[];
for (final File file in files) {
final List<String> lines = file.readAsLinesSync();
for (int index = 0; index < lines.length; index += 1) {
if (lines[index].contains(_flowControlStatementWithoutSpace)) {
problems.add('${file.path}:${index + 1}: no space after flow control statement');
}
}
}
if (problems.isNotEmpty) {
foundError(problems);
}
}

String _bullets(String value) => ' * $value';

Future<void> verifyIssueLinks(String workingDirectory) async {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: type=lint

bool isThereMeaningOfLife = true;

void main() {
if (isThereMeaningOfLife) {}
if(isThereMeaningOfLife) {}
//^

switch (isThereMeaningOfLife) {
case false:
case true:
}
switch(isThereMeaningOfLife) {
// ^
case false:
case true:
}

for (int index = 0; index < 10; index++) {}
for(int index = 0; index < 10; index++) {}
// ^

while (isThereMeaningOfLife) {}
while(isThereMeaningOfLife) {}
// ^

try {
} catch (e) {}
try {
} catch(e) {}
// ^
}
18 changes: 18 additions & 0 deletions dev/bots/test/analyze_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ void main() {
);
});

test('analyze.dart - verifySpacesAfterFlowControlStatements', () async {
final String result = await capture(() => verifySpacesAfterFlowControlStatements(testRootPath, minimumMatches: 2), shouldHaveErrors: true);
final String lines = <String>[
'║ test/analyze-test-input/root/packages/foo/spaces_after_flow.dart:11: no space after flow control statement',
'║ test/analyze-test-input/root/packages/foo/spaces_after_flow.dart:18: no space after flow control statement',
'║ test/analyze-test-input/root/packages/foo/spaces_after_flow.dart:25: no space after flow control statement',
'║ test/analyze-test-input/root/packages/foo/spaces_after_flow.dart:29: no space after flow control statement',
'║ test/analyze-test-input/root/packages/foo/spaces_after_flow.dart:35: no space after flow control statement',
]
.map((String line) => line.replaceAll('/', Platform.isWindows ? r'\' : '/'))
.join('\n');
expect(result,
'╔═╡ERROR╞═══════════════════════════════════════════════════════════════════════\n'
'$lines\n'
'╚═══════════════════════════════════════════════════════════════════════════════\n'
);
});

test('analyze.dart - verifyNoBinaries - positive', () async {
final String result = await capture(() => verifyNoBinaries(
testRootPath,
Expand Down
2 changes: 1 addition & 1 deletion dev/devicelab/bin/summarize.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Future<void> main(List<String> rawArgs) async {
test = ABTest.fromJsonMap(
const JsonDecoder().convert(await file.readAsString()) as Map<String, dynamic>
);
} catch(error) {
} catch (error) {
_usage('Could not parse json file "$filename"');
return;
}
Expand Down
2 changes: 1 addition & 1 deletion dev/devicelab/lib/framework/devices.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ String getArtifactPath() {
String? _findMatchId(List<String> idList, String idPattern) {
String? candidate;
idPattern = idPattern.toLowerCase();
for(final String id in idList) {
for (final String id in idList) {
if (id.toLowerCase() == idPattern) {
return id;
}
Expand Down
2 changes: 1 addition & 1 deletion dev/devicelab/lib/tasks/plugin_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public class $pluginClass: NSObject, FlutterPlugin {
// build files.
await build(buildTarget, validateNativeBuildProject: false);

switch(buildTarget) {
switch (buildTarget) {
case 'apk':
if (await exec(
path.join('.', 'gradlew'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void configureFlutterEngine(FlutterEngine flutterEngine) {

@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
switch(methodCall.method) {
switch (methodCall.method) {
case "pipeFlutterViewEvents":
result.success(null);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void dispose() {

@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
switch(methodCall.method) {
switch (methodCall.method) {
case "pipeTouchEvents":
touchPipe.enable();
result.success(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void enable() {
}

public void disable() {
if(!mEnabled)
if (!mEnabled)
return;
mEnabled = false;
mView.setOnTouchListener(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class MotionEventsBodyState extends State<MotionEventsBody> {
diff.write(currentDiff);
}
return diff.toString();
} catch(e) {
} catch (e) {
return e.toString();
}
}
Expand Down
4 changes: 2 additions & 2 deletions dev/integration_tests/android_views/lib/wm_integrations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class WindowManagerBodyState extends State<WindowManagerBody> {
setState(() {
_lastTestStatus = _LastTestStatus.success;
});
} catch(e) {
} catch (e) {
setState(() {
_lastTestStatus = _LastTestStatus.error;
lastError = '$e';
Expand All @@ -125,7 +125,7 @@ class WindowManagerBodyState extends State<WindowManagerBody> {
setState(() {
windowClickCount++;
});
} catch(e) {
} catch (e) {
setState(() {
_lastTestStatus = _LastTestStatus.error;
lastError = '$e';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class _LeaveBehindListItem extends StatelessWidget {
}
},
confirmDismiss: !confirmDismiss ? null : (DismissDirection dismissDirection) async {
switch(dismissDirection) {
switch (dismissDirection) {
case DismissDirection.endToStart:
return await _showConfirmationDialog(context, 'archive') ?? false;
case DismissDirection.startToEnd:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ScrollableTabsDemoState extends State<ScrollableTabsDemo> with SingleTicke
return const UnderlineTabIndicator();
}

switch(_demoStyle) {
switch (_demoStyle) {
case TabsDemoStyle.iconsAndText:
return ShapeDecoration(
shape: const RoundedRectangleBorder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Set<String> _unTestedDemos = Set<String>.from(_allDemos);
class _MessageHandler {
static LiveWidgetController? controller;
Future<String> call(String message) async {
switch(message) {
switch (message) {
case 'demoNames':
return const JsonEncoder.withIndent(' ').convert(_allDemos);
case 'profileDemos':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void configureFlutterEngine(FlutterEngine flutterEngine) {

@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
switch(methodCall.method) {
switch (methodCall.method) {
case "getStoragePermission":
if (permissionResult != null) {
result.error("error", "already waiting for permissions", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void dispose() {}

@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
switch(methodCall.method) {
switch (methodCall.method) {
case "pipeTouchEvents":
touchPipe.enable();
result.success(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void enable() {
}

public void disable() {
if(!mEnabled)
if (!mEnabled)
return;
mEnabled = false;
mView.setOnTouchListener(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class MotionEventsBodyState extends State<MotionEventsBody> {
diff.write(currentDiff);
}
return diff.toString();
} catch(e) {
} catch (e) {
return e.toString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class NestedViewEventBodyState extends State<NestedViewEventBody> {
setState(() {
_lastTestStatus = _LastTestStatus.success;
});
} catch(e) {
} catch (e) {
setState(() {
_lastTestStatus = _LastTestStatus.error;
lastError = '$e';
Expand All @@ -165,7 +165,7 @@ class NestedViewEventBodyState extends State<NestedViewEventBody> {
setState(() {
nestedViewClickCount++;
});
} catch(e) {
} catch (e) {
setState(() {
_lastTestStatus = _LastTestStatus.error;
lastError = '$e';
Expand Down
8 changes: 4 additions & 4 deletions dev/tools/gen_defaults/lib/input_decorator_template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class _${blockName}DefaultsM3 extends InputDecorationTheme {
if (states.contains(MaterialState.disabled)) {
return ${componentColor('md.comp.filled-text-field.disabled.leading-icon')};
}
if(states.contains(MaterialState.error)) {
if (states.contains(MaterialState.error)) {
if (states.contains(MaterialState.hovered)) {
return ${componentColor('md.comp.filled-text-field.error.hover.leading-icon')};
}
Expand All @@ -114,7 +114,7 @@ class _${blockName}DefaultsM3 extends InputDecorationTheme {
if (states.contains(MaterialState.disabled)) {
return ${componentColor('md.comp.filled-text-field.disabled.trailing-icon')};
}
if(states.contains(MaterialState.error)) {${componentColor('md.comp.filled-text-field.error.trailing-icon') == componentColor('md.comp.filled-text-field.error.focus.trailing-icon') ? '' : '''
if (states.contains(MaterialState.error)) {${componentColor('md.comp.filled-text-field.error.trailing-icon') == componentColor('md.comp.filled-text-field.error.focus.trailing-icon') ? '' : '''
if (states.contains(MaterialState.hovered)) {
return ${componentColor('md.comp.filled-text-field.error.hover.trailing-icon')};
}
Expand All @@ -138,7 +138,7 @@ class _${blockName}DefaultsM3 extends InputDecorationTheme {
if (states.contains(MaterialState.disabled)) {
return textStyle.copyWith(color: ${componentColor('md.comp.filled-text-field.disabled.label-text')});
}
if(states.contains(MaterialState.error)) {
if (states.contains(MaterialState.error)) {
if (states.contains(MaterialState.hovered)) {
return textStyle.copyWith(color: ${componentColor('md.comp.filled-text-field.error.hover.label-text')});
}
Expand All @@ -162,7 +162,7 @@ class _${blockName}DefaultsM3 extends InputDecorationTheme {
if (states.contains(MaterialState.disabled)) {
return textStyle.copyWith(color: ${componentColor('md.comp.filled-text-field.disabled.label-text')});
}
if(states.contains(MaterialState.error)) {
if (states.contains(MaterialState.error)) {
if (states.contains(MaterialState.hovered)) {
return textStyle.copyWith(color: ${componentColor('md.comp.filled-text-field.error.hover.label-text')});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/cupertino/date_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2078,7 +2078,7 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
double maxWidth = double.negativeInfinity;
for (int i = 0; i < labels.length; i++) {
final String? label = labels[i];
if(label == null) {
if (label == null) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/cupertino/radio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class _CupertinoRadioState<T> extends State<CupertinoRadio<T>> with TickerProvid
final bool? accessibilitySelected;
// Apple devices also use `selected` to annotate radio button's semantics
// state.
switch(defaultTargetPlatform) {
switch (defaultTargetPlatform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/cupertino/scrollbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class _CupertinoScrollbarState extends RawScrollbarState<CupertinoScrollbar> {
}
_thicknessAnimationController.reverse();
super.handleThumbPressEnd(localPosition, velocity);
switch(direction) {
switch (direction) {
case Axis.vertical:
if (velocity.pixelsPerSecond.dy.abs() < 10 &&
(localPosition.dy - _pressStartAxisPosition).abs() > 0) {
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter/lib/src/cupertino/switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ class _RenderCupertinoSwitch extends RenderConstrainedBox {
bool get isFocused => _isFocused;
bool _isFocused;
set isFocused(bool value) {
if(value == _isFocused) {
if (value == _isFocused) {
return;
}
_isFocused = value;
Expand Down Expand Up @@ -637,7 +637,7 @@ class _RenderCupertinoSwitch extends RenderConstrainedBox {
final RRect trackRRect = RRect.fromRectAndRadius(trackRect, const Radius.circular(_kTrackRadius));
canvas.drawRRect(trackRRect, paint);

if(_isFocused) {
if (_isFocused) {
// Paints a border around the switch in the focus color.
final RRect borderTrackRRect = trackRRect.inflate(1.75);

Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/foundation/change_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ mixin class ChangeNotifier implements Listenable {
if (_listeners[i] == null) {
// We swap this item with the next not null item.
int swapIndex = i + 1;
while(_listeners[swapIndex] == null) {
while (_listeners[swapIndex] == null) {
swapIndex += 1;
}
_listeners[i] = _listeners[swapIndex];
Expand Down

0 comments on commit bb6454e

Please sign in to comment.