Remove helpers.rs dependency on Binding (#3839)

This commit is contained in:
Charlie Marsh 2023-03-31 23:19:45 -04:00 committed by GitHub
parent b6276e2d95
commit 27e40e9b31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 19 deletions

View file

@ -14,7 +14,7 @@ use rustpython_parser::ast::{
use ruff_diagnostics::Diagnostic;
use ruff_python_ast::context::Context;
use ruff_python_ast::helpers::{binding_range, extract_handled_exceptions, to_module_path};
use ruff_python_ast::helpers::{extract_handled_exceptions, to_module_path};
use ruff_python_ast::operations::{extract_all_names, AllNamesFlags};
use ruff_python_ast::scope::{
Binding, BindingId, BindingKind, ClassDef, Exceptions, ExecutionContext, Export,
@ -4058,7 +4058,16 @@ impl<'a> Checker<'a> {
name: name.to_string(),
line: existing.range.location.row(),
},
binding_range(&binding, self.locator),
matches!(
binding.kind,
BindingKind::ClassDefinition | BindingKind::FunctionDefinition
)
.then(|| {
binding.source.as_ref().map_or(binding.range, |source| {
helpers::identifier_range(source, self.locator)
})
})
.unwrap_or(binding.range),
);
if let Some(parent) = binding.source.as_ref() {
if matches!(parent.node, StmtKind::ImportFrom { .. })
@ -4904,7 +4913,17 @@ impl<'a> Checker<'a> {
name: (*name).to_string(),
line: binding.range.location.row(),
},
binding_range(rebound, self.locator),
matches!(
rebound.kind,
BindingKind::ClassDefinition
| BindingKind::FunctionDefinition
)
.then(|| {
rebound.source.as_ref().map_or(rebound.range, |source| {
helpers::identifier_range(source, self.locator)
})
})
.unwrap_or(rebound.range),
);
if let Some(parent) = &rebound.source {
if matches!(parent.node, StmtKind::ImportFrom { .. })

View file

@ -13,7 +13,6 @@ use rustpython_parser::{lexer, Mode, Tok};
use smallvec::{smallvec, SmallVec};
use crate::context::Context;
use crate::scope::{Binding, BindingKind};
use crate::source_code::{Generator, Indexer, Locator, Stylist};
use crate::types::{CallPath, Range};
use crate::visitor;
@ -978,21 +977,6 @@ pub fn identifier_range(stmt: &Stmt, locator: &Locator) -> Range {
Range::from(stmt)
}
/// Like `identifier_range`, but accepts a `Binding`.
pub fn binding_range(binding: &Binding, locator: &Locator) -> Range {
if matches!(
binding.kind,
BindingKind::ClassDefinition | BindingKind::FunctionDefinition
) {
binding
.source
.as_ref()
.map_or(binding.range, |source| identifier_range(source, locator))
} else {
binding.range
}
}
/// Return the ranges of [`Tok::Name`] tokens within a specified node.
pub fn find_names<'a, T>(
located: &'a Located<T>,