Skip to content

Commit 858aa9a

Browse files
committed
Ruby: add some links to diagnostic messages
1 parent 78a8023 commit 858aa9a

File tree

4 files changed

+75
-39
lines changed

4 files changed

+75
-39
lines changed

ruby/extractor/src/diagnostics.rs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ fn longest_backtick_sequence_length(text: &str) -> usize {
214214
}
215215
result
216216
}
217+
pub enum Arg<'a> {
218+
Code(&'a str),
219+
Link(&'a str, &'a str),
220+
None,
221+
}
222+
217223
impl DiagnosticMessage {
218224
pub fn full_error_message(&self) -> String {
219225
match &self.location {
@@ -236,26 +242,40 @@ impl DiagnosticMessage {
236242
self
237243
}
238244

239-
pub fn message(&mut self, text: &str, args: &[&str]) -> &mut Self {
245+
pub fn message(&mut self, text: &str, args: &[Arg]) -> &mut Self {
240246
let parts = text.split("{}");
241-
let args = args.iter().chain(std::iter::repeat(&""));
247+
let args = args.iter().chain(std::iter::repeat(&Arg::None));
242248
let mut plain = String::with_capacity(2 * text.len());
243249
let mut markdown = String::with_capacity(2 * text.len());
244250
for (p, a) in parts.zip(args) {
245251
plain.push_str(p);
246-
plain.push_str(a);
247252
markdown.push_str(p);
248-
if a.len() > 0 {
249-
let count = longest_backtick_sequence_length(a) + 1;
250-
markdown.push_str(&"`".repeat(count));
251-
if count > 1 {
252-
markdown.push_str(" ");
253+
match a {
254+
Arg::Code(t) => {
255+
plain.push_str(t);
256+
if t.len() > 0 {
257+
let count = longest_backtick_sequence_length(t) + 1;
258+
markdown.push_str(&"`".repeat(count));
259+
if count > 1 {
260+
markdown.push_str(" ");
261+
}
262+
markdown.push_str(t);
263+
if count > 1 {
264+
markdown.push_str(" ");
265+
}
266+
markdown.push_str(&"`".repeat(count));
267+
}
253268
}
254-
markdown.push_str(a);
255-
if count > 1 {
256-
markdown.push_str(" ");
269+
Arg::Link(text, url) => {
270+
plain.push_str(text);
271+
self.help_link(url);
272+
markdown.push_str("[");
273+
markdown.push_str(text);
274+
markdown.push_str("](");
275+
markdown.push_str(url);
276+
markdown.push_str(")");
257277
}
258-
markdown.push_str(&"`".repeat(count));
278+
Arg::None => {}
259279
}
260280
}
261281
self.text(&plain);
@@ -323,14 +343,14 @@ fn test_message() {
323343
let mut m = DiagnosticLoggers::new("foo")
324344
.logger()
325345
.new_entry("id", "name");
326-
m.message("hello: {}", &["hello"]);
346+
m.message("hello: {}", &[Arg::Code("hello")]);
327347
assert_eq!("hello: hello", m.plaintext_message);
328348
assert_eq!("hello: `hello`", m.markdown_message);
329349

330350
let mut m = DiagnosticLoggers::new("foo")
331351
.logger()
332352
.new_entry("id", "name");
333-
m.message("hello with backticks: {}", &["oh `hello`!"]);
353+
m.message("hello with backticks: {}", &[Arg::Code("oh `hello`!")]);
334354
assert_eq!("hello with backticks: oh `hello`!", m.plaintext_message);
335355
assert_eq!(
336356
"hello with backticks: `` oh `hello`! ``",

ruby/extractor/src/extractor.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ impl<'a> Visitor<'a> {
277277
fn record_parse_error_for_node(
278278
&mut self,
279279
message: &str,
280-
args: &[&str],
280+
args: &[diagnostics::Arg],
281281
node: Node,
282282
status_page: bool,
283283
) {
@@ -306,17 +306,17 @@ impl<'a> Visitor<'a> {
306306
fn enter_node(&mut self, node: Node) -> bool {
307307
if node.is_missing() {
308308
self.record_parse_error_for_node(
309-
"A parse error occurred (expected {} symbol). Check the syntax of the file. If the file is invalid, correct the error or exclude the file from analysis.",
310-
&[node.kind()],
309+
"A parse error occurred (expected {} symbol). Check the syntax of the file. If the file is invalid, correct the error or {} the file from analysis.",
310+
&[diagnostics::Arg::Code(node.kind()), diagnostics::Arg::Link("exclude", "https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/customizing-code-scanning")],
311311
node,
312312
true,
313313
);
314314
return false;
315315
}
316316
if node.is_error() {
317317
self.record_parse_error_for_node(
318-
"A parse error occurred. Check the syntax of the file. If the file is invalid, correct the error or exclude the file from analysis.",
319-
&[],
318+
"A parse error occurred. Check the syntax of the file. If the file is invalid, correct the error or {} the file from analysis.",
319+
&[diagnostics::Arg::Link("exclude", "https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/customizing-code-scanning")],
320320
node,
321321
true,
322322
);
@@ -407,7 +407,10 @@ impl<'a> Visitor<'a> {
407407
.new_entry("parse-error", "Parse error")
408408
.severity(diagnostics::Severity::Error)
409409
.location(self.path, start_line, start_column, end_line, end_column)
410-
.message("Unknown table type: {}", &[node.kind()]),
410+
.message(
411+
"Unknown table type: {}",
412+
&[diagnostics::Arg::Code(node.kind())],
413+
),
411414
);
412415

413416
valid = false;
@@ -458,10 +461,10 @@ impl<'a> Visitor<'a> {
458461
self.record_parse_error_for_node(
459462
"Type mismatch for field {}::{} with type {} != {}",
460463
&[
461-
node.kind(),
462-
child_node.field_name.unwrap_or("child"),
463-
&format!("{:?}", child_node.type_name),
464-
&format!("{:?}", field.type_info),
464+
diagnostics::Arg::Code(node.kind()),
465+
diagnostics::Arg::Code(child_node.field_name.unwrap_or("child")),
466+
diagnostics::Arg::Code(&format!("{:?}", child_node.type_name)),
467+
diagnostics::Arg::Code(&format!("{:?}", field.type_info)),
465468
],
466469
*node,
467470
false,
@@ -471,9 +474,9 @@ impl<'a> Visitor<'a> {
471474
self.record_parse_error_for_node(
472475
"Value for unknown field: {}::{} and type {}",
473476
&[
474-
node.kind(),
475-
&child_node.field_name.unwrap_or("child"),
476-
&format!("{:?}", child_node.type_name),
477+
diagnostics::Arg::Code(node.kind()),
478+
diagnostics::Arg::Code(&child_node.field_name.unwrap_or("child")),
479+
diagnostics::Arg::Code(&format!("{:?}", child_node.type_name)),
477480
],
478481
*node,
479482
false,
@@ -512,7 +515,10 @@ impl<'a> Visitor<'a> {
512515
if !*has_index && index > 0 {
513516
self.record_parse_error_for_node(
514517
"Too many values for field: {}::{}",
515-
&[node.kind(), table_name],
518+
&[
519+
diagnostics::Arg::Code(node.kind()),
520+
diagnostics::Arg::Code(table_name),
521+
],
516522
*node,
517523
false,
518524
);
@@ -629,8 +635,11 @@ fn location_for(visitor: &mut Visitor, n: Node) -> (usize, usize, usize, usize)
629635
.diagnostics_writer
630636
.new_entry("internal-error", "Internal error")
631637
.message(
632-
"Cannot correct end column value: end_byte index {} is not in range [1,{}]",
633-
&[&index.to_string(), &source.len().to_string()],
638+
"Cannot correct end column value: end_byte index {} is not in range [1,{}].",
639+
&[
640+
diagnostics::Arg::Code(&index.to_string()),
641+
diagnostics::Arg::Code(&source.len().to_string()),
642+
],
634643
)
635644
.severity(diagnostics::Severity::Error),
636645
);

ruby/extractor/src/main.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn main() -> std::io::Result<()> {
7474
main_thread_logger.write(
7575
main_thread_logger
7676
.new_entry("configuration-error", "Configuration error")
77-
.message("{}; defaulting to 1 thread.", &[&e])
77+
.message("{}; defaulting to 1 thread.", &[diagnostics::Arg::Code(&e)])
7878
.severity(diagnostics::Severity::Warning),
7979
);
8080
1
@@ -95,7 +95,7 @@ fn main() -> std::io::Result<()> {
9595
main_thread_logger.write(
9696
main_thread_logger
9797
.new_entry("configuration-error", "Configuration error")
98-
.message("{}; using gzip.", &[&e])
98+
.message("{}; using gzip.", &[diagnostics::Arg::Code(&e)])
9999
.severity(diagnostics::Severity::Warning),
100100
);
101101
trap::Compression::Gzip
@@ -203,11 +203,15 @@ fn main() -> std::io::Result<()> {
203203
)
204204
.file(&path.to_string_lossy())
205205
.message(
206-
"Could not decode the file contents as {}: {}. The contents of the file must match the character encoding specified in the {} directive.",
207-
&[&encoding_name, &msg, "encoding:"],
206+
"Could not decode the file contents as {}: {}. The contents of the file must match the character encoding specified in the {} {}.",
207+
&[
208+
diagnostics::Arg::Code(&encoding_name),
209+
diagnostics::Arg::Code(&msg),
210+
diagnostics::Arg::Code("encoding:"),
211+
diagnostics::Arg::Link("directive", "https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html#label-encoding+Directive")
212+
],
208213
)
209214
.status_page()
210-
.help_link("https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html#label-encoding+Directive")
211215
.severity(diagnostics::Severity::Warning),
212216
);
213217
}
@@ -219,11 +223,14 @@ fn main() -> std::io::Result<()> {
219223
.new_entry("unknown-character-encoding", "Unknown character encoding")
220224
.file(&path.to_string_lossy())
221225
.message(
222-
"Unknown character encoding {} in {} directive.",
223-
&[&encoding_name, "#encoding:"],
226+
"Unknown character encoding {} in {} {}.",
227+
&[
228+
diagnostics::Arg::Code(&encoding_name),
229+
diagnostics::Arg::Code("#encoding:"),
230+
diagnostics::Arg::Link("directive", "https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html#label-encoding+Directive")
231+
],
224232
)
225233
.status_page()
226-
.help_link("https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html#label-encoding+Directive")
227234
.severity(diagnostics::Severity::Warning),
228235
);
229236
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| src/not_ruby.rb:5:25:5:26 | A parse error occurred. Check the syntax of the file using the ruby -c command. If the file is invalid, correct the error or exclude the file from analysis. | Extraction failed in src/not_ruby.rb with error A parse error occurred. Check the syntax of the file using the ruby -c command. If the file is invalid, correct the error or exclude the file from analysis. | 2 |
1+
| src/not_ruby.rb:5:25:5:26 | A parse error occurred. Check the syntax of the file. If the file is invalid, correct the error or exclude the file from analysis. | Extraction failed in src/not_ruby.rb with error A parse error occurred. Check the syntax of the file. If the file is invalid, correct the error or exclude the file from analysis. | 2 |

0 commit comments

Comments
 (0)