Skip to content

Commit

Permalink
Add pyproject.toml support (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Aug 20, 2022
1 parent b7d7c50 commit 7359e86
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Local cache
.cache
resources/test
!resources/test/src
resources/test

###
# Rust.gitignore
Expand Down
29 changes: 28 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ chrono = { version = "0.4.21" }
clap = { version = "3.2.16", features = ["derive"] }
clearscreen = { version = "1.0.10" }
colored = { version = "2.0.0" }
common-path = "1.0.0"
dirs = "4.0.0"
fern = { version = "0.6.1" }
lazy_static = { version = "1.4.0" }
log = { version = "0.4.17" }
Expand All @@ -25,6 +27,7 @@ regex = { version = "1.6.0" }
rustpython-parser = { git = "https://github.com/RustPython/RustPython.git", rev = "dff916d45c5d13074d21ad329a5ab68a6499426a" }
serde = { version = "1.0.143", features = ["derive"] }
serde_json = { version = "1.0.83" }
toml = { version = "0.5.9"}
walkdir = { version = "2.3.2" }

[profile.release]
Expand Down
2 changes: 2 additions & 0 deletions resources/test/src/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.linter]
line-length = 88
17 changes: 11 additions & 6 deletions src/bin/rust_python_linter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::mpsc::channel;
use std::time::Instant;

Expand All @@ -15,6 +15,7 @@ use ::rust_python_linter::linter::check_path;
use ::rust_python_linter::logging::set_up_logging;
use ::rust_python_linter::message::Message;
use ::rust_python_linter::tell_user;
use rust_python_linter::settings::Settings;

#[derive(Debug, Parser)]
#[clap(name = "rust-python-linter")]
Expand All @@ -30,7 +31,7 @@ struct Cli {
no_cache: bool,
}

fn run_once(files: &[PathBuf], cache: bool) -> Result<Vec<Message>> {
fn run_once(files: &[PathBuf], settings: &Settings, cache: bool) -> Result<Vec<Message>> {
// Collect all the files to check.
let start = Instant::now();
let files: Vec<DirEntry> = files.iter().flat_map(iter_python_files).collect();
Expand All @@ -41,7 +42,7 @@ fn run_once(files: &[PathBuf], cache: bool) -> Result<Vec<Message>> {
let messages: Vec<Message> = files
.par_iter()
.map(|entry| {
check_path(entry.path(), &cache.into()).unwrap_or_else(|e| {
check_path(entry.path(), settings, &cache.into()).unwrap_or_else(|e| {
error!("Failed to check {}: {e:?}", entry.path().to_string_lossy());
vec![]
})
Expand Down Expand Up @@ -88,12 +89,16 @@ fn main() -> Result<()> {

set_up_logging(cli.verbose)?;

// TODO(charlie): Avoid this cast.
let paths: Vec<&Path> = cli.files.iter().map(PathBuf::as_path).collect();
let settings = Settings::from_paths(paths)?;

if cli.watch {
// Perform an initial run instantly.
clearscreen::clear()?;
tell_user!("Starting linter in watch mode...\n");

let messages = run_once(&cli.files, !cli.no_cache)?;
let messages = run_once(&cli.files, &settings, !cli.no_cache)?;
report_continuously(&messages)?;

// Configure the file watcher.
Expand All @@ -111,7 +116,7 @@ fn main() -> Result<()> {
clearscreen::clear()?;
tell_user!("File change detected...\n");

let messages = run_once(&cli.files, !cli.no_cache)?;
let messages = run_once(&cli.files, &settings, !cli.no_cache)?;
report_continuously(&messages)?;
}
}
Expand All @@ -120,7 +125,7 @@ fn main() -> Result<()> {
}
}
} else {
let messages = run_once(&cli.files, !cli.no_cache)?;
let messages = run_once(&cli.files, &settings, !cli.no_cache)?;
report_once(&messages)?;
}

Expand Down
8 changes: 4 additions & 4 deletions src/check_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ use rustpython_parser::ast::Location;

use crate::checks::Check;
use crate::checks::CheckKind::LineTooLong;
use crate::settings::MAX_LINE_LENGTH;
use crate::settings::Settings;

pub fn check_lines(contents: &str) -> Vec<Check> {
pub fn check_lines(contents: &str, settings: &Settings) -> Vec<Check> {
contents
.lines()
.enumerate()
.filter_map(|(row, line)| {
if line.len() > *MAX_LINE_LENGTH {
if line.len() > settings.line_length {
let chunks: Vec<&str> = line.split_whitespace().collect();
if chunks.len() == 1 || (chunks.len() == 2 && chunks[0] == "#") {
None
} else {
Some(Check {
kind: LineTooLong,
location: Location::new(row + 1, MAX_LINE_LENGTH + 1),
location: Location::new(row + 1, settings.line_length + 1),
})
}
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use anyhow::Result;
use lazy_static::lazy_static;
use std::collections::HashSet;
use std::fs::File;
use std::io::{BufRead, BufReader, Read};
use std::ops::Deref;
use std::path::{Path, PathBuf};

use anyhow::Result;
use lazy_static::lazy_static;
use walkdir::{DirEntry, WalkDir};

lazy_static! {
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ pub mod fs;
pub mod linter;
pub mod logging;
pub mod message;
mod settings;
mod pyproject;
pub mod settings;
mod visitor;
12 changes: 9 additions & 3 deletions src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use rustpython_parser::parser;
use crate::check_ast::check_ast;
use crate::check_lines::check_lines;
use crate::message::Message;
use crate::settings::Settings;
use crate::{cache, fs};

pub fn check_path(path: &Path, mode: &cache::Mode) -> Result<Vec<Message>> {
pub fn check_path(path: &Path, settings: &Settings, mode: &cache::Mode) -> Result<Vec<Message>> {
// Check the cache.
if let Some(messages) = cache::get(path, mode) {
debug!("Cache hit for: {}", path.to_string_lossy());
Expand All @@ -23,7 +24,7 @@ pub fn check_path(path: &Path, mode: &cache::Mode) -> Result<Vec<Message>> {
let python_ast = parser::parse_program(&contents)?;
let messages: Vec<Message> = check_ast(&python_ast)
.into_iter()
.chain(check_lines(&contents))
.chain(check_lines(&contents, settings))
.map(|check| Message {
kind: check.kind,
location: check.location,
Expand All @@ -43,17 +44,18 @@ mod tests {
use anyhow::Result;
use rustpython_parser::ast::Location;

use crate::cache;
use crate::checks::CheckKind::{
DuplicateArgumentName, FStringMissingPlaceholders, IfTuple, ImportStarUsage, LineTooLong,
};
use crate::linter::check_path;
use crate::message::Message;
use crate::{cache, settings};

#[test]
fn duplicate_argument_name() -> Result<()> {
let actual = check_path(
&Path::new("./resources/test/src/duplicate_argument_name.py"),
&settings::Settings { line_length: 88 },
&cache::Mode::None,
)?;
let expected = vec![
Expand Down Expand Up @@ -85,6 +87,7 @@ mod tests {
fn f_string_missing_placeholders() -> Result<()> {
let actual = check_path(
&Path::new("./resources/test/src/f_string_missing_placeholders.py"),
&settings::Settings { line_length: 88 },
&cache::Mode::None,
)?;
let expected = vec![
Expand Down Expand Up @@ -116,6 +119,7 @@ mod tests {
fn if_tuple() -> Result<()> {
let actual = check_path(
&Path::new("./resources/test/src/if_tuple.py"),
&settings::Settings { line_length: 88 },
&cache::Mode::None,
)?;
let expected = vec![
Expand All @@ -142,6 +146,7 @@ mod tests {
fn import_star_usage() -> Result<()> {
let actual = check_path(
&Path::new("./resources/test/src/import_star_usage.py"),
&settings::Settings { line_length: 88 },
&cache::Mode::None,
)?;
let expected = vec![
Expand All @@ -168,6 +173,7 @@ mod tests {
fn line_too_long() -> Result<()> {
let actual = check_path(
&Path::new("./resources/test/src/line_too_long.py"),
&settings::Settings { line_length: 88 },
&cache::Mode::None,
)?;
let expected = vec![Message {
Expand Down
Loading

0 comments on commit 7359e86

Please sign in to comment.