## Summary Enable using the new `Mode::Jupyter` for the tokenizer/parser to parse Jupyter line magic tokens. The individual call to the lexer i.e., `lex_starts_at` done by various rules should consider the context of the source code (is this content from a Jupyter Notebook?). Thus, a new field `source_type` (of type `PySourceType`) is added to `Checker` which is being passed around as an argument to the relevant functions. This is then used to determine the `Mode` for the lexer. ## Test Plan Add new test cases to make sure that the magic statement is considered while generating the diagnostic and autofix: * For `I001`, if there's a magic statement in between two import blocks, they should be sorted independently fixes: #6090
31 lines
751 B
Rust
31 lines
751 B
Rust
//! Print the AST for a given Python file.
|
|
#![allow(clippy::print_stdout, clippy::print_stderr)]
|
|
|
|
use std::fs;
|
|
use std::path::PathBuf;
|
|
|
|
use anyhow::Result;
|
|
use ruff_python_parser::{parse, Mode};
|
|
|
|
#[derive(clap::Args)]
|
|
pub(crate) struct Args {
|
|
/// Python file for which to generate the AST.
|
|
#[arg(required = true)]
|
|
file: PathBuf,
|
|
/// Run in Jupyter mode i.e., allow line magics.
|
|
#[arg(long)]
|
|
jupyter: bool,
|
|
}
|
|
|
|
pub(crate) fn main(args: &Args) -> Result<()> {
|
|
let contents = fs::read_to_string(&args.file)?;
|
|
let mode = if args.jupyter {
|
|
Mode::Jupyter
|
|
} else {
|
|
Mode::Module
|
|
};
|
|
let python_ast = parse(&contents, mode, &args.file.to_string_lossy())?;
|
|
println!("{python_ast:#?}");
|
|
Ok(())
|
|
}
|