From b19964bf8716940f5f973d0b6ee5530c1ec9b599 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 14 Jun 2023 23:37:34 -0400 Subject: [PATCH] Use a trait --- crates/ruff/src/checkers/ast/mod.rs | 24 +- .../flake8_annotations/rules/definition.rs | 15 +- .../rules/abstract_base_class.rs | 4 +- .../rules/f_string_docstring.rs | 4 +- .../ruff/src/rules/flake8_builtins/helpers.rs | 4 +- .../flake8_pyi/rules/non_self_return_type.rs | 12 +- .../rules/str_or_repr_defined_in_stub.rs | 4 +- .../rules/stub_body_multiple_statements.rs | 4 +- .../flake8_pytest_style/rules/fixture.rs | 7 +- .../rules/no_slots_in_namedtuple_subclass.rs | 4 +- .../rules/no_slots_in_str_subclass.rs | 4 +- .../rules/no_slots_in_tuple_subclass.rs | 4 +- .../mccabe/rules/function_is_too_complex.rs | 4 +- .../pep8_naming/rules/dunder_function_name.rs | 4 +- .../rules/error_suffix_on_exception_name.rs | 4 +- .../pep8_naming/rules/invalid_class_name.rs | 4 +- .../rules/invalid_function_name.rs | 4 +- .../rules/pycodestyle/rules/bare_except.rs | 4 +- .../src/rules/pydocstyle/rules/if_needed.rs | 4 +- .../src/rules/pydocstyle/rules/not_missing.rs | 16 +- .../src/rules/pydocstyle/rules/sections.rs | 4 +- .../pyflakes/rules/default_except_not_last.rs | 4 +- .../pylint/rules/property_with_parameters.rs | 4 +- .../rules/pylint/rules/too_many_arguments.rs | 4 +- .../rules/pylint/rules/too_many_branches.rs | 4 +- .../rules/too_many_return_statements.rs | 4 +- .../rules/pylint/rules/too_many_statements.rs | 4 +- .../unexpected_special_method_signature.rs | 4 +- .../rules/unnecessary_class_parentheses.rs | 4 +- .../rules/useless_object_inheritance.rs | 4 +- crates/ruff_python_ast/src/identifier.rs | 229 +++++++++--------- 31 files changed, 208 insertions(+), 195 deletions(-) diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index 2f1a0c8223b5cf..bbb6940d8a5be2 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -12,6 +12,7 @@ use rustpython_parser::ast::{ use ruff_diagnostics::{Diagnostic, Fix, IsolationLevel}; use ruff_python_ast::all::{extract_all_names, AllNamesFlags}; use ruff_python_ast::helpers::{extract_handled_exceptions, to_module_path}; +use ruff_python_ast::identifier::Identifier; use ruff_python_ast::source_code::{Generator, Indexer, Locator, Quote, Stylist}; use ruff_python_ast::str::trailing_quote; use ruff_python_ast::types::Node; @@ -367,7 +368,7 @@ where if self.enabled(Rule::AmbiguousFunctionName) { if let Some(diagnostic) = pycodestyle::rules::ambiguous_function_name(name, || { - identifier::statement(stmt, self.locator) + stmt.identifier(self.locator) }) { self.diagnostics.push(diagnostic); @@ -692,7 +693,7 @@ where } if self.enabled(Rule::AmbiguousClassName) { if let Some(diagnostic) = pycodestyle::rules::ambiguous_class_name(name, || { - identifier::statement(stmt, self.locator) + stmt.identifier(self.locator) }) { self.diagnostics.push(diagnostic); } @@ -809,7 +810,7 @@ where self.add_binding( name, // NOTE: Incorrect, needs to be the range of the alias. - identifier::alias(&alias, self.locator), + alias.identifier(self.locator), BindingKind::FutureImportation, BindingFlags::empty(), ); @@ -830,7 +831,7 @@ where self.add_binding( name, // NOTE: Incorrect, needs to be the range of `name`. - identifier::alias(&alias, self.locator), + alias.identifier(self.locator), BindingKind::SubmoduleImportation(SubmoduleImportation { qualified_name, }), @@ -842,7 +843,7 @@ where self.add_binding( name, // NOTE: Incorrect, needs to be the range of `name`. - identifier::alias(&alias, self.locator), + alias.identifier(self.locator), BindingKind::Importation(Importation { qualified_name }), if alias .asname @@ -1088,7 +1089,7 @@ where self.add_binding( name, // NOTE: Incorrect, needs to be the range of `name`. - identifier::alias(&alias, self.locator), + alias.identifier(self.locator), BindingKind::FutureImportation, BindingFlags::empty(), ); @@ -1150,7 +1151,7 @@ where self.add_binding( name, // NOTE: Incorrect, needs to be the range of `name`. - identifier::alias(&alias, self.locator), + alias.identifier(self.locator), BindingKind::FromImportation(FromImportation { qualified_name }), if alias .asname @@ -1877,7 +1878,7 @@ where self.add_binding( name, // NOTE: Correct! - identifier::statement(stmt, self.locator), + stmt.identifier(self.locator), BindingKind::FunctionDefinition, BindingFlags::empty(), ); @@ -2101,7 +2102,7 @@ where self.add_binding( name, // NOTE: Correct! - identifier::statement(stmt, self.locator), + stmt.identifier(self.locator), BindingKind::ClassDefinition, BindingFlags::empty(), ); @@ -3909,8 +3910,7 @@ where } match name { Some(name) => { - let range = identifier::exception_range(excepthandler, self.locator) - .expect("Failed to find `name` range"); + let range = excepthandler.identifier(self.locator); if self.enabled(Rule::AmbiguousVariableName) { if let Some(diagnostic) = @@ -4028,7 +4028,7 @@ where // upstream. self.add_binding( &arg.arg, - identifier::arg(arg, self.locator), + arg.identifier(self.locator), BindingKind::Argument, BindingFlags::empty(), ); diff --git a/crates/ruff/src/rules/flake8_annotations/rules/definition.rs b/crates/ruff/src/rules/flake8_annotations/rules/definition.rs index cd99f4f258792a..cdff92be156865 100644 --- a/crates/ruff/src/rules/flake8_annotations/rules/definition.rs +++ b/crates/ruff/src/rules/flake8_annotations/rules/definition.rs @@ -3,8 +3,9 @@ use rustpython_parser::ast::{Expr, Ranged, Stmt}; use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::ReturnStatementVisitor; +use ruff_python_ast::identifier::Identifier; use ruff_python_ast::statement_visitor::StatementVisitor; -use ruff_python_ast::{cast, identifier}; +use ruff_python_ast::{cast}; use ruff_python_semantic::analyze::visibility; use ruff_python_semantic::{Definition, Member, MemberKind, SemanticModel}; use ruff_python_stdlib::typing::SIMPLE_MAGIC_RETURN_TYPES; @@ -640,7 +641,7 @@ pub(crate) fn definition( MissingReturnTypeClassMethod { name: name.to_string(), }, - identifier::statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } } else if is_method @@ -651,7 +652,7 @@ pub(crate) fn definition( MissingReturnTypeStaticMethod { name: name.to_string(), }, - identifier::statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } } else if is_method && visibility::is_init(name) { @@ -663,7 +664,7 @@ pub(crate) fn definition( MissingReturnTypeSpecialMethod { name: name.to_string(), }, - identifier::statement(stmt, checker.locator), + stmt.identifier(checker.locator), ); if checker.patch(diagnostic.kind.rule()) { #[allow(deprecated)] @@ -680,7 +681,7 @@ pub(crate) fn definition( MissingReturnTypeSpecialMethod { name: name.to_string(), }, - identifier::statement(stmt, checker.locator), + stmt.identifier(checker.locator), ); let return_type = SIMPLE_MAGIC_RETURN_TYPES.get(name); if let Some(return_type) = return_type { @@ -701,7 +702,7 @@ pub(crate) fn definition( MissingReturnTypeUndocumentedPublicFunction { name: name.to_string(), }, - identifier::statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } } @@ -711,7 +712,7 @@ pub(crate) fn definition( MissingReturnTypePrivateFunction { name: name.to_string(), }, - identifier::statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } } diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/abstract_base_class.rs b/crates/ruff/src/rules/flake8_bugbear/rules/abstract_base_class.rs index 97f1433b4796a9..16606d08fff429 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/abstract_base_class.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/abstract_base_class.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Constant, Expr, Keyword, Ranged, Stmt}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_semantic::analyze::visibility::{is_abstract, is_overload}; use ruff_python_semantic::SemanticModel; @@ -134,7 +134,7 @@ pub(crate) fn abstract_base_class( AbstractBaseClassWithoutAbstractMethod { name: name.to_string(), }, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } } diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/f_string_docstring.rs b/crates/ruff/src/rules/flake8_bugbear/rules/f_string_docstring.rs index 6d8b72634a7b0d..8556160ba59e4c 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/f_string_docstring.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/f_string_docstring.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Expr, Stmt}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier; +use ruff_python_ast::identifier::Identifier; use crate::checkers::ast::Checker; @@ -31,6 +31,6 @@ pub(crate) fn f_string_docstring(checker: &mut Checker, body: &[Stmt]) { }; checker.diagnostics.push(Diagnostic::new( FStringDocstring, - identifier::statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } diff --git a/crates/ruff/src/rules/flake8_builtins/helpers.rs b/crates/ruff/src/rules/flake8_builtins/helpers.rs index d5b24698678b2a..5b6c45761efb32 100644 --- a/crates/ruff/src/rules/flake8_builtins/helpers.rs +++ b/crates/ruff/src/rules/flake8_builtins/helpers.rs @@ -1,7 +1,7 @@ use ruff_text_size::TextRange; use rustpython_parser::ast::{Excepthandler, Expr, Ranged, Stmt}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_ast::source_code::Locator; use ruff_python_stdlib::builtins::BUILTINS; @@ -20,7 +20,7 @@ impl AnyShadowing<'_> { pub(crate) fn range(self, locator: &Locator) -> TextRange { match self { AnyShadowing::Expression(expr) => expr.range(), - AnyShadowing::Statement(stmt) => statement(stmt, locator), + AnyShadowing::Statement(stmt) => stmt.identifier(locator), AnyShadowing::ExceptHandler(handler) => handler.range(), } } diff --git a/crates/ruff/src/rules/flake8_pyi/rules/non_self_return_type.rs b/crates/ruff/src/rules/flake8_pyi/rules/non_self_return_type.rs index 9aad391cef372d..78155686c98284 100644 --- a/crates/ruff/src/rules/flake8_pyi/rules/non_self_return_type.rs +++ b/crates/ruff/src/rules/flake8_pyi/rules/non_self_return_type.rs @@ -3,7 +3,7 @@ use rustpython_parser::ast::{self, Arguments, Decorator, Expr, Stmt}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::map_subscript; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_semantic::analyze::visibility::{is_abstract, is_final, is_overload}; use ruff_python_semantic::{ScopeKind, SemanticModel}; @@ -148,7 +148,7 @@ pub(crate) fn non_self_return_type( class_name: class_def.name.to_string(), method_name: name.to_string(), }, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } return; @@ -162,7 +162,7 @@ pub(crate) fn non_self_return_type( class_name: class_def.name.to_string(), method_name: name.to_string(), }, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } return; @@ -177,7 +177,7 @@ pub(crate) fn non_self_return_type( class_name: class_def.name.to_string(), method_name: name.to_string(), }, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } return; @@ -193,7 +193,7 @@ pub(crate) fn non_self_return_type( class_name: class_def.name.to_string(), method_name: name.to_string(), }, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } } @@ -206,7 +206,7 @@ pub(crate) fn non_self_return_type( class_name: class_def.name.to_string(), method_name: name.to_string(), }, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } } diff --git a/crates/ruff/src/rules/flake8_pyi/rules/str_or_repr_defined_in_stub.rs b/crates/ruff/src/rules/flake8_pyi/rules/str_or_repr_defined_in_stub.rs index 993036e974bd71..3ae9eab2f869db 100644 --- a/crates/ruff/src/rules/flake8_pyi/rules/str_or_repr_defined_in_stub.rs +++ b/crates/ruff/src/rules/flake8_pyi/rules/str_or_repr_defined_in_stub.rs @@ -3,7 +3,7 @@ use rustpython_parser::ast::Stmt; use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_semantic::analyze::visibility::is_abstract; use crate::autofix::edits::delete_stmt; @@ -90,7 +90,7 @@ pub(crate) fn str_or_repr_defined_in_stub(checker: &mut Checker, stmt: &Stmt) { StrOrReprDefinedInStub { name: name.to_string(), }, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), ); if checker.patch(diagnostic.kind.rule()) { let stmt = checker.semantic().stmt(); diff --git a/crates/ruff/src/rules/flake8_pyi/rules/stub_body_multiple_statements.rs b/crates/ruff/src/rules/flake8_pyi/rules/stub_body_multiple_statements.rs index 19825870a79dd1..bfc0e708571327 100644 --- a/crates/ruff/src/rules/flake8_pyi/rules/stub_body_multiple_statements.rs +++ b/crates/ruff/src/rules/flake8_pyi/rules/stub_body_multiple_statements.rs @@ -3,7 +3,7 @@ use rustpython_parser::ast::Stmt; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::is_docstring_stmt; -use ruff_python_ast::identifier; +use ruff_python_ast::identifier::Identifier; use crate::checkers::ast::Checker; @@ -32,6 +32,6 @@ pub(crate) fn stub_body_multiple_statements(checker: &mut Checker, stmt: &Stmt, checker.diagnostics.push(Diagnostic::new( StubBodyMultipleStatements, - identifier::statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } diff --git a/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs b/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs index 4efae2d4823d2b..3b3bb2b77fe2df 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs @@ -9,10 +9,11 @@ use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::call_path::collect_call_path; use ruff_python_ast::helpers::collect_arg_names; +use ruff_python_ast::identifier::Identifier; use ruff_python_ast::prelude::Decorator; use ruff_python_ast::source_code::Locator; use ruff_python_ast::visitor::Visitor; -use ruff_python_ast::{identifier, visitor}; +use ruff_python_ast::{visitor}; use ruff_python_semantic::analyze::visibility::is_abstract; use ruff_python_semantic::SemanticModel; @@ -378,7 +379,7 @@ fn check_fixture_returns(checker: &mut Checker, stmt: &Stmt, name: &str, body: & PytestIncorrectFixtureNameUnderscore { function: name.to_string(), }, - identifier::statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } else if checker.enabled(Rule::PytestMissingFixtureNameUnderscore) && !visitor.has_return_with_value @@ -389,7 +390,7 @@ fn check_fixture_returns(checker: &mut Checker, stmt: &Stmt, name: &str, body: & PytestMissingFixtureNameUnderscore { function: name.to_string(), }, - identifier::statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } diff --git a/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_namedtuple_subclass.rs b/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_namedtuple_subclass.rs index 29aeab3ed69d54..3a56fc07c63ae5 100644 --- a/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_namedtuple_subclass.rs +++ b/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_namedtuple_subclass.rs @@ -3,7 +3,7 @@ use rustpython_parser::ast::{Expr, StmtClassDef}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_ast::prelude::Stmt; use crate::checkers::ast::Checker; @@ -77,7 +77,7 @@ pub(crate) fn no_slots_in_namedtuple_subclass( if !has_slots(&class.body) { checker.diagnostics.push(Diagnostic::new( NoSlotsInNamedtupleSubclass, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } } diff --git a/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_str_subclass.rs b/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_str_subclass.rs index 944a49854aabe9..a1a5182c6190d6 100644 --- a/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_str_subclass.rs +++ b/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_str_subclass.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::{Stmt, StmtClassDef}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use crate::checkers::ast::Checker; use crate::rules::flake8_slots::rules::helpers::has_slots; @@ -61,7 +61,7 @@ pub(crate) fn no_slots_in_str_subclass(checker: &mut Checker, stmt: &Stmt, class if !has_slots(&class.body) { checker.diagnostics.push(Diagnostic::new( NoSlotsInStrSubclass, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } } diff --git a/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_tuple_subclass.rs b/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_tuple_subclass.rs index 964141e51e448d..c57cd510b1d370 100644 --- a/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_tuple_subclass.rs +++ b/crates/ruff/src/rules/flake8_slots/rules/no_slots_in_tuple_subclass.rs @@ -3,7 +3,7 @@ use rustpython_parser::ast::{Stmt, StmtClassDef}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::map_subscript; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use crate::checkers::ast::Checker; use crate::rules::flake8_slots::rules::helpers::has_slots; @@ -65,7 +65,7 @@ pub(crate) fn no_slots_in_tuple_subclass(checker: &mut Checker, stmt: &Stmt, cla if !has_slots(&class.body) { checker.diagnostics.push(Diagnostic::new( NoSlotsInTupleSubclass, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } } diff --git a/crates/ruff/src/rules/mccabe/rules/function_is_too_complex.rs b/crates/ruff/src/rules/mccabe/rules/function_is_too_complex.rs index 8a5dd3405e992a..6b27ba7d624a10 100644 --- a/crates/ruff/src/rules/mccabe/rules/function_is_too_complex.rs +++ b/crates/ruff/src/rules/mccabe/rules/function_is_too_complex.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Excepthandler, Stmt}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_ast::source_code::Locator; /// ## What it does @@ -152,7 +152,7 @@ pub(crate) fn function_is_too_complex( complexity, max_complexity, }, - statement(stmt, locator), + stmt.identifier(locator), )) } else { None diff --git a/crates/ruff/src/rules/pep8_naming/rules/dunder_function_name.rs b/crates/ruff/src/rules/pep8_naming/rules/dunder_function_name.rs index 9b283f3e9771d3..4f248b2040379b 100644 --- a/crates/ruff/src/rules/pep8_naming/rules/dunder_function_name.rs +++ b/crates/ruff/src/rules/pep8_naming/rules/dunder_function_name.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::Stmt; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_ast::source_code::Locator; use ruff_python_semantic::{Scope, ScopeKind}; @@ -69,6 +69,6 @@ pub(crate) fn dunder_function_name( Some(Diagnostic::new( DunderFunctionName, - statement(stmt, locator), + stmt.identifier(locator), )) } diff --git a/crates/ruff/src/rules/pep8_naming/rules/error_suffix_on_exception_name.rs b/crates/ruff/src/rules/pep8_naming/rules/error_suffix_on_exception_name.rs index 0b6e13ee987699..182ba1e404622b 100644 --- a/crates/ruff/src/rules/pep8_naming/rules/error_suffix_on_exception_name.rs +++ b/crates/ruff/src/rules/pep8_naming/rules/error_suffix_on_exception_name.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Expr, Stmt}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_ast::source_code::Locator; use crate::settings::types::IdentifierPattern; @@ -75,6 +75,6 @@ pub(crate) fn error_suffix_on_exception_name( ErrorSuffixOnExceptionName { name: name.to_string(), }, - statement(class_def, locator), + class_def.identifier(locator), )) } diff --git a/crates/ruff/src/rules/pep8_naming/rules/invalid_class_name.rs b/crates/ruff/src/rules/pep8_naming/rules/invalid_class_name.rs index a4ea79769fdb80..c6d7e72e9982b9 100644 --- a/crates/ruff/src/rules/pep8_naming/rules/invalid_class_name.rs +++ b/crates/ruff/src/rules/pep8_naming/rules/invalid_class_name.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::Stmt; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_ast::source_code::Locator; use crate::settings::types::IdentifierPattern; @@ -69,7 +69,7 @@ pub(crate) fn invalid_class_name( InvalidClassName { name: name.to_string(), }, - statement(class_def, locator), + class_def.identifier(locator), )); } None diff --git a/crates/ruff/src/rules/pep8_naming/rules/invalid_function_name.rs b/crates/ruff/src/rules/pep8_naming/rules/invalid_function_name.rs index 7dd7685719f469..b23556566e2336 100644 --- a/crates/ruff/src/rules/pep8_naming/rules/invalid_function_name.rs +++ b/crates/ruff/src/rules/pep8_naming/rules/invalid_function_name.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::{Decorator, Stmt}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_ast::source_code::Locator; use ruff_python_semantic::analyze::visibility; use ruff_python_semantic::SemanticModel; @@ -81,6 +81,6 @@ pub(crate) fn invalid_function_name( InvalidFunctionName { name: name.to_string(), }, - statement(stmt, locator), + stmt.identifier(locator), )) } diff --git a/crates/ruff/src/rules/pycodestyle/rules/bare_except.rs b/crates/ruff/src/rules/pycodestyle/rules/bare_except.rs index a62a5b2efab04f..457bdd63470a17 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/bare_except.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/bare_except.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Excepthandler, Expr, Stmt}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::except_range; +use ruff_python_ast::identifier::except; use ruff_python_ast::source_code::Locator; /// ## What it does @@ -66,7 +66,7 @@ pub(crate) fn bare_except( .iter() .any(|stmt| matches!(stmt, Stmt::Raise(ast::StmtRaise { exc: None, .. }))) { - Some(Diagnostic::new(BareExcept, except_range(handler, locator))) + Some(Diagnostic::new(BareExcept, except(handler, locator))) } else { None } diff --git a/crates/ruff/src/rules/pydocstyle/rules/if_needed.rs b/crates/ruff/src/rules/pydocstyle/rules/if_needed.rs index 68be612c9cea8d..0f531d8d746f43 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/if_needed.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/if_needed.rs @@ -1,7 +1,7 @@ use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::cast; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_semantic::analyze::visibility::is_overload; use ruff_python_semantic::{Definition, Member, MemberKind}; @@ -32,6 +32,6 @@ pub(crate) fn if_needed(checker: &mut Checker, docstring: &Docstring) { } checker.diagnostics.push(Diagnostic::new( OverloadWithDocstring, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } diff --git a/crates/ruff/src/rules/pydocstyle/rules/not_missing.rs b/crates/ruff/src/rules/pydocstyle/rules/not_missing.rs index da222c5f4626e4..1d78d994442bba 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/not_missing.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/not_missing.rs @@ -3,7 +3,7 @@ use ruff_text_size::TextRange; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::cast; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_semantic::analyze::visibility::{ is_call, is_init, is_magic, is_new, is_overload, is_override, Visibility, }; @@ -135,7 +135,7 @@ pub(crate) fn not_missing( if checker.enabled(Rule::UndocumentedPublicClass) { checker.diagnostics.push(Diagnostic::new( UndocumentedPublicClass, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } false @@ -148,7 +148,7 @@ pub(crate) fn not_missing( if checker.enabled(Rule::UndocumentedPublicNestedClass) { checker.diagnostics.push(Diagnostic::new( UndocumentedPublicNestedClass, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } false @@ -164,7 +164,7 @@ pub(crate) fn not_missing( if checker.enabled(Rule::UndocumentedPublicFunction) { checker.diagnostics.push(Diagnostic::new( UndocumentedPublicFunction, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } false @@ -183,7 +183,7 @@ pub(crate) fn not_missing( if checker.enabled(Rule::UndocumentedPublicInit) { checker.diagnostics.push(Diagnostic::new( UndocumentedPublicInit, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } true @@ -191,7 +191,7 @@ pub(crate) fn not_missing( if checker.enabled(Rule::UndocumentedPublicMethod) { checker.diagnostics.push(Diagnostic::new( UndocumentedPublicMethod, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } true @@ -199,7 +199,7 @@ pub(crate) fn not_missing( if checker.enabled(Rule::UndocumentedMagicMethod) { checker.diagnostics.push(Diagnostic::new( UndocumentedMagicMethod, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } true @@ -207,7 +207,7 @@ pub(crate) fn not_missing( if checker.enabled(Rule::UndocumentedPublicMethod) { checker.diagnostics.push(Diagnostic::new( UndocumentedPublicMethod, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } true diff --git a/crates/ruff/src/rules/pydocstyle/rules/sections.rs b/crates/ruff/src/rules/pydocstyle/rules/sections.rs index 8d7dcdbd282cd5..26d3f061099d0c 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/sections.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/sections.rs @@ -10,7 +10,7 @@ use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::cast; use ruff_python_ast::docstrings::{clean_space, leading_space}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_semantic::analyze::visibility::is_staticmethod; use ruff_python_semantic::{Definition, Member, MemberKind}; use ruff_python_whitespace::NewlineWithTrailingNewline; @@ -759,7 +759,7 @@ fn missing_args(checker: &mut Checker, docstring: &Docstring, docstrings_args: & let names = missing_arg_names.into_iter().sorted().collect(); checker.diagnostics.push(Diagnostic::new( UndocumentedParam { names }, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } } diff --git a/crates/ruff/src/rules/pyflakes/rules/default_except_not_last.rs b/crates/ruff/src/rules/pyflakes/rules/default_except_not_last.rs index f31cadf327dbe9..26a760d2b7d658 100644 --- a/crates/ruff/src/rules/pyflakes/rules/default_except_not_last.rs +++ b/crates/ruff/src/rules/pyflakes/rules/default_except_not_last.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Excepthandler}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::except_range; +use ruff_python_ast::identifier::except; use ruff_python_ast::source_code::Locator; /// ## What it does @@ -63,7 +63,7 @@ pub(crate) fn default_except_not_last( if type_.is_none() && idx < handlers.len() - 1 { return Some(Diagnostic::new( DefaultExceptNotLast, - except_range(handler, locator), + except(handler, locator), )); } } diff --git a/crates/ruff/src/rules/pylint/rules/property_with_parameters.rs b/crates/ruff/src/rules/pylint/rules/property_with_parameters.rs index 0baf92fbbc9997..1029a9d97f4835 100644 --- a/crates/ruff/src/rules/pylint/rules/property_with_parameters.rs +++ b/crates/ruff/src/rules/pylint/rules/property_with_parameters.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Arguments, Decorator, Expr, Stmt}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use crate::checkers::ast::Checker; @@ -70,7 +70,7 @@ pub(crate) fn property_with_parameters( { checker.diagnostics.push(Diagnostic::new( PropertyWithParameters, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } } diff --git a/crates/ruff/src/rules/pylint/rules/too_many_arguments.rs b/crates/ruff/src/rules/pylint/rules/too_many_arguments.rs index 5d08a090d0e842..2d050ee75c1127 100644 --- a/crates/ruff/src/rules/pylint/rules/too_many_arguments.rs +++ b/crates/ruff/src/rules/pylint/rules/too_many_arguments.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::{Arguments, Stmt}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use crate::checkers::ast::Checker; @@ -72,7 +72,7 @@ pub(crate) fn too_many_arguments(checker: &mut Checker, args: &Arguments, stmt: c_args: num_args, max_args: checker.settings.pylint.max_args, }, - statement(stmt, checker.locator), + stmt.identifier(checker.locator), )); } } diff --git a/crates/ruff/src/rules/pylint/rules/too_many_branches.rs b/crates/ruff/src/rules/pylint/rules/too_many_branches.rs index aa7d742f7f20ea..8a9b0be78f8615 100644 --- a/crates/ruff/src/rules/pylint/rules/too_many_branches.rs +++ b/crates/ruff/src/rules/pylint/rules/too_many_branches.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Excepthandler, Stmt}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_ast::source_code::Locator; /// ## What it does @@ -175,7 +175,7 @@ pub(crate) fn too_many_branches( branches, max_branches, }, - statement(stmt, locator), + stmt.identifier(locator), )) } else { None diff --git a/crates/ruff/src/rules/pylint/rules/too_many_return_statements.rs b/crates/ruff/src/rules/pylint/rules/too_many_return_statements.rs index 1998d0c7cbdadb..ef5e45d2d3d796 100644 --- a/crates/ruff/src/rules/pylint/rules/too_many_return_statements.rs +++ b/crates/ruff/src/rules/pylint/rules/too_many_return_statements.rs @@ -3,7 +3,7 @@ use rustpython_parser::ast::Stmt; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::ReturnStatementVisitor; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_ast::source_code::Locator; use ruff_python_ast::statement_visitor::StatementVisitor; @@ -90,7 +90,7 @@ pub(crate) fn too_many_return_statements( returns, max_returns, }, - statement(stmt, locator), + stmt.identifier(locator), )) } else { None diff --git a/crates/ruff/src/rules/pylint/rules/too_many_statements.rs b/crates/ruff/src/rules/pylint/rules/too_many_statements.rs index 3fa10a6f2e433b..755daab2b0fdb6 100644 --- a/crates/ruff/src/rules/pylint/rules/too_many_statements.rs +++ b/crates/ruff/src/rules/pylint/rules/too_many_statements.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Excepthandler, Stmt}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_ast::source_code::Locator; /// ## What it does @@ -158,7 +158,7 @@ pub(crate) fn too_many_statements( statements, max_statements, }, - statement(stmt, locator), + stmt.identifier(locator), )) } else { None diff --git a/crates/ruff/src/rules/pylint/rules/unexpected_special_method_signature.rs b/crates/ruff/src/rules/pylint/rules/unexpected_special_method_signature.rs index 8e14344b8d1c8c..8277cb699e3801 100644 --- a/crates/ruff/src/rules/pylint/rules/unexpected_special_method_signature.rs +++ b/crates/ruff/src/rules/pylint/rules/unexpected_special_method_signature.rs @@ -4,7 +4,7 @@ use rustpython_parser::ast::{Arguments, Decorator, Stmt}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use ruff_python_ast::source_code::Locator; use ruff_python_semantic::analyze::visibility::is_staticmethod; @@ -189,7 +189,7 @@ pub(crate) fn unexpected_special_method_signature( expected_params, actual_params, }, - statement(stmt, locator), + stmt.identifier(locator), )); } } diff --git a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_class_parentheses.rs b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_class_parentheses.rs index e7e3f22080bcd8..cd2483fd656344 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_class_parentheses.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_class_parentheses.rs @@ -5,7 +5,7 @@ use rustpython_parser::ast::{self, Stmt}; use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use crate::checkers::ast::Checker; use crate::registry::AsRule; @@ -53,7 +53,7 @@ pub(crate) fn unnecessary_class_parentheses( return; } - let offset = statement(stmt, checker.locator).start(); + let offset = stmt.identifier(checker.locator).start(); let contents = checker.locator.after(offset); // Find the open and closing parentheses between the class name and the colon, if they exist. diff --git a/crates/ruff/src/rules/pyupgrade/rules/useless_object_inheritance.rs b/crates/ruff/src/rules/pyupgrade/rules/useless_object_inheritance.rs index 1de357739cbc9a..ad61c35c8b6c4f 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/useless_object_inheritance.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/useless_object_inheritance.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Expr, Ranged, Stmt}; use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::identifier::statement; +use ruff_python_ast::identifier::Identifier; use crate::autofix::edits::remove_argument; use crate::checkers::ast::Checker; @@ -73,7 +73,7 @@ pub(crate) fn useless_object_inheritance( diagnostic.try_set_fix(|| { let edit = remove_argument( checker.locator, - statement(stmt, checker.locator).start(), + stmt.identifier(checker.locator).start(), expr.range(), &class_def.bases, &class_def.keywords, diff --git a/crates/ruff_python_ast/src/identifier.rs b/crates/ruff_python_ast/src/identifier.rs index 2b6ede5f71808d..e3e30b70415225 100644 --- a/crates/ruff_python_ast/src/identifier.rs +++ b/crates/ruff_python_ast/src/identifier.rs @@ -27,87 +27,118 @@ use ruff_python_whitespace::is_python_whitespace; use crate::source_code::Locator; -/// Return the [`TextRange`] of the identifier in the given statement. -/// -/// For example, return the range of `f` in: -/// ```python -/// def f(): -/// ... -/// ``` -pub fn statement(stmt: &Stmt, locator: &Locator) -> TextRange { - match stmt { - Stmt::ClassDef(ast::StmtClassDef { - decorator_list, - range, - .. - }) - | Stmt::FunctionDef(ast::StmtFunctionDef { - decorator_list, - range, - .. - }) => { - let range = decorator_list.last().map_or(*range, |last_decorator| { - TextRange::new(last_decorator.end(), range.end()) - }); - - // The first "identifier" is the `def` or `class` keyword. - // The second "identifier" is the function or class name. - IdentifierTokenizer::starts_at(range.start(), locator.contents()) - .nth(1) - .expect("Unable to identify identifier in function or class definition") - } - Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { - decorator_list, - range, - .. - }) => { - let range = decorator_list.last().map_or(*range, |last_decorator| { - TextRange::new(last_decorator.end(), range.end()) - }); - - // The first "identifier" is the `async` keyword. - // The second "identifier" is the `def` or `class` keyword. - // The third "identifier" is the function or class name. - IdentifierTokenizer::starts_at(range.start(), locator.contents()) - .nth(2) - .expect("Unable to identify identifier in function or class definition") +pub trait Identifier { + fn identifier(&self, locator: &Locator) -> TextRange; +} + +impl Identifier for Stmt { + /// Return the [`TextRange`] of the identifier in the given statement. + /// + /// For example, return the range of `f` in: + /// ```python + /// def f(): + /// ... + /// ``` + fn identifier(&self, locator: &Locator) -> TextRange { + match self { + Stmt::ClassDef(ast::StmtClassDef { + decorator_list, + range, + .. + }) + | Stmt::FunctionDef(ast::StmtFunctionDef { + decorator_list, + range, + .. + }) => { + let range = decorator_list.last().map_or(*range, |last_decorator| { + TextRange::new(last_decorator.end(), range.end()) + }); + + // The first "identifier" is the `def` or `class` keyword. + // The second "identifier" is the function or class name. + IdentifierTokenizer::starts_at(range.start(), locator.contents()) + .nth(1) + .expect("Unable to identify identifier in function or class definition") + } + Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { + decorator_list, + range, + .. + }) => { + let range = decorator_list.last().map_or(*range, |last_decorator| { + TextRange::new(last_decorator.end(), range.end()) + }); + + // The first "identifier" is the `async` keyword. + // The second "identifier" is the `def` or `class` keyword. + // The third "identifier" is the function or class name. + IdentifierTokenizer::starts_at(range.start(), locator.contents()) + .nth(2) + .expect("Unable to identify identifier in function or class definition") + } + _ => self.range(), } - _ => stmt.range(), } } -/// Return the [`TextRange`] for the identifier defining an [`Arg`]. -/// -/// For example, return the range of `x` in: -/// ```python -/// def f(x: int = 0): -/// ... -/// ``` -pub fn arg(arg: &Arg, locator: &Locator) -> TextRange { - IdentifierTokenizer::new(locator.contents(), arg.range()) - .next() - .expect("Failed to find argument identifier") +impl Identifier for Arg { + /// Return the [`TextRange`] for the identifier defining an [`Arg`]. + /// + /// For example, return the range of `x` in: + /// ```python + /// def f(x: int = 0): + /// ... + /// ``` + fn identifier(&self, locator: &Locator) -> TextRange { + IdentifierTokenizer::new(locator.contents(), self.range()) + .next() + .expect("Failed to find argument identifier") + } } -/// Return the [`TextRange`] for the identifier defining an [`Alias`]. -/// -/// For example, return the range of `x` in: -/// ```python -/// from foo import bar as x -/// ``` -pub fn alias(alias: &Alias, locator: &Locator) -> TextRange { - if alias.asname.is_none() { - // The first identifier is the module name. - IdentifierTokenizer::new(locator.contents(), alias.range()) - .next() - .expect("Failed to find alias identifier") - } else { - // The first identifier is the module name. - // The second identifier is the "as" keyword. - // The third identifier is the alias name. - IdentifierTokenizer::new(locator.contents(), alias.range()) - .nth(2) - .expect("Failed to find alias identifier") +impl Identifier for Alias { + /// Return the [`TextRange`] for the identifier defining an [`Alias`]. + /// + /// For example, return the range of `x` in: + /// ```python + /// from foo import bar as x + /// ``` + fn identifier(&self, locator: &Locator) -> TextRange { + if matches!(self.name.as_str(), "*") { + self.range() + } else if self.asname.is_none() { + // The first identifier is the module name. + IdentifierTokenizer::new(locator.contents(), self.range()) + .next() + .expect("Failed to find alias identifier") + } else { + // The first identifier is the module name. + // The second identifier is the "as" keyword. + // The third identifier is the alias name. + IdentifierTokenizer::new(locator.contents(), self.range()) + .nth(2) + .expect("Failed to find alias identifier") + } + } +} + +impl Identifier for Excepthandler { + /// Return the [`TextRange`] of a named exception in an [`Excepthandler`]. + /// + /// For example, return the range of `e` in: + /// ```python + /// try: + /// ... + /// except ValueError as e: + /// ... + /// ``` + fn identifier(&self, locator: &Locator) -> TextRange { + // The exception name is the first identifier token after the `as` keyword. + let Excepthandler::ExceptHandler(ast::ExcepthandlerExceptHandler { type_, .. }) = self; + IdentifierTokenizer::starts_at(type_.as_ref().unwrap().end(), locator.contents()) + .nth(1) + .expect("Failed to find exception identifier in exception handler") } } @@ -123,28 +154,8 @@ pub fn names<'a>(stmt: &Stmt, locator: &'a Locator<'a>) -> impl Iterator Option { - let Excepthandler::ExceptHandler(ast::ExcepthandlerExceptHandler { type_, .. }) = handler; - - let Some(type_) = type_ else { - return None; - }; - - // The exception name is the first identifier token after the `as` keyword. - IdentifierTokenizer::starts_at(type_.end(), locator.contents()).nth(1) -} - /// Return the [`TextRange`] of the `except` token in an [`Excepthandler`]. -pub fn except_range(handler: &Excepthandler, locator: &Locator) -> TextRange { +pub fn except(handler: &Excepthandler, locator: &Locator) -> TextRange { IdentifierTokenizer::new(locator.contents(), handler.range()) .next() .expect("Failed to find `except` token in `Excepthandler`") @@ -155,7 +166,7 @@ fn is_python_identifier(c: char) -> bool { c.is_alphanumeric() || c == '_' || c == '.' } -/// Simple zero allocation tokenizer for tokenizing trivia (and some tokens). +/// Simple zero allocation tokenizer for Python identifiers. /// /// The tokenizer must start at an offset that is trivia (e.g. not inside of a multiline string). /// @@ -294,7 +305,7 @@ mod tests { use rustpython_parser::Parse; use crate::helpers::{elif_else_range, else_range, first_colon_range}; - use crate::identifier; + use crate::identifier::Identifier; use crate::source_code::Locator; #[test] @@ -306,7 +317,7 @@ mod tests { let arg = &args[0]; let locator = Locator::new(contents); assert_eq!( - identifier::arg(arg, &locator), + arg.identifier(&locator), TextRange::new(TextSize::from(6), TextSize::from(7)) ); @@ -317,7 +328,7 @@ mod tests { let arg = &args[0]; let locator = Locator::new(contents); assert_eq!( - identifier::arg(arg, &locator), + arg.identifier(&locator), TextRange::new(TextSize::from(6), TextSize::from(7)) ); @@ -334,7 +345,7 @@ def f( let arg = &args[0]; let locator = Locator::new(contents); assert_eq!( - identifier::arg(arg, &locator), + arg.identifier(&locator), TextRange::new(TextSize::from(11), TextSize::from(12)) ); @@ -347,7 +358,7 @@ def f( let stmt = Stmt::parse(contents, "")?; let locator = Locator::new(contents); assert_eq!( - identifier::statement(&stmt, &locator), + stmt.identifier(&locator), TextRange::new(TextSize::from(4), TextSize::from(5)) ); @@ -355,7 +366,7 @@ def f( let stmt = Stmt::parse(contents, "")?; let locator = Locator::new(contents); assert_eq!( - identifier::statement(&stmt, &locator), + stmt.identifier(&locator), TextRange::new(TextSize::from(10), TextSize::from(11)) ); @@ -368,7 +379,7 @@ def \ let stmt = Stmt::parse(contents, "")?; let locator = Locator::new(contents); assert_eq!( - identifier::statement(&stmt, &locator), + stmt.identifier(&locator), TextRange::new(TextSize::from(8), TextSize::from(9)) ); @@ -376,7 +387,7 @@ def \ let stmt = Stmt::parse(contents, "")?; let locator = Locator::new(contents); assert_eq!( - identifier::statement(&stmt, &locator), + stmt.identifier(&locator), TextRange::new(TextSize::from(6), TextSize::from(11)) ); @@ -384,7 +395,7 @@ def \ let stmt = Stmt::parse(contents, "")?; let locator = Locator::new(contents); assert_eq!( - identifier::statement(&stmt, &locator), + stmt.identifier(&locator), TextRange::new(TextSize::from(6), TextSize::from(11)) ); @@ -397,7 +408,7 @@ class Class(): let stmt = Stmt::parse(contents, "")?; let locator = Locator::new(contents); assert_eq!( - identifier::statement(&stmt, &locator), + stmt.identifier(&locator), TextRange::new(TextSize::from(19), TextSize::from(24)) ); @@ -410,7 +421,7 @@ class Class(): let stmt = Stmt::parse(contents, "")?; let locator = Locator::new(contents); assert_eq!( - identifier::statement(&stmt, &locator), + stmt.identifier(&locator), TextRange::new(TextSize::from(30), TextSize::from(35)) ); @@ -418,7 +429,7 @@ class Class(): let stmt = Stmt::parse(contents, "")?; let locator = Locator::new(contents); assert_eq!( - identifier::statement(&stmt, &locator), + stmt.identifier(&locator), TextRange::new(TextSize::from(0), TextSize::from(9)) );