Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
expose the verbose flag on PrintDiagnostic as explicit constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
leops committed Nov 21, 2022
1 parent 59e835a commit 65d6304
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 42 deletions.
10 changes: 5 additions & 5 deletions crates/rome_cli/src/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ fn process_messages(options: ProcessMessagesOptions) {
if mode.should_report_to_terminal() {
if should_print {
console.error(markup! {
{PrintDiagnostic(&err, verbose)}
{if verbose { PrintDiagnostic::verbose(&err) } else { PrintDiagnostic::simple(&err) }}
});
}
} else {
Expand Down Expand Up @@ -466,7 +466,7 @@ fn process_messages(options: ProcessMessagesOptions) {

let diag = diag.with_file_path(&name).with_file_source_code(&content);
console.error(markup! {
{PrintDiagnostic(&diag, verbose)}
{if verbose { PrintDiagnostic::verbose(&diag) } else { PrintDiagnostic::simple(&diag) }}
});
}
} else {
Expand All @@ -492,7 +492,7 @@ fn process_messages(options: ProcessMessagesOptions) {
let diag =
diag.with_file_path(&name).with_file_source_code(&content);
console.error(markup! {
{PrintDiagnostic(&diag, verbose)}
{if verbose { PrintDiagnostic::verbose(&diag) } else { PrintDiagnostic::simple(&diag) }}
});
}
} else {
Expand Down Expand Up @@ -542,7 +542,7 @@ fn process_messages(options: ProcessMessagesOptions) {
};

console.error(markup! {
{PrintDiagnostic(&diag, verbose)}
{if verbose { PrintDiagnostic::verbose(&diag) } else { PrintDiagnostic::simple(&diag) }}
});
} else {
let diag = FormatDiffDiagnostic {
Expand All @@ -554,7 +554,7 @@ fn process_messages(options: ProcessMessagesOptions) {
};

console.error(markup! {
{PrintDiagnostic(&diag, verbose)}
{if verbose { PrintDiagnostic::verbose(&diag) } else { PrintDiagnostic::simple(&diag) }}
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_diagnostics/examples/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ pub fn main() {
},
};

EnvConsole::default().error(markup!({ PrintDiagnostic(&diag, true) }));
EnvConsole::default().error(markup!({ PrintDiagnostic::verbose(&diag) }));
}
2 changes: 1 addition & 1 deletion crates/rome_diagnostics/examples/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ pub fn main() {
},
};

EnvConsole::default().error(markup!({ PrintDiagnostic(&diag, true) }));
EnvConsole::default().error(markup!({ PrintDiagnostic::verbose(&diag) }));
}
2 changes: 1 addition & 1 deletion crates/rome_diagnostics/examples/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ console.log(FOO);";
},
};

EnvConsole::default().error(markup!({ PrintDiagnostic(&diag, true) }));
EnvConsole::default().error(markup!({ PrintDiagnostic::verbose(&diag) }));
}
2 changes: 1 addition & 1 deletion crates/rome_diagnostics/examples/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ fn from_str(input: &str) -> Result<serde_json::Value> {

pub fn main() {
if let Err(err) = from_str("{\"syntax_error\"") {
EnvConsole::default().error(markup!({ PrintDiagnostic(&err, true) }));
EnvConsole::default().error(markup!({ PrintDiagnostic::verbose(&err) }));
};
}
41 changes: 30 additions & 11 deletions crates/rome_diagnostics/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,30 @@ impl<'fmt, D: AsDiagnostic + ?Sized> std::fmt::Display for PrintDescription<'fmt

/// Helper struct for printing a diagnostic as markup into any formatter
/// implementing [rome_console::fmt::Write].
pub struct PrintDiagnostic<'fmt, D: ?Sized>(pub &'fmt D, pub bool);
pub struct PrintDiagnostic<'fmt, D: ?Sized> {
diag: &'fmt D,
verbose: bool,
}

impl<'fmt, D: AsDiagnostic + ?Sized> PrintDiagnostic<'fmt, D> {
pub fn simple(diag: &'fmt D) -> Self {
Self {
diag,
verbose: false,
}
}

pub fn verbose(diag: &'fmt D) -> Self {
Self {
diag,
verbose: true,
}
}
}

impl<'fmt, D: AsDiagnostic + ?Sized> fmt::Display for PrintDiagnostic<'fmt, D> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> io::Result<()> {
let diagnostic = self.0.as_diagnostic();
let diagnostic = self.diag.as_diagnostic();

// Print the header for the diagnostic
fmt.write_markup(markup! {
Expand All @@ -49,7 +68,7 @@ impl<'fmt, D: AsDiagnostic + ?Sized> fmt::Display for PrintDiagnostic<'fmt, D> {
let mut fmt = IndentWriter::wrap(fmt, &mut slot, true, " ");
let mut visitor = PrintAdvices(&mut fmt);

print_advices(&mut visitor, diagnostic, self.1)
print_advices(&mut visitor, diagnostic, self.verbose)
}
}

Expand Down Expand Up @@ -734,7 +753,7 @@ mod tests {
fn test_header() {
let diag = TestDiagnostic::<LogAdvices>::with_location();

let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned();
let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned();

let expected = markup!{
"path:1:1 internalError/io "<Inverse>" FIXABLE "</Inverse>" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
Expand All @@ -758,7 +777,7 @@ mod tests {
..TestDiagnostic::empty()
};

let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned();
let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned();

let expected = markup!{
"internalError/io "<Inverse>" FIXABLE "</Inverse>" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
Expand Down Expand Up @@ -788,7 +807,7 @@ mod tests {
..TestDiagnostic::empty()
};

let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned();
let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned();

let expected = markup!{
"internalError/io "<Inverse>" FIXABLE "</Inverse>" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
Expand All @@ -813,7 +832,7 @@ mod tests {
..TestDiagnostic::empty()
};

let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned();
let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned();

let expected = markup!{
"internalError/io "<Inverse>" FIXABLE "</Inverse>" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
Expand All @@ -838,7 +857,7 @@ mod tests {
..TestDiagnostic::empty()
};

let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned();
let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned();

let expected = markup!{
"internalError/io "<Inverse>" FIXABLE "</Inverse>" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
Expand All @@ -863,7 +882,7 @@ mod tests {
..TestDiagnostic::empty()
};

let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned();
let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned();

let expected = markup!{
"internalError/io "<Inverse>" FIXABLE "</Inverse>" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
Expand All @@ -889,7 +908,7 @@ mod tests {
..TestDiagnostic::empty()
};

let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned();
let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned();

let expected = markup!{
"internalError/io "<Inverse>" FIXABLE "</Inverse>" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
Expand All @@ -913,7 +932,7 @@ mod tests {
..TestDiagnostic::empty()
};

let diag = markup!({ PrintDiagnostic(&diag, true) }).to_owned();
let diag = markup!({ PrintDiagnostic::verbose(&diag) }).to_owned();

let expected = markup!{
"internalError/io "<Inverse>" FIXABLE "</Inverse>" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_js_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ mod tests {
}
let error = diag.with_file_path("ahahah").with_file_source_code(SOURCE);
let text = markup_to_string(markup! {
{PrintDiagnostic(&error, true)}
{PrintDiagnostic::verbose(&error)}
});
eprintln!("{text}");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_js_analyze/tests/spec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn diagnostic_to_string(name: &str, source: &str, diag: AnalyzerDiagnostic) -> S
.with_file_path((name, FileId::zero()))
.with_file_source_code(source);
let text = markup_to_string(markup! {
{PrintDiagnostic(&error, true)}
{PrintDiagnostic::verbose(&error)}
});

text
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_js_formatter/src/check_reformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn check_reformat(params: CheckReformatParams) {
.with_file_source_code(text.to_string());
Formatter::new(&mut Termcolor(&mut buffer))
.write_markup(markup! {
{PrintDiagnostic(&error, true)}
{PrintDiagnostic::verbose(&error)}
})
.expect("failed to emit diagnostic");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_js_formatter/tests/check_reformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn check_reformat(params: CheckReformatParams) {
.with_file_source_code(text.to_string());
Formatter::new(&mut Termcolor(&mut buffer))
.write_markup(markup! {
{PrintDiagnostic(&error, true)}
{PrintDiagnostic::verbose(&error)}
})
.expect("failed to emit diagnostic");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_js_formatter/tests/prettier_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ fn test_snapshot(input: &'static str, _: &str, _: &str, _: &str) {
.with_file_source_code(parse_input.clone());
Formatter::new(&mut Termcolor(&mut buffer))
.write_markup(markup! {
{PrintDiagnostic(&error, true)}
{PrintDiagnostic::verbose(&error)}
})
.expect("failed to emit diagnostic");
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rome_js_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ impl ParseDiagnostic {
/// .with_file_source_code(source.to_string());
/// Formatter::new(&mut Termcolor(&mut write))
/// .write_markup(markup! {
/// {PrintDiagnostic(&error, true)}
/// {PrintDiagnostic::verbose(&error)}
/// })
/// .expect("failed to emit diagnostic");
///
Expand Down Expand Up @@ -575,7 +575,7 @@ impl ParseDiagnostic {
/// .with_file_source_code(source.to_string());
/// Formatter::new(&mut Termcolor(&mut write))
/// .write_markup(markup! {
/// {PrintDiagnostic(&error, true)}
/// {PrintDiagnostic::verbose(&error)}
/// })
/// .expect("failed to emit diagnostic");
///
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_js_parser/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ where
.with_file_source_code(syntax.to_string());
Formatter::new(&mut Termcolor(&mut buffer))
.write_markup(markup! {
{PrintDiagnostic(&error, true)}
{PrintDiagnostic::verbose(&error)}
})
.unwrap();
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rome_js_parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn run_and_expect_errors(path: &str, _: &str, _: &str, _: &str) {
.with_file_source_code(text.to_string());
Formatter::new(&mut Termcolor(&mut write))
.write_markup(markup! {
{PrintDiagnostic(&error, true)}
{PrintDiagnostic::verbose(&error)}
})
.expect("failed to emit diagnostic");
write!(
Expand Down Expand Up @@ -378,7 +378,7 @@ fn diagnostics_print_correctly() {

Formatter::new(&mut Termcolor(&mut write))
.write_markup(markup! {
{PrintDiagnostic(&error, true)}
{PrintDiagnostic::verbose(&error)}
})
.expect("failed to emit diagnostic");

Expand Down
12 changes: 6 additions & 6 deletions crates/rome_js_semantic/src/tests/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub fn assert(code: &str, test_name: &str) {
.with_file_path(FileId::zero())
.with_file_source_code(code);
console.log(markup! {
{PrintDiagnostic(&error, true)}
{PrintDiagnostic::verbose(&error)}
});
}
panic!("Compilation error");
Expand Down Expand Up @@ -754,7 +754,7 @@ fn error_assertion_not_attached_to_a_declaration(

let mut console = EnvConsole::default();
console.log(markup! {
{PrintDiagnostic(&error, true)}
{PrintDiagnostic::verbose(&error)}
});
panic!("This assertion must be attached to a SemanticEvent::DeclarationFound.");
}
Expand All @@ -775,7 +775,7 @@ fn error_declaration_pointing_to_unknown_scope(

let mut console = EnvConsole::default();
console.log(markup! {
{PrintDiagnostic(&error, true)}
{PrintDiagnostic::verbose(&error)}
});
}

Expand All @@ -800,7 +800,7 @@ fn error_assertion_name_clash(

let mut console = EnvConsole::default();
console.log(markup! {
{PrintDiagnostic(&error, true)}
{PrintDiagnostic::verbose(&error)}
});

panic!("Assertion label conflict");
Expand All @@ -823,7 +823,7 @@ fn error_scope_end_assertion_points_to_non_existing_scope_start_assertion(

let mut console = EnvConsole::default();
console.log(markup! {
{PrintDiagnostic(&error, true)}
{PrintDiagnostic::verbose(&error)}
});
panic!("Scope start assertion not found.");
}
Expand All @@ -850,7 +850,7 @@ fn error_scope_end_assertion_points_to_the_wrong_scope_start(

let mut console = EnvConsole::default();
console.log(markup! {
{PrintDiagnostic(&error, true)}
{PrintDiagnostic::verbose(&error)}
});
panic!("Wrong scope start");
}
8 changes: 7 additions & 1 deletion crates/rome_wasm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ impl DiagnosticPrinter {

let mut html = HTML(&mut self.buffer);
Formatter::new(&mut html)
.write_markup(markup!({ PrintDiagnostic(&err, verbose) }))
.write_markup(markup!({
if verbose {
PrintDiagnostic::verbose(&err)
} else {
PrintDiagnostic::simple(&err)
}
}))
.map_err(into_error)?;

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion xtask/bench/src/features/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Display for ParseMeasurement {
.with_file_source_code(self.code.clone());
rome_diagnostics::console::fmt::Formatter::new(&mut Termcolor(&mut buffer))
.write_markup(markup! {
{PrintDiagnostic(&error, true)}
{PrintDiagnostic::verbose(&error)}
})
.unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion xtask/coverage/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl TestCaseFiles {
pub(crate) fn emit_errors(&self, errors: &[Error], buffer: &mut Buffer) {
for error in errors {
if let Err(err) = Formatter::new(&mut Termcolor(&mut *buffer)).write_markup(markup! {
{PrintDiagnostic(error, true)}
{PrintDiagnostic::verbose(error)}
}) {
eprintln!("Failed to print diagnostic: {}", err);
}
Expand Down
6 changes: 3 additions & 3 deletions xtask/lintdoc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ fn assert_lint(
let mut write_diagnostic = |code: &str, diag: rome_diagnostics::Error| {
let category = diag.category().map_or("", |code| code.name());
Formatter::new(&mut write).write_markup(markup! {
{PrintDiagnostic(&diag, true)}
{PrintDiagnostic::verbose(&diag)}
})?;

all_diagnostics.push(diag);
Expand All @@ -479,7 +479,7 @@ fn assert_lint(
console.print(
rome_console::LogLevel::Error,
markup! {
{PrintDiagnostic(diag, true)}
{PrintDiagnostic::verbose(diag)}
},
);
}
Expand All @@ -497,7 +497,7 @@ fn assert_lint(
console.print(
rome_console::LogLevel::Error,
markup! {
{PrintDiagnostic(diag, true)}
{PrintDiagnostic::verbose(diag)}
},
);
}
Expand Down

0 comments on commit 65d6304

Please sign in to comment.