flutter analyze cleanup (#20490)

* `flutter analyze` cleanup

* Make `--dartdocs` work in all modes.
* Make `analyze-sample-code.dart` more resilient.
* Add a test for `analyze-sample-code.dart`.
* Minor cleanup in related code and files.

* Apply review comments

* Fix tests
This commit is contained in:
Ian Hickson
2018-08-20 12:51:07 -07:00
committed by GitHub
parent 2ed8a50e7e
commit ad1eaff45a
11 changed files with 234 additions and 134 deletions

View File

@@ -20,8 +20,8 @@ class AnalyzeCommand extends FlutterCommand {
help: 'Analyze the current project, if applicable.', defaultsTo: true);
argParser.addFlag('dartdocs',
negatable: false,
help: 'List every public member that is lacking documentation '
'(only works with --flutter-repo).',
help: 'List every public member that is lacking documentation.\n'
'(The public_member_api_docs lint must be enabled in analysis_options.yaml)',
hide: !verboseHelp);
argParser.addFlag('watch',
help: 'Run analysis continuously, watching the filesystem for changes.',
@@ -29,7 +29,7 @@ class AnalyzeCommand extends FlutterCommand {
argParser.addOption('write',
valueHelp: 'file',
help: 'Also output the results to a file. This is useful with --watch '
'if you want a file to always contain the latest results.');
'if you want a file to always contain the latest results.');
argParser.addOption('dart-sdk',
valueHelp: 'path-to-sdk',
help: 'The path to the Dart SDK.',
@@ -45,13 +45,14 @@ class AnalyzeCommand extends FlutterCommand {
// Not used by analyze --watch
argParser.addFlag('congratulate',
help: 'When analyzing the flutter repository, show output even when '
'there are no errors, warnings, hints, or lints.',
help: 'Show output even when there are no errors, warnings, hints, or lints.\n'
'Ignored if --watch is specified.',
defaultsTo: true);
argParser.addFlag('preamble',
defaultsTo: true,
help: 'When analyzing the flutter repository, display the number of '
'files that will be analyzed.');
'files that will be analyzed.\n'
'Ignored if --watch is specified.');
}
/// The working directory for testing analysis using dartanalyzer.

View File

@@ -36,9 +36,6 @@ class AnalyzeContinuously extends AnalyzeBase {
Future<Null> analyze() async {
List<String> directories;
if (argResults['dartdocs'])
throwToolExit('The --dartdocs option is currently not supported when using --watch.');
if (argResults['flutter-repo']) {
final PackageDependencyTracker dependencies = new PackageDependencyTracker();
dependencies.checkForConflictingDependencies(repoPackages, dependencies);
@@ -96,6 +93,17 @@ class AnalyzeContinuously extends AnalyzeBase {
}
}
int issueCount = errors.length;
// count missing dartdocs
final int undocumentedMembers = errors.where((AnalysisError error) {
return error.code == 'public_member_api_docs';
}).length;
if (!argResults['dartdocs']) {
errors.removeWhere((AnalysisError error) => error.code == 'public_member_api_docs');
issueCount -= undocumentedMembers;
}
errors.sort();
for (AnalysisError error in errors) {
@@ -108,15 +116,9 @@ class AnalyzeContinuously extends AnalyzeBase {
// Print an analysis summary.
String errorsMessage;
int issueCount = errors.length;
final int issueDiff = issueCount - lastErrorCount;
lastErrorCount = issueCount;
final int undocumentedCount = errors.where((AnalysisError issue) {
return issue.code == 'public_member_api_docs';
}).length;
if (firstAnalysis)
errorsMessage = '$issueCount ${pluralize('issue', issueCount)} found';
else if (issueDiff > 0)
@@ -128,15 +130,23 @@ class AnalyzeContinuously extends AnalyzeBase {
else
errorsMessage = 'no issues found';
String dartdocMessage;
if (undocumentedMembers == 1) {
dartdocMessage = 'one public member lacks documentation';
} else {
dartdocMessage = '$undocumentedMembers public members lack documentation';
}
final String files = '${analyzedPaths.length} ${pluralize('file', analyzedPaths.length)}';
final String seconds = (analysisTimer.elapsedMilliseconds / 1000.0).toStringAsFixed(2);
printStatus('$errorsMessage • analyzed $files in $seconds seconds');
if (undocumentedMembers > 0) {
printStatus('$errorsMessage • $dartdocMessage • analyzed $files in $seconds seconds');
} else {
printStatus('$errorsMessage • analyzed $files in $seconds seconds');
}
if (firstAnalysis && isBenchmarking) {
// We don't want to return a failing exit code based on missing documentation.
issueCount -= undocumentedCount;
writeBenchmark(analysisTimer, issueCount, undocumentedCount);
writeBenchmark(analysisTimer, issueCount, undocumentedMembers);
server.dispose().whenComplete(() { exit(issueCount > 0 ? 1 : 0); });
}

View File

@@ -54,30 +54,18 @@ class AnalyzeOnce extends AnalyzeBase {
if (argResults['flutter-repo']) {
// check for conflicting dependencies
final PackageDependencyTracker dependencies =
new PackageDependencyTracker();
final PackageDependencyTracker dependencies = new PackageDependencyTracker();
dependencies.checkForConflictingDependencies(repoPackages, dependencies);
directories.addAll(repoRoots);
if (argResults.wasParsed('current-package') &&
argResults['current-package']) {
if (argResults.wasParsed('current-package') && argResults['current-package'])
directories.add(currentDirectory);
}
} else {
if (argResults['current-package']) {
if (argResults['current-package'])
directories.add(currentDirectory);
}
}
if (argResults['dartdocs'] && !argResults['flutter-repo']) {
throwToolExit(
'The --dartdocs option is currently only supported with --flutter-repo.');
}
if (directories.isEmpty) {
if (directories.isEmpty)
throwToolExit('Nothing to analyze.', exitCode: 0);
}
// analyze all
final Completer<Null> analysisCompleter = new Completer<Null>();
@@ -96,8 +84,6 @@ class AnalyzeOnce extends AnalyzeBase {
}
});
server.onErrors.listen((FileAnalysisErrors fileErrors) {
fileErrors.errors
.removeWhere((AnalysisError error) => error.type == 'TODO');
errors.addAll(fileErrors.errors);
});
@@ -123,62 +109,50 @@ class AnalyzeOnce extends AnalyzeBase {
progress?.cancel();
timer.stop();
// report dartdocs
int undocumentedMembers = 0;
if (argResults['flutter-repo']) {
undocumentedMembers = errors.where((AnalysisError error) {
return error.code == 'public_member_api_docs';
}).length;
if (!argResults['dartdocs']) {
errors.removeWhere(
(AnalysisError error) => error.code == 'public_member_api_docs');
}
}
// count missing dartdocs
final int undocumentedMembers = errors.where((AnalysisError error) {
return error.code == 'public_member_api_docs';
}).length;
if (!argResults['dartdocs'])
errors.removeWhere((AnalysisError error) => error.code == 'public_member_api_docs');
// emit benchmarks
if (isBenchmarking) {
if (isBenchmarking)
writeBenchmark(timer, errors.length, undocumentedMembers);
}
// report results
dumpErrors(
errors.map<String>((AnalysisError error) => error.toLegacyString()));
// --write
dumpErrors(errors.map<String>((AnalysisError error) => error.toLegacyString()));
if (errors.isNotEmpty && argResults['preamble']) {
// report errors
if (errors.isNotEmpty && argResults['preamble'])
printStatus('');
}
errors.sort();
for (AnalysisError error in errors) {
for (AnalysisError error in errors)
printStatus(error.toString());
}
final String seconds =
(timer.elapsedMilliseconds / 1000.0).toStringAsFixed(1);
final String seconds = (timer.elapsedMilliseconds / 1000.0).toStringAsFixed(1);
String dartdocMessage;
if (undocumentedMembers == 1) {
dartdocMessage = 'one public member lacks documentation';
} else {
dartdocMessage = '$undocumentedMembers public members lack documentation';
}
// We consider any level of error to be an error exit (we don't report different levels).
if (errors.isNotEmpty) {
final int errorCount = errors.length;
printStatus('');
printStatus(
'${errors.length} ${pluralize('issue', errors.length)} found. (ran in ${seconds}s)');
if (undocumentedMembers > 0) {
throwToolExit('[lint] $undocumentedMembers public '
'${ undocumentedMembers == 1
? "member lacks"
: "members lack" } documentation');
throwToolExit('$errorCount ${pluralize('issue', errorCount)} found. (ran in ${seconds}s; $dartdocMessage)');
} else {
throwToolExit(null);
throwToolExit('$errorCount ${pluralize('issue', errorCount)} found. (ran in ${seconds}s)');
}
}
if (argResults['congratulate']) {
if (undocumentedMembers > 0) {
printStatus('No issues found! (ran in ${seconds}s; '
'$undocumentedMembers public ${ undocumentedMembers ==
1 ? "member lacks" : "members lack" } documentation)');
printStatus('No issues found! (ran in ${seconds}s; $dartdocMessage)');
} else {
printStatus('No issues found! (ran in ${seconds}s)');
}

View File

@@ -201,7 +201,8 @@ class AnalysisError implements Comparable<AnalysisError> {
String toString() {
return '${severity.toLowerCase().padLeft(7)} $_separator '
'$messageSentenceFragment $_separator '
'${fs.path.relative(file)}:$startLine:$startColumn';
'${fs.path.relative(file)}:$startLine:$startColumn $_separator '
'$code';
}
String toLegacyString() {

View File

@@ -95,8 +95,8 @@ void main() {
'Analyzing',
'warning $analyzerSeparator The parameter \'onPressed\' is required',
'info $analyzerSeparator The method \'_incrementCounter\' isn\'t used',
'2 issues found.',
],
exitMessageContains: '2 issues found.',
toolExit: true,
);
}, timeout: allowForSlowAnalyzeTests);
@@ -122,8 +122,8 @@ void main() {
'warning $analyzerSeparator The parameter \'onPressed\' is required',
'info $analyzerSeparator The method \'_incrementCounter\' isn\'t used',
'info $analyzerSeparator Only throw instances of classes extending either Exception or Error',
'3 issues found.',
],
exitMessageContains: '3 issues found.',
toolExit: true,
);
}, timeout: allowForSlowAnalyzeTests);
@@ -153,8 +153,8 @@ void bar() {
arguments: <String>['analyze'],
statusTextContains: <String>[
'Analyzing',
'1 issue found.',
],
exitMessageContains: '1 issue found.',
toolExit: true,
);
} finally {