Skip to content

Commit

Permalink
refactor(prettier)!: remove source_text argument from constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Oct 11, 2024
1 parent 5200960 commit b8a3dd6
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 12 deletions.
1 change: 0 additions & 1 deletion crates/oxc_prettier/examples/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ fn main() -> std::io::Result<()> {
.parse();
let output = Prettier::new(
&allocator,
&source_text,
PrettierOptions { semi, trailing_comma: TrailingComma::All, ..PrettierOptions::default() },
)
.build(&ret.program);
Expand Down
7 changes: 4 additions & 3 deletions crates/oxc_prettier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ impl<'a> DocBuilder<'a> for Prettier<'a> {

impl<'a> Prettier<'a> {
#[allow(clippy::needless_pass_by_value)]
pub fn new(allocator: &'a Allocator, source_text: &'a str, options: PrettierOptions) -> Self {
pub fn new(allocator: &'a Allocator, options: PrettierOptions) -> Self {
Self {
allocator,
source_text,
source_text: "",
options,
stack: vec![],
group_id_builder: GroupIdBuilder::default(),
Expand All @@ -83,8 +83,9 @@ impl<'a> Prettier<'a> {
}

pub fn build(&mut self, program: &Program<'a>) -> String {
self.source_text = program.source_text;
let doc = program.format(self);
Printer::new(doc, self.source_text, self.options, self.allocator).build()
Printer::new(doc, program.source_text, self.options, self.allocator).build()
}

pub fn doc(mut self, program: &Program<'a>) -> Doc<'a> {
Expand Down
5 changes: 2 additions & 3 deletions crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl Oxc {
.with_options(ParseOptions { preserve_parens: false, ..ParseOptions::default() })
.parse();

let mut prettier = Prettier::new(&allocator, source_text, PrettierOptions::default());
let mut prettier = Prettier::new(&allocator, PrettierOptions::default());

if run_options.prettier_format.unwrap_or_default() {
self.prettier_formatted_text = prettier.build(&ret.program);
Expand All @@ -326,8 +326,7 @@ impl Oxc {
let prettier_doc = prettier.doc(&ret.program).to_string();
self.prettier_ir_text = {
let ret = Parser::new(&allocator, &prettier_doc, SourceType::default()).parse();
Prettier::new(&allocator, &prettier_doc, PrettierOptions::default())
.build(&ret.program)
Prettier::new(&allocator, PrettierOptions::default()).build(&ret.program)
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions tasks/benchmark/benches/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ fn bench_prettier(criterion: &mut Criterion) {
let allocator1 = Allocator::default();
let allocator2 = Allocator::default();
let ret = Parser::new(&allocator1, source_text, source_type).parse();
let _ = Prettier::new(&allocator2, source_text, PrettierOptions::default())
.build(&ret.program);
let _ =
Prettier::new(&allocator2, PrettierOptions::default()).build(&ret.program);
});
},
);
Expand Down
4 changes: 2 additions & 2 deletions tasks/coverage/src/tools/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ fn get_result(source_text: &str, source_type: SourceType) -> TestResult {
let parse_options = ParseOptions { preserve_parens: false, ..ParseOptions::default() };
let ParserReturn { program, .. } =
Parser::new(&allocator, source_text, source_type).with_options(parse_options).parse();
let source_text1 = Prettier::new(&allocator, source_text, options).build(&program);
let source_text1 = Prettier::new(&allocator, options).build(&program);

let allocator = Allocator::default();
let ParserReturn { program, .. } =
Parser::new(&allocator, &source_text1, source_type).with_options(parse_options).parse();
let source_text2 = Prettier::new(&allocator, &source_text1, options).build(&program);
let source_text2 = Prettier::new(&allocator, options).build(&program);

if source_text1 == source_text2 {
TestResult::Passed
Expand Down
2 changes: 1 addition & 1 deletion tasks/prettier_conformance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ impl TestRunner {
let ret = Parser::new(&allocator, source_text, source_type)
.with_options(ParseOptions { preserve_parens: false, ..ParseOptions::default() })
.parse();
Prettier::new(&allocator, source_text, prettier_options).build(&ret.program)
Prettier::new(&allocator, prettier_options).build(&ret.program)
}
}

Expand Down

0 comments on commit b8a3dd6

Please sign in to comment.