From 69ff651201a7a4e46f4ec80ad26ce46383000438 Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Fri, 15 Mar 2024 20:18:34 -0700 Subject: [PATCH] fix recast bug (#1746) Signed-off-by: Jess Frazelle --- src/wasm-lib/kcl/src/ast/types.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/wasm-lib/kcl/src/ast/types.rs b/src/wasm-lib/kcl/src/ast/types.rs index 1fb568c0e..ab883e2be 100644 --- a/src/wasm-lib/kcl/src/ast/types.rs +++ b/src/wasm-lib/kcl/src/ast/types.rs @@ -2519,7 +2519,17 @@ impl UnaryExpression { } fn recast(&self, options: &FormatOptions) -> String { - format!("{}{}", &self.operator, self.argument.recast(options, 0)) + match self.argument { + BinaryPart::Literal(_) + | BinaryPart::Identifier(_) + | BinaryPart::MemberExpression(_) + | BinaryPart::CallExpression(_) => { + format!("{}{}", &self.operator, self.argument.recast(options, 0)) + } + BinaryPart::BinaryExpression(_) | BinaryPart::UnaryExpression(_) => { + format!("{}({})", &self.operator, self.argument.recast(options, 0)) + } + } } pub async fn get_result( @@ -3774,6 +3784,25 @@ const firstExtrude = startSketchOn('XY') assert_eq!(recasted.trim(), some_program_string); } + #[tokio::test(flavor = "multi_thread")] + async fn test_recast_math_negate_parens() { + let some_program_string = r#"const wallMountL = 3.82 +const thickness = 0.5 + +startSketchOn('XY') + |> startProfileAt([0, 0], %) + |> line([0, -(wallMountL - thickness)], %) + |> line([0, -(5 - thickness)], %) + |> line([0, -(5 - 1)], %) + |> line([0, -(-5 - 1)], %)"#; + let tokens = crate::token::lexer(some_program_string); + let parser = crate::parser::Parser::new(tokens); + let program = parser.ast().unwrap(); + + let recasted = program.recast(&Default::default(), 0); + assert_eq!(recasted.trim(), some_program_string); + } + #[tokio::test(flavor = "multi_thread")] async fn test_recast_math_nested_parens() { let some_program_string = r#"const distance = 5