Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rewrite the linter for spaces before semicolons in Lean #16532

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
31afbee
feat: rewrite the windows line endings linter
grunweg Aug 30, 2024
59ea5e9
chore: make text-based linters return the fixed lines, if any
grunweg Aug 30, 2024
f4c1436
feat: auto-fix line ending warnings
grunweg Aug 30, 2024
6436ad3
feat: when the --fix is given, change the linted module according to …
grunweg Aug 30, 2024
2900855
chore: remove corresponding Python style linter
grunweg Aug 30, 2024
8988620
fix: actually detect line length thingies
grunweg Sep 1, 2024
8ca28f3
Merge branch 'master' into MR-rewrite-lineending-linter
grunweg Sep 2, 2024
4fa03af
Fix merge
grunweg Sep 2, 2024
044cbbf
Merge branch 'master' into MR-rewrite-lineending-linter
grunweg Sep 3, 2024
5196b7d
Review comments.
grunweg Sep 4, 2024
4994d77
Merge branch 'master' into MR-rewrite-lineending-linter
grunweg Sep 5, 2024
4524784
feat: rewrite the trailing whitespace linter in Lean
grunweg Aug 30, 2024
34b6ef3
fix line numbers; better iteration over the array
grunweg Sep 1, 2024
81bfec2
Tweaks.
grunweg Sep 5, 2024
5a7b317
Merge branch 'master' into MR-trailingWhitespaceLinter
grunweg Sep 6, 2024
8863151
Small golf: use Array.zipWithIndex
grunweg Sep 7, 2024
6ef05fb
Merge branch 'master' into MR-trailingWhitespaceLinter
grunweg Sep 18, 2024
1099845
Merge branch 'master' into MR-trailingWhitespaceLinter
grunweg Nov 21, 2024
cae8fab
chore: also use zipWithIndex in the adaptationNoteLinter
grunweg Nov 21, 2024
2f91482
feat: rewrite the linter for the string ' ;' in Lean
grunweg Nov 21, 2024
cf9e63e
Tweak auto-fix and simplify the logic.
grunweg Nov 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions Mathlib/Tactic/Linter/TextBased.lean
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ inductive StyleError where
| adaptationNote
/-- A line ends with windows line endings (\r\n) instead of unix ones (\n). -/
| windowsLineEnding
/-- A line contains trailing whitespace. -/
| trailingWhitespace
/-- A line contains the string " ;" -/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[lint-style (comment with "bot fix style" to have the bot commit all style suggestions)] reported by reviewdog 🐶

Suggested change
/-- A line contains the string " ;" -/
/-- A line contains the string ";" -/

| semicolon
deriving BEq

/-- How to format style errors -/
Expand All @@ -76,13 +80,17 @@ def StyleError.errorMessage (err : StyleError) : String := match err with
"Found the string \"Adaptation note:\", please use the #adaptation_note command instead"
| windowsLineEnding => "This line ends with a windows line ending (\r\n): please use Unix line\
endings (\n) instead"
| trailingWhitespace => "This line ends with some whitespace: please remove this"
| semicolon => "This line contains a space before a semicolon"

/-- The error code for a given style error. Keep this in sync with `parse?_errorContext` below! -/
-- FUTURE: we're matching the old codes in `lint-style.py` for compatibility;
-- in principle, we could also print something more readable.
def StyleError.errorCode (err : StyleError) : String := match err with
| StyleError.adaptationNote => "ERR_ADN"
| StyleError.windowsLineEnding => "ERR_WIN"
| StyleError.trailingWhitespace => "ERR_TWS"
| StyleError.semicolon => "ERR_SEM"

/-- Context for a style error: the actual error, the line number in the file we're reading
and the path to the file. -/
Expand Down Expand Up @@ -160,6 +168,8 @@ def parse?_errorContext (line : String) : Option ErrorContext := Id.run do
-- Use default values for parameters which are ignored for comparing style exceptions.
-- NB: keep this in sync with `compare` above!
| "ERR_ADN" => some (StyleError.adaptationNote)
| "ERR_SEM" => some (StyleError.semicolon)
| "ERR_TWS" => some (StyleError.trailingWhitespace)
| "ERR_WIN" => some (StyleError.windowsLineEnding)
| _ => none
match String.toNat? lineNumber with
Expand Down Expand Up @@ -196,14 +206,37 @@ section
/-- Lint on any occurrences of the string "Adaptation note:" or variants thereof. -/
def adaptationNoteLinter : TextbasedLinter := fun lines ↦ Id.run do
let mut errors := Array.mkEmpty 0
let mut lineNumber := 1
for line in lines do
for (line, idx) in lines.zipWithIndex do
-- We make this shorter to catch "Adaptation note", "adaptation note" and a missing colon.
if line.containsSubstr "daptation note" then
errors := errors.push (StyleError.adaptationNote, lineNumber)
lineNumber := lineNumber + 1
errors := errors.push (StyleError.adaptationNote, idx + 1)
return (errors, none)


/-- Lint a collection of input strings if one of them contains trailing whitespace. -/
def trailingWhitespaceLinter : TextbasedLinter := fun lines ↦ Id.run do
let mut errors := Array.mkEmpty 0
let mut fixedLines := lines
for (line, idx) in lines.zipWithIndex do
if line.back == ' ' then
errors := errors.push (StyleError.trailingWhitespace, idx + 1)
fixedLines := fixedLines.set! idx line.trimRight
return (errors, if errors.size > 0 then some fixedLines else none)


/-- Lint a collection of input strings for the substring " ;". -/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[lint-style (comment with "bot fix style" to have the bot commit all style suggestions)] reported by reviewdog 🐶

Suggested change
/-- Lint a collection of input strings for the substring " ;". -/
/-- Lint a collection of input strings for the substring ";". -/

def semicolonLinter : TextbasedLinter := fun lines ↦ Id.run do
let mut errors := Array.mkEmpty 0
let mut fixedLines := lines
for (line, idx) in lines.zipWithIndex do
let pos := line.find (· == ';')
-- Future: also lint for a semicolon *not* followed by a space or ⟩.
if pos != line.endPos && line.get (line.prev pos) == ' ' then
errors := errors.push (StyleError.semicolon, idx + 1)
fixedLines := fixedLines.set! idx (line.replace " ;" ";")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[lint-style (comment with "bot fix style" to have the bot commit all style suggestions)] reported by reviewdog 🐶

Suggested change
fixedLines := fixedLines.set! idx (line.replace " ;" ";")
fixedLines := fixedLines.set! idx (line.replace ";" ";")

return (errors, if errors.size > 0 then some fixedLines else none)


/-- 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. -/
def isImportsOnlyFile (lines : Array String) : Bool :=
Expand All @@ -215,7 +248,7 @@ end

/-- All text-based linters registered in this file. -/
def allLinters : Array TextbasedLinter := #[
adaptationNoteLinter
adaptationNoteLinter, semicolonLinter, trailingWhitespaceLinter
]


Expand Down Expand Up @@ -257,7 +290,6 @@ def lintFile (path : FilePath) (exceptions : Array ErrorContext) :
(allOutput.flatten.filter (fun e ↦ (e.find?_comparable exceptions).isNone))
return (errors, if changes_made then some changed else none)


/-- Lint a collection of modules for style violations.
Print formatted errors for all unexpected style violations to standard output;
correct automatically fixable style errors if configured so.
Expand Down
21 changes: 1 addition & 20 deletions scripts/lint-style.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@

ERR_IBY = 11 # isolated by
ERR_IWH = 22 # isolated where
ERR_SEM = 13 # the substring " ;"
ERR_TWS = 15 # trailing whitespace
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 @@ -107,15 +105,6 @@ def annotate_strings(enumerate_lines):
continue
yield line_nr, line, *rem, False

def line_endings_check(lines, path):
errors = []
newlines = []
for line_nr, line in lines:
if line.endswith(" \n"):
errors += [(ERR_TWS, line_nr, path)]
line = line.rstrip() + "\n"
newlines.append((line_nr, line))
return errors, newlines

def four_spaces_in_second_line(lines, path):
# TODO: also fix the space for all lines before ":=", right now we only fix the line after
Expand Down Expand Up @@ -207,9 +196,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 @@ -246,10 +232,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_TWS:
output_message(path, line_nr, "ERR_TWS", "Trailing whitespace detected on line")
if errno == ERR_CLN:
output_message(path, line_nr, "ERR_CLN", "Put : and := before line breaks, not after")
if errno == ERR_IND:
Expand All @@ -267,8 +249,7 @@ def lint(path, fix=False):
lines = f.readlines()
enum_lines = enumerate(lines, 1)
newlines = enum_lines
for error_check in [line_endings_check,
four_spaces_in_second_line,
for error_check in [four_spaces_in_second_line,
isolated_by_dot_semicolon_check,
left_arrow_check,
nonterminal_simp_check]:
Expand Down
5 changes: 5 additions & 0 deletions scripts/nolints-style.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ Mathlib/Tactic/Linter/TextBased.lean : line 84 : ERR_ADN : Found the string "Ada
Mathlib/Tactic/Linter/TextBased.lean : line 274 : ERR_ADN : Found the string "Adaptation note:", please use the #adaptation_note command instead
Mathlib/Tactic/Linter/TextBased.lean : line 279 : ERR_ADN : Found the string "Adaptation note:", please use the #adaptation_note command instead
Mathlib/Tactic/Linter/TextBased.lean : line 280 : ERR_ADN : Found the string "Adaptation note:", please use the #adaptation_note command instead

-- The linter for the substring " ;" fires on its own implementation.
Mathlib/Tactic/Linter/TextBased.lean : line 69 : ERR_SEM : This line contains a space before a semicolon
Mathlib/Tactic/Linter/TextBased.lean : line 336 : ERR_SEM : This line contains a space before a semicolon
Mathlib/Tactic/Linter/TextBased.lean : line 345 : ERR_SEM : This line contains a space before a semicolon