Skip to content

Commit

Permalink
Expand NamedExpr range to include full range of parenthesized value (
Browse files Browse the repository at this point in the history
…#6632)

## Summary

Given:

```python
if (
    x
    :=
    (  # 4
        y # 5
    )  # 6
):
    pass
```

It turns out the parser ended the range of the `NamedExpr` at the end of
`y`, rather than the end of the parenthesis that encloses `y`. This just
seems like a bug -- the range should be from the start of the name on
the left, to the end of the parenthesized node on the right.

## Test Plan

`cargo test`
  • Loading branch information
charliermarsh committed Aug 17, 2023
1 parent d9bb51d commit a70807e
Show file tree
Hide file tree
Showing 4 changed files with 9,547 additions and 9,409 deletions.
7 changes: 7 additions & 0 deletions crates/ruff_python_parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,13 @@ def func[T, U: str, *Ts, **P]():
insta::assert_debug_snapshot!(parse_ast);
}

#[test]
fn test_named_expression() {
let source = "(x := ( y * z ))";
let parse_ast = parse_expression(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}

#[test]
fn test_with_statement() {
let source = "\
Expand Down
14 changes: 9 additions & 5 deletions crates/ruff_python_parser/src/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -1335,15 +1335,19 @@ NamedExpressionTest: ast::Expr = {
Test<"all">,
}

NamedExpressionName: ast::Expr = {
<location:@L> <id:Identifier> <end_location:@R> => ast::Expr::Name(
ast::ExprName { id: id.into(), ctx: ast::ExprContext::Store, range: (location..end_location).into() },
),
}

NamedExpression: ast::Expr = {
<location:@L> <id:Identifier> <end_location:@R> ":=" <value:Test<"all">> => {
<location:@L> <target:NamedExpressionName> ":=" <value:Test<"all">> <end_location:@R> => {
ast::Expr::NamedExpr(
ast::ExprNamedExpr {
target: Box::new(ast::Expr::Name(
ast::ExprName { id: id.into(), ctx: ast::ExprContext::Store, range: (location..end_location).into() },
)),
range: (location..value.end()).into(),
target: Box::new(target),
value: Box::new(value),
range: (location..end_location).into(),
}
)
},
Expand Down
Loading

0 comments on commit a70807e

Please sign in to comment.