refactor(coverage): avoid an String::from_utf8 over head during serialization (#3145)

This commit is contained in:
Boshen 2024-04-30 23:11:22 +08:00 committed by GitHub
parent 67225a8091
commit f5dccc96fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 5 deletions

View file

@ -28,11 +28,17 @@ impl serde_json::ser::Formatter for EcmaFormatter {
impl<'a> Program<'a> {
/// # Panics
pub fn to_json(&self) -> String {
let buf = std::vec::Vec::new();
let mut ser = serde_json::Serializer::with_formatter(buf, crate::serialize::EcmaFormatter);
self.serialize(&mut ser).unwrap();
let ser = self.serializer();
String::from_utf8(ser.into_inner()).unwrap()
}
/// # Panics
pub fn serializer(&self) -> serde_json::Serializer<std::vec::Vec<u8>, EcmaFormatter> {
let buf = std::vec::Vec::new();
let mut ser = serde_json::Serializer::with_formatter(buf, EcmaFormatter);
self.serialize(&mut ser).unwrap();
ser
}
}
impl Serialize for RegExpFlags {

View file

@ -322,8 +322,8 @@ pub trait Case: Sized + Sync + Send + UnwindSafe {
return res;
}
// Make sure serialization doesn't crash for ast and hir, also for code coverage.
let _json = parser_ret.program.to_json();
// Make sure serialization doesn't crash; also for code coverage.
let _serializer = parser_ret.program.serializer();
let program = allocator.alloc(parser_ret.program);
let semantic_ret = SemanticBuilder::new(source_text, source_type)