Skip to content

Commit

Permalink
Tweak trailing-whitespace-fixer (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 authored Dec 10, 2024
1 parent 5a2c894 commit 21dfcb5
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/builtin/pre_commit_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ pub(crate) async fn fix_trailing_whitespace(
let entry = shlex::split(&hook.entry).ok_or(anyhow::anyhow!("Failed to parse entry"))?;
let args = Args::try_parse_from(entry.iter().chain(&hook.args))?;

// Validate markdown extensions
if args.markdown_linebreak_ext.contains(&String::new()) {
return Err(anyhow::anyhow!("Empty string in --markdown-linebreak-ext"));
}

let force_markdown = args.markdown_linebreak_ext.iter().any(|ext| ext == "*");
let markdown_exts = args
.markdown_linebreak_ext
Expand All @@ -78,7 +73,7 @@ pub(crate) async fn fix_trailing_whitespace(
let chars = if args.chars.is_empty() {
None
} else {
Some(args.chars.iter().collect::<Vec<_>>())
Some(args.chars)
};

// Validate extensions don't contain path separators
Expand All @@ -88,8 +83,7 @@ pub(crate) async fn fix_trailing_whitespace(
.any(|c| matches!(c, '.' | '/' | '\\' | ':'))
{
return Err(anyhow::anyhow!(
"bad --markdown-linebreak-ext extension '{}' (has . / \\ :)",
ext
"bad --markdown-linebreak-ext extension '{ext}' (has . / \\ :)"
));
}
}
Expand Down Expand Up @@ -139,7 +133,7 @@ pub(crate) async fn fix_trailing_whitespace(
{
// Preserve trailing two spaces for markdown, but trim any additional whitespace
let trimmed = if let Some(chars) = chars.as_deref() {
line[..line.len() - 2].trim_end_with(|b| chars.contains(&&b))
line[..line.len() - 2].trim_end_with(|b| chars.contains(&b))
} else {
line[..line.len() - 2].trim_ascii_end()
};
Expand All @@ -149,7 +143,7 @@ pub(crate) async fn fix_trailing_whitespace(
} else {
// Normal whitespace trimming
let trimmed = if let Some(chars) = chars.as_deref() {
line.trim_end_with(|b| chars.contains(&&b))
line.trim_end_with(|b| chars.contains(&b))
} else {
line.trim_ascii_end()
};
Expand Down

0 comments on commit 21dfcb5

Please sign in to comment.