OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /// Provides a default implementation of the report and format methods of | |
6 /// [CompilerContext] that are suitable for command-line tools. The methods in | |
7 /// this library aren't intended to be called directly, instead, one should use | |
8 /// [CompilerContext]. | |
9 library fasta.command_line_reporting; | |
10 | |
11 import 'dart:io' show exitCode; | |
12 | |
13 import 'package:kernel/ast.dart' show Location; | |
14 | |
15 import 'colors.dart' show cyan, magenta, red; | |
16 | |
17 import 'compiler_context.dart' show CompilerContext; | |
18 | |
19 import 'deprecated_problems.dart' show deprecated_InputError; | |
20 | |
21 import 'fasta_codes.dart' show LocatedMessage, Message; | |
22 | |
23 import 'messages.dart' show getLocation, getSourceLine, isVerbose; | |
24 | |
25 import 'problems.dart' show unhandled; | |
26 | |
27 import 'severity.dart' show Severity; | |
28 | |
29 import 'util/relativize.dart' show relativizeUri; | |
30 | |
31 const bool hideWarnings = false; | |
32 | |
33 /// Formats [message] as a string that is suitable for output from a | |
34 /// command-line tool. This includes source snippets and different colors based | |
35 /// on [severity]. | |
36 /// | |
37 /// It is a assumed that a formatted message is reported to a user, so | |
38 /// [exitCode] is also set depending on the value of | |
39 /// `CompilerContext.current.options.setExitCodeOnProblem`. | |
40 /// | |
41 /// This is shared implementation used by methods below, and isn't intended to | |
42 /// be called directly. | |
43 String formatInternal(Message message, Severity severity, Uri uri, int offset) { | |
44 if (CompilerContext.current.options.setExitCodeOnProblem) { | |
45 exitCode = 1; | |
46 } | |
47 String text = | |
48 "${severityName(severity, capitalized: true)}: ${message.message}"; | |
49 if (message.tip != null) { | |
50 text += "\n${message.tip}"; | |
51 } | |
52 if (CompilerContext.enableColors) { | |
53 switch (severity) { | |
54 case Severity.error: | |
55 case Severity.internalProblem: | |
56 text = red(text); | |
57 break; | |
58 | |
59 case Severity.nit: | |
60 text = cyan(text); | |
61 break; | |
62 | |
63 case Severity.warning: | |
64 text = magenta(text); | |
65 break; | |
66 } | |
67 } | |
68 | |
69 if (uri != null) { | |
70 String path = relativizeUri(uri); | |
71 Location location = offset == -1 ? null : getLocation(path, offset); | |
72 String sourceLine = getSourceLine(location); | |
73 if (sourceLine == null) { | |
74 sourceLine = ""; | |
75 } else { | |
76 sourceLine = "\n$sourceLine\n" | |
77 "${' ' * (location.column - 1)}^"; | |
Siggi Cherem (dart-lang)
2017/07/13 21:05:56
BTW this is logic for which I'd like to use packag
ahe
2017/07/14 10:01:07
Done.
| |
78 } | |
79 String position = location?.toString() ?? path; | |
80 return "$position: $text$sourceLine"; | |
81 } else { | |
82 return text; | |
83 } | |
84 } | |
85 | |
86 /// Are problems of [severity] suppressed? | |
87 bool isHidden(Severity severity) { | |
88 switch (severity) { | |
89 case Severity.error: | |
90 case Severity.internalProblem: | |
91 return false; | |
92 | |
93 case Severity.nit: | |
94 return !isVerbose; | |
95 | |
96 case Severity.warning: | |
97 return hideWarnings; | |
98 } | |
99 return unhandled("$severity", "isHidden", -1, null); | |
100 } | |
101 | |
102 /// Are problems of [severity] fatal? That is, should the compiler terminate | |
103 /// immediately? | |
104 bool isFatal(Severity severity) { | |
105 switch (severity) { | |
106 case Severity.error: | |
107 return CompilerContext.current.options.errorsAreFatal; | |
108 | |
109 case Severity.internalProblem: | |
110 return true; | |
111 | |
112 case Severity.nit: | |
113 return CompilerContext.current.options.nitsAreFatal; | |
114 | |
115 case Severity.warning: | |
116 return CompilerContext.current.options.warningsAreFatal; | |
117 } | |
118 return unhandled("$severity", "isFatal", -1, null); | |
119 } | |
120 | |
121 /// Convert [severity] to a name that can be used to prefix a message. | |
122 String severityName(Severity severity, {bool capitalized: false}) { | |
123 switch (severity) { | |
124 case Severity.error: | |
125 return capitalized ? "Error" : "error"; | |
126 | |
127 case Severity.internalProblem: | |
128 return capitalized ? "Internal problem" : "internal problem"; | |
129 | |
130 case Severity.nit: | |
131 return capitalized ? "Nit" : "nit"; | |
132 | |
133 case Severity.warning: | |
134 return capitalized ? "Warning" : "warning"; | |
135 } | |
136 return unhandled("$severity", "severityName", -1, null); | |
137 } | |
138 | |
139 /// Report [message] unless [severity] is suppressed (see [isHidden]). Throws | |
140 /// an exception if [severity] is fatal (see [isFatal]). | |
141 /// | |
142 /// This method isn't intended to be called directly. Use | |
143 /// [CompilerContext.report] instead. | |
144 void report(LocatedMessage message, Severity severity) { | |
145 if (isHidden(severity)) return; | |
146 print(format(message, severity)); | |
147 if (isFatal(severity)) { | |
148 if (isVerbose) print(StackTrace.current); | |
149 throw new deprecated_InputError(message.uri, message.charOffset, | |
150 "Compilation aborted due to fatal ${severityName(severity)}."); | |
151 } | |
152 } | |
153 | |
154 /// Similar to [report]. | |
155 /// | |
156 /// This method isn't intended to be called directly. Use | |
157 /// [CompilerContext.reportWithoutLocation] instead. | |
158 void reportWithoutLocation(Message message, Severity severity) { | |
159 if (isHidden(severity)) return; | |
160 print(formatWithoutLocation(message, severity)); | |
161 if (isFatal(severity)) { | |
Siggi Cherem (dart-lang)
2017/07/13 21:05:56
considering adding a common shared function to avo
ahe
2017/07/14 10:01:07
Done.
| |
162 if (isVerbose) print(StackTrace.current); | |
163 throw new deprecated_InputError(null, -1, | |
164 "Compilation aborted due to fatal ${severityName(severity)}."); | |
165 } | |
166 } | |
167 | |
168 /// Formats [message] as described in [formatInternal]. | |
169 /// | |
170 /// This method isn't intended to be called directly. Use | |
171 /// [CompilerContext.format] instead. | |
172 String format(LocatedMessage message, Severity severity) { | |
173 return formatInternal( | |
174 message.messageObject, severity, message.uri, message.charOffset); | |
175 } | |
176 | |
177 /// Formats [message] as described in [formatInternal]. | |
178 /// | |
179 /// This method isn't intended to be called directly. Use | |
180 /// [CompilerContext.formatWithoutLocation] instead. | |
181 String formatWithoutLocation(Message message, Severity severity) { | |
182 return formatInternal(message, severity, null, -1); | |
183 } | |
OLD | NEW |