Fix end location of nodes containing body

This commit is contained in:
harupy 2022-12-11 12:35:28 +09:00
parent 3abdc87076
commit 1b7a272b77
3 changed files with 9 additions and 6 deletions

View file

@ -492,9 +492,10 @@ WithItem: ast::Withitem = {
};
FuncDef: ast::Stmt = {
<decorator_list:Decorator*> <location:@L> <is_async:"async"?> "def" <name:Identifier> <args:Parameters> <r:("->" Test)?> ":" <body:Suite> <end_location:@R> => {
<decorator_list:Decorator*> <location:@L> <is_async:"async"?> "def" <name:Identifier> <args:Parameters> <r:("->" Test)?> ":" <body:Suite> => {
let args = Box::new(args);
let returns = r.map(|x| Box::new(x.1));
let end_location = body.last().unwrap().end_location.unwrap();
let type_comment = None;
let node = if is_async.is_some() {
ast::StmtKind::AsyncFunctionDef { name, args, body, decorator_list, returns, type_comment }
@ -646,15 +647,16 @@ KwargParameter<ArgType>: Option<Box<ast::Arg>> = {
};
ClassDef: ast::Stmt = {
<decorator_list:Decorator*> <location:@L> "class" <name:Identifier> <a:("(" ArgumentList ")")?> ":" <body:Suite> <end_location:@R> => {
<decorator_list:Decorator*> <location:@L> "class" <name:Identifier> <a:("(" ArgumentList ")")?> ":" <body:Suite> => {
let (bases, keywords) = match a {
Some((_, arg, _)) => (arg.args, arg.keywords),
None => (vec![], vec![]),
};
let end_location = body.last().unwrap().end_location;
ast::Stmt {
custom: (),
location,
end_location: Some(end_location),
end_location,
node: ast::StmtKind::ClassDef {
name,
bases,

View file

@ -173,7 +173,8 @@ class Foo(A, B):
def __init__(self):
pass
def method_with_default(self, arg='default'):
pass";
pass
";
insta::assert_debug_snapshot!(parse_program(source, "<test>").unwrap());
}

View file

@ -62,8 +62,8 @@ expression: "parse_program(source, \"<test>\").unwrap()"
},
end_location: Some(
Location {
row: 4,
column: 1,
row: 3,
column: 6,
},
),
custom: (),