Remove prelude from ruff_python_ast (#5369)

## Summary

Per @MichaReiser, this is causing more confusion than it is helpful.
This commit is contained in:
Charlie Marsh 2023-06-26 11:43:49 -04:00 committed by GitHub
parent baa7264ca4
commit fa1b85b3da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 688 additions and 656 deletions

View file

@ -3,7 +3,7 @@ use rustpython_parser::ast::{Expr, Ranged};
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::prelude::Constant;
use rustpython_parser::ast::Constant;
use crate::checkers::ast::Checker;

View file

@ -2,7 +2,7 @@ use rustpython_parser::ast::Expr;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::prelude::Ranged;
use rustpython_parser::ast::Ranged;
use crate::checkers::ast::Checker;

View file

@ -3,8 +3,8 @@ use rustpython_parser::ast::{Ranged, Stmt};
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::prelude::Expr;
use ruff_python_semantic::{Definition, Member, MemberKind};
use rustpython_parser::ast::Expr;
use crate::checkers::ast::Checker;

View file

@ -4,7 +4,7 @@ use rustpython_parser::ast::Ranged;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::prelude::Arguments;
use rustpython_parser::ast::Arguments;
use crate::checkers::ast::Checker;
use crate::settings::types::PythonVersion::Py311;

View file

@ -2,6 +2,7 @@ use std::fmt;
use anyhow::Result;
use ruff_text_size::{TextLen, TextRange, TextSize};
use rustpython_parser::ast::Decorator;
use rustpython_parser::ast::{self, ArgWithDefault, Arguments, Expr, Keyword, Ranged, Stmt};
use ruff_diagnostics::{AlwaysAutofixableViolation, Violation};
@ -10,7 +11,6 @@ use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::collect_call_path;
use ruff_python_ast::helpers::collect_arg_names;
use ruff_python_ast::identifier::Identifier;
use ruff_python_ast::prelude::Decorator;
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::visitor;
use ruff_python_ast::visitor::Visitor;

View file

@ -4,7 +4,7 @@ use rustpython_parser::ast::{Expr, StmtClassDef};
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::identifier::Identifier;
use ruff_python_ast::prelude::Stmt;
use rustpython_parser::ast::Stmt;
use crate::checkers::ast::Checker;
use crate::rules::flake8_slots::rules::helpers::has_slots;

View file

@ -6,8 +6,8 @@ use rustpython_parser::{ast, lexer, Mode, Tok};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::prelude::Ranged;
use ruff_python_ast::source_code::Locator;
use rustpython_parser::ast::Ranged;
use crate::checkers::ast::Checker;
use crate::registry::AsRule;

View file

@ -3,7 +3,7 @@ use rustpython_parser::ast::{self, Expr};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::prelude::Stmt;
use rustpython_parser::ast::Stmt;
use crate::checkers::ast::Checker;
use crate::registry::AsRule;

View file

@ -9,7 +9,6 @@ pub mod helpers;
pub mod identifier;
pub mod imports;
pub mod node;
pub mod prelude;
pub mod relocate;
pub mod source_code;
pub mod statement_visitor;

File diff suppressed because it is too large Load diff

View file

@ -1,3 +0,0 @@
pub use crate::node::AstNode;
pub use rustpython_ast::*;
pub use rustpython_parser::*;

View file

@ -2,10 +2,9 @@
pub mod preorder;
use rustpython_ast::Decorator;
use rustpython_parser::ast::{
self, Alias, Arg, Arguments, BoolOp, CmpOp, Comprehension, Constant, ExceptHandler, Expr,
ExprContext, Keyword, MatchCase, Operator, Pattern, Stmt, UnaryOp, WithItem,
self, Alias, Arg, Arguments, BoolOp, CmpOp, Comprehension, Constant, Decorator, ExceptHandler,
Expr, ExprContext, Keyword, MatchCase, Operator, Pattern, Stmt, UnaryOp, WithItem,
};
/// A trait for AST visitors. Visits all nodes in the AST recursively in evaluation-order.

View file

@ -1,4 +1,8 @@
use crate::prelude::*;
use rustpython_ast::{ArgWithDefault, Mod, TypeIgnore};
use rustpython_parser::ast::{
self, Alias, Arg, Arguments, BoolOp, CmpOp, Comprehension, Constant, Decorator, ExceptHandler,
Expr, Keyword, MatchCase, Operator, Pattern, Stmt, UnaryOp, WithItem,
};
/// Visitor that traverses all nodes recursively in pre-order.
pub trait PreorderVisitor<'a> {
@ -100,7 +104,7 @@ where
V: PreorderVisitor<'a> + ?Sized,
{
match module {
Mod::Module(ModModule {
Mod::Module(ast::ModModule {
body,
range: _,
type_ignores,
@ -110,9 +114,9 @@ where
visitor.visit_type_ignore(ignore);
}
}
Mod::Interactive(ModInteractive { body, range: _ }) => visitor.visit_body(body),
Mod::Expression(ModExpression { body, range: _ }) => visitor.visit_expr(body),
Mod::FunctionType(ModFunctionType {
Mod::Interactive(ast::ModInteractive { body, range: _ }) => visitor.visit_body(body),
Mod::Expression(ast::ModExpression { body, range: _ }) => visitor.visit_expr(body),
Mod::FunctionType(ast::ModFunctionType {
range: _,
argtypes,
returns,
@ -140,19 +144,19 @@ where
V: PreorderVisitor<'a> + ?Sized,
{
match stmt {
Stmt::Expr(StmtExpr {
Stmt::Expr(ast::StmtExpr {
value,
range: _range,
}) => visitor.visit_expr(value),
Stmt::FunctionDef(StmtFunctionDef {
Stmt::FunctionDef(ast::StmtFunctionDef {
args,
body,
decorator_list,
returns,
..
})
| Stmt::AsyncFunctionDef(StmtAsyncFunctionDef {
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
args,
body,
decorator_list,
@ -172,7 +176,7 @@ where
visitor.visit_body(body);
}
Stmt::ClassDef(StmtClassDef {
Stmt::ClassDef(ast::StmtClassDef {
bases,
keywords,
body,
@ -194,7 +198,7 @@ where
visitor.visit_body(body);
}
Stmt::Return(StmtReturn {
Stmt::Return(ast::StmtReturn {
value,
range: _range,
}) => {
@ -203,7 +207,7 @@ where
}
}
Stmt::Delete(StmtDelete {
Stmt::Delete(ast::StmtDelete {
targets,
range: _range,
}) => {
@ -212,7 +216,7 @@ where
}
}
Stmt::Assign(StmtAssign {
Stmt::Assign(ast::StmtAssign {
targets,
value,
range: _,
@ -225,7 +229,7 @@ where
visitor.visit_expr(value);
}
Stmt::AugAssign(StmtAugAssign {
Stmt::AugAssign(ast::StmtAugAssign {
target,
op,
value,
@ -236,7 +240,7 @@ where
visitor.visit_expr(value);
}
Stmt::AnnAssign(StmtAnnAssign {
Stmt::AnnAssign(ast::StmtAnnAssign {
target,
annotation,
value,
@ -250,14 +254,14 @@ where
}
}
Stmt::For(StmtFor {
Stmt::For(ast::StmtFor {
target,
iter,
body,
orelse,
..
})
| Stmt::AsyncFor(StmtAsyncFor {
| Stmt::AsyncFor(ast::StmtAsyncFor {
target,
iter,
body,
@ -270,7 +274,7 @@ where
visitor.visit_body(orelse);
}
Stmt::While(StmtWhile {
Stmt::While(ast::StmtWhile {
test,
body,
orelse,
@ -281,7 +285,7 @@ where
visitor.visit_body(orelse);
}
Stmt::If(StmtIf {
Stmt::If(ast::StmtIf {
test,
body,
orelse,
@ -292,13 +296,13 @@ where
visitor.visit_body(orelse);
}
Stmt::With(StmtWith {
Stmt::With(ast::StmtWith {
items,
body,
type_comment: _,
range: _,
})
| Stmt::AsyncWith(StmtAsyncWith {
| Stmt::AsyncWith(ast::StmtAsyncWith {
items,
body,
type_comment: _,
@ -310,7 +314,7 @@ where
visitor.visit_body(body);
}
Stmt::Match(StmtMatch {
Stmt::Match(ast::StmtMatch {
subject,
cases,
range: _range,
@ -321,7 +325,7 @@ where
}
}
Stmt::Raise(StmtRaise {
Stmt::Raise(ast::StmtRaise {
exc,
cause,
range: _range,
@ -334,14 +338,14 @@ where
};
}
Stmt::Try(StmtTry {
Stmt::Try(ast::StmtTry {
body,
handlers,
orelse,
finalbody,
range: _range,
})
| Stmt::TryStar(StmtTryStar {
| Stmt::TryStar(ast::StmtTryStar {
body,
handlers,
orelse,
@ -356,7 +360,7 @@ where
visitor.visit_body(finalbody);
}
Stmt::Assert(StmtAssert {
Stmt::Assert(ast::StmtAssert {
test,
msg,
range: _range,
@ -367,7 +371,7 @@ where
}
}
Stmt::Import(StmtImport {
Stmt::Import(ast::StmtImport {
names,
range: _range,
}) => {
@ -376,7 +380,7 @@ where
}
}
Stmt::ImportFrom(StmtImportFrom {
Stmt::ImportFrom(ast::StmtImportFrom {
range: _,
module: _,
names,
@ -411,7 +415,7 @@ where
V: PreorderVisitor<'a> + ?Sized,
{
match expr {
Expr::BoolOp(ExprBoolOp {
Expr::BoolOp(ast::ExprBoolOp {
op,
values,
range: _range,
@ -428,7 +432,7 @@ where
}
},
Expr::NamedExpr(ExprNamedExpr {
Expr::NamedExpr(ast::ExprNamedExpr {
target,
value,
range: _range,
@ -437,7 +441,7 @@ where
visitor.visit_expr(value);
}
Expr::BinOp(ExprBinOp {
Expr::BinOp(ast::ExprBinOp {
left,
op,
right,
@ -448,7 +452,7 @@ where
visitor.visit_expr(right);
}
Expr::UnaryOp(ExprUnaryOp {
Expr::UnaryOp(ast::ExprUnaryOp {
op,
operand,
range: _range,
@ -457,7 +461,7 @@ where
visitor.visit_expr(operand);
}
Expr::Lambda(ExprLambda {
Expr::Lambda(ast::ExprLambda {
args,
body,
range: _range,
@ -466,7 +470,7 @@ where
visitor.visit_expr(body);
}
Expr::IfExp(ExprIfExp {
Expr::IfExp(ast::ExprIfExp {
test,
body,
orelse,
@ -477,7 +481,7 @@ where
visitor.visit_expr(orelse);
}
Expr::Dict(ExprDict {
Expr::Dict(ast::ExprDict {
keys,
values,
range: _range,
@ -490,7 +494,7 @@ where
}
}
Expr::Set(ExprSet {
Expr::Set(ast::ExprSet {
elts,
range: _range,
}) => {
@ -499,7 +503,7 @@ where
}
}
Expr::ListComp(ExprListComp {
Expr::ListComp(ast::ExprListComp {
elt,
generators,
range: _range,
@ -510,7 +514,7 @@ where
}
}
Expr::SetComp(ExprSetComp {
Expr::SetComp(ast::ExprSetComp {
elt,
generators,
range: _range,
@ -521,7 +525,7 @@ where
}
}
Expr::DictComp(ExprDictComp {
Expr::DictComp(ast::ExprDictComp {
key,
value,
generators,
@ -535,7 +539,7 @@ where
}
}
Expr::GeneratorExp(ExprGeneratorExp {
Expr::GeneratorExp(ast::ExprGeneratorExp {
elt,
generators,
range: _range,
@ -546,16 +550,16 @@ where
}
}
Expr::Await(ExprAwait {
Expr::Await(ast::ExprAwait {
value,
range: _range,
})
| Expr::YieldFrom(ExprYieldFrom {
| Expr::YieldFrom(ast::ExprYieldFrom {
value,
range: _range,
}) => visitor.visit_expr(value),
Expr::Yield(ExprYield {
Expr::Yield(ast::ExprYield {
value,
range: _range,
}) => {
@ -564,7 +568,7 @@ where
}
}
Expr::Compare(ExprCompare {
Expr::Compare(ast::ExprCompare {
left,
ops,
comparators,
@ -578,7 +582,7 @@ where
}
}
Expr::Call(ExprCall {
Expr::Call(ast::ExprCall {
func,
args,
keywords,
@ -593,7 +597,7 @@ where
}
}
Expr::FormattedValue(ExprFormattedValue {
Expr::FormattedValue(ast::ExprFormattedValue {
value, format_spec, ..
}) => {
visitor.visit_expr(value);
@ -603,7 +607,7 @@ where
}
}
Expr::JoinedStr(ExprJoinedStr {
Expr::JoinedStr(ast::ExprJoinedStr {
values,
range: _range,
}) => {
@ -612,13 +616,13 @@ where
}
}
Expr::Constant(ExprConstant {
Expr::Constant(ast::ExprConstant {
value,
range: _,
kind: _,
}) => visitor.visit_constant(value),
Expr::Attribute(ExprAttribute {
Expr::Attribute(ast::ExprAttribute {
value,
attr: _,
ctx: _,
@ -627,7 +631,7 @@ where
visitor.visit_expr(value);
}
Expr::Subscript(ExprSubscript {
Expr::Subscript(ast::ExprSubscript {
value,
slice,
ctx: _,
@ -636,7 +640,7 @@ where
visitor.visit_expr(value);
visitor.visit_expr(slice);
}
Expr::Starred(ExprStarred {
Expr::Starred(ast::ExprStarred {
value,
ctx: _,
range: _range,
@ -644,13 +648,13 @@ where
visitor.visit_expr(value);
}
Expr::Name(ExprName {
Expr::Name(ast::ExprName {
id: _,
ctx: _,
range: _,
}) => {}
Expr::List(ExprList {
Expr::List(ast::ExprList {
elts,
ctx: _,
range: _range,
@ -659,7 +663,7 @@ where
visitor.visit_expr(expr);
}
}
Expr::Tuple(ExprTuple {
Expr::Tuple(ast::ExprTuple {
elts,
ctx: _,
range: _range,
@ -669,7 +673,7 @@ where
}
}
Expr::Slice(ExprSlice {
Expr::Slice(ast::ExprSlice {
lower,
upper,
step,
@ -716,7 +720,7 @@ where
V: PreorderVisitor<'a> + ?Sized,
{
match except_handler {
ExceptHandler::ExceptHandler(ExceptHandlerExceptHandler {
ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
range: _,
type_,
name: _,
@ -812,19 +816,19 @@ where
V: PreorderVisitor<'a> + ?Sized,
{
match pattern {
Pattern::MatchValue(PatternMatchValue {
Pattern::MatchValue(ast::PatternMatchValue {
value,
range: _range,
}) => visitor.visit_expr(value),
Pattern::MatchSingleton(PatternMatchSingleton {
Pattern::MatchSingleton(ast::PatternMatchSingleton {
value,
range: _range,
}) => {
visitor.visit_constant(value);
}
Pattern::MatchSequence(PatternMatchSequence {
Pattern::MatchSequence(ast::PatternMatchSequence {
patterns,
range: _range,
}) => {
@ -833,7 +837,7 @@ where
}
}
Pattern::MatchMapping(PatternMatchMapping {
Pattern::MatchMapping(ast::PatternMatchMapping {
keys,
patterns,
range: _,
@ -845,7 +849,7 @@ where
}
}
Pattern::MatchClass(PatternMatchClass {
Pattern::MatchClass(ast::PatternMatchClass {
cls,
patterns,
kwd_attrs: _,
@ -864,7 +868,7 @@ where
Pattern::MatchStar(_) => {}
Pattern::MatchAs(PatternMatchAs {
Pattern::MatchAs(ast::PatternMatchAs {
pattern,
range: _,
name: _,
@ -874,7 +878,7 @@ where
}
}
Pattern::MatchOr(PatternMatchOr {
Pattern::MatchOr(ast::PatternMatchOr {
patterns,
range: _range,
}) => {
@ -928,18 +932,20 @@ where
#[cfg(test)]
mod tests {
use std::fmt::{Debug, Write};
use insta::assert_snapshot;
use rustpython_parser::lexer::lex;
use rustpython_parser::{parse_tokens, Mode};
use crate::node::AnyNodeRef;
use crate::visitor::preorder::{
walk_alias, walk_arg, walk_arguments, walk_comprehension, walk_except_handler, walk_expr,
walk_keyword, walk_match_case, walk_module, walk_pattern, walk_stmt, walk_type_ignore,
walk_with_item, Alias, Arg, Arguments, BoolOp, CmpOp, Comprehension, Constant,
ExceptHandler, Expr, Keyword, MatchCase, Mod, Operator, Pattern, PreorderVisitor, Stmt,
String, TypeIgnore, UnaryOp, WithItem,
TypeIgnore, UnaryOp, WithItem,
};
use insta::assert_snapshot;
use rustpython_parser::lexer::lex;
use rustpython_parser::{parse_tokens, Mode};
use std::fmt::{Debug, Write};
#[test]
fn function_arguments() {

View file

@ -1,9 +1,12 @@
use std::fmt::{Debug, Formatter, Write};
use itertools::Itertools;
use rustpython_parser::ast::Ranged;
use ruff_formatter::SourceCode;
use crate::comments::node_key::NodeRefEqualityKey;
use crate::comments::{CommentsMap, SourceComment};
use itertools::Itertools;
use ruff_formatter::SourceCode;
use ruff_python_ast::prelude::*;
use std::fmt::{Debug, Formatter, Write};
/// Prints a debug representation of [`SourceComment`] that includes the comment's text
pub(crate) struct DebugComment<'a> {
@ -176,14 +179,16 @@ impl Debug for DebugNodeCommentSlice<'_> {
#[cfg(test)]
mod tests {
use crate::comments::map::MultiMap;
use crate::comments::{CommentLinePosition, Comments, CommentsMap, SourceComment};
use insta::assert_debug_snapshot;
use ruff_formatter::SourceCode;
use ruff_python_ast::node::AnyNode;
use ruff_text_size::{TextRange, TextSize};
use rustpython_parser::ast::{StmtBreak, StmtContinue};
use ruff_formatter::SourceCode;
use ruff_python_ast::node::AnyNode;
use crate::comments::map::MultiMap;
use crate::comments::{CommentLinePosition, Comments, CommentsMap, SourceComment};
#[test]
fn debug() {
let continue_statement = AnyNode::from(StmtContinue {

View file

@ -1,12 +1,13 @@
use ruff_text_size::{TextLen, TextRange, TextSize};
use rustpython_parser::ast::Ranged;
use ruff_formatter::{format_args, write, FormatError, SourceCode};
use ruff_python_ast::node::{AnyNodeRef, AstNode};
use crate::comments::SourceComment;
use crate::context::NodeLevel;
use crate::prelude::*;
use crate::trivia::{lines_after, lines_before, skip_trailing_trivia};
use ruff_formatter::{format_args, write, FormatError, SourceCode};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::prelude::AstNode;
use ruff_text_size::{TextLen, TextRange, TextSize};
use rustpython_parser::ast::Ranged;
/// Formats the leading comments of a node.
pub(crate) fn leading_node_comments<T>(node: &T) -> FormatLeadingComments
@ -69,7 +70,7 @@ where
{
FormatLeadingAlternateBranchComments {
comments,
last_node: last_node.map(std::convert::Into::into),
last_node: last_node.map(Into::into),
}
}

View file

@ -87,10 +87,12 @@
//!
//! It is possible to add an additional optional label to [`SourceComment`] If ever the need arises to distinguish two *dangling comments* in the formatting logic,
use crate::comments::debug::{DebugComment, DebugComments};
use crate::comments::map::MultiMap;
use crate::comments::node_key::NodeRefEqualityKey;
use crate::comments::visitor::CommentsVisitor;
use std::cell::Cell;
use std::fmt::Debug;
use std::rc::Rc;
use rustpython_parser::ast::Mod;
pub(crate) use format::{
dangling_comments, dangling_node_comments, leading_alternate_branch_comments, leading_comments,
leading_node_comments, trailing_comments, trailing_node_comments,
@ -98,10 +100,11 @@ pub(crate) use format::{
use ruff_formatter::{SourceCode, SourceCodeSlice};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::source_code::CommentRanges;
use rustpython_parser::ast::Mod;
use std::cell::Cell;
use std::fmt::Debug;
use std::rc::Rc;
use crate::comments::debug::{DebugComment, DebugComments};
use crate::comments::map::MultiMap;
use crate::comments::node_key::NodeRefEqualityKey;
use crate::comments::visitor::CommentsVisitor;
mod debug;
mod format;
@ -404,14 +407,16 @@ struct CommentsData<'a> {
#[cfg(test)]
mod tests {
use crate::comments::Comments;
use insta::assert_debug_snapshot;
use ruff_formatter::SourceCode;
use ruff_python_ast::prelude::*;
use ruff_python_ast::source_code::{CommentRanges, CommentRangesBuilder};
use rustpython_parser::ast::Mod;
use rustpython_parser::lexer::lex;
use rustpython_parser::{parse_tokens, Mode};
use ruff_formatter::SourceCode;
use ruff_python_ast::source_code::{CommentRanges, CommentRangesBuilder};
use crate::comments::Comments;
struct CommentsTestCase<'a> {
module: Mod,
comment_ranges: CommentRanges,

View file

@ -1,16 +1,20 @@
use std::cmp::Ordering;
use ruff_text_size::TextRange;
use rustpython_parser::ast;
use rustpython_parser::ast::{Expr, ExprSlice, Ranged};
use ruff_python_ast::node::{AnyNodeRef, AstNode};
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::whitespace;
use ruff_python_whitespace::{PythonWhitespace, UniversalNewlines};
use crate::comments::visitor::{CommentPlacement, DecoratedComment};
use crate::expression::expr_slice::{assign_comment_in_slice, ExprSliceCommentSection};
use crate::other::arguments::{
assign_argument_separator_comment_placement, find_argument_separators,
};
use crate::trivia::{first_non_trivia_token_rev, SimpleTokenizer, Token, TokenKind};
use ruff_python_ast::node::{AnyNodeRef, AstNode};
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::whitespace;
use ruff_python_whitespace::{PythonWhitespace, UniversalNewlines};
use ruff_text_size::TextRange;
use rustpython_parser::ast::{Expr, ExprSlice, Ranged};
use std::cmp::Ordering;
/// Implements the custom comment placement logic.
pub(super) fn place_comment<'a>(
@ -574,8 +578,6 @@ fn handle_trailing_end_of_line_condition_comment<'a>(
comment: DecoratedComment<'a>,
locator: &Locator,
) -> CommentPlacement<'a> {
use ruff_python_ast::prelude::*;
// Must be an end of line comment
if comment.line_position().is_own_line() {
return CommentPlacement::Default(comment);
@ -587,23 +589,25 @@ fn handle_trailing_end_of_line_condition_comment<'a>(
};
let expression_before_colon = match comment.enclosing_node() {
AnyNodeRef::StmtIf(StmtIf { test: expr, .. })
| AnyNodeRef::StmtWhile(StmtWhile { test: expr, .. })
| AnyNodeRef::StmtFor(StmtFor { iter: expr, .. })
| AnyNodeRef::StmtAsyncFor(StmtAsyncFor { iter: expr, .. }) => {
AnyNodeRef::StmtIf(ast::StmtIf { test: expr, .. })
| AnyNodeRef::StmtWhile(ast::StmtWhile { test: expr, .. })
| AnyNodeRef::StmtFor(ast::StmtFor { iter: expr, .. })
| AnyNodeRef::StmtAsyncFor(ast::StmtAsyncFor { iter: expr, .. }) => {
Some(AnyNodeRef::from(expr.as_ref()))
}
AnyNodeRef::StmtWith(StmtWith { items, .. })
| AnyNodeRef::StmtAsyncWith(StmtAsyncWith { items, .. }) => {
AnyNodeRef::StmtWith(ast::StmtWith { items, .. })
| AnyNodeRef::StmtAsyncWith(ast::StmtAsyncWith { items, .. }) => {
items.last().map(AnyNodeRef::from)
}
AnyNodeRef::StmtFunctionDef(StmtFunctionDef { returns, args, .. })
| AnyNodeRef::StmtAsyncFunctionDef(StmtAsyncFunctionDef { returns, args, .. }) => returns
AnyNodeRef::StmtFunctionDef(ast::StmtFunctionDef { returns, args, .. })
| AnyNodeRef::StmtAsyncFunctionDef(ast::StmtAsyncFunctionDef { returns, args, .. }) => {
returns
.as_deref()
.map(AnyNodeRef::from)
.or_else(|| Some(AnyNodeRef::from(args.as_ref()))),
AnyNodeRef::StmtClassDef(StmtClassDef {
.or_else(|| Some(AnyNodeRef::from(args.as_ref())))
}
AnyNodeRef::StmtClassDef(ast::StmtClassDef {
bases, keywords, ..
}) => keywords
.last()
@ -1104,21 +1108,21 @@ where
}
fn last_child_in_body(node: AnyNodeRef) -> Option<AnyNodeRef> {
use ruff_python_ast::prelude::*;
let body = match node {
AnyNodeRef::StmtFunctionDef(StmtFunctionDef { body, .. })
| AnyNodeRef::StmtAsyncFunctionDef(StmtAsyncFunctionDef { body, .. })
| AnyNodeRef::StmtClassDef(StmtClassDef { body, .. })
| AnyNodeRef::StmtWith(StmtWith { body, .. })
| AnyNodeRef::StmtAsyncWith(StmtAsyncWith { body, .. })
| AnyNodeRef::MatchCase(MatchCase { body, .. })
| AnyNodeRef::ExceptHandlerExceptHandler(ExceptHandlerExceptHandler { body, .. }) => body,
AnyNodeRef::StmtFunctionDef(ast::StmtFunctionDef { body, .. })
| AnyNodeRef::StmtAsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. })
| AnyNodeRef::StmtClassDef(ast::StmtClassDef { body, .. })
| AnyNodeRef::StmtWith(ast::StmtWith { body, .. })
| AnyNodeRef::StmtAsyncWith(ast::StmtAsyncWith { body, .. })
| AnyNodeRef::MatchCase(ast::MatchCase { body, .. })
| AnyNodeRef::ExceptHandlerExceptHandler(ast::ExceptHandlerExceptHandler {
body, ..
}) => body,
AnyNodeRef::StmtIf(StmtIf { body, orelse, .. })
| AnyNodeRef::StmtFor(StmtFor { body, orelse, .. })
| AnyNodeRef::StmtAsyncFor(StmtAsyncFor { body, orelse, .. })
| AnyNodeRef::StmtWhile(StmtWhile { body, orelse, .. }) => {
AnyNodeRef::StmtIf(ast::StmtIf { body, orelse, .. })
| AnyNodeRef::StmtFor(ast::StmtFor { body, orelse, .. })
| AnyNodeRef::StmtAsyncFor(ast::StmtAsyncFor { body, orelse, .. })
| AnyNodeRef::StmtWhile(ast::StmtWhile { body, orelse, .. }) => {
if orelse.is_empty() {
body
} else {
@ -1126,18 +1130,18 @@ fn last_child_in_body(node: AnyNodeRef) -> Option<AnyNodeRef> {
}
}
AnyNodeRef::StmtMatch(StmtMatch { cases, .. }) => {
AnyNodeRef::StmtMatch(ast::StmtMatch { cases, .. }) => {
return cases.last().map(AnyNodeRef::from)
}
AnyNodeRef::StmtTry(StmtTry {
AnyNodeRef::StmtTry(ast::StmtTry {
body,
handlers,
orelse,
finalbody,
..
})
| AnyNodeRef::StmtTryStar(StmtTryStar {
| AnyNodeRef::StmtTryStar(ast::StmtTryStar {
body,
handlers,
orelse,
@ -1171,23 +1175,21 @@ fn is_first_statement_in_enclosing_alternate_body(
following: AnyNodeRef,
enclosing: AnyNodeRef,
) -> bool {
use ruff_python_ast::prelude::*;
match enclosing {
AnyNodeRef::StmtIf(StmtIf { orelse, .. })
| AnyNodeRef::StmtFor(StmtFor { orelse, .. })
| AnyNodeRef::StmtAsyncFor(StmtAsyncFor { orelse, .. })
| AnyNodeRef::StmtWhile(StmtWhile { orelse, .. }) => {
AnyNodeRef::StmtIf(ast::StmtIf { orelse, .. })
| AnyNodeRef::StmtFor(ast::StmtFor { orelse, .. })
| AnyNodeRef::StmtAsyncFor(ast::StmtAsyncFor { orelse, .. })
| AnyNodeRef::StmtWhile(ast::StmtWhile { orelse, .. }) => {
are_same_optional(following, orelse.first())
}
AnyNodeRef::StmtTry(StmtTry {
AnyNodeRef::StmtTry(ast::StmtTry {
handlers,
orelse,
finalbody,
..
})
| AnyNodeRef::StmtTryStar(StmtTryStar {
| AnyNodeRef::StmtTryStar(ast::StmtTryStar {
handlers,
orelse,
finalbody,

View file

@ -1,17 +1,23 @@
use crate::comments::node_key::NodeRefEqualityKey;
use crate::comments::placement::place_comment;
use crate::comments::{CommentLinePosition, CommentsMap, SourceComment};
use std::iter::Peekable;
use ruff_text_size::{TextRange, TextSize};
use rustpython_parser::ast::{
Alias, Arg, ArgWithDefault, Arguments, Comprehension, Decorator, ExceptHandler, Expr, Keyword,
MatchCase, Mod, Pattern, Ranged, Stmt, WithItem,
};
use ruff_formatter::{SourceCode, SourceCodeSlice};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::prelude::*;
use ruff_python_ast::source_code::{CommentRanges, Locator};
// The interface is designed to only export the members relevant for iterating nodes in
// pre-order.
#[allow(clippy::wildcard_imports)]
use ruff_python_ast::visitor::preorder::*;
use ruff_python_whitespace::is_python_whitespace;
use ruff_text_size::TextRange;
use std::iter::Peekable;
use crate::comments::node_key::NodeRefEqualityKey;
use crate::comments::placement::place_comment;
use crate::comments::{CommentLinePosition, CommentsMap, SourceComment};
/// Visitor extracting the comments from an AST.
#[derive(Debug, Clone)]

View file

@ -1,10 +1,12 @@
//! This module provides helper utilities to format an expression that has a left side, an operator,
//! and a right side (binary like).
use rustpython_parser::ast::{self, Expr};
use ruff_formatter::{format_args, write};
use crate::expression::parentheses::Parentheses;
use crate::prelude::*;
use ruff_formatter::{format_args, write};
use rustpython_parser::ast::Expr;
/// Trait to implement a binary like syntax that has a left operand, an operator, and a right operand.
pub(super) trait FormatBinaryLike<'ast> {
@ -133,25 +135,25 @@ pub(super) trait FormatBinaryLike<'ast> {
}
fn can_break_expr(expr: &Expr) -> bool {
use ruff_python_ast::prelude::*;
match expr {
Expr::Tuple(ExprTuple {
Expr::Tuple(ast::ExprTuple {
elts: expressions, ..
})
| Expr::List(ExprList {
| Expr::List(ast::ExprList {
elts: expressions, ..
})
| Expr::Set(ExprSet {
| Expr::Set(ast::ExprSet {
elts: expressions, ..
})
| Expr::Dict(ExprDict {
| Expr::Dict(ast::ExprDict {
values: expressions,
..
}) => !expressions.is_empty(),
Expr::Call(ExprCall { args, keywords, .. }) => !(args.is_empty() && keywords.is_empty()),
Expr::Call(ast::ExprCall { args, keywords, .. }) => {
!(args.is_empty() && keywords.is_empty())
}
Expr::ListComp(_) | Expr::SetComp(_) | Expr::DictComp(_) | Expr::GeneratorExp(_) => true,
Expr::UnaryOp(ExprUnaryOp { operand, .. }) => match operand.as_ref() {
Expr::UnaryOp(ast::ExprUnaryOp { operand, .. }) => match operand.as_ref() {
Expr::BinOp(_) => true,
_ => can_break_expr(operand.as_ref()),
},

View file

@ -8,7 +8,7 @@ use crate::FormatNodeRule;
use ruff_formatter::{
write, FormatError, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions,
};
use ruff_python_ast::prelude::Expr;
use rustpython_parser::ast::Expr;
use rustpython_parser::ast::{CmpOp, ExprCompare};
#[derive(Default)]

View file

@ -5,8 +5,8 @@ use crate::expression::parentheses::{
use crate::prelude::*;
use crate::FormatNodeRule;
use ruff_formatter::{format_args, write};
use ruff_python_ast::prelude::Ranged;
use ruff_text_size::TextRange;
use rustpython_parser::ast::Ranged;
use rustpython_parser::ast::{Expr, ExprDict};
#[derive(Default)]

View file

@ -8,9 +8,9 @@ use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{hard_line_break, line_suffix_boundary, space, text};
use ruff_formatter::{write, Buffer, Format, FormatError, FormatResult};
use ruff_python_ast::node::AstNode;
use ruff_python_ast::prelude::{Expr, Ranged};
use ruff_text_size::TextRange;
use rustpython_parser::ast::ExprSlice;
use rustpython_parser::ast::{Expr, Ranged};
#[derive(Default)]
pub struct FormatExprSlice;

View file

@ -5,9 +5,9 @@ use crate::expression::parentheses::{
};
use crate::prelude::*;
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
use ruff_python_ast::prelude::{Expr, Ranged};
use ruff_text_size::TextRange;
use rustpython_parser::ast::ExprTuple;
use rustpython_parser::ast::{Expr, Ranged};
#[derive(Eq, PartialEq, Debug, Default)]
pub enum TupleParentheses {

View file

@ -6,8 +6,8 @@ use crate::trivia::{SimpleTokenizer, TokenKind};
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{hard_line_break, space, text};
use ruff_formatter::{Format, FormatContext, FormatResult};
use ruff_python_ast::prelude::UnaryOp;
use ruff_text_size::{TextLen, TextRange};
use rustpython_parser::ast::UnaryOp;
use rustpython_parser::ast::{ExprUnaryOp, Ranged};
#[derive(Default)]

View file

@ -4,7 +4,7 @@ use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::formatter::Formatter;
use ruff_formatter::prelude::{space, text};
use ruff_formatter::{write, Buffer, Format, FormatResult};
use ruff_python_ast::prelude::Expr;
use rustpython_parser::ast::Expr;
use rustpython_parser::ast::StmtAssign;
// Note: This currently does wrap but not the black way so the types below likely need to be

View file

@ -1,10 +1,12 @@
use rustpython_parser::ast::StmtWith;
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::node::AstNode;
use crate::builders::optional_parentheses;
use crate::comments::trailing_comments;
use crate::prelude::*;
use crate::{FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::prelude::*;
use rustpython_parser::ast::StmtWith;
#[derive(Default)]
pub struct FormatStmtWith;

View file

@ -24,6 +24,9 @@ ruff_python_ast = { path = "../crates/ruff_python_ast" }
ruff_python_formatter = { path = "../crates/ruff_python_formatter" }
similar = { version = "2.2.1" }
# Current tag: v0.0.6
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "8078663b6c914c1cb86993e427764f7c422fc12c" , default-features = false, features = ["full-lexer", "num-bigint"] }
# Prevent this from interfering with workspaces
[workspace]
members = ["."]

View file

@ -4,8 +4,9 @@
#![no_main]
use libfuzzer_sys::{fuzz_target, Corpus};
use ruff_python_ast::prelude::{lexer, Mode, Parse, ParseError, Suite};
use ruff_python_ast::source_code::{Generator, Locator, Stylist};
use rustpython_parser::ast::Suite;
use rustpython_parser::{lexer, Mode, Parse, ParseError};
fn do_fuzz(case: &[u8]) -> Corpus {
let Ok(code) = std::str::from_utf8(case) else { return Corpus::Reject; };