Skip to content

Commit 11b3e34

Browse files
devoncarewcommit-bot@chromium.org
authored andcommitted
[pkg:analyzer] add an example of using the analysis context collection API
Change-Id: Ic00410c941861a791b549c799c113b5364ba6486 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/174981 Commit-Queue: Devon Carew <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 6143fb5 commit 11b3e34

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

pkg/analyzer/example/analyze.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

Comments
 (0)