-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
384 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
_(f"{'value'}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
_("{}".format("line")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
_("%s" % "line") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//! Rules from [flake8-gettext](https://pypi.org/project/flake8-gettext/). | ||
pub(crate) mod rules; | ||
pub mod settings; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
use insta::assert_yaml_snapshot; | ||
use test_case::test_case; | ||
|
||
use crate::registry::Rule; | ||
use crate::settings; | ||
use crate::test::test_path; | ||
|
||
#[test_case(Rule::FStringInGetTextFuncCall,Path::new("INT001.py"); "INT001")] | ||
#[test_case(Rule::FormatInGetTextFuncCall, Path::new("INT002.py"); "INT002")] | ||
#[test_case(Rule::PrintfInGetTextFuncCall, Path::new("INT003.py"); "INT003")] | ||
fn rules(rule_code: Rule, path: &Path) -> Result<()> { | ||
let snapshot = format!("{}_{}", rule_code.as_ref(), path.to_string_lossy()); | ||
let diagnostics = test_path( | ||
Path::new("flake8_gettext").join(path).as_path(), | ||
&settings::Settings::for_rule(rule_code), | ||
)?; | ||
assert_yaml_snapshot!(snapshot, diagnostics); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use rustpython_parser::ast::{Constant, Expr, ExprKind, Operator}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::types::Range; | ||
|
||
#[violation] | ||
pub struct FStringInGetTextFuncCall; | ||
|
||
impl Violation for FStringInGetTextFuncCall { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("f-string is resolved before function call; consider `_(\"string %s\") % arg`") | ||
} | ||
} | ||
|
||
#[violation] | ||
pub struct FormatInGetTextFuncCall; | ||
|
||
impl Violation for FormatInGetTextFuncCall { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`format` method argument is resolved before function call; consider `_(\"string %s\") % arg`") | ||
} | ||
} | ||
#[violation] | ||
pub struct PrintfInGetTextFuncCall; | ||
|
||
impl Violation for PrintfInGetTextFuncCall { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("printf-style format is resolved before function call; consider `_(\"string %s\") % arg`") | ||
} | ||
} | ||
|
||
/// Returns true if the [`Expr`] is an internationalization function call. | ||
pub fn is_gettext_func_call(func: &Expr, functions_names: &[String]) -> bool { | ||
if let ExprKind::Name { id, .. } = &func.node { | ||
functions_names.contains(id) | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
/// INT001 | ||
pub fn f_string_in_gettext_func_call(args: &[Expr]) -> Option<Diagnostic> { | ||
if let Some(first) = args.first() { | ||
if matches!(first.node, ExprKind::JoinedStr { .. }) { | ||
return Some(Diagnostic::new( | ||
FStringInGetTextFuncCall {}, | ||
Range::from(first), | ||
)); | ||
} | ||
} | ||
None | ||
} | ||
|
||
/// INT002 | ||
pub fn format_in_gettext_func_call(args: &[Expr]) -> Option<Diagnostic> { | ||
if let Some(first) = args.first() { | ||
if let ExprKind::Call { func, .. } = &first.node { | ||
if let ExprKind::Attribute { attr, .. } = &func.node { | ||
if attr == "format" { | ||
return Some(Diagnostic::new( | ||
FormatInGetTextFuncCall {}, | ||
Range::from(first), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
None | ||
} | ||
|
||
/// INT003 | ||
pub fn printf_in_gettext_func_call(args: &[Expr]) -> Option<Diagnostic> { | ||
if let Some(first) = args.first() { | ||
if let ExprKind::BinOp { | ||
op: Operator::Mod { .. }, | ||
left, | ||
.. | ||
} = &first.node | ||
{ | ||
if let ExprKind::Constant { | ||
value: Constant::Str(_), | ||
.. | ||
} = left.node | ||
{ | ||
return Some(Diagnostic::new( | ||
PrintfInGetTextFuncCall {}, | ||
Range::from(first), | ||
)); | ||
} | ||
} | ||
} | ||
None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use schemars::JsonSchema; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use ruff_macros::{CacheKey, ConfigurationOptions}; | ||
|
||
#[derive( | ||
Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, JsonSchema, | ||
)] | ||
#[serde( | ||
deny_unknown_fields, | ||
rename_all = "kebab-case", | ||
rename = "Flake8GetTextOptions" | ||
)] | ||
pub struct Options { | ||
#[option( | ||
default = r#"["_", "gettext", "ngettext"]"#, | ||
value_type = "list[str]", | ||
example = r#"function-names = ["_", "gettext", "ngettext", "ugettetxt"]"# | ||
)] | ||
/// The function names to consider as internationalization calls. | ||
pub function_names: Option<Vec<String>>, | ||
#[option( | ||
default = r#"[]"#, | ||
value_type = "list[str]", | ||
example = r#"extend-function-names = ["ugettetxt"]"# | ||
)] | ||
#[serde(default)] | ||
/// Additional function names to consider as internationalization calls, in addition to those | ||
/// included in `function-names`. | ||
pub extend_function_names: Vec<String>, | ||
} | ||
|
||
#[derive(Debug, CacheKey)] | ||
pub struct Settings { | ||
pub functions_names: Vec<String>, | ||
} | ||
|
||
fn default_func_names() -> Vec<String> { | ||
["_", "gettext", "ngettext"] | ||
.iter() | ||
.map(std::string::ToString::to_string) | ||
.collect() | ||
} | ||
|
||
impl Default for Settings { | ||
fn default() -> Self { | ||
Self { | ||
functions_names: default_func_names(), | ||
} | ||
} | ||
} | ||
|
||
impl From<Options> for Settings { | ||
fn from(options: Options) -> Self { | ||
Self { | ||
functions_names: options | ||
.function_names | ||
.unwrap_or_else(default_func_names) | ||
.into_iter() | ||
.chain(options.extend_function_names) | ||
.collect(), | ||
} | ||
} | ||
} | ||
|
||
impl From<Settings> for Options { | ||
fn from(settings: Settings) -> Self { | ||
Self { | ||
function_names: Some(settings.functions_names), | ||
extend_function_names: vec![], | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...apshots/ruff__rules__flake8_gettext__tests__f-string-in-get-text-func-call_INT001.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_gettext/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
name: FStringInGetTextFuncCall | ||
body: "f-string is resolved before function call; consider `_(\"string %s\") % arg`" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 1 | ||
column: 2 | ||
end_location: | ||
row: 1 | ||
column: 14 | ||
fix: | ||
edits: [] | ||
parent: ~ | ||
|
19 changes: 19 additions & 0 deletions
19
...snapshots/ruff__rules__flake8_gettext__tests__format-in-get-text-func-call_INT002.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_gettext/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
name: FormatInGetTextFuncCall | ||
body: "`format` method argument is resolved before function call; consider `_(\"string %s\") % arg`" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 1 | ||
column: 2 | ||
end_location: | ||
row: 1 | ||
column: 21 | ||
fix: | ||
edits: [] | ||
parent: ~ | ||
|
19 changes: 19 additions & 0 deletions
19
...snapshots/ruff__rules__flake8_gettext__tests__printf-in-get-text-func-call_INT003.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_gettext/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
name: PrintfInGetTextFuncCall | ||
body: "printf-style format is resolved before function call; consider `_(\"string %s\") % arg`" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 1 | ||
column: 2 | ||
end_location: | ||
row: 1 | ||
column: 15 | ||
fix: | ||
edits: [] | ||
parent: ~ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.