Make JsonEmitter
more like HumanEmitter
.
Use `derive(Setters)` to derive setters, and then change `JsonEmitter::new` to only have the arguments that are always used.
This commit is contained in:
parent
2999d8dc72
commit
9ff4487999
5 changed files with 32 additions and 58 deletions
|
@ -21,12 +21,11 @@ use crate::{
|
|||
FluentBundle, LazyFallbackBundle, Level, MultiSpan, Subdiag, SubstitutionHighlight,
|
||||
SuggestionStyle, TerminalUrl,
|
||||
};
|
||||
use rustc_lint_defs::pluralize;
|
||||
|
||||
use derive_setters::Setters;
|
||||
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
|
||||
use rustc_data_structures::sync::{DynSend, IntoDynSyncSend, Lrc};
|
||||
use rustc_error_messages::{FluentArgs, SpanLabel};
|
||||
use rustc_lint_defs::pluralize;
|
||||
use rustc_span::hygiene::{ExpnKind, MacroKind};
|
||||
use std::borrow::Cow;
|
||||
use std::cmp::{max, min, Reverse};
|
||||
|
|
|
@ -9,9 +9,6 @@
|
|||
|
||||
// FIXME: spec the JSON output properly.
|
||||
|
||||
use rustc_span::source_map::SourceMap;
|
||||
use termcolor::{ColorSpec, WriteColor};
|
||||
|
||||
use crate::emitter::{
|
||||
should_show_source_code, ColorConfig, Destination, Emitter, HumanEmitter,
|
||||
HumanReadableErrorType,
|
||||
|
@ -22,32 +19,39 @@ use crate::{
|
|||
diagnostic::IsLint, CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel,
|
||||
Subdiag, TerminalUrl,
|
||||
};
|
||||
use rustc_lint_defs::Applicability;
|
||||
|
||||
use derive_setters::Setters;
|
||||
use rustc_data_structures::sync::{IntoDynSyncSend, Lrc};
|
||||
use rustc_error_messages::FluentArgs;
|
||||
use rustc_lint_defs::Applicability;
|
||||
use rustc_span::hygiene::ExpnData;
|
||||
use rustc_span::source_map::SourceMap;
|
||||
use rustc_span::Span;
|
||||
use serde::Serialize;
|
||||
use std::error::Report;
|
||||
use std::io::{self, Write};
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::vec;
|
||||
|
||||
use serde::Serialize;
|
||||
use termcolor::{ColorSpec, WriteColor};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[derive(Setters)]
|
||||
pub struct JsonEmitter {
|
||||
#[setters(skip)]
|
||||
dst: IntoDynSyncSend<Box<dyn Write + Send>>,
|
||||
registry: Option<Registry>,
|
||||
#[setters(skip)]
|
||||
sm: Lrc<SourceMap>,
|
||||
fluent_bundle: Option<Lrc<FluentBundle>>,
|
||||
#[setters(skip)]
|
||||
fallback_bundle: LazyFallbackBundle,
|
||||
#[setters(skip)]
|
||||
pretty: bool,
|
||||
ui_testing: bool,
|
||||
ignored_directories_in_source_blocks: Vec<String>,
|
||||
#[setters(skip)]
|
||||
json_rendered: HumanReadableErrorType,
|
||||
diagnostic_width: Option<usize>,
|
||||
macro_backtrace: bool,
|
||||
|
@ -58,42 +62,28 @@ pub struct JsonEmitter {
|
|||
impl JsonEmitter {
|
||||
pub fn new(
|
||||
dst: Box<dyn Write + Send>,
|
||||
registry: Option<Registry>,
|
||||
source_map: Lrc<SourceMap>,
|
||||
fluent_bundle: Option<Lrc<FluentBundle>>,
|
||||
sm: Lrc<SourceMap>,
|
||||
fallback_bundle: LazyFallbackBundle,
|
||||
pretty: bool,
|
||||
json_rendered: HumanReadableErrorType,
|
||||
diagnostic_width: Option<usize>,
|
||||
macro_backtrace: bool,
|
||||
track_diagnostics: bool,
|
||||
terminal_url: TerminalUrl,
|
||||
) -> JsonEmitter {
|
||||
JsonEmitter {
|
||||
dst: IntoDynSyncSend(dst),
|
||||
registry,
|
||||
sm: source_map,
|
||||
fluent_bundle,
|
||||
registry: None,
|
||||
sm,
|
||||
fluent_bundle: None,
|
||||
fallback_bundle,
|
||||
pretty,
|
||||
ui_testing: false,
|
||||
ignored_directories_in_source_blocks: Vec::new(),
|
||||
json_rendered,
|
||||
diagnostic_width,
|
||||
macro_backtrace,
|
||||
track_diagnostics,
|
||||
terminal_url,
|
||||
diagnostic_width: None,
|
||||
macro_backtrace: false,
|
||||
track_diagnostics: false,
|
||||
terminal_url: TerminalUrl::No,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ui_testing(self, ui_testing: bool) -> Self {
|
||||
Self { ui_testing, ..self }
|
||||
}
|
||||
|
||||
pub fn ignored_directories_in_source_blocks(self, value: Vec<String>) -> Self {
|
||||
Self { ignored_directories_in_source_blocks: value, ..self }
|
||||
}
|
||||
|
||||
fn emit(&mut self, val: EmitTyped<'_>) -> io::Result<()> {
|
||||
if self.pretty {
|
||||
serde_json::to_writer_pretty(&mut *self.dst, &val)?
|
||||
|
|
|
@ -48,16 +48,10 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
|
|||
let output = Arc::new(Mutex::new(Vec::new()));
|
||||
let je = JsonEmitter::new(
|
||||
Box::new(Shared { data: output.clone() }),
|
||||
None,
|
||||
sm,
|
||||
None,
|
||||
fallback_bundle,
|
||||
true,
|
||||
true, // pretty
|
||||
HumanReadableErrorType::Short(ColorConfig::Never),
|
||||
None,
|
||||
false,
|
||||
false,
|
||||
TerminalUrl::No,
|
||||
);
|
||||
|
||||
let span = Span::with_root_ctxt(BytePos(span.0), BytePos(span.1));
|
||||
|
|
|
@ -1001,21 +1001,21 @@ fn default_emitter(
|
|||
config::ErrorOutputType::Json { pretty, json_rendered } => Box::new(
|
||||
JsonEmitter::new(
|
||||
Box::new(io::BufWriter::new(io::stderr())),
|
||||
Some(registry),
|
||||
source_map,
|
||||
bundle,
|
||||
fallback_bundle,
|
||||
pretty,
|
||||
json_rendered,
|
||||
sopts.diagnostic_width,
|
||||
macro_backtrace,
|
||||
track_diagnostics,
|
||||
terminal_url,
|
||||
)
|
||||
.registry(Some(registry))
|
||||
.fluent_bundle(bundle)
|
||||
.ui_testing(sopts.unstable_opts.ui_testing)
|
||||
.ignored_directories_in_source_blocks(
|
||||
sopts.unstable_opts.ignore_directory_in_diagnostics_source_blocks.clone(),
|
||||
),
|
||||
)
|
||||
.diagnostic_width(sopts.diagnostic_width)
|
||||
.macro_backtrace(macro_backtrace)
|
||||
.track_diagnostics(track_diagnostics)
|
||||
.terminal_url(terminal_url),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
@ -1482,16 +1482,10 @@ fn mk_emitter(output: ErrorOutputType) -> Box<DynEmitter> {
|
|||
}
|
||||
config::ErrorOutputType::Json { pretty, json_rendered } => Box::new(JsonEmitter::new(
|
||||
Box::new(io::BufWriter::new(io::stderr())),
|
||||
None,
|
||||
Lrc::new(SourceMap::new(FilePathMapping::empty())),
|
||||
None,
|
||||
fallback_bundle,
|
||||
pretty,
|
||||
json_rendered,
|
||||
None,
|
||||
false,
|
||||
false,
|
||||
TerminalUrl::No,
|
||||
)),
|
||||
};
|
||||
emitter
|
||||
|
|
|
@ -158,18 +158,15 @@ pub(crate) fn new_dcx(
|
|||
Box::new(
|
||||
JsonEmitter::new(
|
||||
Box::new(io::BufWriter::new(io::stderr())),
|
||||
None,
|
||||
source_map,
|
||||
None,
|
||||
fallback_bundle,
|
||||
pretty,
|
||||
json_rendered,
|
||||
diagnostic_width,
|
||||
false,
|
||||
unstable_opts.track_diagnostics,
|
||||
TerminalUrl::No,
|
||||
)
|
||||
.ui_testing(unstable_opts.ui_testing),
|
||||
.ui_testing(unstable_opts.ui_testing)
|
||||
.diagnostic_width(diagnostic_width)
|
||||
.track_diagnostics(unstable_opts.track_diagnostics)
|
||||
.terminal_url(TerminalUrl::No),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue