88// bin/cache/dart-sdk/bin/dart dev/bots/analyze-sample-code.dart
99
1010import 'dart:io' ;
11- import 'dart:isolate' ;
1211
1312import 'package:args/args.dart' ;
1413import 'package:path/path.dart' as path;
@@ -203,6 +202,8 @@ class SampleChecker {
203202 return _headers ?? = < String > [
204203 '// generated code' ,
205204 '// ignore_for_file: unused_import' ,
205+ '// ignore_for_file: unused_element' ,
206+ '// ignore_for_file: unused_local_variable' ,
206207 "import 'dart:async';" ,
207208 "import 'dart:convert';" ,
208209 "import 'dart:math' as math;" ,
@@ -547,8 +548,9 @@ linter:
547548 }
548549
549550 /// Invokes the analyzer on the given [directory] and returns the stdout.
550- List <String > _runAnalyzer (Directory directory) {
551- print ('Starting analysis of code samples.' );
551+ List <String > _runAnalyzer (Directory directory, {bool silent}) {
552+ if (! silent)
553+ print ('Starting analysis of code samples.' );
552554 _createConfigurationFiles (directory);
553555 final ProcessResult result = Process .runSync (
554556 _flutter,
@@ -570,7 +572,7 @@ linter:
570572 stderr.removeLast ();
571573 }
572574 }
573- if (stderr.isNotEmpty) {
575+ if (stderr.isNotEmpty && stderr. any (( String line) => line.isNotEmpty) ) {
574576 throw 'Cannot analyze dartdocs; unexpected error output:\n $stderr ' ;
575577 }
576578 if (stdout.isNotEmpty && stdout.first == 'Building flutter tool...' ) {
@@ -588,9 +590,10 @@ linter:
588590 Map <String , List <AnalysisError >> _analyze (
589591 Directory directory,
590592 Map <String , Section > sections,
591- Map <String , Sample > samples,
592- ) {
593- final List <String > errors = _runAnalyzer (directory);
593+ Map <String , Sample > samples, {
594+ bool silent = false ,
595+ }) {
596+ final List <String > errors = _runAnalyzer (directory, silent: silent);
594597 final Map <String , List <AnalysisError >> analysisErrors = < String , List <AnalysisError >> {};
595598 void addAnalysisError (File file, AnalysisError error) {
596599 if (analysisErrors.containsKey (file.path)) {
@@ -621,10 +624,6 @@ linter:
621624 final String errorCode = parts[6 ];
622625 final int lineNumber = int .parse (line, radix: 10 ) - (isSnippet ? headerLength : 0 );
623626 final int columnNumber = int .parse (column, radix: 10 );
624- if (lineNumber < 0 && errorCode == 'unused_import' ) {
625- // We don't care about unused imports.
626- continue ;
627- }
628627
629628 // For when errors occur outside of the things we're trying to analyze.
630629 if (! isSnippet && ! isSample) {
@@ -649,10 +648,6 @@ linter:
649648 );
650649 }
651650
652- if (errorCode == 'unused_element' || errorCode == 'unused_local_variable' ) {
653- // We don't really care if sample code isn't used!
654- continue ;
655- }
656651 if (isSample) {
657652 addAnalysisError (
658653 file,
@@ -739,7 +734,8 @@ linter:
739734 _exitCode = 0 ;
740735 }
741736 if (_exitCode == 0 ) {
742- print ('No analysis errors in samples!' );
737+ if (! silent)
738+ print ('No analysis errors in samples!' );
743739 assert (analysisErrors.isEmpty);
744740 }
745741 return analysisErrors;
@@ -960,24 +956,28 @@ Future<void> _runInteractive(Directory tempDir, Directory flutterPackage, String
960956 print ('Using temp dir ${tempDir .path }' );
961957 }
962958
963- final SampleChecker checker = SampleChecker (flutterPackage, tempDirectory: tempDir)
964- .._createConfigurationFiles (tempDir)
965- .._extractSamples (< File > [file], silent: true );
966-
967- await Isolate .spawn (_watcher, < dynamic > [checker, file]);
959+ void analyze (SampleChecker checker, File file) {
960+ final Map <String , Section > sections = < String , Section > {};
961+ final Map <String , Sample > snippets = < String , Sample > {};
962+ checker._extractSamples (< File > [file], silent: true , sectionMap: sections, sampleMap: snippets);
963+ final Map <String , List <AnalysisError >> errors = checker._analyze (checker._tempDirectory, sections, snippets, silent: true );
964+ stderr.writeln ('\u 001B[2J\u 001B[H' ); // Clears the old results from the terminal.
965+ if (errors.isNotEmpty) {
966+ for (final String filePath in errors.keys) {
967+ errors[filePath].forEach (stderr.writeln);
968+ }
969+ stderr.writeln ('\n Found ${errors .length } errors.' );
970+ } else {
971+ stderr.writeln ('\n No issues found.' );
972+ }
973+ }
968974
969- await Process .start (
970- _flutter,
971- < String > ['--no-wrap' , 'analyze' , '--no-preamble' , '--no-congratulate' , '--watch' , '.' ],
972- workingDirectory: tempDir.absolute.path,
973- mode: ProcessStartMode .inheritStdio
974- );
975- }
975+ final SampleChecker checker = SampleChecker (flutterPackage, tempDirectory: tempDir)
976+ .._createConfigurationFiles (tempDir);
977+ analyze (checker, file);
976978
977- void _watcher (List <dynamic > args) {
978- final File file = args.last as File ;
979- final SampleChecker checker = args.first as SampleChecker ;
980979 Watcher (file.absolute.path).events.listen ((_) {
981- checker._extractSamples (< File > [file], silent: true );
980+ print ('\n\n Rerunning...' );
981+ analyze (checker, file);
982982 });
983983}
0 commit comments