Skip to content

Correct newline style in json emitter #4743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: rustfmt-2.0.0-rc.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::io::{self, Write};

use thiserror::Error;

use crate::{config::FileName, FormatReport, FormatResult};
use crate::{config::options::NewlineStyle, config::FileName, FormatReport, FormatResult};

pub mod checkstyle;
pub mod diff;
Expand Down Expand Up @@ -45,6 +45,7 @@ pub trait Emitter {
&mut self,
output: &mut dyn Write,
formatted_file: FormattedFile<'_>,
newline_style: NewlineStyle,
) -> Result<EmitterResult, EmitterError>;

fn emit_header(&self, _output: &mut dyn Write) -> Result<(), EmitterError> {
Expand Down Expand Up @@ -136,6 +137,7 @@ pub struct EmitterConfig {
pub color: Color,
pub verbosity: Verbosity,
pub print_filename: bool,
pub newline_style: NewlineStyle,
}

impl Default for EmitterConfig {
Expand All @@ -145,6 +147,7 @@ impl Default for EmitterConfig {
color: Color::Auto,
verbosity: Verbosity::Normal,
print_filename: false,
newline_style: NewlineStyle::default(),
}
}
}
Expand All @@ -162,7 +165,14 @@ where

emitter.emit_header(out)?;
for (filename, format_result) in format_report.format_result_as_rc().borrow().iter() {
has_diff |= write_file(filename, &format_result, out, &mut *emitter)?.has_diff;
has_diff |= write_file(
filename,
&format_result,
out,
&mut *emitter,
config.newline_style,
)?
.has_diff;
}
emitter.emit_footer(out)?;

Expand All @@ -174,6 +184,7 @@ pub(crate) fn write_file<T>(
formatted_result: &FormatResult,
out: &mut T,
emitter: &mut dyn Emitter,
newline_style: NewlineStyle,
) -> Result<EmitterResult, EmitterError>
where
T: Write,
Expand All @@ -183,8 +194,7 @@ where
original_text: formatted_result.original_text(),
formatted_text: formatted_result.formatted_text(),
};

emitter.emit_formatted_file(out, formatted_file)
emitter.emit_formatted_file(out, formatted_file, newline_style)
}

fn create_emitter(emitter_config: EmitterConfig) -> Box<dyn Emitter> {
Expand Down
3 changes: 3 additions & 0 deletions src/emitter/checkstyle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl Emitter for CheckstyleEmitter {
original_text,
formatted_text,
}: FormattedFile<'_>,
_newline_style: NewlineStyle,
) -> Result<EmitterResult, EmitterError> {
const CONTEXT_SIZE: usize = 0;
let diff = make_diff(original_text, formatted_text, CONTEXT_SIZE);
Expand Down Expand Up @@ -107,6 +108,7 @@ mod tests {
original_text: &bin_original.join("\n"),
formatted_text: &bin_formatted.join("\n"),
},
NewlineStyle::default(),
)
.unwrap();
let _ = emitter
Expand All @@ -117,6 +119,7 @@ mod tests {
original_text: &lib_original.join("\n"),
formatted_text: &lib_formatted.join("\n"),
},
NewlineStyle::default(),
)
.unwrap();
let _ = emitter.emit_footer(&mut writer);
Expand Down
5 changes: 5 additions & 0 deletions src/emitter/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl Emitter for DiffEmitter {
original_text,
formatted_text,
}: FormattedFile<'_>,
_newline_style: NewlineStyle,
) -> Result<EmitterResult, EmitterError> {
const CONTEXT_SIZE: usize = 3;
let mismatch = make_diff(&original_text, formatted_text, CONTEXT_SIZE);
Expand Down Expand Up @@ -80,6 +81,7 @@ mod tests {
original_text: "fn empty() {}\n",
formatted_text: "fn empty() {}\n",
},
NewlineStyle::default(),
)
.unwrap();
assert_eq!(result.has_diff, false);
Expand Down Expand Up @@ -108,6 +110,7 @@ mod tests {
original_text: bin_original,
formatted_text: bin_formatted,
},
NewlineStyle::default(),
)
.unwrap();
let _ = emitter
Expand All @@ -118,6 +121,7 @@ mod tests {
original_text: lib_original,
formatted_text: lib_formatted,
},
NewlineStyle::default(),
)
.unwrap();

Expand All @@ -139,6 +143,7 @@ mod tests {
original_text: "fn empty() {}\n",
formatted_text: "fn empty() {}\r\n",
},
NewlineStyle::default(),
)
.unwrap();
assert_eq!(
Expand Down
1 change: 1 addition & 0 deletions src/emitter/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl Emitter for FilesEmitter {
original_text,
formatted_text,
}: FormattedFile<'_>,
_newline_style: NewlineStyle,
) -> Result<EmitterResult, EmitterError> {
// Write text directly over original file if there is a diff.
let filename = match filename {
Expand Down
Loading