mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-16 09:35:07 +00:00
Update type annotation parsing API to return Parsed
(#11739)
## Summary This PR updates the return type of `parse_type_annotation` from `Expr` to `Parsed<ModExpression>`. This is to allow accessing the tokens for the parsed sub-expression in the follow-up PR. ## Test Plan `cargo insta test`
This commit is contained in:
parent
8338db6c12
commit
eed6d784df
6 changed files with 37 additions and 25 deletions
|
@ -357,6 +357,11 @@ impl Parsed<ModExpression> {
|
|||
&self.syntax.body
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the expression contained in this parsed output.
|
||||
pub fn expr_mut(&mut self) -> &mut Expr {
|
||||
&mut self.syntax.body
|
||||
}
|
||||
|
||||
/// Consumes the [`Parsed`] output and returns the contained [`Expr`].
|
||||
pub fn into_expr(self) -> Expr {
|
||||
*self.syntax.body
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
use ruff_python_ast::relocate::relocate_expr;
|
||||
use ruff_python_ast::str::raw_contents;
|
||||
use ruff_python_ast::{Expr, ExprStringLiteral, StringFlags, StringLiteral};
|
||||
use ruff_python_ast::{ExprStringLiteral, ModExpression, StringFlags, StringLiteral};
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
use crate::{parse_expression, parse_expression_range, ParseError};
|
||||
use crate::{parse_expression, parse_expression_range, ParseError, Parsed};
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum AnnotationKind {
|
||||
|
@ -34,7 +34,7 @@ impl AnnotationKind {
|
|||
pub fn parse_type_annotation(
|
||||
string_expr: &ExprStringLiteral,
|
||||
source: &str,
|
||||
) -> Result<(Expr, AnnotationKind), ParseError> {
|
||||
) -> Result<(Parsed<ModExpression>, AnnotationKind), ParseError> {
|
||||
let expr_text = &source[string_expr.range()];
|
||||
|
||||
if let [string_literal] = string_expr.value.as_slice() {
|
||||
|
@ -58,7 +58,7 @@ pub fn parse_type_annotation(
|
|||
fn parse_simple_type_annotation(
|
||||
string_literal: &StringLiteral,
|
||||
source: &str,
|
||||
) -> Result<(Expr, AnnotationKind), ParseError> {
|
||||
) -> Result<(Parsed<ModExpression>, AnnotationKind), ParseError> {
|
||||
Ok((
|
||||
parse_expression_range(
|
||||
source,
|
||||
|
@ -66,16 +66,15 @@ fn parse_simple_type_annotation(
|
|||
.range()
|
||||
.add_start(string_literal.flags.opener_len())
|
||||
.sub_end(string_literal.flags.closer_len()),
|
||||
)?
|
||||
.into_expr(),
|
||||
)?,
|
||||
AnnotationKind::Simple,
|
||||
))
|
||||
}
|
||||
|
||||
fn parse_complex_type_annotation(
|
||||
string_expr: &ExprStringLiteral,
|
||||
) -> Result<(Expr, AnnotationKind), ParseError> {
|
||||
let mut parsed = parse_expression(string_expr.value.to_str())?.into_expr();
|
||||
relocate_expr(&mut parsed, string_expr.range());
|
||||
) -> Result<(Parsed<ModExpression>, AnnotationKind), ParseError> {
|
||||
let mut parsed = parse_expression(string_expr.value.to_str())?;
|
||||
relocate_expr(parsed.expr_mut(), string_expr.range());
|
||||
Ok((parsed, AnnotationKind::Complex))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue