Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add 'docs' suggestion #26463

Merged
merged 6 commits into from
Oct 22, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: add 'docs' suggestion
  • Loading branch information
bartlomieju committed Oct 22, 2024
commit 1090de9f9f89604a3be397344cc0e8fc8c3cbff1
50 changes: 34 additions & 16 deletions runtime/fmt_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
use color_print::cstr;
use deno_core::error::format_frame;
use deno_core::error::JsError;
use deno_terminal::colors::cyan;
use deno_terminal::colors::italic_bold;
use deno_terminal::colors::red;
use deno_terminal::colors::yellow;
use deno_terminal::colors;
use std::fmt::Write as _;

#[derive(Debug, Clone)]
Expand All @@ -25,6 +22,7 @@ struct IndexedErrorReference<'a> {
enum FixSuggestionKind {
Info,
Hint,
Docs,
}

#[derive(Debug)]
Expand Down Expand Up @@ -67,6 +65,13 @@ impl<'a> FixSuggestion<'a> {
message: FixSuggestionMessage::Multiline(messages),
}
}

pub fn docs(url: &'a str) -> Self {
Self {
kind: FixSuggestionKind::Docs,
message: FixSuggestionMessage::Single(url),
}
}
}

struct AnsiColors;
Expand All @@ -79,10 +84,10 @@ impl deno_core::error::ErrorFormat for AnsiColors {
use deno_core::error::ErrorElement::*;
match element {
Anonymous | NativeFrame | FileName | EvalOrigin => {
cyan(s).to_string().into()
colors::cyan(s).to_string().into()
}
LineNumber | ColumnNumber => yellow(s).to_string().into(),
FunctionName | PromiseAll => italic_bold(s).to_string().into(),
LineNumber | ColumnNumber => colors::yellow(s).to_string().into(),
FunctionName | PromiseAll => colors::italic_bold(s).to_string().into(),
}
}
}
Expand Down Expand Up @@ -115,7 +120,7 @@ fn format_maybe_source_line(
if column_number as usize > source_line.len() {
return format!(
"\n{} Couldn't format source line: Column {} is out of bounds (source may have changed at runtime)",
yellow("Warning"), column_number,
colors::yellow("Warning"), column_number,
);
}

Expand All @@ -128,9 +133,9 @@ fn format_maybe_source_line(
}
s.push('^');
let color_underline = if is_error {
red(&s).to_string()
colors::red(&s).to_string()
} else {
cyan(&s).to_string()
colors::cyan(&s).to_string()
};

let indent = format!("{:indent$}", "", indent = level);
Expand Down Expand Up @@ -201,7 +206,8 @@ fn format_js_error_inner(

if let Some(circular) = &circular {
if js_error.is_same_error(circular.reference.to) {
write!(s, " {}", cyan(format!("<ref *{}>", circular.index))).unwrap();
write!(s, " {}", colors::cyan(format!("<ref *{}>", circular.index)))
.unwrap();
}
}

Expand Down Expand Up @@ -239,7 +245,8 @@ fn format_js_error_inner(
.unwrap_or(false);

let error_string = if is_caused_by_circular {
cyan(format!("[Circular *{}]", circular.unwrap().index)).to_string()
colors::cyan(format!("[Circular *{}]", circular.unwrap().index))
.to_string()
} else {
format_js_error_inner(cause, circular, false, vec![])
};
Expand All @@ -256,12 +263,23 @@ fn format_js_error_inner(
for (index, suggestion) in suggestions.iter().enumerate() {
write!(s, " ").unwrap();
match suggestion.kind {
FixSuggestionKind::Hint => write!(s, "{} ", cyan("hint:")).unwrap(),
FixSuggestionKind::Info => write!(s, "{} ", yellow("info:")).unwrap(),
FixSuggestionKind::Hint => {
write!(s, "{} ", colors::cyan("hint:")).unwrap()
}
FixSuggestionKind::Info => {
write!(s, "{} ", colors::yellow("info:")).unwrap()
}
FixSuggestionKind::Docs => {
write!(s, "{} ", colors::green("docs:")).unwrap()
}
};
match suggestion.message {
FixSuggestionMessage::Single(msg) => {
write!(s, "{}", msg).unwrap();
if matches!(suggestion.kind, FixSuggestionKind::Docs) {
write!(s, "{}", colors::underline(msg)).unwrap();
} else {
write!(s, "{}", msg).unwrap();
}
}
FixSuggestionMessage::Multiline(messages) => {
for (idx, message) in messages.iter().enumerate() {
Expand Down Expand Up @@ -300,7 +318,7 @@ fn get_suggestions_for_terminal_errors(e: &JsError) -> Vec<FixSuggestion> {
cstr!("or add <u>package.json</> next to the file with <i>\"type\": \"commonjs\"</> option"),
cstr!("and pass <i>--unstable-detect-cjs</> flag."),
]),
FixSuggestion::hint("See https://docs.deno.com/go/commonjs for details"),
FixSuggestion::docs("https://docs.deno.com/go/commonjs"),
];
} else if msg.contains("openKv is not a function") {
return vec![
Expand Down
Loading