Remove hidden autoformat command (#1486)

This commit is contained in:
Charlie Marsh 2022-12-30 15:32:05 -05:00 committed by GitHub
parent 7a66f98590
commit 4c2fbb7ac0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 108 deletions

View File

@ -350,6 +350,10 @@ Options:
List of mappings from file pattern to code to exclude
--format <FORMAT>
Output serialization format for error messages [possible values: text, json, junit, grouped, github, gitlab]
--stdin-filename <STDIN_FILENAME>
The name of the file when passing it through stdin
--cache-dir <CACHE_DIR>
Path to the cache directory
--show-source
Show violations with source code
--respect-gitignore
@ -358,12 +362,6 @@ Options:
Enforce exclusions, even for paths passed to Ruff directly on the command-line
--update-check
Enable or disable automatic update checks
--show-files
See the files Ruff will be run against with the current settings
--show-settings
See the settings Ruff will use to check a given Python file
--add-noqa
Enable automatic additions of `noqa` directives to failing lines
--dummy-variable-rgx <DUMMY_VARIABLE_RGX>
Regular expression matching the name of dummy variables
--target-version <TARGET_VERSION>
@ -372,12 +370,16 @@ Options:
Set the line-length for length-associated checks and automatic formatting
--max-complexity <MAX_COMPLEXITY>
Maximum McCabe complexity allowed for a given function
--stdin-filename <STDIN_FILENAME>
The name of the file when passing it through stdin
--add-noqa
Enable automatic additions of `noqa` directives to failing lines
--clean
Clear any caches in the current directory or any subdirectories
--explain <EXPLAIN>
Explain a rule
--cache-dir <CACHE_DIR>
Path to the cache directory
--show-files
See the files Ruff will be run against with the current settings
--show-settings
See the settings Ruff will use to check a given Python file
-h, --help
Print help information
-V, --version

View File

@ -138,7 +138,6 @@ pub struct Cli {
#[arg(
long,
// conflicts_with = "add_noqa",
conflicts_with = "autoformat",
conflicts_with = "clean",
conflicts_with = "explain",
conflicts_with = "generate_shell_completion",
@ -149,29 +148,11 @@ pub struct Cli {
conflicts_with = "watch",
)]
pub add_noqa: bool,
/// Round-trip auto-formatting.
#[arg(
long,
hide = true,
// Fake subcommands.
conflicts_with = "add_noqa",
// conflicts_with = "autoformat",
conflicts_with = "clean",
conflicts_with = "explain",
conflicts_with = "generate_shell_completion",
conflicts_with = "show_files",
conflicts_with = "show_settings",
// Unsupported default-command arguments.
conflicts_with = "stdin_filename",
conflicts_with = "watch",
)]
pub autoformat: bool,
/// Clear any caches in the current directory or any subdirectories.
#[arg(
long,
// Fake subcommands.
conflicts_with = "add_noqa",
conflicts_with = "autoformat",
// conflicts_with = "clean",
conflicts_with = "explain",
conflicts_with = "generate_shell_completion",
@ -187,7 +168,6 @@ pub struct Cli {
long,
// Fake subcommands.
conflicts_with = "add_noqa",
conflicts_with = "autoformat",
conflicts_with = "clean",
// conflicts_with = "explain",
conflicts_with = "generate_shell_completion",
@ -205,7 +185,6 @@ pub struct Cli {
value_name = "SHELL",
// Fake subcommands.
conflicts_with = "add_noqa",
conflicts_with = "autoformat",
conflicts_with = "clean",
conflicts_with = "explain",
// conflicts_with = "generate_shell_completion",
@ -221,7 +200,6 @@ pub struct Cli {
long,
// Fake subcommands.
conflicts_with = "add_noqa",
conflicts_with = "autoformat",
conflicts_with = "clean",
conflicts_with = "explain",
conflicts_with = "generate_shell_completion",
@ -237,7 +215,6 @@ pub struct Cli {
long,
// Fake subcommands.
conflicts_with = "add_noqa",
conflicts_with = "autoformat",
conflicts_with = "clean",
conflicts_with = "explain",
conflicts_with = "generate_shell_completion",
@ -257,7 +234,6 @@ impl Cli {
(
Arguments {
add_noqa: self.add_noqa,
autoformat: self.autoformat,
clean: self.clean,
config: self.config,
diff: self.diff,
@ -319,7 +295,6 @@ fn resolve_bool_arg(yes: bool, no: bool) -> Option<bool> {
#[allow(clippy::struct_excessive_bools)]
pub struct Arguments {
pub add_noqa: bool,
pub autoformat: bool,
pub clean: bool,
pub config: Option<PathBuf>,
pub diff: bool,

View File

@ -20,7 +20,7 @@ use crate::cache::DEFAULT_CACHE_DIR_NAME;
use crate::checks::{CheckCode, CheckKind};
use crate::cli::Overrides;
use crate::iterators::par_iter;
use crate::linter::{add_noqa_to_path, autoformat_path, lint_path, lint_stdin, Diagnostics};
use crate::linter::{add_noqa_to_path, lint_path, lint_stdin, Diagnostics};
use crate::logging::LogLevel;
use crate::message::Message;
use crate::resolver::{FileDiscovery, PyprojectDiscovery};
@ -210,45 +210,6 @@ pub fn add_noqa(
Ok(modifications)
}
/// Automatically format a collection of files.
pub fn autoformat(
files: &[PathBuf],
pyproject_strategy: &PyprojectDiscovery,
file_strategy: &FileDiscovery,
overrides: &Overrides,
) -> Result<usize> {
// Collect all the files to format.
let start = Instant::now();
let (paths, resolver) =
resolver::python_files_in_path(files, pyproject_strategy, file_strategy, overrides)?;
let duration = start.elapsed();
debug!("Identified files to lint in: {:?}", duration);
// Validate the `Settings` and return any errors.
resolver.validate(pyproject_strategy)?;
let start = Instant::now();
let modifications = par_iter(&paths)
.flatten()
.filter_map(|entry| {
let path = entry.path();
let settings = resolver.resolve(path, pyproject_strategy);
match autoformat_path(path, settings) {
Ok(()) => Some(()),
Err(e) => {
error!("Failed to autoformat {}: {e}", path.to_string_lossy());
None
}
}
})
.count();
let duration = start.elapsed();
debug!("Auto-formatted files in: {:?}", duration);
Ok(modifications)
}
/// Print the user-facing configuration settings.
pub fn show_settings(
files: &[PathBuf],

View File

@ -23,7 +23,6 @@ use crate::directives::Directives;
use crate::message::{Message, Source};
use crate::noqa::add_noqa;
use crate::settings::{flags, Settings};
use crate::source_code_generator::SourceCodeGenerator;
use crate::source_code_locator::SourceCodeLocator;
use crate::source_code_style::SourceCodeStyleDetector;
use crate::{cache, directives, fs, rustpython_helpers};
@ -290,32 +289,6 @@ pub fn add_noqa_to_path(path: &Path, settings: &Settings) -> Result<usize> {
)
}
/// Apply autoformatting to the source code at the given `Path`.
pub fn autoformat_path(path: &Path, settings: &Settings) -> Result<()> {
// Validate the `Settings` and return any errors.
settings.validate()?;
// Read the file from disk.
let contents = fs::read_file(path)?;
// Tokenize once.
let tokens: Vec<LexResult> = rustpython_helpers::tokenize(&contents);
// Map row and column locations to byte slices (lazily).
let locator = SourceCodeLocator::new(&contents);
// Detect the current code style (lazily).
let stylist = SourceCodeStyleDetector::from_contents(&contents, &locator);
// Generate the AST.
let python_ast = rustpython_helpers::parse_program_tokens(tokens, "<filename>")?;
let mut generator = SourceCodeGenerator::new(stylist.indentation(), stylist.quote());
generator.unparse_suite(&python_ast);
write(path, generator.generate()?)?;
Ok(())
}
/// Generate a list of `Check` violations from source code content derived from
/// stdin.
pub fn lint_stdin(

View File

@ -235,12 +235,6 @@ pub(crate) fn inner_main() -> Result<ExitCode> {
if modifications > 0 && log_level >= LogLevel::Default {
println!("Added {modifications} noqa directives.");
}
} else if cli.autoformat {
let modifications =
commands::autoformat(&cli.files, &pyproject_strategy, &file_strategy, &overrides)?;
if modifications > 0 && log_level >= LogLevel::Default {
println!("Formatted {modifications} files.");
}
} else {
let is_stdin = cli.files == vec![PathBuf::from("-")];