[ty] Fix completion in decorator without class or function definition

This commit is contained in:
Micha Reiser
2025-12-24 16:57:36 +01:00
parent e3498121b4
commit adef89eb7c
6 changed files with 165 additions and 22 deletions

View File

@@ -2914,21 +2914,27 @@ impl<'src> Parser<'src> {
self.current_token_range(),
);
// TODO(dhruvmanila): It seems that this recovery drops all the parsed
// decorators. Maybe we could convert them into statement expression
// with a flag indicating that this expression is part of a decorator.
// It's only possible to keep them if it's a function or class definition.
// We could possibly keep them if there's indentation error:
//
// ```python
// @decorator
// @decorator
// def foo(): ...
// ```
//
// Or, parse it as a binary expression where the left side is missing.
// We would need to convert each decorator into a binary expression.
self.parse_statement()
let range = self.node_range(start);
ast::StmtFunctionDef {
node_index: Default::default(),
range,
is_async: false,
decorator_list: decorators,
name: ast::Identifier {
id: Name::empty(),
range: self.missing_node_range(),
node_index: AtomicNodeIndex::NONE,
},
type_params: None,
parameters: Box::new(ast::Parameters {
range: self.missing_node_range(),
..ast::Parameters::default()
}),
returns: None,
body: vec![],
}
.into()
}
}
}