Update our deprecation style. (#44618)

This commit is contained in:
Ian Hickson
2019-11-15 19:21:53 -08:00
committed by GitHub
parent 879da263ff
commit 62e4ab87b3
27 changed files with 363 additions and 53 deletions

View File

@@ -65,6 +65,7 @@ Future<void> run(List<String> arguments) async {
exit(1);
}
await verifyDeprecations(flutterRoot);
await verifyNoMissingLicense(flutterRoot);
await verifyNoTestImports(flutterRoot);
await verifyNoTestPackageImports(flutterRoot);
@@ -124,11 +125,100 @@ Future<void> run(List<String> arguments) async {
}
}
// TESTS
final RegExp _findDeprecationPattern = RegExp(r'@[Dd]eprecated');
final RegExp _deprecationPattern1 = RegExp(r'^( *)@Deprecated\($'); // ignore: flutter_deprecation_syntax (see analyze.dart)
final RegExp _deprecationPattern2 = RegExp(r"^ *'(.+) '$");
final RegExp _deprecationPattern3 = RegExp(r"^ *'This feature was deprecated after v([0-9]+)\.([0-9]+)\.([0-9]+)\.(?: See: (https://flutter.dev/.+))?'$");
final RegExp _deprecationPattern4 = RegExp(r'^ *\)$');
/// Some deprecation notices are special, for example they're used to annotate members that
/// will never go away and were never allowed but which we are trying to show messages for.
/// (One example would be a library that intentionally conflicts with a member in another
/// library to indicate that it is incompatible with that other library. Another would be
/// the regexp just above...)
const String _ignoreDeprecation = ' // ignore: flutter_deprecation_syntax (see analyze.dart)';
/// Some deprecation notices are grand-fathered in for now. They must have an issue listed.
final RegExp _grandfatheredDeprecation = RegExp(r' // ignore: flutter_deprecation_syntax, https://github.com/flutter/flutter/issues/[0-9]+$');
Future<void> verifyDeprecations(String workingDirectory) async {
final List<String> errors = <String>[];
for (File file in _dartFiles(workingDirectory)) {
int lineNumber = 0;
final List<String> lines = file.readAsLinesSync();
final List<int> linesWithDeprecations = <int>[];
for (String line in lines) {
if (line.contains(_findDeprecationPattern) &&
!line.endsWith(_ignoreDeprecation) &&
!line.contains(_grandfatheredDeprecation)) {
linesWithDeprecations.add(lineNumber);
}
lineNumber += 1;
}
for (int lineNumber in linesWithDeprecations) {
try {
final Match match1 = _deprecationPattern1.firstMatch(lines[lineNumber]);
if (match1 == null)
throw 'Deprecation notice does not match required pattern.';
final String indent = match1[1];
lineNumber += 1;
if (lineNumber >= lines.length)
throw 'Incomplete deprecation notice.';
Match match3;
String message;
do {
final Match match2 = _deprecationPattern2.firstMatch(lines[lineNumber]);
if (match2 == null)
throw 'Deprecation notice does not match required pattern.';
if (!lines[lineNumber].startsWith("$indent '"))
throw 'Unexpected deprecation notice indent.';
if (message == null) {
final String firstChar = String.fromCharCode(match2[1].runes.first);
if (firstChar.toUpperCase() != firstChar)
throw 'Deprecation notice should be a grammatically correct sentence and start with a capital letter; see style guide.';
}
message = match2[1];
lineNumber += 1;
if (lineNumber >= lines.length)
throw 'Incomplete deprecation notice.';
match3 = _deprecationPattern3.firstMatch(lines[lineNumber]);
} while (match3 == null);
if (!message.endsWith('.') && !message.endsWith('!') && !message.endsWith('?'))
throw 'Deprecation notice should be a grammatically correct sentence and end with a period.';
if (!lines[lineNumber].startsWith("$indent '"))
throw 'Unexpected deprecation notice indent.';
if (int.parse(match3[1]) > 1 || int.parse(match3[2]) > 11) {
if (match3[4] == null)
throw 'A URL to the deprecation notice is required.';
}
lineNumber += 1;
if (lineNumber >= lines.length)
throw 'Incomplete deprecation notice.';
if (!lines[lineNumber].contains(_deprecationPattern4))
throw 'End of deprecation notice does not match required pattern.';
if (!lines[lineNumber].startsWith('$indent)'))
throw 'Unexpected deprecation notice indent.';
} catch (error) {
errors.add('${file.path}:${lineNumber + 1}: $error');
}
}
}
// Fail if any errors
if (errors.isNotEmpty) {
print('$redLine');
print(errors.join('\n'));
print('${bold}See: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes$reset\n');
print('$redLine\n');
exit(1);
}
}
Future<void> verifyNoMissingLicense(String workingDirectory) async {
final List<String> errors = <String>[];
for (FileSystemEntity entity in Directory(path.join(workingDirectory, 'packages'))
.listSync(recursive: true)
.where((FileSystemEntity entity) => entity is File && path.extension(entity.path) == '.dart')) {
for (FileSystemEntity entity in _dartFiles(workingDirectory)) {
final File file = entity;
bool hasLicense = false;
final List<String> lines = file.readAsLinesSync();
@@ -489,6 +579,26 @@ bool _listEquals<T>(List<T> a, List<T> b) {
return true;
}
Iterable<File> _dartFiles(String workingDirectory) sync* {
final Set<FileSystemEntity> pending = <FileSystemEntity>{ Directory(workingDirectory) };
while (pending.isNotEmpty) {
final FileSystemEntity entity = pending.first;
pending.remove(entity);
if (entity is File) {
if (path.extension(entity.path) == '.dart')
yield entity;
} else if (entity is Directory) {
if (File(path.join(entity.path, '.dartignore')).existsSync())
continue;
if (path.basename(entity.path) == '.git')
continue;
if (path.basename(entity.path) == '.dart_tool')
continue;
pending.addAll(entity.listSync());
}
}
}
Future<String> _getCommitRange() async {
// Using --fork-point is more conservative, and will result in the correct
// fork point, but when running locally, it may return nothing. Git is

View File

@@ -0,0 +1,6 @@
This directory is excluded from analysis because its whole point is to
test the analysis (so it has issues).
We have to have the actual test file system in a subdirectory (root)
because otherwise the .dartignore file in that directory would cause
the test itself to ignore the directory.

View File

@@ -0,0 +1,54 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@Deprecated(
'This is the reason and what you should use instead. '
'This feature was deprecated after v1.2.3.'
)
void test1() { }
@Deprecated(
'Missing space ->.'
'This feature was deprecated after v1.2.3.'
)
void test2() { }
@Deprecated(
'bad grammar. '
'This feature was deprecated after v1.2.3.'
)
void test3() { }
@Deprecated(
'Also bad grammar '
'This feature was deprecated after v1.2.3.'
)
void test4() { }
@deprecated // no message
void test5() { }
@Deprecated('Not the right syntax. This feature was deprecated after v1.2.3.')
void test6() { }
@Deprecated(
'Missing the version line. '
)
void test7() { }
@Deprecated(
'This feature was deprecated after v1.2.3.'
)
void test8() { }
@Deprecated(
'Not the right syntax. '
'This feature was deprecated after v1.2.3.'
) void test9() { }
@Deprecated(
'Not the right syntax. '
'This feature was deprecated after v1.2.3.'
)
void test10() { }

View File

@@ -31,13 +31,38 @@ Future<String> capture(AsyncVoidCallback callback, { int exitCode = 0 }) async {
}
void main() {
test('analyze.dart - verifyDeprecations', () async {
final String result = await capture(() => verifyDeprecations(path.join('test', 'analyze-test-input', 'root')), exitCode: 1);
expect(result,
'━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n'
+
(
'test/analyze-test-input/root/packages/foo/deprecation.dart:12: Deprecation notice does not match required pattern.\n'
'test/analyze-test-input/root/packages/foo/deprecation.dart:18: Deprecation notice should be a grammatically correct sentence and start with a capital letter; see style guide.\n'
'test/analyze-test-input/root/packages/foo/deprecation.dart:25: Deprecation notice should be a grammatically correct sentence and end with a period.\n'
'test/analyze-test-input/root/packages/foo/deprecation.dart:29: Deprecation notice does not match required pattern.\n'
'test/analyze-test-input/root/packages/foo/deprecation.dart:32: Deprecation notice does not match required pattern.\n'
'test/analyze-test-input/root/packages/foo/deprecation.dart:37: Deprecation notice does not match required pattern.\n'
'test/analyze-test-input/root/packages/foo/deprecation.dart:41: Deprecation notice does not match required pattern.\n'
'test/analyze-test-input/root/packages/foo/deprecation.dart:48: End of deprecation notice does not match required pattern.\n'
'test/analyze-test-input/root/packages/foo/deprecation.dart:51: Unexpected deprecation notice indent.\n'
.replaceAll('/', Platform.isWindows ? '\\' : '/')
)
+
'See: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes\n'
'\n'
'━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n'
'\n'
);
});
test('analyze.dart - verifyNoMissingLicense', () async {
final String result = await capture(() => verifyNoMissingLicense(path.join('test', 'analyze-test-input')), exitCode: 1);
final String result = await capture(() => verifyNoMissingLicense(path.join('test', 'analyze-test-input', 'root')), exitCode: 1);
expect(result, '''
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
License headers cannot be found at the beginning of the following file.
test/analyze-test-input/packages/foo/foo.dart
test/analyze-test-input/root/packages/foo/foo.dart
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
'''.replaceAll('/', Platform.isWindows ? '\\' : '/'));