-
Notifications
You must be signed in to change notification settings - Fork 38
/
messages.dart
131 lines (103 loc) · 3.61 KB
/
messages.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// 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:source_span/source_span.dart';
import 'preprocessor_options.dart';
enum MessageLevel { info, warning, severe }
// TODO(#159): Remove the global messages, use some object that tracks
// compilation state.
/// The global [Messages] for tracking info/warnings/messages.
late Messages messages;
// Color constants used for generating messages.
const _greenColor = '\u001b[32m';
const _redColor = '\u001b[31m';
const _magentaColor = '\u001b[35m';
const _noColor = '\u001b[0m';
/// Map between error levels and their display color.
const Map<MessageLevel, String> _errorColors = {
MessageLevel.severe: _redColor,
MessageLevel.warning: _magentaColor,
MessageLevel.info: _greenColor,
};
/// Map between error levels and their friendly name.
const Map<MessageLevel, String> _errorLabel = {
MessageLevel.severe: 'error',
MessageLevel.warning: 'warning',
MessageLevel.info: 'info',
};
/// A single message from the compiler.
class Message {
final MessageLevel level;
final String message;
final SourceSpan? span;
final bool useColors;
Message(this.level, this.message, {this.span, this.useColors = false});
String get describe {
var span = this.span;
if (span == null) {
return message;
}
var start = span.start;
return '${start.line + 1}:${start.column + 1}:$message';
}
@override
String toString() {
var output = StringBuffer();
var colors = useColors && _errorColors.containsKey(level);
var levelColor = colors ? _errorColors[level] : null;
if (colors) output.write(levelColor);
output
..write(_errorLabel[level])
..write(' ');
if (colors) output.write(_noColor);
if (span == null) {
output.write(message);
} else {
output.write('on ');
output.write(span!.message(message, color: levelColor));
}
return output.toString();
}
}
/// This class tracks and prints information, warnings, and errors emitted by
/// the compiler.
class Messages {
/// Called on every error. Set to blank function to suppress printing.
final void Function(Message obj) printHandler;
final PreprocessorOptions options;
final List<Message> messages = <Message>[];
Messages({PreprocessorOptions? options, this.printHandler = print})
: options = options ?? const PreprocessorOptions();
/// Report a compile-time CSS error.
void error(String message, SourceSpan? span) {
var msg = Message(MessageLevel.severe, message,
span: span, useColors: options.useColors);
messages.add(msg);
printHandler(msg);
}
/// Report a compile-time CSS warning.
void warning(String message, SourceSpan? span) {
if (options.warningsAsErrors) {
error(message, span);
} else {
var msg = Message(MessageLevel.warning, message,
span: span, useColors: options.useColors);
messages.add(msg);
}
}
/// Report and informational message about what the compiler is doing.
void info(String message, SourceSpan span) {
var msg = Message(MessageLevel.info, message,
span: span, useColors: options.useColors);
messages.add(msg);
if (options.verbose) printHandler(msg);
}
/// Merge [newMessages] to this message list.
void mergeMessages(Messages newMessages) {
messages.addAll(newMessages.messages);
newMessages.messages
.where((message) =>
message.level == MessageLevel.severe || options.verbose)
.forEach(printHandler);
}
}