Skip to content

Commit

Permalink
wip: rewrite semicolon linter also. TODO perf-test, see if I need to …
Browse files Browse the repository at this point in the history
…speed up...
  • Loading branch information
grunweg committed Sep 5, 2024
1 parent fa9b485 commit d11b81c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
18 changes: 17 additions & 1 deletion Mathlib/Tactic/Linter/TextBased.lean
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,29 @@ def broadImportsLinter : TextbasedLinter := fun lines ↦ Id.run do
def trailingWhitespaceLinter : TextbasedLinter := fun lines ↦ Id.run do
let mut errors := Array.mkEmpty 0
let mut fixedLines := lines
for i in [0::lines.size -1] do
for i in [0::lines.size - 1] do
let line := lines[i]
if line.back == ' ' then
errors := errors.push (StyleError.trailingWhitespace, i + 1)
fixedLines := fixedLines.set! 0 (line.dropRightWhile (· == ' '))
return (errors, fixedLines)
-- xxx: can I avoid set!, as I know the index is inbounds? is there a better way?
-- ask on zulip for the most idiomatic construct

-- TODO: apply the same change to all other linters, once this has been tested...

def semicolonLinter : TextbaseLinter := fun lines ↦ Id.run do
let mut errors := Array.mkEmpty 0
let mut fixedLines := lines
for i in [0::lines.size - 1] do
let line := lines[i]
if line.contains ';' then
let replaced := (line.replace " ;" "; ").replace " " " "
if replaced != line then
errors := errors.push (StyleError.semicolon, i + 1) -- TODO: add error variant etc!
-- "Line contains a space before a semicolon"
fixedLines := fixedLines.set! 0 replaced
return (errors, fixedLines)

/-- Whether a collection of lines consists *only* of imports, blank lines and single-line comments.
In practice, this means it's an imports-only file and exempt from almost all linting. -/
Expand Down
6 changes: 0 additions & 6 deletions scripts/lint-style.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
ERR_MOD = 2 # module docstring
ERR_IBY = 11 # isolated by
ERR_IWH = 22 # isolated where
ERR_SEM = 13 # the substring " ;"
ERR_CLN = 16 # line starts with a colon
ERR_IND = 17 # second line not correctly indented
ERR_ARR = 18 # space after "←"
Expand Down Expand Up @@ -231,9 +230,6 @@ def isolated_by_dot_semicolon_check(lines, path):
line = f"{indent}{line.lstrip()[3:]}"
elif line.lstrip() == "where":
errors += [(ERR_IWH, line_nr, path)]
if " ;" in line:
errors += [(ERR_SEM, line_nr, path)]
line = line.replace(" ;", ";")
if line.lstrip().startswith(":"):
errors += [(ERR_CLN, line_nr, path)]
newlines.append((line_nr, line))
Expand Down Expand Up @@ -272,8 +268,6 @@ def format_errors(errors):
output_message(path, line_nr, "ERR_IBY", "Line is an isolated 'by'")
if errno == ERR_IWH:
output_message(path, line_nr, "ERR_IWH", "Line is an isolated where")
if errno == ERR_SEM:
output_message(path, line_nr, "ERR_SEM", "Line contains a space before a semicolon")
if errno == ERR_CLN:
output_message(path, line_nr, "ERR_CLN", "Put : and := before line breaks, not after")
if errno == ERR_IND:
Expand Down

0 comments on commit d11b81c

Please sign in to comment.