|
| 1 | +// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:analyzer/dart/analysis/analysis_context_collection.dart'; |
| 8 | +import 'package:analyzer/error/error.dart'; |
| 9 | +import 'package:analyzer/file_system/physical_file_system.dart'; |
| 10 | + |
| 11 | +/// A simple example of using the AnalysisContextCollection API. |
| 12 | +void main(List<String> args) async { |
| 13 | + var entity = Directory.current; |
| 14 | + if (args.isNotEmpty) { |
| 15 | + String arg = args.first; |
| 16 | + entity = FileSystemEntity.isDirectorySync(arg) ? Directory(arg) : File(arg); |
| 17 | + } |
| 18 | + |
| 19 | + var issueCount = 0; |
| 20 | + final collection = AnalysisContextCollection( |
| 21 | + includedPaths: [entity.absolute.path], |
| 22 | + resourceProvider: PhysicalResourceProvider.INSTANCE); |
| 23 | + |
| 24 | + // Often one context is returned, but depending on the project structure we |
| 25 | + // can see multiple contexts. |
| 26 | + for (final context in collection.contexts) { |
| 27 | + print('Analyzing ${context.contextRoot.root.path} ...'); |
| 28 | + |
| 29 | + for (final filePath in context.contextRoot.analyzedFiles()) { |
| 30 | + if (!filePath.endsWith('.dart')) { |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + final errorsResult = await context.currentSession.getErrors(filePath); |
| 35 | + for (final error in errorsResult.errors) { |
| 36 | + if (error.errorCode.type != ErrorType.TODO) { |
| 37 | + print( |
| 38 | + ' \u001b[1m${error.source.shortName}\u001b[0m ${error.message}'); |
| 39 | + issueCount++; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + print('$issueCount issues found.'); |
| 46 | +} |
0 commit comments