Skip to content

Commit 0437de8

Browse files
committed
Use PagerDuty/jsonlint to lint JSON files.
For whatever reason, `node -c` generally does not like JSON files.
1 parent 9dba937 commit 0437de8

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ the type of file(s) you want to lint:
4444

4545
$ which node # returns a path if it is installed properly.
4646

47+
* If you want to syntax-lint JSON files, you will need to install
48+
[jsonlint](https://github.com/PagerDuty/jsonlint#installation).
49+
50+
$ which jsonlint # returns a path if it is installed properly.
51+
4752
* If you want to syntax-lint PHP files, you will need to
4853
[install PHP](http://php.net/manual/install.php).
4954

pre-commit/1-js-syntax.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ case "${1}" in
3131
;;
3232

3333
* )
34-
for file in $(git diff-index --cached --name-only HEAD | grep -E '\.(js|json)') ; do
34+
for file in $(git diff-index --cached --name-only HEAD | grep -E '\.(js)') ; do
3535
test_file "${file}"
3636
done
3737
;;

pre-commit/1-json-syntax.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
3+
# shellcheck disable=SC2034
4+
GREP_OPTIONS=""
5+
6+
function test_file {
7+
file="${1}"
8+
9+
if [ ! -f "${file}" ] ; then
10+
return
11+
fi
12+
13+
if which -s node ; then
14+
echo "Running JSON syntax lint..."
15+
16+
# Set -e before and +e after for _required_ linters (i.e.: that will prevent
17+
# commit, e.g.: syntax linters).
18+
# Set +e before and -e after for _optional_ linters (i.e.: that will only
19+
# output messages upon commit, e.g.: style linters).
20+
set -e
21+
jsonlint -c "$file"
22+
set +e
23+
else
24+
echo "Can't run JSON syntax linter because the jsonlint executable isn't in the PATH."
25+
fi
26+
}
27+
28+
case "${1}" in
29+
--about )
30+
echo "JSON syntax lint."
31+
;;
32+
33+
* )
34+
for file in $(git diff-index --cached --name-only HEAD | grep -E '\.(json)') ; do
35+
test_file "${file}"
36+
done
37+
;;
38+
esac

0 commit comments

Comments
 (0)