Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[native] Don't redundantly nest StarredElement inside another Element #624

Merged
merged 1 commit into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions libcst/_nodes/tests/test_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,47 @@ class TupleTest(CSTNodeTest):
"parser": parse_expression,
"expected_position": CodeRange((1, 1), (1, 11)),
},
# top-level two-element tuple, with one being starred
{
"node": cst.SimpleStatementLine(
body=[
cst.Expr(
value=cst.Tuple(
[
cst.Element(cst.Name("one"), comma=cst.Comma()),
cst.StarredElement(cst.Name("two")),
],
lpar=[],
rpar=[],
)
)
]
),
"code": "one,*two\n",
"parser": parse_statement,
},
# top-level three-element tuple, start/end is starred
{
"node": cst.SimpleStatementLine(
body=[
cst.Expr(
value=cst.Tuple(
[
cst.StarredElement(
cst.Name("one"), comma=cst.Comma()
),
cst.Element(cst.Name("two"), comma=cst.Comma()),
cst.StarredElement(cst.Name("three")),
],
lpar=[],
rpar=[],
)
)
]
),
"code": "*one,two,*three\n",
"parser": parse_statement,
},
# missing spaces around tuple, okay with parenthesis
{
"node": cst.For(
Expand Down
9 changes: 6 additions & 3 deletions native/libcst/src/parser/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2174,9 +2174,12 @@ fn make_assignment<'a>(
}

fn expr_to_element(expr: Expression) -> Element {
Element::Simple {
value: expr,
comma: Default::default(),
match expr {
Expression::StarredElement(inner_expr) => Element::Starred(inner_expr),
_ => Element::Simple {
value: expr,
comma: Default::default(),
},
}
}

Expand Down