Skip to content

Commit

Permalink
fixup! Add rule WPS111 from wemake-python-styleguide
Browse files Browse the repository at this point in the history
  • Loading branch information
bo5o committed Jan 31, 2024
1 parent bbeab7c commit a092918
Showing 1 changed file with 38 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::Path;

use ruff_python_ast::{Alias, Expr, ExprAttribute, ExprName, Identifier, Parameter};
use ruff_python_ast::{Alias, Expr, ExprAttribute, Identifier, Parameter};
use ruff_text_size::{Ranged, TextRange};

use ruff_diagnostics::{Diagnostic, Violation};
Expand Down Expand Up @@ -54,71 +54,68 @@ impl Violation for TooShortName {
}
}

pub(crate) struct Checkable(Identifier);

impl From<&Identifier> for Checkable {
fn from(value: &Identifier) -> Self {
Self(value.clone())
}
pub(crate) enum Checkable<'a> {
Identifier(&'a Identifier),
Parameter(&'a Parameter),
Alias(&'a Alias),
Expr(&'a Expr),
}

impl From<&Parameter> for Checkable {
fn from(value: &Parameter) -> Self {
(&value.name).into()
impl<'a> From<&'a Identifier> for Checkable<'a> {
fn from(value: &'a Identifier) -> Self {
Checkable::Identifier(value)
}
}

impl TryFrom<&Alias> for Checkable {
type Error = ();

fn try_from(value: &Alias) -> Result<Self, Self::Error> {
match value {
Alias {
asname: Some(identifier),
..
} => Ok(identifier.into()),
_ => Err(()),
}
impl<'a> From<&'a Parameter> for Checkable<'a> {
fn from(value: &'a Parameter) -> Self {
Checkable::Parameter(value)
}
}

impl TryFrom<&Expr> for Checkable {
type Error = ();

fn try_from(value: &Expr) -> Result<Self, Self::Error> {
match value {
Expr::Name(ExprName { id, range, .. }) => Ok(Self(Identifier::new(id, *range))),
Expr::Attribute(ExprAttribute { attr, .. }) => Ok(attr.into()),
_ => Err(()),
}
impl<'a> From<&'a Alias> for Checkable<'a> {
fn from(value: &'a Alias) -> Self {
Checkable::Alias(value)
}
}

impl TryFrom<&Box<Expr>> for Checkable {
type Error = ();

fn try_from(value: &Box<Expr>) -> Result<Self, Self::Error> {
(&(**value)).try_into()
impl<'a> From<&'a Expr> for Checkable<'a> {
fn from(value: &'a Expr) -> Self {
Checkable::Expr(value)
}
}

/// WPS111
pub(crate) fn too_short_name(checker: &mut Checker, node: impl TryInto<Checkable>) {
if let Ok(Checkable(identifier)) = node.try_into() {
pub(crate) fn too_short_name<'a, 'b>(checker: &'a mut Checker, node: impl Into<Checkable<'b>>) {
if let Some((name, range)) = match node.into() {
Checkable::Identifier(identifier)
| Checkable::Parameter(Parameter {
name: identifier, ..
})
| Checkable::Alias(Alias {
asname: Some(identifier),
..
})
| Checkable::Expr(Expr::Attribute(ExprAttribute {
attr: identifier, ..
})) => Some((identifier.as_str(), identifier.range())),
Checkable::Expr(Expr::Name(name)) => Some((name.id.as_str(), name.range())),
_ => None,
} {
if naming::is_too_short_name(
identifier.as_str(),
name,
checker.settings.wemake_python_styleguide.min_name_length,
true,
) {
checker.diagnostics.push(Diagnostic::new(
TooShortName {
name: identifier.to_string(),
name: name.to_string(),
is_module: false,
},
identifier.range(),
range,
));
}
};
}
}

/// WPS111 (for filesystem)
Expand Down

0 comments on commit a092918

Please sign in to comment.