From 9b1cb43cbd7b9fcfe542327181b9698a1bcbfb8f Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Sun, 08 Feb 2026 21:03:23 +0000 Subject: [PATCH] fix(fmt): differentiate between nodes of the same type --- --- /dev/null +++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__asserts.snap @@ -0,0 +1,9 @@ +--- +source: crates/jrsonnet-formatter/src/tests.rs +expression: "reformat(indoc!(\"\n\t\t\t{\n\t\t\t\tassert 1 > 0 : 'one should be greater than zero',\n\t\t\t\tassert true,\n\t\t\t\tvalue: 42,\n\t\t\t}\n\t\t\"))" +--- +{ + assert 1 > 0: 'one should be greater than zero', + assert true, + value: 42, +} --- a/crates/jrsonnet-formatter/src/tests.rs +++ b/crates/jrsonnet-formatter/src/tests.rs @@ -91,3 +91,16 @@ " ))); } + +#[test] +fn asserts() { + insta::assert_snapshot!(reformat(indoc!( + " + { + assert 1 > 0 : 'one should be greater than zero', + assert true, + value: 42, + } + " + ))); +} --- a/crates/jrsonnet-rowan-parser/src/generated/nodes.rs +++ b/crates/jrsonnet-rowan-parser/src/generated/nodes.rs @@ -14,7 +14,7 @@ } impl SourceFile { pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -27,7 +27,7 @@ support::children(&self.syntax) } pub fn expr_base(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn suffixs(&self) -> AstChildren { support::children(&self.syntax) @@ -46,7 +46,7 @@ support::token(&self.syntax, T![.]) } pub fn index(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -75,7 +75,7 @@ support::token(&self.syntax, T!['[']) } pub fn index(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn r_brack_token(&self) -> Option { support::token(&self.syntax, T![']']) @@ -88,7 +88,7 @@ } impl SuffixSlice { pub fn slice_desc(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -101,16 +101,16 @@ support::token(&self.syntax, T!['[']) } pub fn from(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn colon_token(&self) -> Option { support::token(&self.syntax, T![:]) } pub fn end(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn step(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn r_brack_token(&self) -> Option { support::token(&self.syntax, T![']']) @@ -123,7 +123,7 @@ } impl SuffixApply { pub fn args_desc(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn tailstrict_kw_token(&self) -> Option { support::token(&self.syntax, T![tailstrict]) @@ -168,7 +168,7 @@ } impl StmtAssert { pub fn assertion(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn semi_token(&self) -> Option { support::token(&self.syntax, T![;]) @@ -184,13 +184,13 @@ support::token(&self.syntax, T![assert]) } pub fn condition(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn colon_token(&self) -> Option { support::token(&self.syntax, T![:]) } pub fn message(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).nth(1usize) } } @@ -200,13 +200,13 @@ } impl ExprBinary { pub fn lhs(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn binary_operator(&self) -> Option { support::token_child(&self.syntax) } pub fn rhs(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).nth(1usize) } } @@ -219,7 +219,7 @@ support::token_child(&self.syntax) } pub fn rhs(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -229,7 +229,7 @@ } impl ExprObjExtend { pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -242,7 +242,7 @@ support::token(&self.syntax, T!['(']) } pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn r_paren_token(&self) -> Option { support::token(&self.syntax, T![')']) @@ -301,7 +301,7 @@ } impl ExprObject { pub fn obj_body(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -314,7 +314,7 @@ support::token(&self.syntax, T!['[']) } pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn comma_token(&self) -> Option { support::token(&self.syntax, T![,]) @@ -346,7 +346,7 @@ } impl ExprVar { pub fn name(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -359,19 +359,19 @@ support::token(&self.syntax, T![if]) } pub fn cond(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn then_kw_token(&self) -> Option { support::token(&self.syntax, T![then]) } pub fn then(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn else_kw_token(&self) -> Option { support::token(&self.syntax, T![else]) } pub fn else_(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -381,7 +381,7 @@ } impl TrueExpr { pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -391,7 +391,7 @@ } impl FalseExpr { pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -407,13 +407,13 @@ support::token(&self.syntax, T!['(']) } pub fn params_desc(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn r_paren_token(&self) -> Option { support::token(&self.syntax, T![')']) } pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -442,7 +442,7 @@ support::token(&self.syntax, T![error]) } pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -452,7 +452,7 @@ } impl SliceDescEnd { pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -462,7 +462,7 @@ } impl SliceDescStep { pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -472,13 +472,13 @@ } impl Arg { pub fn name(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn assign_token(&self) -> Option { support::token(&self.syntax, T![=]) } pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -523,7 +523,7 @@ } impl MemberBindStmt { pub fn obj_local(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -536,7 +536,7 @@ support::token(&self.syntax, T![local]) } pub fn bind(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -546,7 +546,7 @@ } impl MemberAssertStmt { pub fn assertion(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -556,7 +556,7 @@ } impl MemberFieldNormal { pub fn field_name(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn plus_token(&self) -> Option { support::token(&self.syntax, T![+]) @@ -565,7 +565,7 @@ support::token_child(&self.syntax) } pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -575,16 +575,16 @@ } impl MemberFieldMethod { pub fn field_name(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn params_desc(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn visibility(&self) -> Option { support::token_child(&self.syntax) } pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -594,7 +594,7 @@ } impl FieldNameFixed { pub fn id(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn text(&self) -> Option { support::token_child(&self.syntax) @@ -610,7 +610,7 @@ support::token(&self.syntax, T!['[']) } pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn r_brack_token(&self) -> Option { support::token(&self.syntax, T![']']) @@ -626,13 +626,13 @@ support::token(&self.syntax, T![for]) } pub fn bind(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn in_kw_token(&self) -> Option { support::token(&self.syntax, T![in]) } pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -645,7 +645,7 @@ support::token(&self.syntax, T![if]) } pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -655,13 +655,13 @@ } impl BindDestruct { pub fn into(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn assign_token(&self) -> Option { support::token(&self.syntax, T![=]) } pub fn value(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -671,16 +671,16 @@ } impl BindFunction { pub fn name(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn params(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn assign_token(&self) -> Option { support::token(&self.syntax, T![=]) } pub fn value(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -690,13 +690,13 @@ } impl Param { pub fn destruct(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn assign_token(&self) -> Option { support::token(&self.syntax, T![=]) } pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -706,7 +706,7 @@ } impl DestructFull { pub fn name(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -748,7 +748,7 @@ support::children(&self.syntax) } pub fn destruct_rest(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn comma_token(&self) -> Option { support::token(&self.syntax, T![,]) @@ -764,19 +764,19 @@ } impl DestructObjectField { pub fn field(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn colon_token(&self) -> Option { support::token(&self.syntax, T![:]) } pub fn destruct(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } pub fn assign_token(&self) -> Option { support::token(&self.syntax, T![=]) } pub fn expr(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -789,7 +789,7 @@ support::token(&self.syntax, T![...]) } pub fn into(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } @@ -799,7 +799,7 @@ } impl DestructArrayElement { pub fn destruct(&self) -> Option { - support::child(&self.syntax) + support::children(&self.syntax).next() } } --- a/xtask/src/sourcegen/mod.rs +++ b/xtask/src/sourcegen/mod.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::{collections::HashMap, path::PathBuf}; use anyhow::Result; use ast::{lower, AstSrc}; @@ -202,7 +202,15 @@ quote!(impl ast::#trait_name for #name {}) }); - let methods = node.fields.iter().map(|field| { + let mut type_positions: HashMap = HashMap::new(); + let field_positions: Vec<_> = node.fields.iter().map(|field| { + let ty_str = field.ty().to_string(); + let pos = *type_positions.get(&ty_str).unwrap_or(&0); + type_positions.insert(ty_str, pos + 1); + pos + }).collect(); + + let methods = node.fields.iter().zip(field_positions.iter()).map(|(field, &pos)| { let method_name = field.method_name(kinds); let ty = field.ty(); @@ -224,10 +232,16 @@ support::token_child(&self.syntax) } } + } else if pos == 0 { + quote! { + pub fn #method_name(&self) -> Option<#ty> { + support::children(&self.syntax).next() + } + } } else { quote! { pub fn #method_name(&self) -> Option<#ty> { - support::child(&self.syntax) + support::children(&self.syntax).nth(#pos) } } } -- gitstuff