Add spaces after flow control statements (#126320)

This commit is contained in:
Tomasz Gucio 2023-05-15 11:07:30 +02:00 committed by GitHub
parent 231e00d26b
commit 99c7e9f088
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
116 changed files with 264 additions and 174 deletions

View File

@ -112,6 +112,9 @@ Future<void> run(List<String> arguments) async {
printProgress('Trailing spaces...'); printProgress('Trailing spaces...');
await verifyNoTrailingSpaces(flutterRoot); // assumes no unexpected binaries, so should be after verifyNoBinaries await verifyNoTrailingSpaces(flutterRoot); // assumes no unexpected binaries, so should be after verifyNoBinaries
printProgress('Spaces after flow control statements...');
await verifySpacesAfterFlowControlStatements(flutterRoot);
printProgress('Deprecations...'); printProgress('Deprecations...');
await verifyDeprecations(flutterRoot); await verifyDeprecations(flutterRoot);
@ -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'; String _bullets(String value) => ' * $value';
Future<void> verifyIssueLinks(String workingDirectory) async { Future<void> verifyIssueLinks(String workingDirectory) async {

View File

@ -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) {}
// ^
}

View File

@ -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 { test('analyze.dart - verifyNoBinaries - positive', () async {
final String result = await capture(() => verifyNoBinaries( final String result = await capture(() => verifyNoBinaries(
testRootPath, testRootPath,