Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 96 additions & 1 deletion crates/syntax/src/ast/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,19 @@ pub fn item_static(
}

pub fn unnamed_param(ty: ast::Type) -> ast::Param {
ast_from_text(&format!("fn f({ty}) {{ }}"))
quote! {
Param {
#ty
}
}
}

pub fn untyped_param(pat: ast::Pat) -> ast::Param {
quote! {
Param {
#pat
}
}
}

pub fn param(pat: ast::Pat, ty: ast::Type) -> ast::Param {
Expand Down Expand Up @@ -1456,3 +1468,86 @@ pub mod tokens {
}
}
}

#[cfg(test)]
mod tests {
use expect_test::expect;

use super::*;

#[track_caller]
fn check(node: impl AstNode, expect: expect_test::Expect) {
let node_debug = format!("{:#?}", node.syntax());
expect.assert_eq(&node_debug);
}

#[test]
fn test_unnamed_param() {
check(
unnamed_param(ty("Vec")),
expect![[r#"
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "Vec"
"#]],
);

check(
unnamed_param(ty("Vec<T>")),
expect![[r#"
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "Vec"
[email protected]
[email protected] "<"
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "T"
[email protected] ">"
"#]],
);
}

#[test]
fn test_untyped_param() {
check(
untyped_param(path_pat(ext::ident_path("name"))),
expect![[r#"
[email protected]
[email protected]
[email protected]
[email protected] "name"
"#]],
);

check(
untyped_param(
range_pat(
Some(path_pat(ext::ident_path("start"))),
Some(path_pat(ext::ident_path("end"))),
)
.into(),
),
expect![[r#"
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "start"
[email protected] ".."
[email protected]
[email protected]
[email protected] "end"
"#]],
);
}
}