mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-19 01:51:30 +00:00
Move required import parsing out of lint rule (#12536)
## Summary Instead, make it part of the serialization and deserialization itself. This makes it _much_ easier to reuse when solving https://github.com/astral-sh/ruff/issues/12458.
This commit is contained in:
parent
7ad4df9e9f
commit
d930052de8
18 changed files with 330 additions and 276 deletions
|
@ -1,114 +0,0 @@
|
|||
/// A representation of an individual name imported via any import statement.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum AnyImport<'a> {
|
||||
Import(Import<'a>),
|
||||
ImportFrom(ImportFrom<'a>),
|
||||
}
|
||||
|
||||
/// A representation of an individual name imported via an `import` statement.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Import<'a> {
|
||||
pub name: Alias<'a>,
|
||||
}
|
||||
|
||||
/// A representation of an individual name imported via a `from ... import` statement.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ImportFrom<'a> {
|
||||
pub module: Option<&'a str>,
|
||||
pub name: Alias<'a>,
|
||||
pub level: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Alias<'a> {
|
||||
pub name: &'a str,
|
||||
pub as_name: Option<&'a str>,
|
||||
}
|
||||
|
||||
impl<'a> Import<'a> {
|
||||
/// Creates a new `Import` to import the specified module.
|
||||
pub fn module(name: &'a str) -> Self {
|
||||
Self {
|
||||
name: Alias {
|
||||
name,
|
||||
as_name: None,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ImportFrom<'a> {
|
||||
/// Creates a new `ImportFrom` to import a member from the specified module.
|
||||
pub fn member(module: &'a str, name: &'a str) -> Self {
|
||||
Self {
|
||||
module: Some(module),
|
||||
name: Alias {
|
||||
name,
|
||||
as_name: None,
|
||||
},
|
||||
level: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for AnyImport<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
AnyImport::Import(import) => write!(f, "{import}"),
|
||||
AnyImport::ImportFrom(import_from) => write!(f, "{import_from}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Import<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "import {}", self.name.name)?;
|
||||
if let Some(as_name) = self.name.as_name {
|
||||
write!(f, " as {as_name}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ImportFrom<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "from ")?;
|
||||
if self.level > 0 {
|
||||
write!(f, "{}", ".".repeat(self.level as usize))?;
|
||||
}
|
||||
if let Some(module) = self.module {
|
||||
write!(f, "{module}")?;
|
||||
}
|
||||
write!(f, " import {}", self.name.name)?;
|
||||
if let Some(as_name) = self.name.as_name {
|
||||
write!(f, " as {as_name}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FutureImport {
|
||||
/// Returns `true` if this import is from the `__future__` module.
|
||||
fn is_future_import(&self) -> bool;
|
||||
}
|
||||
|
||||
impl FutureImport for Import<'_> {
|
||||
fn is_future_import(&self) -> bool {
|
||||
self.name.name == "__future__"
|
||||
}
|
||||
}
|
||||
|
||||
impl FutureImport for ImportFrom<'_> {
|
||||
fn is_future_import(&self) -> bool {
|
||||
self.module == Some("__future__")
|
||||
}
|
||||
}
|
||||
|
||||
impl FutureImport for AnyImport<'_> {
|
||||
fn is_future_import(&self) -> bool {
|
||||
match self {
|
||||
AnyImport::Import(import) => import.is_future_import(),
|
||||
AnyImport::ImportFrom(import_from) => import_from.is_future_import(),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -12,7 +12,6 @@ mod expression;
|
|||
pub mod hashable;
|
||||
pub mod helpers;
|
||||
pub mod identifier;
|
||||
pub mod imports;
|
||||
mod int;
|
||||
pub mod name;
|
||||
mod node;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue