Skip to content

Commit

Permalink
linter: Remove dead linter options: include and exclude
Browse files Browse the repository at this point in the history
The options validator reports if you ever use `include` or `exclude` as a key
under `linter`, so these are dead and unused options.

Change-Id: I5883e05753f3c6e3f4370c070343d8f31655fd5b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390808
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
  • Loading branch information
srawlins authored and Commit Queue committed Oct 18, 2024
1 parent 2ac5765 commit 4232c45
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 80 deletions.
41 changes: 7 additions & 34 deletions pkg/analyzer/lib/src/lint/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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 'package:analyzer/src/task/options.dart';
import 'package:analyzer/src/util/yaml.dart';
import 'package:yaml/yaml.dart';

Expand Down Expand Up @@ -31,13 +32,9 @@ LintConfig? processAnalysisOptionsFile(String fileContents, {String? fileUrl}) {

/// The configuration of lint rules within an analysis options file.
class LintConfig {
final List<String> fileIncludes;

final List<String> fileExcludes;

final List<RuleConfig> ruleConfigs;

LintConfig(this.fileIncludes, this.fileExcludes, this.ruleConfigs);
LintConfig(this.ruleConfigs);

factory LintConfig.parse(String source, {String? sourceUrl}) {
var yaml = loadYamlNode(source,
Expand All @@ -51,37 +48,13 @@ class LintConfig {
}

factory LintConfig.parseMap(YamlMap yaml) {
var fileIncludes = <String>[];
var fileExcludes = <String>[];
var ruleConfigs = <RuleConfig>[];

yaml.nodes.forEach((key, value) {
if (key is! YamlScalar) {
return;
}
switch (key.toString()) {
case 'files':
if (value is YamlMap) {
_addAsListOrString(value['include'], fileIncludes);
_addAsListOrString(value['exclude'], fileExcludes);
}

case 'rules':
ruleConfigs.addAll(_ruleConfigs(value));
}
});

return LintConfig(fileIncludes, fileExcludes, ruleConfigs);
}

static void _addAsListOrString(Object? value, List<String> list) {
if (value is List) {
for (var entry in value) {
list.add(entry as String);
}
} else if (value is String) {
list.add(value);
var rulesNode = yaml.valueAt(AnalyzerOptions.rules);
if (rulesNode != null) {
ruleConfigs.addAll(_ruleConfigs(rulesNode));
}

return LintConfig(ruleConfigs);
}

static bool? _asBool(YamlNode scalar) {
Expand Down
9 changes: 8 additions & 1 deletion pkg/analyzer/lib/src/task/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ class AnalyzerOptions {
// Formatter options.
static const String pageWidth = 'page_width';

// Linter options.
static const String rules = 'rules';

static const String propagateLinterExceptions = 'propagate-linter-exceptions';

/// Ways to say `true` or `false`.
Expand Down Expand Up @@ -297,6 +300,10 @@ class AnalyzerOptions {
strictRawTypes,
];

static const List<String> linterOptions = [
rules,
];

/// Supported 'analyzer' optional checks options.
static const List<String> optionalChecksOptions = [
chromeOsManifestChecks,
Expand Down Expand Up @@ -749,7 +756,7 @@ class LanguageOptionValidator extends OptionsValidator {
/// Validates `linter` top-level options.
// TODO(pq): move into `linter` package and plugin.
class LinterOptionsValidator extends TopLevelOptionValidator {
LinterOptionsValidator() : super('linter', const ['rules']);
LinterOptionsValidator() : super('linter', AnalyzerOptions.linterOptions);
}

/// Validates `analyzer` optional-checks value configuration options.
Expand Down
9 changes: 0 additions & 9 deletions pkg/analyzer/test/src/lint/config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ rules:
var config = LintConfig.parse(src);

group('lint config', () {
group('file', () {
test('includes', () {
expect(config.fileIncludes, unorderedEquals(['foo']));
});
test('excludes', () {
expect(
config.fileExcludes, unorderedEquals(['test/**', '**/_data.dart']));
});
});
group('rule', () {
test('configs', () {
expect(config.ruleConfigs, hasLength(3));
Expand Down
1 change: 0 additions & 1 deletion pkg/linter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ dev_dependencies:
analyzer_utilities: any
args: any
cli_util: any
glob: any
http: any
lints: any
test: any
Expand Down
48 changes: 13 additions & 35 deletions pkg/linter/tool/benchmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import 'dart:async';
import 'dart:io';
import 'dart:math' as math;

import 'package:analyzer/error/error.dart';
import 'package:analyzer/src/lint/config.dart';
import 'package:analyzer/src/lint/io.dart';
import 'package:analyzer/src/lint/registry.dart';
import 'package:args/args.dart';
import 'package:glob/glob.dart';
import 'package:linter/src/analyzer.dart';
import 'package:linter/src/extensions.dart';
import 'package:linter/src/rules.dart';
Expand All @@ -28,7 +26,7 @@ Future<void> main(List<String> args) async {
const unableToProcessExitCode = 64;

Future<Iterable<AnalysisErrorInfo>> lintFiles(
TestLinter linter, List<File> filesToLint, LintFilter filter) async {
TestLinter linter, List<File> filesToLint) async {
// Setup an error watcher to track whether an error was logged to stderr so
// we can set the exit code accordingly.
var errorWatcher = _ErrorWatchingSink(errorSink);
Expand All @@ -37,7 +35,7 @@ Future<Iterable<AnalysisErrorInfo>> lintFiles(
if (errorWatcher.encounteredError) {
exitCode = loggedAnalyzerErrorExitCode;
} else if (errors.isNotEmpty) {
exitCode = _maxSeverity(errors, filter);
exitCode = _maxSeverity(errors);
}

return errors;
Expand Down Expand Up @@ -94,12 +92,11 @@ Future<void> runLinter(List<String> args) async {
var ruleNames = options['rules'];

LinterOptions linterOptions;
LintFilter? filter;
if (configFile is String) {
var config = LintConfig.parse(readFile(configFile));
var enabledRules = Registry.ruleRegistry.where((LintRule rule) =>
!config.ruleConfigs.any((rc) => rc.disables(rule.name)));
filter = _FileGlobFilter(config.fileIncludes, config.fileExcludes);

linterOptions = LinterOptions(enabledRules: enabledRules);
} else if (ruleNames is Iterable<String> && ruleNames.isNotEmpty) {
var rules = <LintRule>[];
Expand All @@ -115,7 +112,6 @@ Future<void> runLinter(List<String> args) async {
} else {
linterOptions = LinterOptions();
}
filter ??= _FileGlobFilter([], []);

var customSdk = options['dart-sdk'];
if (customSdk is String) {
Expand All @@ -131,14 +127,18 @@ Future<void> runLinter(List<String> args) async {
.map(File.new),
];

await writeBenchmarks(outSink, filesToLint, linterOptions, filter);
await writeBenchmarks(
outSink,
filesToLint,
linterOptions,
);
}

Future<void> writeBenchmarks(StringSink out, List<File> filesToLint,
LinterOptions linterOptions, LintFilter filter) async {
Future<void> writeBenchmarks(
StringSink out, List<File> filesToLint, LinterOptions linterOptions) async {
var timings = <String, int>{};
for (var i = 0; i < benchmarkRuns; ++i) {
await lintFiles(TestLinter(linterOptions), filesToLint, filter);
await lintFiles(TestLinter(linterOptions), filesToLint);
lintRuleTimers.timers.forEach((n, t) {
var timing = t.elapsedMilliseconds;
var previous = timings[n];
Expand Down Expand Up @@ -168,19 +168,12 @@ Future<void> writeBenchmarks(StringSink out, List<File> filesToLint,
out.writeTimings(stats, 0);
}

int _maxSeverity(List<AnalysisErrorInfo> infos, LintFilter filter) {
var filteredErrors = infos
.expand((i) => i.errors)
.where((AnalysisError e) => !filter.filter(e));
int _maxSeverity(List<AnalysisErrorInfo> infos) {
var filteredErrors = infos.expand((i) => i.errors);
return filteredErrors.fold(
0, (value, e) => math.max(value, e.errorCode.errorSeverity.ordinal));
}

/// Filtered lints are omitted from linter output.
abstract class LintFilter {
bool filter(AnalysisError lint);
}

class _ErrorWatchingSink implements StringSink {
bool encounteredError = false;

Expand All @@ -207,18 +200,3 @@ class _ErrorWatchingSink implements StringSink {
delegate.writeln(obj);
}
}

class _FileGlobFilter extends LintFilter {
final List<Glob> includes;
final List<Glob> excludes;

_FileGlobFilter(Iterable<String> includeGlobs, Iterable<String> excludeGlobs)
: includes = includeGlobs.map(Glob.new).toList(),
excludes = excludeGlobs.map(Glob.new).toList();

@override
bool filter(AnalysisError lint) =>
// TODO(srawlins): specify order.
excludes.any((glob) => glob.matches(lint.source.fullName)) &&
!includes.any((glob) => glob.matches(lint.source.fullName));
}

0 comments on commit 4232c45

Please sign in to comment.