forked from stdlib-js/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
204 lines (175 loc) · 5.64 KB
/
pre-commit
File metadata and controls
204 lines (175 loc) · 5.64 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env bash
#
# A git hook called by `git commit`. If this scripts exits with a non-zero status, the commit will be aborted.
#
# This hook is called with no arguments.
# VARIABLES #
# Determine root directory:
root=$(git rev-parse --show-toplevel)
# Define the path to a utility for linting filenames:
lint_filenames="${root}/tools/lint/filenames/bin/cli"
# Define the path to a utility for linting package.json files:
lint_package_json="${root}/tools/lint/pkg-json/bin/cli"
# Define the path to ESLint configuration file for linting examples:
eslint_examples_conf="${root}/etc/eslint/.eslintrc.examples.js"
# Define the path to ESLint configuration file for linting tests:
eslint_tests_conf="${root}/etc/eslint/.eslintrc.tests.js"
# Define the path to ESLint configuration file for linting benchmarks:
eslint_benchmarks_conf="${root}/etc/eslint/.eslintrc.benchmarks.js"
# FUNCTIONS #
# Defines an error handler.
#
# $1 - error status
on_error() {
cleanup
exit "$1"
}
# Runs clean-up tasks.
cleanup() {
echo '' >&2
}
# Checks for non-ASCII filenames (to ensure cross platform portability).
check_filenames() {
local commit=$(git rev-parse --verify HEAD)
local against
if [[ -z "${commit}" ]]; then
# This is the initial commit, so we diff against an empty tree object:
against='4b825dc642cb6eb9a060e54bf8d69288fbee4904'
else
against='HEAD'
fi
# We exploit the fact that the printable range starts with the space character and ends with the tilde. Note that the use of brackets around a `tr` range is okay here, (for portability to Solaris 10's /usr/bin/tr, it's even required), since the square bracket bytes happen to fall in the designated range.
local num_files=$(git diff --cached --name-only --diff-filter=A -z "${against}" | LC_ALL=C tr -d '[ -~]\0' | wc -c)
if [[ "${num_files}" -ne 0 ]]; then
echo 'Error: Attempting to add a non-ASCII filename. Non-ASCII filenames limit cross-platform portability. Please rename offending files before committing.' >&2
return 1
fi
return 0
}
# Lints staged files.
run_lint() {
local changed_files
local files
# Get the set of changed files (added and modified):
changed_files=$(git diff --name-only --cached --diff-filter AM)
# Lint filenames:
echo "${changed_files}" | "${lint_filenames}"
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Filename lint errors.' >&2
return 1
fi
# Lint Markdown files...
files=$(echo "${changed_files}" | grep '\.md$' | tr '\n' ' ')
if [[ -n "${files}" ]]; then
make FILES="${files}" lint-markdown-files > /dev/null
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Markdown lint errors.' >&2
return 1
fi
fi
# Lint package.json files...
files=$(echo "${changed_files}" | grep 'package\.json$' | grep -v 'datapackage\.json$' )
if [[ -n "${files}" ]]; then
echo "${files}" | "${lint_package_json}" >&2
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Package.json lint errors.' >&2
return 1
fi
fi
# Lint JavaScript source files...
files=$(echo "${changed_files}" | grep '\.js$' | grep -v -e 'examples' -e 'test' -e 'benchmark' | tr '\n' ' ')
if [[ -n "${files}" ]]; then
make FILES="${files}" lint-javascript-files > /dev/null >&2
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'JavaScript lint errors for source files.' >&2
return 1
fi
fi
# Lint JavaScript command-line interfaces...
files=$(echo "${changed_files}" | grep '\/bin\/cli$' | tr '\n' ' ')
if [[ -n "${files}" ]]; then
make FILES="${files}" lint-javascript-files > /dev/null >&2
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'JavaScript lint errors for command-line interface files.' >&2
return 1
fi
fi
# Lint JavaScript examples files...
files=$(echo "${changed_files}" | grep 'examples/.*\.js$' | tr '\n' ' ')
if [[ -n "${files}" ]]; then
make JAVASCRIPT_LINTER=eslint ESLINT_CONF="${eslint_examples_conf}" FILES="${files}" lint-javascript-files > /dev/null >&2
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'JavaScript lint errors for example files.' >&2
return 1
fi
fi
# Lint JavaScript test files...
files=$(echo "${changed_files}" | grep 'test/.*\.js$' | tr '\n' ' ')
if [[ -n "${files}" ]]; then
make JAVASCRIPT_LINTER=eslint ESLINT_CONF="${eslint_tests_conf}" FILES="${files}" lint-javascript-files > /dev/null >&2
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'JavaScript lint errors for test files.' >&2
return 1
fi
fi
# Lint JavaScript benchmark files...
files=$(echo "${changed_files}" | grep 'benchmark/.*\.js$' | tr '\n' ' ')
if [[ -n "${files}" ]]; then
make JAVASCRIPT_LINTER=eslint ESLINT_CONF="${eslint_benchmarks_conf}" FILES="${files}" lint-javascript-files > /dev/null >&2
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'JavaScript lint errors for benchmark files.' >&2
return 1
fi
fi
# Lint Python files...
files=$(echo "${changed_files}" | grep '\.py$' | tr '\n' ' ')
if [[ -n "${files}" ]]; then
make check-python-linters > /dev/null >&2
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Unable to lint Python files. Ensure that linters are installed.' >&2
else
make FILES="${files}" lint-python-files > /dev/null >&2
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Python lint errors.' >&2
return 1
fi
fi
fi
# Lint R files...
files=$(echo "${changed_files}" | grep '\.R$' | tr '\n' ' ')
if [[ -n "${files}" ]]; then
make FILES="${files}" lint-r-files > /dev/null >&2
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'R lint errors.' >&2
return 1
fi
fi
# TODO: if datapackage.json, validate via schema
return 0
}
# Main execution sequence.
main() {
check_filenames
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
run_lint
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
cleanup
exit 0
}
# Run main:
main