Skip to content

Commit

Permalink
Wrap contrib/check-whitespace.jl in a function (#53468)
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano authored Feb 26, 2024
1 parent 4b47cc4 commit b18824a
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions contrib/check-whitespace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,43 @@ allow_tabs(path) =

const errors = Set{Tuple{String,Int,String}}()

for path in eachline(`git ls-files -- $patterns`)
lineno = 0
non_blank = 0
function check_whitespace()
for path in eachline(`git ls-files -- $patterns`)
lineno = 0
non_blank = 0

file_err(msg) = push!(errors, (path, 0, msg))
line_err(msg) = push!(errors, (path, lineno, msg))
file_err(msg) = push!(errors, (path, 0, msg))
line_err(msg) = push!(errors, (path, lineno, msg))

isfile(path) || continue
for line in eachline(path, keep=true)
lineno += 1
contains(line, '\r') && file_err("non-UNIX line endings")
contains(line, '\ua0') && line_err("non-breaking space")
allow_tabs(path) ||
contains(line, '\t') && line_err("tab")
endswith(line, '\n') || line_err("no trailing newline")
line = chomp(line)
endswith(line, r"\s") && line_err("trailing whitespace")
contains(line, r"\S") && (non_blank = lineno)
isfile(path) || continue
for line in eachline(path, keep=true)
lineno += 1
contains(line, '\r') && file_err("non-UNIX line endings")
contains(line, '\ua0') && line_err("non-breaking space")
allow_tabs(path) ||
contains(line, '\t') && line_err("tab")
endswith(line, '\n') || line_err("no trailing newline")
line = chomp(line)
endswith(line, r"\s") && line_err("trailing whitespace")
contains(line, r"\S") && (non_blank = lineno)
end
non_blank < lineno && line_err("trailing blank lines")
end
non_blank < lineno && line_err("trailing blank lines")
end

if isempty(errors)
println(stderr, "Whitespace check found no issues.")
exit(0)
else
println(stderr, "Whitespace check found $(length(errors)) issues:")
for (path, lineno, msg) in sort!(collect(errors))
if lineno == 0
println(stderr, "$path -- $msg")
else
println(stderr, "$path:$lineno -- $msg")
if isempty(errors)
println(stderr, "Whitespace check found no issues.")
exit(0)
else
println(stderr, "Whitespace check found $(length(errors)) issues:")
for (path, lineno, msg) in sort!(collect(errors))
if lineno == 0
println(stderr, "$path -- $msg")
else
println(stderr, "$path:$lineno -- $msg")
end
end
exit(1)
end
exit(1)
end

check_whitespace()

0 comments on commit b18824a

Please sign in to comment.