Compare commits

..

11 Commits

Author SHA1 Message Date
Charlie Marsh
34c91224d7 Bump version to 0.0.100 2022-11-04 12:10:17 -04:00
Charlie Marsh
50c4bbc52b Implement ambiguous unicode character detection (#578) 2022-11-04 12:08:26 -04:00
Charlie Marsh
f0239de559 Use a shared Rope between AST checker and fixer (#585) 2022-11-04 12:05:46 -04:00
Charlie Marsh
2be632f3cc Use a Rope to power fixer (#584) 2022-11-04 12:04:14 -04:00
Harutaka Kawamura
ec3fed5a61 Implement B018 (#582) 2022-11-04 10:53:30 -04:00
Charlie Marsh
1da44485d5 Confine subscript annotation checks to ExprContext::Load (#583) 2022-11-04 10:51:05 -04:00
Charlie Marsh
e5f30ff5a8 Use a rope to manage string slicing (#576) 2022-11-03 23:23:38 -04:00
Charlie Marsh
c92f5c14a3 Bump Rust version to 1.65.0 (#575) 2022-11-03 22:49:21 -04:00
Charlie Marsh
2f92399f4e Add W to list of default flake8-to-ruff codes (#574) 2022-11-03 22:13:00 -04:00
Charlie Marsh
83dfb5fe8b Infer Flake8 plugins from .flake8 config (#573) 2022-11-03 22:05:46 -04:00
Charlie Marsh
cc915371ca Implement autofix for F901 (#571) 2022-11-03 11:58:54 -04:00
51 changed files with 3166 additions and 389 deletions

View File

@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.99
rev: v0.0.100
hooks:
- id: ruff

21
Cargo.lock generated
View File

@@ -920,7 +920,7 @@ checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "flake8-to-ruff"
version = "0.0.99-dev.0"
version = "0.0.100-dev.0"
dependencies = [
"anyhow",
"clap 4.0.15",
@@ -2209,9 +2209,19 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "ropey"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd22239fafefc42138ca5da064f3c17726a80d2379d817a3521240e78dd0064"
dependencies = [
"smallvec",
"str_indices",
]
[[package]]
name = "ruff"
version = "0.0.99"
version = "0.0.100"
dependencies = [
"anyhow",
"assert_cmd",
@@ -2239,6 +2249,7 @@ dependencies = [
"path-absolutize",
"rayon",
"regex",
"ropey",
"rustpython-ast",
"rustpython-common",
"rustpython-parser",
@@ -2559,6 +2570,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "str_indices"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d9199fa80c817e074620be84374a520062ebac833f358d74b37060ce4a0f2c0"
[[package]]
name = "string_cache"
version = "0.8.4"

View File

@@ -1,11 +1,11 @@
[workspace]
members = [
"flake8_to_ruff",
"flake8_to_ruff",
]
[package]
name = "ruff"
version = "0.0.99"
version = "0.0.100"
edition = "2021"
[lib]
@@ -31,6 +31,7 @@ once_cell = { version = "1.13.1" }
path-absolutize = { version = "3.0.14", features = ["once_cell_cache", "use_unix_paths_on_wasm"] }
rayon = { version = "1.5.3" }
regex = { version = "1.6.0" }
ropey = { version = "1.5.0" }
rustpython-ast = { features = ["unparse"], git = "https://github.com/RustPython/RustPython.git", rev = "77b821a1941019fe34f73ce17cea013ae1b98fd0" }
rustpython-common = { git = "https://github.com/RustPython/RustPython.git", rev = "77b821a1941019fe34f73ce17cea013ae1b98fd0" }
rustpython-parser = { features = ["lalrpop"], git = "https://github.com/RustPython/RustPython.git", rev = "77b821a1941019fe34f73ce17cea013ae1b98fd0" }

View File

@@ -49,7 +49,8 @@ Read the [launch blog post](https://notes.crmarsh.com/python-tooling-could-be-mu
9. [flake8-builtins](#flake8-builtins)
10. [flake8-print](#flake8-print)
11. [flake8-quotes](#flake8-quotes)
12. [Meta rules](#meta-rules)
12. [Ruff-specific rules](#ruff-specific-rules)
13. [Meta rules](#meta-rules)
5. [Editor Integrations](#editor-integrations)
6. [FAQ](#faq)
7. [Development](#development)
@@ -89,7 +90,7 @@ Ruff also works with [pre-commit](https://pre-commit.com):
```yaml
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.99
rev: v0.0.100
hooks:
- id: ruff
```
@@ -325,7 +326,7 @@ The 🛠 emoji indicates that a rule is automatically fixable by the `--fix` com
| F823 | UndefinedLocal | Local variable `...` referenced before assignment | |
| F831 | DuplicateArgumentName | Duplicate argument name in function definition | |
| F841 | UnusedVariable | Local variable `...` is assigned to but never used | |
| F901 | RaiseNotImplemented | `raise NotImplemented` should be `raise NotImplementedError` | |
| F901 | RaiseNotImplemented | `raise NotImplemented` should be `raise NotImplementedError` | 🛠 |
### pycodestyle (error)
@@ -467,6 +468,7 @@ The 🛠 emoji indicates that a rule is automatically fixable by the `--fix` com
| B013 | RedundantTupleInExceptionHandler | A length-one tuple literal is redundant. Write `except ValueError:` instead of `except (ValueError,):`. | |
| B014 | DuplicateHandlerException | Exception handler with duplicate exception: `ValueError` | 🛠 |
| B017 | NoAssertRaisesException | `assertRaises(Exception):` should be considered evil. | |
| B018 | UselessExpression | Found useless expression. Either assign it to a variable or remove it. | |
| B025 | DuplicateTryBlockException | try-except block with duplicate exception `Exception` | |
### flake8-builtins
@@ -493,6 +495,12 @@ The 🛠 emoji indicates that a rule is automatically fixable by the `--fix` com
| Q002 | BadQuotesDocstring | Single quote docstring found but double quotes preferred | |
| Q003 | AvoidQuoteEscape | Change outer quotes to avoid escaping inner quotes | |
### Ruff-specific rules
| Code | Name | Message | Fix |
| ---- | ---- | ------- | --- |
| X001 | AmbiguousUnicodeCharacter | Use of ambiguous unicode character '𝐁' (did you mean 'B'?) | 🛠 |
### Meta rules
| Code | Name | Message | Fix |
@@ -684,7 +692,7 @@ extend-ignore = [
## Development
Ruff is written in Rust (1.64.0). You'll need to install the [Rust toolchain](https://www.rust-lang.org/tools/install)
Ruff is written in Rust (1.65.0). You'll need to install the [Rust toolchain](https://www.rust-lang.org/tools/install)
for development.
Assuming you have `cargo` installed, you can run:

View File

@@ -1,13 +1,16 @@
use std::path::Path;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use ropey::Rope;
use ruff::fs;
use ruff::source_code_locator::compute_offsets;
fn criterion_benchmark(c: &mut Criterion) {
let contents = fs::read_file(Path::new("resources/test/fixtures/D.py")).unwrap();
c.bench_function("compute_offsets", |b| {
b.iter(|| compute_offsets(black_box(&contents)))
c.bench_function("rope", |b| {
b.iter(|| {
let rope = Rope::from_str(black_box(&contents));
rope.line_to_char(black_box(4));
})
});
}

View File

@@ -31,6 +31,8 @@ fn main() {
.derive("Debug")
.derive("PartialEq")
.derive("Eq")
.derive("PartialOrd")
.derive("Ord")
.derive("Clone")
.derive("Serialize")
.derive("Deserialize");

View File

@@ -771,7 +771,7 @@ checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "flake8_to_ruff"
version = "0.0.99"
version = "0.0.100"
dependencies = [
"anyhow",
"clap",
@@ -1975,7 +1975,7 @@ dependencies = [
[[package]]
name = "ruff"
version = "0.0.99"
version = "0.0.100"
dependencies = [
"anyhow",
"bincode",

View File

@@ -1,6 +1,6 @@
[package]
name = "flake8-to-ruff"
version = "0.0.99-dev.0"
version = "0.0.100-dev.0"
edition = "2021"
[lib]

View File

@@ -17,7 +17,7 @@ pip install flake8-to-ruff
### Usage
To run Ruff, try any of the following:
To run `flake8-to-ruff`:
```shell
flake8-to-ruff path/to/setup.cfg
@@ -25,6 +25,60 @@ flake8-to-ruff path/to/tox.ini
flake8-to-ruff path/to/.flake8
```
`flake8-to-ruff` will print the relevant `pyproject.toml` sections to standard output, like so:
```toml
[tool.ruff]
exclude = [
'.svn',
'CVS',
'.bzr',
'.hg',
'.git',
'__pycache__',
'.tox',
'.idea',
'.mypy_cache',
'.venv',
'node_modules',
'_state_machine.py',
'test_fstring.py',
'bad_coding2.py',
'badsyntax_*.py',
]
select = [
'A',
'E',
'F',
'Q',
]
ignore = []
[tool.ruff.flake8-quotes]
inline-quotes = 'single'
[tool.ruff.pep8-naming]
ignore-names = [
'foo',
'bar',
]
```
### Plugins
`flake8-to-ruff` will attempt to infer any activated plugins based on the settings provided in your
configuration file.
For example, if your `.flake8` file includes a `docstring-convention` property, `flake8-to-ruff`
will enable the appropriate [`flake8-docstrings`](https://pypi.org/project/flake8-docstrings/)
checks.
Alternatively, you can manually specify plugins on the command-line:
```shell
flake8-to-ruff path/to/.flake8 --plugin flake8-builtins --plugin flake8-quotes
```
## Limitations
1. Ruff only supports a subset of the Flake configuration options. `flake8-to-ruff` will warn on and
@@ -33,9 +87,6 @@ flake8-to-ruff path/to/.flake8
2. Ruff will omit any error codes that are unimplemented or unsupported by Ruff, including error
codes from unsupported plugins. (See the [Ruff README](https://github.com/charliermarsh/ruff#user-content-how-does-ruff-compare-to-flake8)
for the complete list of supported plugins.)
3. `flake8-to-ruff` does not auto-detect your Flake8 plugins, so any reliance on Flake8 plugins that
implicitly enable third-party checks will be ignored. Instead, add those error codes to your
`select` or `extend-select` fields, so that `flake8-to-ruff` can pick them up.
## License

View File

@@ -0,0 +1,36 @@
[flake8]
min_python_version = 3.7.0
max-line-length = 88
ban-relative-imports = true
# flake8-use-fstring: https://github.com/MichaelKim0407/flake8-use-fstring#--percent-greedy-and---format-greedy
format-greedy = 1
inline-quotes = double
enable-extensions = TC, TC1
type-checking-strict = true
eradicate-whitelist-extend = ^-.*;
extend-ignore =
# E203: Whitespace before ':' (pycqa/pycodestyle#373)
E203,
# SIM106: Handle error-cases first
SIM106,
# ANN101: Missing type annotation for self in method
ANN101,
# ANN102: Missing type annotation for cls in classmethod
ANN102,
# PIE781: assign-and-return
PIE781,
# PIE798 no-unnecessary-class: Consider using a module for namespacing instead
PIE798,
per-file-ignores =
# TC002: Move third-party import '...' into a type-checking block
__init__.py:TC002,
# ANN201: Missing return type annotation for public function
tests/test_*:ANN201
tests/**/test_*:ANN201
extend-exclude =
# Frozen and not subject to change in this repo:
get-poetry.py,
install-poetry.py,
# External to the project's coding standards:
tests/fixtures/*,
tests/**/fixtures/*,

View File

@@ -0,0 +1,19 @@
[flake8]
max-line-length=120
docstring-convention=all
import-order-style=pycharm
application_import_names=bot,tests
exclude=.cache,.venv,.git,constants.py
extend-ignore=
B311,W503,E226,S311,T000,E731
# Missing Docstrings
D100,D104,D105,D107,
# Docstring Whitespace
D203,D212,D214,D215,
# Docstring Quotes
D301,D302,
# Docstring Content
D400,D401,D402,D404,D405,D406,D407,D408,D409,D410,D411,D412,D413,D414,D416,D417
# Type Annotations
ANN002,ANN003,ANN101,ANN102,ANN204,ANN206,ANN401
per-file-ignores=tests/*:D,ANN

View File

@@ -0,0 +1,6 @@
[flake8]
ignore = E203, E501, W503
per-file-ignores =
requests/__init__.py:E402, F401
requests/compat.py:E402, F401
tests/compat.py:F401

View File

@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{BTreeSet, HashMap};
use anyhow::Result;
use ruff::flake8_quotes::settings::Quote;
@@ -6,13 +6,36 @@ use ruff::settings::options::Options;
use ruff::settings::pyproject::Pyproject;
use ruff::{flake8_quotes, pep8_naming};
use crate::parser;
use crate::plugin::Plugin;
use crate::{parser, plugin};
pub fn convert(config: HashMap<String, HashMap<String, Option<String>>>) -> Result<Pyproject> {
// Extract the Flake8 section.
let flake8 = config
.get("flake8")
.expect("Unable to find flake8 section in INI file.");
pub fn convert(
flake8: &HashMap<String, Option<String>>,
plugins: Option<Vec<Plugin>>,
) -> Result<Pyproject> {
// Check if the user has specified a `select`. If not, we'll add our own
// default `select`, and populate it based on user plugins.
let mut select = flake8
.get("select")
.and_then(|value| {
value
.as_ref()
.map(|value| BTreeSet::from_iter(parser::parse_prefix_codes(value)))
})
.unwrap_or_else(|| {
plugin::resolve_select(
flake8,
&plugins.unwrap_or_else(|| plugin::infer_plugins(flake8)),
)
});
let mut ignore = flake8
.get("ignore")
.and_then(|value| {
value
.as_ref()
.map(|value| BTreeSet::from_iter(parser::parse_prefix_codes(value)))
})
.unwrap_or_default();
// Parse each supported option.
let mut options: Options = Default::default();
@@ -27,16 +50,18 @@ pub fn convert(config: HashMap<String, HashMap<String, Option<String>>>) -> Resu
Err(e) => eprintln!("Unable to parse '{key}' property: {e}"),
},
"select" => {
options.select = Some(parser::parse_prefix_codes(value.as_ref()));
}
"extend-select" | "extend_select" => {
options.extend_select = Some(parser::parse_prefix_codes(value.as_ref()));
// No-op (handled above).
}
"ignore" => {
options.ignore = Some(parser::parse_prefix_codes(value.as_ref()));
// No-op (handled above).
}
"extend-select" | "extend_select" => {
// Unlike Flake8, use a single explicit `select`.
select.extend(parser::parse_prefix_codes(value.as_ref()));
}
"extend-ignore" | "extend_ignore" => {
options.extend_ignore = Some(parser::parse_prefix_codes(value.as_ref()));
// Unlike Flake8, use a single explicit `ignore`.
ignore.extend(parser::parse_prefix_codes(value.as_ref()));
}
"exclude" => {
options.exclude = Some(parser::parse_strings(value.as_ref()));
@@ -86,12 +111,19 @@ pub fn convert(config: HashMap<String, HashMap<String, Option<String>>>) -> Resu
pep8_naming.staticmethod_decorators =
Some(parser::parse_strings(value.as_ref()));
}
// flake8-docstrings
"docstring-convention" => {
// No-op (handled above).
}
// Unknown
_ => eprintln!("Skipping unsupported property: {key}"),
}
}
}
// Deduplicate and sort.
options.select = Some(Vec::from_iter(select));
options.ignore = Some(Vec::from_iter(ignore));
if flake8_quotes != Default::default() {
options.flake8_quotes = Some(flake8_quotes);
}
@@ -108,22 +140,28 @@ mod tests {
use std::collections::HashMap;
use anyhow::Result;
use ruff::checks_gen::CheckCodePrefix;
use ruff::flake8_quotes;
use ruff::settings::options::Options;
use ruff::settings::pyproject::Pyproject;
use crate::converter::convert;
use crate::plugin::Plugin;
#[test]
fn it_converts_empty() -> Result<()> {
let actual = convert(HashMap::from([("flake8".to_string(), HashMap::from([]))]))?;
let actual = convert(&HashMap::from([]), None)?;
let expected = Pyproject::new(Options {
line_length: None,
exclude: None,
extend_exclude: None,
select: None,
select: Some(vec![
CheckCodePrefix::E,
CheckCodePrefix::F,
CheckCodePrefix::W,
]),
extend_select: None,
ignore: None,
ignore: Some(vec![]),
extend_ignore: None,
per_file_ignores: None,
dummy_variable_rgx: None,
@@ -138,17 +176,21 @@ mod tests {
#[test]
fn it_converts_dashes() -> Result<()> {
let actual = convert(HashMap::from([(
"flake8".to_string(),
HashMap::from([("max-line-length".to_string(), Some("100".to_string()))]),
)]))?;
let actual = convert(
&HashMap::from([("max-line-length".to_string(), Some("100".to_string()))]),
Some(vec![]),
)?;
let expected = Pyproject::new(Options {
line_length: Some(100),
exclude: None,
extend_exclude: None,
select: None,
select: Some(vec![
CheckCodePrefix::E,
CheckCodePrefix::F,
CheckCodePrefix::W,
]),
extend_select: None,
ignore: None,
ignore: Some(vec![]),
extend_ignore: None,
per_file_ignores: None,
dummy_variable_rgx: None,
@@ -163,17 +205,21 @@ mod tests {
#[test]
fn it_converts_underscores() -> Result<()> {
let actual = convert(HashMap::from([(
"flake8".to_string(),
HashMap::from([("max_line_length".to_string(), Some("100".to_string()))]),
)]))?;
let actual = convert(
&HashMap::from([("max_line_length".to_string(), Some("100".to_string()))]),
Some(vec![]),
)?;
let expected = Pyproject::new(Options {
line_length: Some(100),
exclude: None,
extend_exclude: None,
select: None,
select: Some(vec![
CheckCodePrefix::E,
CheckCodePrefix::F,
CheckCodePrefix::W,
]),
extend_select: None,
ignore: None,
ignore: Some(vec![]),
extend_ignore: None,
per_file_ignores: None,
dummy_variable_rgx: None,
@@ -188,17 +234,21 @@ mod tests {
#[test]
fn it_ignores_parse_errors() -> Result<()> {
let actual = convert(HashMap::from([(
"flake8".to_string(),
HashMap::from([("max_line_length".to_string(), Some("abc".to_string()))]),
)]))?;
let actual = convert(
&HashMap::from([("max_line_length".to_string(), Some("abc".to_string()))]),
Some(vec![]),
)?;
let expected = Pyproject::new(Options {
line_length: None,
exclude: None,
extend_exclude: None,
select: None,
select: Some(vec![
CheckCodePrefix::E,
CheckCodePrefix::F,
CheckCodePrefix::W,
]),
extend_select: None,
ignore: None,
ignore: Some(vec![]),
extend_ignore: None,
per_file_ignores: None,
dummy_variable_rgx: None,
@@ -212,18 +262,124 @@ mod tests {
}
#[test]
fn it_converts_extensions() -> Result<()> {
let actual = convert(HashMap::from([(
"flake8".to_string(),
HashMap::from([("inline-quotes".to_string(), Some("single".to_string()))]),
)]))?;
fn it_converts_plugin_options() -> Result<()> {
let actual = convert(
&HashMap::from([("inline-quotes".to_string(), Some("single".to_string()))]),
Some(vec![]),
)?;
let expected = Pyproject::new(Options {
line_length: None,
exclude: None,
extend_exclude: None,
select: None,
select: Some(vec![
CheckCodePrefix::E,
CheckCodePrefix::F,
CheckCodePrefix::W,
]),
extend_select: None,
ignore: None,
ignore: Some(vec![]),
extend_ignore: None,
per_file_ignores: None,
dummy_variable_rgx: None,
target_version: None,
flake8_quotes: Some(flake8_quotes::settings::Options {
inline_quotes: Some(flake8_quotes::settings::Quote::Single),
multiline_quotes: None,
docstring_quotes: None,
avoid_escape: None,
}),
pep8_naming: None,
});
assert_eq!(actual, expected);
Ok(())
}
#[test]
fn it_converts_docstring_conventions() -> Result<()> {
let actual = convert(
&HashMap::from([(
"docstring-convention".to_string(),
Some("numpy".to_string()),
)]),
Some(vec![Plugin::Flake8Docstrings]),
)?;
let expected = Pyproject::new(Options {
line_length: None,
exclude: None,
extend_exclude: None,
select: Some(vec![
CheckCodePrefix::D100,
CheckCodePrefix::D101,
CheckCodePrefix::D102,
CheckCodePrefix::D103,
CheckCodePrefix::D104,
CheckCodePrefix::D105,
CheckCodePrefix::D106,
CheckCodePrefix::D200,
CheckCodePrefix::D201,
CheckCodePrefix::D202,
CheckCodePrefix::D204,
CheckCodePrefix::D205,
CheckCodePrefix::D206,
CheckCodePrefix::D207,
CheckCodePrefix::D208,
CheckCodePrefix::D209,
CheckCodePrefix::D210,
CheckCodePrefix::D211,
CheckCodePrefix::D214,
CheckCodePrefix::D215,
CheckCodePrefix::D300,
CheckCodePrefix::D400,
CheckCodePrefix::D403,
CheckCodePrefix::D404,
CheckCodePrefix::D405,
CheckCodePrefix::D406,
CheckCodePrefix::D407,
CheckCodePrefix::D408,
CheckCodePrefix::D409,
CheckCodePrefix::D410,
CheckCodePrefix::D411,
CheckCodePrefix::D412,
CheckCodePrefix::D414,
CheckCodePrefix::D418,
CheckCodePrefix::D419,
CheckCodePrefix::E,
CheckCodePrefix::F,
CheckCodePrefix::W,
]),
extend_select: None,
ignore: Some(vec![]),
extend_ignore: None,
per_file_ignores: None,
dummy_variable_rgx: None,
target_version: None,
flake8_quotes: None,
pep8_naming: None,
});
assert_eq!(actual, expected);
Ok(())
}
#[test]
fn it_infers_plugins_if_omitted() -> Result<()> {
let actual = convert(
&HashMap::from([("inline-quotes".to_string(), Some("single".to_string()))]),
None,
)?;
let expected = Pyproject::new(Options {
line_length: None,
exclude: None,
extend_exclude: None,
select: Some(vec![
CheckCodePrefix::E,
CheckCodePrefix::F,
CheckCodePrefix::Q,
CheckCodePrefix::W,
]),
extend_select: None,
ignore: Some(vec![]),
extend_ignore: None,
per_file_ignores: None,
dummy_variable_rgx: None,

View File

@@ -2,3 +2,4 @@
pub mod converter;
mod parser;
pub mod plugin;

View File

@@ -6,6 +6,7 @@ use anyhow::Result;
use clap::Parser;
use configparser::ini::Ini;
use flake8_to_ruff::converter;
use flake8_to_ruff::plugin::Plugin;
#[derive(Parser)]
#[command(
@@ -17,6 +18,9 @@ struct Cli {
/// '.flake8').
#[arg(required = true)]
file: PathBuf,
/// List of plugins to enable.
#[arg(long, value_delimiter = ',')]
plugin: Option<Vec<Plugin>>,
}
fn main() -> Result<()> {
@@ -27,8 +31,13 @@ fn main() -> Result<()> {
ini.set_multiline(true);
let config = ini.load(cli.file).map_err(|msg| anyhow::anyhow!(msg))?;
// Extract the Flake8 section.
let flake8 = config
.get("flake8")
.expect("Unable to find flake8 section in INI file.");
// Create the pyproject.toml.
let pyproject = converter::convert(config)?;
let pyproject = converter::convert(flake8, cli.plugin)?;
println!("{}", toml::to_string_pretty(&pyproject)?);
Ok(())

View File

@@ -0,0 +1,321 @@
use std::collections::{BTreeSet, HashMap};
use std::str::FromStr;
use anyhow::anyhow;
use ruff::checks_gen::CheckCodePrefix;
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub enum Plugin {
Flake8Bugbear,
Flake8Builtins,
Flake8Comprehensions,
Flake8Docstrings,
Flake8Print,
Flake8Quotes,
PEP8Naming,
Pyupgrade,
}
impl FromStr for Plugin {
type Err = anyhow::Error;
fn from_str(string: &str) -> Result<Self, Self::Err> {
match string {
"flake8-bugbear" => Ok(Plugin::Flake8Bugbear),
"flake8-builtins" => Ok(Plugin::Flake8Builtins),
"flake8-comprehensions" => Ok(Plugin::Flake8Comprehensions),
"flake8-docstrings" => Ok(Plugin::Flake8Docstrings),
"flake8-print" => Ok(Plugin::Flake8Print),
"flake8-quotes" => Ok(Plugin::Flake8Quotes),
"pep8-naming" => Ok(Plugin::PEP8Naming),
"pyupgrade" => Ok(Plugin::Pyupgrade),
_ => Err(anyhow!("Unknown plugin: {}", string)),
}
}
}
impl Plugin {
pub fn select(&self, flake8: &HashMap<String, Option<String>>) -> Vec<CheckCodePrefix> {
match self {
Plugin::Flake8Bugbear => vec![CheckCodePrefix::B],
Plugin::Flake8Builtins => vec![CheckCodePrefix::A],
Plugin::Flake8Comprehensions => vec![CheckCodePrefix::C],
Plugin::Flake8Docstrings => {
// Use the user-provided docstring.
for key in ["docstring-convention", "docstring_convention"] {
if let Some(Some(value)) = flake8.get(key) {
match DocstringConvention::from_str(value) {
Ok(convention) => return convention.select(),
Err(e) => {
eprintln!("Unexpected '{key}' value: {value} ({e}");
return vec![];
}
}
}
}
// Default to PEP8.
DocstringConvention::PEP8.select()
}
Plugin::Flake8Print => vec![CheckCodePrefix::T],
Plugin::Flake8Quotes => vec![CheckCodePrefix::Q],
Plugin::PEP8Naming => vec![CheckCodePrefix::N],
Plugin::Pyupgrade => vec![CheckCodePrefix::U],
}
}
}
pub enum DocstringConvention {
All,
PEP8,
NumPy,
Google,
}
impl FromStr for DocstringConvention {
type Err = anyhow::Error;
fn from_str(string: &str) -> Result<Self, Self::Err> {
match string {
"all" => Ok(DocstringConvention::All),
"pep8" => Ok(DocstringConvention::PEP8),
"numpy" => Ok(DocstringConvention::NumPy),
"google" => Ok(DocstringConvention::Google),
_ => Err(anyhow!("Unknown docstring convention: {}", string)),
}
}
}
impl DocstringConvention {
fn select(&self) -> Vec<CheckCodePrefix> {
match self {
DocstringConvention::All => vec![CheckCodePrefix::D],
DocstringConvention::PEP8 => vec![
// All errors except D203, D212, D213, D214, D215, D404, D405, D406, D407, D408,
// D409, D410, D411, D413, D415, D416 and D417.
CheckCodePrefix::D100,
CheckCodePrefix::D101,
CheckCodePrefix::D102,
CheckCodePrefix::D103,
CheckCodePrefix::D104,
CheckCodePrefix::D105,
CheckCodePrefix::D106,
CheckCodePrefix::D107,
CheckCodePrefix::D200,
CheckCodePrefix::D201,
CheckCodePrefix::D202,
// CheckCodePrefix::D203,
CheckCodePrefix::D204,
CheckCodePrefix::D205,
CheckCodePrefix::D206,
CheckCodePrefix::D207,
CheckCodePrefix::D208,
CheckCodePrefix::D209,
CheckCodePrefix::D210,
CheckCodePrefix::D211,
// CheckCodePrefix::D212,
// CheckCodePrefix::D213,
// CheckCodePrefix::D214,
// CheckCodePrefix::D215,
CheckCodePrefix::D300,
CheckCodePrefix::D400,
CheckCodePrefix::D402,
CheckCodePrefix::D403,
// CheckCodePrefix::D404,
// CheckCodePrefix::D405,
// CheckCodePrefix::D406,
// CheckCodePrefix::D407,
// CheckCodePrefix::D408,
// CheckCodePrefix::D409,
// CheckCodePrefix::D410,
// CheckCodePrefix::D411,
CheckCodePrefix::D412,
// CheckCodePrefix::D413,
CheckCodePrefix::D414,
// CheckCodePrefix::D415,
// CheckCodePrefix::D416,
// CheckCodePrefix::D417,
CheckCodePrefix::D418,
CheckCodePrefix::D419,
],
DocstringConvention::NumPy => vec![
// All errors except D107, D203, D212, D213, D402, D413, D415, D416, and D417.
CheckCodePrefix::D100,
CheckCodePrefix::D101,
CheckCodePrefix::D102,
CheckCodePrefix::D103,
CheckCodePrefix::D104,
CheckCodePrefix::D105,
CheckCodePrefix::D106,
// CheckCodePrefix::D107,
CheckCodePrefix::D200,
CheckCodePrefix::D201,
CheckCodePrefix::D202,
// CheckCodePrefix::D203,
CheckCodePrefix::D204,
CheckCodePrefix::D205,
CheckCodePrefix::D206,
CheckCodePrefix::D207,
CheckCodePrefix::D208,
CheckCodePrefix::D209,
CheckCodePrefix::D210,
CheckCodePrefix::D211,
// CheckCodePrefix::D212,
// CheckCodePrefix::D213,
CheckCodePrefix::D214,
CheckCodePrefix::D215,
CheckCodePrefix::D300,
CheckCodePrefix::D400,
// CheckCodePrefix::D402,
CheckCodePrefix::D403,
CheckCodePrefix::D404,
CheckCodePrefix::D405,
CheckCodePrefix::D406,
CheckCodePrefix::D407,
CheckCodePrefix::D408,
CheckCodePrefix::D409,
CheckCodePrefix::D410,
CheckCodePrefix::D411,
CheckCodePrefix::D412,
// CheckCodePrefix::D413,
CheckCodePrefix::D414,
// CheckCodePrefix::D415,
// CheckCodePrefix::D416,
// CheckCodePrefix::D417,
CheckCodePrefix::D418,
CheckCodePrefix::D419,
],
DocstringConvention::Google => vec![
// All errors except D203, D204, D213, D215, D400, D401, D404, D406, D407, D408,
// D409 and D413.
CheckCodePrefix::D100,
CheckCodePrefix::D101,
CheckCodePrefix::D102,
CheckCodePrefix::D103,
CheckCodePrefix::D104,
CheckCodePrefix::D105,
CheckCodePrefix::D106,
CheckCodePrefix::D107,
CheckCodePrefix::D200,
CheckCodePrefix::D201,
CheckCodePrefix::D202,
// CheckCodePrefix::D203,
// CheckCodePrefix::D204,
CheckCodePrefix::D205,
CheckCodePrefix::D206,
CheckCodePrefix::D207,
CheckCodePrefix::D208,
CheckCodePrefix::D209,
CheckCodePrefix::D210,
CheckCodePrefix::D211,
CheckCodePrefix::D212,
// CheckCodePrefix::D213,
CheckCodePrefix::D214,
// CheckCodePrefix::D215,
CheckCodePrefix::D300,
// CheckCodePrefix::D400,
CheckCodePrefix::D402,
CheckCodePrefix::D403,
// CheckCodePrefix::D404,
CheckCodePrefix::D405,
// CheckCodePrefix::D406,
// CheckCodePrefix::D407,
// CheckCodePrefix::D408,
// CheckCodePrefix::D409,
CheckCodePrefix::D410,
CheckCodePrefix::D411,
CheckCodePrefix::D412,
// CheckCodePrefix::D413,
CheckCodePrefix::D414,
CheckCodePrefix::D415,
CheckCodePrefix::D416,
CheckCodePrefix::D417,
CheckCodePrefix::D418,
CheckCodePrefix::D419,
],
}
}
}
/// Infer the enabled plugins based on user-provided settings.
pub fn infer_plugins(flake8: &HashMap<String, Option<String>>) -> Vec<Plugin> {
let mut plugins = BTreeSet::new();
for key in flake8.keys() {
match key.as_str() {
// flake8-docstrings
"docstring-convention" | "docstring_convention" => {
plugins.insert(Plugin::Flake8Docstrings);
}
// flake8-builtins
"builtins-ignorelist" | "builtins_ignorelist" => {
plugins.insert(Plugin::Flake8Builtins);
}
// flake8-quotes
"quotes" | "inline-quotes" | "inline_quotes" => {
plugins.insert(Plugin::Flake8Quotes);
}
"multiline-quotes" | "multiline_quotes" => {
plugins.insert(Plugin::Flake8Quotes);
}
"docstring-quotes" | "docstring_quotes" => {
plugins.insert(Plugin::Flake8Quotes);
}
"avoid-escape" | "avoid_escape" => {
plugins.insert(Plugin::Flake8Quotes);
}
// pep8-naming
"ignore-names" | "ignore_names" => {
plugins.insert(Plugin::PEP8Naming);
}
"classmethod-decorators" | "classmethod_decorators" => {
plugins.insert(Plugin::PEP8Naming);
}
"staticmethod-decorators" | "staticmethod_decorators" => {
plugins.insert(Plugin::PEP8Naming);
}
_ => {}
}
}
Vec::from_iter(plugins)
}
/// Resolve the set of enabled `CheckCodePrefix` values for the given plugins.
pub fn resolve_select(
flake8: &HashMap<String, Option<String>>,
plugins: &[Plugin],
) -> BTreeSet<CheckCodePrefix> {
// Include default Pyflakes and pycodestyle checks.
let mut select = BTreeSet::from([CheckCodePrefix::E, CheckCodePrefix::F, CheckCodePrefix::W]);
// Add prefix codes for every plugin.
for plugin in plugins {
for prefix in plugin.select(flake8) {
select.insert(prefix);
}
}
select
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use crate::plugin::{infer_plugins, Plugin};
#[test]
fn it_infers_plugins() {
let actual = infer_plugins(&HashMap::from([(
"inline-quotes".to_string(),
Some("single".to_string()),
)]));
let expected = vec![Plugin::Flake8Quotes];
assert_eq!(actual, expected);
let actual = infer_plugins(&HashMap::from([(
"staticmethod-decorators".to_string(),
Some("[]".to_string()),
)]));
let expected = vec![Plugin::PEP8Naming];
assert_eq!(actual, expected);
}
}

55
resources/test/fixtures/B018.py vendored Normal file
View File

@@ -0,0 +1,55 @@
class Foo1:
"""abc"""
class Foo2:
"""abc"""
a = 2
"str" # Str (no raise)
f"{int}" # JoinedStr (no raise)
1j # Number (complex)
1 # Number (int)
1.0 # Number (float)
b"foo" # Binary
True # NameConstant (True)
False # NameConstant (False)
None # NameConstant (None)
[1, 2] # list
{1, 2} # set
{"foo": "bar"} # dict
class Foo3:
123
a = 2
"str"
1
def foo1():
"""my docstring"""
def foo2():
"""my docstring"""
a = 2
"str" # Str (no raise)
f"{int}" # JoinedStr (no raise)
1j # Number (complex)
1 # Number (int)
1.0 # Number (float)
b"foo" # Binary
True # NameConstant (True)
False # NameConstant (False)
None # NameConstant (None)
[1, 2] # list
{1, 2} # set
{"foo": "bar"} # dict
def foo3():
123
a = 2
"str"
3

14
resources/test/fixtures/F821_3.py vendored Normal file
View File

@@ -0,0 +1,14 @@
"""Test (edge case): accesses of object named `dict`."""
# OK
dict = {"parameters": {}}
dict["parameters"]["userId"] = "userId"
dict["parameters"]["itemId"] = "itemId"
del dict["parameters"]["itemId"]
# F821 Undefined name `key`
# F821 Undefined name `value`
x: dict["key", "value"]
# OK
x: dict[str, str]

6
resources/test/fixtures/X001.py vendored Normal file
View File

@@ -0,0 +1,6 @@
x = "𝐁ad string"
def f():
"""Here's a comment with an unusual parenthesis: """
...

View File

@@ -1 +1 @@
1.64.0
1.65.0

View File

@@ -1,10 +1,14 @@
use std::borrow::Cow;
use std::collections::BTreeSet;
use itertools::Itertools;
use ropey::RopeBuilder;
use rustpython_parser::ast::Location;
use crate::ast::types::Range;
use crate::autofix::{Fix, Patch};
use crate::checks::Check;
use crate::source_code_locator::SourceCodeLocator;
// TODO(charlie): The model here is awkward because `Apply` is only relevant at
// higher levels in the execution flow.
@@ -36,24 +40,28 @@ impl From<bool> for Mode {
}
/// Auto-fix errors in a file, and write the fixed source code to disk.
pub fn fix_file(checks: &mut [Check], contents: &str) -> Option<String> {
pub fn fix_file<'a>(
checks: &'a mut [Check],
locator: &'a SourceCodeLocator<'a>,
) -> Option<Cow<'a, str>> {
if checks.iter().all(|check| check.fix.is_none()) {
return None;
}
Some(apply_fixes(
checks.iter_mut().filter_map(|check| check.fix.as_mut()),
contents,
locator,
))
}
/// Apply a series of fixes.
fn apply_fixes<'a>(fixes: impl Iterator<Item = &'a mut Fix>, contents: &str) -> String {
let lines: Vec<&str> = contents.lines().collect();
let mut output: String = Default::default();
let mut last_pos: Location = Default::default();
let mut applied: BTreeSet<&Patch> = Default::default();
fn apply_fixes<'a>(
fixes: impl Iterator<Item = &'a mut Fix>,
locator: &'a SourceCodeLocator<'a>,
) -> Cow<'a, str> {
let mut output = RopeBuilder::new();
let mut last_pos: Location = Location::new(1, 0);
let mut applied: BTreeSet<&Patch> = BTreeSet::new();
for fix in fixes.sorted_by_key(|fix| fix.patch.location) {
// If we already applied an identical fix as part of another correction, skip
@@ -69,44 +77,27 @@ fn apply_fixes<'a>(fixes: impl Iterator<Item = &'a mut Fix>, contents: &str) ->
continue;
}
if fix.patch.location.row() > last_pos.row() {
if last_pos.row() > 0 || last_pos.column() > 0 {
output.push_str(&lines[last_pos.row() - 1][last_pos.column()..]);
output.push('\n');
}
for line in &lines[last_pos.row()..fix.patch.location.row() - 1] {
output.push_str(line);
output.push('\n');
}
output.push_str(&lines[fix.patch.location.row() - 1][..fix.patch.location.column()]);
output.push_str(&fix.patch.content);
} else {
output.push_str(
&lines[last_pos.row() - 1][last_pos.column()..fix.patch.location.column()],
);
output.push_str(&fix.patch.content);
}
last_pos = fix.patch.end_location;
// Add all contents from `last_pos` to `fix.patch.location`.
let slice = locator.slice_source_code_range(&Range {
location: last_pos,
end_location: fix.patch.location,
});
output.append(&slice);
// Add the patch itself.
output.append(&fix.patch.content);
// Track that the fix was applied.
last_pos = fix.patch.end_location;
applied.insert(&fix.patch);
fix.applied = true;
}
if last_pos.row() > 0
&& (last_pos.row() - 1) < lines.len()
&& (last_pos.row() > 0 || last_pos.column() > 0)
{
output.push_str(&lines[last_pos.row() - 1][last_pos.column()..]);
output.push('\n');
}
if last_pos.row() < lines.len() {
for line in &lines[last_pos.row()..] {
output.push_str(line);
output.push('\n');
}
}
// Add the remaining content.
let slice = locator.slice_source_code_at(&last_pos);
output.append(&slice);
output
Cow::from(output.finish())
}
#[cfg(test)]
@@ -116,11 +107,13 @@ mod tests {
use crate::autofix::fixer::apply_fixes;
use crate::autofix::{Fix, Patch};
use crate::SourceCodeLocator;
#[test]
fn empty_file() -> Result<()> {
let mut fixes = vec![];
let actual = apply_fixes(fixes.iter_mut(), "");
let locator = SourceCodeLocator::new("");
let actual = apply_fixes(fixes.iter_mut(), &locator);
let expected = "";
assert_eq!(actual, expected);
@@ -138,12 +131,12 @@ mod tests {
},
applied: false,
}];
let actual = apply_fixes(
fixes.iter_mut(),
let locator = SourceCodeLocator::new(
"class A(object):
...
",
);
let actual = apply_fixes(fixes.iter_mut(), &locator);
let expected = "class A(Bar):
...
@@ -164,12 +157,12 @@ mod tests {
},
applied: false,
}];
let actual = apply_fixes(
fixes.iter_mut(),
let locator = SourceCodeLocator::new(
"class A(object):
...
",
);
let actual = apply_fixes(fixes.iter_mut(), &locator);
let expected = "class A:
...
@@ -200,12 +193,12 @@ mod tests {
applied: false,
},
];
let actual = apply_fixes(
fixes.iter_mut(),
let locator = SourceCodeLocator::new(
"class A(object, object):
...
",
);
let actual = apply_fixes(fixes.iter_mut(), &locator);
let expected = "class A:
...
@@ -236,12 +229,12 @@ mod tests {
applied: false,
},
];
let actual = apply_fixes(
fixes.iter_mut(),
let locator = SourceCodeLocator::new(
"class A(object):
...
",
);
let actual = apply_fixes(fixes.iter_mut(), &locator);
let expected = "class A:
...

View File

@@ -79,6 +79,7 @@ pub struct Checker<'a> {
in_f_string: Option<Range>,
in_annotation: bool,
in_literal: bool,
in_subscript: bool,
seen_import_boundary: bool,
futures_allowed: bool,
annotations_future_enabled: bool,
@@ -118,6 +119,7 @@ impl<'a> Checker<'a> {
in_f_string: Default::default(),
in_annotation: Default::default(),
in_literal: Default::default(),
in_subscript: Default::default(),
seen_import_boundary: Default::default(),
futures_allowed: true,
annotations_future_enabled: Default::default(),
@@ -238,6 +240,7 @@ where
decorator_list,
returns,
args,
body,
..
}
| StmtKind::AsyncFunctionDef {
@@ -245,6 +248,7 @@ where
decorator_list,
returns,
args,
body,
..
} => {
if self.settings.enabled.contains(&CheckCode::E743) {
@@ -298,6 +302,10 @@ where
}
}
if self.settings.enabled.contains(&CheckCode::B018) {
flake8_bugbear::plugins::useless_expression(self, body);
}
self.check_builtin_shadowing(name, Range::from_located(stmt), true);
// Visit the decorators and arguments, but avoid the body, which will be
@@ -368,6 +376,7 @@ where
bases,
keywords,
decorator_list,
body,
..
} => {
if self.settings.enabled.contains(&CheckCode::U004) {
@@ -399,6 +408,10 @@ where
}
}
if self.settings.enabled.contains(&CheckCode::B018) {
flake8_bugbear::plugins::useless_expression(self, body);
}
self.check_builtin_shadowing(
name,
self.locate_check(Range::from_located(stmt)),
@@ -717,9 +730,7 @@ where
StmtKind::Raise { exc, .. } => {
if self.settings.enabled.contains(&CheckCode::F901) {
if let Some(expr) = exc {
if let Some(check) = pyflakes::checks::raise_not_implemented(expr) {
self.checks.push(check);
}
pyflakes::plugins::raise_not_implemented(self, expr);
}
}
}
@@ -1490,36 +1501,56 @@ where
}
}
ExprKind::Subscript { value, slice, ctx } => {
match typing::match_annotated_subscript(value, &self.from_imports) {
Some(subscript) => match subscript {
// Ex) Optional[int]
SubscriptKind::AnnotatedSubscript => {
self.visit_expr(value);
self.visit_annotation(slice);
self.visit_expr_context(ctx);
}
// Ex) Annotated[int, "Hello, world!"]
SubscriptKind::PEP593AnnotatedSubscript => {
// First argument is a type (including forward references); the rest are
// arbitrary Python objects.
self.visit_expr(value);
if let ExprKind::Tuple { elts, ctx } = &slice.node {
if let Some(expr) = elts.first() {
self.visit_expr(expr);
self.in_annotation = false;
for expr in elts.iter().skip(1) {
self.visit_expr(expr);
}
self.in_annotation = true;
// Only allow annotations in `ExprContext::Load`. If we have, e.g.,
// `obj["foo"]["bar"]`, we need to avoid treating the `obj["foo"]`
// portion as an annotation, despite having `ExprContext::Load`. Thus, we track
// the `ExprContext` at the top-level.
let prev_in_subscript = self.in_subscript;
if self.in_subscript {
visitor::walk_expr(self, expr);
} else if matches!(ctx, ExprContext::Store | ExprContext::Del) {
self.in_subscript = true;
visitor::walk_expr(self, expr);
} else {
self.in_subscript = true;
match typing::match_annotated_subscript(value, &self.from_imports) {
Some(subscript) => {
match subscript {
// Ex) Optional[int]
SubscriptKind::AnnotatedSubscript => {
self.visit_expr(value);
self.visit_annotation(slice);
self.visit_expr_context(ctx);
}
} else {
error!("Found non-ExprKind::Tuple argument to PEP 593 Annotation.")
// Ex) Annotated[int, "Hello, world!"]
SubscriptKind::PEP593AnnotatedSubscript => {
// First argument is a type (including forward references); the
// rest are arbitrary Python
// objects.
self.visit_expr(value);
if let ExprKind::Tuple { elts, ctx } = &slice.node {
if let Some(expr) = elts.first() {
self.visit_expr(expr);
self.in_annotation = false;
for expr in elts.iter().skip(1) {
self.visit_expr(expr);
}
self.in_annotation = true;
self.visit_expr_context(ctx);
}
} else {
error!(
"Found non-ExprKind::Tuple argument to PEP 593 \
Annotation."
)
}
}
}
}
},
None => visitor::walk_expr(self, expr),
None => visitor::walk_expr(self, expr),
}
}
self.in_subscript = prev_in_subscript;
}
_ => visitor::walk_expr(self, expr),
}

View File

@@ -2,17 +2,20 @@
use rustpython_parser::lexer::{LexResult, Tok};
use crate::autofix::fixer;
use crate::checks::{Check, CheckCode};
use crate::flake8_quotes::docstring_detection::StateMachine;
use crate::source_code_locator::SourceCodeLocator;
use crate::{flake8_quotes, pycodestyle, Settings};
use crate::{flake8_quotes, pycodestyle, rules, Settings};
pub fn check_tokens(
checks: &mut Vec<Check>,
locator: &SourceCodeLocator,
tokens: &[LexResult],
settings: &Settings,
autofix: &fixer::Mode,
) {
let enforce_ambiguous_unicode_character = settings.enabled.contains(&CheckCode::X001);
let enforce_invalid_escape_sequence = settings.enabled.contains(&CheckCode::W605);
let enforce_quotes = settings.enabled.contains(&CheckCode::Q000)
| settings.enabled.contains(&CheckCode::Q001)
@@ -21,6 +24,18 @@ pub fn check_tokens(
let mut state_machine: StateMachine = Default::default();
for (start, tok, end) in tokens.iter().flatten() {
// X001
if enforce_ambiguous_unicode_character {
if matches!(tok, Tok::String { .. }) {
checks.extend(rules::checks::ambiguous_unicode_character(
locator,
start,
end,
autofix.patch(),
));
}
}
// W605
if enforce_invalid_escape_sequence {
if matches!(tok, Tok::String { .. }) {

View File

@@ -84,6 +84,7 @@ pub enum CheckCode {
B013,
B014,
B017,
B018,
B025,
// flake8-comprehensions
C400,
@@ -180,6 +181,8 @@ pub enum CheckCode {
N816,
N817,
N818,
// Ruff
X001,
// Meta
M001,
}
@@ -197,6 +200,7 @@ pub enum CheckCategory {
Flake8Builtins,
Flake8Print,
Flake8Quotes,
Ruff,
Meta,
}
@@ -214,6 +218,7 @@ impl CheckCategory {
CheckCategory::Pyupgrade => "pyupgrade",
CheckCategory::Pydocstyle => "pydocstyle",
CheckCategory::PEP8Naming => "pep8-naming",
CheckCategory::Ruff => "Ruff-specific rules",
CheckCategory::Meta => "Meta rules",
}
}
@@ -294,6 +299,7 @@ pub enum CheckKind {
RedundantTupleInExceptionHandler(String),
DuplicateHandlerException(Vec<String>),
NoAssertRaisesException,
UselessExpression,
DuplicateTryBlockException(String),
// flake8-comprehensions
UnnecessaryGeneratorList,
@@ -390,6 +396,8 @@ pub enum CheckKind {
MixedCaseVariableInGlobalScope(String),
CamelcaseImportedAsAcronym(String, String),
ErrorSuffixOnExceptionName(String),
// Ruff
AmbiguousUnicodeCharacter(char, char),
// Meta
UnusedNOQA(Option<Vec<String>>),
}
@@ -400,11 +408,12 @@ impl CheckCode {
pub fn lint_source(&self) -> &'static LintSource {
match self {
CheckCode::E501 | CheckCode::W292 | CheckCode::M001 => &LintSource::Lines,
CheckCode::W605
| CheckCode::Q000
CheckCode::Q000
| CheckCode::Q001
| CheckCode::Q002
| CheckCode::Q003 => &LintSource::Tokens,
| CheckCode::Q003
| CheckCode::W605
| CheckCode::X001 => &LintSource::Tokens,
CheckCode::E902 => &LintSource::FileSystem,
_ => &LintSource::AST,
}
@@ -476,6 +485,7 @@ impl CheckCode {
}
CheckCode::B014 => CheckKind::DuplicateHandlerException(vec!["ValueError".to_string()]),
CheckCode::B017 => CheckKind::NoAssertRaisesException,
CheckCode::B018 => CheckKind::UselessExpression,
CheckCode::B025 => CheckKind::DuplicateTryBlockException("Exception".to_string()),
// flake8-comprehensions
CheckCode::C400 => CheckKind::UnnecessaryGeneratorList,
@@ -604,6 +614,8 @@ impl CheckCode {
CheckKind::CamelcaseImportedAsAcronym("...".to_string(), "...".to_string())
}
CheckCode::N818 => CheckKind::ErrorSuffixOnExceptionName("...".to_string()),
// Ruff
CheckCode::X001 => CheckKind::AmbiguousUnicodeCharacter('𝐁', 'B'),
// Meta
CheckCode::M001 => CheckKind::UnusedNOQA(None),
}
@@ -665,6 +677,7 @@ impl CheckCode {
CheckCode::B013 => CheckCategory::Flake8Bugbear,
CheckCode::B014 => CheckCategory::Flake8Bugbear,
CheckCode::B017 => CheckCategory::Flake8Bugbear,
CheckCode::B018 => CheckCategory::Flake8Bugbear,
CheckCode::B025 => CheckCategory::Flake8Bugbear,
CheckCode::C400 => CheckCategory::Flake8Comprehensions,
CheckCode::C401 => CheckCategory::Flake8Comprehensions,
@@ -755,6 +768,7 @@ impl CheckCode {
CheckCode::N816 => CheckCategory::PEP8Naming,
CheckCode::N817 => CheckCategory::PEP8Naming,
CheckCode::N818 => CheckCategory::PEP8Naming,
CheckCode::X001 => CheckCategory::Ruff,
CheckCode::M001 => CheckCategory::Meta,
}
}
@@ -822,6 +836,7 @@ impl CheckKind {
CheckKind::RedundantTupleInExceptionHandler(_) => &CheckCode::B013,
CheckKind::DuplicateHandlerException(_) => &CheckCode::B014,
CheckKind::NoAssertRaisesException => &CheckCode::B017,
CheckKind::UselessExpression => &CheckCode::B018,
CheckKind::DuplicateTryBlockException(_) => &CheckCode::B025,
// flake8-comprehensions
CheckKind::UnnecessaryGeneratorList => &CheckCode::C400,
@@ -918,6 +933,8 @@ impl CheckKind {
CheckKind::MixedCaseVariableInGlobalScope(..) => &CheckCode::N816,
CheckKind::CamelcaseImportedAsAcronym(..) => &CheckCode::N817,
CheckKind::ErrorSuffixOnExceptionName(..) => &CheckCode::N818,
// Ruff
CheckKind::AmbiguousUnicodeCharacter(..) => &CheckCode::X001,
// Meta
CheckKind::UnusedNOQA(_) => &CheckCode::M001,
}
@@ -1110,6 +1127,9 @@ impl CheckKind {
`assertRaisesRegex`, or use the context manager form of `assertRaises`."
.to_string()
}
CheckKind::UselessExpression => {
"Found useless expression. Either assign it to a variable or remove it.".to_string()
}
CheckKind::DuplicateTryBlockException(name) => {
format!("try-except block with duplicate exception `{name}`")
}
@@ -1398,6 +1418,13 @@ impl CheckKind {
CheckKind::ErrorSuffixOnExceptionName(name) => {
format!("Exception name `{name}` should be named with an Error suffix")
}
// Ruff
CheckKind::AmbiguousUnicodeCharacter(confusable, representant) => {
format!(
"Use of ambiguous unicode character '{confusable}' (did you mean \
'{representant}'?)"
)
}
// Meta
CheckKind::UnusedNOQA(codes) => match codes {
None => "Unused `noqa` directive".to_string(),
@@ -1439,7 +1466,8 @@ impl CheckKind {
pub fn fixable(&self) -> bool {
matches!(
self,
CheckKind::BlankLineAfterLastSection(_)
CheckKind::AmbiguousUnicodeCharacter(_, _)
| CheckKind::BlankLineAfterLastSection(_)
| CheckKind::BlankLineAfterSection(_)
| CheckKind::BlankLineAfterSummary
| CheckKind::BlankLineBeforeSection(_)
@@ -1461,6 +1489,7 @@ impl CheckKind {
| CheckKind::OneBlankLineBeforeClass(_)
| CheckKind::PPrintFound
| CheckKind::PrintFound
| CheckKind::RaiseNotImplemented
| CheckKind::SectionNameEndsInColon(_)
| CheckKind::SectionNotOverIndented(_)
| CheckKind::SectionUnderlineAfterName(_)

View File

@@ -1,11 +1,11 @@
//! File automatically generated by examples/generate_check_code_prefix.rs.
use serde::{Deserialize, Serialize};
use strum_macros::{AsRefStr, EnumString};
use strum_macros::EnumString;
use crate::checks::CheckCode;
#[derive(AsRefStr, EnumString, Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[derive(EnumString, Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
pub enum CheckCodePrefix {
A,
A0,
@@ -24,6 +24,7 @@ pub enum CheckCodePrefix {
B013,
B014,
B017,
B018,
B02,
B025,
C,
@@ -227,6 +228,10 @@ pub enum CheckCodePrefix {
W6,
W60,
W605,
X,
X0,
X00,
X001,
}
#[derive(PartialEq, Eq, PartialOrd, Ord)]
@@ -254,6 +259,7 @@ impl CheckCodePrefix {
CheckCode::B013,
CheckCode::B014,
CheckCode::B017,
CheckCode::B018,
CheckCode::B025,
],
CheckCodePrefix::B0 => vec![
@@ -264,24 +270,25 @@ impl CheckCodePrefix {
CheckCode::B013,
CheckCode::B014,
CheckCode::B017,
CheckCode::B018,
CheckCode::B025,
],
CheckCodePrefix::B00 => vec![CheckCode::B002, CheckCode::B006, CheckCode::B007],
CheckCodePrefix::B002 => vec![CheckCode::B002],
CheckCodePrefix::B006 => vec![CheckCode::B006],
CheckCodePrefix::B007 => vec![CheckCode::B007],
CheckCodePrefix::B01 => {
vec![
CheckCode::B011,
CheckCode::B013,
CheckCode::B014,
CheckCode::B017,
]
}
CheckCodePrefix::B01 => vec![
CheckCode::B011,
CheckCode::B013,
CheckCode::B014,
CheckCode::B017,
CheckCode::B018,
],
CheckCodePrefix::B011 => vec![CheckCode::B011],
CheckCodePrefix::B013 => vec![CheckCode::B013],
CheckCodePrefix::B014 => vec![CheckCode::B014],
CheckCodePrefix::B017 => vec![CheckCode::B017],
CheckCodePrefix::B018 => vec![CheckCode::B018],
CheckCodePrefix::B02 => vec![CheckCode::B025],
CheckCodePrefix::B025 => vec![CheckCode::B025],
CheckCodePrefix::C => vec![
@@ -584,14 +591,12 @@ impl CheckCodePrefix {
CheckCode::E742,
CheckCode::E743,
],
CheckCodePrefix::E71 => {
vec![
CheckCode::E711,
CheckCode::E712,
CheckCode::E713,
CheckCode::E714,
]
}
CheckCodePrefix::E71 => vec![
CheckCode::E711,
CheckCode::E712,
CheckCode::E713,
CheckCode::E714,
],
CheckCodePrefix::E711 => vec![CheckCode::E711],
CheckCodePrefix::E712 => vec![CheckCode::E712],
CheckCodePrefix::E713 => vec![CheckCode::E713],
@@ -684,14 +689,12 @@ impl CheckCodePrefix {
CheckCodePrefix::F62 => vec![CheckCode::F621, CheckCode::F622],
CheckCodePrefix::F621 => vec![CheckCode::F621],
CheckCodePrefix::F622 => vec![CheckCode::F622],
CheckCodePrefix::F63 => {
vec![
CheckCode::F631,
CheckCode::F632,
CheckCode::F633,
CheckCode::F634,
]
}
CheckCodePrefix::F63 => vec![
CheckCode::F631,
CheckCode::F632,
CheckCode::F633,
CheckCode::F634,
],
CheckCodePrefix::F631 => vec![CheckCode::F631],
CheckCodePrefix::F632 => vec![CheckCode::F632],
CheckCodePrefix::F633 => vec![CheckCode::F633],
@@ -808,30 +811,24 @@ impl CheckCodePrefix {
CheckCodePrefix::N816 => vec![CheckCode::N816],
CheckCodePrefix::N817 => vec![CheckCode::N817],
CheckCodePrefix::N818 => vec![CheckCode::N818],
CheckCodePrefix::Q => {
vec![
CheckCode::Q000,
CheckCode::Q001,
CheckCode::Q002,
CheckCode::Q003,
]
}
CheckCodePrefix::Q0 => {
vec![
CheckCode::Q000,
CheckCode::Q001,
CheckCode::Q002,
CheckCode::Q003,
]
}
CheckCodePrefix::Q00 => {
vec![
CheckCode::Q000,
CheckCode::Q001,
CheckCode::Q002,
CheckCode::Q003,
]
}
CheckCodePrefix::Q => vec![
CheckCode::Q000,
CheckCode::Q001,
CheckCode::Q002,
CheckCode::Q003,
],
CheckCodePrefix::Q0 => vec![
CheckCode::Q000,
CheckCode::Q001,
CheckCode::Q002,
CheckCode::Q003,
],
CheckCodePrefix::Q00 => vec![
CheckCode::Q000,
CheckCode::Q001,
CheckCode::Q002,
CheckCode::Q003,
],
CheckCodePrefix::Q000 => vec![CheckCode::Q000],
CheckCodePrefix::Q001 => vec![CheckCode::Q001],
CheckCodePrefix::Q002 => vec![CheckCode::Q002],
@@ -886,6 +883,10 @@ impl CheckCodePrefix {
CheckCodePrefix::W6 => vec![CheckCode::W605],
CheckCodePrefix::W60 => vec![CheckCode::W605],
CheckCodePrefix::W605 => vec![CheckCode::W605],
CheckCodePrefix::X => vec![CheckCode::X001],
CheckCodePrefix::X0 => vec![CheckCode::X001],
CheckCodePrefix::X00 => vec![CheckCode::X001],
CheckCodePrefix::X001 => vec![CheckCode::X001],
}
}
}
@@ -910,6 +911,7 @@ impl CheckCodePrefix {
CheckCodePrefix::B013 => PrefixSpecificity::Explicit,
CheckCodePrefix::B014 => PrefixSpecificity::Explicit,
CheckCodePrefix::B017 => PrefixSpecificity::Explicit,
CheckCodePrefix::B018 => PrefixSpecificity::Explicit,
CheckCodePrefix::B02 => PrefixSpecificity::Tens,
CheckCodePrefix::B025 => PrefixSpecificity::Explicit,
CheckCodePrefix::C => PrefixSpecificity::Category,
@@ -1113,6 +1115,10 @@ impl CheckCodePrefix {
CheckCodePrefix::W6 => PrefixSpecificity::Hundreds,
CheckCodePrefix::W60 => PrefixSpecificity::Tens,
CheckCodePrefix::W605 => PrefixSpecificity::Explicit,
CheckCodePrefix::X => PrefixSpecificity::Category,
CheckCodePrefix::X0 => PrefixSpecificity::Hundreds,
CheckCodePrefix::X00 => PrefixSpecificity::Tens,
CheckCodePrefix::X001 => PrefixSpecificity::Explicit,
}
}
}

View File

@@ -1,18 +1,8 @@
use anyhow::Result;
use libcst_native::Module;
use rustpython_ast::Located;
use crate::ast::types::Range;
use crate::source_code_locator::SourceCodeLocator;
pub fn match_tree<'a, T>(
locator: &'a SourceCodeLocator,
located: &'a Located<T>,
) -> Result<Module<'a>> {
match libcst_native::parse_module(
locator.slice_source_code_range(&Range::from_located(located)),
None,
) {
pub fn match_module(module_text: &str) -> Result<Module> {
match libcst_native::parse_module(module_text, None) {
Ok(module) => Ok(module),
Err(_) => return Err(anyhow::anyhow!("Failed to extract CST from source.")),
}

View File

@@ -25,12 +25,15 @@ pub fn leading_space(line: &str) -> String {
}
/// Extract the leading indentation from a docstring.
pub fn indentation<'a>(checker: &'a Checker, docstring: &Expr) -> &'a str {
pub fn indentation<'a>(checker: &'a Checker, docstring: &Expr) -> String {
let range = Range::from_located(docstring);
checker.locator.slice_source_code_range(&Range {
location: Location::new(range.location.row(), 0),
end_location: Location::new(range.location.row(), range.location.column()),
})
checker
.locator
.slice_source_code_range(&Range {
location: Location::new(range.location.row(), 0),
end_location: Location::new(range.location.row(), range.location.column()),
})
.to_string()
}
/// Replace any non-whitespace characters from an indentation string.

View File

@@ -5,6 +5,7 @@ pub use mutable_argument_default::mutable_argument_default;
pub use redundant_tuple_in_exception_handler::redundant_tuple_in_exception_handler;
pub use unary_prefix_increment::unary_prefix_increment;
pub use unused_loop_control_variable::unused_loop_control_variable;
pub use useless_expression::useless_expression;
mod assert_false;
mod assert_raises_exception;
@@ -13,3 +14,4 @@ mod mutable_argument_default;
mod redundant_tuple_in_exception_handler;
mod unary_prefix_increment;
mod unused_loop_control_variable;
mod useless_expression;

View File

@@ -0,0 +1,30 @@
use rustpython_ast::{Constant, ExprKind, Stmt, StmtKind};
use crate::ast::types::{CheckLocator, Range};
use crate::check_ast::Checker;
use crate::checks::{Check, CheckKind};
pub fn useless_expression(checker: &mut Checker, body: &[Stmt]) {
for stmt in body {
if let StmtKind::Expr { value } = &stmt.node {
match &value.node {
ExprKind::List { .. } | ExprKind::Dict { .. } | ExprKind::Set { .. } => {
checker.add_check(Check::new(
CheckKind::UselessExpression,
checker.locate_check(Range::from_located(value)),
));
}
ExprKind::Constant { value: val, .. } => match &val {
Constant::Str { .. } => {}
_ => {
checker.add_check(Check::new(
CheckKind::UselessExpression,
checker.locate_check(Range::from_located(value)),
));
}
},
_ => {}
}
}
}
}

View File

@@ -6,8 +6,9 @@ use libcst_native::{
SmallStatement, Statement, Tuple,
};
use crate::ast::types::Range;
use crate::autofix::Fix;
use crate::cst::matchers::match_tree;
use crate::cst::matchers::match_module;
use crate::source_code_locator::SourceCodeLocator;
fn match_expr<'a, 'b>(module: &'a mut Module<'b>) -> Result<&'a mut Expr<'b>> {
@@ -44,7 +45,8 @@ pub fn fix_unnecessary_generator_list(
expr: &rustpython_ast::Expr,
) -> Result<Fix> {
// Expr(Call(GeneratorExp)))) -> Expr(ListComp)))
let mut tree = match_tree(locator, expr)?;
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
let mut tree = match_module(&module_text)?;
let mut body = match_expr(&mut tree)?;
let call = match_call(body)?;
let arg = match_arg(call)?;
@@ -86,7 +88,8 @@ pub fn fix_unnecessary_generator_set(
expr: &rustpython_ast::Expr,
) -> Result<Fix> {
// Expr(Call(GeneratorExp)))) -> Expr(SetComp)))
let mut tree = match_tree(locator, expr)?;
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
let mut tree = match_module(&module_text)?;
let mut body = match_expr(&mut tree)?;
let call = match_call(body)?;
let arg = match_arg(call)?;
@@ -128,7 +131,8 @@ pub fn fix_unnecessary_generator_dict(
locator: &SourceCodeLocator,
expr: &rustpython_ast::Expr,
) -> Result<Fix> {
let mut tree = match_tree(locator, expr)?;
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
let mut tree = match_module(&module_text)?;
let mut body = match_expr(&mut tree)?;
let call = match_call(body)?;
let arg = match_arg(call)?;
@@ -194,7 +198,8 @@ pub fn fix_unnecessary_list_comprehension_set(
) -> Result<Fix> {
// Expr(Call(ListComp)))) ->
// Expr(SetComp)))
let mut tree = match_tree(locator, expr)?;
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
let mut tree = match_module(&module_text)?;
let mut body = match_expr(&mut tree)?;
let call = match_call(body)?;
let arg = match_arg(call)?;
@@ -234,7 +239,8 @@ pub fn fix_unnecessary_literal_set(
expr: &rustpython_ast::Expr,
) -> Result<Fix> {
// Expr(Call(List|Tuple)))) -> Expr(Set)))
let mut tree = match_tree(locator, expr)?;
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
let mut tree = match_module(&module_text)?;
let mut body = match_expr(&mut tree)?;
let mut call = match_call(body)?;
let arg = match_arg(call)?;
@@ -281,7 +287,8 @@ pub fn fix_unnecessary_literal_dict(
expr: &rustpython_ast::Expr,
) -> Result<Fix> {
// Expr(Call(List|Tuple)))) -> Expr(Dict)))
let mut tree = match_tree(locator, expr)?;
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
let mut tree = match_module(&module_text)?;
let mut body = match_expr(&mut tree)?;
let call = match_call(body)?;
let arg = match_arg(call)?;
@@ -351,8 +358,9 @@ pub fn fix_unnecessary_collection_call(
locator: &SourceCodeLocator,
expr: &rustpython_ast::Expr,
) -> Result<Fix> {
// Expr(Call("list" | "tuple" | "dict")))) -> Expr(List|Tuple|Dict)))
let mut tree = match_tree(locator, expr)?;
// Expr(Call("list" | "tuple" | "dict")))) -> Expr(List|Tuple|Dict)
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
let mut tree = match_module(&module_text)?;
let mut body = match_expr(&mut tree)?;
let call = match_call(body)?;
let name = if let Expression::Name(name) = &call.func.as_ref() {
@@ -463,7 +471,8 @@ pub fn fix_unnecessary_literal_within_tuple_call(
locator: &SourceCodeLocator,
expr: &rustpython_ast::Expr,
) -> Result<Fix> {
let mut tree = match_tree(locator, expr)?;
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
let mut tree = match_module(&module_text)?;
let mut body = match_expr(&mut tree)?;
let call = match_call(body)?;
let arg = match_arg(call)?;
@@ -518,7 +527,8 @@ pub fn fix_unnecessary_literal_within_list_call(
locator: &SourceCodeLocator,
expr: &rustpython_ast::Expr,
) -> Result<Fix> {
let mut tree = match_tree(locator, expr)?;
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
let mut tree = match_module(&module_text)?;
let mut body = match_expr(&mut tree)?;
let call = match_call(body)?;
let arg = match_arg(call)?;
@@ -576,7 +586,8 @@ pub fn fix_unnecessary_list_call(
expr: &rustpython_ast::Expr,
) -> Result<Fix> {
// Expr(Call(List|Tuple)))) -> Expr(List|Tuple)))
let mut tree = match_tree(locator, expr)?;
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
let mut tree = match_module(&module_text)?;
let mut body = match_expr(&mut tree)?;
let call = match_call(body)?;
let arg = match_arg(call)?;
@@ -598,7 +609,8 @@ pub fn fix_unnecessary_comprehension(
locator: &SourceCodeLocator,
expr: &rustpython_ast::Expr,
) -> Result<Fix> {
let mut tree = match_tree(locator, expr)?;
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
let mut tree = match_module(&module_text)?;
let mut body = match_expr(&mut tree)?;
match &body.value {

View File

@@ -148,13 +148,22 @@ mod tests {
use crate::checks::{Check, CheckCode};
use crate::flake8_quotes::settings::Quote;
use crate::linter::tokenize;
use crate::{flake8_quotes, fs, linter, noqa, Settings};
use crate::{flake8_quotes, fs, linter, noqa, Settings, SourceCodeLocator};
fn check_path(path: &Path, settings: &Settings, autofix: &fixer::Mode) -> Result<Vec<Check>> {
let contents = fs::read_file(path)?;
let tokens: Vec<LexResult> = tokenize(&contents);
let locator = SourceCodeLocator::new(&contents);
let noqa_line_for = noqa::extract_noqa_line_for(&tokens);
linter::check_path(path, &contents, tokens, &noqa_line_for, settings, autofix)
linter::check_path(
path,
&contents,
tokens,
&locator,
&noqa_line_for,
settings,
autofix,
)
}
#[test_case(Path::new("doubles.py"))]

View File

@@ -11,6 +11,7 @@ use crate::autofix::fixer::Mode;
use crate::checks::Check;
use crate::linter::{check_path, tokenize};
use crate::settings::configuration::Configuration;
use crate::source_code_locator::SourceCodeLocator;
mod ast;
pub mod autofix;
@@ -41,6 +42,7 @@ mod pydocstyle;
mod pyflakes;
mod python;
mod pyupgrade;
mod rules;
pub mod settings;
pub mod source_code_locator;
pub mod visibility;
@@ -65,6 +67,9 @@ pub fn check(path: &Path, contents: &str, autofix: bool) -> Result<Vec<Check>> {
// Tokenize once.
let tokens: Vec<LexResult> = tokenize(contents);
// Initialize the SourceCodeLocator (which computes offsets lazily).
let locator = SourceCodeLocator::new(contents);
// Determine the noqa line for every line in the source.
let noqa_line_for = noqa::extract_noqa_line_for(&tokens);
@@ -73,6 +78,7 @@ pub fn check(path: &Path, contents: &str, autofix: bool) -> Result<Vec<Check>> {
path,
contents,
tokens,
&locator,
&noqa_line_for,
&settings,
&if autofix { Mode::Generate } else { Mode::None },

View File

@@ -54,6 +54,7 @@ pub(crate) fn check_path(
path: &Path,
contents: &str,
tokens: Vec<LexResult>,
locator: &SourceCodeLocator,
noqa_line_for: &[usize],
settings: &Settings,
autofix: &fixer::Mode,
@@ -61,16 +62,13 @@ pub(crate) fn check_path(
// Aggregate all checks.
let mut checks: Vec<Check> = vec![];
// Initialize the SourceCodeLocator (which computes offsets lazily).
let locator = SourceCodeLocator::new(contents);
// Run the token-based checks.
if settings
.enabled
.iter()
.any(|check_code| matches!(check_code.lint_source(), LintSource::Tokens))
{
check_tokens(&mut checks, &locator, &tokens, settings);
check_tokens(&mut checks, locator, &tokens, settings, autofix);
}
// Run the AST-based checks.
@@ -81,7 +79,7 @@ pub(crate) fn check_path(
{
match parse_program_tokens(tokens, "<filename>") {
Ok(python_ast) => {
checks.extend(check_ast(&python_ast, &locator, settings, autofix, path))
checks.extend(check_ast(&python_ast, locator, settings, autofix, path))
}
Err(parse_error) => {
if settings.enabled.contains(&CheckCode::E999) {
@@ -123,19 +121,29 @@ pub fn lint_stdin(
// Tokenize once.
let tokens: Vec<LexResult> = tokenize(stdin);
// Initialize the SourceCodeLocator (which computes offsets lazily).
let locator = SourceCodeLocator::new(stdin);
// Determine the noqa line for every line in the source.
let noqa_line_for = noqa::extract_noqa_line_for(&tokens);
// Generate checks.
let mut checks = check_path(path, stdin, tokens, &noqa_line_for, settings, autofix)?;
let mut checks = check_path(
path,
stdin,
tokens,
&locator,
&noqa_line_for,
settings,
autofix,
)?;
// Apply autofix, write results to stdout.
if matches!(autofix, fixer::Mode::Apply) {
let output = match fix_file(&mut checks, stdin) {
None => stdin.to_string(),
Some(content) => content,
};
io::stdout().write_all(output.as_bytes())?;
match fix_file(&mut checks, &locator) {
None => io::stdout().write_all(stdin.as_bytes()),
Some(contents) => io::stdout().write_all(contents.as_bytes()),
}?;
}
// Convert to messages.
@@ -167,16 +175,27 @@ pub fn lint_path(
// Tokenize once.
let tokens: Vec<LexResult> = tokenize(&contents);
// Initialize the SourceCodeLocator (which computes offsets lazily).
let locator = SourceCodeLocator::new(&contents);
// Determine the noqa line for every line in the source.
let noqa_line_for = noqa::extract_noqa_line_for(&tokens);
// Generate checks.
let mut checks = check_path(path, &contents, tokens, &noqa_line_for, settings, autofix)?;
let mut checks = check_path(
path,
&contents,
tokens,
&locator,
&noqa_line_for,
settings,
autofix,
)?;
// Apply autofix.
if matches!(autofix, fixer::Mode::Apply) {
if let Some(fixed_contents) = fix_file(&mut checks, &contents) {
write(path, fixed_contents)?;
if let Some(fixed_contents) = fix_file(&mut checks, &locator) {
write(path, fixed_contents.as_ref())?;
}
};
@@ -198,6 +217,9 @@ pub fn add_noqa_to_path(path: &Path, settings: &Settings) -> Result<usize> {
// Tokenize once.
let tokens: Vec<LexResult> = tokenize(&contents);
// Initialize the SourceCodeLocator (which computes offsets lazily).
let locator = SourceCodeLocator::new(&contents);
// Determine the noqa line for every line in the source.
let noqa_line_for = noqa::extract_noqa_line_for(&tokens);
@@ -206,6 +228,7 @@ pub fn add_noqa_to_path(path: &Path, settings: &Settings) -> Result<usize> {
path,
&contents,
tokens,
&locator,
&noqa_line_for,
settings,
&fixer::Mode::None,
@@ -243,13 +266,27 @@ mod tests {
use crate::autofix::fixer;
use crate::checks::{Check, CheckCode};
use crate::linter::tokenize;
use crate::{fs, linter, noqa, settings, Settings};
use crate::source_code_locator::SourceCodeLocator;
use crate::{fs, linter, noqa, settings};
fn check_path(path: &Path, settings: &Settings, autofix: &fixer::Mode) -> Result<Vec<Check>> {
fn check_path(
path: &Path,
settings: &settings::Settings,
autofix: &fixer::Mode,
) -> Result<Vec<Check>> {
let contents = fs::read_file(path)?;
let tokens: Vec<LexResult> = tokenize(&contents);
let locator = SourceCodeLocator::new(&contents);
let noqa_line_for = noqa::extract_noqa_line_for(&tokens);
linter::check_path(path, &contents, tokens, &noqa_line_for, settings, autofix)
linter::check_path(
path,
&contents,
tokens,
&locator,
&noqa_line_for,
settings,
autofix,
)
}
#[test_case(CheckCode::A001, Path::new("A001.py"); "A001")]
@@ -262,6 +299,7 @@ mod tests {
#[test_case(CheckCode::B013, Path::new("B013.py"); "B013")]
#[test_case(CheckCode::B014, Path::new("B014.py"); "B014")]
#[test_case(CheckCode::B017, Path::new("B017.py"); "B017")]
#[test_case(CheckCode::B018, Path::new("B018.py"); "B018")]
#[test_case(CheckCode::B025, Path::new("B025.py"); "B025")]
#[test_case(CheckCode::C400, Path::new("C400.py"); "C400")]
#[test_case(CheckCode::C401, Path::new("C401.py"); "C401")]
@@ -366,6 +404,7 @@ mod tests {
#[test_case(CheckCode::F821, Path::new("F821_0.py"); "F821_0")]
#[test_case(CheckCode::F821, Path::new("F821_1.py"); "F821_1")]
#[test_case(CheckCode::F821, Path::new("F821_2.py"); "F821_2")]
#[test_case(CheckCode::F821, Path::new("F821_3.py"); "F821_3")]
#[test_case(CheckCode::F822, Path::new("F822.py"); "F822")]
#[test_case(CheckCode::F823, Path::new("F823.py"); "F823")]
#[test_case(CheckCode::F831, Path::new("F831.py"); "F831")]
@@ -400,6 +439,7 @@ mod tests {
#[test_case(CheckCode::W292, Path::new("W292_1.py"); "W292_1")]
#[test_case(CheckCode::W292, Path::new("W292_2.py"); "W292_2")]
#[test_case(CheckCode::W605, Path::new("W605.py"); "W605")]
#[test_case(CheckCode::X001, Path::new("X001.py"); "X001")]
fn checks(check_code: CheckCode, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", check_code.as_ref(), path.to_string_lossy());
let mut checks = check_path(

View File

@@ -271,7 +271,7 @@ pub fn invalid_escape_sequence(
});
// Determine whether the string is single- or triple-quoted.
let quote = extract_quote(text);
let quote = extract_quote(&text);
let quote_pos = text.find(quote).unwrap();
let prefix = text[..quote_pos].to_lowercase();
let body = &text[(quote_pos + quote.len())..(text.len() - quote.len())];
@@ -299,9 +299,9 @@ pub fn invalid_escape_sequence(
start.column() + prefix.len() + quote.len() + col_offset,
)
} else {
Location::new(start.row() + row_offset, col_offset + 1)
Location::new(start.row() + row_offset, col_offset)
};
let end_location = Location::new(location.row(), location.column() + 1);
let end_location = Location::new(location.row(), location.column() + 2);
checks.push(Check::new(
CheckKind::InvalidEscapeSequence(next_char),
Range {

View File

@@ -210,7 +210,7 @@ pub fn blank_before_after_function(checker: &mut Checker, definition: &Definitio
.count();
// Avoid D202 violations for blank lines followed by inner functions or classes.
if blank_lines_after == 1 && INNER_FUNCTION_OR_CLASS_REGEX.is_match(after) {
if blank_lines_after == 1 && INNER_FUNCTION_OR_CLASS_REGEX.is_match(&after) {
return;
}
@@ -387,7 +387,7 @@ pub fn indent(checker: &mut Checker, definition: &Definition) {
return;
}
let docstring_indent = helpers::indentation(checker, docstring).to_string();
let docstring_indent = helpers::indentation(checker, docstring);
let mut has_seen_tab = docstring_indent.contains('\t');
let mut is_over_indented = true;
let mut over_indented_lines = vec![];
@@ -537,7 +537,7 @@ pub fn newline_after_last_paragraph(checker: &mut Checker, definition: &Definiti
// Insert a newline just before the end-quote(s).
let content = format!(
"\n{}",
helpers::clean(helpers::indentation(checker, docstring))
helpers::clean(&helpers::indentation(checker, docstring))
);
check.amend(Fix::insertion(
content,
@@ -916,7 +916,7 @@ fn blanks_and_section_underline(
// Add a dashed line (of the appropriate length) under the section header.
let content = format!(
"{}{}\n",
helpers::clean(helpers::indentation(checker, docstring)),
helpers::clean(&helpers::indentation(checker, docstring)),
"-".repeat(context.section_name.len())
);
check.amend(Fix::insertion(
@@ -950,7 +950,7 @@ fn blanks_and_section_underline(
// Add a dashed line (of the appropriate length) under the section header.
let content = format!(
"{}{}\n",
helpers::clean(helpers::indentation(checker, docstring)),
helpers::clean(&helpers::indentation(checker, docstring)),
"-".repeat(context.section_name.len())
);
check.amend(Fix::insertion(
@@ -1026,7 +1026,7 @@ fn blanks_and_section_underline(
// Replace the existing underline with a line of the appropriate length.
let content = format!(
"{}{}\n",
helpers::clean(helpers::indentation(checker, docstring)),
helpers::clean(&helpers::indentation(checker, docstring)),
"-".repeat(context.section_name.len())
);
check.amend(Fix::replacement(
@@ -1054,7 +1054,7 @@ fn blanks_and_section_underline(
if checker.settings.enabled.contains(&CheckCode::D215) {
let leading_space = helpers::leading_space(non_empty_line);
let indentation = helpers::indentation(checker, docstring).to_string();
let indentation = helpers::indentation(checker, docstring);
if leading_space.len() > indentation.len() {
let mut check = Check::new(
CheckKind::SectionUnderlineNotOverIndented(context.section_name.to_string()),
@@ -1195,7 +1195,7 @@ fn common_section(
if checker.settings.enabled.contains(&CheckCode::D214) {
let leading_space = helpers::leading_space(context.line);
let indentation = helpers::indentation(checker, docstring).to_string();
let indentation = helpers::indentation(checker, docstring);
if leading_space.len() > indentation.len() {
let mut check = Check::new(
CheckKind::SectionNotOverIndented(context.section_name.to_string()),

View File

@@ -10,16 +10,6 @@ use rustpython_parser::ast::{
use crate::ast::types::{BindingKind, CheckLocator, FunctionScope, Range, Scope, ScopeKind};
use crate::checks::{Check, CheckKind};
/// F634
pub fn if_tuple(test: &Expr, location: Range) -> Option<Check> {
if let ExprKind::Tuple { elts, .. } = &test.node {
if !elts.is_empty() {
return Some(Check::new(CheckKind::IfTuple, location));
}
}
None
}
/// F631
pub fn assert_tuple(test: &Expr, location: Range) -> Option<Check> {
if let ExprKind::Tuple { elts, .. } = &test.node {
@@ -30,6 +20,16 @@ pub fn assert_tuple(test: &Expr, location: Range) -> Option<Check> {
None
}
/// F634
pub fn if_tuple(test: &Expr, location: Range) -> Option<Check> {
if let ExprKind::Tuple { elts, .. } = &test.node {
if !elts.is_empty() {
return Some(Check::new(CheckKind::IfTuple, location));
}
}
None
}
/// F841
pub fn unused_variables(
scope: &Scope,
@@ -78,33 +78,6 @@ pub fn default_except_not_last(handlers: &[Excepthandler]) -> Option<Check> {
None
}
/// F901
pub fn raise_not_implemented(expr: &Expr) -> Option<Check> {
match &expr.node {
ExprKind::Call { func, .. } => {
if let ExprKind::Name { id, .. } = &func.node {
if id == "NotImplemented" {
return Some(Check::new(
CheckKind::RaiseNotImplemented,
Range::from_located(expr),
));
}
}
}
ExprKind::Name { id, .. } => {
if id == "NotImplemented" {
return Some(Check::new(
CheckKind::RaiseNotImplemented,
Range::from_located(expr),
));
}
}
_ => {}
}
None
}
/// F831
pub fn duplicate_arguments(arguments: &Arguments) -> Vec<Check> {
let mut checks: Vec<Check> = vec![];

View File

@@ -2,9 +2,10 @@ use anyhow::Result;
use libcst_native::{Codegen, ImportNames, NameOrAttribute, SmallStatement, Statement};
use rustpython_ast::Stmt;
use crate::ast::types::Range;
use crate::autofix::{helpers, Fix};
use crate::cst::helpers::compose_module_path;
use crate::cst::matchers::match_tree;
use crate::cst::matchers::match_module;
use crate::source_code_locator::SourceCodeLocator;
/// Generate a Fix to remove any unused imports from an `import` statement.
@@ -15,7 +16,8 @@ pub fn remove_unused_imports(
parent: Option<&Stmt>,
deleted: &[&Stmt],
) -> Result<Fix> {
let mut tree = match_tree(locator, stmt)?;
let module_text = locator.slice_source_code_range(&Range::from_located(stmt));
let mut tree = match_module(&module_text)?;
let body = if let Some(Statement::Simple(body)) = tree.body.first_mut() {
body
@@ -72,7 +74,8 @@ pub fn remove_unused_import_froms(
parent: Option<&Stmt>,
deleted: &[&Stmt],
) -> Result<Fix> {
let mut tree = match_tree(locator, stmt)?;
let module_text = locator.slice_source_code_range(&Range::from_located(stmt));
let mut tree = match_module(&module_text)?;
let body = if let Some(Statement::Simple(body)) = tree.body.first_mut() {
body

View File

@@ -4,6 +4,7 @@ use crate::ast::types::{CheckLocator, Range};
use crate::check_ast::Checker;
use crate::pyflakes::checks;
/// F631
pub fn assert_tuple(checker: &mut Checker, stmt: &Stmt, test: &Expr) {
if let Some(check) = checks::assert_tuple(test, checker.locate_check(Range::from_located(stmt)))
{

View File

@@ -4,6 +4,7 @@ use crate::ast::types::{CheckLocator, Range};
use crate::check_ast::Checker;
use crate::pyflakes::checks;
/// F634
pub fn if_tuple(checker: &mut Checker, stmt: &Stmt, test: &Expr) {
if let Some(check) = checks::if_tuple(test, checker.locate_check(Range::from_located(stmt))) {
checker.add_check(check);

View File

@@ -4,6 +4,7 @@ use crate::ast::types::{Binding, BindingKind, Range};
use crate::check_ast::Checker;
use crate::checks::{Check, CheckKind};
/// F633
pub fn invalid_print_syntax(checker: &mut Checker, left: &Expr) {
if let ExprKind::Name { id, .. } = &left.node {
if id == "print" {

View File

@@ -1,7 +1,9 @@
pub use assert_tuple::assert_tuple;
pub use if_tuple::if_tuple;
pub use invalid_print_syntax::invalid_print_syntax;
pub use raise_not_implemented::raise_not_implemented;
mod assert_tuple;
mod if_tuple;
mod invalid_print_syntax;
mod raise_not_implemented;

View File

@@ -0,0 +1,43 @@
use rustpython_ast::{Expr, ExprKind};
use crate::ast::types::{CheckLocator, Range};
use crate::autofix::Fix;
use crate::check_ast::Checker;
use crate::checks::{Check, CheckKind};
fn match_not_implemented(expr: &Expr) -> Option<&Expr> {
match &expr.node {
ExprKind::Call { func, .. } => {
if let ExprKind::Name { id, .. } = &func.node {
if id == "NotImplemented" {
return Some(func);
}
}
}
ExprKind::Name { id, .. } => {
if id == "NotImplemented" {
return Some(expr);
}
}
_ => {}
}
None
}
/// F901
pub fn raise_not_implemented(checker: &mut Checker, expr: &Expr) {
if let Some(expr) = match_not_implemented(expr) {
let mut check = Check::new(
CheckKind::RaiseNotImplemented,
checker.locate_check(Range::from_located(expr)),
);
if checker.patch() {
check.amend(Fix::replacement(
"NotImplementedError".to_string(),
expr.location,
expr.end_location.unwrap(),
))
}
checker.add_check(check);
}
}

View File

@@ -16,14 +16,14 @@ pub fn remove_class_def_base(
bases: &[Expr],
keywords: &[Keyword],
) -> Option<Fix> {
let content = locator.slice_source_code_at(stmt_at);
let contents = locator.slice_source_code_at(stmt_at);
// Case 1: `object` is the only base.
if bases.len() == 1 && keywords.is_empty() {
let mut fix_start = None;
let mut fix_end = None;
let mut count: usize = 0;
for (start, tok, end) in lexer::make_tokenizer(content).flatten() {
for (start, tok, end) in lexer::make_tokenizer(&contents).flatten() {
if matches!(tok, Tok::Lpar) {
if count == 0 {
fix_start = Some(helpers::to_absolute(&start, stmt_at));
@@ -56,7 +56,7 @@ pub fn remove_class_def_base(
let mut fix_start: Option<Location> = None;
let mut fix_end: Option<Location> = None;
let mut seen_comma = false;
for (start, tok, end) in lexer::make_tokenizer(content).flatten() {
for (start, tok, end) in lexer::make_tokenizer(&contents).flatten() {
let start = helpers::to_absolute(&start, stmt_at);
if seen_comma {
if matches!(tok, Tok::Newline) {
@@ -83,7 +83,7 @@ pub fn remove_class_def_base(
// isn't a comma.
let mut fix_start: Option<Location> = None;
let mut fix_end: Option<Location> = None;
for (start, tok, end) in lexer::make_tokenizer(content).flatten() {
for (start, tok, end) in lexer::make_tokenizer(&contents).flatten() {
let start = helpers::to_absolute(&start, stmt_at);
let end = helpers::to_absolute(&end, stmt_at);
if start == expr_at {
@@ -106,7 +106,7 @@ pub fn remove_super_arguments(locator: &SourceCodeLocator, expr: &Expr) -> Optio
let range = Range::from_located(expr);
let contents = locator.slice_source_code_range(&range);
let mut tree = match libcst_native::parse_module(contents, None) {
let mut tree = match libcst_native::parse_module(&contents, None) {
Ok(m) => m,
Err(_) => return None,
};

1652
src/rules/checks.rs Normal file

File diff suppressed because it is too large Load Diff

3
src/rules/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
//! Module for Ruff-specific rules.
pub mod checks;

View File

@@ -0,0 +1,197 @@
---
source: src/linter.rs
expression: checks
---
- kind: UselessExpression
location:
row: 11
column: 4
end_location:
row: 11
column: 6
fix: ~
- kind: UselessExpression
location:
row: 12
column: 4
end_location:
row: 12
column: 5
fix: ~
- kind: UselessExpression
location:
row: 13
column: 4
end_location:
row: 13
column: 7
fix: ~
- kind: UselessExpression
location:
row: 14
column: 4
end_location:
row: 14
column: 10
fix: ~
- kind: UselessExpression
location:
row: 15
column: 4
end_location:
row: 15
column: 8
fix: ~
- kind: UselessExpression
location:
row: 16
column: 4
end_location:
row: 16
column: 9
fix: ~
- kind: UselessExpression
location:
row: 17
column: 4
end_location:
row: 17
column: 8
fix: ~
- kind: UselessExpression
location:
row: 18
column: 4
end_location:
row: 18
column: 10
fix: ~
- kind: UselessExpression
location:
row: 19
column: 4
end_location:
row: 19
column: 10
fix: ~
- kind: UselessExpression
location:
row: 20
column: 4
end_location:
row: 20
column: 18
fix: ~
- kind: UselessExpression
location:
row: 24
column: 4
end_location:
row: 24
column: 7
fix: ~
- kind: UselessExpression
location:
row: 27
column: 4
end_location:
row: 27
column: 5
fix: ~
- kind: UselessExpression
location:
row: 39
column: 4
end_location:
row: 39
column: 6
fix: ~
- kind: UselessExpression
location:
row: 40
column: 4
end_location:
row: 40
column: 5
fix: ~
- kind: UselessExpression
location:
row: 41
column: 4
end_location:
row: 41
column: 7
fix: ~
- kind: UselessExpression
location:
row: 42
column: 4
end_location:
row: 42
column: 10
fix: ~
- kind: UselessExpression
location:
row: 43
column: 4
end_location:
row: 43
column: 8
fix: ~
- kind: UselessExpression
location:
row: 44
column: 4
end_location:
row: 44
column: 9
fix: ~
- kind: UselessExpression
location:
row: 45
column: 4
end_location:
row: 45
column: 8
fix: ~
- kind: UselessExpression
location:
row: 46
column: 4
end_location:
row: 46
column: 10
fix: ~
- kind: UselessExpression
location:
row: 47
column: 4
end_location:
row: 47
column: 10
fix: ~
- kind: UselessExpression
location:
row: 48
column: 4
end_location:
row: 48
column: 18
fix: ~
- kind: UselessExpression
location:
row: 52
column: 4
end_location:
row: 52
column: 7
fix: ~
- kind: UselessExpression
location:
row: 55
column: 4
end_location:
row: 55
column: 5
fix: ~

View File

@@ -0,0 +1,23 @@
---
source: src/linter.rs
expression: checks
---
- kind:
UndefinedName: key
location:
row: 11
column: 8
end_location:
row: 11
column: 13
fix: ~
- kind:
UndefinedName: value
location:
row: 11
column: 15
end_location:
row: 11
column: 22
fix: ~

View File

@@ -8,8 +8,17 @@ expression: checks
column: 10
end_location:
row: 2
column: 26
fix: ~
column: 24
fix:
patch:
content: NotImplementedError
location:
row: 2
column: 10
end_location:
row: 2
column: 24
applied: false
- kind: RaiseNotImplemented
location:
row: 6
@@ -17,5 +26,14 @@ expression: checks
end_location:
row: 6
column: 24
fix: ~
fix:
patch:
content: NotImplementedError
location:
row: 6
column: 10
end_location:
row: 6
column: 24
applied: false

View File

@@ -9,13 +9,13 @@ expression: checks
column: 9
end_location:
row: 2
column: 10
column: 11
fix: ~
- kind:
InvalidEscapeSequence: "."
location:
row: 6
column: 1
column: 0
end_location:
row: 6
column: 2
@@ -27,13 +27,13 @@ expression: checks
column: 5
end_location:
row: 11
column: 6
column: 7
fix: ~
- kind:
InvalidEscapeSequence: _
location:
row: 18
column: 6
column: 5
end_location:
row: 18
column: 7

View File

@@ -0,0 +1,45 @@
---
source: src/linter.rs
expression: checks
---
- kind:
AmbiguousUnicodeCharacter:
- 𝐁
- B
location:
row: 1
column: 5
end_location:
row: 1
column: 6
fix:
patch:
content: B
location:
row: 1
column: 5
end_location:
row: 1
column: 6
applied: false
- kind:
AmbiguousUnicodeCharacter:
-
- )
location:
row: 5
column: 53
end_location:
row: 5
column: 54
fix:
patch:
content: )
location:
row: 5
column: 53
end_location:
row: 5
column: 54
applied: false

View File

@@ -1,115 +1,59 @@
//! Struct used to efficiently slice source code at (row, column) Locations.
use std::borrow::Cow;
use once_cell::unsync::OnceCell;
use ropey::Rope;
use rustpython_ast::Location;
use crate::ast::types::Range;
pub struct SourceCodeLocator<'a> {
contents: &'a str,
offsets: OnceCell<Vec<Vec<usize>>>,
}
pub fn compute_offsets(contents: &str) -> Vec<Vec<usize>> {
let mut offsets = vec![vec![]];
let mut line_index = 0;
let mut char_index = 0;
let mut newline = false;
for (i, char) in contents.char_indices() {
offsets[line_index].push(i);
newline = char == '\n';
if newline {
line_index += 1;
offsets.push(vec![]);
char_index = i + char.len_utf8();
}
}
// If we end in a newline, add an extra character to indicate the start of that
// line.
if newline {
offsets[line_index].push(char_index);
}
offsets
rope: OnceCell<Rope>,
}
impl<'a> SourceCodeLocator<'a> {
pub fn new(contents: &'a str) -> Self {
SourceCodeLocator {
contents,
offsets: OnceCell::new(),
rope: OnceCell::new(),
}
}
fn get_or_init_offsets(&self) -> &Vec<Vec<usize>> {
self.offsets.get_or_init(|| compute_offsets(self.contents))
fn get_or_init_rope(&self) -> &Rope {
self.rope.get_or_init(|| Rope::from_str(self.contents))
}
pub fn slice_source_code_at(&self, location: &Location) -> &'a str {
let offsets = self.get_or_init_offsets();
let offset = offsets[location.row() - 1][location.column()];
&self.contents[offset..]
pub fn slice_source_code_at(&self, location: &Location) -> Cow<'_, str> {
let rope = self.get_or_init_rope();
let offset = rope.line_to_char(location.row() - 1) + location.column();
Cow::from(rope.slice(offset..))
}
pub fn slice_source_code_range(&self, range: &Range) -> &'a str {
let offsets = self.get_or_init_offsets();
let start = offsets[range.location.row() - 1][range.location.column()];
let end = offsets[range.end_location.row() - 1][range.end_location.column()];
&self.contents[start..end]
pub fn slice_source_code_range(&self, range: &Range) -> Cow<'_, str> {
let rope = self.get_or_init_rope();
let start = rope.line_to_char(range.location.row() - 1) + range.location.column();
let end = rope.line_to_char(range.end_location.row() - 1) + range.end_location.column();
Cow::from(rope.slice(start..end))
}
pub fn partition_source_code_at(
&self,
outer: &Range,
inner: &Range,
) -> (&'a str, &'a str, &'a str) {
let offsets = self.get_or_init_offsets();
let outer_start = offsets[outer.location.row() - 1][outer.location.column()];
let outer_end = offsets[outer.end_location.row() - 1][outer.end_location.column()];
let inner_start = offsets[inner.location.row() - 1][inner.location.column()];
let inner_end = offsets[inner.end_location.row() - 1][inner.end_location.column()];
) -> (Cow<'_, str>, Cow<'_, str>, Cow<'_, str>) {
let rope = self.get_or_init_rope();
let outer_start = rope.line_to_char(outer.location.row() - 1) + outer.location.column();
let outer_end =
rope.line_to_char(outer.end_location.row() - 1) + outer.end_location.column();
let inner_start = rope.line_to_char(inner.location.row() - 1) + inner.location.column();
let inner_end =
rope.line_to_char(inner.end_location.row() - 1) + inner.end_location.column();
(
&self.contents[outer_start..inner_start],
&self.contents[inner_start..inner_end],
&self.contents[inner_end..outer_end],
Cow::from(rope.slice(outer_start..inner_start)),
Cow::from(rope.slice(inner_start..inner_end)),
Cow::from(rope.slice(inner_end..outer_end)),
)
}
}
#[cfg(test)]
mod tests {
use crate::source_code_locator::SourceCodeLocator;
#[test]
fn source_code_locator_init() {
let content = "x = 1";
let locator = SourceCodeLocator::new(content);
let offsets = locator.get_or_init_offsets();
assert_eq!(offsets.len(), 1);
assert_eq!(offsets[0], [0, 1, 2, 3, 4]);
let content = "x = 1\n";
let locator = SourceCodeLocator::new(content);
let offsets = locator.get_or_init_offsets();
assert_eq!(offsets.len(), 2);
assert_eq!(offsets[0], [0, 1, 2, 3, 4, 5]);
assert_eq!(offsets[1], [6]);
let content = "x = 1\ny = 2\nz = x + y\n";
let locator = SourceCodeLocator::new(content);
let offsets = locator.get_or_init_offsets();
assert_eq!(offsets.len(), 4);
assert_eq!(offsets[0], [0, 1, 2, 3, 4, 5]);
assert_eq!(offsets[1], [6, 7, 8, 9, 10, 11]);
assert_eq!(offsets[2], [12, 13, 14, 15, 16, 17, 18, 19, 20, 21]);
assert_eq!(offsets[3], [22]);
let content = "# \u{4e9c}\nclass Foo:\n \"\"\".\"\"\"";
let locator = SourceCodeLocator::new(content);
let offsets = locator.get_or_init_offsets();
assert_eq!(offsets.len(), 3);
assert_eq!(offsets[0], [0, 1, 2, 5]);
assert_eq!(offsets[1], [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
assert_eq!(offsets[2], [17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27]);
}
}