diff --git a/crates/ruff_benchmark/benches/formatter.rs b/crates/ruff_benchmark/benches/formatter.rs index 9b3bff8b6ac35..cb6db8608fc4d 100644 --- a/crates/ruff_benchmark/benches/formatter.rs +++ b/crates/ruff_benchmark/benches/formatter.rs @@ -51,13 +51,13 @@ fn benchmark_formatter(criterion: &mut Criterion) { &case, |b, case| { // Parse the source. - let program = + let parsed = parse(case.code(), Mode::Module).expect("Input should be a valid Python code"); b.iter(|| { let options = PyFormatOptions::from_extension(Path::new(case.name())) .with_preview(PreviewMode::Enabled); - let formatted = format_module_ast(&program, case.code(), options) + let formatted = format_module_ast(&parsed, case.code(), options) .expect("Formatting to succeed"); formatted.print().expect("Printing to succeed") diff --git a/crates/ruff_benchmark/benches/lexer.rs b/crates/ruff_benchmark/benches/lexer.rs index b8b464bfbb4ee..64b68a7a3539a 100644 --- a/crates/ruff_benchmark/benches/lexer.rs +++ b/crates/ruff_benchmark/benches/lexer.rs @@ -52,7 +52,7 @@ fn benchmark_lexer(criterion: &mut Criterion) { let token = lexer.next_token(); match token { TokenKind::EndOfFile => break, - TokenKind::Unknown => panic!("Input to be a valid Python program"), + TokenKind::Unknown => panic!("Input to be a valid Python source code"), _ => {} } } diff --git a/crates/ruff_benchmark/benches/linter.rs b/crates/ruff_benchmark/benches/linter.rs index 07c235be04fb5..1301d9e7cc179 100644 --- a/crates/ruff_benchmark/benches/linter.rs +++ b/crates/ruff_benchmark/benches/linter.rs @@ -55,12 +55,12 @@ fn benchmark_linter(mut group: BenchmarkGroup, settings: &LinterSettings) { &case, |b, case| { // Parse the source. - let program = + let parsed = parse_module(case.code()).expect("Input should be a valid Python code"); b.iter_batched( - || program.clone(), - |program| { + || parsed.clone(), + |parsed| { let path = case.path(); let result = lint_only( &path, @@ -69,7 +69,7 @@ fn benchmark_linter(mut group: BenchmarkGroup, settings: &LinterSettings) { flags::Noqa::Enabled, &SourceKind::Python(case.code().to_string()), PySourceType::from(path.as_path()), - ParseSource::Precomputed(program), + ParseSource::Precomputed(parsed), ); // Assert that file contains no parse errors diff --git a/crates/ruff_dev/src/print_tokens.rs b/crates/ruff_dev/src/print_tokens.rs index 0f6e3dbddda0a..c767727fdd2b1 100644 --- a/crates/ruff_dev/src/print_tokens.rs +++ b/crates/ruff_dev/src/print_tokens.rs @@ -25,8 +25,8 @@ pub(crate) fn main(args: &Args) -> Result<()> { args.file.display() ) })?; - let program = parse_unchecked_source(source_kind.source_code(), source_type); - for token in program.tokens() { + let parsed = parse_unchecked_source(source_kind.source_code(), source_type); + for token in parsed.tokens() { println!( "{start:#?} {kind:#?} {end:#?}", start = token.start(), diff --git a/crates/ruff_linter/src/checkers/ast/mod.rs b/crates/ruff_linter/src/checkers/ast/mod.rs index f9ea4c329b960..5f26244df7fff 100644 --- a/crates/ruff_linter/src/checkers/ast/mod.rs +++ b/crates/ruff_linter/src/checkers/ast/mod.rs @@ -35,7 +35,7 @@ use ruff_python_ast::{ FStringElement, Keyword, MatchCase, ModModule, Parameter, Parameters, Pattern, Stmt, Suite, UnaryOp, }; -use ruff_python_parser::Program; +use ruff_python_parser::Parsed; use ruff_text_size::{Ranged, TextRange, TextSize}; use ruff_diagnostics::{Diagnostic, IsolationLevel}; @@ -176,8 +176,8 @@ impl ExpectedDocstringKind { } pub(crate) struct Checker<'a> { - /// The parsed [`Program`]. - program: &'a Program, + /// The parsed [`Parsed`]. + parsed: &'a Parsed, /// The [`Path`] to the file under analysis. path: &'a Path, /// The [`Path`] to the package containing the current file. @@ -227,7 +227,7 @@ pub(crate) struct Checker<'a> { impl<'a> Checker<'a> { #[allow(clippy::too_many_arguments)] pub(crate) fn new( - program: &'a Program, + parsed: &'a Parsed, settings: &'a LinterSettings, noqa_line_for: &'a NoqaMapping, noqa: flags::Noqa, @@ -242,7 +242,7 @@ impl<'a> Checker<'a> { notebook_index: Option<&'a NotebookIndex>, ) -> Checker<'a> { Checker { - program, + parsed, settings, noqa_line_for, noqa, @@ -253,7 +253,7 @@ impl<'a> Checker<'a> { locator, stylist, indexer, - importer: Importer::new(program, locator, stylist), + importer: Importer::new(parsed, locator, stylist), semantic: SemanticModel::new(&settings.typing_modules, path, module), visit: deferred::Visit::default(), analyze: deferred::Analyze::default(), @@ -323,9 +323,9 @@ impl<'a> Checker<'a> { } } - /// The [`Program`] for the current file, which contains the tokens, AST, and more. - pub(crate) const fn program(&self) -> &'a Program { - self.program + /// The [`Parsed`] output for the current file, which contains the tokens, AST, and more. + pub(crate) const fn parsed(&self) -> &'a Parsed { + self.parsed } /// The [`Locator`] for the current file, which enables extraction of source code from byte @@ -2336,7 +2336,7 @@ impl<'a> Checker<'a> { #[allow(clippy::too_many_arguments)] pub(crate) fn check_ast( - program: &Program, + parsed: &Parsed, locator: &Locator, stylist: &Stylist, indexer: &Indexer, @@ -2366,11 +2366,11 @@ pub(crate) fn check_ast( } else { ModuleSource::File(path) }, - python_ast: program.suite(), + python_ast: parsed.suite(), }; let mut checker = Checker::new( - program, + parsed, settings, noqa_line_for, noqa, @@ -2387,8 +2387,8 @@ pub(crate) fn check_ast( checker.bind_builtins(); // Iterate over the AST. - checker.visit_module(program.suite()); - checker.visit_body(program.suite()); + checker.visit_module(parsed.suite()); + checker.visit_body(parsed.suite()); // Visit any deferred syntax nodes. Take care to visit in order, such that we avoid adding // new deferred nodes after visiting nodes of that kind. For example, visiting a deferred diff --git a/crates/ruff_linter/src/checkers/imports.rs b/crates/ruff_linter/src/checkers/imports.rs index b2abaef2195c2..c2cc0fccb4170 100644 --- a/crates/ruff_linter/src/checkers/imports.rs +++ b/crates/ruff_linter/src/checkers/imports.rs @@ -7,7 +7,7 @@ use ruff_python_ast::statement_visitor::StatementVisitor; use ruff_python_ast::{ModModule, PySourceType}; use ruff_python_codegen::Stylist; use ruff_python_index::Indexer; -use ruff_python_parser::Program; +use ruff_python_parser::Parsed; use ruff_source_file::Locator; use crate::directives::IsortDirectives; @@ -18,7 +18,7 @@ use crate::settings::LinterSettings; #[allow(clippy::too_many_arguments)] pub(crate) fn check_imports( - program: &Program, + parsed: &Parsed, locator: &Locator, indexer: &Indexer, directives: &IsortDirectives, @@ -32,7 +32,7 @@ pub(crate) fn check_imports( let tracker = { let mut tracker = BlockBuilder::new(locator, directives, source_type.is_stub(), cell_offsets); - tracker.visit_body(program.suite()); + tracker.visit_body(parsed.suite()); tracker }; @@ -51,7 +51,7 @@ pub(crate) fn check_imports( settings, package, source_type, - program, + parsed, ) { diagnostics.push(diagnostic); } @@ -60,7 +60,7 @@ pub(crate) fn check_imports( } if settings.rules.enabled(Rule::MissingRequiredImport) { diagnostics.extend(isort::rules::add_required_imports( - program, + parsed, locator, stylist, settings, diff --git a/crates/ruff_linter/src/checkers/physical_lines.rs b/crates/ruff_linter/src/checkers/physical_lines.rs index b2ccaf6c39b43..938c6be6e4dc2 100644 --- a/crates/ruff_linter/src/checkers/physical_lines.rs +++ b/crates/ruff_linter/src/checkers/physical_lines.rs @@ -106,16 +106,16 @@ mod tests { fn e501_non_ascii_char() { let line = "'\u{4e9c}' * 2"; // 7 in UTF-32, 9 in UTF-8. let locator = Locator::new(line); - let program = parse_module(line).unwrap(); - let indexer = Indexer::from_tokens(program.tokens(), &locator); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(line).unwrap(); + let indexer = Indexer::from_tokens(parsed.tokens(), &locator); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); let check_with_max_line_length = |line_length: LineLength| { check_physical_lines( &locator, &stylist, &indexer, - program.comment_ranges(), + parsed.comment_ranges(), &[], &LinterSettings { pycodestyle: pycodestyle::settings::Settings { diff --git a/crates/ruff_linter/src/checkers/tokens.rs b/crates/ruff_linter/src/checkers/tokens.rs index fae8ce0928cd5..0c59df78576bf 100644 --- a/crates/ruff_linter/src/checkers/tokens.rs +++ b/crates/ruff_linter/src/checkers/tokens.rs @@ -8,7 +8,7 @@ use ruff_python_codegen::Stylist; use ruff_diagnostics::Diagnostic; use ruff_python_index::Indexer; -use ruff_python_parser::Program; +use ruff_python_parser::Parsed; use ruff_source_file::Locator; use ruff_text_size::Ranged; @@ -23,7 +23,7 @@ use crate::settings::LinterSettings; #[allow(clippy::too_many_arguments)] pub(crate) fn check_tokens( - program: &Program, + parsed: &Parsed, path: &Path, locator: &Locator, indexer: &Indexer, @@ -34,8 +34,8 @@ pub(crate) fn check_tokens( ) -> Vec { let mut diagnostics: Vec = vec![]; - let tokens = program.tokens(); - let comment_ranges = program.comment_ranges(); + let tokens = parsed.tokens(); + let comment_ranges = parsed.comment_ranges(); if settings.rules.any_enabled(&[ Rule::BlankLineBetweenMethods, diff --git a/crates/ruff_linter/src/directives.rs b/crates/ruff_linter/src/directives.rs index 37e346da52f1f..398d02696a0a5 100644 --- a/crates/ruff_linter/src/directives.rs +++ b/crates/ruff_linter/src/directives.rs @@ -5,7 +5,7 @@ use std::str::FromStr; use bitflags::bitflags; use ruff_python_ast::ModModule; -use ruff_python_parser::{Program, TokenKind, Tokens}; +use ruff_python_parser::{Parsed, TokenKind, Tokens}; use ruff_python_trivia::CommentRanges; use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; @@ -52,19 +52,19 @@ pub struct Directives { } pub fn extract_directives( - program: &Program, + parsed: &Parsed, flags: Flags, locator: &Locator, indexer: &Indexer, ) -> Directives { Directives { noqa_line_for: if flags.intersects(Flags::NOQA) { - extract_noqa_line_for(program.tokens(), locator, indexer) + extract_noqa_line_for(parsed.tokens(), locator, indexer) } else { NoqaMapping::default() }, isort: if flags.intersects(Flags::ISORT) { - extract_isort_directives(locator, program.comment_ranges()) + extract_isort_directives(locator, parsed.comment_ranges()) } else { IsortDirectives::default() }, @@ -393,11 +393,11 @@ mod tests { use super::IsortDirectives; fn noqa_mappings(contents: &str) -> NoqaMapping { - let program = parse_module(contents).unwrap(); + let parsed = parse_module(contents).unwrap(); let locator = Locator::new(contents); - let indexer = Indexer::from_tokens(program.tokens(), &locator); + let indexer = Indexer::from_tokens(parsed.tokens(), &locator); - extract_noqa_line_for(program.tokens(), &locator, &indexer) + extract_noqa_line_for(parsed.tokens(), &locator, &indexer) } #[test] @@ -568,9 +568,9 @@ assert foo, \ } fn isort_directives(contents: &str) -> IsortDirectives { - let program = parse_module(contents).unwrap(); + let parsed = parse_module(contents).unwrap(); let locator = Locator::new(contents); - extract_isort_directives(&locator, program.comment_ranges()) + extract_isort_directives(&locator, parsed.comment_ranges()) } #[test] diff --git a/crates/ruff_linter/src/fix/edits.rs b/crates/ruff_linter/src/fix/edits.rs index 1746dc0e7d14c..0901a9f694a2f 100644 --- a/crates/ruff_linter/src/fix/edits.rs +++ b/crates/ruff_linter/src/fix/edits.rs @@ -664,9 +664,9 @@ x = 1 \ fn add_to_dunder_all_test(raw: &str, names: &[&str], expect: &str) -> Result<()> { let locator = Locator::new(raw); let edits = { - let program = parse_expression(raw)?; - let stylist = Stylist::from_tokens(program.tokens(), &locator); - add_to_dunder_all(names.iter().copied(), program.expr(), &stylist) + let parsed = parse_expression(raw)?; + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); + add_to_dunder_all(names.iter().copied(), parsed.expr(), &stylist) }; let diag = { use crate::rules::pycodestyle::rules::MissingNewlineAtEndOfFile; diff --git a/crates/ruff_linter/src/importer/insertion.rs b/crates/ruff_linter/src/importer/insertion.rs index 70b160a683b2f..715405e19ece5 100644 --- a/crates/ruff_linter/src/importer/insertion.rs +++ b/crates/ruff_linter/src/importer/insertion.rs @@ -327,14 +327,10 @@ mod tests { #[test] fn start_of_file() -> Result<()> { fn insert(contents: &str) -> Result { - let program = parse_module(contents)?; + let parsed = parse_module(contents)?; let locator = Locator::new(contents); - let stylist = Stylist::from_tokens(program.tokens(), &locator); - Ok(Insertion::start_of_file( - program.suite(), - &locator, - &stylist, - )) + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); + Ok(Insertion::start_of_file(parsed.suite(), &locator, &stylist)) } let contents = ""; @@ -442,10 +438,10 @@ x = 1 #[test] fn start_of_block() { fn insert(contents: &str, offset: TextSize) -> Insertion { - let program = parse_module(contents).unwrap(); + let parsed = parse_module(contents).unwrap(); let locator = Locator::new(contents); - let stylist = Stylist::from_tokens(program.tokens(), &locator); - Insertion::start_of_block(offset, &locator, &stylist, program.tokens()) + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); + Insertion::start_of_block(offset, &locator, &stylist, parsed.tokens()) } let contents = "if True: pass"; diff --git a/crates/ruff_linter/src/importer/mod.rs b/crates/ruff_linter/src/importer/mod.rs index fae0b74f107cd..51ada8f45d37b 100644 --- a/crates/ruff_linter/src/importer/mod.rs +++ b/crates/ruff_linter/src/importer/mod.rs @@ -8,7 +8,7 @@ use std::error::Error; use anyhow::Result; use libcst_native::{ImportAlias, Name, NameOrAttribute}; use ruff_python_ast::{self as ast, ModModule, Stmt}; -use ruff_python_parser::{Program, Tokens}; +use ruff_python_parser::{Parsed, Tokens}; use ruff_text_size::{Ranged, TextSize}; use ruff_diagnostics::Edit; @@ -42,13 +42,13 @@ pub(crate) struct Importer<'a> { impl<'a> Importer<'a> { pub(crate) fn new( - program: &'a Program, + parsed: &'a Parsed, locator: &'a Locator<'a>, stylist: &'a Stylist<'a>, ) -> Self { Self { - python_ast: program.suite(), - tokens: program.tokens(), + python_ast: parsed.suite(), + tokens: parsed.tokens(), locator, stylist, runtime_imports: Vec::default(), diff --git a/crates/ruff_linter/src/linter.rs b/crates/ruff_linter/src/linter.rs index 49bcea3afd6ec..7a36e67d5b2aa 100644 --- a/crates/ruff_linter/src/linter.rs +++ b/crates/ruff_linter/src/linter.rs @@ -13,7 +13,7 @@ use ruff_notebook::Notebook; use ruff_python_ast::{ModModule, PySourceType}; use ruff_python_codegen::Stylist; use ruff_python_index::Indexer; -use ruff_python_parser::{ParseError, Program}; +use ruff_python_parser::{ParseError, Parsed}; use ruff_source_file::{Locator, SourceFileBuilder}; use ruff_text_size::Ranged; @@ -81,14 +81,14 @@ pub fn check_path( noqa: flags::Noqa, source_kind: &SourceKind, source_type: PySourceType, - program: &Program, + parsed: &Parsed, ) -> LinterResult> { // Aggregate all diagnostics. let mut diagnostics = vec![]; let mut error = None; - let tokens = program.tokens(); - let comment_ranges = program.comment_ranges(); + let tokens = parsed.tokens(); + let comment_ranges = parsed.comment_ranges(); // Collect doc lines. This requires a rare mix of tokens (for comments) and AST // (for docstrings), which demands special-casing at this level. @@ -105,7 +105,7 @@ pub fn check_path( .any(|rule_code| rule_code.lint_source().is_tokens()) { diagnostics.extend(check_tokens( - program, + parsed, path, locator, indexer, @@ -153,13 +153,13 @@ pub fn check_path( .iter_enabled() .any(|rule_code| rule_code.lint_source().is_imports()); if use_ast || use_imports || use_doc_lines { - match program.as_result() { - Ok(program) => { + match parsed.as_result() { + Ok(parsed) => { let cell_offsets = source_kind.as_ipy_notebook().map(Notebook::cell_offsets); let notebook_index = source_kind.as_ipy_notebook().map(Notebook::index); if use_ast { diagnostics.extend(check_ast( - program, + parsed, locator, stylist, indexer, @@ -175,7 +175,7 @@ pub fn check_path( } if use_imports { let import_diagnostics = check_imports( - program, + parsed, locator, indexer, &directives.isort, @@ -189,7 +189,7 @@ pub fn check_path( diagnostics.extend(import_diagnostics); } if use_doc_lines { - doc_lines.extend(doc_lines_from_ast(program.suite(), locator)); + doc_lines.extend(doc_lines_from_ast(parsed.suite(), locator)); } } Err(parse_error) => { @@ -372,21 +372,20 @@ pub fn add_noqa_to_path( settings: &LinterSettings, ) -> Result { // Parse once. - let program = - ruff_python_parser::parse_unchecked_source(source_kind.source_code(), source_type); + let parsed = ruff_python_parser::parse_unchecked_source(source_kind.source_code(), source_type); // Map row and column locations to byte slices (lazily). let locator = Locator::new(source_kind.source_code()); // Detect the current code style (lazily). - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); // Extra indices from the code. - let indexer = Indexer::from_tokens(program.tokens(), &locator); + let indexer = Indexer::from_tokens(parsed.tokens(), &locator); // Extract the `# noqa` and `# isort: skip` directives from the source. let directives = directives::extract_directives( - &program, + &parsed, directives::Flags::from_settings(settings), &locator, &indexer, @@ -407,7 +406,7 @@ pub fn add_noqa_to_path( flags::Noqa::Disabled, source_kind, source_type, - &program, + &parsed, ); // Log any parse errors. @@ -429,7 +428,7 @@ pub fn add_noqa_to_path( path, &diagnostics, &locator, - program.comment_ranges(), + parsed.comment_ranges(), &settings.external, &directives.noqa_line_for, stylist.line_ending(), @@ -447,20 +446,20 @@ pub fn lint_only( source_type: PySourceType, source: ParseSource, ) -> LinterResult> { - let program = source.into_program(source_kind, source_type); + let parsed = source.into_parsed(source_kind, source_type); // Map row and column locations to byte slices (lazily). let locator = Locator::new(source_kind.source_code()); // Detect the current code style (lazily). - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); // Extra indices from the code. - let indexer = Indexer::from_tokens(program.tokens(), &locator); + let indexer = Indexer::from_tokens(parsed.tokens(), &locator); // Extract the `# noqa` and `# isort: skip` directives from the source. let directives = directives::extract_directives( - &program, + &parsed, directives::Flags::from_settings(settings), &locator, &indexer, @@ -478,7 +477,7 @@ pub fn lint_only( noqa, source_kind, source_type, - &program, + &parsed, ); result.map(|diagnostics| diagnostics_to_messages(diagnostics, path, &locator, &directives)) @@ -537,21 +536,21 @@ pub fn lint_fix<'a>( // Continuously fix until the source code stabilizes. loop { // Parse once. - let program = + let parsed = ruff_python_parser::parse_unchecked_source(transformed.source_code(), source_type); // Map row and column locations to byte slices (lazily). let locator = Locator::new(transformed.source_code()); // Detect the current code style (lazily). - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); // Extra indices from the code. - let indexer = Indexer::from_tokens(program.tokens(), &locator); + let indexer = Indexer::from_tokens(parsed.tokens(), &locator); // Extract the `# noqa` and `# isort: skip` directives from the source. let directives = directives::extract_directives( - &program, + &parsed, directives::Flags::from_settings(settings), &locator, &indexer, @@ -569,7 +568,7 @@ pub fn lint_fix<'a>( noqa, &transformed, source_type, - &program, + &parsed, ); if iterations == 0 { @@ -705,25 +704,21 @@ This indicates a bug in Ruff. If you could open an issue at: #[derive(Debug, Clone)] pub enum ParseSource { - /// Parse the [`Program`] from the given source code. + /// Parse the [`Parsed`] from the given source code. None, - /// Use the precomputed [`Program`]. - Precomputed(Program), + /// Use the precomputed [`Parsed`]. + Precomputed(Parsed), } impl ParseSource { - /// Consumes the [`ParseSource`] and returns the parsed [`Program`], parsing the source code if + /// Consumes the [`ParseSource`] and returns the parsed [`Parsed`], parsing the source code if /// necessary. - fn into_program( - self, - source_kind: &SourceKind, - source_type: PySourceType, - ) -> Program { + fn into_parsed(self, source_kind: &SourceKind, source_type: PySourceType) -> Parsed { match self { ParseSource::None => { ruff_python_parser::parse_unchecked_source(source_kind.source_code(), source_type) } - ParseSource::Precomputed(program) => program, + ParseSource::Precomputed(parsed) => parsed, } } } diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/rules/zip_without_explicit_strict.rs b/crates/ruff_linter/src/rules/flake8_bugbear/rules/zip_without_explicit_strict.rs index 80c654e013036..7e38b527e77d9 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/rules/zip_without_explicit_strict.rs +++ b/crates/ruff_linter/src/rules/flake8_bugbear/rules/zip_without_explicit_strict.rs @@ -68,7 +68,7 @@ pub(crate) fn zip_without_explicit_strict(checker: &mut Checker, call: &ast::Exp add_argument( "strict=False", &call.arguments, - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ), // If the function call contains `**kwargs`, mark the fix as unsafe. diff --git a/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs b/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs index 00dd8d382ef56..9d1c59e387c53 100644 --- a/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs +++ b/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs @@ -139,7 +139,7 @@ pub(crate) fn unnecessary_generator_list(checker: &mut Checker, call: &ast::Expr let range = parenthesized_range( argument.into(), (&call.arguments).into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or(argument.range()); diff --git a/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_dict_kwargs.rs b/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_dict_kwargs.rs index 160409f14432c..1f0b799855815 100644 --- a/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_dict_kwargs.rs +++ b/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_dict_kwargs.rs @@ -129,7 +129,7 @@ pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, call: &ast::ExprCal parenthesized_range( value.into(), dict.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or(value.range()) diff --git a/crates/ruff_linter/src/rules/flake8_pyi/rules/generic_not_last_base_class.rs b/crates/ruff_linter/src/rules/flake8_pyi/rules/generic_not_last_base_class.rs index 539264f5f028b..6c104f0006383 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/rules/generic_not_last_base_class.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/rules/generic_not_last_base_class.rs @@ -114,7 +114,7 @@ fn generate_fix( let insertion = add_argument( locator.slice(generic_base), arguments, - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), source, ); diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/assertion.rs b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/assertion.rs index fd708422f35fe..674fb0f4bfef2 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/assertion.rs +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/assertion.rs @@ -284,7 +284,7 @@ pub(crate) fn unittest_assertion( // the assertion is part of a larger expression. if checker.semantic().current_statement().is_expr_stmt() && checker.semantic().current_expression_parent().is_none() - && !checker.program().comment_ranges().intersects(expr.range()) + && !checker.parsed().comment_ranges().intersects(expr.range()) { if let Ok(stmt) = unittest_assert.generate_assert(args, keywords) { diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement( @@ -292,7 +292,7 @@ pub(crate) fn unittest_assertion( parenthesized_range( expr.into(), checker.semantic().current_statement().into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or(expr.range()), @@ -385,7 +385,7 @@ pub(crate) fn unittest_raises_assertion( call.func.range(), ); if !checker - .program() + .parsed() .comment_ranges() .has_comments(call, checker.locator()) { @@ -745,7 +745,7 @@ pub(crate) fn composite_condition( let mut diagnostic = Diagnostic::new(PytestCompositeAssertion, stmt.range()); if matches!(composite, CompositionKind::Simple) && msg.is_none() - && !checker.program().comment_ranges().intersects(stmt.range()) + && !checker.parsed().comment_ranges().intersects(stmt.range()) && !checker .indexer() .in_multi_statement_line(stmt, checker.locator()) diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs index 5af9428453c16..6ef3b8687ec6f 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs @@ -353,7 +353,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) { let name_range = get_parametrize_name_range( decorator, expr, - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or(expr.range()); @@ -388,7 +388,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) { let name_range = get_parametrize_name_range( decorator, expr, - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or(expr.range()); @@ -681,11 +681,7 @@ fn check_duplicates(checker: &mut Checker, values: &Expr) { let element_end = trailing_comma(element, checker.locator().contents(), values_end); let deletion_range = TextRange::new(previous_end, element_end); - if !checker - .program() - .comment_ranges() - .intersects(deletion_range) - { + if !checker.parsed().comment_ranges().intersects(deletion_range) { diagnostic.set_fix(Fix::unsafe_edit(Edit::range_deletion(deletion_range))); } } diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs index fb2e248e05a8b..241ba9695d716 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs @@ -527,7 +527,7 @@ pub(crate) fn compare_with_tuple(checker: &mut Checker, expr: &Expr) { // Avoid removing comments. if checker - .program() + .parsed() .comment_ranges() .has_comments(expr, checker.locator()) { @@ -779,7 +779,7 @@ fn is_short_circuit( parenthesized_range( furthest.into(), expr.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or(furthest.range()) @@ -807,7 +807,7 @@ fn is_short_circuit( parenthesized_range( furthest.into(), expr.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or(furthest.range()) diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_ifexp.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_ifexp.rs index 5a37baca14745..f9b9b5752e712 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_ifexp.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_ifexp.rs @@ -164,7 +164,7 @@ pub(crate) fn if_expr_with_true_false( parenthesized_range( test.into(), expr.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or(test.range()), diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_with.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_with.rs index d3952659d8063..17b04340f527f 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_with.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_with.rs @@ -168,7 +168,7 @@ pub(crate) fn multiple_with_statements( TextRange::new(with_stmt.start(), colon.end()), ); if !checker - .program() + .parsed() .comment_ranges() .intersects(TextRange::new(with_stmt.start(), with_stmt.body[0].start())) { diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/collapsible_if.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/collapsible_if.rs index 18e18b3e19eba..2a78b971c5634 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/collapsible_if.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/collapsible_if.rs @@ -113,14 +113,10 @@ pub(crate) fn nested_if_statements( ); // The fixer preserves comments in the nested body, but removes comments between // the outer and inner if statements. - if !checker - .program() - .comment_ranges() - .intersects(TextRange::new( - nested_if.start(), - nested_if.body()[0].start(), - )) - { + if !checker.parsed().comment_ranges().intersects(TextRange::new( + nested_if.start(), + nested_if.body()[0].start(), + )) { match collapse_nested_if(checker.locator(), checker.stylist(), nested_if) { Ok(edit) => { if edit.content().map_or(true, |content| { diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_dict_get.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_dict_get.rs index c62698f748014..64a0294816857 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_dict_get.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_dict_get.rs @@ -210,7 +210,7 @@ pub(crate) fn if_else_block_instead_of_dict_get(checker: &mut Checker, stmt_if: stmt_if.range(), ); if !checker - .program() + .parsed() .comment_ranges() .has_comments(stmt_if, checker.locator()) { @@ -300,7 +300,7 @@ pub(crate) fn if_exp_instead_of_dict_get( expr.range(), ); if !checker - .program() + .parsed() .comment_ranges() .has_comments(expr, checker.locator()) { diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs index 0a8c31eae4575..60deb30459e25 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs @@ -143,7 +143,7 @@ pub(crate) fn if_else_block_instead_of_if_exp(checker: &mut Checker, stmt_if: &a stmt_if.range(), ); if !checker - .program() + .parsed() .comment_ranges() .has_comments(stmt_if, checker.locator()) { diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_with_same_arms.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_with_same_arms.rs index 01c1958b204f2..e43eb1b7c02a4 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_with_same_arms.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_with_same_arms.rs @@ -73,13 +73,13 @@ pub(crate) fn if_with_same_arms(checker: &mut Checker, stmt_if: &ast::StmtIf) { // ...and the same comments let first_comments = checker - .program() + .parsed() .comment_ranges() .comments_in_range(body_range(¤t_branch, checker.locator())) .iter() .map(|range| checker.locator().slice(*range)); let second_comments = checker - .program() + .parsed() .comment_ranges() .comments_in_range(body_range(following_branch, checker.locator())) .iter() @@ -99,7 +99,7 @@ pub(crate) fn if_with_same_arms(checker: &mut Checker, stmt_if: &ast::StmtIf) { ¤t_branch, following_branch, checker.locator(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), ) }); diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/key_in_dict.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/key_in_dict.rs index 920051b7df6ec..619fdddca4490 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/key_in_dict.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/key_in_dict.rs @@ -100,14 +100,14 @@ fn key_in_dict( let left_range = parenthesized_range( left.into(), parent, - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or(left.range()); let right_range = parenthesized_range( right.into(), parent, - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or(right.range()); diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/needless_bool.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/needless_bool.rs index 5cf987f7b2b97..263290759a59a 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/needless_bool.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/needless_bool.rs @@ -194,7 +194,7 @@ pub(crate) fn needless_bool(checker: &mut Checker, stmt: &Stmt) { // Generate the replacement condition. let condition = if checker - .program() + .parsed() .comment_ranges() .has_comments(&range, checker.locator()) { diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/suppressible_exception.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/suppressible_exception.rs index 8f0ba658027d7..b94cdacec5604 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/suppressible_exception.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/suppressible_exception.rs @@ -126,7 +126,7 @@ pub(crate) fn suppressible_exception( stmt.range(), ); if !checker - .program() + .parsed() .comment_ranges() .has_comments(stmt, checker.locator()) { diff --git a/crates/ruff_linter/src/rules/isort/helpers.rs b/crates/ruff_linter/src/rules/isort/helpers.rs index 1e1718d8d084a..50b8b7ffca49b 100644 --- a/crates/ruff_linter/src/rules/isort/helpers.rs +++ b/crates/ruff_linter/src/rules/isort/helpers.rs @@ -11,7 +11,7 @@ use crate::rules::isort::types::TrailingComma; pub(super) fn trailing_comma(stmt: &Stmt, tokens: &Tokens) -> TrailingComma { let mut count = 0u32; let mut trailing_comma = TrailingComma::Absent; - for token in tokens.tokens_in_range(stmt.range()) { + for token in tokens.in_range(stmt.range()) { match token.kind() { TokenKind::Lpar => count = count.saturating_add(1), TokenKind::Rpar => count = count.saturating_sub(1), diff --git a/crates/ruff_linter/src/rules/isort/rules/add_required_imports.rs b/crates/ruff_linter/src/rules/isort/rules/add_required_imports.rs index 6a6e62196abcb..87265c9cd28d1 100644 --- a/crates/ruff_linter/src/rules/isort/rules/add_required_imports.rs +++ b/crates/ruff_linter/src/rules/isort/rules/add_required_imports.rs @@ -6,7 +6,7 @@ use ruff_python_ast::helpers::is_docstring_stmt; use ruff_python_ast::imports::{Alias, AnyImport, FutureImport, Import, ImportFrom}; use ruff_python_ast::{self as ast, ModModule, PySourceType, Stmt}; use ruff_python_codegen::Stylist; -use ruff_python_parser::{parse_module, Program}; +use ruff_python_parser::{parse_module, Parsed}; use ruff_source_file::Locator; use ruff_text_size::{TextRange, TextSize}; @@ -87,13 +87,13 @@ fn includes_import(stmt: &Stmt, target: &AnyImport) -> bool { #[allow(clippy::too_many_arguments)] fn add_required_import( required_import: &AnyImport, - program: &Program, + parsed: &Parsed, locator: &Locator, stylist: &Stylist, source_type: PySourceType, ) -> Option { // Don't add imports to semantically-empty files. - if program.suite().iter().all(is_docstring_stmt) { + if parsed.suite().iter().all(is_docstring_stmt) { return None; } @@ -103,7 +103,7 @@ fn add_required_import( } // If the import is already present in a top-level block, don't add it. - if program + if parsed .suite() .iter() .any(|stmt| includes_import(stmt, required_import)) @@ -117,14 +117,14 @@ fn add_required_import( TextRange::default(), ); diagnostic.set_fix(Fix::safe_edit( - Importer::new(program, locator, stylist).add_import(required_import, TextSize::default()), + Importer::new(parsed, locator, stylist).add_import(required_import, TextSize::default()), )); Some(diagnostic) } /// I002 pub(crate) fn add_required_imports( - program: &Program, + parsed: &Parsed, locator: &Locator, stylist: &Stylist, settings: &LinterSettings, @@ -135,7 +135,7 @@ pub(crate) fn add_required_imports( .required_imports .iter() .flat_map(|required_import| { - let Ok(body) = parse_module(required_import).map(Program::into_suite) else { + let Ok(body) = parse_module(required_import).map(Parsed::into_suite) else { error!("Failed to parse required import: `{}`", required_import); return vec![]; }; @@ -165,7 +165,7 @@ pub(crate) fn add_required_imports( }, level: *level, }), - program, + parsed, locator, stylist, source_type, @@ -182,7 +182,7 @@ pub(crate) fn add_required_imports( as_name: name.asname.as_deref(), }, }), - program, + parsed, locator, stylist, source_type, diff --git a/crates/ruff_linter/src/rules/isort/rules/organize_imports.rs b/crates/ruff_linter/src/rules/isort/rules/organize_imports.rs index a4ea72803ecf8..7e0c3be59d6d5 100644 --- a/crates/ruff_linter/src/rules/isort/rules/organize_imports.rs +++ b/crates/ruff_linter/src/rules/isort/rules/organize_imports.rs @@ -8,7 +8,7 @@ use ruff_python_ast::whitespace::trailing_lines_end; use ruff_python_ast::{ModModule, PySourceType, Stmt}; use ruff_python_codegen::Stylist; use ruff_python_index::Indexer; -use ruff_python_parser::Program; +use ruff_python_parser::Parsed; use ruff_python_trivia::{leading_indentation, textwrap::indent, PythonWhitespace}; use ruff_source_file::{Locator, UniversalNewlines}; use ruff_text_size::{Ranged, TextRange}; @@ -89,7 +89,7 @@ pub(crate) fn organize_imports( settings: &LinterSettings, package: Option<&Path>, source_type: PySourceType, - program: &Program, + parsed: &Parsed, ) -> Option { let indentation = locator.slice(extract_indentation_range(&block.imports, locator)); let indentation = leading_indentation(indentation); @@ -108,7 +108,7 @@ pub(crate) fn organize_imports( let comments = comments::collect_comments( TextRange::new(range.start(), locator.full_line_end(range.end())), locator, - program.comment_ranges(), + parsed.comment_ranges(), ); let trailing_line_end = if block.trailer.is_none() { @@ -130,7 +130,7 @@ pub(crate) fn organize_imports( source_type, settings.target_version, &settings.isort, - program.tokens(), + parsed.tokens(), ); // Expand the span the entire range, including leading and trailing space. diff --git a/crates/ruff_linter/src/rules/pandas_vet/rules/inplace_argument.rs b/crates/ruff_linter/src/rules/pandas_vet/rules/inplace_argument.rs index 1c720df1c0f9a..ed4446660cb21 100644 --- a/crates/ruff_linter/src/rules/pandas_vet/rules/inplace_argument.rs +++ b/crates/ruff_linter/src/rules/pandas_vet/rules/inplace_argument.rs @@ -93,7 +93,7 @@ pub(crate) fn inplace_argument(checker: &mut Checker, call: &ast::ExprCall) { call, keyword, statement, - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator(), ) { diagnostic.set_fix(fix); diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/literal_comparisons.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/literal_comparisons.rs index b54ad6571786b..3489aa0c56e6a 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/literal_comparisons.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/literal_comparisons.rs @@ -324,7 +324,7 @@ pub(crate) fn literal_comparisons(checker: &mut Checker, compare: &ast::ExprComp &ops, &compare.comparators, compare.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator(), ); for diagnostic in &mut diagnostics { diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/mod.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/mod.rs index 27320a43a3e1d..a483187e574cd 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/mod.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/mod.rs @@ -588,9 +588,9 @@ if False: } fn assert_logical_lines(contents: &str, expected: &[&str]) { - let program = parse_module(contents).unwrap(); + let parsed = parse_module(contents).unwrap(); let locator = Locator::new(contents); - let actual: Vec = LogicalLines::from_tokens(program.tokens(), &locator) + let actual: Vec = LogicalLines::from_tokens(parsed.tokens(), &locator) .into_iter() .map(|line| line.text_trimmed()) .map(ToString::to_string) diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/not_tests.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/not_tests.rs index 7f566b4d28a21..1602e84f79644 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/not_tests.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/not_tests.rs @@ -104,7 +104,7 @@ pub(crate) fn not_tests(checker: &mut Checker, unary_op: &ast::ExprUnaryOp) { &[CmpOp::NotIn], comparators, unary_op.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator(), ), unary_op.range(), @@ -125,7 +125,7 @@ pub(crate) fn not_tests(checker: &mut Checker, unary_op: &ast::ExprUnaryOp) { &[CmpOp::IsNot], comparators, unary_op.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator(), ), unary_op.range(), diff --git a/crates/ruff_linter/src/rules/pyflakes/mod.rs b/crates/ruff_linter/src/rules/pyflakes/mod.rs index b5c87f9351f21..f88cc6f285272 100644 --- a/crates/ruff_linter/src/rules/pyflakes/mod.rs +++ b/crates/ruff_linter/src/rules/pyflakes/mod.rs @@ -638,13 +638,13 @@ mod tests { let source_type = PySourceType::default(); let source_kind = SourceKind::Python(contents.to_string()); let settings = LinterSettings::for_rules(Linter::Pyflakes.rules()); - let program = + let parsed = ruff_python_parser::parse_unchecked_source(source_kind.source_code(), source_type); let locator = Locator::new(&contents); - let stylist = Stylist::from_tokens(program.tokens(), &locator); - let indexer = Indexer::from_tokens(program.tokens(), &locator); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); + let indexer = Indexer::from_tokens(parsed.tokens(), &locator); let directives = directives::extract_directives( - &program, + &parsed, directives::Flags::from_settings(&settings), &locator, &indexer, @@ -663,7 +663,7 @@ mod tests { flags::Noqa::Enabled, &source_kind, source_type, - &program, + &parsed, ); diagnostics.sort_by_key(Ranged::start); let actual = diagnostics diff --git a/crates/ruff_linter/src/rules/pyflakes/rules/invalid_literal_comparisons.rs b/crates/ruff_linter/src/rules/pyflakes/rules/invalid_literal_comparisons.rs index 6df04ed5afce0..5301e1cada51a 100644 --- a/crates/ruff_linter/src/rules/pyflakes/rules/invalid_literal_comparisons.rs +++ b/crates/ruff_linter/src/rules/pyflakes/rules/invalid_literal_comparisons.rs @@ -96,7 +96,7 @@ pub(crate) fn invalid_literal_comparison( { let mut diagnostic = Diagnostic::new(IsLiteral { cmp_op: op.into() }, expr.range()); if lazy_located.is_none() { - lazy_located = Some(locate_cmp_ops(expr, checker.program().tokens())); + lazy_located = Some(locate_cmp_ops(expr, checker.parsed().tokens())); } if let Some(located_op) = lazy_located.as_ref().and_then(|located| located.get(index)) { assert_eq!(located_op.op, *op); @@ -144,7 +144,7 @@ impl From<&CmpOp> for IsCmpOp { /// with valid ranges. fn locate_cmp_ops(expr: &Expr, tokens: &Tokens) -> Vec { let mut tok_iter = tokens - .tokens_in_range(expr.range()) + .in_range(expr.range()) .iter() .filter(|token| !token.is_trivia()) .peekable(); @@ -248,8 +248,8 @@ mod tests { use super::{locate_cmp_ops, LocatedCmpOp}; fn extract_cmp_op_locations(source: &str) -> Result> { - let program = parse_expression(source)?; - Ok(locate_cmp_ops(program.expr(), program.tokens())) + let parsed = parse_expression(source)?; + Ok(locate_cmp_ops(parsed.expr(), parsed.tokens())) } #[test] diff --git a/crates/ruff_linter/src/rules/pyflakes/rules/repeated_keys.rs b/crates/ruff_linter/src/rules/pyflakes/rules/repeated_keys.rs index 27f0a435d1f07..66fcfdc0eaefd 100644 --- a/crates/ruff_linter/src/rules/pyflakes/rules/repeated_keys.rs +++ b/crates/ruff_linter/src/rules/pyflakes/rules/repeated_keys.rs @@ -169,7 +169,7 @@ pub(crate) fn repeated_keys(checker: &mut Checker, dict: &ast::ExprDict) { parenthesized_range( dict.value(i - 1).into(), dict.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or_else(|| dict.value(i - 1).range()) @@ -177,7 +177,7 @@ pub(crate) fn repeated_keys(checker: &mut Checker, dict: &ast::ExprDict) { parenthesized_range( dict.value(i).into(), dict.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or_else(|| dict.value(i).range()) @@ -201,7 +201,7 @@ pub(crate) fn repeated_keys(checker: &mut Checker, dict: &ast::ExprDict) { parenthesized_range( dict.value(i - 1).into(), dict.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or_else(|| dict.value(i - 1).range()) @@ -209,7 +209,7 @@ pub(crate) fn repeated_keys(checker: &mut Checker, dict: &ast::ExprDict) { parenthesized_range( dict.value(i).into(), dict.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or_else(|| dict.value(i).range()) diff --git a/crates/ruff_linter/src/rules/pyflakes/rules/unused_variable.rs b/crates/ruff_linter/src/rules/pyflakes/rules/unused_variable.rs index 65e1efd2e0dc6..934a4d0af97c8 100644 --- a/crates/ruff_linter/src/rules/pyflakes/rules/unused_variable.rs +++ b/crates/ruff_linter/src/rules/pyflakes/rules/unused_variable.rs @@ -165,13 +165,13 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option { let start = parenthesized_range( target.into(), statement.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or(target.range()) .start(); let end = - match_token_after(checker.program().tokens(), target.end(), |token| { + match_token_after(checker.parsed().tokens(), target.end(), |token| { token == TokenKind::Equal })? .start(); @@ -206,7 +206,7 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option { // If the expression is complex (`x = foo()`), remove the assignment, // but preserve the right-hand side. let start = statement.start(); - let end = match_token_after(checker.program().tokens(), start, |token| { + let end = match_token_after(checker.parsed().tokens(), start, |token| { token == TokenKind::Equal })? .start(); @@ -229,7 +229,7 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option { if optional_vars.range() == binding.range() { // Find the first token before the `as` keyword. let start = match_token_before( - checker.program().tokens(), + checker.parsed().tokens(), item.context_expr.start(), |token| token == TokenKind::As, )? @@ -237,7 +237,7 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option { // Find the first colon, comma, or closing bracket after the `as` keyword. let end = - match_token_or_closing_brace(checker.program().tokens(), start, |token| { + match_token_or_closing_brace(checker.parsed().tokens(), start, |token| { token == TokenKind::Colon || token == TokenKind::Comma })? .start(); diff --git a/crates/ruff_linter/src/rules/pylint/rules/if_stmt_min_max.rs b/crates/ruff_linter/src/rules/pylint/rules/if_stmt_min_max.rs index c9fa0f3baaa9c..0e9eceb984f26 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/if_stmt_min_max.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/if_stmt_min_max.rs @@ -160,7 +160,7 @@ pub(crate) fn if_stmt_min_max(checker: &mut Checker, stmt_if: &ast::StmtIf) { parenthesized_range( body_target.into(), body.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents() ) .unwrap_or(body_target.range()) diff --git a/crates/ruff_linter/src/rules/pylint/rules/nested_min_max.rs b/crates/ruff_linter/src/rules/pylint/rules/nested_min_max.rs index 98d280237c677..a78d4de6771fb 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/nested_min_max.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/nested_min_max.rs @@ -156,7 +156,7 @@ pub(crate) fn nested_min_max( }) { let mut diagnostic = Diagnostic::new(NestedMinMax { func: min_max }, expr.range()); if !checker - .program() + .parsed() .comment_ranges() .has_comments(expr, checker.locator()) { diff --git a/crates/ruff_linter/src/rules/pylint/rules/subprocess_run_without_check.rs b/crates/ruff_linter/src/rules/pylint/rules/subprocess_run_without_check.rs index 5f5ed84479977..03690dd350561 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/subprocess_run_without_check.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/subprocess_run_without_check.rs @@ -76,7 +76,7 @@ pub(crate) fn subprocess_run_without_check(checker: &mut Checker, call: &ast::Ex add_argument( "check=False", &call.arguments, - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ), // If the function call contains `**kwargs`, mark the fix as unsafe. diff --git a/crates/ruff_linter/src/rules/pylint/rules/too_many_branches.rs b/crates/ruff_linter/src/rules/pylint/rules/too_many_branches.rs index a923c55cd8f75..e2b76a4101ff8 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/too_many_branches.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/too_many_branches.rs @@ -259,8 +259,8 @@ mod tests { use super::num_branches; fn test_helper(source: &str, expected_num_branches: usize) -> Result<()> { - let program = parse_module(source)?; - assert_eq!(num_branches(program.suite()), expected_num_branches); + let parsed = parse_module(source)?; + assert_eq!(num_branches(parsed.suite()), expected_num_branches); Ok(()) } diff --git a/crates/ruff_linter/src/rules/pylint/rules/too_many_return_statements.rs b/crates/ruff_linter/src/rules/pylint/rules/too_many_return_statements.rs index d03cdf735b309..5e6e34dba86b0 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/too_many_return_statements.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/too_many_return_statements.rs @@ -103,8 +103,8 @@ mod tests { use super::num_returns; fn test_helper(source: &str, expected: usize) -> Result<()> { - let program = parse_module(source)?; - assert_eq!(num_returns(program.suite()), expected); + let parsed = parse_module(source)?; + assert_eq!(num_returns(parsed.suite()), expected); Ok(()) } diff --git a/crates/ruff_linter/src/rules/pylint/rules/unspecified_encoding.rs b/crates/ruff_linter/src/rules/pylint/rules/unspecified_encoding.rs index 6c606a4d569c6..8fc8c9692a99b 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/unspecified_encoding.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/unspecified_encoding.rs @@ -175,7 +175,7 @@ fn generate_keyword_fix(checker: &Checker, call: &ast::ExprCall) -> Fix { })) ), &call.arguments, - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), )) } @@ -190,7 +190,7 @@ fn generate_import_fix(checker: &Checker, call: &ast::ExprCall) -> Result { let argument_edit = add_argument( &format!("encoding={binding}(False)"), &call.arguments, - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ); Ok(Fix::unsafe_edits(import_edit, [argument_edit])) diff --git a/crates/ruff_linter/src/rules/pyupgrade/fixes.rs b/crates/ruff_linter/src/rules/pyupgrade/fixes.rs index 7fd746138e8a5..65486abee28e7 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/fixes.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/fixes.rs @@ -11,7 +11,7 @@ pub(crate) fn remove_import_members( members_to_remove: &[&str], ) -> String { let commas: Vec = tokens - .tokens_in_range(import_from_stmt.range()) + .in_range(import_from_stmt.range()) .iter() .skip_while(|token| token.kind() != TokenKind::Import) .filter_map(|token| { @@ -71,8 +71,8 @@ mod tests { use super::remove_import_members; fn test_helper(source: &str, members_to_remove: &[&str]) -> String { - let program = parse_module(source).unwrap(); - let import_from_stmt = program + let parsed = parse_module(source).unwrap(); + let import_from_stmt = parsed .suite() .first() .expect("source should have one statement") @@ -81,7 +81,7 @@ mod tests { remove_import_members( &Locator::new(source), import_from_stmt, - program.tokens(), + parsed.tokens(), members_to_remove, ) } diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/deprecated_import.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/deprecated_import.rs index 7e3e01f14aeea..4b282e569a4d8 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/deprecated_import.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/deprecated_import.rs @@ -666,7 +666,7 @@ pub(crate) fn deprecated_import(checker: &mut Checker, import_from_stmt: &StmtIm module, checker.locator(), checker.stylist(), - checker.program().tokens(), + checker.parsed().tokens(), checker.settings.target_version, ); diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/f_strings.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/f_strings.rs index 6ecffdcb27897..869a48e96736c 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/f_strings.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/f_strings.rs @@ -409,11 +409,7 @@ pub(crate) fn f_strings(checker: &mut Checker, call: &ast::ExprCall, summary: &F }; let mut patches: Vec<(TextRange, FStringConversion)> = vec![]; - let mut tokens = checker - .program() - .tokens() - .tokens_in_range(call.func.range()) - .iter(); + let mut tokens = checker.parsed().tokens().in_range(call.func.range()).iter(); let end = loop { let Some(token) = tokens.next() else { unreachable!("Should break from the `Tok::Dot` arm"); @@ -517,7 +513,7 @@ pub(crate) fn f_strings(checker: &mut Checker, call: &ast::ExprCall, summary: &F // ) // ``` let has_comments = checker - .program() + .parsed() .comment_ranges() .intersects(call.arguments.range()); diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/printf_string_formatting.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/printf_string_formatting.rs index 4fbbfe1e39fb5..046ef14a6b312 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/printf_string_formatting.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/printf_string_formatting.rs @@ -460,7 +460,7 @@ pub(crate) fn printf_string_formatting( } if let Some(prev_end) = prev_end { - for token in checker.program().tokens().after(prev_end) { + for token in checker.parsed().tokens().after(prev_end) { match token.kind() { // If we hit a right paren, we have to preserve it. TokenKind::Rpar => { diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/redundant_open_modes.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/redundant_open_modes.rs index 21ea4e4ff7cde..d502107007894 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/redundant_open_modes.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/redundant_open_modes.rs @@ -79,7 +79,7 @@ pub(crate) fn redundant_open_modes(checker: &mut Checker, call: &ast::ExprCall) call, &keyword.value, mode.replacement_value(), - checker.program().tokens(), + checker.parsed().tokens(), )); } } @@ -93,7 +93,7 @@ pub(crate) fn redundant_open_modes(checker: &mut Checker, call: &ast::ExprCall) call, mode_param, mode.replacement_value(), - checker.program().tokens(), + checker.parsed().tokens(), )); } } @@ -181,7 +181,7 @@ fn create_remove_param_fix( let mut is_first_arg: bool = false; let mut delete_first_arg: bool = false; - for token in tokens.tokens_in_range(call.range()) { + for token in tokens.in_range(call.range()) { if token.start() == mode_param.start() { if is_first_arg { delete_first_arg = true; diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/unnecessary_encode_utf8.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/unnecessary_encode_utf8.rs index f9ce477469a05..6ed669662897e 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/unnecessary_encode_utf8.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/unnecessary_encode_utf8.rs @@ -121,7 +121,7 @@ fn replace_with_bytes_literal(locator: &Locator, call: &ast::ExprCall, tokens: & // Build up a replacement string by prefixing all string tokens with `b`. let mut replacement = String::with_capacity(call.range().len().to_usize() + 1); let mut prev = call.start(); - for token in tokens.tokens_in_range(call.range()) { + for token in tokens.in_range(call.range()) { match token.kind() { TokenKind::Dot => break, TokenKind::String => { @@ -165,7 +165,7 @@ pub(crate) fn unnecessary_encode_utf8(checker: &mut Checker, call: &ast::ExprCal diagnostic.set_fix(replace_with_bytes_literal( checker.locator(), call, - checker.program().tokens(), + checker.parsed().tokens(), )); checker.diagnostics.push(diagnostic); } else if let EncodingArg::Keyword(kwarg) = encoding_arg { diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/yield_in_for_loop.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/yield_in_for_loop.rs index 53adab3a29311..7e87c72d4ff5c 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/yield_in_for_loop.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/yield_in_for_loop.rs @@ -116,7 +116,7 @@ pub(crate) fn yield_in_for_loop(checker: &mut Checker, stmt_for: &ast::StmtFor) parenthesized_range( iter.as_ref().into(), stmt_for.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or(iter.range()), diff --git a/crates/ruff_linter/src/rules/refurb/rules/if_exp_instead_of_or_operator.rs b/crates/ruff_linter/src/rules/refurb/rules/if_exp_instead_of_or_operator.rs index 18817c63e7749..c3f404c2437f6 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/if_exp_instead_of_or_operator.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/if_exp_instead_of_or_operator.rs @@ -77,13 +77,13 @@ pub(crate) fn if_exp_instead_of_or_operator(checker: &mut Checker, if_expr: &ast parenthesize_test( test, if_expr, - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator() ), parenthesize_test( orelse, if_expr, - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator() ), ), diff --git a/crates/ruff_linter/src/rules/refurb/rules/repeated_append.rs b/crates/ruff_linter/src/rules/refurb/rules/repeated_append.rs index 6cb349bfb16d8..60893aa8e3988 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/repeated_append.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/repeated_append.rs @@ -114,7 +114,7 @@ pub(crate) fn repeated_append(checker: &mut Checker, stmt: &Stmt) { // # comment // a.append(2) // ``` - if group.is_consecutive && !checker.program().comment_ranges().intersects(group.range()) + if group.is_consecutive && !checker.parsed().comment_ranges().intersects(group.range()) { diagnostic.set_fix(Fix::unsafe_edit(Edit::replacement( replacement, diff --git a/crates/ruff_linter/src/rules/refurb/rules/single_item_membership_test.rs b/crates/ruff_linter/src/rules/refurb/rules/single_item_membership_test.rs index 18f9a4d90fc7b..97f9aea11648a 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/single_item_membership_test.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/single_item_membership_test.rs @@ -83,7 +83,7 @@ pub(crate) fn single_item_membership_test( &[membership_test.replacement_op()], &[item.clone()], expr.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator(), ), expr.range(), diff --git a/crates/ruff_linter/src/rules/ruff/rules/collection_literal_concatenation.rs b/crates/ruff_linter/src/rules/ruff/rules/collection_literal_concatenation.rs index 3f25ec9b849a9..b4fe7df371f25 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/collection_literal_concatenation.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/collection_literal_concatenation.rs @@ -199,7 +199,7 @@ pub(crate) fn collection_literal_concatenation(checker: &mut Checker, expr: &Exp expr.range(), ); if !checker - .program() + .parsed() .comment_ranges() .has_comments(expr, checker.locator()) { diff --git a/crates/ruff_linter/src/rules/ruff/rules/invalid_formatter_suppression_comment.rs b/crates/ruff_linter/src/rules/ruff/rules/invalid_formatter_suppression_comment.rs index 8922ca7329d78..2f132ca140ab0 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/invalid_formatter_suppression_comment.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/invalid_formatter_suppression_comment.rs @@ -71,7 +71,7 @@ impl AlwaysFixableViolation for InvalidFormatterSuppressionComment { pub(crate) fn ignored_formatter_suppression_comment(checker: &mut Checker, suite: &ast::Suite) { let locator = checker.locator(); let comment_ranges: SmallVec<[SuppressionComment; 8]> = checker - .program() + .parsed() .comment_ranges() .into_iter() .filter_map(|range| { diff --git a/crates/ruff_linter/src/rules/ruff/rules/missing_fstring_syntax.rs b/crates/ruff_linter/src/rules/ruff/rules/missing_fstring_syntax.rs index e060a63d3fbba..95989f9721ce9 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/missing_fstring_syntax.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/missing_fstring_syntax.rs @@ -114,12 +114,12 @@ fn should_be_fstring( } let fstring_expr = format!("f{}", locator.slice(literal)); - let Ok(program) = parse_expression(&fstring_expr) else { + let Ok(parsed) = parse_expression(&fstring_expr) else { return false; }; // Note: Range offsets for `value` are based on `fstring_expr` - let Some(ast::ExprFString { value, .. }) = program.expr().as_f_string_expr() else { + let Some(ast::ExprFString { value, .. }) = parsed.expr().as_f_string_expr() else { return false; }; diff --git a/crates/ruff_linter/src/rules/ruff/rules/parenthesize_logical_operators.rs b/crates/ruff_linter/src/rules/ruff/rules/parenthesize_logical_operators.rs index e5a556f323be7..cdb7b9e6c1ce7 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/parenthesize_logical_operators.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/parenthesize_logical_operators.rs @@ -84,7 +84,7 @@ pub(crate) fn parenthesize_chained_logical_operators( if parenthesized_range( bool_op.into(), expr.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), locator.contents(), ) .is_none() diff --git a/crates/ruff_linter/src/rules/ruff/rules/quadratic_list_summation.rs b/crates/ruff_linter/src/rules/ruff/rules/quadratic_list_summation.rs index 425cc6b9f1ff3..2d8e684f0f1a2 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/quadratic_list_summation.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/quadratic_list_summation.rs @@ -111,7 +111,7 @@ fn convert_to_reduce(iterable: &Expr, call: &ast::ExprCall, checker: &Checker) - parenthesized_range( iterable.into(), call.arguments.as_any_node_ref(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or(iterable.range()), diff --git a/crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs b/crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs index 34e7ff32517bd..9f4ce6129cabc 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs @@ -502,7 +502,7 @@ fn collect_string_sequence_lines<'a>( // An iterator over the string values in the sequence. let mut string_items_iter = string_items.iter(); - let mut token_iter = tokens.tokens_in_range(range).iter(); + let mut token_iter = tokens.in_range(range).iter(); let first_token = token_iter.next()?; if first_token.kind() != kind.opening_token_for_multiline_definition() { return None; diff --git a/crates/ruff_linter/src/rules/ruff/rules/sort_dunder_all.rs b/crates/ruff_linter/src/rules/ruff/rules/sort_dunder_all.rs index fe79e518443dc..0ac227e935794 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/sort_dunder_all.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/sort_dunder_all.rs @@ -216,7 +216,7 @@ fn create_fix( range, kind, locator, - checker.program().tokens(), + checker.parsed().tokens(), string_items, )?; assert_eq!(value.len(), elts.len()); diff --git a/crates/ruff_linter/src/rules/ruff/rules/sort_dunder_slots.rs b/crates/ruff_linter/src/rules/ruff/rules/sort_dunder_slots.rs index a62d059b18a27..55b12f2684911 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/sort_dunder_slots.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/sort_dunder_slots.rs @@ -210,7 +210,7 @@ impl<'a> StringLiteralDisplay<'a> { self.range(), *sequence_kind, locator, - checker.program().tokens(), + checker.parsed().tokens(), elements, )?; assert_eq!(analyzed_sequence.len(), self.elts.len()); diff --git a/crates/ruff_linter/src/rules/ruff/rules/unnecessary_key_check.rs b/crates/ruff_linter/src/rules/ruff/rules/unnecessary_key_check.rs index a8478f0d09c12..1813efd784ddf 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/unnecessary_key_check.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/unnecessary_key_check.rs @@ -110,7 +110,7 @@ pub(crate) fn unnecessary_key_check(checker: &mut Checker, expr: &Expr) { parenthesized_range( obj_right.into(), right.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or(obj_right.range()) @@ -119,7 +119,7 @@ pub(crate) fn unnecessary_key_check(checker: &mut Checker, expr: &Expr) { parenthesized_range( key_right.into(), right.into(), - checker.program().comment_ranges(), + checker.parsed().comment_ranges(), checker.locator().contents(), ) .unwrap_or(key_right.range()) diff --git a/crates/ruff_linter/src/test.rs b/crates/ruff_linter/src/test.rs index e68de264df5fe..d23406bef00b0 100644 --- a/crates/ruff_linter/src/test.rs +++ b/crates/ruff_linter/src/test.rs @@ -109,13 +109,12 @@ pub(crate) fn test_contents<'a>( settings: &LinterSettings, ) -> (Vec, Cow<'a, SourceKind>) { let source_type = PySourceType::from(path); - let program = - ruff_python_parser::parse_unchecked_source(source_kind.source_code(), source_type); + let parsed = ruff_python_parser::parse_unchecked_source(source_kind.source_code(), source_type); let locator = Locator::new(source_kind.source_code()); - let stylist = Stylist::from_tokens(program.tokens(), &locator); - let indexer = Indexer::from_tokens(program.tokens(), &locator); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); + let indexer = Indexer::from_tokens(parsed.tokens(), &locator); let directives = directives::extract_directives( - &program, + &parsed, directives::Flags::from_settings(settings), &locator, &indexer, @@ -135,7 +134,7 @@ pub(crate) fn test_contents<'a>( flags::Noqa::Enabled, source_kind, source_type, - &program, + &parsed, ); let source_has_errors = error.is_some(); @@ -175,13 +174,13 @@ pub(crate) fn test_contents<'a>( transformed = Cow::Owned(transformed.updated(fixed_contents, &source_map)); - let program = + let parsed = ruff_python_parser::parse_unchecked_source(transformed.source_code(), source_type); let locator = Locator::new(transformed.source_code()); - let stylist = Stylist::from_tokens(program.tokens(), &locator); - let indexer = Indexer::from_tokens(program.tokens(), &locator); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); + let indexer = Indexer::from_tokens(parsed.tokens(), &locator); let directives = directives::extract_directives( - &program, + &parsed, directives::Flags::from_settings(settings), &locator, &indexer, @@ -201,7 +200,7 @@ pub(crate) fn test_contents<'a>( flags::Noqa::Enabled, &transformed, source_type, - &program, + &parsed, ); if let Some(fixed_error) = fixed_error { diff --git a/crates/ruff_python_ast_integration_tests/tests/parenthesize.rs b/crates/ruff_python_ast_integration_tests/tests/parenthesize.rs index c877261fd2eb3..ec6b5d8650935 100644 --- a/crates/ruff_python_ast_integration_tests/tests/parenthesize.rs +++ b/crates/ruff_python_ast_integration_tests/tests/parenthesize.rs @@ -6,9 +6,9 @@ use ruff_text_size::TextRange; #[test] fn test_parenthesized_name() { let source_code = r"(x) + 1"; - let program = parse_expression(source_code).unwrap(); + let parsed = parse_expression(source_code).unwrap(); - let bin_op = program.expr().as_bin_op_expr().unwrap(); + let bin_op = parsed.expr().as_bin_op_expr().unwrap(); let name = bin_op.left.as_ref(); let parenthesized = parenthesized_range( @@ -23,9 +23,9 @@ fn test_parenthesized_name() { #[test] fn test_non_parenthesized_name() { let source_code = r"x + 1"; - let program = parse_expression(source_code).unwrap(); + let parsed = parse_expression(source_code).unwrap(); - let bin_op = program.expr().as_bin_op_expr().unwrap(); + let bin_op = parsed.expr().as_bin_op_expr().unwrap(); let name = bin_op.left.as_ref(); let parenthesized = parenthesized_range( @@ -40,9 +40,9 @@ fn test_non_parenthesized_name() { #[test] fn test_parenthesized_argument() { let source_code = r"f((a))"; - let program = parse_expression(source_code).unwrap(); + let parsed = parse_expression(source_code).unwrap(); - let call = program.expr().as_call_expr().unwrap(); + let call = parsed.expr().as_call_expr().unwrap(); let arguments = &call.arguments; let argument = arguments.args.first().unwrap(); @@ -58,9 +58,9 @@ fn test_parenthesized_argument() { #[test] fn test_non_parenthesized_argument() { let source_code = r"f(a)"; - let program = parse_expression(source_code).unwrap(); + let parsed = parse_expression(source_code).unwrap(); - let call = program.expr().as_call_expr().unwrap(); + let call = parsed.expr().as_call_expr().unwrap(); let arguments = &call.arguments; let argument = arguments.args.first().unwrap(); @@ -76,9 +76,9 @@ fn test_non_parenthesized_argument() { #[test] fn test_parenthesized_tuple_member() { let source_code = r"(a, (b))"; - let program = parse_expression(source_code).unwrap(); + let parsed = parse_expression(source_code).unwrap(); - let tuple = program.expr().as_tuple_expr().unwrap(); + let tuple = parsed.expr().as_tuple_expr().unwrap(); let member = tuple.elts.last().unwrap(); let parenthesized = parenthesized_range( @@ -93,9 +93,9 @@ fn test_parenthesized_tuple_member() { #[test] fn test_non_parenthesized_tuple_member() { let source_code = r"(a, b)"; - let program = parse_expression(source_code).unwrap(); + let parsed = parse_expression(source_code).unwrap(); - let tuple = program.expr().as_tuple_expr().unwrap(); + let tuple = parsed.expr().as_tuple_expr().unwrap(); let member = tuple.elts.last().unwrap(); let parenthesized = parenthesized_range( @@ -110,9 +110,9 @@ fn test_non_parenthesized_tuple_member() { #[test] fn test_twice_parenthesized_name() { let source_code = r"((x)) + 1"; - let program = parse_expression(source_code).unwrap(); + let parsed = parse_expression(source_code).unwrap(); - let bin_op = program.expr().as_bin_op_expr().unwrap(); + let bin_op = parsed.expr().as_bin_op_expr().unwrap(); let name = bin_op.left.as_ref(); let parenthesized = parenthesized_range( @@ -127,9 +127,9 @@ fn test_twice_parenthesized_name() { #[test] fn test_twice_parenthesized_argument() { let source_code = r"f(((a + 1)))"; - let program = parse_expression(source_code).unwrap(); + let parsed = parse_expression(source_code).unwrap(); - let call = program.expr().as_call_expr().unwrap(); + let call = parsed.expr().as_call_expr().unwrap(); let arguments = &call.arguments; let argument = arguments.args.first().unwrap(); diff --git a/crates/ruff_python_ast_integration_tests/tests/stmt_if.rs b/crates/ruff_python_ast_integration_tests/tests/stmt_if.rs index fd416e6845b4a..240d01187efc8 100644 --- a/crates/ruff_python_ast_integration_tests/tests/stmt_if.rs +++ b/crates/ruff_python_ast_integration_tests/tests/stmt_if.rs @@ -9,8 +9,8 @@ fn extract_elif_else_range() -> Result<(), ParseError> { elif b: ... "; - let program = parse_module(contents)?; - let if_stmt = program + let parsed = parse_module(contents)?; + let if_stmt = parsed .suite() .first() .expect("module should contain at least one statement") @@ -25,8 +25,8 @@ elif b: else: ... "; - let program = parse_module(contents)?; - let if_stmt = program + let parsed = parse_module(contents)?; + let if_stmt = parsed .suite() .first() .expect("module should contain at least one statement") diff --git a/crates/ruff_python_codegen/src/generator.rs b/crates/ruff_python_codegen/src/generator.rs index c7d8ba74d7f06..9cb98dd7c6174 100644 --- a/crates/ruff_python_codegen/src/generator.rs +++ b/crates/ruff_python_codegen/src/generator.rs @@ -1449,8 +1449,8 @@ mod tests { let indentation = Indentation::default(); let quote = Quote::default(); let line_ending = LineEnding::default(); - let program = ruff_python_parser::parse(contents, Mode::Ipython).unwrap(); - let Mod::Module(ModModule { body, .. }) = program.into_syntax() else { + let parsed = ruff_python_parser::parse(contents, Mode::Ipython).unwrap(); + let Mod::Module(ModModule { body, .. }) = parsed.into_syntax() else { panic!("Source code didn't return ModModule") }; let [stmt] = body.as_slice() else { diff --git a/crates/ruff_python_codegen/src/lib.rs b/crates/ruff_python_codegen/src/lib.rs index ed267dfa67646..64a991edcd750 100644 --- a/crates/ruff_python_codegen/src/lib.rs +++ b/crates/ruff_python_codegen/src/lib.rs @@ -9,9 +9,9 @@ pub use stylist::Stylist; /// Run round-trip source code generation on a given Python code. pub fn round_trip(code: &str) -> Result { let locator = Locator::new(code); - let program = parse_module(code)?; - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(code)?; + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); let mut generator: Generator = (&stylist).into(); - generator.unparse_suite(program.suite()); + generator.unparse_suite(parsed.suite()); Ok(generator.generate()) } diff --git a/crates/ruff_python_codegen/src/stylist.rs b/crates/ruff_python_codegen/src/stylist.rs index 901a07fdbec86..a3f41bd00b259 100644 --- a/crates/ruff_python_codegen/src/stylist.rs +++ b/crates/ruff_python_codegen/src/stylist.rs @@ -171,8 +171,8 @@ mod tests { fn indentation() { let contents = r"x = 1"; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.indentation(), &Indentation::default()); let contents = r" @@ -180,8 +180,8 @@ if True: pass "; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.indentation(), &Indentation(" ".to_string())); let contents = r" @@ -189,8 +189,8 @@ if True: pass "; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.indentation(), &Indentation(" ".to_string())); let contents = r" @@ -198,8 +198,8 @@ if True: pass "; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.indentation(), &Indentation("\t".to_string())); let contents = r" @@ -210,8 +210,8 @@ x = ( ) "; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.indentation(), &Indentation(" ".to_string())); // formfeed indent, see `detect_indention` comment. @@ -221,8 +221,8 @@ class FormFeedIndent: print(a) "; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.indentation(), &Indentation(" ".to_string())); } @@ -230,38 +230,38 @@ class FormFeedIndent: fn quote() { let contents = r"x = 1"; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.quote(), Quote::default()); let contents = r"x = '1'"; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.quote(), Quote::Single); let contents = r"x = f'1'"; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.quote(), Quote::Single); let contents = r#"x = "1""#; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.quote(), Quote::Double); let contents = r#"x = f"1""#; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.quote(), Quote::Double); let contents = r#"s = "It's done.""#; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.quote(), Quote::Double); // No style if only double quoted docstring (will take default Double) @@ -271,8 +271,8 @@ def f(): pass "#; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.quote(), Quote::default()); // Detect from string literal appearing after docstring @@ -282,8 +282,8 @@ def f(): a = 'v' "#; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.quote(), Quote::Single); let contents = r#" @@ -292,8 +292,8 @@ a = 'v' a = "v" "#; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.quote(), Quote::Double); // Detect from f-string appearing after docstring @@ -303,8 +303,8 @@ a = "v" a = f'v' "#; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.quote(), Quote::Single); let contents = r#" @@ -313,16 +313,16 @@ a = f'v' a = f"v" "#; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.quote(), Quote::Double); let contents = r" f'''Module docstring.''' "; let locator = Locator::new(contents); - let program = parse_module(contents).unwrap(); - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let parsed = parse_module(contents).unwrap(); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); assert_eq!(stylist.quote(), Quote::Single); } diff --git a/crates/ruff_python_formatter/src/cli.rs b/crates/ruff_python_formatter/src/cli.rs index acffb5f4e2f5e..f2f86c7bd1096 100644 --- a/crates/ruff_python_formatter/src/cli.rs +++ b/crates/ruff_python_formatter/src/cli.rs @@ -47,7 +47,7 @@ pub fn format_and_debug_print(source: &str, cli: &Cli, source_path: &Path) -> Re let source_type = PySourceType::from(source_path); // Parse the AST. - let program = parse(source, source_type.as_mode()).context("Syntax error in input")?; + let parsed = parse(source, source_type.as_mode()).context("Syntax error in input")?; let options = PyFormatOptions::from_extension(source_path) .with_preview(if cli.preview { @@ -62,15 +62,14 @@ pub fn format_and_debug_print(source: &str, cli: &Cli, source_path: &Path) -> Re }); let source_code = SourceCode::new(source); - let formatted = - format_module_ast(&program, source, options).context("Failed to format node")?; + let formatted = format_module_ast(&parsed, source, options).context("Failed to format node")?; if cli.print_ir { println!("{}", formatted.document().display(source_code)); } if cli.print_comments { // Print preceding, following and enclosing nodes let decorated_comments = - collect_comments(program.syntax(), source_code, program.comment_ranges()); + collect_comments(parsed.syntax(), source_code, parsed.comment_ranges()); if !decorated_comments.is_empty() { println!("# Comment decoration: Range, Preceding, Following, Enclosing, Comment"); } diff --git a/crates/ruff_python_formatter/src/comments/mod.rs b/crates/ruff_python_formatter/src/comments/mod.rs index 0e9bfa63d1c7c..3731a082e6830 100644 --- a/crates/ruff_python_formatter/src/comments/mod.rs +++ b/crates/ruff_python_formatter/src/comments/mod.rs @@ -481,12 +481,12 @@ mod tests { use ruff_formatter::SourceCode; use ruff_python_ast::{Mod, PySourceType}; - use ruff_python_parser::{parse, AsMode, Program}; + use ruff_python_parser::{parse, AsMode, Parsed}; use crate::comments::Comments; struct CommentsTestCase<'a> { - program: Program, + parsed: Parsed, source_code: SourceCode<'a>, } @@ -494,20 +494,20 @@ mod tests { fn from_code(source: &'a str) -> Self { let source_code = SourceCode::new(source); let source_type = PySourceType::Python; - let program = + let parsed = parse(source, source_type.as_mode()).expect("Expect source to be valid Python"); CommentsTestCase { - program, + parsed, source_code, } } fn to_comments(&self) -> Comments { Comments::from_ast( - self.program.syntax(), + self.parsed.syntax(), self.source_code, - self.program.comment_ranges(), + self.parsed.comment_ranges(), ) } } diff --git a/crates/ruff_python_formatter/src/expression/parentheses.rs b/crates/ruff_python_formatter/src/expression/parentheses.rs index 0003459a3ca81..c85355922f14e 100644 --- a/crates/ruff_python_formatter/src/expression/parentheses.rs +++ b/crates/ruff_python_formatter/src/expression/parentheses.rs @@ -450,10 +450,10 @@ mod tests { #[test] fn test_has_parentheses() { let expression = r#"(b().c("")).d()"#; - let program = parse_expression(expression).unwrap(); + let parsed = parse_expression(expression).unwrap(); assert!(!is_expression_parenthesized( - ExpressionRef::from(program.expr()), - program.comment_ranges(), + ExpressionRef::from(parsed.expr()), + parsed.comment_ranges(), expression )); } diff --git a/crates/ruff_python_formatter/src/lib.rs b/crates/ruff_python_formatter/src/lib.rs index e95d885b8706f..283727ff76cc8 100644 --- a/crates/ruff_python_formatter/src/lib.rs +++ b/crates/ruff_python_formatter/src/lib.rs @@ -6,7 +6,7 @@ use ruff_formatter::prelude::*; use ruff_formatter::{format, write, FormatError, Formatted, PrintError, Printed, SourceCode}; use ruff_python_ast::AstNode; use ruff_python_ast::Mod; -use ruff_python_parser::{parse, AsMode, ParseError, Program}; +use ruff_python_parser::{parse, AsMode, ParseError, Parsed}; use ruff_python_trivia::CommentRanges; use ruff_source_file::Locator; @@ -113,23 +113,23 @@ pub fn format_module_source( options: PyFormatOptions, ) -> Result { let source_type = options.source_type(); - let program = parse(source, source_type.as_mode())?; - let formatted = format_module_ast(&program, source, options)?; + let parsed = parse(source, source_type.as_mode())?; + let formatted = format_module_ast(&parsed, source, options)?; Ok(formatted.print()?) } pub fn format_module_ast<'a>( - program: &'a Program, + parsed: &'a Parsed, source: &'a str, options: PyFormatOptions, ) -> FormatResult>> { let source_code = SourceCode::new(source); - let comments = Comments::from_ast(program.syntax(), source_code, program.comment_ranges()); + let comments = Comments::from_ast(parsed.syntax(), source_code, parsed.comment_ranges()); let locator = Locator::new(source); let formatted = format!( - PyFormatContext::new(options, locator.contents(), comments, program.tokens()), - [program.syntax().format()] + PyFormatContext::new(options, locator.contents(), comments, parsed.tokens()), + [parsed.syntax().format()] )?; formatted .context() @@ -198,9 +198,9 @@ def main() -> None: // Parse the AST. let source_path = "code_inline.py"; - let program = parse(source, source_type.as_mode()).unwrap(); + let parsed = parse(source, source_type.as_mode()).unwrap(); let options = PyFormatOptions::from_extension(Path::new(source_path)); - let formatted = format_module_ast(&program, source, options).unwrap(); + let formatted = format_module_ast(&parsed, source, options).unwrap(); // Uncomment the `dbg` to print the IR. // Use `dbg_write!(f, []) instead of `write!(f, [])` in your formatting code to print some IR diff --git a/crates/ruff_python_formatter/src/range.rs b/crates/ruff_python_formatter/src/range.rs index 7215e38ed5f67..7e5f152ad7883 100644 --- a/crates/ruff_python_formatter/src/range.rs +++ b/crates/ruff_python_formatter/src/range.rs @@ -72,19 +72,19 @@ pub fn format_range( assert_valid_char_boundaries(range, source); - let program = parse(source, options.source_type().as_mode())?; + let parsed = parse(source, options.source_type().as_mode())?; let source_code = SourceCode::new(source); - let comments = Comments::from_ast(program.syntax(), source_code, program.comment_ranges()); + let comments = Comments::from_ast(parsed.syntax(), source_code, parsed.comment_ranges()); let mut context = PyFormatContext::new( options.with_source_map_generation(SourceMapGeneration::Enabled), source, comments, - program.tokens(), + parsed.tokens(), ); let (enclosing_node, base_indent) = - match find_enclosing_node(range, AnyNodeRef::from(program.syntax()), &context) { + match find_enclosing_node(range, AnyNodeRef::from(parsed.syntax()), &context) { EnclosingNode::Node { node, indent_level } => (node, indent_level), EnclosingNode::Suppressed => { // The entire range falls into a suppressed range. There's nothing to format. diff --git a/crates/ruff_python_formatter/src/statement/suite.rs b/crates/ruff_python_formatter/src/statement/suite.rs index f8cb5e89a20b5..164c5cff5b983 100644 --- a/crates/ruff_python_formatter/src/statement/suite.rs +++ b/crates/ruff_python_formatter/src/statement/suite.rs @@ -858,17 +858,17 @@ def trailing_func(): pass "; - let program = parse_module(source).unwrap(); + let parsed = parse_module(source).unwrap(); let context = PyFormatContext::new( PyFormatOptions::default(), source, - Comments::from_ranges(program.comment_ranges()), - program.tokens(), + Comments::from_ranges(parsed.comment_ranges()), + parsed.tokens(), ); let test_formatter = - format_with(|f: &mut PyFormatter| program.suite().format().with_options(level).fmt(f)); + format_with(|f: &mut PyFormatter| parsed.suite().format().with_options(level).fmt(f)); let formatted = format!(context, [test_formatter]).unwrap(); let printed = formatted.print().unwrap(); diff --git a/crates/ruff_python_formatter/src/string/docstring.rs b/crates/ruff_python_formatter/src/string/docstring.rs index fa8e2d38fc061..65de2979b0182 100644 --- a/crates/ruff_python_formatter/src/string/docstring.rs +++ b/crates/ruff_python_formatter/src/string/docstring.rs @@ -1551,15 +1551,14 @@ fn docstring_format_source( use ruff_python_parser::AsMode; let source_type = options.source_type(); - let program = ruff_python_parser::parse(source, source_type.as_mode())?; + let parsed = ruff_python_parser::parse(source, source_type.as_mode())?; let source_code = ruff_formatter::SourceCode::new(source); - let comments = - crate::Comments::from_ast(program.syntax(), source_code, program.comment_ranges()); + let comments = crate::Comments::from_ast(parsed.syntax(), source_code, parsed.comment_ranges()); let locator = Locator::new(source); - let ctx = PyFormatContext::new(options, locator.contents(), comments, program.tokens()) + let ctx = PyFormatContext::new(options, locator.contents(), comments, parsed.tokens()) .in_docstring(docstring_quote_style); - let formatted = crate::format!(ctx, [program.syntax().format()])?; + let formatted = crate::format!(ctx, [parsed.syntax().format()])?; formatted .context() .comments() diff --git a/crates/ruff_python_formatter/src/verbatim.rs b/crates/ruff_python_formatter/src/verbatim.rs index dcff8c497bb34..587f2d0690383 100644 --- a/crates/ruff_python_formatter/src/verbatim.rs +++ b/crates/ruff_python_formatter/src/verbatim.rs @@ -726,10 +726,7 @@ struct FormatVerbatimStatementRange { impl Format> for FormatVerbatimStatementRange { fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { let logical_lines = LogicalLinesIter::new( - f.context() - .tokens() - .tokens_in_range(self.verbatim_range) - .iter(), + f.context().tokens().in_range(self.verbatim_range).iter(), self.verbatim_range, ); let mut first = true; diff --git a/crates/ruff_python_index/src/indexer.rs b/crates/ruff_python_index/src/indexer.rs index 9b6995062fa6d..fb813f9814a22 100644 --- a/crates/ruff_python_index/src/indexer.rs +++ b/crates/ruff_python_index/src/indexer.rs @@ -218,9 +218,9 @@ mod tests { use crate::Indexer; fn new_indexer(contents: &str) -> Indexer { - let program = parse_module(contents).unwrap(); + let parsed = parse_module(contents).unwrap(); let locator = Locator::new(contents); - Indexer::from_tokens(program.tokens(), &locator) + Indexer::from_tokens(parsed.tokens(), &locator) } #[test] diff --git a/crates/ruff_python_parser/src/lib.rs b/crates/ruff_python_parser/src/lib.rs index 9613e8452d1dd..523c8451d07d6 100644 --- a/crates/ruff_python_parser/src/lib.rs +++ b/crates/ruff_python_parser/src/lib.rs @@ -57,7 +57,7 @@ //! //! - token: This module contains the definition of the tokens that are generated by the lexer. //! - [lexer]: This module contains the lexer and is responsible for generating the tokens. -//! - parser: This module contains an interface to the [Program] and is responsible for generating the AST. +//! - parser: This module contains an interface to the [Parsed] and is responsible for generating the AST. //! - mode: This module contains the definition of the different modes that the `ruff_python_parser` can be in. //! //! [lexical analysis]: https://en.wikipedia.org/wiki/Lexical_analysis @@ -109,7 +109,7 @@ pub mod typing; /// let module = parse_module(source); /// assert!(module.is_ok()); /// ``` -pub fn parse_module(source: &str) -> Result, ParseError> { +pub fn parse_module(source: &str) -> Result, ParseError> { Parser::new(source, Mode::Module) .parse() .try_into_module() @@ -132,7 +132,7 @@ pub fn parse_module(source: &str) -> Result, ParseError> { /// let expr = parse_expression("1 + 2"); /// assert!(expr.is_ok()); /// ``` -pub fn parse_expression(source: &str) -> Result, ParseError> { +pub fn parse_expression(source: &str) -> Result, ParseError> { Parser::new(source, Mode::Expression) .parse() .try_into_expression() @@ -153,13 +153,13 @@ pub fn parse_expression(source: &str) -> Result, ParseErr /// use ruff_python_parser::parse_expression_range; /// # use ruff_text_size::{TextRange, TextSize}; /// -/// let program = parse_expression_range("11 + 22 + 33", TextRange::new(TextSize::new(5), TextSize::new(7))); -/// assert!(program.is_ok()); +/// let parsed = parse_expression_range("11 + 22 + 33", TextRange::new(TextSize::new(5), TextSize::new(7))); +/// assert!(parsed.is_ok()); /// ``` pub fn parse_expression_range( source: &str, range: TextRange, -) -> Result, ParseError> { +) -> Result, ParseError> { let source = &source[..range.end().to_usize()]; Parser::new_starts_at(source, Mode::Expression, range.start()) .parse() @@ -182,8 +182,8 @@ pub fn parse_expression_range( /// ``` /// use ruff_python_parser::{Mode, parse}; /// -/// let program = parse("1 + 2", Mode::Expression); -/// assert!(program.is_ok()); +/// let parsed = parse("1 + 2", Mode::Expression); +/// assert!(parsed.is_ok()); /// ``` /// /// Alternatively, we can parse a full Python program consisting of multiple lines: @@ -197,8 +197,8 @@ pub fn parse_expression_range( /// def greet(self): /// print("Hello, world!") /// "#; -/// let program = parse(source, Mode::Module); -/// assert!(program.is_ok()); +/// let parsed = parse(source, Mode::Module); +/// assert!(parsed.is_ok()); /// ``` /// /// Additionally, we can parse a Python program containing IPython escapes: @@ -211,23 +211,23 @@ pub fn parse_expression_range( /// ?str.replace /// !ls /// "#; -/// let program = parse(source, Mode::Ipython); -/// assert!(program.is_ok()); +/// let parsed = parse(source, Mode::Ipython); +/// assert!(parsed.is_ok()); /// ``` -pub fn parse(source: &str, mode: Mode) -> Result, ParseError> { +pub fn parse(source: &str, mode: Mode) -> Result, ParseError> { parse_unchecked(source, mode).into_result() } /// Parse the given Python source code using the specified [`Mode`]. /// /// This is same as the [`parse`] function except that it doesn't check for any [`ParseError`] -/// and returns the [`Program`] as is. -pub fn parse_unchecked(source: &str, mode: Mode) -> Program { +/// and returns the [`Parsed`] as is. +pub fn parse_unchecked(source: &str, mode: Mode) -> Parsed { Parser::new(source, mode).parse() } /// Parse the given Python source code using the specified [`PySourceType`]. -pub fn parse_unchecked_source(source: &str, source_type: PySourceType) -> Program { +pub fn parse_unchecked_source(source: &str, source_type: PySourceType) -> Parsed { // SAFETY: Safe because `PySourceType` always parses to a `ModModule` Parser::new(source, source_type.as_mode()) .parse() @@ -237,20 +237,20 @@ pub fn parse_unchecked_source(source: &str, source_type: PySourceType) -> Progra /// Represents the parsed source code. #[derive(Debug, Clone)] -pub struct Program { +pub struct Parsed { syntax: T, tokens: Tokens, errors: Vec, comment_ranges: CommentRanges, } -impl Program { - /// Returns the syntax node represented by this program. +impl Parsed { + /// Returns the syntax node represented by this parsed output. pub fn syntax(&self) -> &T { &self.syntax } - /// Returns all the tokens for the program. + /// Returns all the tokens for the parsed output. pub fn tokens(&self) -> &Tokens { &self.tokens } @@ -260,34 +260,29 @@ impl Program { &self.errors } - /// Returns the comment ranges for the program. + /// Returns the comment ranges for the parsed output. pub fn comment_ranges(&self) -> &CommentRanges { &self.comment_ranges } - /// Consumes the [`Program`] and returns the syntax node represented by this program. + /// Consumes the [`Parsed`] output and returns the contained syntax node. pub fn into_syntax(self) -> T { self.syntax } - /// Consumes the [`Program`] and returns a list of syntax errors found during parsing. + /// Consumes the [`Parsed`] output and returns a list of syntax errors found during parsing. pub fn into_errors(self) -> Vec { self.errors } - /// Consumes the [`Program`] and returns the comment ranges found during parsing. - pub fn into_comment_ranges(self) -> CommentRanges { - self.comment_ranges - } - - /// Returns `true` if the program is valid i.e., it has no syntax errors. + /// Returns `true` if the parsed source code is valid i.e., it has no syntax errors. pub fn is_valid(&self) -> bool { self.errors.is_empty() } - /// Converts the [`Program`] into a [`Result`], returning [`Ok`] if the program has no syntax - /// errors, or [`Err`] containing the first [`ParseError`] encountered. - pub fn as_result(&self) -> Result<&Program, &ParseError> { + /// Returns the [`Parsed`] output as a [`Result`], returning [`Ok`] if it has no syntax errors, + /// or [`Err`] containing the first [`ParseError`] encountered. + pub fn as_result(&self) -> Result<&Parsed, &ParseError> { if let [error, ..] = self.errors() { Err(error) } else { @@ -295,9 +290,9 @@ impl Program { } } - /// Consumes the [`Program`] and returns a [`Result`] which is [`Ok`] if the program has no - /// syntax errors, or [`Err`] containing the first [`ParseError`] encountered. - pub(crate) fn into_result(self) -> Result, ParseError> { + /// Consumes the [`Parsed`] output and returns a [`Result`] which is [`Ok`] if it has no syntax + /// errors, or [`Err`] containing the first [`ParseError`] encountered. + pub(crate) fn into_result(self) -> Result, ParseError> { if self.is_valid() { Ok(self) } else { @@ -306,17 +301,17 @@ impl Program { } } -impl Program { - /// Attempts to convert the [`Program`] into a [`Program`]. +impl Parsed { + /// Attempts to convert the [`Parsed`] into a [`Parsed`]. /// - /// This method checks if the `syntax` field of the program is a [`Mod::Module`]. If it is, the - /// method returns [`Some(Program)`] with the contained module. Otherwise, it + /// This method checks if the `syntax` field of the output is a [`Mod::Module`]. If it is, the + /// method returns [`Some(Parsed)`] with the contained module. Otherwise, it /// returns [`None`]. /// - /// [`Some(Program)`]: Some - fn try_into_module(self) -> Option> { + /// [`Some(Parsed)`]: Some + fn try_into_module(self) -> Option> { match self.syntax { - Mod::Module(module) => Some(Program { + Mod::Module(module) => Some(Parsed { syntax: module, tokens: self.tokens, errors: self.errors, @@ -326,17 +321,17 @@ impl Program { } } - /// Attempts to convert the [`Program`] into a [`Program`]. + /// Attempts to convert the [`Parsed`] into a [`Parsed`]. /// - /// This method checks if the `syntax` field of the program is a [`Mod::Expression`]. If it is, - /// the method returns [`Some(Program)`] with the contained expression. + /// This method checks if the `syntax` field of the output is a [`Mod::Expression`]. If it is, + /// the method returns [`Some(Parsed)`] with the contained expression. /// Otherwise, it returns [`None`]. /// - /// [`Some(Program)`]: Some - fn try_into_expression(self) -> Option> { + /// [`Some(Parsed)`]: Some + fn try_into_expression(self) -> Option> { match self.syntax { Mod::Module(_) => None, - Mod::Expression(expression) => Some(Program { + Mod::Expression(expression) => Some(Parsed { syntax: expression, tokens: self.tokens, errors: self.errors, @@ -346,25 +341,25 @@ impl Program { } } -impl Program { - /// Returns the module body contained in this program as a [`Suite`]. +impl Parsed { + /// Returns the module body contained in this parsed output as a [`Suite`]. pub fn suite(&self) -> &Suite { &self.syntax.body } - /// Consumes the [`Program`] and returns the module body as a [`Suite`]. + /// Consumes the [`Parsed`] output and returns the module body as a [`Suite`]. pub fn into_suite(self) -> Suite { self.syntax.body } } -impl Program { - /// Returns the expression contained in this program. +impl Parsed { + /// Returns the expression contained in this parsed output. pub fn expr(&self) -> &Expr { &self.syntax.body } - /// Consumes the [`Program`] and returns the parsed [`Expr`]. + /// Consumes the [`Parsed`] output and returns the contained [`Expr`]. pub fn into_expr(self) -> Expr { *self.syntax.body } @@ -440,7 +435,7 @@ impl Tokens { /// # Panics /// /// If either the start or end offset of the given range is within a token range. - pub fn tokens_in_range(&self, range: TextRange) -> &[Token] { + pub fn in_range(&self, range: TextRange) -> &[Token] { let tokens_after_start = self.after(range.start()); match tokens_after_start.binary_search_by_key(&range.end(), Ranged::end) { @@ -701,7 +696,7 @@ mod tests { #[test] fn tokens_in_range_at_token_offset() { let tokens = new_tokens(TEST_CASE_WITH_GAP.into_iter()); - let in_range = tokens.tokens_in_range(TextRange::new(4.into(), 10.into())); + let in_range = tokens.in_range(TextRange::new(4.into(), 10.into())); assert_eq!(in_range.len(), 4); assert_eq!(in_range.first().unwrap().kind(), TokenKind::Name); assert_eq!(in_range.last().unwrap().kind(), TokenKind::Colon); @@ -710,7 +705,7 @@ mod tests { #[test] fn tokens_in_range_start_offset_at_token_end() { let tokens = new_tokens(TEST_CASE_WITH_GAP.into_iter()); - let in_range = tokens.tokens_in_range(TextRange::new(11.into(), 29.into())); + let in_range = tokens.in_range(TextRange::new(11.into(), 29.into())); assert_eq!(in_range.len(), 3); assert_eq!(in_range.first().unwrap().kind(), TokenKind::Comment); assert_eq!(in_range.last().unwrap().kind(), TokenKind::Indent); @@ -719,7 +714,7 @@ mod tests { #[test] fn tokens_in_range_end_offset_at_token_start() { let tokens = new_tokens(TEST_CASE_WITH_GAP.into_iter()); - let in_range = tokens.tokens_in_range(TextRange::new(8.into(), 15.into())); + let in_range = tokens.in_range(TextRange::new(8.into(), 15.into())); assert_eq!(in_range.len(), 3); assert_eq!(in_range.first().unwrap().kind(), TokenKind::Rpar); assert_eq!(in_range.last().unwrap().kind(), TokenKind::Newline); @@ -728,7 +723,7 @@ mod tests { #[test] fn tokens_in_range_start_offset_between_tokens() { let tokens = new_tokens(TEST_CASE_WITH_GAP.into_iter()); - let in_range = tokens.tokens_in_range(TextRange::new(13.into(), 29.into())); + let in_range = tokens.in_range(TextRange::new(13.into(), 29.into())); assert_eq!(in_range.len(), 3); assert_eq!(in_range.first().unwrap().kind(), TokenKind::Comment); assert_eq!(in_range.last().unwrap().kind(), TokenKind::Indent); @@ -737,7 +732,7 @@ mod tests { #[test] fn tokens_in_range_end_offset_between_tokens() { let tokens = new_tokens(TEST_CASE_WITH_GAP.into_iter()); - let in_range = tokens.tokens_in_range(TextRange::new(9.into(), 13.into())); + let in_range = tokens.in_range(TextRange::new(9.into(), 13.into())); assert_eq!(in_range.len(), 2); assert_eq!(in_range.first().unwrap().kind(), TokenKind::Colon); assert_eq!(in_range.last().unwrap().kind(), TokenKind::Newline); @@ -747,13 +742,13 @@ mod tests { #[should_panic(expected = "Offset 5 is inside a token range 4..7")] fn tokens_in_range_start_offset_inside_token() { let tokens = new_tokens(TEST_CASE_WITH_GAP.into_iter()); - tokens.tokens_in_range(TextRange::new(5.into(), 10.into())); + tokens.in_range(TextRange::new(5.into(), 10.into())); } #[test] #[should_panic(expected = "End offset 6 is inside a token range 4..7")] fn tokens_in_range_end_offset_inside_token() { let tokens = new_tokens(TEST_CASE_WITH_GAP.into_iter()); - tokens.tokens_in_range(TextRange::new(0.into(), 6.into())); + tokens.in_range(TextRange::new(0.into(), 6.into())); } } diff --git a/crates/ruff_python_parser/src/parser/mod.rs b/crates/ruff_python_parser/src/parser/mod.rs index a2e1ef687c054..b9e8c230dcac3 100644 --- a/crates/ruff_python_parser/src/parser/mod.rs +++ b/crates/ruff_python_parser/src/parser/mod.rs @@ -11,7 +11,7 @@ use crate::parser::progress::{ParserProgress, TokenId}; use crate::token_set::TokenSet; use crate::token_source::{TokenSource, TokenSourceCheckpoint}; use crate::{Mode, ParseError, ParseErrorType, TokenKind}; -use crate::{Program, Tokens}; +use crate::{Parsed, Tokens}; mod expression; mod helpers; @@ -71,8 +71,8 @@ impl<'src> Parser<'src> { } } - /// Consumes the [`Parser`] and returns the parsed [`Program`]. - pub(crate) fn parse(mut self) -> Program { + /// Consumes the [`Parser`] and returns the parsed [`Parsed`]. + pub(crate) fn parse(mut self) -> Parsed { let syntax = match self.mode { Mode::Expression => Mod::Expression(self.parse_single_expression()), Mode::Module | Mode::Ipython => Mod::Module(self.parse_module()), @@ -138,7 +138,7 @@ impl<'src> Parser<'src> { } } - fn finish(self, syntax: Mod) -> Program { + fn finish(self, syntax: Mod) -> Parsed { assert_eq!( self.current_token_kind(), TokenKind::EndOfFile, @@ -153,7 +153,7 @@ impl<'src> Parser<'src> { // There's no fast path for when there are no parse errors because a lex error // always results in a parse error. if lex_errors.is_empty() { - return Program { + return Parsed { syntax, tokens: Tokens::new(tokens), comment_ranges, @@ -185,7 +185,7 @@ impl<'src> Parser<'src> { merged.extend(parse_errors); merged.extend(lex_errors.map(ParseError::from)); - Program { + Parsed { syntax, tokens: Tokens::new(tokens), comment_ranges, @@ -866,7 +866,7 @@ impl RecoveryContextKind { fn is_list_terminator(self, p: &Parser) -> bool { match self { - // The program must consume all tokens until the end + // The parser must consume all tokens until the end RecoveryContextKind::ModuleStatements => false, RecoveryContextKind::BlockStatements => p.at(TokenKind::Dedent), diff --git a/crates/ruff_python_parser/src/parser/tests.rs b/crates/ruff_python_parser/src/parser/tests.rs index 8de198b8eb253..09bc41e7f7b66 100644 --- a/crates/ruff_python_parser/src/parser/tests.rs +++ b/crates/ruff_python_parser/src/parser/tests.rs @@ -45,9 +45,9 @@ fn test_expr_mode_valid_syntax() { let source = "first "; - let program = parse_expression(source).unwrap(); + let parsed = parse_expression(source).unwrap(); - insta::assert_debug_snapshot!(program.expr()); + insta::assert_debug_snapshot!(parsed.expr()); } #[test] @@ -61,7 +61,7 @@ fn test_unicode_aliases() { #[test] fn test_ipython_escape_commands() { - let program = parse( + let parsed = parse( r" # Normal Python code ( @@ -132,5 +132,5 @@ foo.bar[0].baz[2].egg?? Mode::Ipython, ) .unwrap(); - insta::assert_debug_snapshot!(program.syntax()); + insta::assert_debug_snapshot!(parsed.syntax()); } diff --git a/crates/ruff_python_parser/src/string.rs b/crates/ruff_python_parser/src/string.rs index 772034527a218..3976da33876ee 100644 --- a/crates/ruff_python_parser/src/string.rs +++ b/crates/ruff_python_parser/src/string.rs @@ -472,14 +472,14 @@ mod tests { use ruff_python_ast::Suite; use crate::lexer::LexicalErrorType; - use crate::{parse_module, FStringErrorType, ParseError, ParseErrorType, Program}; + use crate::{parse_module, FStringErrorType, ParseError, ParseErrorType, Parsed}; const WINDOWS_EOL: &str = "\r\n"; const MAC_EOL: &str = "\r"; const UNIX_EOL: &str = "\n"; fn parse_suite(source: &str) -> Result { - parse_module(source).map(Program::into_suite) + parse_module(source).map(Parsed::into_suite) } fn string_parser_escaped_eol(eol: &str) -> Suite { diff --git a/crates/ruff_python_parser/tests/fixtures.rs b/crates/ruff_python_parser/tests/fixtures.rs index 6b00bff4deead..2a3dce311ae62 100644 --- a/crates/ruff_python_parser/tests/fixtures.rs +++ b/crates/ruff_python_parser/tests/fixtures.rs @@ -36,15 +36,15 @@ fn inline_err() { /// Snapshots the AST. fn test_valid_syntax(input_path: &Path) { let source = fs::read_to_string(input_path).expect("Expected test file to exist"); - let program = parse_unchecked(&source, Mode::Module); + let parsed = parse_unchecked(&source, Mode::Module); - if !program.is_valid() { + if !parsed.is_valid() { let line_index = LineIndex::from_source_text(&source); let source_code = SourceCode::new(&source, &line_index); let mut message = "Expected no syntax errors for a valid program but the parser generated the following errors:\n".to_string(); - for error in program.errors() { + for error in parsed.errors() { writeln!( &mut message, "{}\n", @@ -60,11 +60,11 @@ fn test_valid_syntax(input_path: &Path) { panic!("{input_path:?}: {message}"); } - validate_ast(program.syntax(), source.text_len(), input_path); + validate_ast(parsed.syntax(), source.text_len(), input_path); let mut output = String::new(); writeln!(&mut output, "## AST").unwrap(); - writeln!(&mut output, "\n```\n{:#?}\n```", program.syntax()).unwrap(); + writeln!(&mut output, "\n```\n{:#?}\n```", parsed.syntax()).unwrap(); insta::with_settings!({ omit_expression => true, @@ -79,25 +79,25 @@ fn test_valid_syntax(input_path: &Path) { /// Snapshots the AST and the error messages. fn test_invalid_syntax(input_path: &Path) { let source = fs::read_to_string(input_path).expect("Expected test file to exist"); - let program = parse_unchecked(&source, Mode::Module); + let parsed = parse_unchecked(&source, Mode::Module); assert!( - !program.is_valid(), + !parsed.is_valid(), "{input_path:?}: Expected parser to generate at least one syntax error for a program containing syntax errors." ); - validate_ast(program.syntax(), source.text_len(), input_path); + validate_ast(parsed.syntax(), source.text_len(), input_path); let mut output = String::new(); writeln!(&mut output, "## AST").unwrap(); - writeln!(&mut output, "\n```\n{:#?}\n```", program.syntax()).unwrap(); + writeln!(&mut output, "\n```\n{:#?}\n```", parsed.syntax()).unwrap(); writeln!(&mut output, "## Errors\n").unwrap(); let line_index = LineIndex::from_source_text(&source); let source_code = SourceCode::new(&source, &line_index); - for error in program.errors() { + for error in parsed.errors() { writeln!( &mut output, "{}\n", @@ -130,18 +130,18 @@ def foo() pass "; - let program = parse_unchecked(source, Mode::Module); + let parsed = parse_unchecked(source, Mode::Module); - println!("AST:\n----\n{:#?}", program.syntax()); - println!("Tokens:\n-------\n{:#?}", program.tokens()); + println!("AST:\n----\n{:#?}", parsed.syntax()); + println!("Tokens:\n-------\n{:#?}", parsed.tokens()); - if !program.is_valid() { + if !parsed.is_valid() { println!("Errors:\n-------"); let line_index = LineIndex::from_source_text(source); let source_code = SourceCode::new(source, &line_index); - for error in program.errors() { + for error in parsed.errors() { // Sometimes the code frame doesn't show the error message, so we print // the message as well. println!("Syntax Error: {error}"); diff --git a/crates/ruff_python_semantic/src/analyze/type_inference.rs b/crates/ruff_python_semantic/src/analyze/type_inference.rs index 4d15c0c36de90..6f7dfb0469a2c 100644 --- a/crates/ruff_python_semantic/src/analyze/type_inference.rs +++ b/crates/ruff_python_semantic/src/analyze/type_inference.rs @@ -429,11 +429,11 @@ impl NumberLike { #[cfg(test)] mod tests { use ruff_python_ast::ModExpression; - use ruff_python_parser::{parse_expression, Program}; + use ruff_python_parser::{parse_expression, Parsed}; use crate::analyze::type_inference::{NumberLike, PythonType, ResolvedPythonType}; - fn parse(expression: &str) -> Program { + fn parse(expression: &str) -> Parsed { parse_expression(expression).unwrap() } diff --git a/crates/ruff_python_trivia_integration_tests/tests/block_comments.rs b/crates/ruff_python_trivia_integration_tests/tests/block_comments.rs index 884c4362d09f1..8bc8c5eb4c579 100644 --- a/crates/ruff_python_trivia_integration_tests/tests/block_comments.rs +++ b/crates/ruff_python_trivia_integration_tests/tests/block_comments.rs @@ -6,11 +6,11 @@ use ruff_text_size::TextSize; fn block_comments_two_line_block_at_start() { // arrange let source = "# line 1\n# line 2\n"; - let program = parse_unchecked(source, Mode::Module); + let parsed = parse_unchecked(source, Mode::Module); let locator = Locator::new(source); // act - let block_comments = program.comment_ranges().block_comments(&locator); + let block_comments = parsed.comment_ranges().block_comments(&locator); // assert assert_eq!(block_comments, vec![TextSize::new(0), TextSize::new(9)]); @@ -20,11 +20,11 @@ fn block_comments_two_line_block_at_start() { fn block_comments_indented_block() { // arrange let source = " # line 1\n # line 2\n"; - let program = parse_unchecked(source, Mode::Module); + let parsed = parse_unchecked(source, Mode::Module); let locator = Locator::new(source); // act - let block_comments = program.comment_ranges().block_comments(&locator); + let block_comments = parsed.comment_ranges().block_comments(&locator); // assert assert_eq!(block_comments, vec![TextSize::new(4), TextSize::new(17)]); @@ -34,11 +34,11 @@ fn block_comments_indented_block() { fn block_comments_single_line_is_not_a_block() { // arrange let source = "\n"; - let program = parse_unchecked(source, Mode::Module); + let parsed = parse_unchecked(source, Mode::Module); let locator = Locator::new(source); // act - let block_comments = program.comment_ranges().block_comments(&locator); + let block_comments = parsed.comment_ranges().block_comments(&locator); // assert assert_eq!(block_comments, Vec::::new()); @@ -48,11 +48,11 @@ fn block_comments_single_line_is_not_a_block() { fn block_comments_lines_with_code_not_a_block() { // arrange let source = "x = 1 # line 1\ny = 2 # line 2\n"; - let program = parse_unchecked(source, Mode::Module); + let parsed = parse_unchecked(source, Mode::Module); let locator = Locator::new(source); // act - let block_comments = program.comment_ranges().block_comments(&locator); + let block_comments = parsed.comment_ranges().block_comments(&locator); // assert assert_eq!(block_comments, Vec::::new()); @@ -62,11 +62,11 @@ fn block_comments_lines_with_code_not_a_block() { fn block_comments_sequential_lines_not_in_block() { // arrange let source = " # line 1\n # line 2\n"; - let program = parse_unchecked(source, Mode::Module); + let parsed = parse_unchecked(source, Mode::Module); let locator = Locator::new(source); // act - let block_comments = program.comment_ranges().block_comments(&locator); + let block_comments = parsed.comment_ranges().block_comments(&locator); // assert assert_eq!(block_comments, Vec::::new()); @@ -81,11 +81,11 @@ fn block_comments_lines_in_triple_quotes_not_a_block() { # line 2 """ "#; - let program = parse_unchecked(source, Mode::Module); + let parsed = parse_unchecked(source, Mode::Module); let locator = Locator::new(source); // act - let block_comments = program.comment_ranges().block_comments(&locator); + let block_comments = parsed.comment_ranges().block_comments(&locator); // assert assert_eq!(block_comments, Vec::::new()); @@ -117,11 +117,11 @@ y = 2 # do not form a block comment # therefore do not form a block comment """ "#; - let program = parse_unchecked(source, Mode::Module); + let parsed = parse_unchecked(source, Mode::Module); let locator = Locator::new(source); // act - let block_comments = program.comment_ranges().block_comments(&locator); + let block_comments = parsed.comment_ranges().block_comments(&locator); // assert assert_eq!( diff --git a/crates/ruff_python_trivia_integration_tests/tests/simple_tokenizer.rs b/crates/ruff_python_trivia_integration_tests/tests/simple_tokenizer.rs index 7010ec56316da..7db3766463b9c 100644 --- a/crates/ruff_python_trivia_integration_tests/tests/simple_tokenizer.rs +++ b/crates/ruff_python_trivia_integration_tests/tests/simple_tokenizer.rs @@ -22,8 +22,8 @@ impl TokenizationTestCase { } fn tokenize_reverse(&self) -> Vec { - let program = parse_unchecked(self.source, Mode::Module); - BackwardsTokenizer::new(self.source, self.range, program.comment_ranges()).collect() + let parsed = parse_unchecked(self.source, Mode::Module); + BackwardsTokenizer::new(self.source, self.range, parsed.comment_ranges()).collect() } fn tokens(&self) -> &[SimpleToken] { diff --git a/crates/ruff_server/src/lint.rs b/crates/ruff_server/src/lint.rs index 74b6295f969b3..82c3b2209d951 100644 --- a/crates/ruff_server/src/lint.rs +++ b/crates/ruff_server/src/lint.rs @@ -90,8 +90,7 @@ pub(crate) fn check(query: &DocumentQuery, encoding: PositionEncoding) -> Diagno let source_type = query.source_type(); // Parse once. - let program = - ruff_python_parser::parse_unchecked_source(source_kind.source_code(), source_type); + let parsed = ruff_python_parser::parse_unchecked_source(source_kind.source_code(), source_type); let index = LineIndex::from_source_text(source_kind.source_code()); @@ -99,13 +98,13 @@ pub(crate) fn check(query: &DocumentQuery, encoding: PositionEncoding) -> Diagno let locator = Locator::with_index(source_kind.source_code(), index.clone()); // Detect the current code style (lazily). - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); // Extra indices from the code. - let indexer = Indexer::from_tokens(program.tokens(), &locator); + let indexer = Indexer::from_tokens(parsed.tokens(), &locator); // Extract the `# noqa` and `# isort: skip` directives from the source. - let directives = extract_directives(&program, Flags::all(), &locator, &indexer); + let directives = extract_directives(&parsed, Flags::all(), &locator, &indexer); // Generate checks. let LinterResult { data, .. } = check_path( @@ -119,14 +118,14 @@ pub(crate) fn check(query: &DocumentQuery, encoding: PositionEncoding) -> Diagno flags::Noqa::Enabled, &source_kind, source_type, - &program, + &parsed, ); let noqa_edits = generate_noqa_edits( document_path, data.as_slice(), &locator, - program.comment_ranges(), + parsed.comment_ranges(), &linter_settings.external, &directives.noqa_line_for, stylist.line_ending(), diff --git a/crates/ruff_wasm/src/lib.rs b/crates/ruff_wasm/src/lib.rs index d09d59e93cbab..068975fe8393d 100644 --- a/crates/ruff_wasm/src/lib.rs +++ b/crates/ruff_wasm/src/lib.rs @@ -17,7 +17,7 @@ use ruff_python_ast::{Mod, PySourceType}; use ruff_python_codegen::Stylist; use ruff_python_formatter::{format_module_ast, pretty_comments, PyFormatContext, QuoteStyle}; use ruff_python_index::Indexer; -use ruff_python_parser::{parse, parse_unchecked, parse_unchecked_source, Mode, Program}; +use ruff_python_parser::{parse, parse_unchecked, parse_unchecked_source, Mode, Parsed}; use ruff_source_file::{Locator, SourceLocation}; use ruff_text_size::Ranged; use ruff_workspace::configuration::Configuration; @@ -160,24 +160,20 @@ impl Workspace { let source_kind = SourceKind::Python(contents.to_string()); // Parse once. - let program = parse_unchecked_source(source_kind.source_code(), source_type); + let parsed = parse_unchecked_source(source_kind.source_code(), source_type); // Map row and column locations to byte slices (lazily). let locator = Locator::new(contents); // Detect the current code style (lazily). - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); // Extra indices from the code. - let indexer = Indexer::from_tokens(program.tokens(), &locator); + let indexer = Indexer::from_tokens(parsed.tokens(), &locator); // Extract the `# noqa` and `# isort: skip` directives from the source. - let directives = directives::extract_directives( - &program, - directives::Flags::empty(), - &locator, - &indexer, - ); + let directives = + directives::extract_directives(&parsed, directives::Flags::empty(), &locator, &indexer); // Generate checks. let LinterResult { @@ -193,7 +189,7 @@ impl Workspace { flags::Noqa::Enabled, &source_kind, source_type, - &program, + &parsed, ); let source_code = locator.to_source_code(); @@ -246,8 +242,8 @@ impl Workspace { pub fn comments(&self, contents: &str) -> Result { let parsed = ParsedModule::from_source(contents)?; let comments = pretty_comments( - parsed.program.syntax(), - parsed.program.comment_ranges(), + parsed.parsed.syntax(), + parsed.parsed.comment_ranges(), contents, ); Ok(comments) @@ -255,15 +251,15 @@ impl Workspace { /// Parses the content and returns its AST pub fn parse(&self, contents: &str) -> Result { - let program = parse_unchecked(contents, Mode::Module); + let parsed = parse_unchecked(contents, Mode::Module); - Ok(format!("{:#?}", program.into_syntax())) + Ok(format!("{:#?}", parsed.into_syntax())) } pub fn tokens(&self, contents: &str) -> Result { - let program = parse_unchecked(contents, Mode::Module); + let parsed = parse_unchecked(contents, Mode::Module); - Ok(format!("{:#?}", program.tokens())) + Ok(format!("{:#?}", parsed.tokens())) } } @@ -273,14 +269,14 @@ pub(crate) fn into_error(err: E) -> Error { struct ParsedModule<'a> { source_code: &'a str, - program: Program, + parsed: Parsed, } impl<'a> ParsedModule<'a> { fn from_source(source_code: &'a str) -> Result { Ok(Self { source_code, - program: parse(source_code, Mode::Module).map_err(into_error)?, + parsed: parse(source_code, Mode::Module).map_err(into_error)?, }) } @@ -291,6 +287,6 @@ impl<'a> ParsedModule<'a> { .to_format_options(PySourceType::default(), self.source_code) .with_source_map_generation(SourceMapGeneration::Enabled); - format_module_ast(&self.program, self.source_code, options) + format_module_ast(&self.parsed, self.source_code, options) } } diff --git a/fuzz/fuzz_targets/ruff_parse_simple.rs b/fuzz/fuzz_targets/ruff_parse_simple.rs index 9d8d64ddb9ba6..805c04cd6753d 100644 --- a/fuzz/fuzz_targets/ruff_parse_simple.rs +++ b/fuzz/fuzz_targets/ruff_parse_simple.rs @@ -16,8 +16,8 @@ fn do_fuzz(case: &[u8]) -> Corpus { // just round-trip it once to trigger both parse and unparse let locator = Locator::new(code); - let program = match parse_module(code) { - Ok(program) => program, + let parsed = match parse_module(code) { + Ok(parsed) => parsed, Err(ParseError { location, .. }) => { let offset = location.start().to_usize(); assert!( @@ -29,7 +29,7 @@ fn do_fuzz(case: &[u8]) -> Corpus { } }; - for token in program.tokens() { + for token in parsed.tokens() { let start = token.start().to_usize(); let end = token.end().to_usize(); assert!( @@ -44,9 +44,9 @@ fn do_fuzz(case: &[u8]) -> Corpus { ); } - let stylist = Stylist::from_tokens(program.tokens(), &locator); + let stylist = Stylist::from_tokens(parsed.tokens(), &locator); let mut generator: Generator = (&stylist).into(); - generator.unparse_suite(program.suite()); + generator.unparse_suite(parsed.suite()); Corpus::Keep }