Add required array element constraints

This commit is contained in:
2026-08-11 17:24:04 +09:00
parent 5a05da6fe4
commit 46b2b3b1b6
35 changed files with 6328 additions and 5042 deletions
+21 -2
View File
@@ -427,7 +427,7 @@ impl<'a> Formatter<'a> {
if expression_contains_comment(node)
&& !matches!(
node.kind(),
"object" | "array" | "let_expression" | "match_expression"
"object" | "array" | "array_constraint" | "let_expression" | "match_expression"
)
{
out.push_str(self.raw_trimmed(node));
@@ -454,6 +454,7 @@ impl<'a> Formatter<'a> {
"comparison_constraint" => self.write_comparison_constraint(out, node, indent),
"object" => self.write_object(out, node, indent),
"array" => self.write_array(out, node, indent),
"array_constraint" => self.write_array_constraint(out, node, indent),
"let_expression" => self.write_let(out, node, indent),
"function_expression" => self.write_function(out, node, indent),
"match_expression" => self.write_match(out, node, indent),
@@ -541,6 +542,14 @@ impl<'a> Formatter<'a> {
out.push(']');
}
fn write_array_constraint(&mut self, out: &mut String, node: Node<'a>, indent: usize) {
out.push_str("[...");
if let Some(element) = node.child_by_field_name("element") {
self.write_expr(out, element, indent, 0);
}
out.push(']');
}
fn write_let(&mut self, out: &mut String, node: Node<'a>, indent: usize) {
let body = node.child_by_field_name("body");
out.push_str("let");
@@ -797,7 +806,7 @@ fn is_inline_expr(node: Node<'_>) -> bool {
!expression_contains_comment(node)
&& named_children(node).into_iter().all(is_inline_expr)
}
"array" => {
"array" | "array_constraint" => {
!expression_contains_comment(node)
&& named_children(node).into_iter().all(is_inline_expr)
}
@@ -842,6 +851,16 @@ mod tests {
);
}
#[test]
fn formats_array_constraints() {
let source = "tags=[...String];ports=[...(Int&>=1)];";
let formatted = format_source(source).unwrap();
assert_eq!(
formatted,
"tags = [...String];\nports = [...(Int & >= 1)];\n"
);
}
#[test]
fn wasm_export_returns_json() {
let output = format_source_json("value={a=1;};");