Skip to content

Commit 4fb242e

Browse files
committed
Fix make::unnamed_param result a untyped_param
- Add `make::untyped_param` - Add some basic make tests Example --- ```rust make::unnamed_param(make::ty("Vec<T>")), ``` **Before this PR** ```text PARAM@0..4 IDENT_PAT@0..4 NAME@0..4 IDENT@0..4 "Vec" ``` **After this PR** ```text PARAM@0..6 PATH_TYPE@0..6 PATH@0..6 PATH_SEGMENT@0..6 NAME_REF@0..3 IDENT@0..3 "Vec" GENERIC_ARG_LIST@3..6 L_ANGLE@3..4 "<" TYPE_ARG@4..5 PATH_TYPE@4..5 PATH@4..5 PATH_SEGMENT@4..5 NAME_REF@4..5 IDENT@4..5 "T" R_ANGLE@5..6 ">" ``` --- Assist: `Generate a type alias for function with unnamed params` ```rust fn foo$0(x: Vec<i32>) {} ``` **Before this PR** ```rust type FooFn = fn(Vec); fn foo(x: Vec<i32>) {} ``` **After this PR** ```rust type FooFn = fn(Vec<i32>); fn foo(x: Vec<i32>) {} ```
1 parent 4bf516e commit 4fb242e

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

crates/syntax/src/ast/make.rs

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,19 @@ pub fn item_static(
10161016
}
10171017

10181018
pub fn unnamed_param(ty: ast::Type) -> ast::Param {
1019-
ast_from_text(&format!("fn f({ty}) {{ }}"))
1019+
quote! {
1020+
Param {
1021+
#ty
1022+
}
1023+
}
1024+
}
1025+
1026+
pub fn untyped_param(pat: ast::Pat) -> ast::Param {
1027+
quote! {
1028+
Param {
1029+
#pat
1030+
}
1031+
}
10201032
}
10211033

10221034
pub fn param(pat: ast::Pat, ty: ast::Type) -> ast::Param {
@@ -1456,3 +1468,86 @@ pub mod tokens {
14561468
}
14571469
}
14581470
}
1471+
1472+
#[cfg(test)]
1473+
mod tests {
1474+
use expect_test::expect;
1475+
1476+
use super::*;
1477+
1478+
#[track_caller]
1479+
fn check(node: impl AstNode, expect: expect_test::Expect) {
1480+
let node_debug = format!("{:#?}", node.syntax());
1481+
expect.assert_eq(&node_debug);
1482+
}
1483+
1484+
#[test]
1485+
fn test_unnamed_param() {
1486+
check(
1487+
unnamed_param(ty("Vec")),
1488+
expect![[r#"
1489+
PARAM@0..3
1490+
PATH_TYPE@0..3
1491+
PATH@0..3
1492+
PATH_SEGMENT@0..3
1493+
NAME_REF@0..3
1494+
IDENT@0..3 "Vec"
1495+
"#]],
1496+
);
1497+
1498+
check(
1499+
unnamed_param(ty("Vec<T>")),
1500+
expect![[r#"
1501+
PARAM@0..6
1502+
PATH_TYPE@0..6
1503+
PATH@0..6
1504+
PATH_SEGMENT@0..6
1505+
NAME_REF@0..3
1506+
IDENT@0..3 "Vec"
1507+
GENERIC_ARG_LIST@3..6
1508+
L_ANGLE@3..4 "<"
1509+
TYPE_ARG@4..5
1510+
PATH_TYPE@4..5
1511+
PATH@4..5
1512+
PATH_SEGMENT@4..5
1513+
NAME_REF@4..5
1514+
IDENT@4..5 "T"
1515+
R_ANGLE@5..6 ">"
1516+
"#]],
1517+
);
1518+
}
1519+
1520+
#[test]
1521+
fn test_untyped_param() {
1522+
check(
1523+
untyped_param(path_pat(ext::ident_path("name"))),
1524+
expect![[r#"
1525+
PARAM@0..4
1526+
IDENT_PAT@0..4
1527+
NAME@0..4
1528+
IDENT@0..4 "name"
1529+
"#]],
1530+
);
1531+
1532+
check(
1533+
untyped_param(
1534+
range_pat(
1535+
Some(path_pat(ext::ident_path("start"))),
1536+
Some(path_pat(ext::ident_path("end"))),
1537+
)
1538+
.into(),
1539+
),
1540+
expect![[r#"
1541+
PARAM@0..10
1542+
RANGE_PAT@0..10
1543+
IDENT_PAT@0..5
1544+
NAME@0..5
1545+
IDENT@0..5 "start"
1546+
DOT2@5..7 ".."
1547+
IDENT_PAT@7..10
1548+
NAME@7..10
1549+
IDENT@7..10 "end"
1550+
"#]],
1551+
);
1552+
}
1553+
}

0 commit comments

Comments
 (0)