Files
sdk/pkg/analyzer/example/analyze.dart
T
Sam Rawlins 7b42aae99f analyzer: Deprecate AnalysisResultWithErrors.errors in favor of .diagnostics
Also rename UnitAnalysisResult.errors (package-private API) to .diagnostics

Work towards https://github.com/dart-lang/sdk/issues/60635

Change-Id: I0bdd7c9c19cff3bff9ee61fe4689564a7b5b727b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/433581
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
2025-06-09 15:51:01 -07:00

52 lines
1.7 KiB
Dart

// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/file_system/physical_file_system.dart';
/// A simple example of using the AnalysisContextCollection API.
void main(List<String> args) async {
FileSystemEntity entity = Directory.current;
if (args.isNotEmpty) {
String arg = args.first;
entity = FileSystemEntity.isDirectorySync(arg) ? Directory(arg) : File(arg);
}
var issueCount = 0;
var collection = AnalysisContextCollection(
includedPaths: [entity.absolute.path],
resourceProvider: PhysicalResourceProvider.INSTANCE,
);
// Often one context is returned, but depending on the project structure we
// can see multiple contexts.
for (var context in collection.contexts) {
print('Analyzing ${context.contextRoot.root.path} ...');
for (var filePath in context.contextRoot.analyzedFiles()) {
if (!filePath.endsWith('.dart')) {
continue;
}
var errorsResult = await context.currentSession.getErrors(filePath);
if (errorsResult is ErrorsResult) {
for (var diagnostic in errorsResult.diagnostics) {
if (diagnostic.diagnosticCode.type != DiagnosticType.TODO) {
print(
' \u001b[1m${diagnostic.source.shortName}\u001b[0m ${diagnostic.message}',
);
issueCount++;
}
}
}
}
}
print('$issueCount issues found.');
}