Skip to content

Commit

Permalink
refactor: add 'docs' suggestion (#26463)
Browse files Browse the repository at this point in the history
Adds another kind to `FixSuggestionKind` specifically for links
documentation pages.
  • Loading branch information
bartlomieju authored Oct 22, 2024
1 parent 8282c38 commit 28b5640
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
51 changes: 35 additions & 16 deletions runtime/fmt_errors.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
//! This mod provides DenoError to unify errors across Deno.
use color_print::cformat;
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 +23,7 @@ struct IndexedErrorReference<'a> {
enum FixSuggestionKind {
Info,
Hint,
Docs,
}

#[derive(Debug)]
Expand Down Expand Up @@ -67,6 +66,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 +85,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 +121,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 +134,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 +207,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 +246,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 +264,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, "{}", cformat!("<u>{}</>", msg)).unwrap();
} else {
write!(s, "{}", msg).unwrap();
}
}
FixSuggestionMessage::Multiline(messages) => {
for (idx, message) in messages.iter().enumerate() {
Expand Down Expand Up @@ -300,7 +319,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
2 changes: 1 addition & 1 deletion tests/specs/run/import_common_js/exports_error.out
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
or change the file extension to .cjs,
or add package.json next to the file with "type": "commonjs" option
and pass --unstable-detect-cjs flag.
hint: See https://docs.deno.com/go/commonjs for details
docs: https://docs.deno.com/go/commonjs
2 changes: 1 addition & 1 deletion tests/specs/run/import_common_js/module_error.out
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ module.exports = {
or change the file extension to .cjs,
or add package.json next to the file with "type": "commonjs" option
and pass --unstable-detect-cjs flag.
hint: See https://docs.deno.com/go/commonjs for details
docs: https://docs.deno.com/go/commonjs
2 changes: 1 addition & 1 deletion tests/specs/run/import_common_js/require_error.out
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ const process = require("process");
or change the file extension to .cjs,
or add package.json next to the file with "type": "commonjs" option
and pass --unstable-detect-cjs flag.
hint: See https://docs.deno.com/go/commonjs for details
docs: https://docs.deno.com/go/commonjs
2 changes: 1 addition & 1 deletion tests/specs/run/package_json_type/commonjs/main_mix.out
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ console.log(require("./add").add(1, 2));
or change the file extension to .cjs,
or add package.json next to the file with "type": "commonjs" option
and pass --unstable-detect-cjs flag.
hint: See https://docs.deno.com/go/commonjs for details
docs: https://docs.deno.com/go/commonjs
2 changes: 1 addition & 1 deletion tests/specs/run/package_json_type/none/main_cjs.out
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ const { add } = require("./add");
or change the file extension to .cjs,
or add package.json next to the file with "type": "commonjs" option
and pass --unstable-detect-cjs flag.
hint: See https://docs.deno.com/go/commonjs for details
docs: https://docs.deno.com/go/commonjs

0 comments on commit 28b5640

Please sign in to comment.