Skip to content

Commit e98a2af

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Use DiagnosticSeverity in _fe_analyzer_shared and analyzer_cli
Work towards #60635 DiagnosticSeverity is the new name for ErrorSeverity. Change-Id: I23a0e3d487934cea2f44a673215bc22faf34ee52 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426000 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 2b1cf0b commit e98a2af

File tree

10 files changed

+59
-65
lines changed

10 files changed

+59
-65
lines changed

pkg/_fe_analyzer_shared/lib/src/base/errors.dart

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ abstract class ErrorCode {
7979
/**
8080
* The severity of the error.
8181
*/
82-
ErrorSeverity get errorSeverity;
82+
DiagnosticSeverity get errorSeverity;
8383

8484
/// Whether a finding of this error is ignorable via comments such as
8585
/// `// ignore:` or `// ignore_for_file:`.
86-
bool get isIgnorable => errorSeverity != ErrorSeverity.ERROR;
86+
bool get isIgnorable => errorSeverity != DiagnosticSeverity.ERROR;
8787

8888
/**
8989
* The template used to create the problem message to be displayed for this
@@ -165,14 +165,8 @@ class ErrorSeverity implements Comparable<ErrorSeverity> {
165165

166166
static const List<ErrorSeverity> values = const [NONE, INFO, WARNING, ERROR];
167167

168-
/**
169-
* The name of this error code.
170-
*/
171168
final String name;
172169

173-
/**
174-
* The ordinal value of the error code.
175-
*/
176170
final int ordinal;
177171

178172
/**
@@ -185,9 +179,6 @@ class ErrorSeverity implements Comparable<ErrorSeverity> {
185179
*/
186180
final String displayName;
187181

188-
/**
189-
* Initialize a newly created severity with the given names.
190-
*/
191182
const ErrorSeverity(
192183
this.name, this.ordinal, this.machineCode, this.displayName);
193184

@@ -220,52 +211,52 @@ class DiagnosticType implements Comparable<DiagnosticType> {
220211
* Task (todo) comments in user code.
221212
*/
222213
static const DiagnosticType TODO =
223-
const DiagnosticType('TODO', 0, ErrorSeverity.INFO);
214+
const DiagnosticType('TODO', 0, DiagnosticSeverity.INFO);
224215

225216
/**
226217
* Extra analysis run over the code to follow best practices, which are not in
227218
* the Dart Language Specification.
228219
*/
229220
static const DiagnosticType HINT =
230-
const DiagnosticType('HINT', 1, ErrorSeverity.INFO);
221+
const DiagnosticType('HINT', 1, DiagnosticSeverity.INFO);
231222

232223
/**
233224
* Compile-time errors are errors that preclude execution. A compile time
234225
* error must be reported by a Dart compiler before the erroneous code is
235226
* executed.
236227
*/
237228
static const DiagnosticType COMPILE_TIME_ERROR =
238-
const DiagnosticType('COMPILE_TIME_ERROR', 2, ErrorSeverity.ERROR);
229+
const DiagnosticType('COMPILE_TIME_ERROR', 2, DiagnosticSeverity.ERROR);
239230

240231
/**
241232
* Checked mode compile-time errors are errors that preclude execution in
242233
* checked mode.
243234
*/
244235
static const DiagnosticType CHECKED_MODE_COMPILE_TIME_ERROR =
245236
const DiagnosticType(
246-
'CHECKED_MODE_COMPILE_TIME_ERROR', 3, ErrorSeverity.ERROR);
237+
'CHECKED_MODE_COMPILE_TIME_ERROR', 3, DiagnosticSeverity.ERROR);
247238

248239
/**
249240
* Static warnings are those warnings reported by the static checker. They
250241
* have no effect on execution. Static warnings must be provided by Dart
251242
* compilers used during development.
252243
*/
253244
static const DiagnosticType STATIC_WARNING =
254-
const DiagnosticType('STATIC_WARNING', 4, ErrorSeverity.WARNING);
245+
const DiagnosticType('STATIC_WARNING', 4, DiagnosticSeverity.WARNING);
255246

256247
/**
257248
* Syntactic errors are errors produced as a result of input that does not
258249
* conform to the grammar.
259250
*/
260251
static const DiagnosticType SYNTACTIC_ERROR =
261-
const DiagnosticType('SYNTACTIC_ERROR', 6, ErrorSeverity.ERROR);
252+
const DiagnosticType('SYNTACTIC_ERROR', 6, DiagnosticSeverity.ERROR);
262253

263254
/**
264255
* Lint warnings describe style and best practice recommendations that can be
265256
* used to formalize a project's style guidelines.
266257
*/
267258
static const DiagnosticType LINT =
268-
const DiagnosticType('LINT', 7, ErrorSeverity.INFO);
259+
const DiagnosticType('LINT', 7, DiagnosticSeverity.INFO);
269260

270261
static const List<DiagnosticType> values = const [
271262
TODO,
@@ -290,7 +281,7 @@ class DiagnosticType implements Comparable<DiagnosticType> {
290281
/**
291282
* The severity of this type of error.
292283
*/
293-
final ErrorSeverity severity;
284+
final DiagnosticSeverity severity;
294285

295286
/**
296287
* Initialize a newly created error type to have the given [name] and

pkg/_fe_analyzer_shared/lib/src/scanner/errors.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ class ScannerErrorCode extends ErrorCode {
202202
);
203203

204204
@override
205-
ErrorSeverity get errorSeverity => ErrorSeverity.ERROR;
205+
DiagnosticSeverity get errorSeverity => DiagnosticSeverity.ERROR;
206206

207207
@override
208208
DiagnosticType get type => DiagnosticType.SYNTACTIC_ERROR;

pkg/analyzer_cli/lib/src/analyzer_impl.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,24 @@ class AnalyzerImpl {
9999
}
100100
}
101101

102-
/// Treats the [sourcePath] as the top level library and analyzes it using
103-
/// the analysis engine. If [printMode] is `0`, then no error or performance
104-
/// information is printed. If [printMode] is `1`, then errors will be printed.
105-
/// If [printMode] is `2`, then performance information will be printed, and
106-
/// it will be marked as being for a cold VM.
107-
Future<ErrorSeverity> analyze(
102+
/// Treats the [libraryFile] as the top level library and analyzes it using
103+
/// the analysis engine.
104+
///
105+
/// If [printMode] is `0`, then no diagnostic or performance information is
106+
/// printed. If [printMode] is `1`, then diagnostics will be printed. If
107+
/// [printMode] is `2`, then performance information will be printed, and it
108+
/// will be marked as being for a cold VM.
109+
Future<DiagnosticSeverity> analyze(
108110
ErrorFormatter formatter, {
109111
int printMode = 1,
110112
}) async {
111113
setupForAnalysis();
112114
return await _analyze(printMode, formatter);
113115
}
114116

115-
/// Returns the maximal [ErrorSeverity] of the recorded errors.
116-
ErrorSeverity computeMaxErrorSeverity() {
117-
var status = ErrorSeverity.NONE;
117+
/// Returns the maximal [DiagnosticSeverity] of the recorded diagnostics.
118+
DiagnosticSeverity computeMaxErrorSeverity() {
119+
var status = DiagnosticSeverity.NONE;
118120
for (var result in errorsResults) {
119121
for (var error in result.errors) {
120122
if (_defaultSeverityProcessor(error) == null) {
@@ -153,7 +155,7 @@ class AnalyzerImpl {
153155
}
154156
}
155157

156-
Future<ErrorSeverity> _analyze(
158+
Future<DiagnosticSeverity> _analyze(
157159
int printMode,
158160
ErrorFormatter formatter,
159161
) async {
@@ -162,7 +164,7 @@ class AnalyzerImpl {
162164
var libraryPath = libraryFile.path;
163165
stderr.writeln('Only libraries can be analyzed.');
164166
stderr.writeln('$libraryPath is a part and cannot be analyzed.');
165-
return ErrorSeverity.ERROR;
167+
return DiagnosticSeverity.ERROR;
166168
}
167169

168170
var libraryElement = await _resolveLibrary();
@@ -180,7 +182,7 @@ class AnalyzerImpl {
180182
return computeMaxErrorSeverity();
181183
}
182184

183-
ErrorSeverity? _defaultSeverityProcessor(AnalysisError error) =>
185+
DiagnosticSeverity? _defaultSeverityProcessor(AnalysisError error) =>
184186
determineProcessedSeverity(error, options, analysisOptions);
185187

186188
/// Returns true if we want to report diagnostics for this library.

pkg/analyzer_cli/lib/src/batch_mode.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import 'dart:io' show exitCode, stdin;
77

88
import 'package:analyzer/error/error.dart';
99

10-
typedef BatchRunnerHandler = Future<ErrorSeverity> Function(List<String> args);
10+
typedef BatchRunnerHandler =
11+
Future<DiagnosticSeverity> Function(List<String> args);
1112

1213
/// Provides a framework to read command line options from stdin and feed them
1314
/// to a callback.
@@ -26,7 +27,7 @@ class BatchRunner {
2627
stopwatch.start();
2728
var testsFailed = 0;
2829
var totalTests = 0;
29-
var batchResult = ErrorSeverity.NONE;
30+
var batchResult = DiagnosticSeverity.NONE;
3031
// Read line from stdin.
3132
var cmdLine = stdin.transform(utf8.decoder).transform(LineSplitter());
3233
cmdLine.listen((String line) async {
@@ -49,7 +50,7 @@ class BatchRunner {
4950
try {
5051
totalTests++;
5152
var result = await handler(args);
52-
var resultPass = result != ErrorSeverity.ERROR;
53+
var resultPass = result != DiagnosticSeverity.ERROR;
5354
if (!resultPass) {
5455
testsFailed++;
5556
}

pkg/analyzer_cli/lib/src/driver.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class Driver implements CommandLineStarter {
111111
} else {
112112
var severity = await _analyzeAll(options);
113113
// Propagate issues to the exit code.
114-
if (_shouldBeFatal(severity, options)) {
114+
if (_shouldBeFatal(severity)) {
115115
io.exitCode = severity.ordinal;
116116
}
117117
}
@@ -154,7 +154,7 @@ class Driver implements CommandLineStarter {
154154
}
155155

156156
/// Perform analysis according to the given [options].
157-
Future<ErrorSeverity> _analyzeAll(CommandLineOptions options) async {
157+
Future<DiagnosticSeverity> _analyzeAll(CommandLineOptions options) async {
158158
if (!options.jsonFormat && !options.machineFormat) {
159159
var fileNames =
160160
options.sourceFiles.map((String file) {
@@ -222,13 +222,13 @@ class Driver implements CommandLineStarter {
222222
);
223223
}
224224

225-
var allResult = ErrorSeverity.NONE;
225+
var allResult = DiagnosticSeverity.NONE;
226226

227227
void reportPartError(String partPath) {
228228
errorSink.writeln('$partPath is a part and cannot be analyzed.');
229229
errorSink.writeln('Please pass in a library that contains this part.');
230-
io.exitCode = ErrorSeverity.ERROR.ordinal;
231-
allResult = allResult.max(ErrorSeverity.ERROR);
230+
io.exitCode = DiagnosticSeverity.ERROR.ordinal;
231+
allResult = allResult.max(DiagnosticSeverity.ERROR);
232232
}
233233

234234
var pathList = options.sourceFiles.map(normalizePath).toList();
@@ -253,8 +253,8 @@ class Driver implements CommandLineStarter {
253253
var files = _collectFiles(sourcePath);
254254
if (files.isEmpty) {
255255
errorSink.writeln('No dart files found at: $sourcePath');
256-
io.exitCode = ErrorSeverity.ERROR.ordinal;
257-
return ErrorSeverity.ERROR;
256+
io.exitCode = DiagnosticSeverity.ERROR.ordinal;
257+
return DiagnosticSeverity.ERROR;
258258
}
259259

260260
for (var file in files) {
@@ -461,7 +461,7 @@ class Driver implements CommandLineStarter {
461461
path.split(relative).any((part) => part.startsWith('.'));
462462

463463
/// Analyze a single source.
464-
Future<ErrorSeverity> _runAnalyzer(
464+
Future<DiagnosticSeverity> _runAnalyzer(
465465
FileState file,
466466
CommandLineOptions options,
467467
ErrorFormatter formatter,
@@ -482,16 +482,16 @@ class Driver implements CommandLineStarter {
482482
return analyzer.analyze(formatter);
483483
}
484484

485-
bool _shouldBeFatal(ErrorSeverity severity, CommandLineOptions options) =>
486-
severity == ErrorSeverity.ERROR;
485+
bool _shouldBeFatal(DiagnosticSeverity severity) =>
486+
severity == DiagnosticSeverity.ERROR;
487487

488488
void _verifyAnalysisOptionsFileExists(CommandLineOptions options) {
489489
var path = options.defaultAnalysisOptionsPath;
490490
if (path != null) {
491491
if (!resourceProvider.getFile(path).exists) {
492492
printAndFail(
493493
'Options file not found: $path',
494-
exitCode: ErrorSeverity.ERROR.ordinal,
494+
exitCode: DiagnosticSeverity.ERROR.ordinal,
495495
);
496496
}
497497
}

pkg/analyzer_cli/lib/src/error_formatter.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ String _relative(String file) {
2929
}
3030

3131
/// Returns the given error's severity.
32-
ErrorSeverity _severityIdentity(AnalysisError error) =>
32+
DiagnosticSeverity _severityIdentity(AnalysisError error) =>
3333
error.errorCode.errorSeverity;
3434

3535
/// Returns desired severity for the given [error] (or `null` if it's to be
3636
/// suppressed).
37-
typedef SeverityProcessor = ErrorSeverity? Function(AnalysisError error);
37+
typedef SeverityProcessor = DiagnosticSeverity? Function(AnalysisError error);
3838

3939
/// Analysis statistics counter.
4040
class AnalysisStats {
@@ -220,7 +220,7 @@ abstract class ErrorFormatter {
220220

221221
/// Compute the severity for this [error] or `null` if this error should be
222222
/// filtered.
223-
ErrorSeverity? _computeSeverity(AnalysisError error) =>
223+
DiagnosticSeverity? _computeSeverity(AnalysisError error) =>
224224
_severityProcessor(error);
225225
}
226226

@@ -299,7 +299,7 @@ class HumanErrorFormatter extends ErrorFormatter {
299299

300300
// Get display name; translate INFOs into LINTS and HINTS.
301301
var errorType = severity.displayName;
302-
if (severity == ErrorSeverity.INFO) {
302+
if (severity == DiagnosticSeverity.INFO) {
303303
if (error.errorCode.type == DiagnosticType.HINT ||
304304
error.errorCode.type == DiagnosticType.LINT) {
305305
errorType = error.errorCode.type.displayName;
@@ -488,9 +488,9 @@ class MachineErrorFormatter extends ErrorFormatter {
488488

489489
var severity = _severityProcessor(error);
490490

491-
if (severity == ErrorSeverity.ERROR) {
491+
if (severity == DiagnosticSeverity.ERROR) {
492492
stats.errorCount++;
493-
} else if (severity == ErrorSeverity.WARNING) {
493+
} else if (severity == DiagnosticSeverity.WARNING) {
494494
stats.warnCount++;
495495
} else if (error.errorCode.type == DiagnosticType.HINT) {
496496
stats.hintCount++;

pkg/analyzer_cli/lib/src/error_severity.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import 'package:analyzer/src/generated/engine.dart';
88
import 'package:analyzer_cli/src/options.dart';
99

1010
/// Compute the severity of the error; however:
11-
/// - if [options.enableTypeChecks] is false, then de-escalate checked-mode
12-
/// compile time errors to a severity of [ErrorSeverity.INFO].
13-
/// - if [options.lintsAreFatal] is true, escalate lints to errors.
14-
ErrorSeverity? computeSeverity(
11+
/// - if `options.enableTypeChecks` is false, then de-escalate checked-mode
12+
/// compile time errors to a severity of [DiagnosticSeverity.INFO].
13+
/// - if `options.lintsAreFatal` is true, escalate lints to errors.
14+
DiagnosticSeverity? computeSeverity(
1515
AnalysisError error,
1616
CommandLineOptions commandLineOptions,
1717
AnalysisOptions analysisOptions,
@@ -27,15 +27,15 @@ ErrorSeverity? computeSeverity(
2727

2828
/// Check various configuration options to get a desired severity for this
2929
/// [error] (or `null` if it's to be suppressed).
30-
ErrorSeverity? determineProcessedSeverity(
30+
DiagnosticSeverity? determineProcessedSeverity(
3131
AnalysisError error,
3232
CommandLineOptions commandLineOptions,
3333
AnalysisOptions analysisOptions,
3434
) {
3535
var severity = computeSeverity(error, commandLineOptions, analysisOptions);
3636
// Skip TODOs categorically unless escalated to ERROR or HINT (#26215).
3737
if (error.errorCode.type == DiagnosticType.TODO &&
38-
severity == ErrorSeverity.INFO) {
38+
severity == DiagnosticSeverity.INFO) {
3939
return null;
4040
}
4141

pkg/analyzer_cli/test/driver_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ class OptionsTest extends BaseTest {
450450
);
451451
expect(
452452
processorFor(assignment_of_do_not_store).severity,
453-
ErrorSeverity.ERROR,
453+
DiagnosticSeverity.ERROR,
454454
);
455455
expect(
456456
bulletToDash(outSink),

pkg/analyzer_cli/test/mocks.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class MockErrorCode implements ErrorCode {
7272
DiagnosticType type;
7373

7474
@override
75-
ErrorSeverity errorSeverity;
75+
DiagnosticSeverity errorSeverity;
7676

7777
@override
7878
String name;

0 commit comments

Comments
 (0)