Skip to content

Commit

Permalink
Implement AnyNode and AnyNodRef for StmtTypeAlias (#5863)
Browse files Browse the repository at this point in the history
Part of #5062
  • Loading branch information
zanieb authored Jul 18, 2023
1 parent c868def commit 0eab4b3
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions crates/ruff_python_ast/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum AnyNode {
StmtClassDef(ast::StmtClassDef),
StmtReturn(ast::StmtReturn),
StmtDelete(ast::StmtDelete),
StmtTypeAlias(ast::StmtTypeAlias),
StmtAssign(ast::StmtAssign),
StmtAugAssign(ast::StmtAugAssign),
StmtAnnAssign(ast::StmtAnnAssign),
Expand Down Expand Up @@ -109,6 +110,7 @@ impl AnyNode {
AnyNode::StmtClassDef(node) => Some(Stmt::ClassDef(node)),
AnyNode::StmtReturn(node) => Some(Stmt::Return(node)),
AnyNode::StmtDelete(node) => Some(Stmt::Delete(node)),
AnyNode::StmtTypeAlias(node) => Some(Stmt::TypeAlias(node)),
AnyNode::StmtAssign(node) => Some(Stmt::Assign(node)),
AnyNode::StmtAugAssign(node) => Some(Stmt::AugAssign(node)),
AnyNode::StmtAnnAssign(node) => Some(Stmt::AnnAssign(node)),
Expand Down Expand Up @@ -225,6 +227,7 @@ impl AnyNode {
| AnyNode::StmtClassDef(_)
| AnyNode::StmtReturn(_)
| AnyNode::StmtDelete(_)
| AnyNode::StmtTypeAlias(_)
| AnyNode::StmtAssign(_)
| AnyNode::StmtAugAssign(_)
| AnyNode::StmtAnnAssign(_)
Expand Down Expand Up @@ -282,6 +285,7 @@ impl AnyNode {
| AnyNode::StmtClassDef(_)
| AnyNode::StmtReturn(_)
| AnyNode::StmtDelete(_)
| AnyNode::StmtTypeAlias(_)
| AnyNode::StmtAssign(_)
| AnyNode::StmtAugAssign(_)
| AnyNode::StmtAnnAssign(_)
Expand Down Expand Up @@ -374,6 +378,7 @@ impl AnyNode {
| AnyNode::StmtClassDef(_)
| AnyNode::StmtReturn(_)
| AnyNode::StmtDelete(_)
| AnyNode::StmtTypeAlias(_)
| AnyNode::StmtAssign(_)
| AnyNode::StmtAugAssign(_)
| AnyNode::StmtAnnAssign(_)
Expand Down Expand Up @@ -451,6 +456,7 @@ impl AnyNode {
| AnyNode::StmtClassDef(_)
| AnyNode::StmtReturn(_)
| AnyNode::StmtDelete(_)
| AnyNode::StmtTypeAlias(_)
| AnyNode::StmtAssign(_)
| AnyNode::StmtAugAssign(_)
| AnyNode::StmtAnnAssign(_)
Expand Down Expand Up @@ -535,6 +541,7 @@ impl AnyNode {
| AnyNode::StmtClassDef(_)
| AnyNode::StmtReturn(_)
| AnyNode::StmtDelete(_)
| AnyNode::StmtTypeAlias(_)
| AnyNode::StmtAssign(_)
| AnyNode::StmtAugAssign(_)
| AnyNode::StmtAnnAssign(_)
Expand Down Expand Up @@ -641,6 +648,7 @@ impl AnyNode {
Self::StmtClassDef(node) => AnyNodeRef::StmtClassDef(node),
Self::StmtReturn(node) => AnyNodeRef::StmtReturn(node),
Self::StmtDelete(node) => AnyNodeRef::StmtDelete(node),
Self::StmtTypeAlias(node) => AnyNodeRef::StmtTypeAlias(node),
Self::StmtAssign(node) => AnyNodeRef::StmtAssign(node),
Self::StmtAugAssign(node) => AnyNodeRef::StmtAugAssign(node),
Self::StmtAnnAssign(node) => AnyNodeRef::StmtAnnAssign(node),
Expand Down Expand Up @@ -971,6 +979,34 @@ impl AstNode for ast::StmtDelete {
AnyNode::from(self)
}
}
impl AstNode for ast::StmtTypeAlias {
fn cast(kind: AnyNode) -> Option<Self>
where
Self: Sized,
{
if let AnyNode::StmtTypeAlias(node) = kind {
Some(node)
} else {
None
}
}

fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
if let AnyNodeRef::StmtTypeAlias(node) = kind {
Some(node)
} else {
None
}
}

fn as_any_node_ref(&self) -> AnyNodeRef {
AnyNodeRef::from(self)
}

fn into_any_node(self) -> AnyNode {
AnyNode::from(self)
}
}
impl AstNode for ast::StmtAssign {
fn cast(kind: AnyNode) -> Option<Self>
where
Expand Down Expand Up @@ -2914,6 +2950,7 @@ impl From<Stmt> for AnyNode {
Stmt::ClassDef(node) => AnyNode::StmtClassDef(node),
Stmt::Return(node) => AnyNode::StmtReturn(node),
Stmt::Delete(node) => AnyNode::StmtDelete(node),
Stmt::TypeAlias(node) => AnyNode::StmtTypeAlias(node),
Stmt::Assign(node) => AnyNode::StmtAssign(node),
Stmt::AugAssign(node) => AnyNode::StmtAugAssign(node),
Stmt::AnnAssign(node) => AnyNode::StmtAnnAssign(node),
Expand All @@ -2936,7 +2973,6 @@ impl From<Stmt> for AnyNode {
Stmt::Pass(node) => AnyNode::StmtPass(node),
Stmt::Break(node) => AnyNode::StmtBreak(node),
Stmt::Continue(node) => AnyNode::StmtContinue(node),
Stmt::TypeAlias(_) => todo!(),
}
}
}
Expand Down Expand Up @@ -3071,6 +3107,12 @@ impl From<ast::StmtDelete> for AnyNode {
}
}

impl From<ast::StmtTypeAlias> for AnyNode {
fn from(node: ast::StmtTypeAlias) -> Self {
AnyNode::StmtTypeAlias(node)
}
}

impl From<ast::StmtAssign> for AnyNode {
fn from(node: ast::StmtAssign) -> Self {
AnyNode::StmtAssign(node)
Expand Down Expand Up @@ -3489,6 +3531,7 @@ impl Ranged for AnyNode {
AnyNode::StmtClassDef(node) => node.range(),
AnyNode::StmtReturn(node) => node.range(),
AnyNode::StmtDelete(node) => node.range(),
AnyNode::StmtTypeAlias(node) => node.range(),
AnyNode::StmtAssign(node) => node.range(),
AnyNode::StmtAugAssign(node) => node.range(),
AnyNode::StmtAnnAssign(node) => node.range(),
Expand Down Expand Up @@ -3573,6 +3616,7 @@ pub enum AnyNodeRef<'a> {
StmtClassDef(&'a ast::StmtClassDef),
StmtReturn(&'a ast::StmtReturn),
StmtDelete(&'a ast::StmtDelete),
StmtTypeAlias(&'a ast::StmtTypeAlias),
StmtAssign(&'a ast::StmtAssign),
StmtAugAssign(&'a ast::StmtAugAssign),
StmtAnnAssign(&'a ast::StmtAnnAssign),
Expand Down Expand Up @@ -3656,6 +3700,7 @@ impl AnyNodeRef<'_> {
AnyNodeRef::StmtClassDef(node) => NonNull::from(*node).cast(),
AnyNodeRef::StmtReturn(node) => NonNull::from(*node).cast(),
AnyNodeRef::StmtDelete(node) => NonNull::from(*node).cast(),
AnyNodeRef::StmtTypeAlias(node) => NonNull::from(*node).cast(),
AnyNodeRef::StmtAssign(node) => NonNull::from(*node).cast(),
AnyNodeRef::StmtAugAssign(node) => NonNull::from(*node).cast(),
AnyNodeRef::StmtAnnAssign(node) => NonNull::from(*node).cast(),
Expand Down Expand Up @@ -3745,6 +3790,7 @@ impl AnyNodeRef<'_> {
AnyNodeRef::StmtClassDef(_) => NodeKind::StmtClassDef,
AnyNodeRef::StmtReturn(_) => NodeKind::StmtReturn,
AnyNodeRef::StmtDelete(_) => NodeKind::StmtDelete,
AnyNodeRef::StmtTypeAlias(_) => NodeKind::StmtTypeAlias,
AnyNodeRef::StmtAssign(_) => NodeKind::StmtAssign,
AnyNodeRef::StmtAugAssign(_) => NodeKind::StmtAugAssign,
AnyNodeRef::StmtAnnAssign(_) => NodeKind::StmtAnnAssign,
Expand Down Expand Up @@ -3824,6 +3870,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::StmtClassDef(_)
| AnyNodeRef::StmtReturn(_)
| AnyNodeRef::StmtDelete(_)
| AnyNodeRef::StmtTypeAlias(_)
| AnyNodeRef::StmtAssign(_)
| AnyNodeRef::StmtAugAssign(_)
| AnyNodeRef::StmtAnnAssign(_)
Expand Down Expand Up @@ -3940,6 +3987,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::StmtClassDef(_)
| AnyNodeRef::StmtReturn(_)
| AnyNodeRef::StmtDelete(_)
| AnyNodeRef::StmtTypeAlias(_)
| AnyNodeRef::StmtAssign(_)
| AnyNodeRef::StmtAugAssign(_)
| AnyNodeRef::StmtAnnAssign(_)
Expand Down Expand Up @@ -3997,6 +4045,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::StmtClassDef(_)
| AnyNodeRef::StmtReturn(_)
| AnyNodeRef::StmtDelete(_)
| AnyNodeRef::StmtTypeAlias(_)
| AnyNodeRef::StmtAssign(_)
| AnyNodeRef::StmtAugAssign(_)
| AnyNodeRef::StmtAnnAssign(_)
Expand Down Expand Up @@ -4089,6 +4138,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::StmtClassDef(_)
| AnyNodeRef::StmtReturn(_)
| AnyNodeRef::StmtDelete(_)
| AnyNodeRef::StmtTypeAlias(_)
| AnyNodeRef::StmtAssign(_)
| AnyNodeRef::StmtAugAssign(_)
| AnyNodeRef::StmtAnnAssign(_)
Expand Down Expand Up @@ -4166,6 +4216,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::StmtClassDef(_)
| AnyNodeRef::StmtReturn(_)
| AnyNodeRef::StmtDelete(_)
| AnyNodeRef::StmtTypeAlias(_)
| AnyNodeRef::StmtAssign(_)
| AnyNodeRef::StmtAugAssign(_)
| AnyNodeRef::StmtAnnAssign(_)
Expand Down Expand Up @@ -4250,6 +4301,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::StmtClassDef(_)
| AnyNodeRef::StmtReturn(_)
| AnyNodeRef::StmtDelete(_)
| AnyNodeRef::StmtTypeAlias(_)
| AnyNodeRef::StmtAssign(_)
| AnyNodeRef::StmtAugAssign(_)
| AnyNodeRef::StmtAnnAssign(_)
Expand Down Expand Up @@ -4394,6 +4446,12 @@ impl<'a> From<&'a ast::StmtDelete> for AnyNodeRef<'a> {
}
}

impl<'a> From<&'a ast::StmtTypeAlias> for AnyNodeRef<'a> {
fn from(node: &'a ast::StmtTypeAlias) -> Self {
AnyNodeRef::StmtTypeAlias(node)
}
}

impl<'a> From<&'a ast::StmtAssign> for AnyNodeRef<'a> {
fn from(node: &'a ast::StmtAssign) -> Self {
AnyNodeRef::StmtAssign(node)
Expand Down Expand Up @@ -4768,6 +4826,7 @@ impl<'a> From<&'a Stmt> for AnyNodeRef<'a> {
Stmt::ClassDef(node) => AnyNodeRef::StmtClassDef(node),
Stmt::Return(node) => AnyNodeRef::StmtReturn(node),
Stmt::Delete(node) => AnyNodeRef::StmtDelete(node),
Stmt::TypeAlias(node) => AnyNodeRef::StmtTypeAlias(node),
Stmt::Assign(node) => AnyNodeRef::StmtAssign(node),
Stmt::AugAssign(node) => AnyNodeRef::StmtAugAssign(node),
Stmt::AnnAssign(node) => AnyNodeRef::StmtAnnAssign(node),
Expand All @@ -4790,7 +4849,6 @@ impl<'a> From<&'a Stmt> for AnyNodeRef<'a> {
Stmt::Pass(node) => AnyNodeRef::StmtPass(node),
Stmt::Break(node) => AnyNodeRef::StmtBreak(node),
Stmt::Continue(node) => AnyNodeRef::StmtContinue(node),
Stmt::TypeAlias(_) => todo!(),
}
}
}
Expand Down Expand Up @@ -4926,6 +4984,7 @@ impl Ranged for AnyNodeRef<'_> {
AnyNodeRef::StmtClassDef(node) => node.range(),
AnyNodeRef::StmtReturn(node) => node.range(),
AnyNodeRef::StmtDelete(node) => node.range(),
AnyNodeRef::StmtTypeAlias(node) => node.range(),
AnyNodeRef::StmtAssign(node) => node.range(),
AnyNodeRef::StmtAugAssign(node) => node.range(),
AnyNodeRef::StmtAnnAssign(node) => node.range(),
Expand Down Expand Up @@ -5010,6 +5069,7 @@ pub enum NodeKind {
StmtClassDef,
StmtReturn,
StmtDelete,
StmtTypeAlias,
StmtAssign,
StmtAugAssign,
StmtAnnAssign,
Expand Down

0 comments on commit 0eab4b3

Please sign in to comment.