-
Notifications
You must be signed in to change notification settings - Fork 354
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
base: master
Are you sure you want to change the base?
Changes from all commits
31afbee
59ea5e9
f4c1436
6436ad3
2900855
8988620
8ca28f3
4fa03af
044cbbf
5196b7d
4994d77
4524784
34b6ef3
81bfec2
5a7b317
8863151
6ef05fb
1099845
cae8fab
2f91482
cf9e63e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 " ;" -/ | ||||||
| semicolon | ||||||
deriving BEq | ||||||
|
||||||
/-- How to format style errors -/ | ||||||
|
@@ -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. -/ | ||||||
|
@@ -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 | ||||||
|
@@ -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 " ;". -/ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
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 " ;" ";") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
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 := | ||||||
|
@@ -215,7 +248,7 @@ end | |||||
|
||||||
/-- All text-based linters registered in this file. -/ | ||||||
def allLinters : Array TextbasedLinter := #[ | ||||||
adaptationNoteLinter | ||||||
adaptationNoteLinter, semicolonLinter, trailingWhitespaceLinter | ||||||
] | ||||||
|
||||||
|
||||||
|
@@ -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. | ||||||
|
There was a problem hiding this comment.
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 🐶