@@ -20,7 +20,6 @@ import 'package:path/path.dart' as path;
2020import 'package:yaml/yaml.dart' ;
2121
2222import 'lint_sets.dart' ;
23- import 'linter_options.dart' ;
2423import 'test_linter.dart' ;
2524
2625/// Benchmarks lint rules.
@@ -62,16 +61,20 @@ Iterable<File> collectFiles(String entityPath) {
6261 return files;
6362}
6463
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 {
6669 // Setup an error watcher to track whether an error was logged to stderr so
6770 // we can set the exit code accordingly.
6871 var errorWatcher = _ErrorWatchingSink (errorSink);
6972 errorSink = errorWatcher;
70- var errors = await linter .lintFiles (filesToLint);
73+ var diagnostics = await TestLinter (rules, dartSdkPath) .lintFiles (filesToLint);
7174 if (errorWatcher.encounteredError) {
7275 exitCode = loggedAnalyzerErrorExitCode;
73- } else if (errors .isNotEmpty) {
74- exitCode = _maxSeverity (errors );
76+ } else if (diagnostics .isNotEmpty) {
77+ exitCode = _maxSeverity (diagnostics );
7578 }
7679}
7780
@@ -116,7 +119,7 @@ Future<void> runLinter(List<String> args) async {
116119 return ;
117120 }
118121
119- if (options[ 'help' ] as bool ) {
122+ if (options. flag ( 'help' ) ) {
120123 printUsage (parser, outSink);
121124 return ;
122125 }
@@ -132,25 +135,21 @@ Future<void> runLinter(List<String> args) async {
132135 return ;
133136 }
134137
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' );
138141
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 ( );
142145 var options = loadYamlNode (optionsContent) as YamlMap ;
143146 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 > [];
154153 for (var ruleName in ruleNames) {
155154 var rule = Registry .ruleRegistry[ruleName];
156155 if (rule == null ) {
@@ -159,9 +158,8 @@ Future<void> runLinter(List<String> args) async {
159158 }
160159 rules.add (rule);
161160 }
162- linterOptions = LinterOptions (enabledRules: rules, dartSdkPath: customSdk);
163161 } else {
164- linterOptions = LinterOptions (dartSdkPath : customSdk );
162+ rules = Registry .ruleRegistry. toList ( );
165163 }
166164
167165 var filesToLint = [
@@ -171,17 +169,23 @@ Future<void> runLinter(List<String> args) async {
171169 ).map ((file) => file.path.toAbsoluteNormalizedPath ()).map (File .new ),
172170 ];
173171
174- await writeBenchmarks (outSink, filesToLint, linterOptions);
172+ await writeBenchmarks (
173+ outSink,
174+ filesToLint,
175+ rules: rules,
176+ dartSdkPath: dartSdkPath,
177+ );
175178}
176179
177180Future <void > writeBenchmarks (
178181 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 {
182186 var timings = < String , int > {};
183187 for (var i = 0 ; i < benchmarkRuns; ++ i) {
184- await lintFiles (TestLinter (linterOptions), filesToLint );
188+ await lintFiles (filesToLint, rules : rules, dartSdkPath : dartSdkPath );
185189 analysisRuleTimers.timers.forEach ((n, t) {
186190 var timing = t.elapsedMilliseconds;
187191 var previous = timings[n];
@@ -195,18 +199,13 @@ Future<void> writeBenchmarks(
195199
196200 var stats =
197201 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+ ];
208207
209- var details = sets .isEmpty ? '' : " [${sets .join (', ' )}]" ;
208+ var details = rulesets .isEmpty ? '' : " [${rulesets .join (', ' )}]" ;
210209 return Stat ('$t $details ' , timings[t] ?? 0 );
211210 }).toList ();
212211 out.writeTimings (stats, 0 );
@@ -260,6 +259,7 @@ extension on String {
260259 path.split (this ).any ((part) => part.startsWith ('.' ));
261260
262261 /// Whether this path is a Dart file or a Pubspec file.
262+ // TODO(srawlins): This should include analysis options files as well.
263263 bool get isLintable =>
264264 endsWith ('.dart' ) || path.basename (this ) == file_paths.pubspecYaml;
265265}
0 commit comments