fix autofmt: don't panic when writing blocks out without a srcfile (#2854)

* fix: don't panic when writing blocks out
* also fix serialization for hotreload
* fix windows line endings
This commit is contained in:
Jonathan Kelley authored and GitHub committed 2024-08-16 21:55:30 -07:00
1 parent b47a6cf83e
commit 4963aa3118
9 files changed
+142 -46

No files matched your search

+1 -1
View File
@@ -93,7 +93,7 @@ pub fn try_fmt_file(
writer.out.indent_level = writer
.out
.indent
.count_indents(writer.src[rsx_start.line - 1]);
.count_indents(writer.src.get(rsx_start.line - 1).unwrap_or(&""));
// TESTME
// Writing *should* not fail but it's possible that it does
+21 -9
View File
@@ -607,7 +607,11 @@ impl<'a> Writer<'a> {
let mut comments = VecDeque::new();
for (id, line) in self.src[..line_start].iter().enumerate().rev() {
let Some(lines) = self.src.get(..line_start) else {
return comments;
};
for (id, line) in lines.iter().enumerate().rev() {
if line.trim().starts_with("//") || line.is_empty() && id != 0 {
if id != 0 {
comments.push_front(id);
@@ -621,7 +625,11 @@ impl<'a> Writer<'a> {
}
fn apply_comments(&mut self, mut comments: VecDeque<usize>) -> Result {
while let Some(comment_line) = comments.pop_front() {
let line = &self.src[comment_line].trim();
let Some(line) = self.src.get(comment_line) else {
continue;
};
let line = &line.trim();
if line.is_empty() {
self.out.new_line()?;
@@ -690,13 +698,15 @@ impl<'a> Writer<'a> {
for attr in attributes {
if self.current_span_is_primary(attr.span().start()) {
'line: for line in self.src[..attr.span().start().line - 1].iter().rev() {
match (line.trim().starts_with("//"), line.is_empty()) {
(true, _) => return 100000,
(_, true) => continue 'line,
_ => break 'line,
if let Some(lines) = self.src.get(..attr.span().start().line - 1) {
'line: for line in lines.iter().rev() {
match (line.trim().starts_with("//"), line.is_empty()) {
(true, _) => return 100000,
(_, true) => continue 'line,
_ => break 'line,
}
}
}
};
}
let name_len = match &attr.name {
@@ -738,7 +748,9 @@ impl<'a> Writer<'a> {
writeln!(self.out)?;
for idx in start.line..end.line {
let line = &self.src[idx];
let Some(line) = self.src.get(idx) else {
continue;
};
if line.trim().starts_with("//") {
for _ in 0..self.out.indent_level + 1 {
write!(self.out, " ")?
+21
View File
@@ -0,0 +1,21 @@
use dioxus_rsx::CallBody;
use proc_macro2::TokenStream as TokenStream2;
/// Ensure we can write RSX blocks without a source file
///
/// Useful in code generation use cases where we still want formatted code.
#[test]
fn write_block_out() {
let src = include_str!("./srcless/basic_expr.rsx");
let tokens: TokenStream2 = syn::parse_str(src).unwrap();
let parsed: CallBody = syn::parse2(tokens).unwrap();
let block = dioxus_autofmt::write_block_out(&parsed).unwrap();
// normalize line endings for windows tests to pass
pretty_assertions::assert_eq!(
block.trim().lines().collect::<Vec<_>>().join("\n"),
src.trim().lines().collect::<Vec<_>>().join("\n")
);
}
@@ -0,0 +1,42 @@
div {
"hi"
{children}
}
Fragment {
Fragment {
Fragment {
Fragment {
Fragment {
div { "Finally have a real node!" }
}
}
}
}
}
div { class, "hello world" }
h1 { class, "hello world" }
h1 { class, {children} }
h1 { class, id, {children} }
h1 { class,
"hello world"
{children}
}
h1 { id,
"hello world"
{children}
}
Other { class, children }
Other { class,
"hello world"
{children}
}
div {
class: "asdasd",
onclick: move |_| {
let a = 10;
let b = 40;
let c = 50;
},
"hi"
}
div { class: "asd", "Jon" }