summaryrefslogtreecommitdiff
path: root/custom-checks
blob: 798049e388cd776cf3575b8f2dea9e0c304b3bf2 (plain)
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
#!/bin/bash -eu

# This Bash script implements custom sanity checks for scripts beyond what
# Vint covers, which are easy to check with regex.

# A flag for automatically fixing some errors.
FIX_ERRORS=0
RETURN_CODE=0

function print_help() {
    echo "Usage: ./custom-checks [--fix] [DIRECTORY]" 1>&2
    echo 1>&2
    echo "  -h, --help    Print this help text" 1>&2
    echo "      --fix     Automatically fix some errors" 1>&2
    exit 1
}

while [ $# -ne 0 ]; do
    case $1 in
    -h) ;& --help)
        print_help
    ;;
    --fix)
        FIX_ERRORS=1
        shift
    ;;
    --)
        shift
        break
    ;;
    -?*)
        echo "Invalid argument: $1" 1>&2
        exit 1
    ;;
    *)
        break
    ;;
    esac
done

if [ $# -eq 0 ] || [ -z "$1" ]; then
    print_help
fi

# Called to output an error.
# If called at least one, the return code for this script will be 1.
output_error() {
    echo "$FILENAME:$LINE_NUMBER $1"
    RETURN_CODE=1
}

# This function is called for each line in each file to check syntax.
check_line() {
    line="$1"

    if [[ "$line" =~ ^function ]]; then
        if ! [[ "$line" =~ abort$ ]]; then
            if ((FIX_ERRORS)); then
                # Use sed to add the 'abort' flag
                sed -i "${LINE_NUMBER}s/$/ abort/" "$FILENAME"
            else
                output_error 'Function without abort keyword (See :help except-compat)'
            fi
        fi
    fi

    if [[ "$line" =~ ' '+$ ]]; then
        output_error 'Trailing whitespace'
    fi
}

# Loop through all of the vim files and keep track of the file line numbers.
for FILENAME in $(find "$1" -name '*.vim'); do
    LINE_NUMBER=0

    while read; do
        LINE_NUMBER=$(expr $LINE_NUMBER + 1)

        check_line "$REPLY"
    done < "$FILENAME"
done

exit $RETURN_CODE