Skip to content

Commit 807ea4a

Browse files
feat: add config severities
1 parent fe22727 commit 807ea4a

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

packages/custom_lint_builder/lib/src/client.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,7 @@ class _ClientAnalyzerPlugin extends analyzer_plugin.ServerPlugin {
995995
allAnalysisErrors,
996996
lineInfo: resolver.lineInfo,
997997
options: analysisContext.getAnalysisOptionsForFile(file),
998+
configSeverities: configs.configs.errors,
998999
),
9991000
).toNotification(),
10001001
),

packages/custom_lint_builder/lib/src/custom_analyzer_converter.dart

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,35 @@ class CustomAnalyzerConverter {
7272
/// start column. If an analysis [options] is provided then the severities of
7373
/// the errors will be altered based on those options.
7474
List<plugin.AnalysisError> convertAnalysisErrors(
75-
List<analyzer.AnalysisError> errors,
76-
{analyzer.LineInfo? lineInfo,
77-
analyzer.AnalysisOptions? options}) {
75+
List<analyzer.AnalysisError> errors, {
76+
analyzer.LineInfo? lineInfo,
77+
analyzer.AnalysisOptions? options,
78+
Map<String, analyzer.ErrorSeverity>? configSeverities,
79+
}) {
7880
var serverErrors = <plugin.AnalysisError>[];
7981
for (var error in errors) {
8082
var processor = analyzer.ErrorProcessor.getProcessor(options, error);
83+
// Check if there's a severity override in the configs
84+
final configSeverity = configSeverities?[error.errorCode.name];
85+
8186
if (processor != null) {
8287
var severity = processor.severity;
8388
// Errors with null severity are filtered out.
8489
if (severity != null) {
85-
// Specified severities override.
86-
serverErrors.add(convertAnalysisError(error,
87-
lineInfo: lineInfo, severity: severity));
90+
// Config severities override processor severities
91+
serverErrors.add(convertAnalysisError(
92+
error,
93+
lineInfo: lineInfo,
94+
severity: configSeverity ?? severity,
95+
));
8896
}
8997
} else {
90-
serverErrors.add(convertAnalysisError(error, lineInfo: lineInfo));
98+
// If no processor, still check for config severities
99+
serverErrors.add(convertAnalysisError(
100+
error,
101+
lineInfo: lineInfo,
102+
severity: configSeverity,
103+
));
91104
}
92105
}
93106
return serverErrors;

packages/custom_lint_core/lib/src/configs.dart

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:analyzer/error/error.dart';
12
import 'package:analyzer/file_system/file_system.dart';
23
import 'package:collection/collection.dart';
34
import 'package:meta/meta.dart';
@@ -17,6 +18,7 @@ class CustomLintConfigs {
1718
required this.verbose,
1819
required this.debug,
1920
required this.rules,
21+
required this.errors,
2022
});
2123

2224
/// Decode a [CustomLintConfigs] from a file.
@@ -108,11 +110,33 @@ class CustomLintConfigs {
108110
}
109111
}
110112

113+
final errors = <String, ErrorSeverity>{...includedOptions.errors};
114+
115+
final errorsYaml = customLint['errors'] as Object?;
116+
if (errorsYaml is Map) {
117+
for (final entry in errorsYaml.entries) {
118+
final value = entry.value;
119+
if (entry.key case final String key?) {
120+
final severity = ErrorSeverity.values.firstWhereOrNull(
121+
(e) => e.displayName == value,
122+
);
123+
if (severity == null) {
124+
throw ArgumentError(
125+
'Provided error severity: $value specified for key: $key is not valid. '
126+
'Valid error severities are: ${ErrorSeverity.values.map((e) => e.displayName).join(', ')}',
127+
);
128+
}
129+
errors[key] = severity;
130+
}
131+
}
132+
}
133+
111134
return CustomLintConfigs(
112135
enableAllLintRules: enableAllLintRules,
113136
verbose: verbose,
114137
debug: debug,
115138
rules: UnmodifiableMapView(rules),
139+
errors: UnmodifiableMapView(errors),
116140
);
117141
}
118142

@@ -123,6 +147,7 @@ class CustomLintConfigs {
123147
verbose: false,
124148
debug: false,
125149
rules: {},
150+
errors: {},
126151
);
127152

128153
/// A field representing whether to enable/disable lint rules that are not
@@ -147,20 +172,26 @@ class CustomLintConfigs {
147172
/// Whether enable hot-reload and log the VM-service URI.
148173
final bool debug;
149174

175+
/// A map of lint rules to their severity. This is used to override the severity
176+
/// of a lint rule for a specific lint.
177+
final Map<String, ErrorSeverity> errors;
178+
150179
@override
151180
bool operator ==(Object other) =>
152181
other is CustomLintConfigs &&
153182
other.enableAllLintRules == enableAllLintRules &&
154183
other.verbose == verbose &&
155184
other.debug == debug &&
156-
const MapEquality<String, LintOptions>().equals(other.rules, rules);
185+
const MapEquality<String, LintOptions>().equals(other.rules, rules) &&
186+
const MapEquality<String, ErrorSeverity>().equals(other.errors, errors);
157187

158188
@override
159189
int get hashCode => Object.hash(
160190
enableAllLintRules,
161191
verbose,
162192
debug,
163193
const MapEquality<String, LintOptions>().hash(rules),
194+
const MapEquality<String, ErrorSeverity>().hash(errors),
164195
);
165196
}
166197

0 commit comments

Comments
 (0)