Add spaces after flow control statements (#126320)
This commit is contained in:
parent
231e00d26b
commit
99c7e9f088
@ -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);
|
||||
|
||||
@ -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 {
|
||||
|
@ -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) {}
|
||||
// ^
|
||||
}
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user