diff --git a/Cargo.lock b/Cargo.lock index 79d0f421e41e0..0479869e7a236 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2049,7 +2049,7 @@ dependencies = [ [[package]] name = "ruff_text_size" version = "0.0.0" -source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=335780aeeac1e6fcd85994ba001d7b8ce99fcf65#335780aeeac1e6fcd85994ba001d7b8ce99fcf65" +source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd#7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd" dependencies = [ "schemars", "serde", @@ -2119,7 +2119,7 @@ dependencies = [ [[package]] name = "rustpython-ast" version = "0.2.0" -source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=335780aeeac1e6fcd85994ba001d7b8ce99fcf65#335780aeeac1e6fcd85994ba001d7b8ce99fcf65" +source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd#7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd" dependencies = [ "is-macro", "num-bigint", @@ -2130,7 +2130,7 @@ dependencies = [ [[package]] name = "rustpython-format" version = "0.2.0" -source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=335780aeeac1e6fcd85994ba001d7b8ce99fcf65#335780aeeac1e6fcd85994ba001d7b8ce99fcf65" +source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd#7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd" dependencies = [ "bitflags 2.3.1", "itertools", @@ -2142,7 +2142,7 @@ dependencies = [ [[package]] name = "rustpython-literal" version = "0.2.0" -source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=335780aeeac1e6fcd85994ba001d7b8ce99fcf65#335780aeeac1e6fcd85994ba001d7b8ce99fcf65" +source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd#7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd" dependencies = [ "hexf-parse", "is-macro", @@ -2154,7 +2154,7 @@ dependencies = [ [[package]] name = "rustpython-parser" version = "0.2.0" -source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=335780aeeac1e6fcd85994ba001d7b8ce99fcf65#335780aeeac1e6fcd85994ba001d7b8ce99fcf65" +source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd#7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd" dependencies = [ "anyhow", "is-macro", @@ -2177,7 +2177,7 @@ dependencies = [ [[package]] name = "rustpython-parser-core" version = "0.2.0" -source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=335780aeeac1e6fcd85994ba001d7b8ce99fcf65#335780aeeac1e6fcd85994ba001d7b8ce99fcf65" +source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd#7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd" dependencies = [ "is-macro", "ruff_text_size", diff --git a/Cargo.toml b/Cargo.toml index 5893749df6de2..ab91b3ac040c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,11 +34,11 @@ proc-macro2 = { version = "1.0.51" } quote = { version = "1.0.23" } regex = { version = "1.7.1" } rustc-hash = { version = "1.1.0" } -ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "335780aeeac1e6fcd85994ba001d7b8ce99fcf65" } -rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "335780aeeac1e6fcd85994ba001d7b8ce99fcf65", default-features = false, features = ["all-nodes-with-ranges"]} -rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "335780aeeac1e6fcd85994ba001d7b8ce99fcf65" } -rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "335780aeeac1e6fcd85994ba001d7b8ce99fcf65" } -rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "335780aeeac1e6fcd85994ba001d7b8ce99fcf65", default-features = false, features = ["full-lexer", "all-nodes-with-ranges"] } +ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd" } +rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd", default-features = false, features = ["all-nodes-with-ranges"]} +rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd" } +rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd" } +rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd", default-features = false, features = ["full-lexer", "all-nodes-with-ranges"] } schemars = { version = "0.8.12" } serde = { version = "1.0.152", features = ["derive"] } serde_json = { version = "1.0.93", features = ["preserve_order"] } diff --git a/crates/ruff/src/autofix/actions.rs b/crates/ruff/src/autofix/actions.rs index afa61a274f6f7..95161c98aeb1e 100644 --- a/crates/ruff/src/autofix/actions.rs +++ b/crates/ruff/src/autofix/actions.rs @@ -424,7 +424,8 @@ pub(crate) fn remove_argument( mod tests { use anyhow::Result; use ruff_text_size::TextSize; - use rustpython_parser as parser; + use rustpython_parser::ast::Suite; + use rustpython_parser::Parse; use ruff_python_ast::source_code::Locator; @@ -433,19 +434,19 @@ mod tests { #[test] fn find_semicolon() -> Result<()> { let contents = "x = 1"; - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); assert_eq!(trailing_semicolon(stmt, &locator), None); let contents = "x = 1; y = 1"; - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); assert_eq!(trailing_semicolon(stmt, &locator), Some(TextSize::from(5))); let contents = "x = 1 ; y = 1"; - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); assert_eq!(trailing_semicolon(stmt, &locator), Some(TextSize::from(6))); @@ -455,7 +456,7 @@ x = 1 \ ; y = 1 "# .trim(); - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); assert_eq!(trailing_semicolon(stmt, &locator), Some(TextSize::from(10))); diff --git a/crates/ruff/src/importer/insertion.rs b/crates/ruff/src/importer/insertion.rs index 6ebd3d6028722..54d69addcc70a 100644 --- a/crates/ruff/src/importer/insertion.rs +++ b/crates/ruff/src/importer/insertion.rs @@ -139,8 +139,9 @@ fn match_docstring_end(body: &[Stmt]) -> Option { mod tests { use anyhow::Result; use ruff_text_size::TextSize; - use rustpython_parser as parser; + use rustpython_parser::ast::Suite; use rustpython_parser::lexer::LexResult; + use rustpython_parser::Parse; use ruff_python_ast::newlines::LineEnding; use ruff_python_ast::source_code::{Locator, Stylist}; @@ -148,7 +149,7 @@ mod tests { use super::Insertion; fn insert(contents: &str) -> Result { - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let tokens: Vec = ruff_rustpython::tokenize(contents); let locator = Locator::new(contents); let stylist = Stylist::from_tokens(&tokens, &locator); diff --git a/crates/ruff/src/rules/eradicate/detection.rs b/crates/ruff/src/rules/eradicate/detection.rs index be063c7353dcc..4f3a77f15a924 100644 --- a/crates/ruff/src/rules/eradicate/detection.rs +++ b/crates/ruff/src/rules/eradicate/detection.rs @@ -1,7 +1,8 @@ /// See: [eradicate.py](https://github.com/myint/eradicate/blob/98f199940979c94447a461d50d27862b118b282d/eradicate.py) use once_cell::sync::Lazy; use regex::Regex; -use rustpython_parser as parser; +use rustpython_parser::ast::Suite; +use rustpython_parser::Parse; static ALLOWLIST_REGEX: Lazy = Lazy::new(|| { Regex::new( @@ -78,7 +79,7 @@ pub(crate) fn comment_contains_code(line: &str, task_tags: &[String]) -> bool { } // Finally, compile the source code. - parser::parse_program(&line, "").is_ok() + Suite::parse(&line, "").is_ok() } /// Returns `true` if a line is probably part of some multiline code. diff --git a/crates/ruff/src/rules/isort/rules/add_required_imports.rs b/crates/ruff/src/rules/isort/rules/add_required_imports.rs index 08f56d2c318cc..1d05832f1ffc5 100644 --- a/crates/ruff/src/rules/isort/rules/add_required_imports.rs +++ b/crates/ruff/src/rules/isort/rules/add_required_imports.rs @@ -1,7 +1,7 @@ use log::error; use ruff_text_size::{TextRange, TextSize}; -use rustpython_parser as parser; use rustpython_parser::ast::{self, Stmt, Suite}; +use rustpython_parser::Parse; use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix}; use ruff_macros::{derive_message_formats, violation}; @@ -140,7 +140,7 @@ pub(crate) fn add_required_imports( .required_imports .iter() .flat_map(|required_import| { - let Ok(body) = parser::parse_program(required_import, "") else { + let Ok(body) = Suite::parse(required_import, "") else { error!("Failed to parse required import: `{}`", required_import); return vec![]; }; diff --git a/crates/ruff/src/rules/mccabe/rules/function_is_too_complex.rs b/crates/ruff/src/rules/mccabe/rules/function_is_too_complex.rs index 9e0599118f590..0311470ac7a03 100644 --- a/crates/ruff/src/rules/mccabe/rules/function_is_too_complex.rs +++ b/crates/ruff/src/rules/mccabe/rules/function_is_too_complex.rs @@ -162,7 +162,8 @@ pub(crate) fn function_is_too_complex( #[cfg(test)] mod tests { use anyhow::Result; - use rustpython_parser as parser; + use rustpython_parser::ast::Suite; + use rustpython_parser::Parse; use super::get_complexity_number; @@ -172,7 +173,7 @@ mod tests { def trivial(): pass "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 1); Ok(()) } @@ -183,7 +184,7 @@ def trivial(): def expr_as_statement(): 0xF00D "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 1); Ok(()) } @@ -196,7 +197,7 @@ def sequential(n): s = k + n return s "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 1); Ok(()) } @@ -212,7 +213,7 @@ def if_elif_else_dead_path(n): else: return "smaller than or equal to three" "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 3); Ok(()) } @@ -229,7 +230,7 @@ def nested_ifs(): else: return "smaller than or equal to three" "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 3); Ok(()) } @@ -241,7 +242,7 @@ def for_loop(): for i in range(10): print(i) "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 2); Ok(()) } @@ -255,7 +256,7 @@ def for_else(mylist): else: print(None) "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 2); Ok(()) } @@ -269,7 +270,7 @@ def recursive(n): else: return n "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 2); Ok(()) } @@ -286,7 +287,7 @@ def nested_functions(): a() "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 3); Ok(()) } @@ -304,7 +305,7 @@ def try_else(): else: print(4) "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 4); Ok(()) } @@ -321,7 +322,7 @@ def nested_try_finally(): finally: print(3) "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 1); Ok(()) } @@ -338,7 +339,7 @@ async def foobar(a, b, c): async for x in a: pass "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 3); Ok(()) } @@ -349,7 +350,7 @@ async def foobar(a, b, c): def annotated_assign(): x: Any = None "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 1); Ok(()) } @@ -385,7 +386,7 @@ class Class: return ServiceProvider(Logger()) "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 9); Ok(()) } @@ -399,7 +400,7 @@ def process_detect_lines(): finally: pass "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 1); Ok(()) } @@ -414,7 +415,7 @@ def process_detect_lines(): if res: errors.append(f"Non-zero exit code {res}") "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 2); Ok(()) } @@ -427,7 +428,7 @@ def with_lock(): if foo: print('bar') "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(get_complexity_number(&stmts), 2); Ok(()) } diff --git a/crates/ruff/src/rules/pylint/rules/too_many_branches.rs b/crates/ruff/src/rules/pylint/rules/too_many_branches.rs index b597cbeae6722..200c8e14b5422 100644 --- a/crates/ruff/src/rules/pylint/rules/too_many_branches.rs +++ b/crates/ruff/src/rules/pylint/rules/too_many_branches.rs @@ -185,12 +185,13 @@ pub(crate) fn too_many_branches( #[cfg(test)] mod tests { use anyhow::Result; - use rustpython_parser as parser; + use rustpython_parser::ast::Suite; + use rustpython_parser::Parse; use super::num_branches; fn test_helper(source: &str, expected_num_branches: usize) -> Result<()> { - let branches = parser::parse_program(source, "")?; + let branches = Suite::parse(source, "")?; assert_eq!(num_branches(&branches), expected_num_branches); Ok(()) } diff --git a/crates/ruff/src/rules/pylint/rules/too_many_return_statements.rs b/crates/ruff/src/rules/pylint/rules/too_many_return_statements.rs index 9ba5dd2202dcf..2a4851706756a 100644 --- a/crates/ruff/src/rules/pylint/rules/too_many_return_statements.rs +++ b/crates/ruff/src/rules/pylint/rules/too_many_return_statements.rs @@ -99,12 +99,13 @@ pub(crate) fn too_many_return_statements( #[cfg(test)] mod tests { use anyhow::Result; - use rustpython_parser as parser; + use rustpython_parser::ast::Suite; + use rustpython_parser::Parse; use super::num_returns; fn test_helper(source: &str, expected: usize) -> Result<()> { - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_returns(&stmts), expected); Ok(()) } diff --git a/crates/ruff/src/rules/pylint/rules/too_many_statements.rs b/crates/ruff/src/rules/pylint/rules/too_many_statements.rs index 6912ca0c102b6..9991ca3215909 100644 --- a/crates/ruff/src/rules/pylint/rules/too_many_statements.rs +++ b/crates/ruff/src/rules/pylint/rules/too_many_statements.rs @@ -168,7 +168,8 @@ pub(crate) fn too_many_statements( #[cfg(test)] mod tests { use anyhow::Result; - use rustpython_parser as parser; + use rustpython_parser::ast::Suite; + use rustpython_parser::Parse; use super::num_statements; @@ -178,7 +179,7 @@ mod tests { def f(): # 2 pass "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 2); Ok(()) } @@ -192,7 +193,7 @@ def f(): else: print() "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 5); Ok(()) } @@ -207,7 +208,7 @@ def f(): if a: print() "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 5); Ok(()) } @@ -221,7 +222,7 @@ def f(): # 5 elif a: print() "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 5); Ok(()) } @@ -239,7 +240,7 @@ def f(): # 9 else: print() "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 9); Ok(()) } @@ -268,7 +269,7 @@ async def f(): # 19 import time pass "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 19); Ok(()) } @@ -280,7 +281,7 @@ def f(): # 2 for i in range(10): pass "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 2); Ok(()) } @@ -294,7 +295,7 @@ def f(): # 3 else: print() "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 3); Ok(()) } @@ -309,7 +310,7 @@ def f(): # 5 print() "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 5); Ok(()) } @@ -327,7 +328,7 @@ def f(): # 3 print() "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 3); Ok(()) } @@ -338,7 +339,7 @@ def f(): # 3 def f(): return "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 1); Ok(()) } @@ -354,7 +355,7 @@ def f(): # 6 print() "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 6); Ok(()) } @@ -368,7 +369,7 @@ def f(): # 5 except Exception: raise "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 5); Ok(()) } @@ -384,7 +385,7 @@ def f(): # 7 else: print() "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 7); Ok(()) } @@ -402,7 +403,7 @@ def f(): # 10 finally: pass "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 10); Ok(()) } @@ -418,7 +419,7 @@ def f(): # 8 except Exception: raise "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 8); Ok(()) } @@ -436,7 +437,7 @@ def f(): # 11 finally: print() "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 11); Ok(()) } @@ -448,7 +449,7 @@ def f(): # 2 for i in range(10): yield i "#; - let stmts = parser::parse_program(source, "")?; + let stmts = Suite::parse(source, "")?; assert_eq!(num_statements(&stmts), 2); Ok(()) } diff --git a/crates/ruff_benchmark/benches/parser.rs b/crates/ruff_benchmark/benches/parser.rs index 1dbd95af424a8..e08836e685f5d 100644 --- a/crates/ruff_benchmark/benches/parser.rs +++ b/crates/ruff_benchmark/benches/parser.rs @@ -2,10 +2,10 @@ use std::time::Duration; use criterion::measurement::WallTime; use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; -use rustpython_parser::ast::Stmt; - use ruff_benchmark::{TestCase, TestCaseSpeed, TestFile, TestFileDownloadError}; use ruff_python_ast::statement_visitor::{walk_stmt, StatementVisitor}; +use rustpython_parser::ast::{Stmt, Suite}; +use rustpython_parser::Parse; #[cfg(target_os = "windows")] #[global_allocator] @@ -66,8 +66,7 @@ fn benchmark_parser(criterion: &mut Criterion) { &case, |b, case| { b.iter(|| { - let parsed = - rustpython_parser::parse_program(case.code(), case.name()).unwrap(); + let parsed = Suite::parse(case.code(), case.name()).unwrap(); let mut visitor = CountVisitor { count: 0 }; visitor.visit_body(&parsed); diff --git a/crates/ruff_dev/src/print_ast.rs b/crates/ruff_dev/src/print_ast.rs index 4556f133c9960..d19f07a373c50 100644 --- a/crates/ruff_dev/src/print_ast.rs +++ b/crates/ruff_dev/src/print_ast.rs @@ -5,7 +5,8 @@ use std::fs; use std::path::PathBuf; use anyhow::Result; -use rustpython_parser as parser; +use rustpython_parser::ast::Suite; +use rustpython_parser::Parse; #[derive(clap::Args)] pub(crate) struct Args { @@ -16,7 +17,7 @@ pub(crate) struct Args { pub(crate) fn main(args: &Args) -> Result<()> { let contents = fs::read_to_string(&args.file)?; - let python_ast = parser::parse_program(&contents, &args.file.to_string_lossy())?; + let python_ast = Suite::parse(&contents, &args.file.to_string_lossy())?; println!("{python_ast:#?}"); Ok(()) } diff --git a/crates/ruff_python_ast/src/helpers.rs b/crates/ruff_python_ast/src/helpers.rs index 25c105b032438..10142c7502dbd 100644 --- a/crates/ruff_python_ast/src/helpers.rs +++ b/crates/ruff_python_ast/src/helpers.rs @@ -1596,8 +1596,9 @@ mod tests { use anyhow::Result; use ruff_text_size::{TextLen, TextRange, TextSize}; - use rustpython_parser as parser; + use rustpython_ast::Suite; use rustpython_parser::ast::Cmpop; + use rustpython_parser::Parse; use crate::helpers::{ elif_else_range, else_range, first_colon_range, has_trailing_content, identifier_range, @@ -1608,25 +1609,25 @@ mod tests { #[test] fn trailing_content() -> Result<()> { let contents = "x = 1"; - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); assert!(!has_trailing_content(stmt, &locator)); let contents = "x = 1; y = 2"; - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); assert!(has_trailing_content(stmt, &locator)); let contents = "x = 1 "; - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); assert!(!has_trailing_content(stmt, &locator)); let contents = "x = 1 # Comment"; - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); assert!(!has_trailing_content(stmt, &locator)); @@ -1636,7 +1637,7 @@ x = 1 y = 2 "# .trim(); - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); assert!(!has_trailing_content(stmt, &locator)); @@ -1647,7 +1648,7 @@ y = 2 #[test] fn extract_identifier_range() -> Result<()> { let contents = "def f(): pass".trim(); - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); assert_eq!( @@ -1661,7 +1662,7 @@ def \ pass "# .trim(); - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); assert_eq!( @@ -1670,7 +1671,7 @@ def \ ); let contents = "class Class(): pass".trim(); - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); assert_eq!( @@ -1679,7 +1680,7 @@ def \ ); let contents = "class Class: pass".trim(); - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); assert_eq!( @@ -1693,7 +1694,7 @@ class Class(): pass "# .trim(); - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); assert_eq!( @@ -1702,7 +1703,7 @@ class Class(): ); let contents = r#"x = y + 1"#.trim(); - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); assert_eq!( @@ -1759,7 +1760,7 @@ else: pass "# .trim(); - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); let range = else_range(stmt, &locator).unwrap(); @@ -1793,7 +1794,7 @@ elif b: ... " .trim_start(); - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); let range = elif_else_range(stmt, &locator).unwrap(); @@ -1807,7 +1808,7 @@ else: ... " .trim_start(); - let program = parser::parse_program(contents, "")?; + let program = Suite::parse(contents, "")?; let stmt = program.first().unwrap(); let locator = Locator::new(contents); let range = elif_else_range(stmt, &locator).unwrap(); diff --git a/crates/ruff_python_ast/src/source_code/generator.rs b/crates/ruff_python_ast/src/source_code/generator.rs index af36d7eaa549d..90a52da10867d 100644 --- a/crates/ruff_python_ast/src/source_code/generator.rs +++ b/crates/ruff_python_ast/src/source_code/generator.rs @@ -1456,7 +1456,8 @@ impl<'a> Generator<'a> { #[cfg(test)] mod tests { - use rustpython_parser as parser; + use rustpython_ast::Suite; + use rustpython_parser::Parse; use crate::newlines::LineEnding; use crate::source_code::stylist::{Indentation, Quote}; @@ -1466,7 +1467,7 @@ mod tests { let indentation = Indentation::default(); let quote = Quote::default(); let line_ending = LineEnding::default(); - let program = parser::parse_program(contents, "").unwrap(); + let program = Suite::parse(contents, "").unwrap(); let stmt = program.first().unwrap(); let mut generator = Generator::new(&indentation, quote, line_ending); generator.unparse_stmt(stmt); @@ -1479,7 +1480,7 @@ mod tests { line_ending: LineEnding, contents: &str, ) -> String { - let program = parser::parse_program(contents, "").unwrap(); + let program = Suite::parse(contents, "").unwrap(); let stmt = program.first().unwrap(); let mut generator = Generator::new(indentation, quote, line_ending); generator.unparse_stmt(stmt); diff --git a/crates/ruff_python_ast/src/source_code/mod.rs b/crates/ruff_python_ast/src/source_code/mod.rs index cce0a7636a7c3..8341f07c35a69 100644 --- a/crates/ruff_python_ast/src/source_code/mod.rs +++ b/crates/ruff_python_ast/src/source_code/mod.rs @@ -3,8 +3,7 @@ use std::fmt::{Debug, Formatter}; use std::sync::Arc; use ruff_text_size::{TextRange, TextSize}; -use rustpython_parser as parser; -use rustpython_parser::{lexer, Mode, ParseError}; +use rustpython_parser::{ast, lexer, Mode, Parse, ParseError}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -26,7 +25,7 @@ mod stylist; /// Run round-trip source code generation on a given Python code. pub fn round_trip(code: &str, source_path: &str) -> Result { let locator = Locator::new(code); - let python_ast = parser::parse_program(code, source_path)?; + let python_ast = ast::Suite::parse(code, source_path)?; let tokens: Vec<_> = lexer::lex(code, Mode::Module).collect(); let stylist = Stylist::from_tokens(&tokens, &locator); let mut generator: Generator = (&stylist).into(); diff --git a/crates/ruff_python_ast/src/typing.rs b/crates/ruff_python_ast/src/typing.rs index 58540e6d6a187..dacf74e078ec4 100644 --- a/crates/ruff_python_ast/src/typing.rs +++ b/crates/ruff_python_ast/src/typing.rs @@ -1,7 +1,7 @@ use anyhow::Result; use ruff_text_size::{TextLen, TextRange}; -use rustpython_parser as parser; use rustpython_parser::ast::Expr; +use rustpython_parser::Parse; use crate::relocate::relocate_expr; use crate::source_code::Locator; @@ -35,7 +35,7 @@ pub fn parse_type_annotation( // isn't the case, e.g., for implicit concatenations, or for annotations that contain // escaped quotes. let leading_quote = str::leading_quote(expression).unwrap(); - let expr = parser::parse_expression_starts_at( + let expr = Expr::parse_starts_at( value, "", range.start() + leading_quote.text_len(), @@ -43,7 +43,7 @@ pub fn parse_type_annotation( Ok((expr, AnnotationKind::Simple)) } else { // Otherwise, consider this a "complex" annotation. - let mut expr = parser::parse_expression(value, "")?; + let mut expr = Expr::parse(value, "")?; relocate_expr(&mut expr, range); Ok((expr, AnnotationKind::Complex)) } diff --git a/crates/ruff_python_formatter/src/comments/debug.rs b/crates/ruff_python_formatter/src/comments/debug.rs index 4ea7a1ef1a2ec..ff8b3e7adc6ce 100644 --- a/crates/ruff_python_formatter/src/comments/debug.rs +++ b/crates/ruff_python_formatter/src/comments/debug.rs @@ -214,6 +214,7 @@ break; continue_statement.as_ref().into(), SourceComment { slice: source_code.slice(TextRange::at(TextSize::new(0), TextSize::new(17))), + #[cfg(debug_assertions)] formatted: Cell::new(false), position: CommentTextPosition::OwnLine, }, @@ -223,6 +224,7 @@ break; continue_statement.as_ref().into(), SourceComment { slice: source_code.slice(TextRange::at(TextSize::new(28), TextSize::new(10))), + #[cfg(debug_assertions)] formatted: Cell::new(false), position: CommentTextPosition::EndOfLine, }, @@ -232,6 +234,7 @@ break; break_statement.as_ref().into(), SourceComment { slice: source_code.slice(TextRange::at(TextSize::new(39), TextSize::new(15))), + #[cfg(debug_assertions)] formatted: Cell::new(false), position: CommentTextPosition::OwnLine, },