-
Notifications
You must be signed in to change notification settings - Fork 1
/
linter
executable file
·111 lines (91 loc) · 3.51 KB
/
linter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
# Helper functions
function print_lined_uncorrectable_issue {
((uncorrectable_issues+=1))
local text="Uncorrectable issue: $1 at '$2' in line $3 in '$4'."
output+=$'\n'"$text"
}
function print_file_uncorrectable_issue {
((uncorrectable_issues+=1))
local text="Uncorrectable issue: $1 in file '$2'"
output+=$'\n'"$text"
}
function print_lined_correctable_issue {
((correctable_issues+=1))
local text="Correctable issue: $1 at '$2' in line $3 in '$4'."
output+=$'\n'"$text"
}
function print_file_correctable_issue {
((correctable_issues+=1))
local text="Correctable issue: $1 in file '$2'"
output+=$'\n'"$text"
}
function print_welcome {
echo "Issues marked as uncorrectable are not fixable by the auto-linter"
echo "Issues marked as correctable are probably fixable by the auto-linter"
}
# Linting Functions
function lint_st {
if [[ $1 =~ "monticello.meta/initializers.st" ]]; then
return
fi
# echo . used to preserve trailing newlines
local content="$(cat "$1"; echo .)"
content="${content%.}"
if (echo -n "$content" | grep "This is an auto-generated method" > /dev/null); then
return
fi
local line_number=1
while IFS= read -r line
do
[[ $line =~ ^.{81,1000}$ ]] && print_lined_uncorrectable_issue "Line too long" "$line" "$line_number" "$1"
[[ $line =~ ([^[:space:]]@)|(@[^[:space:]]) ]] && print_lined_correctable_issue "No spaces around '@'" "$line" "$line_number" "$1"
[[ $line =~ ([aA])([[:space:]]?M2) ]] && print_lined_correctable_issue "Usage of 'aM2', 'a M2', 'A M2' or 'AM2'" "$line" "$line_number" "$1"
[[ $line =~ ([[:space:]])\) ]] && print_lined_correctable_issue "No spaces around brackets" "$line" "$line_number" "$1"
[[ $line =~ \(([[:space:]]) ]] && print_lined_correctable_issue "No spaces around brackets" "$line" "$line_number" "$1"
[[ $line =~ [^[:space:]][[:space:]]$ ]] && print_lined_correctable_issue "Trailing space" "$line" "$line_number" "$1"
((line_number+=1))
done <<< "$content"
[[ $content =~ ^([^[]+?)([[:space:]]{3,})(\|) ]] && print_file_correctable_issue "Empty lines before method local variables" "$1"
[[ $content =~ ^([^[]+?)(\|)([[:space:]]{3,}) ]] && print_file_correctable_issue "Empty lines before method local variables" "$1"
[[ $content =~ (\.)([[:space:]]*)$ ]] && print_file_correctable_issue "Final point" "$1"
# bash doesn't support lookarounds in regexes, use grep with -z (effectively treats input as one line) instead
(echo -n "$content" | grep -zP '\S\n(?![\s\S])' >/dev/null) || print_file_correctable_issue "More or less than one trailing newline" "$1"
}
function lint_md {
local content=$(<"$1")
local line_number=1
while IFS= read -r line
do
[[ $line =~ ([aA])([[:space:]]?M2) ]] && print_lined_correctable_issue "Usage of 'aM2', 'a M2', 'A M2' or 'AM2'" "$line" "$line_number" "$1"
((line_number+=1))
done <<< "$content"
[[ $content =~ ^[[:space:]]*$ ]] && print_file_uncorrectable_issue "Empty class comment" "$1"
}
# Main script
print_welcome
uncorrectable_issues=0
correctable_issues=0
output=""
echo "Linting..."
while IFS= read -rd '' f
do
lint_st "$f"
done < <(git ls-files '*.st' -z)
while IFS= read -rd '' f
do
lint_md "$f"
done < <(git ls-files '*.md' -z)
total_issues=0
((total_issues+=uncorrectable_issues))
((total_issues+=correctable_issues))
if (( total_issues == 0 ))
then
echo "No issues found."
exit 0
else
echo "Linting results:"
echo "$output"
echo "Uncorrectable issues: $uncorrectable_issues; correctable issues: $correctable_issues"
exit 0 # Temporarily un-fail until there is a way to ignore certain rules on lines
fi