mirror of
https://github.com/astral-sh/ruff.git
synced 2025-12-08 11:20:30 +00:00
## Summary I always found it odd that we had to pass this in, since it's really higher-level context for the error. The awkwardness is further evidenced by the fact that we pass in fake values everywhere (even outside of tests). The source path isn't actually used to display the error; it's only accessed elsewhere to _re-display_ the error in certain cases. This PR modifies to instead pass the path directly in those cases.
30 lines
860 B
Rust
30 lines
860 B
Rust
//! Print the AST for a given Python file.
|
|
#![allow(clippy::print_stdout, clippy::print_stderr)]
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use anyhow::Result;
|
|
|
|
use ruff_linter::source_kind::SourceKind;
|
|
use ruff_python_ast::PySourceType;
|
|
use ruff_python_parser::{parse, AsMode};
|
|
|
|
#[derive(clap::Args)]
|
|
pub(crate) struct Args {
|
|
/// Python file for which to generate the AST.
|
|
#[arg(required = true)]
|
|
file: PathBuf,
|
|
}
|
|
|
|
pub(crate) fn main(args: &Args) -> Result<()> {
|
|
let source_type = PySourceType::from(&args.file);
|
|
let source_kind = SourceKind::from_path(&args.file, source_type)?.ok_or_else(|| {
|
|
anyhow::anyhow!(
|
|
"Could not determine source kind for file: {}",
|
|
args.file.display()
|
|
)
|
|
})?;
|
|
let python_ast = parse(source_kind.source_code(), source_type.as_mode())?;
|
|
println!("{python_ast:#?}");
|
|
Ok(())
|
|
}
|