From f89e94cc33c28e503e4f0019a2028c991b049672 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sat, 6 Jan 2024 10:40:25 -0500 Subject: [PATCH] Pass format tests --- examples/assets/read-data.js | 6 ------ src/abstract_tree/block.rs | 24 +++++------------------- src/abstract_tree/mod.rs | 2 +- src/abstract_tree/value_node.rs | 15 +++++++++------ src/value/function.rs | 2 -- tests/format.rs | 20 ++++++++++++-------- 6 files changed, 27 insertions(+), 42 deletions(-) delete mode 100644 examples/assets/read-data.js diff --git a/examples/assets/read-data.js b/examples/assets/read-data.js deleted file mode 100644 index 7fbfd98..0000000 --- a/examples/assets/read-data.js +++ /dev/null @@ -1,6 +0,0 @@ -const fs = require('node:fs'); -const data = fs.readFileSync("examples/assets/jq_data.json"); -const items = JSON.parse(data); -const output = items.map((item) => item:commit:committer:name); - -console.log(output) diff --git a/src/abstract_tree/block.rs b/src/abstract_tree/block.rs index 1f10e35..5d16484 100644 --- a/src/abstract_tree/block.rs +++ b/src/abstract_tree/block.rs @@ -4,7 +4,7 @@ use rayon::prelude::*; use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Expression, Format, Map, Result, Statement, Type, Value}; +use crate::{AbstractTree, Error, Format, Map, Result, Statement, Type, Value}; /// Abstract representation of a block. /// @@ -136,26 +136,12 @@ impl Format for Block { output.push_str("{\n"); } - let mut previous: Option<&Statement> = None; - - for next_statement in &self.statements { - if let Some(previous_statement) = &previous { - match previous_statement { - Statement::While(_) | Statement::Expression(Expression::FunctionCall(_)) => { - output.push('\n'); - } - _ => {} - } + for (index, statement) in self.statements.iter().enumerate() { + if index > 0 { + output.push('\n'); } - match next_statement { - Statement::Block(_) | Statement::While(_) => output.push('\n'), - _ => {} - } - - next_statement.format(output, indent_level + 1); - - previous = Some(next_statement); + statement.format(output, indent_level + 1); } output.push('\n'); diff --git a/src/abstract_tree/mod.rs b/src/abstract_tree/mod.rs index 4f09ed6..0743f0b 100644 --- a/src/abstract_tree/mod.rs +++ b/src/abstract_tree/mod.rs @@ -124,7 +124,7 @@ impl AbstractTree for Root { impl Format for Root { fn format(&self, output: &mut String, indent_level: u8) { for (index, statement) in self.statements.iter().enumerate() { - if index > 1 { + if index > 0 { output.push('\n'); } statement.format(output, indent_level); diff --git a/src/abstract_tree/value_node.rs b/src/abstract_tree/value_node.rs index 3309751..0c742ff 100644 --- a/src/abstract_tree/value_node.rs +++ b/src/abstract_tree/value_node.rs @@ -332,24 +332,27 @@ impl Format for ValueNode { } } ValueNode::Map(nodes) => { - output.push('{'); + output.push_str("{\n"); for (key, (statement, type_option)) in nodes { if let Some(r#type) = type_option { - output.push_str(" "); + ValueNode::indent(output, indent_level + 1); output.push_str(key); output.push_str(" <"); - r#type.format(output, indent_level); + r#type.format(output, 0); output.push_str("> = "); - statement.format(output, indent_level); + statement.format(output, 0); } else { - output.push_str(" "); + ValueNode::indent(output, indent_level + 1); output.push_str(key); output.push_str(" = "); - statement.format(output, indent_level); + statement.format(output, 0); } + + output.push('\n'); } + ValueNode::indent(output, indent_level); output.push('}'); } ValueNode::BuiltInValue(built_in_value) => built_in_value.format(output, indent_level), diff --git a/src/value/function.rs b/src/value/function.rs index 107a87e..b1ae934 100644 --- a/src/value/function.rs +++ b/src/value/function.rs @@ -44,8 +44,6 @@ impl Format for Function { Function::BuiltIn(built_in_function) => built_in_function.format(output, indent_level), Function::ContextDefined(function_node) => function_node.format(output, indent_level), } - - output.push('\n'); } } diff --git a/tests/format.rs b/tests/format.rs index 61a50e5..e4f0a55 100644 --- a/tests/format.rs +++ b/tests/format.rs @@ -6,14 +6,15 @@ fn format_simple_program() { interpreter.run("x=1").unwrap(); - assert_eq!(interpreter.format(), "x = 1"); + assert_eq!(interpreter.format(), "x = 1\n"); } const FORMATTED_BLOCK: &str = "{ 1 2 3 -}"; +} +"; #[test] fn format_block() { @@ -24,24 +25,27 @@ fn format_block() { assert_eq!(FORMATTED_BLOCK, interpreter.format()); } -const FORMATTED_NESTED_BLOCK: &str = "{ +const FORMATTED_MAP: &str = "{ { x = 1 + y = 2 } -}"; +} +"; #[test] -fn format_nested_block() { +fn format_map() { let mut interpreter = Interpreter::new(Map::new()); - interpreter.run("{{x=1}}").unwrap(); + interpreter.run("{{x=1 y = 2}}").unwrap(); - assert_eq!(FORMATTED_NESTED_BLOCK, interpreter.format()); + assert_eq!(FORMATTED_MAP, interpreter.format()); } const FORMATTED_FUNCTION: &str = "(x ) { x / 2 -}"; +} +"; #[test] fn format_function() {