Skip to content

Commit e02ec1c

Browse files
committed
Expose a public 'check' method
1 parent 46bdcb9 commit e02ec1c

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

src/lib.rs

+57
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
use std::path::Path;
2+
3+
use anyhow::Result;
4+
use log::debug;
5+
use rustpython_parser::lexer::LexResult;
6+
7+
use crate::autofix::fixer::Mode;
8+
use crate::linter::{check_path, tokenize};
9+
use crate::message::Message;
10+
use crate::settings::Settings;
11+
112
mod ast;
213
mod autofix;
314
pub mod cache;
@@ -13,3 +24,49 @@ pub mod printer;
1324
pub mod pyproject;
1425
mod python;
1526
pub mod settings;
27+
28+
/// Run ruff over Python source code directly.
29+
pub fn check(path: &Path, contents: &str) -> Result<Vec<Message>> {
30+
// Find the project root and pyproject.toml.
31+
let project_root = pyproject::find_project_root(&[path.to_path_buf()]);
32+
match &project_root {
33+
Some(path) => debug!("Found project root at: {:?}", path),
34+
None => debug!("Unable to identify project root; assuming current directory..."),
35+
};
36+
let pyproject = pyproject::find_pyproject_toml(&project_root);
37+
match &pyproject {
38+
Some(path) => debug!("Found pyproject.toml at: {:?}", path),
39+
None => debug!("Unable to find pyproject.toml; using default settings..."),
40+
};
41+
42+
let settings = Settings::from_pyproject(pyproject, project_root)?;
43+
44+
// Tokenize once.
45+
let tokens: Vec<LexResult> = tokenize(contents);
46+
47+
// Determine the noqa line for every line in the source.
48+
let noqa_line_for = noqa::extract_noqa_line_for(&tokens);
49+
50+
// Generate checks.
51+
let checks = check_path(
52+
path,
53+
contents,
54+
tokens,
55+
&noqa_line_for,
56+
&settings,
57+
&Mode::None,
58+
)?;
59+
60+
// Convert to messages.
61+
let messages: Vec<Message> = checks
62+
.into_iter()
63+
.map(|check| Message {
64+
kind: check.kind,
65+
fixed: check.fix.map(|fix| fix.applied).unwrap_or_default(),
66+
location: check.location,
67+
filename: path.to_string_lossy().to_string(),
68+
})
69+
.collect();
70+
71+
Ok(messages)
72+
}

src/linter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::settings::Settings;
1616
use crate::{cache, fs, noqa};
1717

1818
/// Collect tokens up to and including the first error.
19-
fn tokenize(contents: &str) -> Vec<LexResult> {
19+
pub(crate) fn tokenize(contents: &str) -> Vec<LexResult> {
2020
let mut tokens: Vec<LexResult> = vec![];
2121
for tok in lexer::make_tokenizer(contents) {
2222
let is_err = tok.is_err();
@@ -28,7 +28,7 @@ fn tokenize(contents: &str) -> Vec<LexResult> {
2828
tokens
2929
}
3030

31-
fn check_path(
31+
pub(crate) fn check_path(
3232
path: &Path,
3333
contents: &str,
3434
tokens: Vec<LexResult>,

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use ::ruff::logging::set_up_logging;
2323
use ::ruff::message::Message;
2424
use ::ruff::printer::{Printer, SerializationFormat};
2525
use ::ruff::pyproject::{self, StrCheckCodePair};
26+
use ::ruff::settings::CurrentSettings;
2627
use ::ruff::settings::{FilePattern, PerFileIgnore, Settings};
2728
use ::ruff::tell_user;
28-
use ruff::settings::CurrentSettings;
2929

3030
const CARGO_PKG_NAME: &str = env!("CARGO_PKG_NAME");
3131
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");

0 commit comments

Comments
 (0)