Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit ecf5c34

Browse files
committed
Merge pull request #53 from stereotype441/visit-transitive-closure
Add "--visit-transitive-closure" command line option.
2 parents b7c6c7a + 93ce114 commit ecf5c34

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

bin/linter.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ void main(List<String> args) {
1919
abbr: "h", negatable: false, help: "Shows usage information.")
2020
..addFlag("stats",
2121
abbr: "s", negatable: false, help: "Show lint statistics.")
22+
..addFlag('visit-transitive-closure',
23+
help: 'Visit the transitive closure of imported/exported libraries.')
2224
..addOption('config', abbr: 'c', help: 'Use configuration from this file.')
2325
..addOption('dart-sdk', help: 'Custom path to a Dart SDK.')
2426
..addOption('package-root',
@@ -64,6 +66,8 @@ void main(List<String> args) {
6466
lintOptions.packageRootPath = customPackageRoot;
6567
}
6668

69+
lintOptions.visitTransitiveClosure = options['visit-transitive-closure'];
70+
6771
var linter = new DartLinter(lintOptions);
6872

6973
List<File> filesToLint = [];
@@ -78,7 +82,9 @@ void main(List<String> args) {
7882
var stats = options['stats'];
7983
ReportFormatter reporter = new ReportFormatter(
8084
errors, lintOptions.filter, outSink,
81-
fileCount: filesToLint.length, fileRoot: commonRoot, showStatistics: stats);
85+
fileCount: filesToLint.length,
86+
fileRoot: commonRoot,
87+
showStatistics: stats);
8288
reporter.write();
8389
} catch (err, stack) {
8490
errorSink.writeln('''An error occurred while linting

lib/src/analysis.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
library analysis;
66

7+
import 'dart:collection';
78
import 'dart:io';
89

910
import 'package:analyzer/file_system/file_system.dart' show Folder;
1011
import 'package:analyzer/file_system/physical_file_system.dart';
1112
import 'package:analyzer/source/package_map_provider.dart';
1213
import 'package:analyzer/source/package_map_resolver.dart';
1314
import 'package:analyzer/source/pub_package_map_provider.dart';
15+
import 'package:analyzer/src/generated/element.dart';
1416
import 'package:analyzer/src/generated/engine.dart';
1517
import 'package:analyzer/src/generated/java_io.dart';
1618
import 'package:analyzer/src/generated/sdk.dart';
@@ -34,6 +36,7 @@ AnalysisOptions _buildAnalyzerOptions(DriverOptions options) {
3436

3537
class AnalysisDriver {
3638
final DriverOptions options;
39+
3740
AnalysisDriver(this.options);
3841

3942
List<UriResolver> get resolvers {
@@ -86,13 +89,42 @@ class AnalysisDriver {
8689

8790
List<AnalysisErrorInfo> errors = [];
8891

92+
Set<Source> sourcesAnalyzed = new HashSet<Source>();
93+
8994
for (Source source in sources) {
9095
context.computeErrors(source);
9196
errors.add(context.getErrors(source));
97+
sourcesAnalyzed.add(source);
98+
}
99+
100+
if (options.visitTransitiveClosure) {
101+
// In the process of computing errors for all the sources in [sources],
102+
// the analyzer has visited the transitive closure of all libraries
103+
// referenced by those sources. So now we simply need to visit all
104+
// library sources known to the analysis context, and all parts they
105+
// refer to.
106+
for (Source librarySource in context.librarySources) {
107+
for (Source source in _getAllUnitSources(context, librarySource)) {
108+
if (!sourcesAnalyzed.contains(source)) {
109+
context.computeErrors(source);
110+
errors.add(context.getErrors(source));
111+
sourcesAnalyzed.add(source);
112+
}
113+
}
114+
}
92115
}
93116

94117
return errors;
95118
}
119+
120+
/// Yield the sources for all the compilation units constituting
121+
/// [librarySource] (including the defining compilation unit).
122+
Iterable<Source> _getAllUnitSources(
123+
AnalysisContext context, Source librarySource) sync* {
124+
yield librarySource;
125+
yield* context.getLibraryElement(librarySource).parts
126+
.map((CompilationUnitElement e) => e.source);
127+
}
96128
}
97129

98130
class DriverOptions {
@@ -112,6 +144,10 @@ class DriverOptions {
112144

113145
/// Whether to show SDK warnings.
114146
bool showSdkWarnings = false;
147+
148+
/// Whether to show lints for the transitive closure of imported and exported
149+
/// libraries.
150+
bool visitTransitiveClosure = false;
115151
}
116152

117153
/// Prints logging information comments to the [outSink] and error messages to

0 commit comments

Comments
 (0)