Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ For more help, you can type
## Rules

Here is a non-comprehensive set of rules that are enforced:
* Punctuation should be followed by a space, this include `,`, `;` and `)`.
* Punctuation should be followed by a space, this includes `,`, `;` and `)`.
* Binary operators (`==`, `+`, ...) should be surrounded by spaces
* The following special characters are surrounded by at least one space: `::`, `=`.
* A line should not exceed 120 characters (this is somehow already extreme). The maximum line length can be controlled from the CLI.
Expand All @@ -48,6 +48,7 @@ Here is a non-comprehensive set of rules that are enforced:
* `print` statements should look like `print *, "something"`
* `write` statements should look like `write(*, *) "something"`
* Lines should be indented consistently (by default, using an indentation of 4 spaces)
* [FORD](https://forddocs.readthedocs.io/en/latest/) Compatibility: `!!` and `!>` are preserved and treated as comments like `!` with one space after and at least one space before.

# TODO list

Expand Down
9 changes: 7 additions & 2 deletions fortran_linter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ class FortranRules:
(r"\t", " ", "Should use 2 spaces instead of tabulation"),
# Fix "foo! comment" to "foo ! comment"
(r"(\w)(\!(?!\$)|\!\$)", r"\1 \2", "At least one space before comment"),
# Fix "!bar" to "! bar"
(r"\!(|\s\s+)(?!\$)(\S)", r"! \2", "Exactly one space after comment"),
# Enforce space after comments (but ignoring !$):
# Fix "<>bar" to "<> bar" where <> can be !, !!, !> (FORD Documentation)
(
r"(![!>#]?(?:(?=[^\s!>#$]|(\s\s)|\s\$)|\$(?!\S)))\s*(.*)",
r"\1 \3",
"Exactly one space after comment",
),
# Remove trailing ";"
(r";\s*$", r"\n", 'Useless ";" at end of line'),
[
Expand Down
4 changes: 4 additions & 0 deletions tests/test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ end module merger_commons

a = 2!bar - missing spaces around '!'
a = 2 ! bar - too many spaces after '!'
a = 2!!bar - missing spaces around FORD '!!'
a = 2!>bar - missing spaces around FORD '!>'
a = 2 !! bar - too many spaces after FORD '!!'
a = 2 !> bar - too many spaces after FORD '!>'

integer*4 :: foo ! old syntax, should become integer(4)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_comment_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def test_fortran_comment_detection():
("! test", 0),
(" !test", 4),
("'Contains a string', ! and a comment", 21),
("!! test", 0),
(" !!test", 4),
("'Contains a string', !! and a comment", 21),
("!> test", 0),
(" !>test", 4),
("'Contains a string', !> and a comment", 21),
)

no_comment_lines = (
Expand Down
4 changes: 4 additions & 0 deletions tests/test_reference.f90
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ end module merger_commons

a = 2 ! bar - missing spaces around '!'
a = 2 ! bar - too many spaces after '!'
a = 2 !! bar - missing spaces around FORD '!!'
a = 2 !> bar - missing spaces around FORD '!>'
a = 2 !! bar - too many spaces after FORD '!!'
a = 2 !> bar - too many spaces after FORD '!>'

integer(4) :: foo ! old syntax, should become integer(4)

Expand Down