@@ -20,7 +20,6 @@ import 'package:path/path.dart' as path;
20
20
import 'package:yaml/yaml.dart' ;
21
21
22
22
import 'lint_sets.dart' ;
23
- import 'linter_options.dart' ;
24
23
import 'test_linter.dart' ;
25
24
26
25
/// Benchmarks lint rules.
@@ -62,16 +61,20 @@ Iterable<File> collectFiles(String entityPath) {
62
61
return files;
63
62
}
64
63
65
- Future <void > lintFiles (TestLinter linter, List <File > filesToLint) async {
64
+ Future <void > lintFiles (
65
+ List <File > filesToLint, {
66
+ required List <AbstractAnalysisRule > rules,
67
+ required String ? dartSdkPath,
68
+ }) async {
66
69
// Setup an error watcher to track whether an error was logged to stderr so
67
70
// we can set the exit code accordingly.
68
71
var errorWatcher = _ErrorWatchingSink (errorSink);
69
72
errorSink = errorWatcher;
70
- var errors = await linter .lintFiles (filesToLint);
73
+ var diagnostics = await TestLinter (rules, dartSdkPath) .lintFiles (filesToLint);
71
74
if (errorWatcher.encounteredError) {
72
75
exitCode = loggedAnalyzerErrorExitCode;
73
- } else if (errors .isNotEmpty) {
74
- exitCode = _maxSeverity (errors );
76
+ } else if (diagnostics .isNotEmpty) {
77
+ exitCode = _maxSeverity (diagnostics );
75
78
}
76
79
}
77
80
@@ -116,7 +119,7 @@ Future<void> runLinter(List<String> args) async {
116
119
return ;
117
120
}
118
121
119
- if (options[ 'help' ] as bool ) {
122
+ if (options. flag ( 'help' ) ) {
120
123
printUsage (parser, outSink);
121
124
return ;
122
125
}
@@ -132,25 +135,21 @@ Future<void> runLinter(List<String> args) async {
132
135
return ;
133
136
}
134
137
135
- var configFile = options[ 'config' ] ;
136
- var ruleNames = options[ 'rules' ] ;
137
- var customSdk = options.option ('dart-sdk' );
138
+ var configFile = options. option ( 'config' ) ;
139
+ var ruleNames = options. multiOption ( 'rules' ) ;
140
+ var dartSdkPath = options.option ('dart-sdk' );
138
141
139
- LinterOptions linterOptions ;
140
- if (configFile is String ) {
141
- var optionsContent = readFile (configFile);
142
+ List < AbstractAnalysisRule > rules ;
143
+ if (configFile != null ) {
144
+ var optionsContent = File (configFile). readAsStringSync ( );
142
145
var options = loadYamlNode (optionsContent) as YamlMap ;
143
146
var ruleConfigs = parseLinterSection (options)! .values;
144
- var enabledRules = Registry .ruleRegistry.where (
145
- (rule) => ! ruleConfigs.any ((rc) => rc.disables (rule.name)),
146
- );
147
-
148
- linterOptions = LinterOptions (
149
- enabledRules: enabledRules,
150
- dartSdkPath: customSdk,
151
- );
152
- } else if (ruleNames is Iterable <String > && ruleNames.isNotEmpty) {
153
- var rules = < AbstractAnalysisRule > [];
147
+ rules =
148
+ Registry .ruleRegistry
149
+ .where ((rule) => ! ruleConfigs.any ((rc) => rc.disables (rule.name)))
150
+ .toList ();
151
+ } else if (ruleNames.isNotEmpty) {
152
+ rules = < AbstractAnalysisRule > [];
154
153
for (var ruleName in ruleNames) {
155
154
var rule = Registry .ruleRegistry[ruleName];
156
155
if (rule == null ) {
@@ -159,9 +158,8 @@ Future<void> runLinter(List<String> args) async {
159
158
}
160
159
rules.add (rule);
161
160
}
162
- linterOptions = LinterOptions (enabledRules: rules, dartSdkPath: customSdk);
163
161
} else {
164
- linterOptions = LinterOptions (dartSdkPath : customSdk );
162
+ rules = Registry .ruleRegistry. toList ( );
165
163
}
166
164
167
165
var filesToLint = [
@@ -171,17 +169,23 @@ Future<void> runLinter(List<String> args) async {
171
169
).map ((file) => file.path.toAbsoluteNormalizedPath ()).map (File .new ),
172
170
];
173
171
174
- await writeBenchmarks (outSink, filesToLint, linterOptions);
172
+ await writeBenchmarks (
173
+ outSink,
174
+ filesToLint,
175
+ rules: rules,
176
+ dartSdkPath: dartSdkPath,
177
+ );
175
178
}
176
179
177
180
Future <void > writeBenchmarks (
178
181
StringSink out,
179
- List <File > filesToLint,
180
- LinterOptions linterOptions,
181
- ) async {
182
+ List <File > filesToLint, {
183
+ required List <AbstractAnalysisRule > rules,
184
+ required String ? dartSdkPath,
185
+ }) async {
182
186
var timings = < String , int > {};
183
187
for (var i = 0 ; i < benchmarkRuns; ++ i) {
184
- await lintFiles (TestLinter (linterOptions), filesToLint );
188
+ await lintFiles (filesToLint, rules : rules, dartSdkPath : dartSdkPath );
185
189
analysisRuleTimers.timers.forEach ((n, t) {
186
190
var timing = t.elapsedMilliseconds;
187
191
var previous = timings[n];
@@ -195,18 +199,13 @@ Future<void> writeBenchmarks(
195
199
196
200
var stats =
197
201
timings.keys.map ((t) {
198
- var sets = < String > [];
199
- if (coreRuleset.contains (t)) {
200
- sets.add ('core' );
201
- }
202
- if (recommendedRuleset.contains (t)) {
203
- sets.add ('recommended' );
204
- }
205
- if (flutterRuleset.contains (t)) {
206
- sets.add ('flutter' );
207
- }
202
+ var rulesets = [
203
+ if (coreRuleset.contains (t)) 'core' ,
204
+ if (recommendedRuleset.contains (t)) 'recommended' ,
205
+ if (flutterRuleset.contains (t)) 'flutter' ,
206
+ ];
208
207
209
- var details = sets .isEmpty ? '' : " [${sets .join (', ' )}]" ;
208
+ var details = rulesets .isEmpty ? '' : " [${rulesets .join (', ' )}]" ;
210
209
return Stat ('$t $details ' , timings[t] ?? 0 );
211
210
}).toList ();
212
211
out.writeTimings (stats, 0 );
@@ -260,6 +259,7 @@ extension on String {
260
259
path.split (this ).any ((part) => part.startsWith ('.' ));
261
260
262
261
/// Whether this path is a Dart file or a Pubspec file.
262
+ // TODO(srawlins): This should include analysis options files as well.
263
263
bool get isLintable =>
264
264
endsWith ('.dart' ) || path.basename (this ) == file_paths.pubspecYaml;
265
265
}
0 commit comments