mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-16 01:25:11 +00:00
Integrate the new Jupyter AST nodes in Ruff (#6086)
## Summary This PR adds the implementation for the new Jupyter AST nodes i.e., `ExprLineMagic` and `StmtLineMagic`. ## Test Plan Add test cases for `unparse` containing magic commands resolves: #6087
This commit is contained in:
parent
1fdadee59c
commit
025fa4eba8
14 changed files with 228 additions and 19 deletions
|
@ -652,6 +652,12 @@ pub struct ExprSlice<'a> {
|
|||
step: Option<Box<ComparableExpr<'a>>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ExprLineMagic<'a> {
|
||||
kind: ast::MagicKind,
|
||||
value: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ComparableExpr<'a> {
|
||||
BoolOp(ExprBoolOp<'a>),
|
||||
|
@ -681,6 +687,7 @@ pub enum ComparableExpr<'a> {
|
|||
List(ExprList<'a>),
|
||||
Tuple(ExprTuple<'a>),
|
||||
Slice(ExprSlice<'a>),
|
||||
LineMagic(ExprLineMagic<'a>),
|
||||
}
|
||||
|
||||
impl<'a> From<&'a Box<ast::Expr>> for Box<ComparableExpr<'a>> {
|
||||
|
@ -925,6 +932,14 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
|
|||
upper: upper.as_ref().map(Into::into),
|
||||
step: step.as_ref().map(Into::into),
|
||||
}),
|
||||
ast::Expr::LineMagic(ast::ExprLineMagic {
|
||||
kind,
|
||||
value,
|
||||
range: _range,
|
||||
}) => Self::LineMagic(ExprLineMagic {
|
||||
kind: *kind,
|
||||
value: value.as_str(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1155,6 +1170,12 @@ pub struct StmtExpr<'a> {
|
|||
value: ComparableExpr<'a>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StmtLineMagic<'a> {
|
||||
kind: ast::MagicKind,
|
||||
value: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ComparableStmt<'a> {
|
||||
FunctionDef(StmtFunctionDef<'a>),
|
||||
|
@ -1181,6 +1202,7 @@ pub enum ComparableStmt<'a> {
|
|||
ImportFrom(StmtImportFrom<'a>),
|
||||
Global(StmtGlobal<'a>),
|
||||
Nonlocal(StmtNonlocal<'a>),
|
||||
LineMagic(StmtLineMagic<'a>),
|
||||
Expr(StmtExpr<'a>),
|
||||
Pass,
|
||||
Break,
|
||||
|
@ -1440,6 +1462,14 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
}) => Self::Nonlocal(StmtNonlocal {
|
||||
names: names.iter().map(ast::Identifier::as_str).collect(),
|
||||
}),
|
||||
ast::Stmt::LineMagic(ast::StmtLineMagic {
|
||||
kind,
|
||||
value,
|
||||
range: _range,
|
||||
}) => Self::LineMagic(StmtLineMagic {
|
||||
kind: *kind,
|
||||
value: value.as_str(),
|
||||
}),
|
||||
ast::Stmt::Expr(ast::StmtExpr {
|
||||
value,
|
||||
range: _range,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue