mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:39:12 +00:00
Add as_group
methods to AnyNodeRef
(#17048)
## Summary This PR adds `as_<group>` methods to `AnyNodeRef` to e.g. convert an `AnyNodeRef` to an `ExprRef`. I need this for go to definition where the fallback is to test if `AnyNodeRef` is an expression and then call `inferred_type` (listing this mapping at every call site where we need to convert `AnyNodeRef` to an `ExprRef` is a bit painful ;)) Split out from https://github.com/astral-sh/ruff/pull/16901 ## Test Plan `cargo test`
This commit is contained in:
parent
050f332771
commit
e07741e553
2 changed files with 153 additions and 0 deletions
|
@ -525,6 +525,23 @@ def write_anynoderef(out: list[str], ast: Ast) -> None:
|
|||
}
|
||||
""")
|
||||
|
||||
# `as_*` methods to convert from `AnyNodeRef` to e.g. `ExprRef`
|
||||
out.append(f"""
|
||||
impl<'a> AnyNodeRef<'a> {{
|
||||
pub fn as_{to_snake_case(group.ref_enum_ty)}(self) -> Option<{group.ref_enum_ty}<'a>> {{
|
||||
match self {{
|
||||
""")
|
||||
for node in group.nodes:
|
||||
out.append(
|
||||
f"Self::{node.name}(node) => Some({group.ref_enum_ty}::{node.variant}(node)),"
|
||||
)
|
||||
out.append("""
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
""")
|
||||
|
||||
for node in ast.all_nodes:
|
||||
out.append(f"""
|
||||
impl<'a> From<&'a {node.ty}> for AnyNodeRef<'a> {{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue