Skip to content

Commit fa650e4

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Implement the verbose option for dart analyze
Change-Id: I2186a83ad295ad99a2df09a72f08c2b6f531e982 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/153321 Reviewed-by: Devon Carew <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent b3c9e89 commit fa650e4

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

pkg/dartdev/lib/src/commands/analyze.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,19 @@ class AnalyzeCommand extends DartdevCommand<int> {
103103
'(${error.code})',
104104
);
105105

106-
// TODO(devoncarew): If verbose, print additional information about the
107-
// issue (resolution, more info url, ...).
106+
if (verbose) {
107+
var padding = ' '.padLeft(error.severity.length + 2);
108+
for (var message in error.contextMessages) {
109+
log.stdout('$padding${message.message} '
110+
'at ${message.filePath}:${message.line}:${message.column}');
111+
}
112+
if (error.correction != null) {
113+
log.stdout('$padding${error.correction}');
114+
}
115+
if (error.url != null) {
116+
log.stdout('$padding${error.url}');
117+
}
118+
}
108119

109120
hasErrors |= error.isError;
110121
hasWarnings |= error.isWarning;

pkg/dartdev/lib/src/commands/analyze_impl.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ class AnalysisError implements Comparable<AnalysisError> {
203203

204204
String get code => json['code'] as String;
205205

206+
String get correction => json['correction'] as String;
207+
206208
String get file => json['location']['file'] as String;
207209

208210
int get startLine => json['location']['startLine'] as int;
@@ -213,6 +215,13 @@ class AnalysisError implements Comparable<AnalysisError> {
213215

214216
String get messageSentenceFragment => trimEnd(message, '.');
215217

218+
String get url => json['url'] as String;
219+
220+
List<DiagnosticMessage> get contextMessages {
221+
var messages = json['contextMessages'] as List<dynamic>;
222+
return messages.map((message) => DiagnosticMessage(message)).toList();
223+
}
224+
216225
// TODO(jwren) add some tests to verify that the results are what we are
217226
// expecting, 'other' is not always on the RHS of the subtraction in the
218227
// implementation.
@@ -241,6 +250,20 @@ class AnalysisError implements Comparable<AnalysisError> {
241250
'($code)';
242251
}
243252

253+
class DiagnosticMessage {
254+
final Map<String, dynamic> json;
255+
256+
DiagnosticMessage(this.json);
257+
258+
int get column => json['location']['startColumn'] as int;
259+
260+
String get filePath => json['location']['file'] as String;
261+
262+
int get line => json['location']['startLine'] as int;
263+
264+
String get message => json['message'] as String;
265+
}
266+
244267
class FileAnalysisErrors {
245268
final String file;
246269
final List<AnalysisError> errors;

pkg/dartdev/test/commands/analyze_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,25 @@ void defineAnalyze() {
157157
expect(result.stderr, isEmpty);
158158
expect(result.stdout, contains('1 issue found.'));
159159
});
160+
161+
test('--verbose', () {
162+
p = project(mainSrc: '''
163+
int f() {
164+
var result = one + 2;
165+
var one = 1;
166+
return result;
167+
}''');
168+
var result = p.runSync('analyze', ['--verbose', p.dirPath]);
169+
170+
expect(result.exitCode, 3);
171+
expect(result.stderr, isEmpty);
172+
var stdout = result.stdout;
173+
expect(stdout, contains("The declaration of 'one' is on line 3."));
174+
expect(
175+
stdout, contains('Try moving the declaration to before the first use'));
176+
expect(
177+
stdout,
178+
contains(
179+
'https://dart.dev/tools/diagnostic-messages#referenced_before_declaration'));
180+
});
160181
}

0 commit comments

Comments
 (0)