use std::{borrow::Cow, io::Write, string::FromUtf8Error}; use anstyle::{Ansi256Color, AnsiColor, Color, Style}; use serde::{Deserialize, Serialize}; use crate::{AssetType, Location, PathId, SourceLocation, SourceUrl}; #[derive(Serialize, Deserialize, Debug, PartialEq, Hash, Clone)] #[serde(rename_all = "camelCase")] pub struct Diagnostic { /// The message to log. pub message: String, /// Name of plugin or file that threw this error. pub origin: Option, #[serde(default)] pub code_frames: Vec, #[serde(default)] pub hints: Vec, #[serde(default)] pub severity: DiagnosticSeverity, #[serde(rename = "documentationURL")] pub documentation_url: Option, } #[derive(Serialize, Deserialize, Debug, PartialEq, Hash, Clone, Default)] #[serde(rename_all = "camelCase")] pub struct CodeFrame { pub code: Option, pub url: Option, pub language: Option, pub code_highlights: Vec, } #[derive(Serialize, Deserialize, Debug, PartialEq, Hash, Clone, Default)] pub struct CodeHighlight { pub message: Option, #[serde(default)] pub start: Location, #[serde(default)] pub end: Location, } #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Hash, Clone, Default)] #[serde(rename_all = "camelCase")] pub enum DiagnosticSeverity { /// Fails the build with an error. #[default] Error, /// Logs a warning, but the build does not fail. Warning, /// An error if this is source code in the project, or a warning if in node_modules. SourceError, /// An informative message. Info, } impl CodeFrame { pub fn from_loc(loc: &SourceLocation, message: Option) -> CodeFrame { CodeFrame { code: None, url: Some(loc.url.clone()), language: None, code_highlights: vec![CodeHighlight::from_loc(loc, message)], } } } impl CodeHighlight { pub fn from_loc(loc: &SourceLocation, message: Option) -> CodeHighlight { CodeHighlight { message, start: loc.start.clone(), end: Location { line: loc.end.line, column: loc.end.column.saturating_sub(1), }, } } pub fn from_json( start: json_sourcemap::Location, end: json_sourcemap::Location, message: Option<&str>, ) -> Self { CodeHighlight { message: message.map(|m| m.to_owned()), start: Location { line: start.line as u32 + 1, column: start.column as u32 + 1, }, end: Location { line: end.line as u32 + 1, column: end.column as u32, }, } } } impl std::fmt::Display for Diagnostic { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(&self.message) } } impl std::error::Error for Diagnostic {} impl Diagnostic { pub fn from_message(message: String) -> Self { Diagnostic { origin: None, message: message, code_frames: Vec::new(), hints: Vec::new(), severity: DiagnosticSeverity::Error, documentation_url: None, } } /// Points code frames that have neither a url nor code at `url`, for errors that /// know where in a file they occurred but not which file. pub fn with_url(mut self, url: &SourceUrl) -> Self { for frame in &mut self.code_frames { if frame.url.is_none() && frame.code.is_none() { frame.url = Some(url.clone()); } } self } } impl From for Diagnostic { fn from(value: std::io::Error) -> Self { Diagnostic { origin: Some("@parcel/core".into()), message: value.to_string(), code_frames: Vec::new(), hints: Vec::new(), severity: DiagnosticSeverity::Error, documentation_url: None, } } } impl From for Diagnostic { fn from(value: std::fmt::Error) -> Self { Diagnostic { origin: Some("@parcel/core".into()), message: value.to_string(), code_frames: Vec::new(), hints: Vec::new(), severity: DiagnosticSeverity::Error, documentation_url: None, } } } impl From for Diagnostic { fn from(value: json_sourcemap::Error) -> Self { Diagnostic { origin: Some("@parcel/core".into()), message: value.to_string(), code_frames: Vec::new(), hints: Vec::new(), severity: DiagnosticSeverity::Error, documentation_url: None, } } } impl From for Diagnostic { fn from(value: std::str::Utf8Error) -> Self { Diagnostic { origin: Some("@parcel/core".into()), message: value.to_string(), code_frames: Vec::new(), hints: Vec::new(), severity: DiagnosticSeverity::Error, documentation_url: None, } } } impl From for Diagnostic { fn from(value: serde_json::Error) -> Self { Diagnostic { origin: Some("@parcel/core".into()), message: value.to_string(), code_frames: Vec::new(), hints: Vec::new(), severity: DiagnosticSeverity::Error, documentation_url: None, } } } impl From for Diagnostic { fn from(value: json5::Error) -> Self { let mut message = value.to_string(); // The position goes in the code frame instead of the message. Errors at the end of // the input have none. The error doesn't know which file it came from, so the frame // has no url until `Diagnostic::with_url` gives it one. let mut code_highlights = Vec::new(); if let Some(position) = value.position() { if let Some(rest) = message.strip_suffix(&format!(" at {position}")) { message.truncate(rest.len()); } let location = Location { line: position.line as u32 + 1, column: position.column as u32 + 1, }; code_highlights.push(CodeHighlight { message: None, start: location.clone(), end: location, }); } Diagnostic { origin: Some("@parcel/core".into()), message, code_frames: vec![CodeFrame { code: None, url: None, language: Some(AssetType::Json), code_highlights, }], hints: Vec::new(), severity: DiagnosticSeverity::Error, documentation_url: None, } } } impl From for Diagnostic { fn from(value: FromUtf8Error) -> Self { Diagnostic::from_message(format!("UTF8 error: {}", value.to_string())) } } impl From for Vec { fn from(value: Diagnostic) -> Self { vec![value] } } #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] #[serde(transparent)] pub struct DiagnosticList(pub Vec); #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] pub struct RenderedDiagnostics { pub ansi: Vec, pub html: Vec, } #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] pub struct RenderedAnsiDiagnostic { pub message: String, pub codeframe: Option, pub stack: Option, pub hints: Vec, } #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] pub struct RenderedHtmlDiagnostic { pub message: String, pub stack: Option, pub frames: Vec, pub hints: Vec, pub documentation: Option, } #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] pub struct RenderedHtmlFrame { pub location: String, pub code: String, } impl> From for DiagnosticList { fn from(value: T) -> Self { DiagnosticList(vec![value.into()]) } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum JsonSourceLocationType { Key, Value, KeyAndValue, } /// Returns the source range for a JSON pointer. /// /// Locations returned by `json_sourcemap` are zero-based with an exclusive end. They can be /// converted to a diagnostic highlight with [`CodeHighlight::from_json`]. pub fn json_source_location( code: &str, pointer: &str, location_type: JsonSourceLocationType, ) -> Result, json_sourcemap::Error> { use json_sourcemap::Prop; let source_map = json_sourcemap::parse(code, json_sourcemap::Options::default())?; let Some(location) = source_map.get_location(pointer) else { return Ok(None); }; let range = match location_type { JsonSourceLocationType::Key => location.get(Prop::Key).zip(location.get(Prop::KeyEnd)), JsonSourceLocationType::Value => location.get(Prop::Value).zip(location.get(Prop::ValueEnd)), JsonSourceLocationType::KeyAndValue => location .get(Prop::Key) .or_else(|| location.get(Prop::Value)) .zip(location.get(Prop::ValueEnd)), }; Ok(range) } // pub(crate) struct EscapeMarkdown<'a, T>(pub &'a T); // fn escape_markdown(s: &str) -> Cow<'_, str> { // let mut result = Cow::Borrowed(""); // let mut start = 0; // for (index, matched) in s.match_indices(&['*', '_', '~', '\\']) { // result += &s[start..index]; // result += "\\"; // result += matched; // start = index + 1; // } // result += &s[start..]; // result // } // impl<'a, T: std::fmt::Debug> std::fmt::Debug for EscapeMarkdown<'a, T> { // fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // let res = format!("{:?}", self.0); // escape_markdown(&res).fmt(f) // } // } // impl<'a, T: std::fmt::Display> std::fmt::Display for EscapeMarkdown<'a, T> { // fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // let res = format!("{}", self.0); // escape_markdown(&res).fmt(f) // } // } // macro_rules! format_markdown { // ($s: literal, $($arg: expr),+) => { // format!($s, $(crate::diagnostic::EscapeMarkdown(&$arg)),+) // }; // } // pub(crate) use format_markdown; // pub(crate) struct EscapeJSONKeyComponent<'a, T>(pub &'a T); // fn escape_json_key_component(s: &str) -> Cow<'_, str> { // let mut result = Cow::Borrowed(""); // let mut start = 0; // for (index, matched) in s.match_indices(&['~', '/']) { // result += &s[start..index]; // result += match matched { // "~" => "~0", // "/" => "~1", // _ => unreachable!(), // }; // start = index + 1; // } // result += &s[start..]; // result // } // impl<'a, T: std::fmt::Debug> std::fmt::Debug for EscapeJSONKeyComponent<'a, T> { // fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // let res = format!("{:?}", self.0); // escape_json_key_component(&res).fmt(f) // } // } // impl<'a, T: std::fmt::Display> std::fmt::Display for EscapeJSONKeyComponent<'a, T> { // fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // let res = format!("{}", self.0); // escape_json_key_component(&res).fmt(f) // } // } // macro_rules! json_key { // ($s: literal, $($arg: expr),+) => { // format!($s, $(crate::diagnostic::EscapeJSONKeyComponent(&$arg)),+) // }; // } // pub(crate) use json_key; // impl DiagnosticList { pub fn report(&self, dest: &mut W) -> std::io::Result<()> { for diagnostic in &self.0 { writeln!(dest)?; diagnostic.report(dest)?; } Ok(()) } pub fn render_for_browser(&self) -> RenderedDiagnostics { RenderedDiagnostics { ansi: self.0.iter().map(Diagnostic::render_ansi).collect(), html: self.0.iter().map(Diagnostic::render_html).collect(), } } } impl Diagnostic { fn render_ansi(&self) -> RenderedAnsiDiagnostic { let mut codeframe = Vec::new(); for (index, frame) in self.code_frames.iter().enumerate() { if index > 0 { write!(&mut codeframe, "\n\n").unwrap(); } frame.report(&mut codeframe).unwrap(); } RenderedAnsiDiagnostic { message: self.message.clone(), codeframe: if codeframe.is_empty() { None } else { Some(String::from_utf8(codeframe).unwrap()) }, stack: None, hints: self.hints.clone(), } } fn render_html(&self) -> RenderedHtmlDiagnostic { RenderedHtmlDiagnostic { message: escape_html(&self.message).into_owned(), stack: None, frames: self .code_frames .iter() .map(CodeFrame::render_html) .collect(), hints: self .hints .iter() .map(|hint| escape_html(hint).into_owned()) .collect(), documentation: self .documentation_url .as_ref() .map(|url| escape_html(url).into_owned()), } } pub fn report(&self, dest: &mut W) -> std::io::Result<()> { let (color, emoji) = match self.severity { DiagnosticSeverity::Error | DiagnosticSeverity::SourceError => (AnsiColor::Red, "馃毃"), DiagnosticSeverity::Warning => (AnsiColor::BrightYellow, "鈿狅笍"), DiagnosticSeverity::Info => (AnsiColor::BrightBlue, "鈩癸笍"), }; let style = Style::new().fg_color(Some(Color::Ansi(color))).bold(); writeln!( dest, "{emoji} {style}{}: {}{style:#}", self .origin .as_ref() .map(|o| o.as_str()) .unwrap_or("unknown"), self.message )?; if !self.code_frames.is_empty() { writeln!(dest)?; } let mut first = true; for frame in &self.code_frames { if !first { write!(dest, "\n\n")?; } frame.report(dest)?; first = false; } if !self.hints.is_empty() || self.documentation_url.is_some() { write!(dest, "\n\n")?; } let mut first = true; for hint in &self.hints { if !first { writeln!(dest)?; } writeln!(dest, "馃挕 {}", hint)?; first = false; } if let Some(docs) = &self.documentation_url { writeln!(dest, "馃摑 {}", docs.as_str())?; } Ok(()) } } const PADDING_BEFORE: u32 = 1; const PADDING_AFTER: u32 = 2; // const TERMINAL_WIDTH: usize = 80; const MAX_LINES: u32 = 12; impl CodeFrame { fn render_html(&self) -> RenderedHtmlFrame { RenderedHtmlFrame { location: escape_html(&self.location()).into_owned(), code: self.render_code(&HtmlRenderer), } } /// What the frame shows: a file's path relative to the working directory, or /// the URL of anything else, such as code a plugin generated. fn source_name(url: &SourceUrl) -> String { match url.to_file_path() { Ok(path) => { let cwd = PathId::new(&std::env::current_dir().unwrap_or_default()); path.relative(&cwd).to_string_lossy().into_owned() } Err(_) => url.to_string(), } } fn location(&self) -> String { let mut location = String::new(); if let Some(url) = &self.url { location.push_str(&Self::source_name(url)); if let Some(highlight) = self.code_highlights.first() { use std::fmt::Write; write!( &mut location, ":{}:{}", highlight.start.line, highlight.start.column ) .unwrap(); } } location } fn line_windows(&self) -> Vec<(u32, u32)> { let mut windows = Vec::new(); for highlight in &self.code_highlights { let start = highlight.start.line.saturating_sub(PADDING_BEFORE).max(1); let end = highlight.end.line.saturating_add(PADDING_AFTER); if end.saturating_sub(start) >= MAX_LINES { // Keep both ends of a long highlight, including its explanatory label. windows.push((start, start + MAX_LINES / 2 - 1)); windows.push((end - MAX_LINES / 2 + 1, end)); } else { windows.push((start, end)); } } windows.sort_unstable(); let mut merged: Vec<(u32, u32)> = Vec::new(); for (start, end) in windows { if let Some(last) = merged.last_mut() && start <= last.1.saturating_add(1) { last.1 = last.1.max(end); } else { merged.push((start, end)); } } merged } fn code(&self) -> Option { if let Some(code) = &self.code { return Some(code.clone()); } Some(std::fs::read_to_string(self.url.as_ref()?.to_file_path().ok()?.to_path_buf()).ok()?) } fn render_code(&self, renderer: &R) -> String { let windows = self.line_windows(); let Some(&(_, end_line)) = windows.last() else { return String::new(); }; let line_number_length = end_line.to_string().len(); let Some(code) = self.code() else { return String::new(); }; let mut res = String::new(); for (window, (start_line, end_line)) in windows.into_iter().enumerate() { if window > 0 { use std::fmt::Write; writeln!( &mut res, "\n 路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路路\n", ) .unwrap(); } let lines = code .lines() .skip(start_line as usize - 1) .take(end_line as usize - start_line as usize + 1); for (line_offset, line) in lines.enumerate() { let line_number = start_line + line_offset as u32; let line_highlights = self .code_highlights .iter() .filter(|h| h.start.line <= line_number && h.end.line >= line_number) .collect::>(); let is_whole_line = !line_highlights.is_empty() && line_highlights .iter() .any(|h| h.start.line < line_number && h.end.line > line_number); use std::fmt::Write; write!( &mut res, "{} {:width$} | {}\n", renderer.error(if !line_highlights.is_empty() { ">" } else { " " }), line_number, render_highlighted( line, self.language.clone().unwrap_or(AssetType::Js), renderer ), width = line_number_length ) .unwrap(); if is_whole_line { writeln!( &mut res, "{} {} | {}", renderer.error(">"), " ".repeat(line_number_length), renderer.error(&"^".repeat(line.chars().count())) ) .unwrap(); } else if !line_highlights.is_empty() { let mut last_col = 0; let mut highlight_has_ended = false; let line_len = line.chars().count(); write!( &mut res, "{} {} | ", renderer.error(">"), " ".repeat(line_number_length) ) .unwrap(); for highlight in &line_highlights { let start_col = if highlight.start.line == line_number { highlight.start.column.saturating_sub(1) as usize } else { 0 } .min(line_len); let end_col = if highlight.end.line == line_number { highlight.end.column.saturating_sub(1) as usize } else { line_len.saturating_sub(1) } .min(line_len) .max(start_col); // TODO: Replace tab with spaces? if highlight.end.line == line_number { highlight_has_ended = true; } // If end_col is smaller than last_col it overlaps with another highlight and is no longer visible, we can skip those if end_col >= last_col { let visible_start = start_col.max(last_col); if visible_start > last_col { // Add spaces before the next visible highlight segment. write!(&mut res, "{}", " ".repeat(visible_start - last_col)).unwrap(); } let characters = end_col - visible_start + 1; write!(&mut res, "{}", renderer.error(&"^".repeat(characters))).unwrap(); last_col = end_col + 1; } } if highlight_has_ended && let Some(highlight) = line_highlights.last() && let Some(message) = &highlight.message { write!(&mut res, " {}", renderer.plain(message)).unwrap(); } res.push('\n'); } } } res } pub fn report(&self, dest: &mut W) -> std::io::Result<()> { if let Some(url) = &self.url { let style = Style::new() .fg_color(Some(Color::Ansi(AnsiColor::BrightBlack))) .underline(); write!(dest, "{style}{}", Self::source_name(url))?; if let Some(highlight) = self.code_highlights.first() { write!(dest, ":{}:{}", highlight.start.line, highlight.start.column)?; } write!(dest, "{style:#}\n")?; } write!(dest, "{}", self.render_code(&AnsiRenderer))?; Ok(()) } } #[derive(Clone, Copy)] struct SyntaxStyle { ansi_color: u8, html_color: &'static str, bold: bool, italic: bool, underline: bool, } impl SyntaxStyle { fn ansi(self) -> Style { let mut style = Style::new().fg_color(Some(Color::Ansi256(Ansi256Color(self.ansi_color)))); if self.bold { style = style.bold(); } if self.italic { style = style.italic(); } if self.underline { style = style.underline(); } style } fn ansi_open(self) -> String { self.ansi().to_string() } fn ansi_close(self) -> String { format!("{:#}", self.ansi()) } fn html_style(self) -> String { let mut style = format!("color:{}", self.html_color); if self.bold { style.push_str(";font-weight:700"); } if self.italic { style.push_str(";font-style:italic"); } if self.underline { style.push_str(";text-decoration:underline"); } style } fn html_open(self) -> String { format!("", self.html_style()) } fn html_close(self) -> &'static str { "" } } struct HighlightToken<'a> { style: Option, text: &'a str, } macro_rules! syntax_style { ($ansi: literal, $html: literal) => { SyntaxStyle { ansi_color: $ansi, html_color: $html, bold: false, italic: false, underline: false, } }; ($ansi: literal, $html: literal, italic) => { SyntaxStyle { ansi_color: $ansi, html_color: $html, bold: false, italic: true, underline: false, } }; ($ansi: literal, $html: literal, bold) => { SyntaxStyle { ansi_color: $ansi, html_color: $html, bold: true, italic: false, underline: false, } }; ($ansi: literal, $html: literal, underline) => { SyntaxStyle { ansi_color: $ansi, html_color: $html, bold: false, italic: false, underline: true, } }; } const HIGHLIGHT_STYLES: &[SyntaxStyle] = &[ syntax_style!(124, "#dc2626", italic), // attribute syntax_style!(245, "#a1a1aa", italic), // comment syntax_style!(94, "#a16207"), // constant syntax_style!(94, "#a16207", bold), // constant.builtin syntax_style!(136, "#ca8a04"), // constructor syntax_style!(26, "#2563eb"), // function syntax_style!(26, "#2563eb", bold), // function.builtin syntax_style!(202, "#ea580c"), // keyword syntax_style!(136, "#ca8a04"), // module syntax_style!(94, "#a16207", bold), // number syntax_style!(94, "#a16207", bold), // boolean syntax_style!(239, "#71717a", bold), // operator syntax_style!(124, "#dc2626"), // title syntax_style!(124, "#dc2626"), // label syntax_style!(124, "#dc2626"), // name syntax_style!(124, "#dc2626"), // property syntax_style!(124, "#dc2626", bold), // property.builtin syntax_style!(239, "#71717a"), // punctuation syntax_style!(28, "#16a34a"), // string syntax_style!(30, "#0d9488"), // string.special syntax_style!(18, "#1e40af"), // tag syntax_style!(23, "#0f766e"), // type syntax_style!(23, "#0f766e", bold), // type.builtin syntax_style!(252, "#e4e4e7"), // variable syntax_style!(252, "#e4e4e7", bold), // variable.builtin syntax_style!(252, "#e4e4e7", underline), // variable.parameter ]; trait CodeFrameRenderer { fn plain(&self, text: &str) -> String; fn error(&self, text: &str) -> String; fn syntax(&self, style: SyntaxStyle, text: &str) -> String; } struct AnsiRenderer; struct HtmlRenderer; impl CodeFrameRenderer for AnsiRenderer { fn plain(&self, text: &str) -> String { text.to_string() } fn error(&self, text: &str) -> String { let style = Style::new() .fg_color(Some(Color::Ansi(AnsiColor::Red))) .bold(); format!("{style}{text}{style:#}") } fn syntax(&self, style: SyntaxStyle, text: &str) -> String { format!("{}{}{}", style.ansi_open(), text, style.ansi_close()) } } impl CodeFrameRenderer for HtmlRenderer { fn plain(&self, text: &str) -> String { escape_html(text).into_owned() } fn error(&self, text: &str) -> String { format!( "{}", escape_html(text) ) } fn syntax(&self, style: SyntaxStyle, text: &str) -> String { format!( "{}{}{}", style.html_open(), escape_html(text), style.html_close() ) } } fn render_highlighted(code: &str, lang: AssetType, renderer: &R) -> String { let tokens = highlight_tokens(code, lang); let mut res = String::new(); for token in tokens { if let Some(style) = token.style { res.push_str(&renderer.syntax(HIGHLIGHT_STYLES[style], token.text)); } else { res.push_str(&renderer.plain(token.text)); } } res } fn highlight_tokens(code: &str, lang: AssetType) -> Vec> { use parcel_highlight::Language; let language = match lang { AssetType::Js => Language::Js, AssetType::Jsx => Language::Jsx, AssetType::Ts => Language::Ts, AssetType::Tsx => Language::Tsx, AssetType::Css => Language::Css, AssetType::StyleAttribute => Language::CssDeclarations, AssetType::Json | AssetType::Jsonld => Language::Json, AssetType::Html | AssetType::Xhtml | AssetType::Xml | AssetType::Rss | AssetType::Atom | AssetType::Svg => Language::Html, _ => { return vec![HighlightToken { style: None, text: code, }]; } }; let mut tokens = Vec::new(); let mut pos = 0; for span in parcel_highlight::highlight(code, language) { if span.start > pos { tokens.push(HighlightToken { style: None, text: &code[pos..span.start], }); } tokens.push(HighlightToken { style: Some(class_style(span.class)), text: &code[span.start..span.end], }); pos = span.end; } if pos < code.len() { tokens.push(HighlightToken { style: None, text: &code[pos..], }); } tokens } /// Index into [`HIGHLIGHT_STYLES`] for each highlight class. fn class_style(class: parcel_highlight::Class) -> usize { use parcel_highlight::Class; match class { Class::Attribute => 0, Class::Comment => 1, Class::CapsConst => 2, Class::Constructor => 4, Class::Function => 5, Class::Keyword => 7, Class::Number => 9, Class::Constant => 10, Class::Operator => 11, Class::Property => 15, Class::Punctuation => 17, Class::String => 18, Class::Regex => 19, Class::Tag => 20, } } fn escape_html(value: &str) -> Cow<'_, str> { if !value .as_bytes() .iter() .any(|b| matches!(b, b'&' | b'<' | b'>' | b'"' | b'\'')) { return Cow::Borrowed(value); } let mut result = String::with_capacity(value.len()); for ch in value.chars() { match ch { '&' => result.push_str("&"), '<' => result.push_str("<"), '>' => result.push_str(">"), '"' => result.push_str("""), '\'' => result.push_str("'"), _ => result.push(ch), } } Cow::Owned(result) } #[cfg(test)] mod tests { use super::*; #[test] fn highlights_code_frames() { let html = render_highlighted("const x = 1;", AssetType::Js, &HtmlRenderer); assert!(html.contains(">const"), "{html}"); assert!(html.contains("#ea580c"), "{html}"); // Broken code (the usual code frame subject) keeps highlighting: the // unterminated string stops at end of line and the next line still styles. let html = render_highlighted("const s = 'oops\nreturn 1;", AssetType::Js, &HtmlRenderer); assert!(html.contains(">'oops"), "{html}"); assert!(html.contains(">return"), "{html}"); // Unhighlighted languages pass through unstyled. let plain = render_highlighted("a = 1", AssetType::Toml, &HtmlRenderer); assert_eq!(plain, "a = 1"); } #[test] fn renders_multiline_and_overlapping_code_highlights() { let frame = CodeFrame { code: Some("let result = macro(\n first,\n second,\n);\n".into()), url: None, language: Some(AssetType::Js), code_highlights: vec![ CodeHighlight { message: Some("macro failed".into()), start: Location { line: 1, column: 14, }, end: Location { line: 4, column: 2 }, }, CodeHighlight { message: None, start: Location { line: 1, column: 16, }, end: Location { line: 1, column: u32::MAX, }, }, ], }; let rendered = frame.render_code(&HtmlRenderer); assert!(rendered.contains("macro failed"), "{rendered}"); assert!(rendered.contains('^'), "{rendered}"); } #[test] fn distant_highlights_render_separate_excerpts() { let frame = CodeFrame { code: Some((1..=100).map(|i| format!("source line {i}\n")).collect()), language: Some(AssetType::Toml), code_highlights: [2, 90] .map(|line| CodeHighlight { start: Location { line, column: 1 }, end: Location { line, column: 6 }, message: Some(format!("highlight {line}")), }) .into(), ..CodeFrame::default() }; for rendered in [ frame.render_code(&HtmlRenderer), frame.render_code(&AnsiRenderer), ] { assert!(rendered.contains(" 2 | source line 2"), "{rendered}"); assert!(rendered.contains(" 90 | source line 90"), "{rendered}"); assert!(rendered.contains("highlight 2") && rendered.contains("highlight 90")); assert!(rendered.contains("\n 路路路"), "{rendered}"); assert!(!rendered.contains("source line 50")); assert!(rendered.lines().count() < 15); } } #[test] fn long_highlights_keep_their_start_end_and_message() { let frame = CodeFrame { code: Some((1..=100).map(|i| format!("source line {i}\n")).collect()), language: Some(AssetType::Toml), code_highlights: vec![CodeHighlight { start: Location { line: 1, column: 1 }, end: Location { line: 99, column: 6, }, message: Some("the end".into()), }], ..CodeFrame::default() }; let rendered = frame.render_code(&HtmlRenderer); assert!(rendered.contains("source line 1\n")); assert!(rendered.contains("source line 99\n")); assert!(rendered.contains("the end")); assert!(rendered.contains("\n 路路路"), "{rendered}"); assert!(!rendered.contains("source line 50")); } #[test] fn gets_json_source_locations() { let code = r#"{ "a": 1 }"#; let key = json_source_location(code, "/a", JsonSourceLocationType::Key) .unwrap() .unwrap(); let value = json_source_location(code, "/a", JsonSourceLocationType::Value) .unwrap() .unwrap(); let key_and_value = json_source_location(code, "/a", JsonSourceLocationType::KeyAndValue) .unwrap() .unwrap(); assert_eq!( CodeHighlight::from_json(key.0, key.1, Some("key")), CodeHighlight { message: Some("key".into()), start: Location { line: 2, column: 3 }, end: Location { line: 2, column: 5 }, } ); assert_eq!( CodeHighlight::from_json(value.0, value.1, Some("value")), CodeHighlight { message: Some("value".into()), start: Location { line: 2, column: 8 }, end: Location { line: 2, column: 8 }, } ); assert_eq!( CodeHighlight::from_json(key_and_value.0, key_and_value.1, None), CodeHighlight { message: None, start: Location { line: 2, column: 3 }, end: Location { line: 2, column: 8 }, } ); } #[test] fn returns_none_for_missing_json_pointer() { assert_eq!( json_source_location("{}", "/missing", JsonSourceLocationType::Value).unwrap(), None ); } /// A frame of code that is not a file, such as one a plugin generated, is /// named by its URL rather than taking the reporter down. #[test] fn renders_code_frames_of_urls_that_are_not_files() { let frame = CodeFrame { code: Some("export default 1;\n".into()), url: Some(SourceUrl::parse("custom:generated").unwrap()), language: Some(AssetType::Js), code_highlights: vec![CodeHighlight { message: None, start: Location { line: 1, column: 1 }, end: Location { line: 1, column: 6 }, }], }; let mut ansi = Vec::new(); frame.report(&mut ansi).unwrap(); let ansi = String::from_utf8(ansi).unwrap(); assert!(ansi.contains("custom:generated"), "{ansi}"); assert_eq!(frame.location(), "custom:generated:1:1"); } #[test] fn renders_browser_diagnostics_with_escaped_html_codeframes() { let diagnostics = DiagnosticList(vec![Diagnostic { message: "Unexpected ".into(), origin: Some("test".into()), code_frames: vec![CodeFrame { code: Some("let value = foo < bar;\n".into()), url: None, language: Some(AssetType::Js), code_highlights: vec![CodeHighlight { message: Some("escape ".into()), start: Location { line: 1, column: 13, }, end: Location { line: 1, column: 15, }, }], }], hints: vec!["Use > instead".into()], severity: DiagnosticSeverity::Error, documentation_url: Some("https://example.com?a=".into()), }]); let rendered = diagnostics.render_for_browser(); assert_eq!(rendered.html[0].message, "Unexpected <token>"); assert_eq!(rendered.html[0].hints, vec!["Use > instead"]); assert_eq!( rendered.html[0].documentation, Some("https://example.com?a=<b>".into()) ); assert!(rendered.html[0].frames[0].code.contains("foo")); assert!(rendered.html[0].frames[0].code.contains("<")); assert!(rendered.html[0].frames[0].code.contains("bar")); assert!( rendered.html[0].frames[0] .code .contains("escape <this>") ); assert!(rendered.ansi[0].codeframe.is_some()); } }