Update our deprecation style. (#44618)
This commit is contained in:
@@ -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
|
||||
|
||||
6
dev/bots/test/analyze-test-input/.dartignore
Normal file
6
dev/bots/test/analyze-test-input/.dartignore
Normal 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.
|
||||
@@ -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() { }
|
||||
@@ -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 ? '\\' : '/'));
|
||||
|
||||
@@ -10,18 +10,22 @@ import 'package:flutter_devicelab/framework/framework.dart';
|
||||
import 'package:flutter_devicelab/framework/utils.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
// the numbers below are odd, so that the totals don't seem round. :-)
|
||||
// the numbers below are prime, so that the totals don't seem round. :-)
|
||||
const double todoCost = 1009.0; // about two average SWE days, in dollars
|
||||
const double ignoreCost = 2003.0; // four average SWE days, in dollars
|
||||
const double pythonCost = 3001.0; // six average SWE days, in dollars
|
||||
const double skipCost = 2473.0; // 20 hours: 5 to fix the issue we're ignoring, 15 to fix the bugs we missed because the test was off
|
||||
const double ignoreForFileCost = 2477.0; // similar thinking as skipCost
|
||||
const double asDynamicCost = 2003.0; // same as ignoring analyzer warning
|
||||
const double asDynamicCost = 2011.0; // a few days to refactor the code.
|
||||
const double deprecationCost = 233.0; // a few hours to remove the old code.
|
||||
const double grandfatheredDeprecationCost = 9973.0; // a couple of weeks.
|
||||
|
||||
final RegExp todoPattern = RegExp(r'(?://|#) *TODO');
|
||||
final RegExp ignorePattern = RegExp(r'// *ignore:');
|
||||
final RegExp ignoreForFilePattern = RegExp(r'// *ignore_for_file:');
|
||||
final RegExp asDynamicPattern = RegExp(r'as dynamic');
|
||||
final RegExp asDynamicPattern = RegExp(r'\bas dynamic\b');
|
||||
final RegExp deprecationPattern = RegExp(r'^ *@[dD]eprecated');
|
||||
const String grandfatheredDeprecationPattern = '// ignore: flutter_deprecation_syntax, https';
|
||||
|
||||
Future<double> findCostsForFile(File file) async {
|
||||
if (path.extension(file.path) == '.py')
|
||||
@@ -39,8 +43,12 @@ Future<double> findCostsForFile(File file) async {
|
||||
total += ignoreCost;
|
||||
if (line.contains(ignoreForFilePattern))
|
||||
total += ignoreForFileCost;
|
||||
if (line.contains(asDynamicPattern))
|
||||
if (!isTest && line.contains(asDynamicPattern))
|
||||
total += asDynamicCost;
|
||||
if (line.contains(deprecationPattern))
|
||||
total += deprecationCost;
|
||||
if (line.contains(grandfatheredDeprecationPattern))
|
||||
total += grandfatheredDeprecationCost;
|
||||
if (isTest && line.contains('skip:'))
|
||||
total += skipCost;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user