Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
Include argument parentheses in range (#5)
Browse files Browse the repository at this point in the history
# Conflicts:
#	parser/src/parser.rs
#	parser/src/python.rs
#	parser/src/snapshots/rustpython_parser__parser__tests__decorator_ranges.snap
  • Loading branch information
zanieb committed Jul 17, 2023
1 parent 6c9c5eb commit 8fd4d4f
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 41 deletions.
7 changes: 6 additions & 1 deletion parser/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ mod tests {
use super::*;
use crate::{ast, parser::ParseErrorType, Parse};

#[cfg(feature = "all-nodes-with-ranges")]
macro_rules! function_and_lambda {
($($name:ident: $code:expr,)*) => {
$(
Expand All @@ -149,6 +148,12 @@ mod tests {
}
}

#[cfg(feature = "all-nodes-with-ranges")]
function_and_lambda! {
test_function_no_args_with_ranges: "def f(): pass",
test_function_pos_args_with_ranges: "def f(a, b, c): pass",
}

#[cfg(feature = "all-nodes-with-ranges")]
function_and_lambda! {
test_function_no_args: "def f(): pass",
Expand Down
40 changes: 20 additions & 20 deletions parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,26 +1221,6 @@ def args_to_tuple(*args: *Ts) -> Tuple[*Ts]: ...
insta::assert_debug_snapshot!(parse_ast);
}

#[test]
#[cfg(not(feature = "all-nodes-with-ranges"))]
fn decorator_ranges() {
let parse_ast = parse_program(
r#"
@my_decorator
def test():
pass
@class_decorator
class Abcd:
pass
"#
.trim(),
"<test>",
)
.unwrap();
insta::assert_debug_snapshot!(parse_ast);
}

#[test]
fn test_parse_constant() {
use num_traits::ToPrimitive;
Expand All @@ -1257,4 +1237,24 @@ class Abcd:
let i = ast::Identifier::parse_without_path("test").unwrap();
assert_eq!(i.as_str(), "test");
}

#[test]
#[cfg(not(feature = "all-nodes-with-ranges"))]
fn decorator_ranges() {
let parse_ast = parse_program(
r#"
@my_decorator
def test():
pass
@class_decorator
class Abcd:
pass
"#
.trim(),
"<test>",
)
.unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
}
8 changes: 7 additions & 1 deletion parser/src/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,14 @@ TypeAliasStatement: ast::Stmt = {
Parameters: ast::Arguments = {
<location:@L> "(" <a: (ParameterList<TypedParameter, StarTypedParameter, DoubleStarTypedParameter>)?> ")" <end_location:@R> =>? {
a.as_ref().map(validate_arguments).transpose()?;

let range = optional_range(location, end_location);
let args = a
.unwrap_or_else(|| ast::Arguments::empty(optional_range(location, end_location)));
.map(|mut arguments| {
arguments.range = range;
arguments
})
.unwrap_or_else(|| ast::Arguments::empty(range));

Ok(args)
}
Expand Down
10 changes: 8 additions & 2 deletions parser/src/python.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8fd4d4f

Please sign in to comment.