Skip to content

Commit

Permalink
Use import alias locations for pep8-naming import rules (#3772)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Mar 28, 2023
1 parent 81de3a1 commit e88fbae
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 98 deletions.
45 changes: 15 additions & 30 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -962,10 +962,7 @@ where
{
if let Some(diagnostic) =
pep8_naming::rules::constant_imported_as_non_constant(
stmt,
name,
asname,
self.locator,
name, asname, alias, stmt,
)
{
self.diagnostics.push(diagnostic);
Expand All @@ -979,10 +976,7 @@ where
{
if let Some(diagnostic) =
pep8_naming::rules::lowercase_imported_as_non_lowercase(
stmt,
name,
asname,
self.locator,
name, asname, alias, stmt,
)
{
self.diagnostics.push(diagnostic);
Expand All @@ -996,10 +990,7 @@ where
{
if let Some(diagnostic) =
pep8_naming::rules::camelcase_imported_as_lowercase(
stmt,
name,
asname,
self.locator,
name, asname, alias, stmt,
)
{
self.diagnostics.push(diagnostic);
Expand All @@ -1013,10 +1004,7 @@ where
{
if let Some(diagnostic) =
pep8_naming::rules::camelcase_imported_as_constant(
stmt,
name,
asname,
self.locator,
name, asname, alias, stmt,
)
{
self.diagnostics.push(diagnostic);
Expand All @@ -1030,10 +1018,7 @@ where
{
if let Some(diagnostic) =
pep8_naming::rules::camelcase_imported_as_acronym(
stmt,
name,
asname,
self.locator,
name, asname, alias, stmt,
)
{
self.diagnostics.push(diagnostic);
Expand Down Expand Up @@ -1344,10 +1329,10 @@ where
{
if let Some(diagnostic) =
pep8_naming::rules::constant_imported_as_non_constant(
stmt,
&alias.node.name,
asname,
self.locator,
alias,
stmt,
)
{
self.diagnostics.push(diagnostic);
Expand All @@ -1361,10 +1346,10 @@ where
{
if let Some(diagnostic) =
pep8_naming::rules::lowercase_imported_as_non_lowercase(
stmt,
&alias.node.name,
asname,
self.locator,
alias,
stmt,
)
{
self.diagnostics.push(diagnostic);
Expand All @@ -1378,10 +1363,10 @@ where
{
if let Some(diagnostic) =
pep8_naming::rules::camelcase_imported_as_lowercase(
stmt,
&alias.node.name,
asname,
self.locator,
alias,
stmt,
)
{
self.diagnostics.push(diagnostic);
Expand All @@ -1395,10 +1380,10 @@ where
{
if let Some(diagnostic) =
pep8_naming::rules::camelcase_imported_as_constant(
stmt,
&alias.node.name,
asname,
self.locator,
alias,
stmt,
)
{
self.diagnostics.push(diagnostic);
Expand All @@ -1412,10 +1397,10 @@ where
{
if let Some(diagnostic) =
pep8_naming::rules::camelcase_imported_as_acronym(
stmt,
&alias.node.name,
asname,
self.locator,
alias,
stmt,
)
{
self.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use rustpython_parser::ast::Stmt;
use rustpython_parser::ast::{Alias, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::identifier_range;
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::types::Range;
use ruff_python_stdlib::str::{self};

use crate::rules::pep8_naming::helpers;
Expand Down Expand Up @@ -50,23 +49,25 @@ impl Violation for CamelcaseImportedAsAcronym {

/// N817
pub fn camelcase_imported_as_acronym(
import_from: &Stmt,
name: &str,
asname: &str,
locator: &Locator,
alias: &Alias,
stmt: &Stmt,
) -> Option<Diagnostic> {
if helpers::is_camelcase(name)
&& !str::is_lower(asname)
&& str::is_upper(asname)
&& helpers::is_acronym(name, asname)
{
return Some(Diagnostic::new(
let mut diagnostic = Diagnostic::new(
CamelcaseImportedAsAcronym {
name: name.to_string(),
asname: asname.to_string(),
},
identifier_range(import_from, locator),
));
Range::from(alias),
);
diagnostic.set_parent(stmt.location);
return Some(diagnostic);
}
None
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use rustpython_parser::ast::Stmt;
use rustpython_parser::ast::{Alias, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::identifier_range;
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::types::Range;
use ruff_python_stdlib::str::{self};

use crate::rules::pep8_naming::helpers;
Expand Down Expand Up @@ -47,23 +46,25 @@ impl Violation for CamelcaseImportedAsConstant {

/// N814
pub fn camelcase_imported_as_constant(
import_from: &Stmt,
name: &str,
asname: &str,
locator: &Locator,
alias: &Alias,
stmt: &Stmt,
) -> Option<Diagnostic> {
if helpers::is_camelcase(name)
&& !str::is_lower(asname)
&& str::is_upper(asname)
&& !helpers::is_acronym(name, asname)
{
return Some(Diagnostic::new(
let mut diagnostic = Diagnostic::new(
CamelcaseImportedAsConstant {
name: name.to_string(),
asname: asname.to_string(),
},
identifier_range(import_from, locator),
));
Range::from(alias),
);
diagnostic.set_parent(stmt.location);
return Some(diagnostic);
}
None
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use rustpython_parser::ast::Stmt;
use rustpython_parser::ast::{Alias, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::identifier_range;
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::types::Range;
use ruff_python_stdlib::str;

use crate::rules::pep8_naming::helpers;
Expand Down Expand Up @@ -47,19 +46,21 @@ impl Violation for CamelcaseImportedAsLowercase {

/// N813
pub fn camelcase_imported_as_lowercase(
import_from: &Stmt,
name: &str,
asname: &str,
locator: &Locator,
alias: &Alias,
stmt: &Stmt,
) -> Option<Diagnostic> {
if helpers::is_camelcase(name) && str::is_lower(asname) {
return Some(Diagnostic::new(
let mut diagnostic = Diagnostic::new(
CamelcaseImportedAsLowercase {
name: name.to_string(),
asname: asname.to_string(),
},
identifier_range(import_from, locator),
));
Range::from(alias),
);
diagnostic.set_parent(stmt.location);
return Some(diagnostic);
}
None
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use rustpython_parser::ast::Stmt;
use rustpython_parser::ast::{Alias, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::identifier_range;
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::types::Range;
use ruff_python_stdlib::str;

/// ## What it does
Expand Down Expand Up @@ -46,19 +45,21 @@ impl Violation for ConstantImportedAsNonConstant {

/// N811
pub fn constant_imported_as_non_constant(
import_from: &Stmt,
name: &str,
asname: &str,
locator: &Locator,
alias: &Alias,
stmt: &Stmt,
) -> Option<Diagnostic> {
if str::is_upper(name) && !str::is_upper(asname) {
return Some(Diagnostic::new(
let mut diagnostic = Diagnostic::new(
ConstantImportedAsNonConstant {
name: name.to_string(),
asname: asname.to_string(),
},
identifier_range(import_from, locator),
));
Range::from(alias),
);
diagnostic.set_parent(stmt.location);
return Some(diagnostic);
}
None
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use rustpython_parser::ast::Stmt;
use rustpython_parser::ast::{Alias, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::identifier_range;
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::types::Range;
use ruff_python_stdlib::str;

/// ## What it does
Expand Down Expand Up @@ -45,19 +44,21 @@ impl Violation for LowercaseImportedAsNonLowercase {

/// N812
pub fn lowercase_imported_as_non_lowercase(
import_from: &Stmt,
name: &str,
asname: &str,
locator: &Locator,
alias: &Alias,
stmt: &Stmt,
) -> Option<Diagnostic> {
if !str::is_upper(name) && str::is_lower(name) && asname.to_lowercase() != asname {
return Some(Diagnostic::new(
let mut diagnostic = Diagnostic::new(
LowercaseImportedAsNonLowercase {
name: name.to_string(),
asname: asname.to_string(),
},
identifier_range(import_from, locator),
));
Range::from(alias),
);
diagnostic.set_parent(stmt.location);
return Some(diagnostic);
}
None
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,45 @@ expression: diagnostics
fixable: false
location:
row: 1
column: 0
column: 7
end_location:
row: 1
column: 25
fix:
edits: []
parent: ~
parent:
row: 1
column: 0
- kind:
name: ConstantImportedAsNonConstant
body: "Constant `CONSTANT` imported as non-constant `constant`"
suggestion: ~
fixable: false
location:
row: 2
column: 0
column: 16
end_location:
row: 2
column: 36
fix:
edits: []
parent: ~
parent:
row: 2
column: 0
- kind:
name: ConstantImportedAsNonConstant
body: "Constant `ANOTHER_CONSTANT` imported as non-constant `another_constant`"
suggestion: ~
fixable: false
location:
row: 3
column: 0
column: 16
end_location:
row: 3
column: 52
fix:
edits: []
parent: ~
parent:
row: 3
column: 0

Loading

0 comments on commit e88fbae

Please sign in to comment.