report syn parse errors to the user
This commit is contained in:
1 parent
65d4f922b4
commit
b88c66d8df
5 files changed
+29
-19
No files matched your search
Generated
+2
@@ -2418,12 +2418,14 @@ dependencies = [
|
|||||||
"mlua",
|
"mlua",
|
||||||
"notify",
|
"notify",
|
||||||
"open",
|
"open",
|
||||||
|
"prettier-please",
|
||||||
"rayon",
|
"rayon",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"rsx-rosetta",
|
"rsx-rosetta",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"subprocess",
|
"subprocess",
|
||||||
|
"syn 2.0.48",
|
||||||
"tar",
|
"tar",
|
||||||
"tauri-bundler",
|
"tauri-bundler",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
|
|||||||
@@ -240,10 +240,3 @@ pub(crate) fn write_ifmt(input: &IfmtInput, writable: &mut impl Write) -> std::f
|
|||||||
let display = DisplayIfmt(input);
|
let display = DisplayIfmt(input);
|
||||||
write!(writable, "{}", display)
|
write!(writable, "{}", display)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Call rustfmt to format code, i32 as exitcode
|
|
||||||
pub fn rustfmt(input: &str) -> Option<String> {
|
|
||||||
let syntax_tree = syn::parse_file(input).ok()?;
|
|
||||||
let output = prettyplease::unparse(&syntax_tree);
|
|
||||||
Some(output)
|
|
||||||
}
|
|
||||||
@@ -75,6 +75,10 @@ toml_edit = "0.21.0"
|
|||||||
# bundling
|
# bundling
|
||||||
tauri-bundler = { version = "=1.4.*", features = ["native-tls-vendored"] }
|
tauri-bundler = { version = "=1.4.*", features = ["native-tls-vendored"] }
|
||||||
|
|
||||||
|
# formatting
|
||||||
|
syn = { version = "2.0" }
|
||||||
|
prettyplease = { workspace = true }
|
||||||
|
|
||||||
manganis-cli-support = { workspace = true, features = ["webp", "html"] }
|
manganis-cli-support = { workspace = true, features = ["webp", "html"] }
|
||||||
|
|
||||||
dioxus-autofmt = { workspace = true }
|
dioxus-autofmt = { workspace = true }
|
||||||
|
|||||||
@@ -33,8 +33,7 @@ pub struct Autoformat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Autoformat {
|
impl Autoformat {
|
||||||
// Todo: autoformat the entire crate
|
pub fn autoformat(self) -> Result<()> {
|
||||||
pub async fn autoformat(self) -> Result<()> {
|
|
||||||
let Autoformat {
|
let Autoformat {
|
||||||
check,
|
check,
|
||||||
raw,
|
raw,
|
||||||
@@ -46,7 +45,7 @@ impl Autoformat {
|
|||||||
|
|
||||||
// Default to formatting the project
|
// Default to formatting the project
|
||||||
if raw.is_none() && file.is_none() {
|
if raw.is_none() && file.is_none() {
|
||||||
if let Err(e) = autoformat_project(check, split_line_attributes, do_rustfmt).await {
|
if let Err(e) = autoformat_project(check, split_line_attributes, do_rustfmt) {
|
||||||
eprintln!("error formatting project: {}", e);
|
eprintln!("error formatting project: {}", e);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
@@ -87,7 +86,7 @@ fn refactor_file(file: String, split_line_attributes: bool, do_rustfmt: bool) ->
|
|||||||
};
|
};
|
||||||
|
|
||||||
if do_rustfmt {
|
if do_rustfmt {
|
||||||
s = dioxus_autofmt::rustfmt(&s).ok_or_else(|| Error::ParseError("Syntax Error".into()))?;
|
s = rustfmt(&s)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let edits = dioxus_autofmt::fmt_file(&s, indent);
|
let edits = dioxus_autofmt::fmt_file(&s, indent);
|
||||||
@@ -122,8 +121,8 @@ fn format_file(path: impl AsRef<Path>, indent: IndentOptions, do_rustfmt: bool)
|
|||||||
let mut contents = fs::read_to_string(&path)?;
|
let mut contents = fs::read_to_string(&path)?;
|
||||||
let mut if_write = false;
|
let mut if_write = false;
|
||||||
if do_rustfmt {
|
if do_rustfmt {
|
||||||
let formatted = dioxus_autofmt::rustfmt(&contents)
|
let formatted = rustfmt(&contents)
|
||||||
.ok_or_else(|| Error::ParseError("Syntax Error".into()))?;
|
.map_err(|err| Error::ParseError(format!("Syntax Error:\n{}", err)))?;
|
||||||
if contents != formatted {
|
if contents != formatted {
|
||||||
if_write = true;
|
if_write = true;
|
||||||
contents = formatted;
|
contents = formatted;
|
||||||
@@ -150,11 +149,7 @@ fn format_file(path: impl AsRef<Path>, indent: IndentOptions, do_rustfmt: bool)
|
|||||||
/// Runs using rayon for multithreading, so it should be really really fast
|
/// Runs using rayon for multithreading, so it should be really really fast
|
||||||
///
|
///
|
||||||
/// Doesn't do mod-descending, so it will still try to format unreachable files. TODO.
|
/// Doesn't do mod-descending, so it will still try to format unreachable files. TODO.
|
||||||
async fn autoformat_project(
|
fn autoformat_project(check: bool, split_line_attributes: bool, do_rustfmt: bool) -> Result<()> {
|
||||||
check: bool,
|
|
||||||
split_line_attributes: bool,
|
|
||||||
do_rustfmt: bool,
|
|
||||||
) -> Result<()> {
|
|
||||||
let files_to_format = get_project_files();
|
let files_to_format = get_project_files();
|
||||||
|
|
||||||
if files_to_format.is_empty() {
|
if files_to_format.is_empty() {
|
||||||
@@ -238,6 +233,23 @@ fn indentation_for(
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Format rust code using prettyplease
|
||||||
|
pub fn rustfmt(input: &str) -> Result<String> {
|
||||||
|
let syntax_tree = syn::parse_file(input).map_err(format_syn_error)?;
|
||||||
|
let output = prettyplease::unparse(&syntax_tree);
|
||||||
|
Ok(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn format_syn_error(err: syn::Error) -> Error {
|
||||||
|
let start = err.span().start();
|
||||||
|
let line = start.line;
|
||||||
|
let column = start.column;
|
||||||
|
Error::ParseError(format!(
|
||||||
|
"Syntax Error in line {} column {}:\n{}",
|
||||||
|
line, column, err
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_auto_fmt() {
|
async fn test_auto_fmt() {
|
||||||
let test_rsx = r#"
|
let test_rsx = r#"
|
||||||
|
|||||||
@@ -70,7 +70,6 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
Autoformat(opts) => opts
|
Autoformat(opts) => opts
|
||||||
.autoformat()
|
.autoformat()
|
||||||
.await
|
|
||||||
.context(error_wrapper("Error autoformatting RSX")),
|
.context(error_wrapper("Error autoformatting RSX")),
|
||||||
|
|
||||||
Check(opts) => opts
|
Check(opts) => opts
|
||||||
|
|||||||
Reference in new issue
Block a user