Make Locator available in AST-to-CST conversion pass (#3194)

This commit is contained in:
Charlie Marsh 2023-02-23 19:43:03 -05:00 committed by GitHub
parent 198b301baf
commit eb15371453
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 316 additions and 136 deletions

View file

@ -2,6 +2,7 @@
use rustpython_parser::ast::{Constant, Location}; use rustpython_parser::ast::{Constant, Location};
use crate::core::locator::Locator;
use crate::trivia::{Parenthesize, Trivia}; use crate::trivia::{Parenthesize, Trivia};
type Ident = String; type Ident = String;
@ -506,8 +507,8 @@ pub enum PatternKind {
pub type Pattern = Located<PatternKind>; pub type Pattern = Located<PatternKind>;
impl From<rustpython_parser::ast::Alias> for Alias { impl From<(rustpython_parser::ast::Alias, &Locator<'_>)> for Alias {
fn from(alias: rustpython_parser::ast::Alias) -> Self { fn from((alias, _locator): (rustpython_parser::ast::Alias, &Locator)) -> Self {
Alias { Alias {
location: alias.location, location: alias.location,
end_location: alias.end_location, end_location: alias.end_location,
@ -521,26 +522,31 @@ impl From<rustpython_parser::ast::Alias> for Alias {
} }
} }
impl From<rustpython_parser::ast::Withitem> for Withitem { impl From<(rustpython_parser::ast::Withitem, &Locator<'_>)> for Withitem {
fn from(withitem: rustpython_parser::ast::Withitem) -> Self { fn from((withitem, locator): (rustpython_parser::ast::Withitem, &Locator)) -> Self {
Withitem { Withitem {
context_expr: withitem.context_expr.into(), context_expr: (withitem.context_expr, locator).into(),
optional_vars: withitem.optional_vars.map(|v| Box::new((*v).into())), optional_vars: withitem
.optional_vars
.map(|v| Box::new((*v, locator).into())),
} }
} }
} }
impl From<rustpython_parser::ast::Excepthandler> for Excepthandler { impl From<(rustpython_parser::ast::Excepthandler, &Locator<'_>)> for Excepthandler {
fn from(excepthandler: rustpython_parser::ast::Excepthandler) -> Self { fn from((excepthandler, locator): (rustpython_parser::ast::Excepthandler, &Locator)) -> Self {
let rustpython_parser::ast::ExcepthandlerKind::ExceptHandler { type_, name, body } = let rustpython_parser::ast::ExcepthandlerKind::ExceptHandler { type_, name, body } =
excepthandler.node; excepthandler.node;
Excepthandler { Excepthandler {
location: excepthandler.location, location: excepthandler.location,
end_location: excepthandler.end_location, end_location: excepthandler.end_location,
node: ExcepthandlerKind::ExceptHandler { node: ExcepthandlerKind::ExceptHandler {
type_: type_.map(|type_| Box::new((*type_).into())), type_: type_.map(|type_| Box::new((*type_, locator).into())),
name, name,
body: body.into_iter().map(Into::into).collect(), body: body
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -548,14 +554,14 @@ impl From<rustpython_parser::ast::Excepthandler> for Excepthandler {
} }
} }
impl From<rustpython_parser::ast::Stmt> for Stmt { impl From<(rustpython_parser::ast::Stmt, &Locator<'_>)> for Stmt {
fn from(stmt: rustpython_parser::ast::Stmt) -> Self { fn from((stmt, locator): (rustpython_parser::ast::Stmt, &Locator)) -> Self {
match stmt.node { match stmt.node {
rustpython_parser::ast::StmtKind::Expr { value } => Stmt { rustpython_parser::ast::StmtKind::Expr { value } => Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::Expr { node: StmtKind::Expr {
value: Box::new((*value).into()), value: Box::new((*value, locator).into()),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -571,7 +577,7 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::Return { node: StmtKind::Return {
value: value.map(|v| (*v).into()), value: value.map(|v| (*v, locator).into()),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -584,8 +590,11 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::Assign { node: StmtKind::Assign {
targets: targets.into_iter().map(Into::into).collect(), targets: targets
value: Box::new((*value).into()), .into_iter()
.map(|node| (node, locator).into())
.collect(),
value: Box::new((*value, locator).into()),
type_comment, type_comment,
}, },
trivia: vec![], trivia: vec![],
@ -602,10 +611,22 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::ClassDef { node: StmtKind::ClassDef {
name, name,
bases: bases.into_iter().map(Into::into).collect(), bases: bases
keywords: keywords.into_iter().map(Into::into).collect(), .into_iter()
body: body.into_iter().map(Into::into).collect(), .map(|node| (node, locator).into())
decorator_list: decorator_list.into_iter().map(Into::into).collect(), .collect(),
keywords: keywords
.into_iter()
.map(|node| (node, locator).into())
.collect(),
body: body
.into_iter()
.map(|node| (node, locator).into())
.collect(),
decorator_list: decorator_list
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -624,10 +645,16 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::FunctionDef { node: StmtKind::FunctionDef {
name, name,
args: Box::new((*args).into()), args: Box::new((*args, locator).into()),
body: body.into_iter().map(Into::into).collect(), body: body
decorator_list: decorator_list.into_iter().map(Into::into).collect(), .into_iter()
returns: returns.map(|r| Box::new((*r).into())), .map(|node| (node, locator).into())
.collect(),
decorator_list: decorator_list
.into_iter()
.map(|node| (node, locator).into())
.collect(),
returns: returns.map(|r| Box::new((*r, locator).into())),
type_comment, type_comment,
}, },
trivia: vec![], trivia: vec![],
@ -637,9 +664,15 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::If { node: StmtKind::If {
test: Box::new((*test).into()), test: Box::new((*test, locator).into()),
body: body.into_iter().map(Into::into).collect(), body: body
orelse: orelse.into_iter().map(Into::into).collect(), .into_iter()
.map(|node| (node, locator).into())
.collect(),
orelse: orelse
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -648,8 +681,8 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::Assert { node: StmtKind::Assert {
test: Box::new((*test).into()), test: Box::new((*test, locator).into()),
msg: msg.map(|msg| Box::new((*msg).into())), msg: msg.map(|node| Box::new((*node, locator).into())),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -666,10 +699,16 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::AsyncFunctionDef { node: StmtKind::AsyncFunctionDef {
name, name,
args: Box::new((*args).into()), args: Box::new((*args, locator).into()),
body: body.into_iter().map(Into::into).collect(), body: body
decorator_list: decorator_list.into_iter().map(Into::into).collect(), .into_iter()
returns: returns.map(|r| Box::new((*r).into())), .map(|node| (node, locator).into())
.collect(),
decorator_list: decorator_list
.into_iter()
.map(|node| (node, locator).into())
.collect(),
returns: returns.map(|r| Box::new((*r, locator).into())),
type_comment, type_comment,
}, },
trivia: vec![], trivia: vec![],
@ -679,7 +718,10 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::Delete { node: StmtKind::Delete {
targets: targets.into_iter().map(Into::into).collect(), targets: targets
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -688,9 +730,9 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::AugAssign { node: StmtKind::AugAssign {
target: Box::new((*target).into()), target: Box::new((*target, locator).into()),
op: op.into(), op: op.into(),
value: Box::new((*value).into()), value: Box::new((*value, locator).into()),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -704,9 +746,9 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::AnnAssign { node: StmtKind::AnnAssign {
target: Box::new((*target).into()), target: Box::new((*target, locator).into()),
annotation: Box::new((*annotation).into()), annotation: Box::new((*annotation, locator).into()),
value: value.map(|v| Box::new((*v).into())), value: value.map(|node| Box::new((*node, locator).into())),
simple, simple,
}, },
trivia: vec![], trivia: vec![],
@ -722,10 +764,16 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::For { node: StmtKind::For {
target: Box::new((*target).into()), target: Box::new((*target, locator).into()),
iter: Box::new((*iter).into()), iter: Box::new((*iter, locator).into()),
body: body.into_iter().map(Into::into).collect(), body: body
orelse: orelse.into_iter().map(Into::into).collect(), .into_iter()
.map(|node| (node, locator).into())
.collect(),
orelse: orelse
.into_iter()
.map(|node| (node, locator).into())
.collect(),
type_comment, type_comment,
}, },
trivia: vec![], trivia: vec![],
@ -741,10 +789,16 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::AsyncFor { node: StmtKind::AsyncFor {
target: Box::new((*target).into()), target: Box::new((*target, locator).into()),
iter: Box::new((*iter).into()), iter: Box::new((*iter, locator).into()),
body: body.into_iter().map(Into::into).collect(), body: body
orelse: orelse.into_iter().map(Into::into).collect(), .into_iter()
.map(|node| (node, locator).into())
.collect(),
orelse: orelse
.into_iter()
.map(|node| (node, locator).into())
.collect(),
type_comment, type_comment,
}, },
trivia: vec![], trivia: vec![],
@ -754,9 +808,15 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::While { node: StmtKind::While {
test: Box::new((*test).into()), test: Box::new((*test, locator).into()),
body: body.into_iter().map(Into::into).collect(), body: body
orelse: orelse.into_iter().map(Into::into).collect(), .into_iter()
.map(|node| (node, locator).into())
.collect(),
orelse: orelse
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -769,8 +829,14 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::With { node: StmtKind::With {
items: items.into_iter().map(Into::into).collect(), items: items
body: body.into_iter().map(Into::into).collect(), .into_iter()
.map(|node| (node, locator).into())
.collect(),
body: body
.into_iter()
.map(|node| (node, locator).into())
.collect(),
type_comment, type_comment,
}, },
trivia: vec![], trivia: vec![],
@ -784,8 +850,14 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::AsyncWith { node: StmtKind::AsyncWith {
items: items.into_iter().map(Into::into).collect(), items: items
body: body.into_iter().map(Into::into).collect(), .into_iter()
.map(|node| (node, locator).into())
.collect(),
body: body
.into_iter()
.map(|node| (node, locator).into())
.collect(),
type_comment, type_comment,
}, },
trivia: vec![], trivia: vec![],
@ -798,8 +870,8 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::Raise { node: StmtKind::Raise {
exc: exc.map(|exc| Box::new((*exc).into())), exc: exc.map(|exc| Box::new((*exc, locator).into())),
cause: cause.map(|cause| Box::new((*cause).into())), cause: cause.map(|cause| Box::new((*cause, locator).into())),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -813,10 +885,22 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::Try { node: StmtKind::Try {
body: body.into_iter().map(Into::into).collect(), body: body
handlers: handlers.into_iter().map(Into::into).collect(), .into_iter()
orelse: orelse.into_iter().map(Into::into).collect(), .map(|node| (node, locator).into())
finalbody: finalbody.into_iter().map(Into::into).collect(), .collect(),
handlers: handlers
.into_iter()
.map(|node| (node, locator).into())
.collect(),
orelse: orelse
.into_iter()
.map(|node| (node, locator).into())
.collect(),
finalbody: finalbody
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -830,10 +914,22 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::TryStar { node: StmtKind::TryStar {
body: body.into_iter().map(Into::into).collect(), body: body
handlers: handlers.into_iter().map(Into::into).collect(), .into_iter()
orelse: orelse.into_iter().map(Into::into).collect(), .map(|node| (node, locator).into())
finalbody: finalbody.into_iter().map(Into::into).collect(), .collect(),
handlers: handlers
.into_iter()
.map(|node| (node, locator).into())
.collect(),
orelse: orelse
.into_iter()
.map(|node| (node, locator).into())
.collect(),
finalbody: finalbody
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -842,7 +938,10 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
location: stmt.location, location: stmt.location,
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::Import { node: StmtKind::Import {
names: names.into_iter().map(Into::into).collect(), names: names
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -856,7 +955,10 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
end_location: stmt.end_location, end_location: stmt.end_location,
node: StmtKind::ImportFrom { node: StmtKind::ImportFrom {
module, module,
names: names.into_iter().map(Into::into).collect(), names: names
.into_iter()
.map(|node| (node, locator).into())
.collect(),
level, level,
}, },
trivia: vec![], trivia: vec![],
@ -894,14 +996,14 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
} }
} }
impl From<rustpython_parser::ast::Keyword> for Keyword { impl From<(rustpython_parser::ast::Keyword, &Locator<'_>)> for Keyword {
fn from(keyword: rustpython_parser::ast::Keyword) -> Self { fn from((keyword, locator): (rustpython_parser::ast::Keyword, &Locator)) -> Self {
Keyword { Keyword {
location: keyword.location, location: keyword.location,
end_location: keyword.end_location, end_location: keyword.end_location,
node: KeywordData { node: KeywordData {
arg: keyword.node.arg, arg: keyword.node.arg,
value: keyword.node.value.into(), value: (keyword.node.value, locator).into(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -909,14 +1011,17 @@ impl From<rustpython_parser::ast::Keyword> for Keyword {
} }
} }
impl From<rustpython_parser::ast::Arg> for Arg { impl From<(rustpython_parser::ast::Arg, &Locator<'_>)> for Arg {
fn from(arg: rustpython_parser::ast::Arg) -> Self { fn from((arg, locator): (rustpython_parser::ast::Arg, &Locator)) -> Self {
Arg { Arg {
location: arg.location, location: arg.location,
end_location: arg.end_location, end_location: arg.end_location,
node: ArgData { node: ArgData {
arg: arg.node.arg, arg: arg.node.arg,
annotation: arg.node.annotation.map(|a| Box::new((*a).into())), annotation: arg
.node
.annotation
.map(|node| Box::new((*node, locator).into())),
type_comment: arg.node.type_comment, type_comment: arg.node.type_comment,
}, },
trivia: vec![], trivia: vec![],
@ -925,33 +1030,61 @@ impl From<rustpython_parser::ast::Arg> for Arg {
} }
} }
impl From<rustpython_parser::ast::Arguments> for Arguments { impl From<(rustpython_parser::ast::Arguments, &Locator<'_>)> for Arguments {
fn from(arguments: rustpython_parser::ast::Arguments) -> Self { fn from((arguments, locator): (rustpython_parser::ast::Arguments, &Locator)) -> Self {
Arguments { Arguments {
posonlyargs: arguments.posonlyargs.into_iter().map(Into::into).collect(), posonlyargs: arguments
args: arguments.args.into_iter().map(Into::into).collect(), .posonlyargs
vararg: arguments.vararg.map(|v| Box::new((*v).into())), .into_iter()
kwonlyargs: arguments.kwonlyargs.into_iter().map(Into::into).collect(), .map(|node| (node, locator).into())
kw_defaults: arguments.kw_defaults.into_iter().map(Into::into).collect(), .collect(),
kwarg: arguments.kwarg.map(|k| Box::new((*k).into())), args: arguments
defaults: arguments.defaults.into_iter().map(Into::into).collect(), .args
.into_iter()
.map(|node| (node, locator).into())
.collect(),
vararg: arguments
.vararg
.map(|node| Box::new((*node, locator).into())),
kwonlyargs: arguments
.kwonlyargs
.into_iter()
.map(|node| (node, locator).into())
.collect(),
kw_defaults: arguments
.kw_defaults
.into_iter()
.map(|node| (node, locator).into())
.collect(),
kwarg: arguments
.kwarg
.map(|node| Box::new((*node, locator).into())),
defaults: arguments
.defaults
.into_iter()
.map(|node| (node, locator).into())
.collect(),
} }
} }
} }
impl From<rustpython_parser::ast::Comprehension> for Comprehension { impl From<(rustpython_parser::ast::Comprehension, &Locator<'_>)> for Comprehension {
fn from(comprehension: rustpython_parser::ast::Comprehension) -> Self { fn from((comprehension, locator): (rustpython_parser::ast::Comprehension, &Locator)) -> Self {
Comprehension { Comprehension {
target: comprehension.target.into(), target: (comprehension.target, locator).into(),
iter: comprehension.iter.into(), iter: (comprehension.iter, locator).into(),
ifs: comprehension.ifs.into_iter().map(Into::into).collect(), ifs: comprehension
.ifs
.into_iter()
.map(|node| (node, locator).into())
.collect(),
is_async: comprehension.is_async, is_async: comprehension.is_async,
} }
} }
} }
impl From<rustpython_parser::ast::Expr> for Expr { impl From<(rustpython_parser::ast::Expr, &Locator<'_>)> for Expr {
fn from(expr: rustpython_parser::ast::Expr) -> Self { fn from((expr, locator): (rustpython_parser::ast::Expr, &Locator)) -> Self {
match expr.node { match expr.node {
rustpython_parser::ast::ExprKind::Name { id, ctx } => Expr { rustpython_parser::ast::ExprKind::Name { id, ctx } => Expr {
location: expr.location, location: expr.location,
@ -968,7 +1101,10 @@ impl From<rustpython_parser::ast::Expr> for Expr {
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::BoolOp { node: ExprKind::BoolOp {
op: op.into(), op: op.into(),
values: values.into_iter().map(Into::into).collect(), values: values
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -977,8 +1113,8 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::NamedExpr { node: ExprKind::NamedExpr {
target: Box::new((*target).into()), target: Box::new((*target, locator).into()),
value: Box::new((*value).into()), value: Box::new((*value, locator).into()),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -987,9 +1123,9 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::BinOp { node: ExprKind::BinOp {
left: Box::new((*left).into()), left: Box::new((*left, locator).into()),
op: op.into(), op: op.into(),
right: Box::new((*right).into()), right: Box::new((*right, locator).into()),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -999,7 +1135,7 @@ impl From<rustpython_parser::ast::Expr> for Expr {
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::UnaryOp { node: ExprKind::UnaryOp {
op: op.into(), op: op.into(),
operand: Box::new((*operand).into()), operand: Box::new((*operand, locator).into()),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1008,8 +1144,8 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::Lambda { node: ExprKind::Lambda {
args: Box::new((*args).into()), args: Box::new((*args, locator).into()),
body: Box::new((*body).into()), body: Box::new((*body, locator).into()),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1018,9 +1154,9 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::IfExp { node: ExprKind::IfExp {
test: Box::new((*test).into()), test: Box::new((*test, locator).into()),
body: Box::new((*body).into()), body: Box::new((*body, locator).into()),
orelse: Box::new((*orelse).into()), orelse: Box::new((*orelse, locator).into()),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1029,8 +1165,14 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::Dict { node: ExprKind::Dict {
keys: keys.into_iter().map(|key| key.map(Into::into)).collect(), keys: keys
values: values.into_iter().map(Into::into).collect(), .into_iter()
.map(|key| key.map(|node| (node, locator).into()))
.collect(),
values: values
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1039,7 +1181,10 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::Set { node: ExprKind::Set {
elts: elts.into_iter().map(Into::into).collect(), elts: elts
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1048,8 +1193,11 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::ListComp { node: ExprKind::ListComp {
elt: Box::new((*elt).into()), elt: Box::new((*elt, locator).into()),
generators: generators.into_iter().map(Into::into).collect(), generators: generators
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1058,8 +1206,11 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::SetComp { node: ExprKind::SetComp {
elt: Box::new((*elt).into()), elt: Box::new((*elt, locator).into()),
generators: generators.into_iter().map(Into::into).collect(), generators: generators
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1072,9 +1223,12 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::DictComp { node: ExprKind::DictComp {
key: Box::new((*key).into()), key: Box::new((*key, locator).into()),
value: Box::new((*value).into()), value: Box::new((*value, locator).into()),
generators: generators.into_iter().map(Into::into).collect(), generators: generators
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1083,8 +1237,11 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::GeneratorExp { node: ExprKind::GeneratorExp {
elt: Box::new((*elt).into()), elt: Box::new((*elt, locator).into()),
generators: generators.into_iter().map(Into::into).collect(), generators: generators
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1093,7 +1250,7 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::Await { node: ExprKind::Await {
value: Box::new((*value).into()), value: Box::new((*value, locator).into()),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1102,7 +1259,7 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::Yield { node: ExprKind::Yield {
value: value.map(|v| Box::new((*v).into())), value: value.map(|v| Box::new((*v, locator).into())),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1111,7 +1268,7 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::YieldFrom { node: ExprKind::YieldFrom {
value: Box::new((*value).into()), value: Box::new((*value, locator).into()),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1124,9 +1281,12 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::Compare { node: ExprKind::Compare {
left: Box::new((*left).into()), left: Box::new((*left, locator).into()),
ops: ops.into_iter().map(Into::into).collect(), ops: ops.into_iter().map(Into::into).collect(),
comparators: comparators.into_iter().map(Into::into).collect(), comparators: comparators
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1139,9 +1299,15 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::Call { node: ExprKind::Call {
func: Box::new((*func).into()), func: Box::new((*func, locator).into()),
args: args.into_iter().map(Into::into).collect(), args: args
keywords: keywords.into_iter().map(Into::into).collect(), .into_iter()
.map(|node| (node, locator).into())
.collect(),
keywords: keywords
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1154,9 +1320,9 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::FormattedValue { node: ExprKind::FormattedValue {
value: Box::new((*value).into()), value: Box::new((*value, locator).into()),
conversion, conversion,
format_spec: format_spec.map(|f| Box::new((*f).into())), format_spec: format_spec.map(|f| Box::new((*f, locator).into())),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1165,7 +1331,10 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::JoinedStr { node: ExprKind::JoinedStr {
values: values.into_iter().map(Into::into).collect(), values: values
.into_iter()
.map(|node| (node, locator).into())
.collect(),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,
@ -1181,7 +1350,7 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::Attribute { node: ExprKind::Attribute {
value: Box::new((*value).into()), value: Box::new((*value, locator).into()),
attr, attr,
ctx: ctx.into(), ctx: ctx.into(),
}, },
@ -1192,8 +1361,8 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::Subscript { node: ExprKind::Subscript {
value: Box::new((*value).into()), value: Box::new((*value, locator).into()),
slice: Box::new((*slice).into()), slice: Box::new((*slice, locator).into()),
ctx: ctx.into(), ctx: ctx.into(),
}, },
trivia: vec![], trivia: vec![],
@ -1203,7 +1372,7 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::Starred { node: ExprKind::Starred {
value: Box::new((*value).into()), value: Box::new((*value, locator).into()),
ctx: ctx.into(), ctx: ctx.into(),
}, },
trivia: vec![], trivia: vec![],
@ -1213,7 +1382,10 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::List { node: ExprKind::List {
elts: elts.into_iter().map(Into::into).collect(), elts: elts
.into_iter()
.map(|node| (node, locator).into())
.collect(),
ctx: ctx.into(), ctx: ctx.into(),
}, },
trivia: vec![], trivia: vec![],
@ -1223,7 +1395,10 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::Tuple { node: ExprKind::Tuple {
elts: elts.into_iter().map(Into::into).collect(), elts: elts
.into_iter()
.map(|node| (node, locator).into())
.collect(),
ctx: ctx.into(), ctx: ctx.into(),
}, },
trivia: vec![], trivia: vec![],
@ -1233,9 +1408,9 @@ impl From<rustpython_parser::ast::Expr> for Expr {
location: expr.location, location: expr.location,
end_location: expr.end_location, end_location: expr.end_location,
node: ExprKind::Slice { node: ExprKind::Slice {
lower: lower.map(|l| Box::new((*l).into())), lower: lower.map(|node| Box::new((*node, locator).into())),
upper: upper.map(|u| Box::new((*u).into())), upper: upper.map(|node| Box::new((*node, locator).into())),
step: step.map(|s| Box::new((*s).into())), step: step.map(|node| Box::new((*node, locator).into())),
}, },
trivia: vec![], trivia: vec![],
parentheses: Parenthesize::Never, parentheses: Parenthesize::Never,

View file

@ -1,7 +1,8 @@
use anyhow::Result; use anyhow::Result;
use ruff_formatter::{format, Formatted, IndentStyle, SimpleFormatOptions};
use rustpython_parser::lexer::LexResult; use rustpython_parser::lexer::LexResult;
use ruff_formatter::{format, Formatted, IndentStyle, SimpleFormatOptions};
use crate::attachment::attach; use crate::attachment::attach;
use crate::context::ASTFormatContext; use crate::context::ASTFormatContext;
use crate::core::locator::Locator; use crate::core::locator::Locator;
@ -35,7 +36,10 @@ pub fn fmt(contents: &str) -> Result<Formatted<ASTFormatContext>> {
let python_ast = ruff_rustpython::parse_program_tokens(tokens, "<filename>")?; let python_ast = ruff_rustpython::parse_program_tokens(tokens, "<filename>")?;
// Convert to a CST. // Convert to a CST.
let mut python_cst: Vec<Stmt> = python_ast.into_iter().map(Into::into).collect(); let mut python_cst: Vec<Stmt> = python_ast
.into_iter()
.map(|stmt| (stmt, &locator).into())
.collect();
// Attach trivia. // Attach trivia.
attach(&mut python_cst, trivia); attach(&mut python_cst, trivia);
@ -57,15 +61,16 @@ pub fn fmt(contents: &str) -> Result<Formatted<ASTFormatContext>> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::fmt::{Formatter, Write};
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use anyhow::Result; use anyhow::Result;
use similar::TextDiff;
use ruff_testing_macros::fixture;
use crate::fmt; use crate::fmt;
use ruff_testing_macros::fixture;
use similar::TextDiff;
use std::fmt::{Formatter, Write};
#[fixture( #[fixture(
pattern = "resources/test/fixtures/black/**/*.py", pattern = "resources/test/fixtures/black/**/*.py",