Implements [flake8-commas](https://github.com/PyCQA/flake8-commas). Fixes #1058. The plugin is mostly redundant with Black (and also deprecated upstream), but very useful for projects which can't/won't use an auto-formatter. This linter works on tokens. Before porting to Rust, I cleaned up the Python code ([link](https://gist.github.com/bluetech/7c5dcbdec4a73dd5a74d4bc09c72b8b9)) and made sure the tests pass. In the Rust version I tried to add explanatory comments, to the best of my understanding of the original logic. Some changes I did make: - Got rid of rule C814 - "missing trailing comma in Python 2". Ruff doesn't support Python 2. - Merged rules C815 - "missing trailing comma in Python 3.5+" and C816 - "missing trailing comma in Python 3.6+" into C812 - "missing trailing comma". These Python versions are outdated, didn't think it was worth the complication. - Added autofixes for C812 and C819. Autofix is missing for C818 - "trailing comma on bare tuple prohibited". It needs to turn e.g. `x = 1,` into `x = (1, )`, it's a bit difficult to do with tokens only, so I skipped it for now. I ran the rules on cpython/Lib and on a big internal code base and it works as intended (though I only sampled the diffs).
55 lines
1.5 KiB
Rust
55 lines
1.5 KiB
Rust
// Longer prefixes should come first so that you can find an origin for a code
|
|
// by simply picking the first entry that starts with the given prefix.
|
|
|
|
pub const PREFIX_TO_ORIGIN: &[(&str, &str)] = &[
|
|
("ANN", "Flake8Annotations"),
|
|
("ARG", "Flake8UnusedArguments"),
|
|
("A", "Flake8Builtins"),
|
|
("BLE", "Flake8BlindExcept"),
|
|
("B", "Flake8Bugbear"),
|
|
("C4", "Flake8Comprehensions"),
|
|
("C9", "McCabe"),
|
|
("COM", "Flake8Commas"),
|
|
("DTZ", "Flake8Datetimez"),
|
|
("D", "Pydocstyle"),
|
|
("ERA", "Eradicate"),
|
|
("EM", "Flake8ErrMsg"),
|
|
("E", "Pycodestyle"),
|
|
("FBT", "Flake8BooleanTrap"),
|
|
("F", "Pyflakes"),
|
|
("ICN", "Flake8ImportConventions"),
|
|
("ISC", "Flake8ImplicitStrConcat"),
|
|
("I", "Isort"),
|
|
("N", "PEP8Naming"),
|
|
("PD", "PandasVet"),
|
|
("PGH", "PygrepHooks"),
|
|
("PL", "Pylint"),
|
|
("PT", "Flake8PytestStyle"),
|
|
("Q", "Flake8Quotes"),
|
|
("RET", "Flake8Return"),
|
|
("SIM", "Flake8Simplify"),
|
|
("S", "Flake8Bandit"),
|
|
("T10", "Flake8Debugger"),
|
|
("T20", "Flake8Print"),
|
|
("TID", "Flake8TidyImports"),
|
|
("UP", "Pyupgrade"),
|
|
("W", "Pycodestyle"),
|
|
("YTT", "Flake82020"),
|
|
("PIE", "Flake8Pie"),
|
|
("RUF", "Ruff"),
|
|
];
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::PREFIX_TO_ORIGIN;
|
|
|
|
#[test]
|
|
fn order() {
|
|
for (idx, (prefix, _)) in PREFIX_TO_ORIGIN.iter().enumerate() {
|
|
for (prior_prefix, _) in PREFIX_TO_ORIGIN[..idx].iter() {
|
|
assert!(!prefix.starts_with(prior_prefix));
|
|
}
|
|
}
|
|
}
|
|
}
|