From 0f3e6e2eeaae882be83f5cb80388d0f4b183b474 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Sun, 3 May 2020 04:55:16 +0100 Subject: [PATCH] fix(cli/fmt_errors): Respect NO_COLOR for stack frames (#5051) --- cli/fmt_errors.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/cli/fmt_errors.rs b/cli/fmt_errors.rs index e0fc614592..7317d7fc01 100644 --- a/cli/fmt_errors.rs +++ b/cli/fmt_errors.rs @@ -118,23 +118,31 @@ impl Deref for JSError { impl fmt::Display for JSError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mut formatted_frames = self.0.formatted_frames.clone(); + + // The formatted_frames passed from prepareStackTrace() are colored. + if !colors::use_color() { + formatted_frames = formatted_frames + .iter() + .map(|s| colors::strip_ansi_codes(s).to_string()) + .collect(); + } + // When the stack frame array is empty, but the source location given by // (script_resource_name, line_number, start_column + 1) exists, this is // likely a syntax error. For the sake of formatting we treat it like it was // given as a single stack frame. - let formatted_frames = if self.0.formatted_frames.is_empty() + if formatted_frames.is_empty() && self.0.script_resource_name.is_some() && self.0.line_number.is_some() && self.0.start_column.is_some() { - vec![format!( + formatted_frames = vec![format!( "{}:{}:{}", colors::cyan(self.0.script_resource_name.clone().unwrap()), colors::yellow(self.0.line_number.unwrap().to_string()), colors::yellow((self.0.start_column.unwrap() + 1).to_string()) )] - } else { - self.0.formatted_frames.clone() }; write!(