mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-09-30 00:39:19 +08:00
Compare commits
394 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
f7547c9a5a | ||
|
bd717c9d1b | ||
|
da0931740f | ||
|
555f8a80dd | ||
|
a9c04e8a37 | ||
|
9378227570 | ||
|
a128796c0c | ||
|
a0005bfa5a | ||
|
37a72d05ec | ||
|
c60323fb25 | ||
|
db11e2f663 | ||
|
aac1d05a7e | ||
|
67f0dc4fd5 | ||
|
8cf037fe5e | ||
|
615063a9c3 | ||
|
37e78141bd | ||
|
9f833770b0 | ||
|
7963eeab9d | ||
|
7a5e261d03 | ||
|
9d5363377e | ||
|
86d470c74f | ||
|
acee69676b | ||
|
a57f6d2886 | ||
|
d28c8f883f | ||
|
c43b19f897 | ||
|
45a67e7c64 | ||
|
68a03e05e5 | ||
|
014a66f3f6 | ||
|
fee13732a4 | ||
|
741d499b3d | ||
|
9b66bc2f13 | ||
|
7b998239af | ||
|
4c9210af79 | ||
|
a75219e525 | ||
|
99d6df8a08 | ||
|
106f321cf0 | ||
|
1da0becb0f | ||
|
472579052b | ||
|
c735bbf30a | ||
|
eecd003e2d | ||
|
440d0038aa | ||
|
12bc7a750c | ||
|
c2d67c15f8 | ||
|
6043deb8f2 | ||
|
83d329c8da | ||
|
d0beac6d0b | ||
|
b88b253cad | ||
|
a8f9f25ec9 | ||
|
85c49a8af9 | ||
|
42abcb7ae2 | ||
|
d5c5128115 | ||
|
6d06103cab | ||
|
c95914f9b3 | ||
|
ea24e25efd | ||
|
8f0448133c | ||
|
7fc9496320 | ||
|
962fad038c | ||
|
a223a7a5a5 | ||
|
8e9290badb | ||
|
292b0840d9 | ||
|
43c24cf79c | ||
|
21ad4196db | ||
|
172aa7c4fc | ||
|
c290eace54 | ||
|
a6efd02807 | ||
|
057cc714b3 | ||
|
0e00249eae | ||
|
0ca50159ec | ||
|
7e6a556ef1 | ||
|
4bfe6496d9 | ||
|
ffbbfcfe25 | ||
|
cc424bac11 | ||
|
cb01cbf7eb | ||
|
1e32139f66 | ||
|
4d92a2e15c | ||
|
f8648e5465 | ||
|
4fd8de058b | ||
|
aaffe38198 | ||
|
bd116f252b | ||
|
ef51ed3950 | ||
|
61b073d507 | ||
|
9d604ae732 | ||
|
1ca0b72329 | ||
|
474b23d6e7 | ||
|
fe2b4b5079 | ||
|
e820a5642b | ||
|
392b57b8e8 | ||
|
6595e14d25 | ||
|
115ef29079 | ||
|
76b798394f | ||
|
8a005526cc | ||
|
c29b6afa56 | ||
|
e6e89d68fd | ||
|
f25b8bd03a | ||
|
d7278b95f2 | ||
|
5487b3f229 | ||
|
28978a8b65 | ||
|
f5c6771016 | ||
|
0f48bb78a5 | ||
|
93be86f988 | ||
|
3449e6be21 | ||
|
2e52c2b56a | ||
|
1696296c0a | ||
|
93486ed6ac | ||
|
499e0ceaba | ||
|
ff5f29f661 | ||
|
c7bf1fd96e | ||
|
b96b7f35f4 | ||
|
926ee54036 | ||
|
9008a6833b | ||
|
ce60a1764f | ||
|
cbcca528ae | ||
|
83187dafd7 | ||
|
d919aaa847 | ||
|
3f296a08c1 | ||
|
0f15fa49ba | ||
|
0a4580e234 | ||
|
5c7d8129ad | ||
|
e075cde357 | ||
|
9f578f41a1 | ||
|
2c026f1ec7 | ||
|
874bdcb514 | ||
|
fa3eb47193 | ||
|
989ac32625 | ||
|
2bbfd0570d | ||
|
9b1befadc1 | ||
|
f44624a9c0 | ||
|
c75bbcbd60 | ||
|
daa9c08dd5 | ||
|
4da34fbc64 | ||
|
4a63a3a8bd | ||
|
2341a4c683 | ||
|
7eb6b35cb0 | ||
|
93eca1cb8e | ||
|
e701cf6fad | ||
|
5962b01816 | ||
|
5becc673b2 | ||
|
84ca7711c4 | ||
|
0e0de94045 | ||
|
699aac589a | ||
|
30c75340e6 | ||
|
4dfd7eb1cf | ||
|
79ba67dbd3 | ||
|
60f75e5b8a | ||
|
f042b0ebd1 | ||
|
764fdcb260 | ||
|
7473d4a743 | ||
|
91abd979f2 | ||
|
afea62de4e | ||
|
fa0f88c106 | ||
|
7fb399528c | ||
|
de9ab4e6ef | ||
|
ff1eab286c | ||
|
e01c470598 | ||
|
71a4053e8c | ||
|
3fdc6babb2 | ||
|
c175971bf0 | ||
|
b7b4d5d29e | ||
|
9cc9a575b2 | ||
|
b2dd00e4ee | ||
|
2053ac8882 | ||
|
e4cbf59fda | ||
|
f9c8a255be | ||
|
bfb2d79e54 | ||
|
fbb571811f | ||
|
0eaef95db8 | ||
|
f4deac6e43 | ||
|
49aa600c85 | ||
|
25b5b77240 | ||
|
ded04820b8 | ||
|
7a1fb2523d | ||
|
38bb156a1c | ||
|
023ae5dfda | ||
|
e280116ef0 | ||
|
788cf17076 | ||
|
78b8e76066 | ||
|
914974bd4f | ||
|
c0d3a98fcd | ||
|
380221a02c | ||
|
ba2c20a08a | ||
|
4d56852b9f | ||
|
be1f1c1ab7 | ||
|
bee4303c32 | ||
|
ef764b60ca | ||
|
9e6b07dbba | ||
|
3e3e4fd0cd | ||
|
561075ea79 | ||
|
42f0dce467 | ||
|
9702f1ff9c | ||
|
544047c5af | ||
|
321afa427e | ||
|
c381c5746f | ||
|
eeb7ea01c9 | ||
|
3116ed3ae5 | ||
|
e95d8dd14e | ||
|
f6ba500d6b | ||
|
c5aa171a5f | ||
|
b1aeee564c | ||
|
b8b4a11348 | ||
|
e099625e7d | ||
|
5242e384a1 | ||
|
7e77bfae49 | ||
|
9059024de6 | ||
|
0eebb50563 | ||
|
3b5aa84757 | ||
|
200aabb63c | ||
|
c6dcb4127a | ||
|
61d2112e71 | ||
|
9f0ef5983a | ||
|
1297ef46d7 | ||
|
f4be53eb19 | ||
|
3e7c2bfec0 | ||
|
07ffcb626e | ||
|
36bb1e7858 | ||
|
95b1185882 | ||
|
8efbecd64a | ||
|
52a9d90e1a | ||
|
f5892f2d0d | ||
|
de7541e656 | ||
|
861b63aa77 | ||
|
64c9c83cc8 | ||
|
aa3b709b5d | ||
|
0358090b3c | ||
|
ea05271fa3 | ||
|
50116e8aee | ||
|
5ccaddbcc2 | ||
|
80b7e1e099 | ||
|
50af8aba29 | ||
|
5fb1da6814 | ||
|
58205a3573 | ||
|
5b177d62cb | ||
|
0ab3a726d3 | ||
|
2791a48444 | ||
|
bb63d66f7c | ||
|
d9e419d60f | ||
|
aa4b24e458 | ||
|
1c7a9f8a2f | ||
|
2521c1cf56 | ||
|
65e7f2059d | ||
|
248858c13e | ||
|
c0d4c5a106 | ||
|
ec25fb4052 | ||
|
a3cd5979a2 | ||
|
37b24cc129 | ||
|
d72a5faa1f | ||
|
e2e65e1350 | ||
|
6ccf9d6af1 | ||
|
9470b9dc31 | ||
|
bf1003eae3 | ||
|
301705edea | ||
|
c6c12f52bd | ||
|
af46758ff1 | ||
|
025c380b84 | ||
|
10955a143c | ||
|
67dbbcbd89 | ||
|
cef4c1a0bc | ||
|
b824294961 | ||
|
5b7354918f | ||
|
b76c0a8221 | ||
|
c860b74505 | ||
|
9652ccfdbd | ||
|
f514f5f735 | ||
|
c53c8a5ead | ||
|
b456987b84 | ||
|
ed92fe501f | ||
|
bbe5155e63 | ||
|
4dfb3fce9c | ||
|
581bcc3907 | ||
|
293c3b27b8 | ||
|
25ea405468 | ||
|
e45d81c8fa | ||
|
05e657e130 | ||
|
bd19ab4fa9 | ||
|
8aa44bf529 | ||
|
45021a9b40 | ||
|
d31d31df23 | ||
|
3a276bd336 | ||
|
d3f6e045e2 | ||
|
abe6afc09f | ||
|
d984f8cbe7 | ||
|
acef53be9c | ||
|
2ea2293154 | ||
|
d40d376bf4 | ||
|
a669e1684b | ||
|
420d913bbf | ||
|
fb7ac3f57e | ||
|
112a7d8b9b | ||
|
31c5601c5e | ||
|
2737496b3a | ||
|
a404efab65 | ||
|
3c94d8b3eb | ||
|
a89403f09b | ||
|
6dcf4b8e64 | ||
|
d4d219affd | ||
|
489c3a4ddf | ||
|
1507e92c44 | ||
|
63a259e5be | ||
|
59c47f2266 | ||
|
a621eba6d3 | ||
|
978bfdd5da | ||
|
a03d94c0b2 | ||
|
e1fe9be7af | ||
|
c97cb8cf54 | ||
|
a504ca6b57 | ||
|
437f73c001 | ||
|
f187382a0c | ||
|
661be056f1 | ||
|
9f45dc4c8b | ||
|
8e31e86cc4 | ||
|
c6c615217b | ||
|
c1adc588fb | ||
|
3107a1bae0 | ||
|
73859039dd | ||
|
1e6a30905a | ||
|
a4b9cec9f0 | ||
|
c3a56659f4 | ||
|
e0a4241baa | ||
|
1835ebd3a0 | ||
|
b34f4c1f4b | ||
|
ec6f9e4d49 | ||
|
3760e7945f | ||
|
fcdd6055df | ||
|
fd2beaadfa | ||
|
df7f00eaed | ||
|
e45b679d58 | ||
|
263401cfcb | ||
|
0e21f91c07 | ||
|
4ecdc10599 | ||
|
baa4d2e555 | ||
|
26c55750cf | ||
|
9c42d43e90 | ||
|
434b904746 | ||
|
ab2b0e11a3 | ||
|
394f4d6505 | ||
|
4a2b2c7396 | ||
|
97cb753d21 | ||
|
98266a1878 | ||
|
6138206ce5 | ||
|
6debd59f02 | ||
|
9425654a42 | ||
|
461be74976 | ||
|
278ce56650 | ||
|
73822c3588 | ||
|
29dedbdc9c | ||
|
f6bc009331 | ||
|
ef811995fa | ||
|
73a41cdd2f | ||
|
1b4c486748 | ||
|
95a8cf93c9 | ||
|
bd04af0769 | ||
|
9acc8fcb53 | ||
|
897f019353 | ||
|
0636e7023c | ||
|
08ca1ee6e9 | ||
|
eb3e6fe8e1 | ||
|
ecd61bfc68 | ||
|
a8d88dfe98 | ||
|
7d2c519d64 | ||
|
3403f8d75b | ||
|
408a3b99d8 | ||
|
bc111141f8 | ||
|
3471ad45b1 | ||
|
d5ba41035b | ||
|
88aef838f1 | ||
|
3d61b73e91 | ||
|
138080bdc7 | ||
|
5b3f17c29d | ||
|
b47e083ee3 | ||
|
3cba76dc7d | ||
|
eb588f62f6 | ||
|
bcd13614eb | ||
|
a8376a09a9 | ||
|
5ed89d2241 | ||
|
4a87d2a3de | ||
|
41613babd9 | ||
|
cb57b4a74f | ||
|
e3f0243c0e | ||
|
66b5f13c6f | ||
|
a7a404a5a8 | ||
|
0761f5c923 | ||
|
b55149b22d | ||
|
4097bb5154 | ||
|
1b207b3d43 | ||
|
135b4aa485 | ||
|
cb76951ad2 | ||
|
705e476e4c | ||
|
e705552c97 | ||
|
198aa4fc3d | ||
|
f4044fbcc7 | ||
|
2827b35696 | ||
|
de95c376ea | ||
|
5e1b1e010a | ||
|
620c9c2023 | ||
|
359b1467a2 |
73
.compile_binaries
Executable file
73
.compile_binaries
Executable file
@@ -0,0 +1,73 @@
|
||||
#!/bin/bash
|
||||
|
||||
build_linux() {
|
||||
# Linux Docker image
|
||||
name="$DOCKER_BASE"
|
||||
DOCKER_BUILDS="$DOCKER_BUILDS $name"
|
||||
docker build -t "$name:current" .
|
||||
docker run "$name:current" --version
|
||||
printf '%s\n' "#!/bin/sh" "echo 'hello world'" > myscript
|
||||
docker run -v "$PWD:/mnt" "$name:current" myscript
|
||||
|
||||
# Copy static executable from docker image
|
||||
id=$(docker create "$name:current")
|
||||
docker cp "$id:/bin/shellcheck" "shellcheck"
|
||||
docker rm "$id"
|
||||
ls -l shellcheck
|
||||
./shellcheck myscript
|
||||
for tag in $TAGS
|
||||
do
|
||||
cp "shellcheck" "deploy/shellcheck-$tag.linux-x86_64";
|
||||
done
|
||||
}
|
||||
|
||||
build_aarch64() {
|
||||
# Linux aarch64 static executable
|
||||
docker run -v "$PWD:/mnt" koalaman/aarch64-builder 'buildsc'
|
||||
for tag in $TAGS
|
||||
do
|
||||
cp "shellcheck" "deploy/shellcheck-$tag.linux-aarch64"
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
build_armv6hf() {
|
||||
# Linux armv6hf static executable
|
||||
docker run -v "$PWD:/mnt" koalaman/armv6hf-builder -c 'compile-shellcheck'
|
||||
for tag in $TAGS
|
||||
do
|
||||
cp "shellcheck" "deploy/shellcheck-$tag.linux-armv6hf";
|
||||
done
|
||||
}
|
||||
|
||||
build_windows() {
|
||||
# Windows .exe
|
||||
docker run -v "$PWD:/appdata" koalaman/winghc cuib
|
||||
for tag in $TAGS
|
||||
do
|
||||
cp "dist/build/ShellCheck/shellcheck.exe" "deploy/shellcheck-$tag.exe";
|
||||
done
|
||||
}
|
||||
|
||||
build_osx() {
|
||||
# Darwin x86_64 executable
|
||||
brew update
|
||||
brew install cabal-install pandoc gnu-tar
|
||||
sudo ln -sf /usr/local/bin/gsha512sum /usr/local/bin/sha512sum
|
||||
sudo ln -sf /usr/local/bin/gtar /usr/local/bin/tar
|
||||
export PATH="/usr/local/bin:$PATH"
|
||||
|
||||
cabal update
|
||||
cabal install --dependencies-only
|
||||
cabal build shellcheck
|
||||
|
||||
# Cabal 3 no longer has a predictable output path
|
||||
path="$(find . -name 'shellcheck' -type f -perm +111)"
|
||||
[[ -e "$path" ]]
|
||||
|
||||
for tag in $TAGS
|
||||
do
|
||||
cp "$path" "deploy/shellcheck-$tag.darwin-x86_64";
|
||||
done
|
||||
}
|
||||
|
6
.dockerignore
Normal file
6
.dockerignore
Normal file
@@ -0,0 +1,6 @@
|
||||
*
|
||||
!LICENSE
|
||||
!Setup.hs
|
||||
!ShellCheck.cabal
|
||||
!shellcheck.hs
|
||||
!src
|
2
.github/ISSUE_TEMPLATE.md
vendored
2
.github/ISSUE_TEMPLATE.md
vendored
@@ -1,7 +1,7 @@
|
||||
#### For bugs
|
||||
- Rule Id (if any, e.g. SC1000):
|
||||
- My shellcheck version (`shellcheck --version` or "online"):
|
||||
- [ ] I read the issue's wiki page, e.g. https://github.com/koalaman/shellcheck/wiki/SC2086
|
||||
- [ ] The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
|
||||
- [ ] I tried on shellcheck.net and verified that this is still a problem on the latest commit
|
||||
|
||||
#### For new checks and feature suggestions
|
||||
|
57
.github_deploy
Executable file
57
.github_deploy
Executable file
@@ -0,0 +1,57 @@
|
||||
#!/bin/bash
|
||||
set -x
|
||||
shopt -s extglob
|
||||
|
||||
if [[ "$TRAVIS_SECURE_ENV_VARS" != "true" ]]
|
||||
then
|
||||
echo >&2 "Missing TRAVIS_SECURE_ENV_VARS. Skipping GitHub deployment."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
install_deps() {
|
||||
version="2.7.0" # 2.14.1 fails to overwrite duplicates
|
||||
case "$(uname)" in
|
||||
Linux)
|
||||
sudo apt-get update
|
||||
sudo apt-get install curl
|
||||
curl -L "https://github.com/github/hub/releases/download/v$version/hub-linux-amd64-$version.tgz" | tar xvz --strip-components=1 "hub-linux-amd64-$version/bin/hub"
|
||||
;;
|
||||
Darwin)
|
||||
curl -L "https://github.com/github/hub/releases/download/v$version/hub-darwin-amd64-$version.tgz" | tar xvz --strip-components=1 "hub-darwin-amd64-$version/bin/hub"
|
||||
;;
|
||||
*)
|
||||
echo "Unknown: $(uname)"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
hub_path="$PWD/bin/hub"
|
||||
hub() {
|
||||
"$hub_path" "$@"
|
||||
}
|
||||
}
|
||||
install_deps
|
||||
|
||||
export EDITOR="touch"
|
||||
|
||||
# Sanity check
|
||||
hub release show latest || exit 1
|
||||
|
||||
for tag in $TAGS
|
||||
do
|
||||
if ! hub release show "$tag"
|
||||
then
|
||||
echo "Creating new release $tag"
|
||||
git show --no-patch --format='format:%B' > description
|
||||
hub release create -F description "$tag"
|
||||
fi
|
||||
|
||||
files=()
|
||||
for file in deploy/*
|
||||
do
|
||||
[[ $file == *.@(xz|gz|zip) ]] || continue
|
||||
files+=(-a "$file")
|
||||
done
|
||||
hub release edit "${files[@]}" "$tag" || exit 1
|
||||
done
|
||||
|
113
.multi_arch_docker
Executable file
113
.multi_arch_docker
Executable file
@@ -0,0 +1,113 @@
|
||||
#!/bin/bash
|
||||
# This script builds and deploys multi-architecture docker images from the
|
||||
# binaries previously built and deployed to GCS by the Travis pipeline.
|
||||
|
||||
if [[ "$TRAVIS_SECURE_ENV_VARS" != "true" ]]
|
||||
then
|
||||
echo >&2 "Missing TRAVIS_SECURE_ENV_VARS. Skipping Docker builds."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
function multi_arch_docker::install_docker_buildx() {
|
||||
# Install up-to-date version of docker, with buildx support.
|
||||
local -r docker_apt_repo='https://download.docker.com/linux/ubuntu'
|
||||
curl -fsSL "${docker_apt_repo}/gpg" | sudo apt-key add -
|
||||
local -r os="$(lsb_release -cs)"
|
||||
sudo add-apt-repository "deb [arch=amd64] $docker_apt_repo $os stable"
|
||||
sudo apt-get update
|
||||
sudo apt-get -y -o Dpkg::Options::="--force-confnew" install docker-ce
|
||||
|
||||
# Enable docker daemon experimental support (for 'pull --platform').
|
||||
local -r config='/etc/docker/daemon.json'
|
||||
if [[ -e "$config" ]]; then
|
||||
sudo sed -i -e 's/{/{ "experimental": true, /' "$config"
|
||||
else
|
||||
echo '{ "experimental": true }' | sudo tee "$config"
|
||||
fi
|
||||
sudo systemctl restart docker
|
||||
|
||||
# Install QEMU multi-architecture support for docker buildx.
|
||||
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
|
||||
|
||||
# Instantiate docker buildx builder with multi-architecture support.
|
||||
export DOCKER_CLI_EXPERIMENTAL=enabled
|
||||
docker buildx create --name mybuilder
|
||||
docker buildx use mybuilder
|
||||
# Start up buildx and verify that all is OK.
|
||||
docker buildx inspect --bootstrap
|
||||
}
|
||||
|
||||
# Log in to Docker Hub for deployment.
|
||||
function multi_arch_docker::login_to_docker_hub() {
|
||||
echo "$DOCKER_PASSWORD" | docker login -u="$DOCKER_USERNAME" --password-stdin
|
||||
}
|
||||
|
||||
# Run buildx build and push. Passed in arguments augment the command line.
|
||||
function multi_arch_docker::buildx() {
|
||||
mkdir -p /tmp/empty
|
||||
docker buildx build \
|
||||
--platform "${DOCKER_PLATFORMS// /,}" \
|
||||
--push \
|
||||
--progress plain \
|
||||
-f Dockerfile.multi-arch \
|
||||
"$@" \
|
||||
/tmp/empty
|
||||
rmdir /tmp/empty
|
||||
}
|
||||
|
||||
# Build and push plain and alpine docker images for all tags.
|
||||
function multi_arch_docker::build_and_push_all() {
|
||||
for tag in $TAGS; do
|
||||
multi_arch_docker::buildx -t "$DOCKER_BASE:$tag" --build-arg "tag=$tag"
|
||||
multi_arch_docker::buildx -t "$DOCKER_BASE-alpine:$tag" \
|
||||
--build-arg "tag=$tag" --target alpine
|
||||
done
|
||||
}
|
||||
|
||||
# Test all pushed docker images.
|
||||
function multi_arch_docker::test_all() {
|
||||
printf '%s\n' "#!/bin/sh" "echo 'hello world'" > myscript
|
||||
|
||||
for platform in $DOCKER_PLATFORMS; do
|
||||
for tag in $TAGS; do
|
||||
for ext in '-alpine' ''; do
|
||||
image="${DOCKER_BASE}${ext}:${tag}"
|
||||
msg="Testing docker image $image on platform $platform"
|
||||
line="${msg//?/=}"
|
||||
printf '\n%s\n%s\n%s\n' "${line}" "${msg}" "${line}"
|
||||
docker pull -q --platform "$platform" "$image"
|
||||
if [ -n "$ext" ]; then
|
||||
echo -n "Image architecture: "
|
||||
docker run --rm --entrypoint /bin/sh "$image" -c 'uname -m'
|
||||
version=$(docker run --rm "$image" shellcheck --version \
|
||||
| grep 'version:')
|
||||
else
|
||||
version=$(docker run --rm "$image" --version | grep 'version:')
|
||||
fi
|
||||
version=${version/#version: /v}
|
||||
echo "shellcheck version: $version"
|
||||
if [[ ! ("$tag" =~ ^(latest|stable)$) && "$tag" != "$version" ]]; then
|
||||
echo "Version mismatch: shellcheck $version tagged as $tag"
|
||||
exit 1
|
||||
fi
|
||||
if [ -n "$ext" ]; then
|
||||
docker run --rm -v "$PWD:/mnt" -w /mnt "$image" shellcheck myscript
|
||||
else
|
||||
docker run --rm -v "$PWD:/mnt" "$image" myscript
|
||||
fi
|
||||
done
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
function multi_arch_docker::main() {
|
||||
export DOCKER_PLATFORMS='linux/amd64'
|
||||
DOCKER_PLATFORMS+=' linux/arm64'
|
||||
DOCKER_PLATFORMS+=' linux/arm/v6'
|
||||
|
||||
multi_arch_docker::install_docker_buildx
|
||||
multi_arch_docker::login_to_docker_hub
|
||||
multi_arch_docker::build_and_push_all
|
||||
set +x
|
||||
multi_arch_docker::test_all
|
||||
}
|
@@ -35,6 +35,14 @@ do
|
||||
rm "shellcheck"
|
||||
done
|
||||
|
||||
for file in *.linux-aarch64
|
||||
do
|
||||
base="${file%.*}"
|
||||
cp "$file" "shellcheck"
|
||||
tar -cJf "$base.linux.aarch64.tar.xz" --transform="s:^:$base/:" README.txt LICENSE.txt shellcheck
|
||||
rm "shellcheck"
|
||||
done
|
||||
|
||||
for file in *.linux-armv6hf
|
||||
do
|
||||
base="${file%.*}"
|
||||
@@ -43,8 +51,15 @@ do
|
||||
rm "shellcheck"
|
||||
done
|
||||
|
||||
for file in *.darwin-x86_64
|
||||
do
|
||||
base="${file%.*}"
|
||||
cp "$file" "shellcheck"
|
||||
tar -cJf "$base.darwin.x86_64.tar.xz" --transform="s:^:$base/:" README.txt LICENSE.txt shellcheck
|
||||
rm "shellcheck"
|
||||
done
|
||||
|
||||
for file in ./*
|
||||
do
|
||||
sha512sum "$file" > "$file.sha512sum"
|
||||
done
|
||||
|
||||
|
14
.snapsquid.conf
Normal file
14
.snapsquid.conf
Normal file
@@ -0,0 +1,14 @@
|
||||
# In 2015, cabal-install had a http bug triggered when proxies didn't keep
|
||||
# the connection open. This version made it into Ubuntu Xenial as used by
|
||||
# Snapcraft. In June 2018, Snapcraft's proxy started triggering this bug.
|
||||
#
|
||||
# https://bugs.launchpad.net/launchpad-buildd/+bug/1797809
|
||||
#
|
||||
# Workaround: add more proxy
|
||||
|
||||
visible_hostname localhost
|
||||
http_port 8888
|
||||
cache_peer 10.10.10.1 parent 8222 0 no-query default
|
||||
cache_peer_domain localhost !.internal
|
||||
http_access allow all
|
||||
|
106
.travis.yml
106
.travis.yml
@@ -1,80 +1,64 @@
|
||||
sudo: required
|
||||
|
||||
language: sh
|
||||
language: shell
|
||||
os: linux
|
||||
|
||||
services:
|
||||
- docker
|
||||
|
||||
before_install:
|
||||
- DOCKER_BASE="$DOCKER_USERNAME/shellcheck"
|
||||
- DOCKER_BUILDS=""
|
||||
- TAGS=""
|
||||
- test "$TRAVIS_BRANCH" = master && TAGS="$TAGS latest" || true
|
||||
- test -n "$TRAVIS_TAG" && TAGS="$TAGS stable $TRAVIS_TAG" || true
|
||||
- echo "Tags are $TAGS"
|
||||
jobs:
|
||||
include:
|
||||
- stage: Build
|
||||
|
||||
# This must weirdly not have a dash, otherwise an empty job is created
|
||||
env: BUILD=linux
|
||||
- env: BUILD=windows
|
||||
- env: BUILD=armv6hf
|
||||
- env: BUILD=aarch64
|
||||
- env: BUILD=osx
|
||||
os: osx
|
||||
|
||||
- stage: Deploy docker image
|
||||
# Deploy only for pushes to master branch, not other branches, not PRs.
|
||||
if: type = push
|
||||
script:
|
||||
- source ./.multi_arch_docker
|
||||
- set -ex; multi_arch_docker::main; set +x
|
||||
|
||||
# This is in global context and runs for every stage that doesn't override it.
|
||||
before_install: |
|
||||
DOCKER_BASE="$DOCKER_USERNAME/shellcheck"
|
||||
DOCKER_BUILDS=""
|
||||
export TAGS=""
|
||||
test "$TRAVIS_BRANCH" = master && TAGS="$TAGS latest" || true
|
||||
test -n "$TRAVIS_TAG" && TAGS="$TAGS stable $TRAVIS_TAG" || true
|
||||
echo "Tags are $TAGS"
|
||||
|
||||
# This is in global context and runs for every stage that doesn't override it.
|
||||
script:
|
||||
- mkdir deploy
|
||||
# Remove all tests to reduce binary size
|
||||
- mkdir -p deploy
|
||||
- source ./.compile_binaries
|
||||
- ./striptests
|
||||
# Linux Docker image
|
||||
- name="$DOCKER_BASE"
|
||||
- DOCKER_BUILDS="$DOCKER_BUILDS $name"
|
||||
- docker build -t "$name:current" .
|
||||
- docker run "$name:current" --version
|
||||
- printf '%s\n' "#!/bin/sh" "echo 'hello world'" > myscript
|
||||
- docker run -v "$PWD:/mnt" "$name:current" myscript
|
||||
# Copy static executable from docker image
|
||||
- id=$(docker create "$name:current")
|
||||
- docker cp "$id:/bin/shellcheck" "shellcheck"
|
||||
- docker rm "$id"
|
||||
- ls -l shellcheck
|
||||
- ./shellcheck myscript
|
||||
- for tag in $TAGS; do cp "shellcheck" "deploy/shellcheck-$tag.linux-x86_64"; done
|
||||
# Linux Alpine based Docker image
|
||||
- name="$DOCKER_BASE-alpine"
|
||||
- DOCKER_BUILDS="$DOCKER_BUILDS $name"
|
||||
- sed -e '/DELETE-MARKER/,$d' Dockerfile > Dockerfile.alpine
|
||||
- docker build -f Dockerfile.alpine -t "$name:current" .
|
||||
- docker run "$name:current" sh -c 'shellcheck --version'
|
||||
# Linux armv6hf static executable
|
||||
- docker run -v "$PWD:/mnt" koalaman/armv6hf-builder -c 'compile-shellcheck'
|
||||
- for tag in $TAGS; do cp "shellcheck" "deploy/shellcheck-$tag.linux-armv6hf"; done
|
||||
- rm -f shellcheck || true
|
||||
# Windows .exe
|
||||
- docker run --user="$UID" -v "$PWD:/appdata" koalaman/winghc cuib
|
||||
- for tag in $TAGS; do cp "dist/build/ShellCheck/shellcheck.exe" "deploy/shellcheck-$tag.exe"; done
|
||||
- rm -rf dist shellcheck || true
|
||||
# Misc packaging
|
||||
- set -ex; build_"$BUILD"; set +x;
|
||||
- ./.prepare_deploy
|
||||
- ./.github_deploy
|
||||
|
||||
after_success:
|
||||
- docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"
|
||||
- for repo in $DOCKER_BUILDS;
|
||||
do
|
||||
for tag in $TAGS;
|
||||
do
|
||||
echo "Deploying $repo:current as $repo:$tag...";
|
||||
docker tag "$repo:current" "$repo:$tag" || exit 1;
|
||||
docker push "$repo:$tag" || exit 1;
|
||||
done;
|
||||
done;
|
||||
|
||||
after_failure:
|
||||
- id
|
||||
- pwd
|
||||
- df -h
|
||||
- find . -name '*.log' -type f -exec grep "" /dev/null {} +
|
||||
- find . -ls
|
||||
# This is in global context and runs for every stage that doesn't override it.
|
||||
after_failure: |
|
||||
id
|
||||
pwd
|
||||
df -h
|
||||
find . -name '*.log' -type f -exec grep "" /dev/null {} +
|
||||
find . -ls
|
||||
|
||||
# This is in global context and runs for every stage that doesn't override it.
|
||||
deploy:
|
||||
provider: gcs
|
||||
skip_cleanup: true
|
||||
access_key_id: GOOG7MDN7WEH6IIGBDCA
|
||||
secret_access_key:
|
||||
secure: Bcx2cT0/E2ikj7sdamVq52xlLZF9dz9ojGPtoKfPyQhkkZa+McVI4xgUSuyyoSxyKj77sofx2y8m6PJYYumT4g5hREV1tfeUkl0J2DQFMbGDYEt7kxVkXCxojNvhHwTzLFv0ezstrxWWxQm81BfQQ4U9lggRXtndAP4czZnOeHPINPSiue1QNwRAEw05r5UoIUJXy/5xyUrjIxn381pAs+gJqP2COeN9kTKYH53nS/AAws29RprfZFnPlo7xxWmcjRcdS5KPdGXI/c6tQp5zl2iTh510VC1PN2w1Wvnn/oNWhiNdqPyVDsojIX5+sS3nejzJA+KFMxXSBlyXIY3wPpS/MdscU79X6Q5f9ivsFfsm7gNBmxHUPNn0HAvU4ROT/CCE9j6jSbs5PC7QBo3CK4++jxAwE/pd9HUc2rs3k0ofx3rgveJ7txpy5yPKfwIIBi98kVKlC4w7dLvNTOfjW1Imt2yH87XTfsE0UIG9st1WII6s4l/WgBx2GuwKdt6+3QUYiAlCFckkxWi+fAvpHZUEL43Qxub5fN+ZV7Zib1n7opchH4QKGBb6/y0WaDCmtCfu0lppoe/TH6saOTjDFj67NJSElK6ZDxGZ3uw4R+ret2gm6WRKT2Oeub8J33VzSa7VkmFpMPrAAfPa9N1Z4ewBLoTmvxSg2A0dDrCdJio=
|
||||
bucket: shellcheck
|
||||
local-dir: deploy
|
||||
bucket: shellcheck-private
|
||||
local_dir: deploy
|
||||
on:
|
||||
repo: koalaman/shellcheck
|
||||
condition: $TRAVIS_BUILD_STAGE_NAME = Build
|
||||
all_branches: true
|
||||
|
68
CHANGELOG.md
68
CHANGELOG.md
@@ -1,17 +1,81 @@
|
||||
## ???
|
||||
## v0.7.1 - 2020-04-04
|
||||
### Fixed
|
||||
- `-f diff` no longer claims that it found more issues when it didn't
|
||||
- Known empty variables now correctly trigger SC2086
|
||||
- ShellCheck should now be compatible with Cabal 3
|
||||
- SC2154 and all command-specific checks now trigger for builtins
|
||||
called with `builtin`
|
||||
|
||||
### Added
|
||||
- SC1136: Warn about unexpected characters after ]/]]
|
||||
- SC2254: Suggest quoting expansions in case statements
|
||||
- SC2255: Suggest using `$((..))` in `[ 2*3 -eq 6 ]`
|
||||
- SC2256: Warn about translated strings that are known variables
|
||||
- SC2257: Warn about arithmetic mutation in redirections
|
||||
- SC2258: Warn about trailing commas in for loop elements
|
||||
|
||||
### Changed
|
||||
- SC2230: 'command -v' suggestion is now off by default (-i deprecate-which)
|
||||
- SC1081: Keywords are now correctly parsed case sensitively, with a warning
|
||||
|
||||
|
||||
## v0.7.0 - 2019-07-28
|
||||
### Added
|
||||
- Precompiled binaries for macOS and Linux aarch64
|
||||
- Preliminary support for fix suggestions
|
||||
- New `-f diff` unified diff format for auto-fixes
|
||||
- Files containing Bats tests can now be checked
|
||||
- Directory wide directives can now be placed in a `.shellcheckrc`
|
||||
- Optional checks: Use `--list-optional` to show a list of tests,
|
||||
Enable with `-o` flags or `enable=name` directives
|
||||
- Source paths: Use `-P dir1:dir2` or a `source-path=dir1` directive
|
||||
to specify search paths for sourced files.
|
||||
- json1 format like --format=json but treats tabs as single characters
|
||||
- Recognize FLAGS variables created by the shflags library.
|
||||
- Site-specific changes can now be made in Custom.hs for ease of patching
|
||||
- SC2154: Also warn about unassigned uppercase variables (optional)
|
||||
- SC2252: Warn about `[ $a != x ] || [ $a != y ]`, similar to SC2055
|
||||
- SC2251: Inform about ineffectual ! in front of commands
|
||||
- SC2250: Warn about variable references without braces (optional)
|
||||
- SC2249: Warn about `case` with missing default case (optional)
|
||||
- SC2248: Warn about unquoted variables without special chars (optional)
|
||||
- SC2247: Warn about $"(cmd)" and $"{var}"
|
||||
- SC2246: Warn if a shebang's interpreter ends with /
|
||||
- SC2245: Warn that Ksh ignores all but the first glob result in `[`
|
||||
- SC2243/SC2244: Suggest using explicit -n for `[ $foo ]` (optional)
|
||||
- SC1135: Suggest not ending double quotes just to make $ literal
|
||||
|
||||
### Changed
|
||||
- If a directive or shebang is not specified, a `.bash/.bats/.dash/.ksh`
|
||||
extension will be used to infer the shell type when present.
|
||||
- Disabling SC2120 on a function now disables SC2119 on call sites
|
||||
|
||||
### Fixed
|
||||
- SC2183 no longer warns about missing printf args for `%()T`
|
||||
|
||||
## v0.6.0 - 2018-12-02
|
||||
### Added
|
||||
- Command line option --severity/-S for filtering by minimum severity
|
||||
- Command line option --wiki-link-count/-W for showing wiki links
|
||||
- SC2152/SC2151: Warn about bad `exit` values like `1234` and `"foo"`
|
||||
- SC2236/SC2237: Suggest -n/-z instead of ! -z/-n
|
||||
- SC2238: Warn when redirecting to a known command name, e.g. ls > rm
|
||||
- SC2239: Warn if the shebang is not an absolute path, e.g. #!bin/sh
|
||||
- SC2240: Warn when passing additional arguments to dot (.) in sh/dash
|
||||
- SC1133: Better diagnostics when starting a line with |/||/&&
|
||||
|
||||
### Changed
|
||||
- Most warnings now have useful end positions
|
||||
- SC1117 about unknown double-quoted escape sequences has been retired
|
||||
|
||||
### Fixed
|
||||
- SC2021 no longer triggers for equivalence classes like '[=e=]'
|
||||
- SC2021 no longer triggers for equivalence classes like `[=e=]`
|
||||
- SC2221/SC2222 no longer mistriggers on fall-through case branches
|
||||
- SC2081 about glob matches in `[ .. ]` now also triggers for `!=`
|
||||
- SC2086 no longer warns about spaces in `$#`
|
||||
- SC2164 no longer suggests subshells for `cd ..; cmd; cd ..`
|
||||
- `read -a` is now correctly considered an array assignment
|
||||
- SC2039 no longer warns about LINENO now that it's POSIX
|
||||
|
||||
## v0.5.0 - 2018-05-31
|
||||
### Added
|
||||
|
11
Dockerfile
11
Dockerfile
@@ -1,5 +1,5 @@
|
||||
# Build-only image
|
||||
FROM ubuntu:17.10 AS build
|
||||
FROM ubuntu:18.04 AS build
|
||||
USER root
|
||||
WORKDIR /opt/shellCheck
|
||||
|
||||
@@ -12,7 +12,7 @@ COPY ShellCheck.cabal ./
|
||||
RUN cabal update && cabal install --dependencies-only --ghc-options="-optlo-Os -split-sections"
|
||||
|
||||
# Copy source and build it
|
||||
COPY LICENSE Setup.hs shellcheck.hs ./
|
||||
COPY LICENSE shellcheck.hs ./
|
||||
COPY src src
|
||||
RUN cabal build Paths_ShellCheck && \
|
||||
ghc -optl-static -optl-pthread -isrc -idist/build/autogen --make shellcheck -split-sections -optc-Wl,--gc-sections -optlo-Os && \
|
||||
@@ -21,13 +21,6 @@ RUN cabal build Paths_ShellCheck && \
|
||||
RUN mkdir -p /out/bin && \
|
||||
cp shellcheck /out/bin/
|
||||
|
||||
# Resulting Alpine image
|
||||
FROM alpine:latest
|
||||
LABEL maintainer="Vidar Holen <vidar@vidarholen.net>"
|
||||
COPY --from=build /out /
|
||||
|
||||
# DELETE-MARKER (Remove everything below to keep the alpine image)
|
||||
|
||||
# Resulting ShellCheck image
|
||||
FROM scratch
|
||||
LABEL maintainer="Vidar Holen <vidar@vidarholen.net>"
|
||||
|
26
Dockerfile.multi-arch
Normal file
26
Dockerfile.multi-arch
Normal file
@@ -0,0 +1,26 @@
|
||||
# Alpine image
|
||||
FROM alpine:latest AS alpine
|
||||
LABEL maintainer="Vidar Holen <vidar@vidarholen.net>"
|
||||
ARG tag
|
||||
|
||||
# Put the right binary for each architecture into place for the
|
||||
# multi-architecture docker image.
|
||||
RUN set -x; \
|
||||
arch="$(uname -m)"; \
|
||||
echo "arch is $arch"; \
|
||||
if [ "${arch}" = 'armv7l' ]; then \
|
||||
arch='armv6hf'; \
|
||||
fi; \
|
||||
url_base='https://github.com/koalaman/shellcheck/releases/download/'; \
|
||||
tar_file="${tag}/shellcheck-${tag}.linux.${arch}.tar.xz"; \
|
||||
wget "${url_base}${tar_file}" -O - | tar xJf -; \
|
||||
mv "shellcheck-${tag}/shellcheck" /bin/; \
|
||||
rm -rf "shellcheck-${tag}"; \
|
||||
ls -laF /bin/shellcheck
|
||||
|
||||
# ShellCheck image
|
||||
FROM scratch
|
||||
LABEL maintainer="Vidar Holen <vidar@vidarholen.net>"
|
||||
WORKDIR /mnt
|
||||
COPY --from=alpine /bin/shellcheck /bin/
|
||||
ENTRYPOINT ["/bin/shellcheck"]
|
194
README.md
194
README.md
@@ -8,45 +8,45 @@ ShellCheck is a GPLv3 tool that gives warnings and suggestions for bash/sh shell
|
||||
|
||||
The goals of ShellCheck are
|
||||
|
||||
- To point out and clarify typical beginner's syntax issues that cause a shell
|
||||
* To point out and clarify typical beginner's syntax issues that cause a shell
|
||||
to give cryptic error messages.
|
||||
|
||||
- To point out and clarify typical intermediate level semantic problems that
|
||||
* To point out and clarify typical intermediate level semantic problems that
|
||||
cause a shell to behave strangely and counter-intuitively.
|
||||
|
||||
- To point out subtle caveats, corner cases and pitfalls that may cause an
|
||||
* To point out subtle caveats, corner cases and pitfalls that may cause an
|
||||
advanced user's otherwise working script to fail under future circumstances.
|
||||
|
||||
See [the gallery of bad code](README.md#user-content-gallery-of-bad-code) for examples of what ShellCheck can help you identify!
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [How to use](#how-to-use)
|
||||
- [On the web](#on-the-web)
|
||||
- [From your terminal](#from-your-terminal)
|
||||
- [In your editor](#in-your-editor)
|
||||
- [In your build or test suites](#in-your-build-or-test-suites)
|
||||
- [Installing](#installing)
|
||||
- [Travis CI](#travis-ci)
|
||||
- [Compiling from source](#compiling-from-source)
|
||||
- [Installing Cabal](#installing-cabal)
|
||||
- [Compiling ShellCheck](#compiling-shellcheck)
|
||||
- [Running tests](#running-tests)
|
||||
- [Gallery of bad code](#gallery-of-bad-code)
|
||||
- [Quoting](#quoting)
|
||||
- [Conditionals](#conditionals)
|
||||
- [Frequently misused commands](#frequently-misused-commands)
|
||||
- [Common beginner's mistakes](#common-beginners-mistakes)
|
||||
- [Style](#style)
|
||||
- [Data and typing errors](#data-and-typing-errors)
|
||||
- [Robustness](#robustness)
|
||||
- [Portability](#portability)
|
||||
- [Miscellaneous](#miscellaneous)
|
||||
- [Testimonials](#testimonials)
|
||||
- [Ignoring issues](#ignoring-issues)
|
||||
- [Reporting bugs](#reporting-bugs)
|
||||
- [Contributing](#contributing)
|
||||
- [Copyright](#copyright)
|
||||
* [How to use](#how-to-use)
|
||||
* [On the web](#on-the-web)
|
||||
* [From your terminal](#from-your-terminal)
|
||||
* [In your editor](#in-your-editor)
|
||||
* [In your build or test suites](#in-your-build-or-test-suites)
|
||||
* [Installing](#installing)
|
||||
* [Compiling from source](#compiling-from-source)
|
||||
* [Installing Cabal](#installing-cabal)
|
||||
* [Compiling ShellCheck](#compiling-shellcheck)
|
||||
* [Running tests](#running-tests)
|
||||
* [Gallery of bad code](#gallery-of-bad-code)
|
||||
* [Quoting](#quoting)
|
||||
* [Conditionals](#conditionals)
|
||||
* [Frequently misused commands](#frequently-misused-commands)
|
||||
* [Common beginner's mistakes](#common-beginners-mistakes)
|
||||
* [Style](#style)
|
||||
* [Data and typing errors](#data-and-typing-errors)
|
||||
* [Robustness](#robustness)
|
||||
* [Portability](#portability)
|
||||
* [Miscellaneous](#miscellaneous)
|
||||
* [Testimonials](#testimonials)
|
||||
* [Ignoring issues](#ignoring-issues)
|
||||
* [Reporting bugs](#reporting-bugs)
|
||||
* [Contributing](#contributing)
|
||||
* [Copyright](#copyright)
|
||||
* [Other Resources](#other-resources)
|
||||
|
||||
## How to use
|
||||
|
||||
@@ -54,7 +54,7 @@ There are a number of ways to use ShellCheck!
|
||||
|
||||
### On the web
|
||||
|
||||
Paste a shell script on https://www.shellcheck.net for instant feedback.
|
||||
Paste a shell script on <https://www.shellcheck.net> for instant feedback.
|
||||
|
||||
[ShellCheck.net](https://www.shellcheck.net) is always synchronized to the latest git commit, and is the easiest way to give ShellCheck a go. Tell your friends!
|
||||
|
||||
@@ -85,8 +85,46 @@ You can see ShellCheck suggestions directly in a variety of editors.
|
||||
### In your build or test suites
|
||||
|
||||
While ShellCheck is mostly intended for interactive use, it can easily be added to builds or test suites.
|
||||
It makes canonical use of exit codes, so you can just add a `shellcheck` command as part of the process.
|
||||
|
||||
ShellCheck makes canonical use of exit codes, and can output simple JSON, CheckStyle compatible XML, GCC compatible warnings as well as human readable text (with or without ANSI colors). See the [Integration](https://github.com/koalaman/shellcheck/wiki/Integration) wiki page for more documentation.
|
||||
For example, in a Makefile:
|
||||
|
||||
```Makefile
|
||||
check-scripts:
|
||||
# Fail if any of these files have warnings
|
||||
shellcheck myscripts/*.sh
|
||||
```
|
||||
|
||||
or in a Travis CI `.travis.yml` file:
|
||||
|
||||
```yaml
|
||||
script:
|
||||
# Fail if any of these files have warnings
|
||||
- shellcheck myscripts/*.sh
|
||||
```
|
||||
|
||||
Services and platforms that have ShellCheck pre-installed and ready to use:
|
||||
|
||||
* [Travis CI](https://travis-ci.org/)
|
||||
* [Codacy](https://www.codacy.com/)
|
||||
* [Code Climate](https://codeclimate.com/)
|
||||
* [Code Factor](https://www.codefactor.io/)
|
||||
* [Github](https://github.com/features/actions)(only Linux)
|
||||
|
||||
Services and platforms with third party plugins:
|
||||
|
||||
* [SonarQube](https://www.sonarqube.org/) through [sonar-shellcheck-plugin](https://github.com/emerald-squad/sonar-shellcheck-plugin)
|
||||
|
||||
Most other services, including [GitLab](https://about.gitlab.com/), let you install
|
||||
ShellCheck yourself, either through the system's package manager (see [Installing](#installing)),
|
||||
or by downloading and unpacking a [binary release](#installing-a-pre-compiled-binary).
|
||||
|
||||
It's a good idea to manually install a specific ShellCheck version regardless. This avoids
|
||||
any surprise build breaks when a new version with new warnings is published.
|
||||
|
||||
For customized filtering or reporting, ShellCheck can output simple JSON, CheckStyle compatible XML,
|
||||
GCC compatible warnings as well as human readable text (with or without ANSI colors). See the
|
||||
[Integration](https://github.com/koalaman/shellcheck/wiki/Integration) wiki page for more documentation.
|
||||
|
||||
## Installing
|
||||
|
||||
@@ -133,19 +171,31 @@ On OS X with homebrew:
|
||||
|
||||
brew install shellcheck
|
||||
|
||||
On OpenBSD:
|
||||
|
||||
pkg_add shellcheck
|
||||
|
||||
On openSUSE
|
||||
|
||||
zypper in ShellCheck
|
||||
|
||||
Or use OneClickInstall - https://software.opensuse.org/package/ShellCheck
|
||||
Or use OneClickInstall - <https://software.opensuse.org/package/ShellCheck>
|
||||
|
||||
On Solus:
|
||||
|
||||
eopkg install shellcheck
|
||||
|
||||
On Windows (via [scoop](http://scoop.sh)):
|
||||
|
||||
scoop install shellcheck
|
||||
On Windows (via [chocolatey](https://chocolatey.org/packages/shellcheck)):
|
||||
|
||||
```cmd
|
||||
C:\> choco install shellcheck
|
||||
```
|
||||
|
||||
Or Windows (via [scoop](http://scoop.sh)):
|
||||
|
||||
```cmd
|
||||
C:\> scoop install shellcheck
|
||||
```
|
||||
|
||||
From Snap Store:
|
||||
|
||||
@@ -154,37 +204,55 @@ From Snap Store:
|
||||
From Docker Hub:
|
||||
|
||||
```sh
|
||||
docker pull koalaman/shellcheck:stable # Or :v0.4.7 for that version, or :latest for daily builds
|
||||
docker run -v "$PWD:/mnt" koalaman/shellcheck myscript
|
||||
docker run --rm -v "$PWD:/mnt" koalaman/shellcheck:stable myscript
|
||||
# Or :v0.4.7 for that version, or :latest for daily builds
|
||||
```
|
||||
|
||||
or use `koalaman/shellcheck-alpine` if you want a larger Alpine Linux based image to extend. It works exactly like a regular Alpine image, but has shellcheck preinstalled.
|
||||
|
||||
Using the [nix package manager](https://nixos.org/nix):
|
||||
```sh
|
||||
nix-env -iA nixpkgs.shellcheck
|
||||
```
|
||||
|
||||
Alternatively, you can download pre-compiled binaries for the latest release here:
|
||||
|
||||
* [Linux, x86_64](https://storage.googleapis.com/shellcheck/shellcheck-stable.linux.x86_64.tar.xz) (statically linked)
|
||||
* [Linux, armv6hf](https://storage.googleapis.com/shellcheck/shellcheck-stable.linux.armv6hf.tar.xz), i.e. Raspberry Pi (statically linked)
|
||||
* [Windows, x86](https://storage.googleapis.com/shellcheck/shellcheck-stable.zip)
|
||||
* [Linux, x86_64](https://github.com/koalaman/shellcheck/releases/download/stable/shellcheck-stable.linux.x86_64.tar.xz) (statically linked)
|
||||
* [Linux, armv6hf](https://github.com/koalaman/shellcheck/releases/download/stable/shellcheck-stable.linux.armv6hf.tar.xz), i.e. Raspberry Pi (statically linked)
|
||||
* [Linux, aarch64](https://github.com/koalaman/shellcheck/releases/download/stable/shellcheck-stable.linux.aarch64.tar.xz) aka ARM64 (statically linked)
|
||||
* [MacOS, x86_64](https://github.com/koalaman/shellcheck/releases/download/stable/shellcheck-stable.darwin.x86_64.tar.xz)
|
||||
* [Windows, x86](https://github.com/koalaman/shellcheck/releases/download/stable/shellcheck-stable.zip)
|
||||
|
||||
or see the [storage bucket listing](https://shellcheck.storage.googleapis.com/index.html) for checksums, older versions and the latest daily builds.
|
||||
or see the [GitHub Releases](https://github.com/koalaman/shellcheck/releases) for other releases
|
||||
(including the [latest](https://github.com/koalaman/shellcheck/releases/tag/latest) meta-release for daily git builds).
|
||||
|
||||
## Travis CI
|
||||
Distro packages already come with a `man` page. If you are building from source, it can be installed with:
|
||||
|
||||
```console
|
||||
pandoc -s -f markdown-smart -t man shellcheck.1.md -o shellcheck.1
|
||||
sudo mv shellcheck.1 /usr/share/man/man1
|
||||
```
|
||||
|
||||
### Travis CI
|
||||
|
||||
Travis CI has now integrated ShellCheck by default, so you don't need to manually install it.
|
||||
|
||||
If you still want to do so in order to upgrade at your leisure or ensure the latest release, follow the steps to install the shellcheck binary, bellow.
|
||||
If you still want to do so in order to upgrade at your leisure or ensure you're
|
||||
using the latest release, follow the steps below to install a binary version.
|
||||
|
||||
## Installing the shellcheck binary
|
||||
### Installing a pre-compiled binary
|
||||
|
||||
*Pre-requisite*: the program 'xz' needs to be installed on the system.
|
||||
To install it on debian/ubuntu/linux mint, run `apt install xz-utils`.
|
||||
To install it on Redhat/Fedora/CentOS, run `yum -y install xz`.
|
||||
The pre-compiled binaries come in `tar.xz` files. To decompress them, make sure
|
||||
`xz` is installed.
|
||||
On Debian/Ubuntu/Mint, you can `apt install xz-utils`.
|
||||
On Redhat/Fedora/CentOS, `yum -y install xz`.
|
||||
|
||||
A simple installer may do something like:
|
||||
|
||||
```bash
|
||||
export scversion="stable" # or "v0.4.7", or "latest"
|
||||
wget "https://storage.googleapis.com/shellcheck/shellcheck-${scversion}.linux.x86_64.tar.xz"
|
||||
tar --xz -xvf shellcheck-"${scversion}".linux.x86_64.tar.xz
|
||||
cp shellcheck-"${scversion}"/shellcheck /usr/bin/
|
||||
scversion="stable" # or "v0.4.7", or "latest"
|
||||
wget -qO- "https://github.com/koalaman/shellcheck/releases/download/${scversion?}/shellcheck-${scversion?}.linux.x86_64.tar.xz" | tar -xJv
|
||||
cp "shellcheck-${scversion}/shellcheck" /usr/bin/
|
||||
shellcheck --version
|
||||
```
|
||||
|
||||
@@ -198,11 +266,9 @@ ShellCheck is built and packaged using Cabal. Install the package `cabal-install
|
||||
|
||||
On MacOS (OS X), you can do a fast install of Cabal using brew, which takes a couple of minutes instead of more than 30 minutes if you try to compile it from source.
|
||||
|
||||
brew install cask
|
||||
brew cask install haskell-platform
|
||||
cabal install cabal-install
|
||||
$ brew install cabal-install
|
||||
|
||||
On MacPorts, the package is instead called `hs-cabal-install`, while native Windows users should install the latest version of the Haskell platform from https://www.haskell.org/platform/
|
||||
On MacPorts, the package is instead called `hs-cabal-install`, while native Windows users should install the latest version of the Haskell platform from <https://www.haskell.org/platform/>
|
||||
|
||||
Verify that `cabal` is installed and update its dependency list with
|
||||
|
||||
@@ -238,12 +304,15 @@ may use a legacy codepage. In `cmd.exe`, `powershell.exe` and Powershell ISE,
|
||||
make sure to use a TrueType font, not a Raster font, and set the active
|
||||
codepage to UTF-8 (65001) with `chcp`:
|
||||
|
||||
> chcp 65001
|
||||
Active code page: 65001
|
||||
```cmd
|
||||
chcp 65001
|
||||
```
|
||||
|
||||
In Powershell ISE, you may need to additionally update the output encoding:
|
||||
|
||||
> [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
```powershell
|
||||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
```
|
||||
|
||||
### Running tests
|
||||
|
||||
@@ -421,13 +490,13 @@ Alexander Tarasikov,
|
||||
|
||||
Issues can be ignored via environmental variable, command line, individually or globally within a file:
|
||||
|
||||
https://github.com/koalaman/shellcheck/wiki/Ignore
|
||||
<https://github.com/koalaman/shellcheck/wiki/Ignore>
|
||||
|
||||
## Reporting bugs
|
||||
|
||||
Please use the GitHub issue tracker for any bugs or feature suggestions:
|
||||
|
||||
https://github.com/koalaman/shellcheck/issues
|
||||
<https://github.com/koalaman/shellcheck/issues>
|
||||
|
||||
## Contributing
|
||||
|
||||
@@ -442,11 +511,12 @@ The contributor retains the copyright.
|
||||
|
||||
ShellCheck is licensed under the GNU General Public License, v3. A copy of this license is included in the file [LICENSE](LICENSE).
|
||||
|
||||
Copyright 2012-2018, Vidar 'koala_man' Holen and contributors.
|
||||
Copyright 2012-2019, [Vidar 'koala_man' Holen](https://github.com/koalaman/) and contributors.
|
||||
|
||||
Happy ShellChecking!
|
||||
|
||||
## Other Resources
|
||||
|
||||
## Other Resources
|
||||
* The wiki has [long form descriptions](https://github.com/koalaman/shellcheck/wiki/Checks) for each warning, e.g. [SC2221](https://github.com/koalaman/shellcheck/wiki/SC2221).
|
||||
* ShellCheck does not attempt to enforce any kind of formatting or indenting style, so also check out [shfmt](https://github.com/mvdan/sh)!
|
||||
|
||||
|
36
Setup.hs
36
Setup.hs
@@ -1,36 +0,0 @@
|
||||
import Distribution.PackageDescription (
|
||||
HookedBuildInfo,
|
||||
emptyHookedBuildInfo )
|
||||
import Distribution.Simple (
|
||||
Args,
|
||||
UserHooks ( preSDist ),
|
||||
defaultMainWithHooks,
|
||||
simpleUserHooks )
|
||||
import Distribution.Simple.Setup ( SDistFlags )
|
||||
|
||||
import System.Process ( system )
|
||||
|
||||
|
||||
main = defaultMainWithHooks myHooks
|
||||
where
|
||||
myHooks = simpleUserHooks { preSDist = myPreSDist }
|
||||
|
||||
-- | This hook will be executed before e.g. @cabal sdist@. It runs
|
||||
-- pandoc to create the man page from shellcheck.1.md. If the pandoc
|
||||
-- command is not found, this will fail with an error message:
|
||||
--
|
||||
-- /bin/sh: pandoc: command not found
|
||||
--
|
||||
-- Since the man page is listed in the Extra-Source-Files section of
|
||||
-- our cabal file, a failure here should result in a failure to
|
||||
-- create the distribution tarball (that's a good thing).
|
||||
--
|
||||
myPreSDist :: Args -> SDistFlags -> IO HookedBuildInfo
|
||||
myPreSDist _ _ = do
|
||||
putStrLn "Building the man page (shellcheck.1) with pandoc..."
|
||||
putStrLn pandoc_cmd
|
||||
result <- system pandoc_cmd
|
||||
putStrLn $ "pandoc exited with " ++ show result
|
||||
return emptyHookedBuildInfo
|
||||
where
|
||||
pandoc_cmd = "pandoc -s -t man shellcheck.1.md -o shellcheck.1"
|
@@ -1,5 +1,5 @@
|
||||
Name: ShellCheck
|
||||
Version: 0.5.0
|
||||
Version: 0.7.1
|
||||
Synopsis: Shell script analysis tool
|
||||
License: GPL-3
|
||||
License-file: LICENSE
|
||||
@@ -7,7 +7,7 @@ Category: Static Analysis
|
||||
Author: Vidar Holen
|
||||
Maintainer: vidar@vidarholen.net
|
||||
Homepage: https://www.shellcheck.net/
|
||||
Build-Type: Custom
|
||||
Build-Type: Simple
|
||||
Cabal-Version: >= 1.8
|
||||
Bug-reports: https://github.com/koalaman/shellcheck/issues
|
||||
Description:
|
||||
@@ -26,17 +26,13 @@ Extra-Source-Files:
|
||||
-- documentation
|
||||
README.md
|
||||
shellcheck.1.md
|
||||
-- built with a cabal sdist hook
|
||||
shellcheck.1
|
||||
-- A script to build the man page using pandoc
|
||||
manpage
|
||||
-- convenience script for stripping tests
|
||||
striptests
|
||||
-- tests
|
||||
test/shellcheck.hs
|
||||
|
||||
custom-setup
|
||||
setup-depends:
|
||||
base >= 4 && <5,
|
||||
process >= 1.0 && <1.7,
|
||||
Cabal >= 1.10 && <2.5
|
||||
|
||||
source-repository head
|
||||
type: git
|
||||
location: git://github.com/koalaman/shellcheck.git
|
||||
@@ -47,15 +43,16 @@ library
|
||||
build-depends:
|
||||
semigroups
|
||||
build-depends:
|
||||
-- GHC 7.6.3 (base 4.6.0.1) is buggy (#1131, #1119) in optimized mode.
|
||||
-- Just disable that version entirely to fail fast.
|
||||
aeson,
|
||||
base > 4.6.0.1 && < 5,
|
||||
array,
|
||||
base >= 4.8.0.0 && < 5,
|
||||
bytestring,
|
||||
containers >= 0.5,
|
||||
deepseq >= 1.4.0.0,
|
||||
directory,
|
||||
Diff >= 0.2.0,
|
||||
directory >= 1.2.3.0,
|
||||
mtl >= 2.2.1,
|
||||
filepath,
|
||||
parsec,
|
||||
regex-tdfa,
|
||||
QuickCheck >= 2.7.4,
|
||||
@@ -69,13 +66,18 @@ library
|
||||
ShellCheck.AnalyzerLib
|
||||
ShellCheck.Checker
|
||||
ShellCheck.Checks.Commands
|
||||
ShellCheck.Checks.Custom
|
||||
ShellCheck.Checks.ShellSupport
|
||||
ShellCheck.Data
|
||||
ShellCheck.Fixer
|
||||
ShellCheck.Formatter.Format
|
||||
ShellCheck.Formatter.CheckStyle
|
||||
ShellCheck.Formatter.Diff
|
||||
ShellCheck.Formatter.GCC
|
||||
ShellCheck.Formatter.JSON
|
||||
ShellCheck.Formatter.JSON1
|
||||
ShellCheck.Formatter.TTY
|
||||
ShellCheck.Formatter.Quiet
|
||||
ShellCheck.Interface
|
||||
ShellCheck.Parser
|
||||
ShellCheck.Regex
|
||||
@@ -88,31 +90,37 @@ executable shellcheck
|
||||
semigroups
|
||||
build-depends:
|
||||
aeson,
|
||||
array,
|
||||
base >= 4 && < 5,
|
||||
bytestring,
|
||||
deepseq >= 1.4.0.0,
|
||||
ShellCheck,
|
||||
containers,
|
||||
directory,
|
||||
deepseq >= 1.4.0.0,
|
||||
Diff >= 0.2.0,
|
||||
directory >= 1.2.3.0,
|
||||
mtl >= 2.2.1,
|
||||
filepath,
|
||||
parsec >= 3.0,
|
||||
QuickCheck >= 2.7.4,
|
||||
regex-tdfa
|
||||
regex-tdfa,
|
||||
ShellCheck
|
||||
main-is: shellcheck.hs
|
||||
|
||||
test-suite test-shellcheck
|
||||
type: exitcode-stdio-1.0
|
||||
build-depends:
|
||||
aeson,
|
||||
array,
|
||||
base >= 4 && < 5,
|
||||
bytestring,
|
||||
deepseq >= 1.4.0.0,
|
||||
ShellCheck,
|
||||
containers,
|
||||
directory,
|
||||
deepseq >= 1.4.0.0,
|
||||
Diff >= 0.2.0,
|
||||
directory >= 1.2.3.0,
|
||||
mtl >= 2.2.1,
|
||||
filepath,
|
||||
parsec,
|
||||
QuickCheck >= 2.7.4,
|
||||
regex-tdfa
|
||||
regex-tdfa,
|
||||
ShellCheck
|
||||
main-is: test/shellcheck.hs
|
||||
|
||||
|
4
manpage
Executable file
4
manpage
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
echo >&2 "Generating man page using pandoc"
|
||||
pandoc -s -f markdown-smart -t man shellcheck.1.md -o shellcheck.1 || exit
|
||||
echo >&2 "Done. You can read it with: man ./shellcheck.1"
|
@@ -8,6 +8,6 @@ fi
|
||||
|
||||
for i in 1 2
|
||||
do
|
||||
last=$(grep -hv "^prop" ./**/*.hs | grep -Ewo "$i[0-9]{3}" | sort -n | tail -n 1)
|
||||
last=$(grep -hv "^prop" ./**/*.hs | grep -Ewo "${i}[0-9]{3}" | sort -n | tail -n 1)
|
||||
echo "Next ${i}xxx: $((last+1))"
|
||||
done
|
||||
|
11
quicktest
11
quicktest
@@ -4,15 +4,8 @@
|
||||
# 'cabal test' remains the source of truth.
|
||||
|
||||
(
|
||||
var=$(echo 'liftM and $ sequence [
|
||||
ShellCheck.Analytics.runTests
|
||||
,ShellCheck.Parser.runTests
|
||||
,ShellCheck.Checker.runTests
|
||||
,ShellCheck.Checks.Commands.runTests
|
||||
,ShellCheck.Checks.ShellSupport.runTests
|
||||
,ShellCheck.AnalyzerLib.runTests
|
||||
]' | tr -d '\n' | cabal repl 2>&1 | tee /dev/stderr)
|
||||
if [[ $var == *$'\nTrue'* ]]
|
||||
var=$(echo 'main' | ghci test/shellcheck.hs 2>&1 | tee /dev/stderr)
|
||||
if [[ $var == *ExitSuccess* ]]
|
||||
then
|
||||
exit 0
|
||||
else
|
||||
|
172
shellcheck.1.md
172
shellcheck.1.md
@@ -29,14 +29,13 @@ will warn that decimals are not supported.
|
||||
+ For scripts starting with `#!/bin/ksh` (or using `-s ksh`), ShellCheck will
|
||||
not warn at all, as `ksh` supports decimals in arithmetic contexts.
|
||||
|
||||
|
||||
# OPTIONS
|
||||
|
||||
**-a**,\ **--check-sourced**
|
||||
|
||||
: Emit warnings in sourced files. Normally, `shellcheck` will only warn
|
||||
about issues in the specified files. With this option, any issues in
|
||||
sourced files files will also be reported.
|
||||
sourced files will also be reported.
|
||||
|
||||
**-C**[*WHEN*],\ **--color**[=*WHEN*]
|
||||
|
||||
@@ -44,6 +43,13 @@ not warn at all, as `ksh` supports decimals in arithmetic contexts.
|
||||
is *auto*. **--color** without an argument is equivalent to
|
||||
**--color=always**.
|
||||
|
||||
**-i**\ *CODE1*[,*CODE2*...],\ **--include=***CODE1*[,*CODE2*...]
|
||||
|
||||
: Explicitly include only the specified codes in the report. Subsequent **-i**
|
||||
options are cumulative, but all the codes can be specified at once,
|
||||
comma-separated as a single argument. Include options override any provided
|
||||
exclude options.
|
||||
|
||||
**-e**\ *CODE1*[,*CODE2*...],\ **--exclude=***CODE1*[,*CODE2*...]
|
||||
|
||||
: Explicitly exclude the specified codes from the report. Subsequent **-e**
|
||||
@@ -56,16 +62,39 @@ not warn at all, as `ksh` supports decimals in arithmetic contexts.
|
||||
standard output. Subsequent **-f** options are ignored, see **FORMATS**
|
||||
below for more information.
|
||||
|
||||
**-S**\ *SEVERITY*,\ **--severity=***severity*
|
||||
**--list-optional**
|
||||
|
||||
: Specify minimum severity of errors to consider. Valid values are *error*,
|
||||
*warning*, *info* and *style*. The default is *style*.
|
||||
: Output a list of known optional checks. These can be enabled with **-o**
|
||||
flags or **enable** directives.
|
||||
|
||||
**--norc**
|
||||
|
||||
: Don't try to look for .shellcheckrc configuration files.
|
||||
|
||||
**-o**\ *NAME1*[,*NAME2*...],\ **--enable=***NAME1*[,*NAME2*...]
|
||||
|
||||
: Enable optional checks. The special name *all* enables all of them.
|
||||
Subsequent **-o** options accumulate. This is equivalent to specifying
|
||||
**enable** directives.
|
||||
|
||||
**-P**\ *SOURCEPATH*,\ **--source-path=***SOURCEPATH*
|
||||
|
||||
: Specify paths to search for sourced files, separated by `:` on Unix and
|
||||
`;` on Windows. This is equivalent to specifying `search-path`
|
||||
directives.
|
||||
|
||||
**-s**\ *shell*,\ **--shell=***shell*
|
||||
|
||||
: Specify Bourne shell dialect. Valid values are *sh*, *bash*, *dash* and *ksh*.
|
||||
The default is to use the file's shebang, or *bash* if the target shell
|
||||
can't be determined.
|
||||
The default is to deduce the shell from the file's `shell` directive,
|
||||
shebang, or `.bash/.bats/.dash/.ksh` extension, in that order. *sh* refers to
|
||||
POSIX `sh` (not the system's), and will warn of portability issues.
|
||||
|
||||
**-S**\ *SEVERITY*,\ **--severity=***severity*
|
||||
|
||||
: Specify minimum severity of errors to consider. Valid values in order of
|
||||
severity are *error*, *warning*, *info* and *style*.
|
||||
The default is *style*.
|
||||
|
||||
**-V**,\ **--version**
|
||||
|
||||
@@ -78,11 +107,15 @@ not warn at all, as `ksh` supports decimals in arithmetic contexts.
|
||||
|
||||
**-x**,\ **--external-sources**
|
||||
|
||||
: Follow 'source' statements even when the file is not specified as input.
|
||||
: Follow `source` statements even when the file is not specified as input.
|
||||
By default, `shellcheck` will only follow files specified on the command
|
||||
line (plus `/dev/null`). This option allows following any file the script
|
||||
may `source`.
|
||||
|
||||
**FILES...**
|
||||
|
||||
: One or more script files to check, or "-" for standard input.
|
||||
|
||||
|
||||
# FORMATS
|
||||
|
||||
@@ -119,27 +152,59 @@ not warn at all, as `ksh` supports decimals in arithmetic contexts.
|
||||
...
|
||||
</checkstyle>
|
||||
|
||||
**json**
|
||||
**diff**
|
||||
|
||||
: Auto-fixes in unified diff format. Can be piped to `git apply` or `patch -p1`
|
||||
to automatically apply fixes.
|
||||
|
||||
--- a/test.sh
|
||||
+++ b/test.sh
|
||||
@@ -2,6 +2,6 @@
|
||||
## Example of a broken script.
|
||||
for f in $(ls *.m3u)
|
||||
do
|
||||
- grep -qi hq.*mp3 $f \
|
||||
+ grep -qi hq.*mp3 "$f" \
|
||||
&& echo -e 'Playlist $f contains a HQ file in mp3 format'
|
||||
done
|
||||
|
||||
|
||||
**json1**
|
||||
|
||||
: Json is a popular serialization format that is more suitable for web
|
||||
applications. ShellCheck's json is compact and contains only the bare
|
||||
minimum.
|
||||
minimum. Tabs are counted as 1 character.
|
||||
|
||||
{
|
||||
comments: [
|
||||
{
|
||||
"file": "filename",
|
||||
"line": lineNumber,
|
||||
"column": columnNumber,
|
||||
"level": "severitylevel",
|
||||
"code": errorCode,
|
||||
"message": "warning message"
|
||||
},
|
||||
...
|
||||
]
|
||||
}
|
||||
|
||||
**json**
|
||||
|
||||
: This is a legacy version of the **json1** format. It's a raw array of
|
||||
comments, and all offsets have a tab stop of 8.
|
||||
|
||||
**quiet**
|
||||
|
||||
: Suppress all normal output. Exit with zero if no issues are found,
|
||||
otherwise exit with one. Stops processing after the first issue.
|
||||
|
||||
[
|
||||
{
|
||||
"file": "filename",
|
||||
"line": lineNumber,
|
||||
"column": columnNumber,
|
||||
"level": "severitylevel",
|
||||
"code": errorCode,
|
||||
"message": "warning message"
|
||||
},
|
||||
...
|
||||
]
|
||||
|
||||
# DIRECTIVES
|
||||
ShellCheck directives can be specified as comments in the shell script
|
||||
before a command or block:
|
||||
|
||||
ShellCheck directives can be specified as comments in the shell script.
|
||||
If they appear before the first command, they are considered file-wide.
|
||||
Otherwise, they apply to the immediately following command or block:
|
||||
|
||||
# shellcheck key=value key=value
|
||||
command-or-structure
|
||||
@@ -169,17 +234,64 @@ Valid keys are:
|
||||
The command can be a simple command like `echo foo`, or a compound command
|
||||
like a function definition, subshell block or loop.
|
||||
|
||||
**enable**
|
||||
: Enable an optional check by name, as listed with **--list-optional**.
|
||||
Only file-wide `enable` directives are considered.
|
||||
|
||||
**source**
|
||||
: Overrides the filename included by a `source`/`.` statement. This can be
|
||||
used to tell shellcheck where to look for a file whose name is determined
|
||||
at runtime, or to skip a source by telling it to use `/dev/null`.
|
||||
|
||||
**source-path**
|
||||
: Add a directory to the search path for `source`/`.` statements (by default,
|
||||
only ShellCheck's working directory is included). Absolute paths will also
|
||||
be rooted in these paths. The special path `SCRIPTDIR` can be used to
|
||||
specify the currently checked script's directory, as in
|
||||
`source-path=SCRIPTDIR` or `source-path=SCRIPTDIR/../libs`. Multiple
|
||||
paths accumulate, and `-P` takes precedence over them.
|
||||
|
||||
**shell**
|
||||
: Overrides the shell detected from the shebang. This is useful for
|
||||
files meant to be included (and thus lacking a shebang), or possibly
|
||||
as a more targeted alternative to 'disable=2039'.
|
||||
|
||||
# RC FILES
|
||||
|
||||
Unless `--norc` is used, ShellCheck will look for a file `.shellcheckrc` or
|
||||
`shellcheckrc` in the script's directory and each parent directory. If found,
|
||||
it will read `key=value` pairs from it and treat them as file-wide directives.
|
||||
|
||||
Here is an example `.shellcheckrc`:
|
||||
|
||||
# Look for 'source'd files relative to the checked script,
|
||||
# and also look for absolute paths in /mnt/chroot
|
||||
source-path=SCRIPTDIR
|
||||
source-path=/mnt/chroot
|
||||
|
||||
# Turn on warnings for unquoted variables with safe values
|
||||
enable=quote-safe-variables
|
||||
|
||||
# Turn on warnings for unassigned uppercase variables
|
||||
enable=check-unassigned-uppercase
|
||||
|
||||
# Allow [ ! -z foo ] instead of suggesting -n
|
||||
disable=SC2236
|
||||
|
||||
If no `.shellcheckrc` is found in any of the parent directories, ShellCheck
|
||||
will look in `~/.shellcheckrc` followed by the XDG config directory
|
||||
(usually `~/.config/shellcheckrc`) on Unix, or `%APPDATA%/shellcheckrc` on
|
||||
Windows. Only the first file found will be used.
|
||||
|
||||
Note for Snap users: the Snap sandbox disallows access to hidden files.
|
||||
Use `shellcheckrc` without the dot instead.
|
||||
|
||||
Note for Docker users: ShellCheck will only be able to look for files that
|
||||
are mounted in the container, so `~/.shellcheckrc` will not be read.
|
||||
|
||||
|
||||
# ENVIRONMENT VARIABLES
|
||||
|
||||
The environment variable `SHELLCHECK_OPTS` can be set with default flags:
|
||||
|
||||
export SHELLCHECK_OPTS='--shell=bash --exclude=SC2016'
|
||||
@@ -189,7 +301,7 @@ invocation.
|
||||
|
||||
# RETURN VALUES
|
||||
|
||||
ShellCheck uses the follow exit codes:
|
||||
ShellCheck uses the following exit codes:
|
||||
|
||||
+ 0: All files successfully scanned with no issues.
|
||||
+ 1: All files successfully scanned with some issues.
|
||||
@@ -198,6 +310,7 @@ ShellCheck uses the follow exit codes:
|
||||
+ 4: ShellCheck was invoked with bad options (e.g. unknown formatter).
|
||||
|
||||
# LOCALE
|
||||
|
||||
This version of ShellCheck is only available in English. All files are
|
||||
leniently decoded as UTF-8, with a fallback of ISO-8859-1 for invalid
|
||||
sequences. `LC_CTYPE` is respected for output, and defaults to UTF-8 for
|
||||
@@ -206,20 +319,23 @@ locales where encoding is unspecified (such as the `C` locale).
|
||||
Windows users seeing `commitBuffer: invalid argument (invalid character)`
|
||||
should set their terminal to use UTF-8 with `chcp 65001`.
|
||||
|
||||
# AUTHOR
|
||||
ShellCheck is written and maintained by Vidar Holen.
|
||||
# AUTHORS
|
||||
|
||||
ShellCheck is developed and maintained by Vidar Holen, with assistance from a
|
||||
long list of wonderful contributors.
|
||||
|
||||
# REPORTING BUGS
|
||||
|
||||
Bugs and issues can be reported on GitHub:
|
||||
|
||||
https://github.com/koalaman/shellcheck/issues
|
||||
|
||||
# COPYRIGHT
|
||||
Copyright 2012-2015, Vidar Holen.
|
||||
|
||||
Copyright 2012-2019, Vidar Holen and contributors.
|
||||
Licensed under the GNU General Public License version 3 or later,
|
||||
see https://gnu.org/licenses/gpl.html
|
||||
|
||||
|
||||
# SEE ALSO
|
||||
|
||||
sh(1) bash(1)
|
||||
|
182
shellcheck.hs
182
shellcheck.hs
@@ -1,5 +1,5 @@
|
||||
{-
|
||||
Copyright 2012-2015 Vidar Holen
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
@@ -17,6 +17,7 @@
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
-}
|
||||
import qualified ShellCheck.Analyzer
|
||||
import ShellCheck.Checker
|
||||
import ShellCheck.Data
|
||||
import ShellCheck.Interface
|
||||
@@ -24,9 +25,12 @@ import ShellCheck.Regex
|
||||
|
||||
import qualified ShellCheck.Formatter.CheckStyle
|
||||
import ShellCheck.Formatter.Format
|
||||
import qualified ShellCheck.Formatter.Diff
|
||||
import qualified ShellCheck.Formatter.GCC
|
||||
import qualified ShellCheck.Formatter.JSON
|
||||
import qualified ShellCheck.Formatter.JSON1
|
||||
import qualified ShellCheck.Formatter.TTY
|
||||
import qualified ShellCheck.Formatter.Quiet
|
||||
|
||||
import Control.Exception
|
||||
import Control.Monad
|
||||
@@ -46,6 +50,7 @@ import System.Console.GetOpt
|
||||
import System.Directory
|
||||
import System.Environment
|
||||
import System.Exit
|
||||
import System.FilePath
|
||||
import System.IO
|
||||
|
||||
data Flag = Flag String String
|
||||
@@ -67,6 +72,7 @@ instance Monoid Status where
|
||||
data Options = Options {
|
||||
checkSpec :: CheckSpec,
|
||||
externalSources :: Bool,
|
||||
sourcePaths :: [FilePath],
|
||||
formatterOptions :: FormatterOptions,
|
||||
minSeverity :: Severity
|
||||
}
|
||||
@@ -74,6 +80,7 @@ data Options = Options {
|
||||
defaultOptions = Options {
|
||||
checkSpec = emptyCheckSpec,
|
||||
externalSources = False,
|
||||
sourcePaths = [],
|
||||
formatterOptions = newFormatterOptions {
|
||||
foColorOption = ColorAuto
|
||||
},
|
||||
@@ -87,11 +94,23 @@ options = [
|
||||
Option "C" ["color"]
|
||||
(OptArg (maybe (Flag "color" "always") (Flag "color")) "WHEN")
|
||||
"Use color (auto, always, never)",
|
||||
Option "i" ["include"]
|
||||
(ReqArg (Flag "include") "CODE1,CODE2..") "Consider only given types of warnings",
|
||||
Option "e" ["exclude"]
|
||||
(ReqArg (Flag "exclude") "CODE1,CODE2..") "Exclude types of warnings",
|
||||
Option "f" ["format"]
|
||||
(ReqArg (Flag "format") "FORMAT") $
|
||||
"Output format (" ++ formatList ++ ")",
|
||||
Option "" ["list-optional"]
|
||||
(NoArg $ Flag "list-optional" "true") "List checks disabled by default",
|
||||
Option "" ["norc"]
|
||||
(NoArg $ Flag "norc" "true") "Don't look for .shellcheckrc files",
|
||||
Option "o" ["enable"]
|
||||
(ReqArg (Flag "enable") "check1,check2..")
|
||||
"List of optional checks to enable (or 'all')",
|
||||
Option "P" ["source-path"]
|
||||
(ReqArg (Flag "source-path") "SOURCEPATHS")
|
||||
"Specify path when looking for sourced files (\"SCRIPTDIR\" for script's dir)",
|
||||
Option "s" ["shell"]
|
||||
(ReqArg (Flag "shell") "SHELLNAME")
|
||||
"Specify dialect (sh, bash, dash, ksh)",
|
||||
@@ -102,10 +121,13 @@ options = [
|
||||
(NoArg $ Flag "version" "true") "Print version information",
|
||||
Option "W" ["wiki-link-count"]
|
||||
(ReqArg (Flag "wiki-link-count") "NUM")
|
||||
"The number of wiki links to show, when applicable.",
|
||||
"The number of wiki links to show, when applicable",
|
||||
Option "x" ["external-sources"]
|
||||
(NoArg $ Flag "externals" "true") "Allow 'source' outside of FILES"
|
||||
(NoArg $ Flag "externals" "true") "Allow 'source' outside of FILES",
|
||||
Option "" ["help"]
|
||||
(NoArg $ Flag "help" "true") "Show this usage summary and exit"
|
||||
]
|
||||
getUsageInfo = usageInfo usageHeader options
|
||||
|
||||
printErr = lift . hPutStrLn stderr
|
||||
|
||||
@@ -114,15 +136,18 @@ parseArguments argv =
|
||||
case getOpt Permute options argv of
|
||||
(opts, files, []) -> return (opts, files)
|
||||
(_, _, errors) -> do
|
||||
printErr $ concat errors ++ "\n" ++ usageInfo usageHeader options
|
||||
printErr $ concat errors ++ "\n" ++ getUsageInfo
|
||||
throwError SyntaxFailure
|
||||
|
||||
formats :: FormatterOptions -> Map.Map String (IO Formatter)
|
||||
formats options = Map.fromList [
|
||||
("checkstyle", ShellCheck.Formatter.CheckStyle.format),
|
||||
("diff", ShellCheck.Formatter.Diff.format options),
|
||||
("gcc", ShellCheck.Formatter.GCC.format),
|
||||
("json", ShellCheck.Formatter.JSON.format),
|
||||
("tty", ShellCheck.Formatter.TTY.format options)
|
||||
("json1", ShellCheck.Formatter.JSON1.format),
|
||||
("tty", ShellCheck.Formatter.TTY.format options),
|
||||
("quiet", ShellCheck.Formatter.Quiet.format options)
|
||||
]
|
||||
|
||||
formatList = intercalate ", " names
|
||||
@@ -267,10 +292,30 @@ parseOption flag options =
|
||||
}
|
||||
}
|
||||
|
||||
Flag "include" str -> do
|
||||
new <- mapM parseNum $ filter (not . null) $ split ',' str
|
||||
let old = csIncludedWarnings . checkSpec $ options
|
||||
return options {
|
||||
checkSpec = (checkSpec options) {
|
||||
csIncludedWarnings =
|
||||
if null new
|
||||
then old
|
||||
else Just new `mappend` old
|
||||
}
|
||||
}
|
||||
|
||||
Flag "version" _ -> do
|
||||
liftIO printVersion
|
||||
throwError NoProblems
|
||||
|
||||
Flag "list-optional" _ -> do
|
||||
liftIO printOptional
|
||||
throwError NoProblems
|
||||
|
||||
Flag "help" _ -> do
|
||||
liftIO $ putStrLn getUsageInfo
|
||||
throwError NoProblems
|
||||
|
||||
Flag "externals" _ ->
|
||||
return options {
|
||||
externalSources = True
|
||||
@@ -284,6 +329,12 @@ parseOption flag options =
|
||||
}
|
||||
}
|
||||
|
||||
Flag "source-path" str -> do
|
||||
let paths = splitSearchPath str
|
||||
return options {
|
||||
sourcePaths = (sourcePaths options) ++ paths
|
||||
}
|
||||
|
||||
Flag "sourced" _ ->
|
||||
return options {
|
||||
checkSpec = (checkSpec options) {
|
||||
@@ -307,7 +358,26 @@ parseOption flag options =
|
||||
}
|
||||
}
|
||||
|
||||
_ -> return options
|
||||
Flag "norc" _ ->
|
||||
return options {
|
||||
checkSpec = (checkSpec options) {
|
||||
csIgnoreRC = True
|
||||
}
|
||||
}
|
||||
|
||||
Flag "enable" value ->
|
||||
let cs = checkSpec options in return options {
|
||||
checkSpec = cs {
|
||||
csOptionalChecks = (csOptionalChecks cs) ++ split ',' value
|
||||
}
|
||||
}
|
||||
|
||||
-- This flag is handled specially in 'process'
|
||||
Flag "format" _ -> return options
|
||||
|
||||
Flag str _ -> do
|
||||
printErr $ "Internal error for --" ++ str ++ ". Please file a bug :("
|
||||
return options
|
||||
where
|
||||
die s = do
|
||||
printErr s
|
||||
@@ -322,12 +392,16 @@ parseOption flag options =
|
||||
ioInterface options files = do
|
||||
inputs <- mapM normalize files
|
||||
cache <- newIORef emptyCache
|
||||
configCache <- newIORef ("", Nothing)
|
||||
return SystemInterface {
|
||||
siReadFile = get cache inputs
|
||||
siReadFile = get cache inputs,
|
||||
siFindSource = findSourceFile inputs (sourcePaths options),
|
||||
siGetConfig = getConfig configCache
|
||||
}
|
||||
where
|
||||
emptyCache :: Map.Map FilePath String
|
||||
emptyCache = Map.empty
|
||||
|
||||
get cache inputs file = do
|
||||
map <- readIORef cache
|
||||
case Map.lookup file map of
|
||||
@@ -344,7 +418,6 @@ ioInterface options files = do
|
||||
return $ Right contents
|
||||
) `catch` handler
|
||||
else return $ Left (file ++ " was not specified as input (see shellcheck -x).")
|
||||
|
||||
where
|
||||
handler :: IOException -> IO (Either ErrorMessage String)
|
||||
handler ex = return . Left $ show ex
|
||||
@@ -362,6 +435,88 @@ ioInterface options files = do
|
||||
fallback :: FilePath -> IOException -> IO FilePath
|
||||
fallback path _ = return path
|
||||
|
||||
-- Returns the name and contents of .shellcheckrc for the given file
|
||||
getConfig cache filename = do
|
||||
path <- normalize filename
|
||||
let dir = takeDirectory path
|
||||
(previousPath, result) <- readIORef cache
|
||||
if dir == previousPath
|
||||
then return result
|
||||
else do
|
||||
paths <- getConfigPaths dir
|
||||
result <- findConfig paths
|
||||
writeIORef cache (dir, result)
|
||||
return result
|
||||
|
||||
findConfig paths =
|
||||
case paths of
|
||||
(file:rest) -> do
|
||||
contents <- readConfig file
|
||||
if isJust contents
|
||||
then return contents
|
||||
else findConfig rest
|
||||
[] -> return Nothing
|
||||
|
||||
-- Get a list of candidate filenames. This includes .shellcheckrc
|
||||
-- in all parent directories, plus the user's home dir and xdg dir.
|
||||
-- The dot is optional for Windows and Snap users.
|
||||
getConfigPaths dir = do
|
||||
let next = takeDirectory dir
|
||||
rest <- if next /= dir
|
||||
then getConfigPaths next
|
||||
else defaultPaths `catch`
|
||||
((const $ return []) :: IOException -> IO [FilePath])
|
||||
return $ (dir </> ".shellcheckrc") : (dir </> "shellcheckrc") : rest
|
||||
|
||||
defaultPaths = do
|
||||
home <- getAppUserDataDirectory "shellcheckrc"
|
||||
xdg <- getXdgDirectory XdgConfig "shellcheckrc"
|
||||
return [home, xdg]
|
||||
|
||||
readConfig file = do
|
||||
exists <- doesFileExist file
|
||||
if exists
|
||||
then do
|
||||
(contents, _) <- inputFile file `catch` handler file
|
||||
return $ Just (file, contents)
|
||||
else
|
||||
return Nothing
|
||||
where
|
||||
handler :: FilePath -> IOException -> IO (String, Bool)
|
||||
handler file err = do
|
||||
putStrLn $ file ++ ": " ++ show err
|
||||
return ("", True)
|
||||
|
||||
andM a b arg = do
|
||||
first <- a arg
|
||||
if not first then return False else b arg
|
||||
|
||||
findM p = foldr go (pure Nothing)
|
||||
where
|
||||
go x acc = do
|
||||
b <- p x
|
||||
if b then pure (Just x) else acc
|
||||
|
||||
findSourceFile inputs sourcePathFlag currentScript sourcePathAnnotation original =
|
||||
if isAbsolute original
|
||||
then
|
||||
let (_, relative) = splitDrive original
|
||||
in find relative original
|
||||
else
|
||||
find original original
|
||||
where
|
||||
find filename deflt = do
|
||||
sources <- findM ((allowable inputs) `andM` doesFileExist) $
|
||||
(adjustPath filename):(map (</> filename) $ map adjustPath $ sourcePathFlag ++ sourcePathAnnotation)
|
||||
case sources of
|
||||
Nothing -> return deflt
|
||||
Just first -> return first
|
||||
scriptdir = dropFileName currentScript
|
||||
adjustPath str =
|
||||
case (splitDirectories str) of
|
||||
("SCRIPTDIR":rest) -> joinPath (scriptdir:rest)
|
||||
_ -> str
|
||||
|
||||
inputFile file = do
|
||||
(handle, shouldCache) <-
|
||||
if file == "-"
|
||||
@@ -418,3 +573,14 @@ printVersion = do
|
||||
putStrLn $ "version: " ++ shellcheckVersion
|
||||
putStrLn "license: GNU General Public License, version 3"
|
||||
putStrLn "website: https://www.shellcheck.net"
|
||||
|
||||
printOptional = do
|
||||
mapM f list
|
||||
where
|
||||
list = sortOn cdName ShellCheck.Analyzer.optionalChecks
|
||||
f item = do
|
||||
putStrLn $ "name: " ++ cdName item
|
||||
putStrLn $ "desc: " ++ cdDescription item
|
||||
putStrLn $ "example: " ++ cdPositive item
|
||||
putStrLn $ "fix: " ++ cdNegative item
|
||||
putStrLn ""
|
||||
|
@@ -23,7 +23,8 @@ description: |
|
||||
# snap connect shellcheck:removable-media
|
||||
|
||||
version: git
|
||||
grade: devel
|
||||
base: core18
|
||||
grade: stable
|
||||
confinement: strict
|
||||
|
||||
apps:
|
||||
@@ -34,13 +35,20 @@ apps:
|
||||
parts:
|
||||
shellcheck:
|
||||
plugin: dump
|
||||
source: ./
|
||||
source: .
|
||||
build-packages:
|
||||
- cabal-install
|
||||
build: |
|
||||
- squid
|
||||
override-build: |
|
||||
# See comments in .snapsquid.conf
|
||||
[ "$http_proxy" ] && {
|
||||
squid3 -f .snapsquid.conf
|
||||
export http_proxy="http://localhost:8888"
|
||||
sleep 3
|
||||
}
|
||||
cabal sandbox init
|
||||
cabal update
|
||||
cabal update || cat /var/log/squid/*
|
||||
cabal install -j
|
||||
install: |
|
||||
|
||||
install -d $SNAPCRAFT_PART_INSTALL/usr/bin
|
||||
install .cabal-sandbox/bin/shellcheck $SNAPCRAFT_PART_INSTALL/usr/bin
|
||||
|
@@ -1,5 +1,5 @@
|
||||
{-
|
||||
Copyright 2012-2015 Vidar Holen
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
@@ -17,7 +17,7 @@
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
-}
|
||||
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
|
||||
{-# LANGUAGE DeriveGeneric, DeriveAnyClass, DeriveTraversable, PatternSynonyms #-}
|
||||
module ShellCheck.AST where
|
||||
|
||||
import GHC.Generics (Generic)
|
||||
@@ -37,349 +37,241 @@ newtype FunctionParentheses = FunctionParentheses Bool deriving (Show, Eq)
|
||||
data CaseType = CaseBreak | CaseFallThrough | CaseContinue deriving (Show, Eq)
|
||||
|
||||
newtype Root = Root Token
|
||||
data Token =
|
||||
TA_Binary Id String Token Token
|
||||
| TA_Assignment Id String Token Token
|
||||
| TA_Variable Id String [Token]
|
||||
| TA_Expansion Id [Token]
|
||||
| TA_Sequence Id [Token]
|
||||
| TA_Trinary Id Token Token Token
|
||||
| TA_Unary Id String Token
|
||||
| TC_And Id ConditionType String Token Token
|
||||
| TC_Binary Id ConditionType String Token Token
|
||||
| TC_Group Id ConditionType Token
|
||||
| TC_Nullary Id ConditionType Token
|
||||
| TC_Or Id ConditionType String Token Token
|
||||
| TC_Unary Id ConditionType String Token
|
||||
| TC_Empty Id ConditionType
|
||||
| T_AND_IF Id
|
||||
| T_AndIf Id Token Token
|
||||
| T_Arithmetic Id Token
|
||||
| T_Array Id [Token]
|
||||
| T_IndexedElement Id [Token] Token
|
||||
data Token = OuterToken Id (InnerToken Token) deriving (Show)
|
||||
|
||||
data InnerToken t =
|
||||
Inner_TA_Binary String t t
|
||||
| Inner_TA_Assignment String t t
|
||||
| Inner_TA_Variable String [t]
|
||||
| Inner_TA_Expansion [t]
|
||||
| Inner_TA_Sequence [t]
|
||||
| Inner_TA_Trinary t t t
|
||||
| Inner_TA_Unary String t
|
||||
| Inner_TC_And ConditionType String t t
|
||||
| Inner_TC_Binary ConditionType String t t
|
||||
| Inner_TC_Group ConditionType t
|
||||
| Inner_TC_Nullary ConditionType t
|
||||
| Inner_TC_Or ConditionType String t t
|
||||
| Inner_TC_Unary ConditionType String t
|
||||
| Inner_TC_Empty ConditionType
|
||||
| Inner_T_AND_IF
|
||||
| Inner_T_AndIf t t
|
||||
| Inner_T_Arithmetic t
|
||||
| Inner_T_Array [t]
|
||||
| Inner_T_IndexedElement [t] t
|
||||
-- Store the index as string, and parse as arithmetic or string later
|
||||
| T_UnparsedIndex Id SourcePos String
|
||||
| T_Assignment Id AssignmentMode String [Token] Token
|
||||
| T_Backgrounded Id Token
|
||||
| T_Backticked Id [Token]
|
||||
| T_Bang Id
|
||||
| T_Banged Id Token
|
||||
| T_BraceExpansion Id [Token]
|
||||
| T_BraceGroup Id [Token]
|
||||
| T_CLOBBER Id
|
||||
| T_Case Id
|
||||
| T_CaseExpression Id Token [(CaseType, [Token], [Token])]
|
||||
| T_Condition Id ConditionType Token
|
||||
| T_DGREAT Id
|
||||
| T_DLESS Id
|
||||
| T_DLESSDASH Id
|
||||
| T_DSEMI Id
|
||||
| T_Do Id
|
||||
| T_DollarArithmetic Id Token
|
||||
| T_DollarBraced Id Token
|
||||
| T_DollarBracket Id Token
|
||||
| T_DollarDoubleQuoted Id [Token]
|
||||
| T_DollarExpansion Id [Token]
|
||||
| T_DollarSingleQuoted Id String
|
||||
| T_DollarBraceCommandExpansion Id [Token]
|
||||
| T_Done Id
|
||||
| T_DoubleQuoted Id [Token]
|
||||
| T_EOF Id
|
||||
| T_Elif Id
|
||||
| T_Else Id
|
||||
| T_Esac Id
|
||||
| T_Extglob Id String [Token]
|
||||
| T_FdRedirect Id String Token
|
||||
| T_Fi Id
|
||||
| T_For Id
|
||||
| T_ForArithmetic Id Token Token Token [Token]
|
||||
| T_ForIn Id String [Token] [Token]
|
||||
| T_Function Id FunctionKeyword FunctionParentheses String Token
|
||||
| T_GREATAND Id
|
||||
| T_Glob Id String
|
||||
| T_Greater Id
|
||||
| T_HereDoc Id Dashed Quoted String [Token]
|
||||
| T_HereString Id Token
|
||||
| T_If Id
|
||||
| T_IfExpression Id [([Token],[Token])] [Token]
|
||||
| T_In Id
|
||||
| T_IoFile Id Token Token
|
||||
| T_IoDuplicate Id Token String
|
||||
| T_LESSAND Id
|
||||
| T_LESSGREAT Id
|
||||
| T_Lbrace Id
|
||||
| T_Less Id
|
||||
| T_Literal Id String
|
||||
| T_Lparen Id
|
||||
| T_NEWLINE Id
|
||||
| T_NormalWord Id [Token]
|
||||
| T_OR_IF Id
|
||||
| T_OrIf Id Token Token
|
||||
| T_ParamSubSpecialChar Id String -- e.g. '%' in ${foo%bar} or '/' in ${foo/bar/baz}
|
||||
| T_Pipeline Id [Token] [Token] -- [Pipe separators] [Commands]
|
||||
| T_ProcSub Id String [Token]
|
||||
| T_Rbrace Id
|
||||
| T_Redirecting Id [Token] Token
|
||||
| T_Rparen Id
|
||||
| T_Script Id String [Token]
|
||||
| T_Select Id
|
||||
| T_SelectIn Id String [Token] [Token]
|
||||
| T_Semi Id
|
||||
| T_SimpleCommand Id [Token] [Token]
|
||||
| T_SingleQuoted Id String
|
||||
| T_Subshell Id [Token]
|
||||
| T_Then Id
|
||||
| T_Until Id
|
||||
| T_UntilExpression Id [Token] [Token]
|
||||
| T_While Id
|
||||
| T_WhileExpression Id [Token] [Token]
|
||||
| T_Annotation Id [Annotation] Token
|
||||
| T_Pipe Id String
|
||||
| T_CoProc Id (Maybe String) Token
|
||||
| T_CoProcBody Id Token
|
||||
| T_Include Id Token
|
||||
| T_SourceCommand Id Token Token
|
||||
deriving (Show)
|
||||
| Inner_T_UnparsedIndex SourcePos String
|
||||
| Inner_T_Assignment AssignmentMode String [t] t
|
||||
| Inner_T_Backgrounded t
|
||||
| Inner_T_Backticked [t]
|
||||
| Inner_T_Bang
|
||||
| Inner_T_Banged t
|
||||
| Inner_T_BraceExpansion [t]
|
||||
| Inner_T_BraceGroup [t]
|
||||
| Inner_T_CLOBBER
|
||||
| Inner_T_Case
|
||||
| Inner_T_CaseExpression t [(CaseType, [t], [t])]
|
||||
| Inner_T_Condition ConditionType t
|
||||
| Inner_T_DGREAT
|
||||
| Inner_T_DLESS
|
||||
| Inner_T_DLESSDASH
|
||||
| Inner_T_DSEMI
|
||||
| Inner_T_Do
|
||||
| Inner_T_DollarArithmetic t
|
||||
| Inner_T_DollarBraced Bool t
|
||||
| Inner_T_DollarBracket t
|
||||
| Inner_T_DollarDoubleQuoted [t]
|
||||
| Inner_T_DollarExpansion [t]
|
||||
| Inner_T_DollarSingleQuoted String
|
||||
| Inner_T_DollarBraceCommandExpansion [t]
|
||||
| Inner_T_Done
|
||||
| Inner_T_DoubleQuoted [t]
|
||||
| Inner_T_EOF
|
||||
| Inner_T_Elif
|
||||
| Inner_T_Else
|
||||
| Inner_T_Esac
|
||||
| Inner_T_Extglob String [t]
|
||||
| Inner_T_FdRedirect String t
|
||||
| Inner_T_Fi
|
||||
| Inner_T_For
|
||||
| Inner_T_ForArithmetic t t t [t]
|
||||
| Inner_T_ForIn String [t] [t]
|
||||
| Inner_T_Function FunctionKeyword FunctionParentheses String t
|
||||
| Inner_T_GREATAND
|
||||
| Inner_T_Glob String
|
||||
| Inner_T_Greater
|
||||
| Inner_T_HereDoc Dashed Quoted String [t]
|
||||
| Inner_T_HereString t
|
||||
| Inner_T_If
|
||||
| Inner_T_IfExpression [([t],[t])] [t]
|
||||
| Inner_T_In
|
||||
| Inner_T_IoFile t t
|
||||
| Inner_T_IoDuplicate t String
|
||||
| Inner_T_LESSAND
|
||||
| Inner_T_LESSGREAT
|
||||
| Inner_T_Lbrace
|
||||
| Inner_T_Less
|
||||
| Inner_T_Literal String
|
||||
| Inner_T_Lparen
|
||||
| Inner_T_NEWLINE
|
||||
| Inner_T_NormalWord [t]
|
||||
| Inner_T_OR_IF
|
||||
| Inner_T_OrIf t t
|
||||
| Inner_T_ParamSubSpecialChar String -- e.g. '%' in ${foo%bar} or '/' in ${foo/bar/baz}
|
||||
| Inner_T_Pipeline [t] [t] -- [Pipe separators] [Commands]
|
||||
| Inner_T_ProcSub String [t]
|
||||
| Inner_T_Rbrace
|
||||
| Inner_T_Redirecting [t] t
|
||||
| Inner_T_Rparen
|
||||
| Inner_T_Script t [t] -- Shebang T_Literal, followed by script.
|
||||
| Inner_T_Select
|
||||
| Inner_T_SelectIn String [t] [t]
|
||||
| Inner_T_Semi
|
||||
| Inner_T_SimpleCommand [t] [t]
|
||||
| Inner_T_SingleQuoted String
|
||||
| Inner_T_Subshell [t]
|
||||
| Inner_T_Then
|
||||
| Inner_T_Until
|
||||
| Inner_T_UntilExpression [t] [t]
|
||||
| Inner_T_While
|
||||
| Inner_T_WhileExpression [t] [t]
|
||||
| Inner_T_Annotation [Annotation] t
|
||||
| Inner_T_Pipe String
|
||||
| Inner_T_CoProc (Maybe String) t
|
||||
| Inner_T_CoProcBody t
|
||||
| Inner_T_Include t
|
||||
| Inner_T_SourceCommand t t
|
||||
| Inner_T_BatsTest t t
|
||||
deriving (Show, Eq, Functor, Foldable, Traversable)
|
||||
|
||||
data Annotation =
|
||||
DisableComment Integer
|
||||
| EnableComment String
|
||||
| SourceOverride String
|
||||
| ShellOverride String
|
||||
| SourcePath String
|
||||
deriving (Show, Eq)
|
||||
data ConditionType = DoubleBracket | SingleBracket deriving (Show, Eq)
|
||||
|
||||
-- This is an abomination.
|
||||
tokenEquals :: Token -> Token -> Bool
|
||||
tokenEquals a b = kludge a == kludge b
|
||||
where kludge s = Re.subRegex (Re.mkRegex "\\(Id [0-9]+\\)") (show s) "(Id 0)"
|
||||
pattern T_AND_IF id = OuterToken id Inner_T_AND_IF
|
||||
pattern T_Bang id = OuterToken id Inner_T_Bang
|
||||
pattern T_Case id = OuterToken id Inner_T_Case
|
||||
pattern TC_Empty id typ = OuterToken id (Inner_TC_Empty typ)
|
||||
pattern T_CLOBBER id = OuterToken id Inner_T_CLOBBER
|
||||
pattern T_DGREAT id = OuterToken id Inner_T_DGREAT
|
||||
pattern T_DLESS id = OuterToken id Inner_T_DLESS
|
||||
pattern T_DLESSDASH id = OuterToken id Inner_T_DLESSDASH
|
||||
pattern T_Do id = OuterToken id Inner_T_Do
|
||||
pattern T_DollarSingleQuoted id str = OuterToken id (Inner_T_DollarSingleQuoted str)
|
||||
pattern T_Done id = OuterToken id Inner_T_Done
|
||||
pattern T_DSEMI id = OuterToken id Inner_T_DSEMI
|
||||
pattern T_Elif id = OuterToken id Inner_T_Elif
|
||||
pattern T_Else id = OuterToken id Inner_T_Else
|
||||
pattern T_EOF id = OuterToken id Inner_T_EOF
|
||||
pattern T_Esac id = OuterToken id Inner_T_Esac
|
||||
pattern T_Fi id = OuterToken id Inner_T_Fi
|
||||
pattern T_For id = OuterToken id Inner_T_For
|
||||
pattern T_Glob id str = OuterToken id (Inner_T_Glob str)
|
||||
pattern T_GREATAND id = OuterToken id Inner_T_GREATAND
|
||||
pattern T_Greater id = OuterToken id Inner_T_Greater
|
||||
pattern T_If id = OuterToken id Inner_T_If
|
||||
pattern T_In id = OuterToken id Inner_T_In
|
||||
pattern T_Lbrace id = OuterToken id Inner_T_Lbrace
|
||||
pattern T_Less id = OuterToken id Inner_T_Less
|
||||
pattern T_LESSAND id = OuterToken id Inner_T_LESSAND
|
||||
pattern T_LESSGREAT id = OuterToken id Inner_T_LESSGREAT
|
||||
pattern T_Literal id str = OuterToken id (Inner_T_Literal str)
|
||||
pattern T_Lparen id = OuterToken id Inner_T_Lparen
|
||||
pattern T_NEWLINE id = OuterToken id Inner_T_NEWLINE
|
||||
pattern T_OR_IF id = OuterToken id Inner_T_OR_IF
|
||||
pattern T_ParamSubSpecialChar id str = OuterToken id (Inner_T_ParamSubSpecialChar str)
|
||||
pattern T_Pipe id str = OuterToken id (Inner_T_Pipe str)
|
||||
pattern T_Rbrace id = OuterToken id Inner_T_Rbrace
|
||||
pattern T_Rparen id = OuterToken id Inner_T_Rparen
|
||||
pattern T_Select id = OuterToken id Inner_T_Select
|
||||
pattern T_Semi id = OuterToken id Inner_T_Semi
|
||||
pattern T_SingleQuoted id str = OuterToken id (Inner_T_SingleQuoted str)
|
||||
pattern T_Then id = OuterToken id Inner_T_Then
|
||||
pattern T_UnparsedIndex id pos str = OuterToken id (Inner_T_UnparsedIndex pos str)
|
||||
pattern T_Until id = OuterToken id Inner_T_Until
|
||||
pattern T_While id = OuterToken id Inner_T_While
|
||||
pattern TA_Assignment id op t1 t2 = OuterToken id (Inner_TA_Assignment op t1 t2)
|
||||
pattern TA_Binary id op t1 t2 = OuterToken id (Inner_TA_Binary op t1 t2)
|
||||
pattern TA_Expansion id t = OuterToken id (Inner_TA_Expansion t)
|
||||
pattern T_AndIf id t u = OuterToken id (Inner_T_AndIf t u)
|
||||
pattern T_Annotation id anns t = OuterToken id (Inner_T_Annotation anns t)
|
||||
pattern T_Arithmetic id c = OuterToken id (Inner_T_Arithmetic c)
|
||||
pattern T_Array id t = OuterToken id (Inner_T_Array t)
|
||||
pattern TA_Sequence id l = OuterToken id (Inner_TA_Sequence l)
|
||||
pattern T_Assignment id mode var indices value = OuterToken id (Inner_T_Assignment mode var indices value)
|
||||
pattern TA_Trinary id t1 t2 t3 = OuterToken id (Inner_TA_Trinary t1 t2 t3)
|
||||
pattern TA_Unary id op t1 = OuterToken id (Inner_TA_Unary op t1)
|
||||
pattern TA_Variable id str t = OuterToken id (Inner_TA_Variable str t)
|
||||
pattern T_Backgrounded id l = OuterToken id (Inner_T_Backgrounded l)
|
||||
pattern T_Backticked id list = OuterToken id (Inner_T_Backticked list)
|
||||
pattern T_Banged id l = OuterToken id (Inner_T_Banged l)
|
||||
pattern T_BatsTest id name t = OuterToken id (Inner_T_BatsTest name t)
|
||||
pattern T_BraceExpansion id list = OuterToken id (Inner_T_BraceExpansion list)
|
||||
pattern T_BraceGroup id l = OuterToken id (Inner_T_BraceGroup l)
|
||||
pattern TC_And id typ str t1 t2 = OuterToken id (Inner_TC_And typ str t1 t2)
|
||||
pattern T_CaseExpression id word cases = OuterToken id (Inner_T_CaseExpression word cases)
|
||||
pattern TC_Binary id typ op lhs rhs = OuterToken id (Inner_TC_Binary typ op lhs rhs)
|
||||
pattern TC_Group id typ token = OuterToken id (Inner_TC_Group typ token)
|
||||
pattern TC_Nullary id typ token = OuterToken id (Inner_TC_Nullary typ token)
|
||||
pattern T_Condition id typ token = OuterToken id (Inner_T_Condition typ token)
|
||||
pattern T_CoProcBody id t = OuterToken id (Inner_T_CoProcBody t)
|
||||
pattern T_CoProc id var body = OuterToken id (Inner_T_CoProc var body)
|
||||
pattern TC_Or id typ str t1 t2 = OuterToken id (Inner_TC_Or typ str t1 t2)
|
||||
pattern TC_Unary id typ op token = OuterToken id (Inner_TC_Unary typ op token)
|
||||
pattern T_DollarArithmetic id c = OuterToken id (Inner_T_DollarArithmetic c)
|
||||
pattern T_DollarBraceCommandExpansion id list = OuterToken id (Inner_T_DollarBraceCommandExpansion list)
|
||||
pattern T_DollarBraced id braced op = OuterToken id (Inner_T_DollarBraced braced op)
|
||||
pattern T_DollarBracket id c = OuterToken id (Inner_T_DollarBracket c)
|
||||
pattern T_DollarDoubleQuoted id list = OuterToken id (Inner_T_DollarDoubleQuoted list)
|
||||
pattern T_DollarExpansion id list = OuterToken id (Inner_T_DollarExpansion list)
|
||||
pattern T_DoubleQuoted id list = OuterToken id (Inner_T_DoubleQuoted list)
|
||||
pattern T_Extglob id str l = OuterToken id (Inner_T_Extglob str l)
|
||||
pattern T_FdRedirect id v t = OuterToken id (Inner_T_FdRedirect v t)
|
||||
pattern T_ForArithmetic id a b c group = OuterToken id (Inner_T_ForArithmetic a b c group)
|
||||
pattern T_ForIn id v w l = OuterToken id (Inner_T_ForIn v w l)
|
||||
pattern T_Function id a b name body = OuterToken id (Inner_T_Function a b name body)
|
||||
pattern T_HereDoc id d q str l = OuterToken id (Inner_T_HereDoc d q str l)
|
||||
pattern T_HereString id word = OuterToken id (Inner_T_HereString word)
|
||||
pattern T_IfExpression id conditions elses = OuterToken id (Inner_T_IfExpression conditions elses)
|
||||
pattern T_Include id script = OuterToken id (Inner_T_Include script)
|
||||
pattern T_IndexedElement id indices t = OuterToken id (Inner_T_IndexedElement indices t)
|
||||
pattern T_IoDuplicate id op num = OuterToken id (Inner_T_IoDuplicate op num)
|
||||
pattern T_IoFile id op file = OuterToken id (Inner_T_IoFile op file)
|
||||
pattern T_NormalWord id list = OuterToken id (Inner_T_NormalWord list)
|
||||
pattern T_OrIf id t u = OuterToken id (Inner_T_OrIf t u)
|
||||
pattern T_Pipeline id l1 l2 = OuterToken id (Inner_T_Pipeline l1 l2)
|
||||
pattern T_ProcSub id typ l = OuterToken id (Inner_T_ProcSub typ l)
|
||||
pattern T_Redirecting id redirs cmd = OuterToken id (Inner_T_Redirecting redirs cmd)
|
||||
pattern T_Script id shebang list = OuterToken id (Inner_T_Script shebang list)
|
||||
pattern T_SelectIn id v w l = OuterToken id (Inner_T_SelectIn v w l)
|
||||
pattern T_SimpleCommand id vars cmds = OuterToken id (Inner_T_SimpleCommand vars cmds)
|
||||
pattern T_SourceCommand id includer t_include = OuterToken id (Inner_T_SourceCommand includer t_include)
|
||||
pattern T_Subshell id l = OuterToken id (Inner_T_Subshell l)
|
||||
pattern T_UntilExpression id c l = OuterToken id (Inner_T_UntilExpression c l)
|
||||
pattern T_WhileExpression id c l = OuterToken id (Inner_T_WhileExpression c l)
|
||||
|
||||
{-# COMPLETE T_AND_IF, T_Bang, T_Case, TC_Empty, T_CLOBBER, T_DGREAT, T_DLESS, T_DLESSDASH, T_Do, T_DollarSingleQuoted, T_Done, T_DSEMI, T_Elif, T_Else, T_EOF, T_Esac, T_Fi, T_For, T_Glob, T_GREATAND, T_Greater, T_If, T_In, T_Lbrace, T_Less, T_LESSAND, T_LESSGREAT, T_Literal, T_Lparen, T_NEWLINE, T_OR_IF, T_ParamSubSpecialChar, T_Pipe, T_Rbrace, T_Rparen, T_Select, T_Semi, T_SingleQuoted, T_Then, T_UnparsedIndex, T_Until, T_While, TA_Assignment, TA_Binary, TA_Expansion, T_AndIf, T_Annotation, T_Arithmetic, T_Array, TA_Sequence, T_Assignment, TA_Trinary, TA_Unary, TA_Variable, T_Backgrounded, T_Backticked, T_Banged, T_BatsTest, T_BraceExpansion, T_BraceGroup, TC_And, T_CaseExpression, TC_Binary, TC_Group, TC_Nullary, T_Condition, T_CoProcBody, T_CoProc, TC_Or, TC_Unary, T_DollarArithmetic, T_DollarBraceCommandExpansion, T_DollarBraced, T_DollarBracket, T_DollarDoubleQuoted, T_DollarExpansion, T_DoubleQuoted, T_Extglob, T_FdRedirect, T_ForArithmetic, T_ForIn, T_Function, T_HereDoc, T_HereString, T_IfExpression, T_Include, T_IndexedElement, T_IoDuplicate, T_IoFile, T_NormalWord, T_OrIf, T_Pipeline, T_ProcSub, T_Redirecting, T_Script, T_SelectIn, T_SimpleCommand, T_SourceCommand, T_Subshell, T_UntilExpression, T_WhileExpression #-}
|
||||
|
||||
instance Eq Token where
|
||||
(==) = tokenEquals
|
||||
OuterToken _ a == OuterToken _ b = a == b
|
||||
|
||||
analyze :: Monad m => (Token -> m ()) -> (Token -> m ()) -> (Token -> m Token) -> Token -> m Token
|
||||
analyze f g i =
|
||||
round
|
||||
where
|
||||
round t = do
|
||||
round t@(OuterToken id it) = do
|
||||
f t
|
||||
newT <- delve t
|
||||
newIt <- traverse round it
|
||||
g t
|
||||
i newT
|
||||
roundAll = mapM round
|
||||
|
||||
dl l v = do
|
||||
x <- roundAll l
|
||||
return $ v x
|
||||
dll l m v = do
|
||||
x <- roundAll l
|
||||
y <- roundAll m
|
||||
return $ v x y
|
||||
d1 t v = do
|
||||
x <- round t
|
||||
return $ v x
|
||||
d2 t1 t2 v = do
|
||||
x <- round t1
|
||||
y <- round t2
|
||||
return $ v x y
|
||||
|
||||
delve (T_NormalWord id list) = dl list $ T_NormalWord id
|
||||
delve (T_DoubleQuoted id list) = dl list $ T_DoubleQuoted id
|
||||
delve (T_DollarDoubleQuoted id list) = dl list $ T_DollarDoubleQuoted id
|
||||
delve (T_DollarExpansion id list) = dl list $ T_DollarExpansion id
|
||||
delve (T_DollarBraceCommandExpansion id list) = dl list $ T_DollarBraceCommandExpansion id
|
||||
delve (T_BraceExpansion id list) = dl list $ T_BraceExpansion id
|
||||
delve (T_Backticked id list) = dl list $ T_Backticked id
|
||||
delve (T_DollarArithmetic id c) = d1 c $ T_DollarArithmetic id
|
||||
delve (T_DollarBracket id c) = d1 c $ T_DollarBracket id
|
||||
delve (T_IoFile id op file) = d2 op file $ T_IoFile id
|
||||
delve (T_IoDuplicate id op num) = d1 op $ \x -> T_IoDuplicate id x num
|
||||
delve (T_HereString id word) = d1 word $ T_HereString id
|
||||
delve (T_FdRedirect id v t) = d1 t $ T_FdRedirect id v
|
||||
delve (T_Assignment id mode var indices value) = do
|
||||
a <- roundAll indices
|
||||
b <- round value
|
||||
return $ T_Assignment id mode var a b
|
||||
delve (T_Array id t) = dl t $ T_Array id
|
||||
delve (T_IndexedElement id indices t) = do
|
||||
a <- roundAll indices
|
||||
b <- round t
|
||||
return $ T_IndexedElement id a b
|
||||
delve (T_Redirecting id redirs cmd) = do
|
||||
a <- roundAll redirs
|
||||
b <- round cmd
|
||||
return $ T_Redirecting id a b
|
||||
delve (T_SimpleCommand id vars cmds) = dll vars cmds $ T_SimpleCommand id
|
||||
delve (T_Pipeline id l1 l2) = dll l1 l2 $ T_Pipeline id
|
||||
delve (T_Banged id l) = d1 l $ T_Banged id
|
||||
delve (T_AndIf id t u) = d2 t u $ T_AndIf id
|
||||
delve (T_OrIf id t u) = d2 t u $ T_OrIf id
|
||||
delve (T_Backgrounded id l) = d1 l $ T_Backgrounded id
|
||||
delve (T_Subshell id l) = dl l $ T_Subshell id
|
||||
delve (T_ProcSub id typ l) = dl l $ T_ProcSub id typ
|
||||
delve (T_Arithmetic id c) = d1 c $ T_Arithmetic id
|
||||
delve (T_IfExpression id conditions elses) = do
|
||||
newConds <- mapM (\(c, t) -> do
|
||||
x <- mapM round c
|
||||
y <- mapM round t
|
||||
return (x,y)
|
||||
) conditions
|
||||
newElses <- roundAll elses
|
||||
return $ T_IfExpression id newConds newElses
|
||||
delve (T_BraceGroup id l) = dl l $ T_BraceGroup id
|
||||
delve (T_WhileExpression id c l) = dll c l $ T_WhileExpression id
|
||||
delve (T_UntilExpression id c l) = dll c l $ T_UntilExpression id
|
||||
delve (T_ForIn id v w l) = dll w l $ T_ForIn id v
|
||||
delve (T_SelectIn id v w l) = dll w l $ T_SelectIn id v
|
||||
delve (T_CaseExpression id word cases) = do
|
||||
newWord <- round word
|
||||
newCases <- mapM (\(o, c, t) -> do
|
||||
x <- mapM round c
|
||||
y <- mapM round t
|
||||
return (o, x,y)
|
||||
) cases
|
||||
return $ T_CaseExpression id newWord newCases
|
||||
|
||||
delve (T_ForArithmetic id a b c group) = do
|
||||
x <- round a
|
||||
y <- round b
|
||||
z <- round c
|
||||
list <- mapM round group
|
||||
return $ T_ForArithmetic id x y z list
|
||||
|
||||
delve (T_Script id s l) = dl l $ T_Script id s
|
||||
delve (T_Function id a b name body) = d1 body $ T_Function id a b name
|
||||
delve (T_Condition id typ token) = d1 token $ T_Condition id typ
|
||||
delve (T_Extglob id str l) = dl l $ T_Extglob id str
|
||||
delve (T_DollarBraced id op) = d1 op $ T_DollarBraced id
|
||||
delve (T_HereDoc id d q str l) = dl l $ T_HereDoc id d q str
|
||||
|
||||
delve (TC_And id typ str t1 t2) = d2 t1 t2 $ TC_And id typ str
|
||||
delve (TC_Or id typ str t1 t2) = d2 t1 t2 $ TC_Or id typ str
|
||||
delve (TC_Group id typ token) = d1 token $ TC_Group id typ
|
||||
delve (TC_Binary id typ op lhs rhs) = d2 lhs rhs $ TC_Binary id typ op
|
||||
delve (TC_Unary id typ op token) = d1 token $ TC_Unary id typ op
|
||||
delve (TC_Nullary id typ token) = d1 token $ TC_Nullary id typ
|
||||
|
||||
delve (TA_Binary id op t1 t2) = d2 t1 t2 $ TA_Binary id op
|
||||
delve (TA_Assignment id op t1 t2) = d2 t1 t2 $ TA_Assignment id op
|
||||
delve (TA_Unary id op t1) = d1 t1 $ TA_Unary id op
|
||||
delve (TA_Sequence id l) = dl l $ TA_Sequence id
|
||||
delve (TA_Trinary id t1 t2 t3) = do
|
||||
a <- round t1
|
||||
b <- round t2
|
||||
c <- round t3
|
||||
return $ TA_Trinary id a b c
|
||||
delve (TA_Expansion id t) = dl t $ TA_Expansion id
|
||||
delve (TA_Variable id str t) = dl t $ TA_Variable id str
|
||||
delve (T_Annotation id anns t) = d1 t $ T_Annotation id anns
|
||||
delve (T_CoProc id var body) = d1 body $ T_CoProc id var
|
||||
delve (T_CoProcBody id t) = d1 t $ T_CoProcBody id
|
||||
delve (T_Include id script) = d1 script $ T_Include id
|
||||
delve (T_SourceCommand id includer t_include) = d2 includer t_include $ T_SourceCommand id
|
||||
delve t = return t
|
||||
i (OuterToken id newIt)
|
||||
|
||||
getId :: Token -> Id
|
||||
getId t = case t of
|
||||
T_AND_IF id -> id
|
||||
T_OR_IF id -> id
|
||||
T_DSEMI id -> id
|
||||
T_Semi id -> id
|
||||
T_DLESS id -> id
|
||||
T_DGREAT id -> id
|
||||
T_LESSAND id -> id
|
||||
T_GREATAND id -> id
|
||||
T_LESSGREAT id -> id
|
||||
T_DLESSDASH id -> id
|
||||
T_CLOBBER id -> id
|
||||
T_If id -> id
|
||||
T_Then id -> id
|
||||
T_Else id -> id
|
||||
T_Elif id -> id
|
||||
T_Fi id -> id
|
||||
T_Do id -> id
|
||||
T_Done id -> id
|
||||
T_Case id -> id
|
||||
T_Esac id -> id
|
||||
T_While id -> id
|
||||
T_Until id -> id
|
||||
T_For id -> id
|
||||
T_Select id -> id
|
||||
T_Lbrace id -> id
|
||||
T_Rbrace id -> id
|
||||
T_Lparen id -> id
|
||||
T_Rparen id -> id
|
||||
T_Bang id -> id
|
||||
T_In id -> id
|
||||
T_NEWLINE id -> id
|
||||
T_EOF id -> id
|
||||
T_Less id -> id
|
||||
T_Greater id -> id
|
||||
T_SingleQuoted id _ -> id
|
||||
T_Literal id _ -> id
|
||||
T_NormalWord id _ -> id
|
||||
T_DoubleQuoted id _ -> id
|
||||
T_DollarExpansion id _ -> id
|
||||
T_DollarBraced id _ -> id
|
||||
T_DollarArithmetic id _ -> id
|
||||
T_BraceExpansion id _ -> id
|
||||
T_ParamSubSpecialChar id _ -> id
|
||||
T_DollarBraceCommandExpansion id _ -> id
|
||||
T_IoFile id _ _ -> id
|
||||
T_IoDuplicate id _ _ -> id
|
||||
T_HereDoc id _ _ _ _ -> id
|
||||
T_HereString id _ -> id
|
||||
T_FdRedirect id _ _ -> id
|
||||
T_Assignment id _ _ _ _ -> id
|
||||
T_Array id _ -> id
|
||||
T_IndexedElement id _ _ -> id
|
||||
T_Redirecting id _ _ -> id
|
||||
T_SimpleCommand id _ _ -> id
|
||||
T_Pipeline id _ _ -> id
|
||||
T_Banged id _ -> id
|
||||
T_AndIf id _ _ -> id
|
||||
T_OrIf id _ _ -> id
|
||||
T_Backgrounded id _ -> id
|
||||
T_IfExpression id _ _ -> id
|
||||
T_Subshell id _ -> id
|
||||
T_BraceGroup id _ -> id
|
||||
T_WhileExpression id _ _ -> id
|
||||
T_UntilExpression id _ _ -> id
|
||||
T_ForIn id _ _ _ -> id
|
||||
T_SelectIn id _ _ _ -> id
|
||||
T_CaseExpression id _ _ -> id
|
||||
T_Function id _ _ _ _ -> id
|
||||
T_Arithmetic id _ -> id
|
||||
T_Script id _ _ -> id
|
||||
T_Condition id _ _ -> id
|
||||
T_Extglob id _ _ -> id
|
||||
T_Backticked id _ -> id
|
||||
TC_And id _ _ _ _ -> id
|
||||
TC_Or id _ _ _ _ -> id
|
||||
TC_Group id _ _ -> id
|
||||
TC_Binary id _ _ _ _ -> id
|
||||
TC_Unary id _ _ _ -> id
|
||||
TC_Nullary id _ _ -> id
|
||||
TA_Binary id _ _ _ -> id
|
||||
TA_Assignment id _ _ _ -> id
|
||||
TA_Unary id _ _ -> id
|
||||
TA_Sequence id _ -> id
|
||||
TA_Trinary id _ _ _ -> id
|
||||
TA_Expansion id _ -> id
|
||||
T_ProcSub id _ _ -> id
|
||||
T_Glob id _ -> id
|
||||
T_ForArithmetic id _ _ _ _ -> id
|
||||
T_DollarSingleQuoted id _ -> id
|
||||
T_DollarDoubleQuoted id _ -> id
|
||||
T_DollarBracket id _ -> id
|
||||
T_Annotation id _ _ -> id
|
||||
T_Pipe id _ -> id
|
||||
T_CoProc id _ _ -> id
|
||||
T_CoProcBody id _ -> id
|
||||
T_Include id _ -> id
|
||||
T_SourceCommand id _ _ -> id
|
||||
T_UnparsedIndex id _ _ -> id
|
||||
TC_Empty id _ -> id
|
||||
TA_Variable id _ _ -> id
|
||||
getId (OuterToken id _) = id
|
||||
|
||||
blank :: Monad m => Token -> m ()
|
||||
blank = const $ return ()
|
||||
|
@@ -1,5 +1,5 @@
|
||||
{-
|
||||
Copyright 2012-2015 Vidar Holen
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
@@ -25,6 +25,7 @@ import Control.Monad.Writer
|
||||
import Control.Monad
|
||||
import Data.Char
|
||||
import Data.Functor
|
||||
import Data.Functor.Identity
|
||||
import Data.List
|
||||
import Data.Maybe
|
||||
|
||||
@@ -46,6 +47,7 @@ willSplit x =
|
||||
T_BraceExpansion {} -> True
|
||||
T_Glob {} -> True
|
||||
T_Extglob {} -> True
|
||||
T_DoubleQuoted _ l -> any willBecomeMultipleArgs l
|
||||
T_NormalWord _ l -> any willSplit l
|
||||
_ -> False
|
||||
|
||||
@@ -81,7 +83,7 @@ oversimplify token =
|
||||
(T_NormalWord _ l) -> [concat (concatMap oversimplify l)]
|
||||
(T_DoubleQuoted _ l) -> [concat (concatMap oversimplify l)]
|
||||
(T_SingleQuoted _ s) -> [s]
|
||||
(T_DollarBraced _ _) -> ["${VAR}"]
|
||||
(T_DollarBraced _ _ _) -> ["${VAR}"]
|
||||
(T_DollarArithmetic _ _) -> ["${VAR}"]
|
||||
(T_DollarExpansion _ _) -> ["${VAR}"]
|
||||
(T_Backticked _ _) -> ["${VAR}"]
|
||||
@@ -133,11 +135,11 @@ isUnquotedFlag token = fromMaybe False $ do
|
||||
return $ "-" `isPrefixOf` str
|
||||
|
||||
-- Given a T_DollarBraced, return a simplified version of the string contents.
|
||||
bracedString (T_DollarBraced _ l) = concat $ oversimplify l
|
||||
bracedString (T_DollarBraced _ _ l) = concat $ oversimplify l
|
||||
bracedString _ = error "Internal shellcheck error, please report! (bracedString on non-variable)"
|
||||
|
||||
-- Is this an expansion of multiple items of an array?
|
||||
isArrayExpansion t@(T_DollarBraced _ _) =
|
||||
isArrayExpansion t@(T_DollarBraced _ _ _) =
|
||||
let string = bracedString t in
|
||||
"@" `isPrefixOf` string ||
|
||||
not ("#" `isPrefixOf` string) && "[@]" `isInfixOf` string
|
||||
@@ -146,7 +148,7 @@ isArrayExpansion _ = False
|
||||
-- Is it possible that this arg becomes multiple args?
|
||||
mayBecomeMultipleArgs t = willBecomeMultipleArgs t || f t
|
||||
where
|
||||
f t@(T_DollarBraced _ _) =
|
||||
f t@(T_DollarBraced _ _ _) =
|
||||
let string = bracedString t in
|
||||
"!" `isPrefixOf` string
|
||||
f (T_DoubleQuoted _ parts) = any f parts
|
||||
@@ -175,9 +177,13 @@ willConcatInAssignment token =
|
||||
getLiteralString :: Token -> Maybe String
|
||||
getLiteralString = getLiteralStringExt (const Nothing)
|
||||
|
||||
-- Definitely get a literal string, with a given default for all non-literals
|
||||
getLiteralStringDef :: String -> Token -> String
|
||||
getLiteralStringDef x = runIdentity . getLiteralStringExt (const $ return x)
|
||||
|
||||
-- Definitely get a literal string, skipping over all non-literals
|
||||
onlyLiteralString :: Token -> String
|
||||
onlyLiteralString = fromJust . getLiteralStringExt (const $ return "")
|
||||
onlyLiteralString = getLiteralStringDef ""
|
||||
|
||||
-- Maybe get a literal string, but only if it's an unquoted argument.
|
||||
getUnquotedLiteral (T_NormalWord _ list) =
|
||||
@@ -216,7 +222,7 @@ getGlobOrLiteralString = getLiteralStringExt f
|
||||
|
||||
-- Maybe get the literal value of a token, using a custom function
|
||||
-- to map unrecognized Tokens into strings.
|
||||
getLiteralStringExt :: (Token -> Maybe String) -> Token -> Maybe String
|
||||
getLiteralStringExt :: Monad m => (Token -> m String) -> Token -> m String
|
||||
getLiteralStringExt more = g
|
||||
where
|
||||
allInList = fmap concat . mapM g
|
||||
@@ -297,6 +303,11 @@ getCommand t =
|
||||
getCommandName :: Token -> Maybe String
|
||||
getCommandName = fst . getCommandNameAndToken
|
||||
|
||||
-- Maybe get the name+arguments of a command.
|
||||
getCommandArgv t = do
|
||||
(T_SimpleCommand _ _ args@(_:_)) <- getCommand t
|
||||
return args
|
||||
|
||||
-- Get the command name token from a command, i.e.
|
||||
-- the token representing 'ls' in 'ls -la 2> foo'.
|
||||
-- If it can't be determined, return the original token.
|
||||
@@ -351,22 +362,34 @@ isOnlyRedirection t =
|
||||
|
||||
isFunction t = case t of T_Function {} -> True; _ -> False
|
||||
|
||||
-- Bats tests are functions for the purpose of 'local' and such
|
||||
isFunctionLike t =
|
||||
case t of
|
||||
T_Function {} -> True
|
||||
T_BatsTest {} -> True
|
||||
_ -> False
|
||||
|
||||
|
||||
isBraceExpansion t = case t of T_BraceExpansion {} -> True; _ -> False
|
||||
|
||||
-- Get the lists of commands from tokens that contain them, such as
|
||||
-- the body of while loops or branches of if statements.
|
||||
-- the conditions and bodies of while loops or branches of if statements.
|
||||
getCommandSequences :: Token -> [[Token]]
|
||||
getCommandSequences t =
|
||||
case t of
|
||||
T_Script _ _ cmds -> [cmds]
|
||||
T_BraceGroup _ cmds -> [cmds]
|
||||
T_Subshell _ cmds -> [cmds]
|
||||
T_WhileExpression _ _ cmds -> [cmds]
|
||||
T_UntilExpression _ _ cmds -> [cmds]
|
||||
T_WhileExpression _ cond cmds -> [cond, cmds]
|
||||
T_UntilExpression _ cond cmds -> [cond, cmds]
|
||||
T_ForIn _ _ _ cmds -> [cmds]
|
||||
T_ForArithmetic _ _ _ _ cmds -> [cmds]
|
||||
T_IfExpression _ thens elses -> map snd thens ++ [elses]
|
||||
T_IfExpression _ thens elses -> (concatMap (\(a,b) -> [a,b]) thens) ++ [elses]
|
||||
T_Annotation _ _ t -> getCommandSequences t
|
||||
|
||||
T_DollarExpansion _ cmds -> [cmds]
|
||||
T_DollarBraceCommandExpansion _ cmds -> [cmds]
|
||||
T_Backticked _ cmds -> [cmds]
|
||||
_ -> []
|
||||
|
||||
-- Get a list of names of associative arrays
|
||||
@@ -374,7 +397,7 @@ getAssociativeArrays t =
|
||||
nub . execWriter $ doAnalysis f t
|
||||
where
|
||||
f :: Token -> Writer [String] ()
|
||||
f t@T_SimpleCommand {} = fromMaybe (return ()) $ do
|
||||
f t@T_SimpleCommand {} = sequence_ $ do
|
||||
name <- getCommandName t
|
||||
let assocNames = ["declare","local","typeset"]
|
||||
guard $ elem name assocNames
|
||||
@@ -485,8 +508,21 @@ wordsCanBeEqual x y = fromMaybe True $
|
||||
-- Is this an expansion that can be quoted,
|
||||
-- e.g. $(foo) `foo` $foo (but not {foo,})?
|
||||
isQuoteableExpansion t = case t of
|
||||
T_DollarBraced {} -> True
|
||||
_ -> isCommandSubstitution t
|
||||
|
||||
isCommandSubstitution t = case t of
|
||||
T_DollarExpansion {} -> True
|
||||
T_DollarBraceCommandExpansion {} -> True
|
||||
T_Backticked {} -> True
|
||||
T_DollarBraced {} -> True
|
||||
_ -> False
|
||||
|
||||
|
||||
-- Is this a T_Annotation that ignores a specific code?
|
||||
isAnnotationIgnoringCode code t =
|
||||
case t of
|
||||
T_Annotation _ anns _ -> any hasNum anns
|
||||
_ -> False
|
||||
where
|
||||
hasNum (DisableComment ts) = code == ts
|
||||
hasNum _ = False
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
{-
|
||||
Copyright 2012-2015 Vidar Holen
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
@@ -17,7 +17,7 @@
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
-}
|
||||
module ShellCheck.Analyzer (analyzeScript) where
|
||||
module ShellCheck.Analyzer (analyzeScript, ShellCheck.Analyzer.optionalChecks) where
|
||||
|
||||
import ShellCheck.Analytics
|
||||
import ShellCheck.AnalyzerLib
|
||||
@@ -25,6 +25,7 @@ import ShellCheck.Interface
|
||||
import Data.List
|
||||
import Data.Monoid
|
||||
import qualified ShellCheck.Checks.Commands
|
||||
import qualified ShellCheck.Checks.Custom
|
||||
import qualified ShellCheck.Checks.ShellSupport
|
||||
|
||||
|
||||
@@ -34,12 +35,18 @@ analyzeScript spec = newAnalysisResult {
|
||||
arComments =
|
||||
filterByAnnotation spec params . nub $
|
||||
runAnalytics spec
|
||||
++ runChecker params (checkers params)
|
||||
++ runChecker params (checkers spec params)
|
||||
}
|
||||
where
|
||||
params = makeParameters spec
|
||||
|
||||
checkers params = mconcat $ map ($ params) [
|
||||
ShellCheck.Checks.Commands.checker,
|
||||
checkers spec params = mconcat $ map ($ params) [
|
||||
ShellCheck.Checks.Commands.checker spec,
|
||||
ShellCheck.Checks.Custom.checker,
|
||||
ShellCheck.Checks.ShellSupport.checker
|
||||
]
|
||||
|
||||
optionalChecks = mconcat $ [
|
||||
ShellCheck.Analytics.optionalChecks,
|
||||
ShellCheck.Checks.Commands.optionalChecks
|
||||
]
|
||||
|
@@ -1,5 +1,5 @@
|
||||
{-
|
||||
Copyright 2012-2015 Vidar Holen
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
@@ -77,15 +77,23 @@ composeAnalyzers :: (a -> Analysis) -> (a -> Analysis) -> a -> Analysis
|
||||
composeAnalyzers f g x = f x >> g x
|
||||
|
||||
data Parameters = Parameters {
|
||||
hasLastpipe :: Bool, -- Whether this script has the 'lastpipe' option set/default.
|
||||
hasSetE :: Bool, -- Whether this script has 'set -e' anywhere.
|
||||
variableFlow :: [StackData], -- A linear (bad) analysis of data flow
|
||||
parentMap :: Map.Map Id Token, -- A map from Id to parent Token
|
||||
shellType :: Shell, -- The shell type, such as Bash or Ksh
|
||||
shellTypeSpecified :: Bool, -- True if shell type was forced via flags
|
||||
rootNode :: Token, -- The root node of the AST
|
||||
tokenPositions :: Map.Map Id (Position, Position) -- map from token id to start and end position
|
||||
}
|
||||
-- Whether this script has the 'lastpipe' option set/default.
|
||||
hasLastpipe :: Bool,
|
||||
-- Whether this script has 'set -e' anywhere.
|
||||
hasSetE :: Bool,
|
||||
-- A linear (bad) analysis of data flow
|
||||
variableFlow :: [StackData],
|
||||
-- A map from Id to parent Token
|
||||
parentMap :: Map.Map Id Token,
|
||||
-- The shell type, such as Bash or Ksh
|
||||
shellType :: Shell,
|
||||
-- True if shell type was forced via flags
|
||||
shellTypeSpecified :: Bool,
|
||||
-- The root node of the AST
|
||||
rootNode :: Token,
|
||||
-- map from token id to start and end position
|
||||
tokenPositions :: Map.Map Id (Position, Position)
|
||||
} deriving (Show)
|
||||
|
||||
-- TODO: Cache results of common AST ops here
|
||||
data Cache = Cache {}
|
||||
@@ -112,11 +120,12 @@ data DataSource =
|
||||
|
||||
data VariableState = Dead Token String | Alive deriving (Show)
|
||||
|
||||
defaultSpec root = spec {
|
||||
defaultSpec pr = spec {
|
||||
asShellType = Nothing,
|
||||
asCheckSourced = False,
|
||||
asExecutionMode = Executed
|
||||
} where spec = newAnalysisSpec root
|
||||
asExecutionMode = Executed,
|
||||
asTokenPositions = prTokenPositions pr
|
||||
} where spec = newAnalysisSpec (fromJust $ prRoot pr)
|
||||
|
||||
pScript s =
|
||||
let
|
||||
@@ -124,13 +133,14 @@ pScript s =
|
||||
psFilename = "script",
|
||||
psScript = s
|
||||
}
|
||||
in prRoot . runIdentity $ parseScript (mockedSystemInterface []) pSpec
|
||||
in runIdentity $ parseScript (mockedSystemInterface []) pSpec
|
||||
|
||||
-- For testing. If parsed, returns whether there are any comments
|
||||
producesComments :: Checker -> String -> Maybe Bool
|
||||
producesComments c s = do
|
||||
root <- pScript s
|
||||
let spec = defaultSpec root
|
||||
let pr = pScript s
|
||||
prRoot pr
|
||||
let spec = defaultSpec pr
|
||||
let params = makeParameters spec
|
||||
return . not . null $ runChecker params c
|
||||
|
||||
@@ -153,11 +163,14 @@ err id code str = addComment $ makeComment ErrorC id code str
|
||||
info id code str = addComment $ makeComment InfoC id code str
|
||||
style id code str = addComment $ makeComment StyleC id code str
|
||||
|
||||
warnWithFix id code str fix = addComment $
|
||||
let comment = makeComment WarningC id code str in
|
||||
comment {
|
||||
tcFix = Just fix
|
||||
}
|
||||
warnWithFix :: MonadWriter [TokenComment] m => Id -> Code -> String -> Fix -> m ()
|
||||
warnWithFix = addCommentWithFix WarningC
|
||||
styleWithFix :: MonadWriter [TokenComment] m => Id -> Code -> String -> Fix -> m ()
|
||||
styleWithFix = addCommentWithFix StyleC
|
||||
|
||||
addCommentWithFix :: MonadWriter [TokenComment] m => Severity -> Id -> Code -> String -> Fix -> m ()
|
||||
addCommentWithFix severity id code str fix =
|
||||
addComment $ makeCommentWithFix severity id code str fix
|
||||
|
||||
makeCommentWithFix :: Severity -> Id -> Code -> String -> Fix -> TokenComment
|
||||
makeCommentWithFix severity id code str fix =
|
||||
@@ -170,7 +183,7 @@ makeCommentWithFix severity id code str fix =
|
||||
makeParameters spec =
|
||||
let params = Parameters {
|
||||
rootNode = root,
|
||||
shellType = fromMaybe (determineShell root) $ asShellType spec,
|
||||
shellType = fromMaybe (determineShell (asFallbackShell spec) root) $ asShellType spec,
|
||||
hasSetE = containsSetE root,
|
||||
hasLastpipe =
|
||||
case shellType params of
|
||||
@@ -179,7 +192,7 @@ makeParameters spec =
|
||||
Sh -> False
|
||||
Ksh -> True,
|
||||
|
||||
shellTypeSpecified = isJust $ asShellType spec,
|
||||
shellTypeSpecified = isJust (asShellType spec) || isJust (asFallbackShell spec),
|
||||
parentMap = getParentTree root,
|
||||
variableFlow = getVariableFlow params root,
|
||||
tokenPositions = asTokenPositions spec
|
||||
@@ -193,7 +206,7 @@ containsSetE root = isNothing $ doAnalysis (guard . not . isSetE) root
|
||||
where
|
||||
isSetE t =
|
||||
case t of
|
||||
T_Script _ str _ -> str `matches` re
|
||||
T_Script _ (T_Literal _ str) _ -> str `matches` re
|
||||
T_SimpleCommand {} ->
|
||||
t `isUnqualifiedCommand` "set" &&
|
||||
("errexit" `elem` oversimplify t ||
|
||||
@@ -214,37 +227,34 @@ containsLastpipe root =
|
||||
_ -> False
|
||||
|
||||
|
||||
prop_determineShell0 = determineShell (fromJust $ pScript "#!/bin/sh") == Sh
|
||||
prop_determineShell1 = determineShell (fromJust $ pScript "#!/usr/bin/env ksh") == Ksh
|
||||
prop_determineShell2 = determineShell (fromJust $ pScript "") == Bash
|
||||
prop_determineShell3 = determineShell (fromJust $ pScript "#!/bin/sh -e") == Sh
|
||||
prop_determineShell4 = determineShell (fromJust $ pScript
|
||||
"#!/bin/ksh\n#shellcheck shell=sh\nfoo") == Sh
|
||||
prop_determineShell5 = determineShell (fromJust $ pScript
|
||||
"#shellcheck shell=sh\nfoo") == Sh
|
||||
prop_determineShell6 = determineShell (fromJust $ pScript "#! /bin/sh") == Sh
|
||||
prop_determineShell7 = determineShell (fromJust $ pScript "#! /bin/ash") == Dash
|
||||
determineShell t = fromMaybe Bash $ do
|
||||
shellString <- foldl mplus Nothing $ getCandidates t
|
||||
shellForExecutable shellString
|
||||
prop_determineShell0 = determineShellTest "#!/bin/sh" == Sh
|
||||
prop_determineShell1 = determineShellTest "#!/usr/bin/env ksh" == Ksh
|
||||
prop_determineShell2 = determineShellTest "" == Bash
|
||||
prop_determineShell3 = determineShellTest "#!/bin/sh -e" == Sh
|
||||
prop_determineShell4 = determineShellTest "#!/bin/ksh\n#shellcheck shell=sh\nfoo" == Sh
|
||||
prop_determineShell5 = determineShellTest "#shellcheck shell=sh\nfoo" == Sh
|
||||
prop_determineShell6 = determineShellTest "#! /bin/sh" == Sh
|
||||
prop_determineShell7 = determineShellTest "#! /bin/ash" == Dash
|
||||
prop_determineShell8 = determineShellTest' (Just Ksh) "#!/bin/sh" == Sh
|
||||
|
||||
determineShellTest = determineShellTest' Nothing
|
||||
determineShellTest' fallbackShell = determineShell fallbackShell . fromJust . prRoot . pScript
|
||||
determineShell fallbackShell t = fromMaybe Bash $
|
||||
shellForExecutable shellString `mplus` fallbackShell
|
||||
where
|
||||
forAnnotation t =
|
||||
case t of
|
||||
(ShellOverride s) -> return s
|
||||
_ -> fail ""
|
||||
getCandidates :: Token -> [Maybe String]
|
||||
getCandidates t@T_Script {} = [Just $ fromShebang t]
|
||||
getCandidates (T_Annotation _ annotations s) =
|
||||
map forAnnotation annotations ++
|
||||
[Just $ fromShebang s]
|
||||
fromShebang (T_Script _ s t) = executableFromShebang s
|
||||
shellString = getCandidate t
|
||||
getCandidate :: Token -> String
|
||||
getCandidate t@T_Script {} = fromShebang t
|
||||
getCandidate (T_Annotation _ annotations s) =
|
||||
headOrDefault (fromShebang s) [s | ShellOverride s <- annotations]
|
||||
fromShebang (T_Script _ (T_Literal _ s) _) = executableFromShebang s
|
||||
|
||||
-- Given a string like "/bin/bash" or "/usr/bin/env dash",
|
||||
-- return the shell basename like "bash" or "dash"
|
||||
executableFromShebang :: String -> String
|
||||
executableFromShebang = shellFor
|
||||
where
|
||||
shellFor s | "/env " `isInfixOf` s = head (drop 1 (words s)++[""])
|
||||
shellFor s | "/env " `isInfixOf` s = headOrDefault "" (drop 1 $ words s)
|
||||
shellFor s | ' ' `elem` s = shellFor $ takeWhile (/= ' ') s
|
||||
shellFor s = reverse . takeWhile (/= '/') . reverse $ s
|
||||
|
||||
@@ -284,7 +294,7 @@ isQuoteFree = isQuoteFreeNode False
|
||||
|
||||
isQuoteFreeNode strict tree t =
|
||||
(isQuoteFreeElement t == Just True) ||
|
||||
head (mapMaybe isQuoteFreeContext (drop 1 $ getPath tree t) ++ [False])
|
||||
headOrDefault False (mapMaybe isQuoteFreeContext (drop 1 $ getPath tree t))
|
||||
where
|
||||
-- Is this node self-quoting in itself?
|
||||
isQuoteFreeElement t =
|
||||
@@ -375,10 +385,6 @@ isParentOf tree parent child =
|
||||
|
||||
parents params = getPath (parentMap params)
|
||||
|
||||
pathTo t = do
|
||||
parents <- reader parentMap
|
||||
return $ getPath parents t
|
||||
|
||||
-- Find the first match in a list where the predicate is Just True.
|
||||
-- Stops if it's Just False and ignores Nothing.
|
||||
findFirst :: (a -> Maybe Bool) -> [a] -> Maybe a
|
||||
@@ -422,6 +428,7 @@ getVariableFlow params t =
|
||||
|
||||
assignFirst T_ForIn {} = True
|
||||
assignFirst T_SelectIn {} = True
|
||||
assignFirst (T_BatsTest {}) = True
|
||||
assignFirst _ = False
|
||||
|
||||
setRead t =
|
||||
@@ -439,9 +446,10 @@ leadType params t =
|
||||
T_Backticked _ _ -> SubshellScope "`..` expansion"
|
||||
T_Backgrounded _ _ -> SubshellScope "backgrounding &"
|
||||
T_Subshell _ _ -> SubshellScope "(..) group"
|
||||
T_BatsTest {} -> SubshellScope "@bats test"
|
||||
T_CoProcBody _ _ -> SubshellScope "coproc"
|
||||
T_Redirecting {} ->
|
||||
if fromMaybe False causesSubshell
|
||||
if causesSubshell == Just True
|
||||
then SubshellScope "pipeline"
|
||||
else NoneScope
|
||||
_ -> NoneScope
|
||||
@@ -479,6 +487,12 @@ getModifiedVariables t =
|
||||
guard $ op `elem` ["=", "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|="]
|
||||
return (t, t, name, DataString $ SourceFrom [rhs])
|
||||
|
||||
T_BatsTest {} -> [
|
||||
(t, t, "lines", DataArray SourceExternal),
|
||||
(t, t, "status", DataString SourceInteger),
|
||||
(t, t, "output", DataString SourceExternal)
|
||||
]
|
||||
|
||||
-- Count [[ -v foo ]] as an "assignment".
|
||||
-- This is to prevent [ -v foo ] being unassigned or unused.
|
||||
TC_Unary id _ "-v" token -> maybeToList $ do
|
||||
@@ -491,10 +505,10 @@ getModifiedVariables t =
|
||||
guard . not . null $ str
|
||||
return (t, token, str, DataString SourceChecked)
|
||||
|
||||
T_DollarBraced _ l -> maybeToList $ do
|
||||
T_DollarBraced _ _ l -> maybeToList $ do
|
||||
let string = bracedString t
|
||||
let modifier = getBracedModifier string
|
||||
guard $ ":=" `isPrefixOf` modifier
|
||||
guard $ any (`isPrefixOf` modifier) ["=", ":="]
|
||||
return (t, t, getBracedReference string, DataString $ SourceFrom [l])
|
||||
|
||||
t@(T_FdRedirect _ ('{':var) op) -> -- {foo}>&2 modifies foo
|
||||
@@ -527,14 +541,11 @@ getReferencedVariableCommand base@(T_SimpleCommand _ _ (T_NormalWord _ (T_Litera
|
||||
(not $ any (`elem` flags) ["f", "F"])
|
||||
then concatMap getReference rest
|
||||
else []
|
||||
"readonly" ->
|
||||
if any (`elem` flags) ["f", "p"]
|
||||
then []
|
||||
else concatMap getReference rest
|
||||
"trap" ->
|
||||
case rest of
|
||||
head:_ -> map (\x -> (head, head, x)) $ getVariablesFromLiteralToken head
|
||||
head:_ -> map (\x -> (base, head, x)) $ getVariablesFromLiteralToken head
|
||||
_ -> []
|
||||
"alias" -> [(base, token, name) | token <- rest, name <- getVariablesFromLiteralToken token]
|
||||
_ -> []
|
||||
where
|
||||
getReference t@(T_Assignment _ _ name _ value) = [(t, t, name)]
|
||||
@@ -552,9 +563,11 @@ getReferencedVariableCommand _ = []
|
||||
-- VariableName :: String, -- The variable name, i.e. foo
|
||||
-- VariableValue :: DataType -- A description of the value being assigned, i.e. "Literal string with value foo"
|
||||
-- )
|
||||
getModifiedVariableCommand base@(T_SimpleCommand _ _ (T_NormalWord _ (T_Literal _ x:_):rest)) =
|
||||
getModifiedVariableCommand base@(T_SimpleCommand id cmdPrefix (T_NormalWord _ (T_Literal _ x:_):rest)) =
|
||||
filter (\(_,_,s,_) -> not ("-" `isPrefixOf` s)) $
|
||||
case x of
|
||||
"builtin" ->
|
||||
getModifiedVariableCommand $ T_SimpleCommand id cmdPrefix rest
|
||||
"read" ->
|
||||
let params = map getLiteral rest
|
||||
readArrayVars = getReadArrayVariables rest
|
||||
@@ -587,11 +600,15 @@ getModifiedVariableCommand base@(T_SimpleCommand _ _ (T_NormalWord _ (T_Literal
|
||||
"mapfile" -> maybeToList $ getMapfileArray base rest
|
||||
"readarray" -> maybeToList $ getMapfileArray base rest
|
||||
|
||||
"DEFINE_boolean" -> maybeToList $ getFlagVariable rest
|
||||
"DEFINE_float" -> maybeToList $ getFlagVariable rest
|
||||
"DEFINE_integer" -> maybeToList $ getFlagVariable rest
|
||||
"DEFINE_string" -> maybeToList $ getFlagVariable rest
|
||||
|
||||
_ -> []
|
||||
where
|
||||
flags = map snd $ getAllFlags base
|
||||
stripEquals s = let rest = dropWhile (/= '=') s in
|
||||
if rest == "" then "" else tail rest
|
||||
stripEquals s = drop 1 $ dropWhile (/= '=') s
|
||||
stripEqualsFrom (T_NormalWord id1 (T_Literal id2 s:rs)) =
|
||||
T_NormalWord id1 (T_Literal id2 (stripEquals s):rs)
|
||||
stripEqualsFrom (T_NormalWord id1 [T_DoubleQuoted id2 [T_Literal id3 s]]) =
|
||||
@@ -622,7 +639,7 @@ getModifiedVariableCommand base@(T_SimpleCommand _ _ (T_NormalWord _ (T_Literal
|
||||
getModifierParam _ _ = []
|
||||
|
||||
letParamToLiteral token =
|
||||
if var == ""
|
||||
if null var
|
||||
then []
|
||||
else [(base, token, var, DataString $ SourceFrom [stripEqualsFrom token])]
|
||||
where var = takeWhile isVariableChar $ dropWhile (`elem` "+-") $ concat $ oversimplify token
|
||||
@@ -638,7 +655,11 @@ getModifiedVariableCommand base@(T_SimpleCommand _ _ (T_NormalWord _ (T_Literal
|
||||
|
||||
getPrintfVariable list = f $ map (\x -> (x, getLiteralString x)) list
|
||||
where
|
||||
f ((_, Just "-v") : (t, Just var) : _) = return (base, t, var, DataString $ SourceFrom list)
|
||||
f ((_, Just "-v") : (t, Just var) : _) = return (base, t, varName, varType $ SourceFrom list)
|
||||
where
|
||||
(varName, varType) = case elemIndex '[' var of
|
||||
Just i -> (take i var, DataArray)
|
||||
Nothing -> (var, DataString)
|
||||
f (_:rest) = f rest
|
||||
f [] = fail "not found"
|
||||
|
||||
@@ -652,9 +673,22 @@ getModifiedVariableCommand base@(T_SimpleCommand _ _ (T_NormalWord _ (T_Literal
|
||||
return (base, lastArg, name, DataArray SourceExternal)
|
||||
|
||||
-- get all the array variables used in read, e.g. read -a arr
|
||||
getReadArrayVariables args = do
|
||||
getReadArrayVariables args =
|
||||
map (getLiteralArray . snd)
|
||||
(filter (\(x,_) -> getLiteralString x == Just "-a") (zip (args) (tail args)))
|
||||
(filter (isArrayFlag . fst) (zip args (tail args)))
|
||||
|
||||
isArrayFlag x = fromMaybe False $ do
|
||||
str <- getLiteralString x
|
||||
return $ case str of
|
||||
'-':'-':_ -> False
|
||||
'-':str -> 'a' `elem` str
|
||||
_ -> False
|
||||
|
||||
-- get the FLAGS_ variable created by a shflags DEFINE_ call
|
||||
getFlagVariable (n:v:_) = do
|
||||
name <- getLiteralString n
|
||||
return (base, n, "FLAGS_" ++ name, DataString $ SourceExternal)
|
||||
getFlagVariable _ = Nothing
|
||||
|
||||
getModifiedVariableCommand _ = []
|
||||
|
||||
@@ -679,7 +713,7 @@ getOffsetReferences mods = fromMaybe [] $ do
|
||||
|
||||
getReferencedVariables parents t =
|
||||
case t of
|
||||
T_DollarBraced id l -> let str = bracedString t in
|
||||
T_DollarBraced id _ l -> let str = bracedString t in
|
||||
(t, t, getBracedReference str) :
|
||||
map (\x -> (l, l, x)) (
|
||||
getIndexReferences str
|
||||
@@ -698,6 +732,12 @@ getReferencedVariables parents t =
|
||||
then concatMap (getIfReference t) [lhs, rhs]
|
||||
else []
|
||||
|
||||
T_BatsTest {} -> [ -- pretend @test references vars to avoid warnings
|
||||
(t, t, "lines"),
|
||||
(t, t, "status"),
|
||||
(t, t, "output")
|
||||
]
|
||||
|
||||
t@(T_FdRedirect _ ('{':var) op) -> -- {foo}>&- references and closes foo
|
||||
[(t, t, takeWhile (/= '}') var) | isClosingFileOp op]
|
||||
x -> getReferencedVariableCommand x
|
||||
@@ -718,9 +758,8 @@ getReferencedVariables parents t =
|
||||
_ -> Nothing
|
||||
|
||||
getIfReference context token = maybeToList $ do
|
||||
str <- getLiteralStringExt literalizer token
|
||||
guard . not $ null str
|
||||
when (isDigit $ head str) $ fail "is a number"
|
||||
str@(h:_) <- getLiteralStringExt literalizer token
|
||||
when (isDigit h) $ fail "is a number"
|
||||
return (context, token, getBracedReference str)
|
||||
|
||||
isDereferencing = (`elem` ["-eq", "-ne", "-lt", "-le", "-gt", "-ge"])
|
||||
@@ -740,15 +779,15 @@ isCommand token str = isCommandMatch token (\cmd -> cmd == str || ('/' : str) `
|
||||
-- Compare a command to a literal. Like above, but checks full path.
|
||||
isUnqualifiedCommand token str = isCommandMatch token (== str)
|
||||
|
||||
isCommandMatch token matcher = fromMaybe False $
|
||||
fmap matcher (getCommandName token)
|
||||
isCommandMatch token matcher = maybe False
|
||||
matcher (getCommandName token)
|
||||
|
||||
-- Does this regex look like it was intended as a glob?
|
||||
-- True: *foo*
|
||||
-- False: .*foo.*
|
||||
isConfusedGlobRegex :: String -> Bool
|
||||
isConfusedGlobRegex ('*':_) = True
|
||||
isConfusedGlobRegex [x,'*'] | x /= '\\' = True
|
||||
isConfusedGlobRegex [x,'*'] | x `notElem` "\\." = True
|
||||
isConfusedGlobRegex _ = False
|
||||
|
||||
isVariableStartChar x = x == '_' || isAsciiLower x || isAsciiUpper x
|
||||
@@ -762,7 +801,7 @@ isVariableName (x:r) = isVariableStartChar x && all isVariableChar r
|
||||
isVariableName _ = False
|
||||
|
||||
getVariablesFromLiteralToken token =
|
||||
getVariablesFromLiteral (fromJust $ getLiteralStringExt (const $ return " ") token)
|
||||
getVariablesFromLiteral (getLiteralStringDef " " token)
|
||||
|
||||
-- Try to get referenced variables from a literal string like "$foo"
|
||||
-- Ignores tons of cases like arithmetic evaluation and array indices.
|
||||
@@ -812,7 +851,7 @@ getBracedReference s = fromMaybe s $
|
||||
prop_getBracedModifier1 = getBracedModifier "foo:bar:baz" == ":bar:baz"
|
||||
prop_getBracedModifier2 = getBracedModifier "!var:-foo" == ":-foo"
|
||||
prop_getBracedModifier3 = getBracedModifier "foo[bar]" == "[bar]"
|
||||
getBracedModifier s = fromMaybe "" . listToMaybe $ do
|
||||
getBracedModifier s = headOrDefault "" $ do
|
||||
let var = getBracedReference s
|
||||
a <- dropModifier s
|
||||
dropPrefix var a
|
||||
@@ -826,15 +865,6 @@ getBracedModifier s = fromMaybe "" . listToMaybe $ do
|
||||
|
||||
-- Useful generic functions.
|
||||
|
||||
-- Run an action in a Maybe (or do nothing).
|
||||
-- Example:
|
||||
-- potentially $ do
|
||||
-- s <- getLiteralString cmd
|
||||
-- guard $ s `elem` ["--recursive", "-r"]
|
||||
-- return $ warn .. "Something something recursive"
|
||||
potentially :: Monad m => Maybe (m ()) -> m ()
|
||||
potentially = fromMaybe (return ())
|
||||
|
||||
-- Get element 0 or a default. Like `head` but safe.
|
||||
headOrDefault _ (a:_) = a
|
||||
headOrDefault def _ = def
|
||||
@@ -858,18 +888,17 @@ filterByAnnotation asSpec params =
|
||||
shouldIgnore note =
|
||||
any (shouldIgnoreFor (getCode note)) $
|
||||
getPath parents (T_Bang $ tcId note)
|
||||
shouldIgnoreFor num (T_Annotation _ anns _) =
|
||||
any hasNum anns
|
||||
where
|
||||
hasNum (DisableComment ts) = num == ts
|
||||
hasNum _ = False
|
||||
shouldIgnoreFor _ T_Include {} = not $ asCheckSourced asSpec
|
||||
shouldIgnoreFor _ _ = False
|
||||
shouldIgnoreFor code t = isAnnotationIgnoringCode code t
|
||||
parents = parentMap params
|
||||
getCode = cCode . tcComment
|
||||
|
||||
shouldIgnoreCode params code t =
|
||||
any (isAnnotationIgnoringCode code) $
|
||||
getPath (parentMap params) t
|
||||
|
||||
-- Is this a ${#anything}, to get string length or array count?
|
||||
isCountingReference (T_DollarBraced id token) =
|
||||
isCountingReference (T_DollarBraced id _ token) =
|
||||
case concat $ oversimplify token of
|
||||
'#':_ -> True
|
||||
_ -> False
|
||||
@@ -878,7 +907,7 @@ isCountingReference _ = False
|
||||
-- FIXME: doesn't handle ${a:+$var} vs ${a:+"$var"}
|
||||
isQuotedAlternativeReference t =
|
||||
case t of
|
||||
T_DollarBraced _ _ ->
|
||||
T_DollarBraced _ _ _ ->
|
||||
getBracedModifier (bracedString t) `matches` re
|
||||
_ -> False
|
||||
where
|
||||
@@ -890,12 +919,11 @@ isQuotedAlternativeReference t =
|
||||
-- Just [("r", -re), ("e", -re), ("d", :), ("u", 3), ("", bar)]
|
||||
-- where flags with arguments map to arguments, while others map to themselves.
|
||||
-- Any unrecognized flag will result in Nothing.
|
||||
getGnuOpts = getOpts getAllFlags
|
||||
getBsdOpts = getOpts getLeadingFlags
|
||||
getOpts :: (Token -> [(Token, String)]) -> String -> Token -> Maybe [(String, Token)]
|
||||
getOpts flagTokenizer string cmd = process flags
|
||||
getGnuOpts str t = getOpts str $ getAllFlags t
|
||||
getBsdOpts str t = getOpts str $ getLeadingFlags t
|
||||
getOpts :: String -> [(Token, String)] -> Maybe [(String, Token)]
|
||||
getOpts string flags = process flags
|
||||
where
|
||||
flags = flagTokenizer cmd
|
||||
flagList (c:':':rest) = ([c], True) : flagList rest
|
||||
flagList (c:rest) = ([c], False) : flagList rest
|
||||
flagList [] = []
|
||||
@@ -910,12 +938,23 @@ getOpts flagTokenizer string cmd = process flags
|
||||
takesArg <- Map.lookup flag1 flagMap
|
||||
if takesArg
|
||||
then do
|
||||
guard $ flag2 == ""
|
||||
guard $ null flag2
|
||||
more <- process rest
|
||||
return $ (flag1, token2) : more
|
||||
else do
|
||||
more <- process rest2
|
||||
return $ (flag1, token1) : more
|
||||
|
||||
supportsArrays shell = shell == Bash || shell == Ksh
|
||||
|
||||
-- Returns true if the shell is Bash or Ksh (sorry for the name, Ksh)
|
||||
isBashLike :: Parameters -> Bool
|
||||
isBashLike params =
|
||||
case shellType params of
|
||||
Bash -> True
|
||||
Ksh -> True
|
||||
Dash -> False
|
||||
Sh -> False
|
||||
|
||||
return []
|
||||
runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])
|
||||
|
@@ -1,5 +1,5 @@
|
||||
{-
|
||||
Copyright 2012-2015 Vidar Holen
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
@@ -48,6 +48,17 @@ tokenToPosition startMap t = fromMaybe fail $ do
|
||||
where
|
||||
fail = error "Internal shellcheck error: id doesn't exist. Please report!"
|
||||
|
||||
shellFromFilename filename = listToMaybe candidates
|
||||
where
|
||||
shellExtensions = [(".ksh", Ksh)
|
||||
,(".bash", Bash)
|
||||
,(".bats", Bash)
|
||||
,(".dash", Dash)]
|
||||
-- The `.sh` is too generic to determine the shell:
|
||||
-- We fallback to Bash in this case and emit SC2148 if there is no shebang
|
||||
candidates =
|
||||
[sh | (ext,sh) <- shellExtensions, ext `isSuffixOf` filename]
|
||||
|
||||
checkScript :: Monad m => SystemInterface m -> CheckSpec -> m CheckResult
|
||||
checkScript sys spec = do
|
||||
results <- checkScript (csScript spec)
|
||||
@@ -61,6 +72,7 @@ checkScript sys spec = do
|
||||
psFilename = csFilename spec,
|
||||
psScript = contents,
|
||||
psCheckSourced = csCheckSourced spec,
|
||||
psIgnoreRC = csIgnoreRC spec,
|
||||
psShellTypeOverride = csShellTypeOverride spec
|
||||
}
|
||||
let parseMessages = prComments result
|
||||
@@ -69,26 +81,30 @@ checkScript sys spec = do
|
||||
as {
|
||||
asScript = root,
|
||||
asShellType = csShellTypeOverride spec,
|
||||
asFallbackShell = shellFromFilename $ csFilename spec,
|
||||
asCheckSourced = csCheckSourced spec,
|
||||
asExecutionMode = Executed,
|
||||
asTokenPositions = tokenPositions
|
||||
asTokenPositions = tokenPositions,
|
||||
asOptionalChecks = csOptionalChecks spec
|
||||
} where as = newAnalysisSpec root
|
||||
let analysisMessages =
|
||||
fromMaybe [] $
|
||||
maybe []
|
||||
(arComments . analyzeScript . analysisSpec)
|
||||
<$> prRoot result
|
||||
$ prRoot result
|
||||
let translator = tokenToPosition tokenPositions
|
||||
return . nub . sortMessages . filter shouldInclude $
|
||||
(parseMessages ++ map translator analysisMessages)
|
||||
|
||||
shouldInclude pc =
|
||||
let code = cCode (pcComment pc)
|
||||
severity <= csMinSeverity spec &&
|
||||
case csIncludedWarnings spec of
|
||||
Nothing -> code `notElem` csExcludedWarnings spec
|
||||
Just includedWarnings -> code `elem` includedWarnings
|
||||
where
|
||||
code = cCode (pcComment pc)
|
||||
severity = cSeverity (pcComment pc)
|
||||
in
|
||||
code `notElem` csExcludedWarnings spec &&
|
||||
severity <= csMinSeverity spec
|
||||
|
||||
sortMessages = sortBy (comparing order)
|
||||
sortMessages = sortOn order
|
||||
order pc =
|
||||
let pos = pcStartPos pc
|
||||
comment = pcComment pc in
|
||||
@@ -125,6 +141,21 @@ checkRecursive includes src =
|
||||
csCheckSourced = True
|
||||
}
|
||||
|
||||
checkOptionIncludes includes src =
|
||||
checkWithSpec [] emptyCheckSpec {
|
||||
csScript = src,
|
||||
csIncludedWarnings = includes,
|
||||
csCheckSourced = True
|
||||
}
|
||||
|
||||
checkWithRc rc = getErrors
|
||||
(mockRcFile rc $ mockedSystemInterface [])
|
||||
|
||||
checkWithIncludesAndSourcePath includes mapper = getErrors
|
||||
(mockedSystemInterface includes) {
|
||||
siFindSource = mapper
|
||||
}
|
||||
|
||||
prop_findsParseIssue = check "echo \"$12\"" == [1037]
|
||||
|
||||
prop_commentDisablesParseIssue1 =
|
||||
@@ -167,11 +198,11 @@ prop_optionDisablesBadShebang =
|
||||
}
|
||||
|
||||
prop_annotationDisablesBadShebang =
|
||||
[] == check "#!/usr/bin/python\n# shellcheck shell=sh\ntrue\n"
|
||||
null $ check "#!/usr/bin/python\n# shellcheck shell=sh\ntrue\n"
|
||||
|
||||
|
||||
prop_canParseDevNull =
|
||||
[] == check "source /dev/null"
|
||||
null $ check "source /dev/null"
|
||||
|
||||
prop_failsWhenNotSourcing =
|
||||
[1091, 2154] == check "source lol; echo \"$bar\""
|
||||
@@ -179,11 +210,15 @@ prop_failsWhenNotSourcing =
|
||||
prop_worksWhenSourcing =
|
||||
null $ checkWithIncludes [("lib", "bar=1")] "source lib; echo \"$bar\""
|
||||
|
||||
prop_worksWhenSourcingWithDashDash =
|
||||
null $ checkWithIncludes [("lib", "bar=1")] "source -- lib; echo \"$bar\""
|
||||
|
||||
prop_worksWhenDotting =
|
||||
null $ checkWithIncludes [("lib", "bar=1")] ". lib; echo \"$bar\""
|
||||
|
||||
-- FIXME: This should really be giving [1093], "recursively sourced"
|
||||
prop_noInfiniteSourcing =
|
||||
[] == checkWithIncludes [("lib", "source lib")] "source lib"
|
||||
null $ checkWithIncludes [("lib", "source lib")] "source lib"
|
||||
|
||||
prop_canSourceBadSyntax =
|
||||
[1094, 2086] == checkWithIncludes [("lib", "for f; do")] "source lib; echo $1"
|
||||
@@ -203,6 +238,12 @@ prop_recursiveAnalysis =
|
||||
prop_recursiveParsing =
|
||||
[1037] == checkRecursive [("lib", "echo \"$10\"")] "source lib"
|
||||
|
||||
prop_nonRecursiveAnalysis =
|
||||
null $ checkWithIncludes [("lib", "echo $1")] "source lib"
|
||||
|
||||
prop_nonRecursiveParsing =
|
||||
null $ checkWithIncludes [("lib", "echo \"$10\"")] "source lib"
|
||||
|
||||
prop_sourceDirectiveDoesntFollowFile =
|
||||
null $ checkWithIncludes
|
||||
[("foo", "source bar"), ("bar", "baz=3")]
|
||||
@@ -231,5 +272,130 @@ prop_filewideAnnotation8 = null $
|
||||
prop_sourcePartOfOriginalScript = -- #1181: -x disabled posix warning for 'source'
|
||||
2039 `elem` checkWithIncludes [("./saywhat.sh", "echo foo")] "#!/bin/sh\nsource ./saywhat.sh"
|
||||
|
||||
prop_spinBug1413 = null $ check "fun() {\n# shellcheck disable=SC2188\n> /dev/null\n}\n"
|
||||
|
||||
prop_deducesTypeFromExtension = null result
|
||||
where
|
||||
result = checkWithSpec [] emptyCheckSpec {
|
||||
csFilename = "file.ksh",
|
||||
csScript = "(( 3.14 ))"
|
||||
}
|
||||
|
||||
prop_deducesTypeFromExtension2 = result == [2079]
|
||||
where
|
||||
result = checkWithSpec [] emptyCheckSpec {
|
||||
csFilename = "file.bash",
|
||||
csScript = "(( 3.14 ))"
|
||||
}
|
||||
|
||||
prop_canDisableShebangWarning = null $ result
|
||||
where
|
||||
result = checkWithSpec [] emptyCheckSpec {
|
||||
csFilename = "file.sh",
|
||||
csScript = "#shellcheck disable=SC2148\nfoo"
|
||||
}
|
||||
|
||||
prop_shExtensionDoesntMatter = result == [2148]
|
||||
where
|
||||
result = checkWithSpec [] emptyCheckSpec {
|
||||
csFilename = "file.sh",
|
||||
csScript = "echo 'hello world'"
|
||||
}
|
||||
|
||||
prop_sourcedFileUsesOriginalShellExtension = result == [2079]
|
||||
where
|
||||
result = checkWithSpec [("file.ksh", "(( 3.14 ))")] emptyCheckSpec {
|
||||
csFilename = "file.bash",
|
||||
csScript = "source file.ksh",
|
||||
csCheckSourced = True
|
||||
}
|
||||
|
||||
prop_canEnableOptionalsWithSpec = result == [2244]
|
||||
where
|
||||
result = checkWithSpec [] emptyCheckSpec {
|
||||
csFilename = "file.sh",
|
||||
csScript = "#!/bin/sh\n[ \"$1\" ]",
|
||||
csOptionalChecks = ["avoid-nullary-conditions"]
|
||||
}
|
||||
|
||||
prop_optionIncludes1 =
|
||||
-- expect 2086, but not included, so nothing reported
|
||||
null $ checkOptionIncludes (Just [2080]) "#!/bin/sh\n var='a b'\n echo $var"
|
||||
|
||||
prop_optionIncludes2 =
|
||||
-- expect 2086, included, so it is reported
|
||||
[2086] == checkOptionIncludes (Just [2086]) "#!/bin/sh\n var='a b'\n echo $var"
|
||||
|
||||
prop_optionIncludes3 =
|
||||
-- expect 2086, no inclusions provided, so it is reported
|
||||
[2086] == checkOptionIncludes Nothing "#!/bin/sh\n var='a b'\n echo $var"
|
||||
|
||||
prop_optionIncludes4 =
|
||||
-- expect 2086 & 2154, only 2154 included, so only that's reported
|
||||
[2154] == checkOptionIncludes (Just [2154]) "#!/bin/sh\n var='a b'\n echo $var\n echo $bar"
|
||||
|
||||
|
||||
prop_readsRcFile = null result
|
||||
where
|
||||
result = checkWithRc "disable=2086" emptyCheckSpec {
|
||||
csScript = "#!/bin/sh\necho $1",
|
||||
csIgnoreRC = False
|
||||
}
|
||||
|
||||
prop_canUseNoRC = result == [2086]
|
||||
where
|
||||
result = checkWithRc "disable=2086" emptyCheckSpec {
|
||||
csScript = "#!/bin/sh\necho $1",
|
||||
csIgnoreRC = True
|
||||
}
|
||||
|
||||
prop_NoRCWontLookAtFile = result == [2086]
|
||||
where
|
||||
result = checkWithRc (error "Fail") emptyCheckSpec {
|
||||
csScript = "#!/bin/sh\necho $1",
|
||||
csIgnoreRC = True
|
||||
}
|
||||
|
||||
prop_brokenRcGetsWarning = result == [1134, 2086]
|
||||
where
|
||||
result = checkWithRc "rofl" emptyCheckSpec {
|
||||
csScript = "#!/bin/sh\necho $1",
|
||||
csIgnoreRC = False
|
||||
}
|
||||
|
||||
prop_canEnableOptionalsWithRc = result == [2244]
|
||||
where
|
||||
result = checkWithRc "enable=avoid-nullary-conditions" emptyCheckSpec {
|
||||
csScript = "#!/bin/sh\n[ \"$1\" ]"
|
||||
}
|
||||
|
||||
prop_sourcePathRedirectsName = result == [2086]
|
||||
where
|
||||
f "dir/myscript" _ "lib" = return "foo/lib"
|
||||
result = checkWithIncludesAndSourcePath [("foo/lib", "echo $1")] f emptyCheckSpec {
|
||||
csScript = "#!/bin/bash\nsource lib",
|
||||
csFilename = "dir/myscript",
|
||||
csCheckSourced = True
|
||||
}
|
||||
|
||||
prop_sourcePathAddsAnnotation = result == [2086]
|
||||
where
|
||||
f "dir/myscript" ["mypath"] "lib" = return "foo/lib"
|
||||
result = checkWithIncludesAndSourcePath [("foo/lib", "echo $1")] f emptyCheckSpec {
|
||||
csScript = "#!/bin/bash\n# shellcheck source-path=mypath\nsource lib",
|
||||
csFilename = "dir/myscript",
|
||||
csCheckSourced = True
|
||||
}
|
||||
|
||||
prop_sourcePathRedirectsDirective = result == [2086]
|
||||
where
|
||||
f "dir/myscript" _ "lib" = return "foo/lib"
|
||||
f _ _ _ = return "/dev/null"
|
||||
result = checkWithIncludesAndSourcePath [("foo/lib", "echo $1")] f emptyCheckSpec {
|
||||
csScript = "#!/bin/bash\n# shellcheck source=lib\nsource kittens",
|
||||
csFilename = "dir/myscript",
|
||||
csCheckSourced = True
|
||||
}
|
||||
|
||||
return []
|
||||
runTests = $quickCheckAll
|
||||
|
@@ -1,5 +1,5 @@
|
||||
{-
|
||||
Copyright 2012-2015 Vidar Holen
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
@@ -21,7 +21,7 @@
|
||||
{-# LANGUAGE FlexibleContexts #-}
|
||||
|
||||
-- This module contains checks that examine specific commands by name.
|
||||
module ShellCheck.Checks.Commands (checker , ShellCheck.Checks.Commands.runTests) where
|
||||
module ShellCheck.Checks.Commands (checker, optionalChecks, ShellCheck.Checks.Commands.runTests) where
|
||||
|
||||
import ShellCheck.AST
|
||||
import ShellCheck.ASTLib
|
||||
@@ -34,6 +34,7 @@ import ShellCheck.Regex
|
||||
import Control.Monad
|
||||
import Control.Monad.RWS
|
||||
import Data.Char
|
||||
import Data.Functor.Identity
|
||||
import Data.List
|
||||
import Data.Maybe
|
||||
import qualified Data.Map.Strict as Map
|
||||
@@ -61,6 +62,7 @@ commandChecks = [
|
||||
,checkGrepRe
|
||||
,checkTrapQuotes
|
||||
,checkReturn
|
||||
,checkExit
|
||||
,checkFindExecWithSingleArgument
|
||||
,checkUnusedEchoEscapes
|
||||
,checkInjectableFindSh
|
||||
@@ -89,11 +91,30 @@ commandChecks = [
|
||||
,checkMvArguments, checkCpArguments, checkLnArguments
|
||||
,checkFindRedirections
|
||||
,checkReadExpansions
|
||||
,checkWhich
|
||||
,checkSudoRedirect
|
||||
,checkSudoArgs
|
||||
,checkSourceArgs
|
||||
,checkChmodDashr
|
||||
]
|
||||
|
||||
optionalChecks = map fst optionalCommandChecks
|
||||
optionalCommandChecks :: [(CheckDescription, CommandCheck)]
|
||||
optionalCommandChecks = [
|
||||
(newCheckDescription {
|
||||
cdName = "deprecate-which",
|
||||
cdDescription = "Suggest 'command -v' instead of 'which'",
|
||||
cdPositive = "which javac",
|
||||
cdNegative = "command -v javac"
|
||||
}, checkWhich)
|
||||
]
|
||||
optionalCheckMap = Map.fromList $ map (\(desc, check) -> (cdName desc, check)) optionalCommandChecks
|
||||
|
||||
prop_verifyOptionalExamples = all check optionalCommandChecks
|
||||
where
|
||||
check (desc, check) =
|
||||
verify check (cdPositive desc)
|
||||
&& verifyNot check (cdNegative desc)
|
||||
|
||||
buildCommandMap :: [CommandCheck] -> Map.Map CommandName (Token -> Analysis)
|
||||
buildCommandMap = foldl' addCheck Map.empty
|
||||
where
|
||||
@@ -102,12 +123,16 @@ buildCommandMap = foldl' addCheck Map.empty
|
||||
|
||||
|
||||
checkCommand :: Map.Map CommandName (Token -> Analysis) -> Token -> Analysis
|
||||
checkCommand map t@(T_SimpleCommand id _ (cmd:rest)) = fromMaybe (return ()) $ do
|
||||
checkCommand map t@(T_SimpleCommand id cmdPrefix (cmd:rest)) = sequence_ $ do
|
||||
name <- getLiteralString cmd
|
||||
return $
|
||||
if '/' `elem` name
|
||||
then
|
||||
Map.findWithDefault nullCheck (Basename $ basename name) map t
|
||||
else if name == "builtin" && not (null rest) then
|
||||
let t' = T_SimpleCommand id cmdPrefix rest
|
||||
selectedBuiltin = fromMaybe "" $ getLiteralString . head $ rest
|
||||
in Map.findWithDefault nullCheck (Exactly selectedBuiltin) map t'
|
||||
else do
|
||||
Map.findWithDefault nullCheck (Exactly name) map t
|
||||
Map.findWithDefault nullCheck (Basename name) map t
|
||||
@@ -125,8 +150,14 @@ getChecker list = Checker {
|
||||
map = buildCommandMap list
|
||||
|
||||
|
||||
checker :: Parameters -> Checker
|
||||
checker params = getChecker commandChecks
|
||||
checker :: AnalysisSpec -> Parameters -> Checker
|
||||
checker spec params = getChecker $ commandChecks ++ optionals
|
||||
where
|
||||
keys = asOptionalChecks spec
|
||||
optionals =
|
||||
if "all" `elem` keys
|
||||
then map snd optionalCommandChecks
|
||||
else mapMaybe (\x -> Map.lookup x optionalCheckMap) keys
|
||||
|
||||
prop_checkTr1 = verify checkTr "tr [a-f] [A-F]"
|
||||
prop_checkTr2 = verify checkTr "tr 'a-z' 'A-Z'"
|
||||
@@ -166,15 +197,14 @@ prop_checkFindNameGlob1 = verify checkFindNameGlob "find / -name *.php"
|
||||
prop_checkFindNameGlob2 = verify checkFindNameGlob "find / -type f -ipath *(foo)"
|
||||
prop_checkFindNameGlob3 = verifyNot checkFindNameGlob "find * -name '*.php'"
|
||||
checkFindNameGlob = CommandCheck (Basename "find") (f . arguments) where
|
||||
acceptsGlob (Just s) = s `elem` [ "-ilname", "-iname", "-ipath", "-iregex", "-iwholename", "-lname", "-name", "-path", "-regex", "-wholename" ]
|
||||
acceptsGlob _ = False
|
||||
acceptsGlob s = s `elem` [ "-ilname", "-iname", "-ipath", "-iregex", "-iwholename", "-lname", "-name", "-path", "-regex", "-wholename" ]
|
||||
f [] = return ()
|
||||
f [x] = return ()
|
||||
f (a:b:r) = do
|
||||
when (acceptsGlob (getLiteralString a) && isGlob b) $ do
|
||||
let (Just s) = getLiteralString a
|
||||
f (x:xs) = g x xs
|
||||
g _ [] = return ()
|
||||
g a (b:r) = do
|
||||
forM_ (getLiteralString a) $ \s -> when (acceptsGlob s && isGlob b) $
|
||||
warn (getId b) 2061 $ "Quote the parameter to " ++ s ++ " so the shell won't interpret it."
|
||||
f (b:r)
|
||||
g b r
|
||||
|
||||
|
||||
prop_checkNeedlessExpr = verify checkNeedlessExpr "foo=$(expr 3 + 2)"
|
||||
@@ -206,17 +236,24 @@ prop_checkGrepRe12= verifyNot checkGrepRe "grep -F 'Foo*' file"
|
||||
prop_checkGrepRe13= verifyNot checkGrepRe "grep -- -foo bar*"
|
||||
prop_checkGrepRe14= verifyNot checkGrepRe "grep -e -foo bar*"
|
||||
prop_checkGrepRe15= verifyNot checkGrepRe "grep --regex -foo bar*"
|
||||
prop_checkGrepRe16= verifyNot checkGrepRe "grep --include 'Foo*' file"
|
||||
prop_checkGrepRe17= verifyNot checkGrepRe "grep --exclude 'Foo*' file"
|
||||
prop_checkGrepRe18= verifyNot checkGrepRe "grep --exclude-dir 'Foo*' file"
|
||||
prop_checkGrepRe19= verify checkGrepRe "grep -- 'Foo*' file"
|
||||
prop_checkGrepRe20= verifyNot checkGrepRe "grep --fixed-strings 'Foo*' file"
|
||||
prop_checkGrepRe21= verifyNot checkGrepRe "grep -o 'x*' file"
|
||||
prop_checkGrepRe22= verifyNot checkGrepRe "grep --only-matching 'x*' file"
|
||||
prop_checkGrepRe23= verifyNot checkGrepRe "grep '.*' file"
|
||||
|
||||
checkGrepRe = CommandCheck (Basename "grep") check where
|
||||
check cmd = f cmd (arguments cmd)
|
||||
-- --regex=*(extglob) doesn't work. Fixme?
|
||||
skippable (Just s) = not ("--regex=" `isPrefixOf` s) && "-" `isPrefixOf` s
|
||||
skippable _ = False
|
||||
skippable s = not ("--regex=" `isPrefixOf` s) && "-" `isPrefixOf` s
|
||||
f _ [] = return ()
|
||||
f cmd (x:r) =
|
||||
let str = getLiteralStringExt (const $ return "_") x
|
||||
let str = getLiteralStringDef "_" x
|
||||
in
|
||||
if str `elem` [Just "--", Just "-e", Just "--regex"]
|
||||
if str `elem` ["--", "-e", "--regex"]
|
||||
then checkRE cmd r -- Regex is *after* this
|
||||
else
|
||||
if skippable str
|
||||
@@ -228,20 +265,23 @@ checkGrepRe = CommandCheck (Basename "grep") check where
|
||||
when (isGlob re) $
|
||||
warn (getId re) 2062 "Quote the grep pattern so the shell won't interpret it."
|
||||
|
||||
unless (cmd `hasFlag` "F") $ do
|
||||
unless (any (`elem` flags) grepGlobFlags) $ do
|
||||
let string = concat $ oversimplify re
|
||||
if isConfusedGlobRegex string then
|
||||
warn (getId re) 2063 "Grep uses regex, but this looks like a glob."
|
||||
else potentially $ do
|
||||
else sequence_ $ do
|
||||
char <- getSuspiciousRegexWildcard string
|
||||
return $ info (getId re) 2022 $
|
||||
"Note that unlike globs, " ++ [char] ++ "* here matches '" ++ [char, char, char] ++ "' but not '" ++ wordStartingWith char ++ "'."
|
||||
where
|
||||
flags = map snd $ getAllFlags cmd
|
||||
grepGlobFlags = ["fixed-strings", "F", "include", "exclude", "exclude-dir", "o", "only-matching"]
|
||||
|
||||
wordStartingWith c =
|
||||
head . filter ([c] `isPrefixOf`) $ candidates
|
||||
headOrDefault (c:"test") . filter ([c] `isPrefixOf`) $ candidates
|
||||
where
|
||||
candidates =
|
||||
sampleWords ++ map (\(x:r) -> toUpper x : r) sampleWords ++ [c:"test"]
|
||||
sampleWords ++ map (\(x:r) -> toUpper x : r) sampleWords
|
||||
|
||||
getSuspiciousRegexWildcard str =
|
||||
if not $ str `matches` contra
|
||||
@@ -268,7 +308,7 @@ checkTrapQuotes = CommandCheck (Exactly "trap") (f . arguments) where
|
||||
warning id = warn id 2064 "Use single quotes, otherwise this expands now rather than when signalled."
|
||||
checkExpansions (T_DollarExpansion id _) = warning id
|
||||
checkExpansions (T_Backticked id _) = warning id
|
||||
checkExpansions (T_DollarBraced id _) = warning id
|
||||
checkExpansions (T_DollarBraced id _ _) = warning id
|
||||
checkExpansions (T_DollarArithmetic id _) = warning id
|
||||
checkExpansions _ = return ()
|
||||
|
||||
@@ -280,21 +320,34 @@ prop_checkReturn4 = verifyNot checkReturn "return $((a|b))"
|
||||
prop_checkReturn5 = verify checkReturn "return -1"
|
||||
prop_checkReturn6 = verify checkReturn "return 1000"
|
||||
prop_checkReturn7 = verify checkReturn "return 'hello world'"
|
||||
checkReturn = CommandCheck (Exactly "return") (f . arguments)
|
||||
checkReturn = CommandCheck (Exactly "return") (returnOrExit
|
||||
(\c -> err c 2151 "Only one integer 0-255 can be returned. Use stdout for other data.")
|
||||
(\c -> err c 2152 "Can only return 0-255. Other data should be written to stdout."))
|
||||
|
||||
prop_checkExit1 = verifyNot checkExit "exit"
|
||||
prop_checkExit2 = verifyNot checkExit "exit 1"
|
||||
prop_checkExit3 = verifyNot checkExit "exit $var"
|
||||
prop_checkExit4 = verifyNot checkExit "exit $((a|b))"
|
||||
prop_checkExit5 = verify checkExit "exit -1"
|
||||
prop_checkExit6 = verify checkExit "exit 1000"
|
||||
prop_checkExit7 = verify checkExit "exit 'hello world'"
|
||||
checkExit = CommandCheck (Exactly "exit") (returnOrExit
|
||||
(\c -> err c 2241 "The exit status can only be one integer 0-255. Use stdout for other data.")
|
||||
(\c -> err c 2242 "Can only exit with status 0-255. Other data should be written to stdout/stderr."))
|
||||
|
||||
returnOrExit multi invalid = (f . arguments)
|
||||
where
|
||||
f (first:second:_) =
|
||||
err (getId second) 2151
|
||||
"Only one integer 0-255 can be returned. Use stdout for other data."
|
||||
multi (getId first)
|
||||
f [value] =
|
||||
when (isInvalid $ literal value) $
|
||||
err (getId value) 2152
|
||||
"Can only return 0-255. Other data should be written to stdout."
|
||||
invalid (getId value)
|
||||
f _ = return ()
|
||||
|
||||
isInvalid s = s == "" || any (not . isDigit) s || length s > 5
|
||||
isInvalid s = null s || any (not . isDigit) s || length s > 5
|
||||
|| let value = (read s :: Integer) in value > 255
|
||||
|
||||
literal token = fromJust $ getLiteralStringExt lit token
|
||||
literal token = runIdentity $ getLiteralStringExt lit token
|
||||
lit (T_DollarBraced {}) = return "0"
|
||||
lit (T_DollarArithmetic {}) = return "0"
|
||||
lit (T_DollarExpansion {}) = return "0"
|
||||
@@ -311,7 +364,7 @@ checkFindExecWithSingleArgument = CommandCheck (Basename "find") (f . arguments)
|
||||
check (exec:arg:term:_) = do
|
||||
execS <- getLiteralString exec
|
||||
termS <- getLiteralString term
|
||||
cmdS <- getLiteralStringExt (const $ return " ") arg
|
||||
let cmdS = getLiteralStringDef " " arg
|
||||
|
||||
guard $ execS `elem` ["-exec", "-execdir"] && termS `elem` [";", "+"]
|
||||
guard $ cmdS `matches` commandRegex
|
||||
@@ -407,7 +460,7 @@ prop_checkMkdirDashPM20 = verifyNot checkMkdirDashPM "mkdir -p -m 0755 .././bin"
|
||||
prop_checkMkdirDashPM21 = verifyNot checkMkdirDashPM "mkdir -p -m 0755 ../../bin"
|
||||
checkMkdirDashPM = CommandCheck (Basename "mkdir") check
|
||||
where
|
||||
check t = potentially $ do
|
||||
check t = sequence_ $ do
|
||||
let flags = getAllFlags t
|
||||
dashP <- find ((\f -> f == "p" || f == "parents") . snd) flags
|
||||
dashM <- find ((\f -> f == "m" || f == "mode") . snd) flags
|
||||
@@ -433,7 +486,7 @@ checkNonportableSignals = CommandCheck (Exactly "trap") (f . arguments)
|
||||
first:rest -> unless (isFlag first) $ mapM_ check rest
|
||||
_ -> return ()
|
||||
|
||||
check param = potentially $ do
|
||||
check param = sequence_ $ do
|
||||
str <- getLiteralString param
|
||||
let id = getId param
|
||||
return $ sequence_ $ mapMaybe (\f -> f id str) [
|
||||
@@ -462,7 +515,7 @@ prop_checkInteractiveSu4 = verifyNot checkInteractiveSu "su root < script"
|
||||
checkInteractiveSu = CommandCheck (Basename "su") f
|
||||
where
|
||||
f cmd = when (length (arguments cmd) <= 1) $ do
|
||||
path <- pathTo cmd
|
||||
path <- getPathM cmd
|
||||
when (all undirected path) $
|
||||
info (getId cmd) 2117
|
||||
"To run commands as another user, use su -c or sudo."
|
||||
@@ -511,52 +564,83 @@ prop_checkPrintfVar15= verifyNot checkPrintfVar "printf '%*s\\n' 1 2"
|
||||
prop_checkPrintfVar16= verifyNot checkPrintfVar "printf $'string'"
|
||||
prop_checkPrintfVar17= verify checkPrintfVar "printf '%-*s\\n' 1"
|
||||
prop_checkPrintfVar18= verifyNot checkPrintfVar "printf '%-*s\\n' 1 2"
|
||||
prop_checkPrintfVar19= verifyNot checkPrintfVar "printf '%(%s)T'"
|
||||
prop_checkPrintfVar20= verifyNot checkPrintfVar "printf '%d %(%s)T' 42"
|
||||
prop_checkPrintfVar21= verify checkPrintfVar "printf '%d %(%s)T'"
|
||||
checkPrintfVar = CommandCheck (Exactly "printf") (f . arguments) where
|
||||
f (doubledash:rest) | getLiteralString doubledash == Just "--" = f rest
|
||||
f (dashv:var:rest) | getLiteralString dashv == Just "-v" = f rest
|
||||
f (format:params) = check format params
|
||||
f _ = return ()
|
||||
|
||||
countFormats string =
|
||||
case string of
|
||||
'%':'%':rest -> countFormats rest
|
||||
'%':'(':rest -> 1 + countFormats (dropWhile (/= ')') rest)
|
||||
'%':rest -> regexBasedCountFormats rest + countFormats (dropWhile (/= '%') rest)
|
||||
_:rest -> countFormats rest
|
||||
[] -> 0
|
||||
|
||||
regexBasedCountFormats rest =
|
||||
maybe 1 (foldl (\acc group -> acc + (if group == "*" then 1 else 0)) 1) (matchRegex re rest)
|
||||
where
|
||||
-- constructed based on specifications in "man printf"
|
||||
re = mkRegex "#?-?\\+? ?0?(\\*|\\d*).?(\\d*|\\*)[diouxXfFeEgGaAcsb]"
|
||||
-- \____ _____/\___ ____/ \____ ____/\________ ________/
|
||||
-- V V V V
|
||||
-- flags field width precision format character
|
||||
-- field width and precision can be specified with a '*' instead of a digit,
|
||||
-- in which case printf will accept one more argument for each '*' used
|
||||
check format more = do
|
||||
fromMaybe (return ()) $ do
|
||||
sequence_ $ do
|
||||
string <- getLiteralString format
|
||||
let vars = countFormats string
|
||||
|
||||
return $ do
|
||||
when (vars == 0 && more /= []) $
|
||||
err (getId format) 2182
|
||||
"This printf format string has no variables. Other arguments are ignored."
|
||||
|
||||
when (vars > 0
|
||||
&& ((length more) `mod` vars /= 0 || null more)
|
||||
&& all (not . mayBecomeMultipleArgs) more) $
|
||||
warn (getId format) 2183 $
|
||||
"This format string has " ++ show vars ++ " variables, but is passed " ++ show (length more) ++ " arguments."
|
||||
let formats = getPrintfFormats string
|
||||
let formatCount = length formats
|
||||
let argCount = length more
|
||||
|
||||
return $
|
||||
case () of
|
||||
() | argCount == 0 && formatCount == 0 ->
|
||||
return () -- This is fine
|
||||
() | formatCount == 0 && argCount > 0 ->
|
||||
err (getId format) 2182
|
||||
"This printf format string has no variables. Other arguments are ignored."
|
||||
() | any mayBecomeMultipleArgs more ->
|
||||
return () -- We don't know so trust the user
|
||||
() | argCount < formatCount && onlyTrailingTs formats argCount ->
|
||||
return () -- Allow trailing %()Ts since they use the current time
|
||||
() | argCount > 0 && argCount `mod` formatCount == 0 ->
|
||||
return () -- Great: a suitable number of arguments
|
||||
() ->
|
||||
warn (getId format) 2183 $
|
||||
"This format string has " ++ show formatCount ++ " variables, but is passed " ++ show argCount ++ " arguments."
|
||||
|
||||
unless ('%' `elem` concat (oversimplify format) || isLiteral format) $
|
||||
info (getId format) 2059
|
||||
"Don't use variables in the printf format string. Use printf \"..%s..\" \"$foo\"."
|
||||
"Don't use variables in the printf format string. Use printf '..%s..' \"$foo\"."
|
||||
where
|
||||
onlyTrailingTs format argCount =
|
||||
all (== 'T') $ drop argCount format
|
||||
|
||||
|
||||
prop_checkGetPrintfFormats1 = getPrintfFormats "%s" == "s"
|
||||
prop_checkGetPrintfFormats2 = getPrintfFormats "%0*s" == "*s"
|
||||
prop_checkGetPrintfFormats3 = getPrintfFormats "%(%s)T" == "T"
|
||||
prop_checkGetPrintfFormats4 = getPrintfFormats "%d%%%(%s)T" == "dT"
|
||||
prop_checkGetPrintfFormats5 = getPrintfFormats "%bPassed: %d, %bFailed: %d%b, Skipped: %d, %bErrored: %d%b\\n" == "bdbdbdbdb"
|
||||
getPrintfFormats = getFormats
|
||||
where
|
||||
-- Get the arguments in the string as a string of type characters,
|
||||
-- e.g. "Hello %s" -> "s" and "%(%s)T %0*d\n" -> "T*d"
|
||||
getFormats :: String -> String
|
||||
getFormats string =
|
||||
case string of
|
||||
'%':'%':rest -> getFormats rest
|
||||
'%':'(':rest ->
|
||||
case dropWhile (/= ')') rest of
|
||||
')':c:trailing -> c : getFormats trailing
|
||||
_ -> ""
|
||||
'%':rest -> regexBasedGetFormats rest
|
||||
_:rest -> getFormats rest
|
||||
[] -> ""
|
||||
|
||||
regexBasedGetFormats rest =
|
||||
case matchRegex re rest of
|
||||
Just [width, precision, typ, rest] ->
|
||||
(if width == "*" then "*" else "") ++
|
||||
(if precision == "*" then "*" else "") ++
|
||||
typ ++ getFormats rest
|
||||
Nothing -> take 1 rest ++ getFormats rest
|
||||
where
|
||||
-- constructed based on specifications in "man printf"
|
||||
re = mkRegex "#?-?\\+? ?0?(\\*|\\d*)\\.?(\\d*|\\*)([diouxXfFeEgGaAcsbq])(.*)"
|
||||
-- \____ _____/\___ ____/ \____ ____/\_________ _________/ \ /
|
||||
-- V V V V V
|
||||
-- flags field width precision format character rest
|
||||
-- field width and precision can be specified with a '*' instead of a digit,
|
||||
-- in which case printf will accept one more argument for each '*' used
|
||||
|
||||
|
||||
prop_checkUuoeCmd1 = verify checkUuoeCmd "echo $(date)"
|
||||
@@ -602,7 +686,7 @@ prop_checkExportedExpansions3 = verifyNot checkExportedExpansions "export foo"
|
||||
prop_checkExportedExpansions4 = verifyNot checkExportedExpansions "export ${foo?}"
|
||||
checkExportedExpansions = CommandCheck (Exactly "export") (mapM_ check . arguments)
|
||||
where
|
||||
check t = potentially $ do
|
||||
check t = sequence_ $ do
|
||||
var <- getSingleUnmodifiedVariable t
|
||||
let name = bracedString var
|
||||
return . warn (getId t) 2163 $
|
||||
@@ -618,13 +702,13 @@ prop_checkReadExpansions7 = verifyNot checkReadExpansions "read $1"
|
||||
prop_checkReadExpansions8 = verifyNot checkReadExpansions "read ${var?}"
|
||||
checkReadExpansions = CommandCheck (Exactly "read") check
|
||||
where
|
||||
options = getGnuOpts "sreu:n:N:i:p:a:"
|
||||
options = getGnuOpts flagsForRead
|
||||
getVars cmd = fromMaybe [] $ do
|
||||
opts <- options cmd
|
||||
return . map snd $ filter (\(x,_) -> x == "" || x == "a") opts
|
||||
return [y | (x,y) <- opts, null x || x == "a"]
|
||||
|
||||
check cmd = mapM_ warning $ getVars cmd
|
||||
warning t = potentially $ do
|
||||
warning t = sequence_ $ do
|
||||
var <- getSingleUnmodifiedVariable t
|
||||
let name = bracedString var
|
||||
guard $ isVariableName name -- e.g. not $1
|
||||
@@ -650,7 +734,7 @@ checkAliasesUsesArgs = CommandCheck (Exactly "alias") (f . arguments)
|
||||
re = mkRegex "\\$\\{?[0-9*@]"
|
||||
f = mapM_ checkArg
|
||||
checkArg arg =
|
||||
let string = fromJust $ getLiteralStringExt (const $ return "_") arg in
|
||||
let string = getLiteralStringDef "_" arg in
|
||||
when ('=' `elem` string && string `matches` re) $
|
||||
err (getId arg) 2142
|
||||
"Aliases can't use positional parameters. Use a function."
|
||||
@@ -663,7 +747,7 @@ checkAliasesExpandEarly = CommandCheck (Exactly "alias") (f . arguments)
|
||||
where
|
||||
f = mapM_ checkArg
|
||||
checkArg arg | '=' `elem` concat (oversimplify arg) =
|
||||
forM_ (take 1 $ filter (not . isLiteral) $ getWordParts arg) $
|
||||
forM_ (find (not . isLiteral) $ getWordParts arg) $
|
||||
\x -> warn (getId x) 2139 "This expands when defined, not when used. Consider escaping."
|
||||
checkArg _ = return ()
|
||||
|
||||
@@ -696,7 +780,7 @@ checkFindWithoutPath = CommandCheck (Basename "find") f
|
||||
-- path. We assume that all the pre-path flags are single characters from a
|
||||
-- list of GNU and macOS flags.
|
||||
hasPath (first:rest) =
|
||||
let flag = fromJust $ getLiteralStringExt (const $ return "___") first in
|
||||
let flag = getLiteralStringDef "___" first in
|
||||
not ("-" `isPrefixOf` flag) || isLeadingFlag flag && hasPath rest
|
||||
hasPath [] = False
|
||||
isLeadingFlag flag = length flag <= 2 || all (`elem` leadingFlagChars) flag
|
||||
@@ -748,7 +832,7 @@ prop_checkLocalScope2 = verifyNot checkLocalScope "f() { local foo=3; }"
|
||||
checkLocalScope = CommandCheck (Exactly "local") $ \t ->
|
||||
whenShell [Bash, Dash] $ do -- Ksh allows it, Sh doesn't support local
|
||||
path <- getPathM t
|
||||
unless (any isFunction path) $
|
||||
unless (any isFunctionLike path) $
|
||||
err (getId $ getCommandTokenOrThis t) 2168 "'local' is only valid in functions."
|
||||
|
||||
prop_checkDeprecatedTempfile1 = verify checkDeprecatedTempfile "var=$(tempfile)"
|
||||
@@ -774,7 +858,7 @@ checkWhileGetoptsCase = CommandCheck (Exactly "getopts") f
|
||||
f :: Token -> Analysis
|
||||
f t@(T_SimpleCommand _ _ (cmd:arg1:_)) = do
|
||||
path <- getPathM t
|
||||
potentially $ do
|
||||
sequence_ $ do
|
||||
options <- getLiteralString arg1
|
||||
(T_WhileExpression _ _ body) <- findFirst whileLoop path
|
||||
caseCmd <- mapMaybe findCase body !!! 0
|
||||
@@ -801,7 +885,7 @@ checkWhileGetoptsCase = CommandCheck (Exactly "getopts") f
|
||||
warnUnhandled optId caseId str =
|
||||
warn caseId 2213 $ "getopts specified -" ++ str ++ ", but it's not handled by this 'case'."
|
||||
|
||||
warnRedundant (key, expr) = potentially $ do
|
||||
warnRedundant (key, expr) = sequence_ $ do
|
||||
str <- key
|
||||
guard $ str `notElem` ["*", ":", "?"]
|
||||
return $ warn (getId expr) 2214 "This case is not specified by getopts."
|
||||
@@ -860,7 +944,7 @@ checkCatastrophicRm = CommandCheck (Basename "rm") $ \t ->
|
||||
Nothing ->
|
||||
checkWord' token
|
||||
|
||||
checkWord' token = fromMaybe (return ()) $ do
|
||||
checkWord' token = sequence_ $ do
|
||||
filename <- getPotentialPath token
|
||||
let path = fixPath filename
|
||||
return . when (path `elem` importantPaths) $
|
||||
@@ -873,7 +957,7 @@ checkCatastrophicRm = CommandCheck (Basename "rm") $ \t ->
|
||||
getPotentialPath = getLiteralStringExt f
|
||||
where
|
||||
f (T_Glob _ str) = return str
|
||||
f (T_DollarBraced _ word) =
|
||||
f (T_DollarBraced _ _ word) =
|
||||
let var = onlyLiteralString word in
|
||||
-- This shouldn't handle non-colon cases.
|
||||
if any (`isInfixOf` var) [":?", ":-", ":="]
|
||||
@@ -910,10 +994,9 @@ missingDestination handler token = do
|
||||
_ -> return ()
|
||||
where
|
||||
args = getAllFlags token
|
||||
params = map fst $ filter (\(_,x) -> x == "") args
|
||||
params = [x | (x,"") <- args]
|
||||
hasTarget =
|
||||
any (\x -> x /= "" && x `isPrefixOf` "target-directory") $
|
||||
map snd args
|
||||
any (\(_,x) -> x /= "" && x `isPrefixOf` "target-directory") args
|
||||
|
||||
prop_checkMvArguments1 = verify checkMvArguments "mv 'foo bar'"
|
||||
prop_checkMvArguments2 = verifyNot checkMvArguments "mv foo bar"
|
||||
@@ -973,7 +1056,7 @@ checkSudoRedirect = CommandCheck (Basename "sudo") f
|
||||
Just (T_Redirecting _ redirs _) ->
|
||||
mapM_ warnAbout redirs
|
||||
warnAbout (T_FdRedirect _ s (T_IoFile id op file))
|
||||
| (s == "" || s == "&") && not (special file) =
|
||||
| (null s || s == "&") && not (special file) =
|
||||
case op of
|
||||
T_Less _ ->
|
||||
info (getId op) 2024
|
||||
@@ -997,9 +1080,9 @@ prop_checkSudoArgs6 = verifyNot checkSudoArgs "sudo -n -u export ls"
|
||||
prop_checkSudoArgs7 = verifyNot checkSudoArgs "sudo docker export foo"
|
||||
checkSudoArgs = CommandCheck (Basename "sudo") f
|
||||
where
|
||||
f t = potentially $ do
|
||||
f t = sequence_ $ do
|
||||
opts <- parseOpts t
|
||||
let nonFlags = map snd $ filter (\(flag, _) -> flag == "") opts
|
||||
let nonFlags = [x | ("",x) <- opts]
|
||||
commandArg <- nonFlags !!! 0
|
||||
command <- getLiteralString commandArg
|
||||
guard $ command `elem` builtins
|
||||
@@ -1008,5 +1091,27 @@ checkSudoArgs = CommandCheck (Basename "sudo") f
|
||||
-- This mess is why ShellCheck prefers not to know.
|
||||
parseOpts = getBsdOpts "vAknSbEHPa:g:h:p:u:c:T:r:"
|
||||
|
||||
prop_checkSourceArgs1 = verify checkSourceArgs "#!/bin/sh\n. script arg"
|
||||
prop_checkSourceArgs2 = verifyNot checkSourceArgs "#!/bin/sh\n. script"
|
||||
prop_checkSourceArgs3 = verifyNot checkSourceArgs "#!/bin/bash\n. script arg"
|
||||
checkSourceArgs = CommandCheck (Exactly ".") f
|
||||
where
|
||||
f t = whenShell [Sh, Dash] $
|
||||
case arguments t of
|
||||
(file:arg1:_) -> warn (getId arg1) 2240 $
|
||||
"The dot command does not support arguments in sh/dash. Set them as variables."
|
||||
_ -> return ()
|
||||
|
||||
prop_checkChmodDashr1 = verify checkChmodDashr "chmod -r 0755 dir"
|
||||
prop_checkChmodDashr2 = verifyNot checkChmodDashr "chmod -R 0755 dir"
|
||||
prop_checkChmodDashr3 = verifyNot checkChmodDashr "chmod a-r dir"
|
||||
checkChmodDashr = CommandCheck (Basename "chmod") f
|
||||
where
|
||||
f t = mapM_ check $ arguments t
|
||||
check t = sequence_ $ do
|
||||
flag <- getLiteralString t
|
||||
guard $ flag == "-r"
|
||||
return $ warn (getId t) 2253 "Use -R to recurse, or explicitly a-r to remove read permissions."
|
||||
|
||||
return []
|
||||
runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])
|
||||
|
21
src/ShellCheck/Checks/Custom.hs
Normal file
21
src/ShellCheck/Checks/Custom.hs
Normal file
@@ -0,0 +1,21 @@
|
||||
{-
|
||||
This empty file is provided for ease of patching in site specific checks.
|
||||
However, there are no guarantees regarding compatibility between versions.
|
||||
-}
|
||||
|
||||
{-# LANGUAGE TemplateHaskell #-}
|
||||
module ShellCheck.Checks.Custom (checker, ShellCheck.Checks.Custom.runTests) where
|
||||
|
||||
import ShellCheck.AnalyzerLib
|
||||
import Test.QuickCheck
|
||||
|
||||
checker :: Parameters -> Checker
|
||||
checker params = Checker {
|
||||
perScript = const $ return (),
|
||||
perToken = const $ return ()
|
||||
}
|
||||
|
||||
prop_CustomTestsWork = True
|
||||
|
||||
return []
|
||||
runTests = $quickCheckAll
|
@@ -30,9 +30,11 @@ import ShellCheck.Regex
|
||||
import Control.Monad
|
||||
import Control.Monad.RWS
|
||||
import Data.Char
|
||||
import Data.Functor.Identity
|
||||
import Data.List
|
||||
import Data.Maybe
|
||||
import qualified Data.Map as Map
|
||||
import qualified Data.Set as Set
|
||||
import Test.QuickCheck.All (forAllProperties)
|
||||
import Test.QuickCheck.Test (quickCheckWithResult, stdArgs, maxSuccess)
|
||||
|
||||
@@ -72,7 +74,7 @@ prop_checkForDecimals2 = verify checkForDecimals "foo[1.2]=bar"
|
||||
prop_checkForDecimals3 = verifyNot checkForDecimals "declare -A foo; foo[1.2]=bar"
|
||||
checkForDecimals = ForShell [Sh, Dash, Bash] f
|
||||
where
|
||||
f t@(TA_Expansion id _) = potentially $ do
|
||||
f t@(TA_Expansion id _) = sequence_ $ do
|
||||
str <- getLiteralString t
|
||||
first <- str !!! 0
|
||||
guard $ isDigit first && '.' `elem` str
|
||||
@@ -136,6 +138,46 @@ prop_checkBashisms53= verifyNot checkBashisms "#!/bin/sh\nprintf -- -f\n"
|
||||
prop_checkBashisms54= verify checkBashisms "#!/bin/sh\nfoo+=bar"
|
||||
prop_checkBashisms55= verify checkBashisms "#!/bin/sh\necho ${@%foo}"
|
||||
prop_checkBashisms56= verifyNot checkBashisms "#!/bin/sh\necho ${##}"
|
||||
prop_checkBashisms57= verifyNot checkBashisms "#!/bin/dash\nulimit -c 0"
|
||||
prop_checkBashisms58= verify checkBashisms "#!/bin/sh\nulimit -c 0"
|
||||
prop_checkBashisms59 = verify checkBashisms "#!/bin/sh\njobs -s"
|
||||
prop_checkBashisms60 = verifyNot checkBashisms "#!/bin/sh\njobs -p"
|
||||
prop_checkBashisms61 = verifyNot checkBashisms "#!/bin/sh\njobs -lp"
|
||||
prop_checkBashisms62 = verify checkBashisms "#!/bin/sh\nexport -f foo"
|
||||
prop_checkBashisms63 = verifyNot checkBashisms "#!/bin/sh\nexport -p"
|
||||
prop_checkBashisms64 = verify checkBashisms "#!/bin/sh\nreadonly -a"
|
||||
prop_checkBashisms65 = verifyNot checkBashisms "#!/bin/sh\nreadonly -p"
|
||||
prop_checkBashisms66 = verifyNot checkBashisms "#!/bin/sh\ncd -P ."
|
||||
prop_checkBashisms67 = verify checkBashisms "#!/bin/sh\ncd -P -e ."
|
||||
prop_checkBashisms68 = verify checkBashisms "#!/bin/sh\numask -p"
|
||||
prop_checkBashisms69 = verifyNot checkBashisms "#!/bin/sh\numask -S"
|
||||
prop_checkBashisms70 = verify checkBashisms "#!/bin/sh\ntrap -l"
|
||||
prop_checkBashisms71 = verify checkBashisms "#!/bin/sh\ntype -a ls"
|
||||
prop_checkBashisms72 = verifyNot checkBashisms "#!/bin/sh\ntype ls"
|
||||
prop_checkBashisms73 = verify checkBashisms "#!/bin/sh\nunset -n namevar"
|
||||
prop_checkBashisms74 = verifyNot checkBashisms "#!/bin/sh\nunset -f namevar"
|
||||
prop_checkBashisms75 = verifyNot checkBashisms "#!/bin/sh\necho \"-n foo\""
|
||||
prop_checkBashisms76 = verifyNot checkBashisms "#!/bin/sh\necho \"-ne foo\""
|
||||
prop_checkBashisms77 = verifyNot checkBashisms "#!/bin/sh\necho -Q foo"
|
||||
prop_checkBashisms78 = verify checkBashisms "#!/bin/sh\necho -ne foo"
|
||||
prop_checkBashisms79 = verify checkBashisms "#!/bin/sh\nhash -l"
|
||||
prop_checkBashisms80 = verifyNot checkBashisms "#!/bin/sh\nhash -r"
|
||||
prop_checkBashisms81 = verifyNot checkBashisms "#!/bin/dash\nhash -v"
|
||||
prop_checkBashisms82 = verifyNot checkBashisms "#!/bin/sh\nset -v +o allexport -o errexit -C"
|
||||
prop_checkBashisms83 = verifyNot checkBashisms "#!/bin/sh\nset --"
|
||||
prop_checkBashisms84 = verify checkBashisms "#!/bin/sh\nset -o pipefail"
|
||||
prop_checkBashisms85 = verify checkBashisms "#!/bin/sh\nset -B"
|
||||
prop_checkBashisms86 = verifyNot checkBashisms "#!/bin/dash\nset -o emacs"
|
||||
prop_checkBashisms87 = verify checkBashisms "#!/bin/sh\nset -o emacs"
|
||||
prop_checkBashisms88 = verifyNot checkBashisms "#!/bin/sh\nset -- wget -o foo 'https://some.url'"
|
||||
prop_checkBashisms89 = verifyNot checkBashisms "#!/bin/sh\nopts=$-\nset -\"$opts\""
|
||||
prop_checkBashisms90 = verifyNot checkBashisms "#!/bin/sh\nset -o \"$opt\""
|
||||
prop_checkBashisms91 = verify checkBashisms "#!/bin/sh\nwait -n"
|
||||
prop_checkBashisms92 = verify checkBashisms "#!/bin/sh\necho $((16#FF))"
|
||||
prop_checkBashisms93 = verify checkBashisms "#!/bin/sh\necho $(( 10#$(date +%m) ))"
|
||||
prop_checkBashisms94 = verify checkBashisms "#!/bin/sh\n[ -v var ]"
|
||||
prop_checkBashisms95 = verify checkBashisms "#!/bin/sh\necho $_"
|
||||
prop_checkBashisms96 = verifyNot checkBashisms "#!/bin/dash\necho $_"
|
||||
checkBashisms = ForShell [Sh, Dash] $ \t -> do
|
||||
params <- ask
|
||||
kludge params t
|
||||
@@ -164,12 +206,14 @@ checkBashisms = ForShell [Sh, Dash] $ \t -> do
|
||||
| op `elem` [ "<", ">", "\\<", "\\>", "<=", ">=", "\\<=", "\\>="] =
|
||||
unless isDash $ warnMsg id $ "lexicographical " ++ op ++ " is"
|
||||
bashism (TC_Binary id SingleBracket op _ _)
|
||||
| op `elem` [ "-nt", "-ef" ] =
|
||||
| op `elem` [ "-ot", "-nt", "-ef" ] =
|
||||
unless isDash $ warnMsg id $ op ++ " is"
|
||||
bashism (TC_Binary id SingleBracket "==" _ _) =
|
||||
warnMsg id "== in place of = is"
|
||||
bashism (TC_Binary id SingleBracket "=~" _ _) =
|
||||
warnMsg id "=~ regex matching is"
|
||||
bashism (TC_Unary id SingleBracket "-v" _) =
|
||||
warnMsg id "unary -v (in place of [ -n \"${var+x}\" ]) is"
|
||||
bashism (TC_Unary id _ "-a" _) =
|
||||
warnMsg id "unary -a in place of -e is"
|
||||
bashism (TA_Unary id op _)
|
||||
@@ -194,7 +238,7 @@ checkBashisms = ForShell [Sh, Dash] $ \t -> do
|
||||
bashism t@(TA_Variable id str _) | isBashVariable str =
|
||||
warnMsg id $ str ++ " is"
|
||||
|
||||
bashism t@(T_DollarBraced id token) = do
|
||||
bashism t@(T_DollarBraced id _ token) = do
|
||||
mapM_ check expansion
|
||||
when (isBashVariable var) $
|
||||
warnMsg id $ var ++ " is"
|
||||
@@ -222,20 +266,71 @@ checkBashisms = ForShell [Sh, Dash] $ \t -> do
|
||||
warnMsg id "`<file` to read files is"
|
||||
|
||||
bashism t@(T_SimpleCommand _ _ (cmd:arg:_))
|
||||
| t `isCommand` "echo" && "-" `isPrefixOf` argString =
|
||||
unless ("--" `isPrefixOf` argString) $ -- echo "-----"
|
||||
if isDash
|
||||
then
|
||||
when (argString /= "-n") $
|
||||
warnMsg (getId arg) "echo flags besides -n"
|
||||
else
|
||||
warnMsg (getId arg) "echo flags are"
|
||||
where argString = concat $ oversimplify arg
|
||||
| t `isCommand` "echo" && argString `matches` flagRegex =
|
||||
if isDash
|
||||
then
|
||||
when (argString /= "-n") $
|
||||
warnMsg (getId arg) "echo flags besides -n"
|
||||
else
|
||||
warnMsg (getId arg) "echo flags are"
|
||||
where
|
||||
argString = concat $ oversimplify arg
|
||||
flagRegex = mkRegex "^-[eEsn]+$"
|
||||
|
||||
bashism t@(T_SimpleCommand _ _ (cmd:arg:_))
|
||||
| t `isCommand` "exec" && "-" `isPrefixOf` concat (oversimplify arg) =
|
||||
warnMsg (getId arg) "exec flags are"
|
||||
bashism t@(T_SimpleCommand id _ _)
|
||||
| t `isCommand` "let" = warnMsg id "'let' is"
|
||||
bashism t@(T_SimpleCommand _ _ (cmd:args))
|
||||
| t `isCommand` "set" = unless isDash $
|
||||
checkOptions $ getLiteralArgs args
|
||||
where
|
||||
-- Get the literal options from a list of arguments,
|
||||
-- up until the first non-literal one
|
||||
getLiteralArgs :: [Token] -> [(Id, String)]
|
||||
getLiteralArgs (first:rest) = fromMaybe [] $ do
|
||||
str <- getLiteralString first
|
||||
return $ (getId first, str) : getLiteralArgs rest
|
||||
getLiteralArgs [] = []
|
||||
|
||||
-- Check a flag-option pair (such as -o errexit)
|
||||
checkOptions (flag@(fid,flag') : opt@(oid,opt') : rest)
|
||||
| flag' `matches` oFlagRegex = do
|
||||
when (opt' `notElem` longOptions) $
|
||||
warnMsg oid $ "set option " <> opt' <> " is"
|
||||
checkFlags (flag:rest)
|
||||
| otherwise = checkFlags (flag:opt:rest)
|
||||
checkOptions (flag:rest) = checkFlags (flag:rest)
|
||||
checkOptions _ = return ()
|
||||
|
||||
-- Check that each option in a sequence of flags
|
||||
-- (such as -aveo) is valid
|
||||
checkFlags (flag@(fid, flag'):rest)
|
||||
| startsOption flag' = do
|
||||
unless (flag' `matches` validFlagsRegex) $
|
||||
forM_ (tail flag') $ \letter ->
|
||||
when (letter `notElem` optionsSet) $
|
||||
warnMsg fid $ "set flag " <> ('-':letter:" is")
|
||||
checkOptions rest
|
||||
| beginsWithDoubleDash flag' = do
|
||||
warnMsg fid $ "set flag " <> flag' <> " is"
|
||||
checkOptions rest
|
||||
-- Either a word that doesn't start with a dash, or simply '--',
|
||||
-- so stop checking.
|
||||
| otherwise = return ()
|
||||
checkFlags [] = return ()
|
||||
|
||||
options = "abCefhmnuvxo"
|
||||
optionsSet = Set.fromList options
|
||||
startsOption = (`matches` mkRegex "^(\\+|-[^-])")
|
||||
oFlagRegex = mkRegex $ "^[-+][" <> options <> "]*o$"
|
||||
validFlagsRegex = mkRegex $ "^[-+]([" <> options <> "]+o?|o)$"
|
||||
beginsWithDoubleDash = (`matches` mkRegex "^--.+$")
|
||||
longOptions = Set.fromList
|
||||
[ "allexport", "errexit", "ignoreeof", "monitor", "noclobber"
|
||||
, "noexec", "noglob", "nolog", "notify" , "nounset", "verbose"
|
||||
, "vi", "xtrace" ]
|
||||
|
||||
bashism t@(T_SimpleCommand id _ (cmd:rest)) =
|
||||
let name = fromMaybe "" $ getCommandName t
|
||||
@@ -243,16 +338,17 @@ checkBashisms = ForShell [Sh, Dash] $ \t -> do
|
||||
in do
|
||||
when (name `elem` unsupportedCommands) $
|
||||
warnMsg id $ "'" ++ name ++ "' is"
|
||||
potentially $ do
|
||||
allowed <- Map.lookup name allowedFlags
|
||||
(word, flag) <- listToMaybe $
|
||||
filter (\x -> (not . null . snd $ x) && snd x `notElem` allowed) flags
|
||||
sequence_ $ do
|
||||
allowed' <- Map.lookup name allowedFlags
|
||||
allowed <- allowed'
|
||||
(word, flag) <- find
|
||||
(\x -> (not . null . snd $ x) && snd x `notElem` allowed) flags
|
||||
return . warnMsg (getId word) $ name ++ " -" ++ flag ++ " is"
|
||||
|
||||
when (name == "source") $ warnMsg id "'source' in place of '.' is"
|
||||
when (name == "trap") $
|
||||
let
|
||||
check token = potentially $ do
|
||||
check token = sequence_ $ do
|
||||
str <- getLiteralString token
|
||||
let upper = map toUpper str
|
||||
return $ do
|
||||
@@ -267,7 +363,7 @@ checkBashisms = ForShell [Sh, Dash] $ \t -> do
|
||||
in
|
||||
mapM_ check (drop 1 rest)
|
||||
|
||||
when (name == "printf") $ potentially $ do
|
||||
when (name == "printf") $ sequence_ $ do
|
||||
format <- rest !!! 0 -- flags are covered by allowedFlags
|
||||
let literal = onlyLiteralString format
|
||||
guard $ "%q" `isInfixOf` literal
|
||||
@@ -279,16 +375,28 @@ checkBashisms = ForShell [Sh, Dash] $ \t -> do
|
||||
"typeset"
|
||||
] ++ if not isDash then ["local"] else []
|
||||
allowedFlags = Map.fromList [
|
||||
("exec", []),
|
||||
("export", ["-p"]),
|
||||
("printf", []),
|
||||
("read", if isDash then ["r", "p"] else ["r"]),
|
||||
("ulimit", ["f"])
|
||||
("cd", Just ["L", "P"]),
|
||||
("exec", Just []),
|
||||
("export", Just ["p"]),
|
||||
("hash", Just $ if isDash then ["r", "v"] else ["r"]),
|
||||
("jobs", Just ["l", "p"]),
|
||||
("printf", Just []),
|
||||
("read", Just $ if isDash then ["r", "p"] else ["r"]),
|
||||
("readonly", Just ["p"]),
|
||||
("trap", Just []),
|
||||
("type", Just []),
|
||||
("ulimit", if isDash then Nothing else Just ["f"]),
|
||||
("umask", Just ["S"]),
|
||||
("unset", Just ["f", "v"]),
|
||||
("wait", Just [])
|
||||
]
|
||||
bashism t@(T_SourceCommand id src _) =
|
||||
let name = fromMaybe "" $ getCommandName src
|
||||
in do
|
||||
when (name == "source") $ warnMsg id "'source' in place of '.' is"
|
||||
in when (name == "source") $ warnMsg id "'source' in place of '.' is"
|
||||
bashism (TA_Expansion _ (T_Literal id str : _)) | str `matches` radix =
|
||||
when (str `matches` radix) $ warnMsg id "arithmetic base conversion is"
|
||||
where
|
||||
radix = mkRegex "^[0-9]+#"
|
||||
bashism _ = return ()
|
||||
|
||||
varChars="_0-9a-zA-Z"
|
||||
@@ -303,10 +411,11 @@ checkBashisms = ForShell [Sh, Dash] $ \t -> do
|
||||
]
|
||||
bashVars = [
|
||||
"OSTYPE", "MACHTYPE", "HOSTTYPE", "HOSTNAME",
|
||||
"DIRSTACK", "EUID", "UID", "SHLVL", "PIPESTATUS", "SHELLOPTS"
|
||||
"DIRSTACK", "EUID", "UID", "SHLVL", "PIPESTATUS", "SHELLOPTS",
|
||||
"_"
|
||||
]
|
||||
bashDynamicVars = [ "RANDOM", "SECONDS" ]
|
||||
dashVars = [ ]
|
||||
dashVars = [ "_" ]
|
||||
isBashVariable var =
|
||||
(var `elem` bashDynamicVars
|
||||
|| var `elem` bashVars && not (isAssigned var))
|
||||
@@ -318,31 +427,44 @@ checkBashisms = ForShell [Sh, Dash] $ \t -> do
|
||||
_ -> False
|
||||
|
||||
prop_checkEchoSed1 = verify checkEchoSed "FOO=$(echo \"$cow\" | sed 's/foo/bar/g')"
|
||||
prop_checkEchoSed1b= verify checkEchoSed "FOO=$(sed 's/foo/bar/g' <<< \"$cow\")"
|
||||
prop_checkEchoSed2 = verify checkEchoSed "rm $(echo $cow | sed -e 's,foo,bar,')"
|
||||
prop_checkEchoSed2b= verify checkEchoSed "rm $(sed -e 's,foo,bar,' <<< $cow)"
|
||||
checkEchoSed = ForShell [Bash, Ksh] f
|
||||
where
|
||||
f (T_Redirecting id lefts r) =
|
||||
when (any redirectHereString lefts) $
|
||||
checkSed id rcmd
|
||||
where
|
||||
redirectHereString :: Token -> Bool
|
||||
redirectHereString t = case t of
|
||||
(T_FdRedirect _ _ T_HereString{}) -> True
|
||||
_ -> False
|
||||
rcmd = oversimplify r
|
||||
|
||||
f (T_Pipeline id _ [a, b]) =
|
||||
when (acmd == ["echo", "${VAR}"]) $
|
||||
case bcmd of
|
||||
["sed", v] -> checkIn v
|
||||
["sed", "-e", v] -> checkIn v
|
||||
_ -> return ()
|
||||
checkSed id bcmd
|
||||
where
|
||||
-- This should have used backreferences, but TDFA doesn't support them
|
||||
sedRe = mkRegex "^s(.)([^\n]*)g?$"
|
||||
isSimpleSed s = fromMaybe False $ do
|
||||
[first,rest] <- matchRegex sedRe s
|
||||
let delimiters = filter (== head first) rest
|
||||
guard $ length delimiters == 2
|
||||
return True
|
||||
|
||||
acmd = oversimplify a
|
||||
bcmd = oversimplify b
|
||||
checkIn s =
|
||||
when (isSimpleSed s) $
|
||||
style id 2001 "See if you can use ${variable//search/replace} instead."
|
||||
|
||||
f _ = return ()
|
||||
|
||||
checkSed id ["sed", v] = checkIn id v
|
||||
checkSed id ["sed", "-e", v] = checkIn id v
|
||||
checkSed _ _ = return ()
|
||||
|
||||
-- This should have used backreferences, but TDFA doesn't support them
|
||||
sedRe = mkRegex "^s(.)([^\n]*)g?$"
|
||||
isSimpleSed s = isJust $ do
|
||||
[h:_,rest] <- matchRegex sedRe s
|
||||
let delimiters = filter (== h) rest
|
||||
guard $ length delimiters == 2
|
||||
checkIn id s =
|
||||
when (isSimpleSed s) $
|
||||
style id 2001 "See if you can use ${variable//search/replace} instead."
|
||||
|
||||
|
||||
prop_checkBraceExpansionVars1 = verify checkBraceExpansionVars "echo {1..$n}"
|
||||
prop_checkBraceExpansionVars2 = verifyNot checkBraceExpansionVars "echo {1,3,$n}"
|
||||
@@ -365,11 +487,11 @@ checkBraceExpansionVars = ForShell [Bash] f
|
||||
T_DollarBraced {} -> return "$"
|
||||
T_DollarExpansion {} -> return "$"
|
||||
T_DollarArithmetic {} -> return "$"
|
||||
otherwise -> return "-"
|
||||
toString t = fromJust $ getLiteralStringExt literalExt t
|
||||
_ -> return "-"
|
||||
toString t = runIdentity $ getLiteralStringExt literalExt t
|
||||
isEvaled t = do
|
||||
cmd <- getClosestCommandM t
|
||||
return $ isJust cmd && fromJust cmd `isUnqualifiedCommand` "eval"
|
||||
return $ maybe False (`isUnqualifiedCommand` "eval") cmd
|
||||
|
||||
|
||||
prop_checkMultiDimensionalArrays1 = verify checkMultiDimensionalArrays "foo[a][b]=3"
|
||||
|
@@ -36,13 +36,29 @@ internalVariables = [
|
||||
|
||||
-- Ksh
|
||||
, ".sh.version"
|
||||
|
||||
-- shflags
|
||||
, "FLAGS_ARGC", "FLAGS_ARGV", "FLAGS_ERROR", "FLAGS_FALSE", "FLAGS_HELP",
|
||||
"FLAGS_PARENT", "FLAGS_RESERVED", "FLAGS_TRUE", "FLAGS_VERSION",
|
||||
"flags_error", "flags_return"
|
||||
]
|
||||
|
||||
variablesWithoutSpaces = [
|
||||
"$", "-", "?", "!", "#",
|
||||
specialVariablesWithoutSpaces = [
|
||||
"$", "-", "?", "!", "#"
|
||||
]
|
||||
variablesWithoutSpaces = specialVariablesWithoutSpaces ++ [
|
||||
"BASHPID", "BASH_ARGC", "BASH_LINENO", "BASH_SUBSHELL", "EUID", "LINENO",
|
||||
"OPTIND", "PPID", "RANDOM", "SECONDS", "SHELLOPTS", "SHLVL", "UID",
|
||||
"COLUMNS", "HISTFILESIZE", "HISTSIZE", "LINES"
|
||||
|
||||
-- shflags
|
||||
, "FLAGS_ERROR", "FLAGS_FALSE", "FLAGS_TRUE"
|
||||
]
|
||||
|
||||
specialVariables = specialVariablesWithoutSpaces ++ ["@", "*"]
|
||||
|
||||
unbracedVariables = specialVariables ++ [
|
||||
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
|
||||
]
|
||||
|
||||
arrayVariables = [
|
||||
@@ -98,6 +114,10 @@ binaryTestOps = [
|
||||
"-gt", "-ge", "=~", ">", "<", "=", "\\<", "\\>", "\\<=", "\\>="
|
||||
]
|
||||
|
||||
arithmeticBinaryTestOps = [
|
||||
"-eq", "-ne", "-lt", "-le", "-gt", "-ge"
|
||||
]
|
||||
|
||||
unaryTestOps = [
|
||||
"!", "-a", "-b", "-c", "-d", "-e", "-f", "-g", "-h", "-L", "-k", "-p",
|
||||
"-r", "-s", "-S", "-t", "-u", "-w", "-x", "-O", "-G", "-N", "-z", "-n",
|
||||
@@ -109,9 +129,12 @@ shellForExecutable name =
|
||||
case name of
|
||||
"sh" -> return Sh
|
||||
"bash" -> return Bash
|
||||
"bats" -> return Bash
|
||||
"dash" -> return Dash
|
||||
"ash" -> return Dash -- There's also a warning for this.
|
||||
"ksh" -> return Ksh
|
||||
"ksh88" -> return Ksh
|
||||
"ksh93" -> return Ksh
|
||||
otherwise -> Nothing
|
||||
_ -> Nothing
|
||||
|
||||
flagsForRead = "sreu:n:N:i:p:a:t:"
|
||||
|
409
src/ShellCheck/Fixer.hs
Normal file
409
src/ShellCheck/Fixer.hs
Normal file
@@ -0,0 +1,409 @@
|
||||
{-
|
||||
Copyright 2018-2019 Vidar Holen, Ng Zhi An
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
|
||||
ShellCheck is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
ShellCheck is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
-}
|
||||
|
||||
{-# LANGUAGE TemplateHaskell #-}
|
||||
module ShellCheck.Fixer (applyFix, removeTabStops, mapPositions, Ranged(..), runTests) where
|
||||
|
||||
import ShellCheck.Interface
|
||||
import Control.Monad.State
|
||||
import Data.Array
|
||||
import Data.List
|
||||
import Data.Semigroup
|
||||
import GHC.Exts (sortWith)
|
||||
import Test.QuickCheck
|
||||
|
||||
-- The Ranged class is used for types that has a start and end position.
|
||||
class Ranged a where
|
||||
start :: a -> Position
|
||||
end :: a -> Position
|
||||
overlap :: a -> a -> Bool
|
||||
overlap x y =
|
||||
(yStart >= xStart && yStart < xEnd) || (yStart < xStart && yEnd > xStart)
|
||||
where
|
||||
yStart = start y
|
||||
yEnd = end y
|
||||
xStart = start x
|
||||
xEnd = end x
|
||||
-- Set a new start and end position on a Ranged
|
||||
setRange :: (Position, Position) -> a -> a
|
||||
|
||||
-- Tests auto-verify that overlap commutes
|
||||
assertOverlap x y = overlap x y && overlap y x
|
||||
assertNoOverlap x y = not (overlap x y) && not (overlap y x)
|
||||
|
||||
prop_overlap_contiguous = assertNoOverlap
|
||||
(tFromStart 10 12 "foo" 1)
|
||||
(tFromStart 12 14 "bar" 2)
|
||||
|
||||
prop_overlap_adjacent_zerowidth = assertNoOverlap
|
||||
(tFromStart 3 3 "foo" 1)
|
||||
(tFromStart 3 3 "bar" 2)
|
||||
|
||||
prop_overlap_enclosed = assertOverlap
|
||||
(tFromStart 3 5 "foo" 1)
|
||||
(tFromStart 1 10 "bar" 2)
|
||||
|
||||
prop_overlap_partial = assertOverlap
|
||||
(tFromStart 1 5 "foo" 1)
|
||||
(tFromStart 3 7 "bar" 2)
|
||||
|
||||
|
||||
instance Ranged PositionedComment where
|
||||
start = pcStartPos
|
||||
end = pcEndPos
|
||||
setRange (s, e) pc = pc {
|
||||
pcStartPos = s,
|
||||
pcEndPos = e
|
||||
}
|
||||
|
||||
instance Ranged Replacement where
|
||||
start = repStartPos
|
||||
end = repEndPos
|
||||
setRange (s, e) r = r {
|
||||
repStartPos = s,
|
||||
repEndPos = e
|
||||
}
|
||||
|
||||
-- The Monoid instance for Fix merges fixes that do not conflict.
|
||||
-- TODO: Make an efficient 'mconcat'
|
||||
instance Monoid Fix where
|
||||
mempty = newFix
|
||||
mappend = (<>)
|
||||
|
||||
instance Semigroup Fix where
|
||||
f1 <> f2 =
|
||||
-- FIXME: This might need to also discard adjacent zero-width ranges for
|
||||
-- when two fixes change the same AST node, e.g. `foo` -> "$(foo)"
|
||||
if or [ r2 `overlap` r1 | r1 <- fixReplacements f1, r2 <- fixReplacements f2 ]
|
||||
then f1
|
||||
else newFix {
|
||||
fixReplacements = fixReplacements f1 ++ fixReplacements f2
|
||||
}
|
||||
|
||||
-- Conveniently apply a transformation to positions in a Fix
|
||||
mapPositions :: (Position -> Position) -> Fix -> Fix
|
||||
mapPositions f = adjustFix
|
||||
where
|
||||
adjustReplacement rep =
|
||||
rep {
|
||||
repStartPos = f $ repStartPos rep,
|
||||
repEndPos = f $ repEndPos rep
|
||||
}
|
||||
adjustFix fix =
|
||||
fix {
|
||||
fixReplacements = map adjustReplacement $ fixReplacements fix
|
||||
}
|
||||
|
||||
-- Rewrite a Ranged from a tabstop of 8 to 1
|
||||
removeTabStops :: Ranged a => a -> Array Int String -> a
|
||||
removeTabStops range ls =
|
||||
let startColumn = realignColumn lineNo colNo range
|
||||
endColumn = realignColumn endLineNo endColNo range
|
||||
startPosition = (start range) { posColumn = startColumn }
|
||||
endPosition = (end range) { posColumn = endColumn } in
|
||||
setRange (startPosition, endPosition) range
|
||||
where
|
||||
realignColumn lineNo colNo c =
|
||||
if lineNo c > 0 && lineNo c <= fromIntegral (length ls)
|
||||
then real (ls ! fromIntegral (lineNo c)) 0 0 (colNo c)
|
||||
else colNo c
|
||||
real _ r v target | target <= v = r
|
||||
-- hit this case at the end of line, and if we don't hit the target
|
||||
-- return real + (target - v)
|
||||
real [] r v target = r + (target - v)
|
||||
real ('\t':rest) r v target = real rest (r+1) (v + 8 - (v `mod` 8)) target
|
||||
real (_:rest) r v target = real rest (r+1) (v+1) target
|
||||
lineNo = posLine . start
|
||||
endLineNo = posLine . end
|
||||
colNo = posColumn . start
|
||||
endColNo = posColumn . end
|
||||
|
||||
|
||||
-- A replacement that spans multiple line is applied by:
|
||||
-- 1. merging the affected lines into a single string using `unlines`
|
||||
-- 2. apply the replacement as if it only spanned a single line
|
||||
-- The tricky part is adjusting the end column of the replacement
|
||||
-- (the end line doesn't matter because there is only one line)
|
||||
--
|
||||
-- aaS <--- start of replacement (row 1 column 3)
|
||||
-- bbbb
|
||||
-- cEc
|
||||
-- \------- end of replacement (row 3 column 2)
|
||||
--
|
||||
-- a flattened string will look like:
|
||||
--
|
||||
-- "aaS\nbbbb\ncEc\n"
|
||||
--
|
||||
-- The column of E has to be adjusted by:
|
||||
-- 1. lengths of lines to be replaced, except the end row itself
|
||||
-- 2. end column of the replacement
|
||||
-- 3. number of '\n' by `unlines`
|
||||
multiToSingleLine :: [Fix] -> Array Int String -> ([Fix], String)
|
||||
multiToSingleLine fixes lines =
|
||||
(map (mapPositions adjust) fixes, unlines $ elems lines)
|
||||
where
|
||||
-- A prefix sum tree from line number to column shift.
|
||||
-- FIXME: The tree will be totally unbalanced.
|
||||
shiftTree :: PSTree Int
|
||||
shiftTree =
|
||||
foldl (\t (n,s) -> addPSValue (n+1) (length s + 1) t) newPSTree $
|
||||
assocs lines
|
||||
singleString = unlines $ elems lines
|
||||
adjust pos =
|
||||
pos {
|
||||
posLine = 1,
|
||||
posColumn = (posColumn pos) +
|
||||
(fromIntegral $ getPrefixSum (fromIntegral $ posLine pos) shiftTree)
|
||||
}
|
||||
|
||||
-- Apply a fix and return resulting lines.
|
||||
-- The number of lines can increase or decrease with no obvious mapping back, so
|
||||
-- the function does not return an array.
|
||||
applyFix :: Fix -> Array Int String -> [String]
|
||||
applyFix fix fileLines =
|
||||
let
|
||||
untabbed = fix {
|
||||
fixReplacements =
|
||||
map (\c -> removeTabStops c fileLines) $
|
||||
fixReplacements fix
|
||||
}
|
||||
(adjustedFixes, singleLine) = multiToSingleLine [untabbed] fileLines
|
||||
in
|
||||
lines . runFixer $ applyFixes2 adjustedFixes singleLine
|
||||
|
||||
|
||||
-- start and end comes from pos, which is 1 based
|
||||
prop_doReplace1 = doReplace 0 0 "1234" "A" == "A1234" -- technically not valid
|
||||
prop_doReplace2 = doReplace 1 1 "1234" "A" == "A1234"
|
||||
prop_doReplace3 = doReplace 1 2 "1234" "A" == "A234"
|
||||
prop_doReplace4 = doReplace 3 3 "1234" "A" == "12A34"
|
||||
prop_doReplace5 = doReplace 4 4 "1234" "A" == "123A4"
|
||||
prop_doReplace6 = doReplace 5 5 "1234" "A" == "1234A"
|
||||
doReplace start end o r =
|
||||
let si = fromIntegral (start-1)
|
||||
ei = fromIntegral (end-1)
|
||||
(x, xs) = splitAt si o
|
||||
z = drop (ei - si) xs
|
||||
in
|
||||
x ++ r ++ z
|
||||
|
||||
-- Fail if the 'expected' string is not result when applying 'fixes' to 'original'.
|
||||
testFixes :: String -> String -> [Fix] -> Bool
|
||||
testFixes expected original fixes =
|
||||
actual == expected
|
||||
where
|
||||
actual = runFixer (applyFixes2 fixes original)
|
||||
|
||||
|
||||
-- A Fixer allows doing repeated modifications of a string where each
|
||||
-- replacement automatically accounts for shifts from previous ones.
|
||||
type Fixer a = State (PSTree Int) a
|
||||
|
||||
-- Apply a single replacement using its indices into the original string.
|
||||
-- It does not handle multiple lines, all line indices must be 1.
|
||||
applyReplacement2 :: Replacement -> String -> Fixer String
|
||||
applyReplacement2 rep string = do
|
||||
tree <- get
|
||||
let transform pos = pos + getPrefixSum pos tree
|
||||
let originalPos = (repStartPos rep, repEndPos rep)
|
||||
(oldStart, oldEnd) = tmap (fromInteger . posColumn) originalPos
|
||||
(newStart, newEnd) = tmap transform (oldStart, oldEnd)
|
||||
|
||||
let (l1, l2) = tmap posLine originalPos in
|
||||
when (l1 /= 1 || l2 /= 1) $
|
||||
error "ShellCheck internal error, please report: bad cross-line fix"
|
||||
|
||||
let replacer = repString rep
|
||||
let shift = (length replacer) - (oldEnd - oldStart)
|
||||
let insertionPoint =
|
||||
case repInsertionPoint rep of
|
||||
InsertBefore -> oldStart
|
||||
InsertAfter -> oldEnd+1
|
||||
put $ addPSValue insertionPoint shift tree
|
||||
|
||||
return $ doReplace newStart newEnd string replacer
|
||||
where
|
||||
tmap f (a,b) = (f a, f b)
|
||||
|
||||
-- Apply a list of Replacements in the correct order
|
||||
applyReplacements2 :: [Replacement] -> String -> Fixer String
|
||||
applyReplacements2 reps str =
|
||||
foldM (flip applyReplacement2) str $
|
||||
reverse $ sortWith repPrecedence reps
|
||||
|
||||
-- Apply all fixes with replacements in the correct order
|
||||
applyFixes2 :: [Fix] -> String -> Fixer String
|
||||
applyFixes2 fixes = applyReplacements2 (concatMap fixReplacements fixes)
|
||||
|
||||
-- Get the final value of a Fixer.
|
||||
runFixer :: Fixer a -> a
|
||||
runFixer f = evalState f newPSTree
|
||||
|
||||
|
||||
|
||||
-- A Prefix Sum Tree that lets you look up the sum of values at and below an index.
|
||||
-- It's implemented essentially as a Fenwick tree without the bit-based balancing.
|
||||
-- The last Num is the sum of the left branch plus current element.
|
||||
data PSTree n = PSBranch n (PSTree n) (PSTree n) n | PSLeaf
|
||||
deriving (Show)
|
||||
|
||||
newPSTree :: Num n => PSTree n
|
||||
newPSTree = PSLeaf
|
||||
|
||||
-- Get the sum of values whose keys are <= 'target'
|
||||
getPrefixSum :: (Ord n, Num n) => n -> PSTree n -> n
|
||||
getPrefixSum = f 0
|
||||
where
|
||||
f sum _ PSLeaf = sum
|
||||
f sum target (PSBranch pivot left right cumulative) =
|
||||
case () of
|
||||
_ | target < pivot -> f sum target left
|
||||
_ | target > pivot -> f (sum+cumulative) target right
|
||||
_ -> sum+cumulative
|
||||
|
||||
-- Add a value to the Prefix Sum tree at the given index.
|
||||
-- Values accumulate: addPSValue 42 2 . addPSValue 42 3 == addPSValue 42 5
|
||||
addPSValue :: (Ord n, Num n) => n -> n -> PSTree n -> PSTree n
|
||||
addPSValue key value tree = if value == 0 then tree else f tree
|
||||
where
|
||||
f PSLeaf = PSBranch key PSLeaf PSLeaf value
|
||||
f (PSBranch pivot left right sum) =
|
||||
case () of
|
||||
_ | key < pivot -> PSBranch pivot (f left) right (sum + value)
|
||||
_ | key > pivot -> PSBranch pivot left (f right) sum
|
||||
_ -> PSBranch pivot left right (sum + value)
|
||||
|
||||
prop_pstreeSumsCorrectly kvs targets =
|
||||
let
|
||||
-- Trivial O(n * m) implementation
|
||||
dumbPrefixSums :: [(Int, Int)] -> [Int] -> [Int]
|
||||
dumbPrefixSums kvs targets =
|
||||
let prefixSum target = sum [v | (k,v) <- kvs, k <= target]
|
||||
in map prefixSum targets
|
||||
-- PSTree O(n * log m) implementation
|
||||
smartPrefixSums :: [(Int, Int)] -> [Int] -> [Int]
|
||||
smartPrefixSums kvs targets =
|
||||
let tree = foldl (\tree (pos, shift) -> addPSValue pos shift tree) PSLeaf kvs
|
||||
in map (\x -> getPrefixSum x tree) targets
|
||||
in smartPrefixSums kvs targets == dumbPrefixSums kvs targets
|
||||
|
||||
|
||||
-- Semi-convenient functions for constructing tests.
|
||||
testFix :: [Replacement] -> Fix
|
||||
testFix list = newFix {
|
||||
fixReplacements = list
|
||||
}
|
||||
|
||||
tFromStart :: Int -> Int -> String -> Int -> Replacement
|
||||
tFromStart start end repl order =
|
||||
newReplacement {
|
||||
repStartPos = newPosition {
|
||||
posLine = 1,
|
||||
posColumn = fromIntegral start
|
||||
},
|
||||
repEndPos = newPosition {
|
||||
posLine = 1,
|
||||
posColumn = fromIntegral end
|
||||
},
|
||||
repString = repl,
|
||||
repPrecedence = order,
|
||||
repInsertionPoint = InsertAfter
|
||||
}
|
||||
|
||||
tFromEnd start end repl order =
|
||||
(tFromStart start end repl order) {
|
||||
repInsertionPoint = InsertBefore
|
||||
}
|
||||
|
||||
prop_simpleFix1 = testFixes "hello world" "hell world" [
|
||||
testFix [
|
||||
tFromEnd 5 5 "o" 1
|
||||
]]
|
||||
|
||||
prop_anchorsLeft = testFixes "-->foobar<--" "--><--" [
|
||||
testFix [
|
||||
tFromStart 4 4 "foo" 1,
|
||||
tFromStart 4 4 "bar" 2
|
||||
]]
|
||||
|
||||
prop_anchorsRight = testFixes "-->foobar<--" "--><--" [
|
||||
testFix [
|
||||
tFromEnd 4 4 "bar" 1,
|
||||
tFromEnd 4 4 "foo" 2
|
||||
]]
|
||||
|
||||
prop_anchorsBoth1 = testFixes "-->foobar<--" "--><--" [
|
||||
testFix [
|
||||
tFromStart 4 4 "bar" 2,
|
||||
tFromEnd 4 4 "foo" 1
|
||||
]]
|
||||
|
||||
prop_anchorsBoth2 = testFixes "-->foobar<--" "--><--" [
|
||||
testFix [
|
||||
tFromEnd 4 4 "foo" 2,
|
||||
tFromStart 4 4 "bar" 1
|
||||
]]
|
||||
|
||||
prop_composeFixes1 = testFixes "cd \"$1\" || exit" "cd $1" [
|
||||
testFix [
|
||||
tFromStart 4 4 "\"" 10,
|
||||
tFromEnd 6 6 "\"" 10
|
||||
],
|
||||
testFix [
|
||||
tFromEnd 6 6 " || exit" 5
|
||||
]]
|
||||
|
||||
prop_composeFixes2 = testFixes "$(\"$1\")" "`$1`" [
|
||||
testFix [
|
||||
tFromStart 1 2 "$(" 5,
|
||||
tFromEnd 4 5 ")" 5
|
||||
],
|
||||
testFix [
|
||||
tFromStart 2 2 "\"" 10,
|
||||
tFromEnd 4 4 "\"" 10
|
||||
]]
|
||||
|
||||
prop_composeFixes3 = testFixes "(x)[x]" "xx" [
|
||||
testFix [
|
||||
tFromStart 1 1 "(" 4,
|
||||
tFromEnd 2 2 ")" 3,
|
||||
tFromStart 2 2 "[" 2,
|
||||
tFromEnd 3 3 "]" 1
|
||||
]]
|
||||
|
||||
prop_composeFixes4 = testFixes "(x)[x]" "xx" [
|
||||
testFix [
|
||||
tFromStart 1 1 "(" 4,
|
||||
tFromStart 2 2 "[" 3,
|
||||
tFromEnd 2 2 ")" 2,
|
||||
tFromEnd 3 3 "]" 1
|
||||
]]
|
||||
|
||||
prop_composeFixes5 = testFixes "\"$(x)\"" "`x`" [
|
||||
testFix [
|
||||
tFromStart 1 2 "$(" 2,
|
||||
tFromEnd 3 4 ")" 2,
|
||||
tFromStart 1 1 "\"" 1,
|
||||
tFromEnd 4 4 "\"" 1
|
||||
]]
|
||||
|
||||
|
||||
return []
|
||||
runTests = $quickCheckAll
|
@@ -1,5 +1,5 @@
|
||||
{-
|
||||
Copyright 2012-2015 Vidar Holen
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
|
258
src/ShellCheck/Formatter/Diff.hs
Normal file
258
src/ShellCheck/Formatter/Diff.hs
Normal file
@@ -0,0 +1,258 @@
|
||||
{-
|
||||
Copyright 2019 Vidar 'koala_man' Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
|
||||
ShellCheck is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
ShellCheck is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
-}
|
||||
{-# LANGUAGE TemplateHaskell #-}
|
||||
module ShellCheck.Formatter.Diff (format, ShellCheck.Formatter.Diff.runTests) where
|
||||
|
||||
import ShellCheck.Interface
|
||||
import ShellCheck.Fixer
|
||||
import ShellCheck.Formatter.Format
|
||||
|
||||
import Control.Monad
|
||||
import Data.Algorithm.Diff
|
||||
import Data.Array
|
||||
import Data.IORef
|
||||
import Data.List
|
||||
import qualified Data.Monoid as Monoid
|
||||
import Data.Maybe
|
||||
import qualified Data.Map as M
|
||||
import GHC.Exts (sortWith)
|
||||
import System.IO
|
||||
import System.FilePath
|
||||
|
||||
import Test.QuickCheck
|
||||
|
||||
import Debug.Trace
|
||||
ltt x = trace (show x) x
|
||||
|
||||
format :: FormatterOptions -> IO Formatter
|
||||
format options = do
|
||||
foundIssues <- newIORef False
|
||||
reportedIssues <- newIORef False
|
||||
shouldColor <- shouldOutputColor (foColorOption options)
|
||||
let color = if shouldColor then colorize else nocolor
|
||||
return Formatter {
|
||||
header = return (),
|
||||
footer = checkFooter foundIssues reportedIssues color,
|
||||
onFailure = reportFailure color,
|
||||
onResult = reportResult foundIssues reportedIssues color
|
||||
}
|
||||
|
||||
|
||||
contextSize = 3
|
||||
red = 31
|
||||
green = 32
|
||||
yellow = 33
|
||||
cyan = 36
|
||||
bold = 1
|
||||
|
||||
nocolor n = id
|
||||
colorize n s = (ansi n) ++ s ++ (ansi 0)
|
||||
ansi n = "\x1B[" ++ show n ++ "m"
|
||||
|
||||
printErr :: ColorFunc -> String -> IO ()
|
||||
printErr color = hPutStrLn stderr . color bold . color red
|
||||
reportFailure color file msg = printErr color $ file ++ ": " ++ msg
|
||||
|
||||
checkFooter foundIssues reportedIssues color = do
|
||||
found <- readIORef foundIssues
|
||||
output <- readIORef reportedIssues
|
||||
when (found && not output) $
|
||||
printErr color "Issues were detected, but none were auto-fixable. Use another format to see them."
|
||||
|
||||
type ColorFunc = (Int -> String -> String)
|
||||
data LFStatus = LinefeedMissing | LinefeedOk
|
||||
data DiffDoc a = DiffDoc String LFStatus [DiffRegion a]
|
||||
data DiffRegion a = DiffRegion (Int, Int) (Int, Int) [Diff a]
|
||||
|
||||
reportResult :: (IORef Bool) -> (IORef Bool) -> ColorFunc -> CheckResult -> SystemInterface IO -> IO ()
|
||||
reportResult foundIssues reportedIssues color result sys = do
|
||||
let comments = crComments result
|
||||
unless (null comments) $ writeIORef foundIssues True
|
||||
let suggestedFixes = mapMaybe pcFix comments
|
||||
let fixmap = buildFixMap suggestedFixes
|
||||
mapM_ output $ M.toList fixmap
|
||||
where
|
||||
output (name, fix) = do
|
||||
file <- (siReadFile sys) name
|
||||
case file of
|
||||
Right contents -> do
|
||||
putStrLn $ formatDoc color $ makeDiff name contents fix
|
||||
writeIORef reportedIssues True
|
||||
Left msg -> reportFailure color name msg
|
||||
|
||||
hasTrailingLinefeed str =
|
||||
case str of
|
||||
[] -> True
|
||||
_ -> last str == '\n'
|
||||
|
||||
coversLastLine regions =
|
||||
case regions of
|
||||
[] -> False
|
||||
_ -> (fst $ last regions)
|
||||
|
||||
-- TODO: Factor this out into a unified diff library because we're doing a lot
|
||||
-- of the heavy lifting anyways.
|
||||
makeDiff :: String -> String -> Fix -> DiffDoc String
|
||||
makeDiff name contents fix = do
|
||||
let hunks = groupDiff $ computeDiff contents fix
|
||||
let lf = if coversLastLine hunks && not (hasTrailingLinefeed contents)
|
||||
then LinefeedMissing
|
||||
else LinefeedOk
|
||||
DiffDoc name lf $ findRegions hunks
|
||||
|
||||
computeDiff :: String -> Fix -> [Diff String]
|
||||
computeDiff contents fix =
|
||||
let old = lines contents
|
||||
array = listArray (1, fromIntegral $ (length old)) old
|
||||
new = applyFix fix array
|
||||
in getDiff old new
|
||||
|
||||
-- Group changes into hunks
|
||||
groupDiff :: [Diff a] -> [(Bool, [Diff a])]
|
||||
groupDiff = filter (\(_, l) -> not (null l)) . hunt []
|
||||
where
|
||||
-- Churn through 'Both's until we find a difference
|
||||
hunt current [] = [(False, reverse current)]
|
||||
hunt current (x@Both {}:rest) = hunt (x:current) rest
|
||||
hunt current list =
|
||||
let (context, previous) = splitAt contextSize current
|
||||
in (False, reverse previous) : gather context 0 list
|
||||
|
||||
-- Pick out differences until we find a run of Both's
|
||||
gather current n [] =
|
||||
let (extras, patch) = splitAt (max 0 $ n - contextSize) current
|
||||
in [(True, reverse patch), (False, reverse extras)]
|
||||
|
||||
gather current n list@(Both {}:_) | n == contextSize*2 =
|
||||
let (context, previous) = splitAt contextSize current
|
||||
in (True, reverse previous) : hunt context list
|
||||
|
||||
gather current n (x@Both {}:rest) = gather (x:current) (n+1) rest
|
||||
gather current n (x:rest) = gather (x:current) 0 rest
|
||||
|
||||
-- Get line numbers for hunks
|
||||
findRegions :: [(Bool, [Diff String])] -> [DiffRegion String]
|
||||
findRegions = find' 1 1
|
||||
where
|
||||
find' _ _ [] = []
|
||||
find' left right ((output, run):rest) =
|
||||
let (dl, dr) = countDelta run
|
||||
remainder = find' (left+dl) (right+dr) rest
|
||||
in
|
||||
if output
|
||||
then DiffRegion (left, dl) (right, dr) run : remainder
|
||||
else remainder
|
||||
|
||||
-- Get left/right line counts for a hunk
|
||||
countDelta :: [Diff a] -> (Int, Int)
|
||||
countDelta = count' 0 0
|
||||
where
|
||||
count' left right [] = (left, right)
|
||||
count' left right (x:rest) =
|
||||
case x of
|
||||
Both {} -> count' (left+1) (right+1) rest
|
||||
First {} -> count' (left+1) right rest
|
||||
Second {} -> count' left (right+1) rest
|
||||
|
||||
formatRegion :: ColorFunc -> LFStatus -> DiffRegion String -> String
|
||||
formatRegion color lf (DiffRegion left right diffs) =
|
||||
let header = color cyan ("@@ -" ++ (tup left) ++ " +" ++ (tup right) ++" @@")
|
||||
in
|
||||
unlines $ header : reverse (getStrings lf (reverse diffs))
|
||||
where
|
||||
noLF = "\\ No newline at end of file"
|
||||
|
||||
getStrings LinefeedOk list = map format list
|
||||
getStrings LinefeedMissing list@((Both _ _):_) = noLF : map format list
|
||||
getStrings LinefeedMissing list@((First _):_) = noLF : map format list
|
||||
getStrings LinefeedMissing (last:rest) = format last : getStrings LinefeedMissing rest
|
||||
|
||||
tup (a,b) = (show a) ++ "," ++ (show b)
|
||||
format (Both x _) = ' ':x
|
||||
format (First x) = color red $ '-':x
|
||||
format (Second x) = color green $ '+':x
|
||||
|
||||
splitLast [] = ([], [])
|
||||
splitLast x =
|
||||
let (last, rest) = splitAt 1 $ reverse x
|
||||
in (reverse rest, last)
|
||||
|
||||
formatDoc color (DiffDoc name lf regions) =
|
||||
let (most, last) = splitLast regions
|
||||
in
|
||||
(color bold $ "--- " ++ ("a" </> name)) ++ "\n" ++
|
||||
(color bold $ "+++ " ++ ("b" </> name)) ++ "\n" ++
|
||||
concatMap (formatRegion color LinefeedOk) most ++
|
||||
concatMap (formatRegion color lf) last
|
||||
|
||||
-- Create a Map from filename to Fix
|
||||
buildFixMap :: [Fix] -> M.Map String Fix
|
||||
buildFixMap fixes = perFile
|
||||
where
|
||||
splitFixes = concatMap splitFixByFile fixes
|
||||
perFile = groupByMap (posFile . repStartPos . head . fixReplacements) splitFixes
|
||||
|
||||
-- There are currently no multi-file fixes, but let's handle it anyways
|
||||
splitFixByFile :: Fix -> [Fix]
|
||||
splitFixByFile fix = map makeFix $ groupBy sameFile (fixReplacements fix)
|
||||
where
|
||||
sameFile rep1 rep2 = (posFile $ repStartPos rep1) == (posFile $ repStartPos rep2)
|
||||
makeFix reps = newFix { fixReplacements = reps }
|
||||
|
||||
groupByMap :: (Ord k, Monoid v) => (v -> k) -> [v] -> M.Map k v
|
||||
groupByMap f = M.fromListWith Monoid.mappend . map (\x -> (f x, x))
|
||||
|
||||
-- For building unit tests
|
||||
b n = Both n n
|
||||
l = First
|
||||
r = Second
|
||||
|
||||
prop_identifiesProperContext = groupDiff [b 1, b 2, b 3, b 4, l 5, b 6, b 7, b 8, b 9] ==
|
||||
[(False, [b 1]), -- Omitted
|
||||
(True, [b 2, b 3, b 4, l 5, b 6, b 7, b 8]), -- A change with three lines of context
|
||||
(False, [b 9])] -- Omitted
|
||||
|
||||
prop_includesContextFromStartIfNecessary = groupDiff [b 4, l 5, b 6, b 7, b 8, b 9] ==
|
||||
[ -- Nothing omitted
|
||||
(True, [b 4, l 5, b 6, b 7, b 8]), -- A change with three lines of context
|
||||
(False, [b 9])] -- Omitted
|
||||
|
||||
prop_includesContextUntilEndIfNecessary = groupDiff [b 4, l 5] ==
|
||||
[ -- Nothing omitted
|
||||
(True, [b 4, l 5])
|
||||
] -- Nothing Omitted
|
||||
|
||||
prop_splitsIntoMultipleHunks = groupDiff [l 1, b 1, b 2, b 3, b 4, b 5, b 6, b 7, r 8] ==
|
||||
[ -- Nothing omitted
|
||||
(True, [l 1, b 1, b 2, b 3]),
|
||||
(False, [b 4]),
|
||||
(True, [b 5, b 6, b 7, r 8])
|
||||
] -- Nothing Omitted
|
||||
|
||||
prop_splitsIntoMultipleHunksUnlessTouching = groupDiff [l 1, b 1, b 2, b 3, b 4, b 5, b 6, r 7] ==
|
||||
[
|
||||
(True, [l 1, b 1, b 2, b 3, b 4, b 5, b 6, r 7])
|
||||
]
|
||||
|
||||
prop_countDeltasWorks = countDelta [b 1, l 2, r 3, r 4, b 5] == (3,4)
|
||||
prop_countDeltasWorks2 = countDelta [] == (0,0)
|
||||
|
||||
return []
|
||||
runTests = $quickCheckAll
|
@@ -1,5 +1,5 @@
|
||||
{-
|
||||
Copyright 2012-2015 Vidar Holen
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
@@ -21,6 +21,13 @@ module ShellCheck.Formatter.Format where
|
||||
|
||||
import ShellCheck.Data
|
||||
import ShellCheck.Interface
|
||||
import ShellCheck.Fixer
|
||||
|
||||
import Control.Monad
|
||||
import Data.Array
|
||||
import Data.List
|
||||
import System.IO
|
||||
import System.Info
|
||||
|
||||
-- A formatter that carries along an arbitrary piece of data
|
||||
data Formatter = Formatter {
|
||||
@@ -50,21 +57,23 @@ severityText pc =
|
||||
makeNonVirtual comments contents =
|
||||
map fix comments
|
||||
where
|
||||
ls = lines contents
|
||||
fix c = c {
|
||||
pcStartPos = (pcStartPos c) {
|
||||
posColumn = realignColumn lineNo colNo c
|
||||
}
|
||||
, pcEndPos = (pcEndPos c) {
|
||||
posColumn = realignColumn endLineNo endColNo c
|
||||
}
|
||||
list = lines contents
|
||||
arr = listArray (1, length list) list
|
||||
untabbedFix f = newFix {
|
||||
fixReplacements = map (\r -> removeTabStops r arr) (fixReplacements f)
|
||||
}
|
||||
realignColumn lineNo colNo c =
|
||||
if lineNo c > 0 && lineNo c <= fromIntegral (length ls)
|
||||
then real (ls !! fromIntegral (lineNo c - 1)) 0 0 (colNo c)
|
||||
else colNo c
|
||||
real _ r v target | target <= v = r
|
||||
real [] r v _ = r -- should never happen
|
||||
real ('\t':rest) r v target =
|
||||
real rest (r+1) (v + 8 - (v `mod` 8)) target
|
||||
real (_:rest) r v target = real rest (r+1) (v+1) target
|
||||
fix c = (removeTabStops c arr) {
|
||||
pcFix = fmap untabbedFix (pcFix c)
|
||||
}
|
||||
|
||||
|
||||
shouldOutputColor :: ColorOption -> IO Bool
|
||||
shouldOutputColor colorOption = do
|
||||
term <- hIsTerminalDevice stdout
|
||||
let windows = "mingw" `isPrefixOf` os
|
||||
let isUsableTty = term && not windows
|
||||
let useColor = case colorOption of
|
||||
ColorAlways -> True
|
||||
ColorNever -> False
|
||||
ColorAuto -> isUsableTty
|
||||
return useColor
|
||||
|
@@ -1,5 +1,5 @@
|
||||
{-
|
||||
Copyright 2012-2015 Vidar Holen
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-
|
||||
Copyright 2012-2015 Vidar Holen
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
@@ -30,6 +30,7 @@ import GHC.Exts
|
||||
import System.IO
|
||||
import qualified Data.ByteString.Lazy.Char8 as BL
|
||||
|
||||
format :: IO Formatter
|
||||
format = do
|
||||
ref <- newIORef []
|
||||
return Formatter {
|
||||
@@ -45,11 +46,16 @@ instance ToJSON Replacement where
|
||||
end = repEndPos replacement
|
||||
str = repString replacement in
|
||||
object [
|
||||
"precedence" .= repPrecedence replacement,
|
||||
"insertionPoint" .=
|
||||
case repInsertionPoint replacement of
|
||||
InsertBefore -> "beforeStart" :: String
|
||||
InsertAfter -> "afterEnd",
|
||||
"line" .= posLine start,
|
||||
"endLine" .= posLine end,
|
||||
"column" .= posColumn start,
|
||||
"endLine" .= posLine end,
|
||||
"endColumn" .= posColumn end,
|
||||
"replaceWith" .= str
|
||||
"replacement" .= str
|
||||
]
|
||||
|
||||
instance ToJSON PositionedComment where
|
||||
@@ -91,8 +97,13 @@ instance ToJSON Fix where
|
||||
]
|
||||
|
||||
outputError file msg = hPutStrLn stderr $ file ++ ": " ++ msg
|
||||
collectResult ref result _ =
|
||||
modifyIORef ref (\x -> crComments result ++ x)
|
||||
|
||||
collectResult ref cr sys = mapM_ f groups
|
||||
where
|
||||
comments = crComments cr
|
||||
groups = groupWith sourceFile comments
|
||||
f :: [PositionedComment] -> IO ()
|
||||
f group = modifyIORef ref (\x -> comments ++ x)
|
||||
|
||||
finish ref = do
|
||||
list <- readIORef ref
|
||||
|
127
src/ShellCheck/Formatter/JSON1.hs
Normal file
127
src/ShellCheck/Formatter/JSON1.hs
Normal file
@@ -0,0 +1,127 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
|
||||
ShellCheck is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
ShellCheck is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
-}
|
||||
module ShellCheck.Formatter.JSON1 (format) where
|
||||
|
||||
import ShellCheck.Interface
|
||||
import ShellCheck.Formatter.Format
|
||||
|
||||
import Data.Aeson
|
||||
import Data.IORef
|
||||
import Data.Monoid
|
||||
import GHC.Exts
|
||||
import System.IO
|
||||
import qualified Data.ByteString.Lazy.Char8 as BL
|
||||
|
||||
format :: IO Formatter
|
||||
format = do
|
||||
ref <- newIORef []
|
||||
return Formatter {
|
||||
header = return (),
|
||||
onResult = collectResult ref,
|
||||
onFailure = outputError,
|
||||
footer = finish ref
|
||||
}
|
||||
|
||||
data Json1Output = Json1Output {
|
||||
comments :: [PositionedComment]
|
||||
}
|
||||
|
||||
instance ToJSON Json1Output where
|
||||
toJSON result = object [
|
||||
"comments" .= comments result
|
||||
]
|
||||
toEncoding result = pairs (
|
||||
"comments" .= comments result
|
||||
)
|
||||
|
||||
instance ToJSON Replacement where
|
||||
toJSON replacement =
|
||||
let start = repStartPos replacement
|
||||
end = repEndPos replacement
|
||||
str = repString replacement in
|
||||
object [
|
||||
"precedence" .= repPrecedence replacement,
|
||||
"insertionPoint" .=
|
||||
case repInsertionPoint replacement of
|
||||
InsertBefore -> "beforeStart" :: String
|
||||
InsertAfter -> "afterEnd",
|
||||
"line" .= posLine start,
|
||||
"column" .= posColumn start,
|
||||
"endLine" .= posLine end,
|
||||
"endColumn" .= posColumn end,
|
||||
"replacement" .= str
|
||||
]
|
||||
|
||||
instance ToJSON PositionedComment where
|
||||
toJSON comment =
|
||||
let start = pcStartPos comment
|
||||
end = pcEndPos comment
|
||||
c = pcComment comment in
|
||||
object [
|
||||
"file" .= posFile start,
|
||||
"line" .= posLine start,
|
||||
"endLine" .= posLine end,
|
||||
"column" .= posColumn start,
|
||||
"endColumn" .= posColumn end,
|
||||
"level" .= severityText comment,
|
||||
"code" .= cCode c,
|
||||
"message" .= cMessage c,
|
||||
"fix" .= pcFix comment
|
||||
]
|
||||
|
||||
toEncoding comment =
|
||||
let start = pcStartPos comment
|
||||
end = pcEndPos comment
|
||||
c = pcComment comment in
|
||||
pairs (
|
||||
"file" .= posFile start
|
||||
<> "line" .= posLine start
|
||||
<> "endLine" .= posLine end
|
||||
<> "column" .= posColumn start
|
||||
<> "endColumn" .= posColumn end
|
||||
<> "level" .= severityText comment
|
||||
<> "code" .= cCode c
|
||||
<> "message" .= cMessage c
|
||||
<> "fix" .= pcFix comment
|
||||
)
|
||||
|
||||
instance ToJSON Fix where
|
||||
toJSON fix = object [
|
||||
"replacements" .= fixReplacements fix
|
||||
]
|
||||
|
||||
outputError file msg = hPutStrLn stderr $ file ++ ": " ++ msg
|
||||
|
||||
collectResult ref cr sys = mapM_ f groups
|
||||
where
|
||||
comments = crComments cr
|
||||
groups = groupWith sourceFile comments
|
||||
f :: [PositionedComment] -> IO ()
|
||||
f group = do
|
||||
let filename = sourceFile (head group)
|
||||
result <- siReadFile sys filename
|
||||
let contents = either (const "") id result
|
||||
let comments' = makeNonVirtual comments contents
|
||||
modifyIORef ref (\x -> comments' ++ x)
|
||||
|
||||
finish ref = do
|
||||
list <- readIORef ref
|
||||
BL.putStrLn $ encode $ Json1Output { comments = list }
|
36
src/ShellCheck/Formatter/Quiet.hs
Normal file
36
src/ShellCheck/Formatter/Quiet.hs
Normal file
@@ -0,0 +1,36 @@
|
||||
{-
|
||||
Copyright 2019 Austin Voecks
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
|
||||
ShellCheck is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
ShellCheck is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
-}
|
||||
module ShellCheck.Formatter.Quiet (format) where
|
||||
|
||||
import ShellCheck.Interface
|
||||
import ShellCheck.Formatter.Format
|
||||
|
||||
import Control.Monad
|
||||
import Data.IORef
|
||||
import System.Exit
|
||||
|
||||
format :: FormatterOptions -> IO Formatter
|
||||
format options =
|
||||
return Formatter {
|
||||
header = return (),
|
||||
footer = return (),
|
||||
onFailure = \ _ _ -> exitFailure,
|
||||
onResult = \ result _ -> unless (null $ crComments result) exitFailure
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
{-
|
||||
Copyright 2012-2015 Vidar Holen
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
@@ -19,10 +19,14 @@
|
||||
-}
|
||||
module ShellCheck.Formatter.TTY (format) where
|
||||
|
||||
import ShellCheck.Fixer
|
||||
import ShellCheck.Interface
|
||||
import ShellCheck.Formatter.Format
|
||||
|
||||
import Control.Monad
|
||||
import Data.Array
|
||||
import Data.Foldable
|
||||
import Data.Ord
|
||||
import Data.IORef
|
||||
import Data.List
|
||||
import Data.Maybe
|
||||
@@ -34,6 +38,8 @@ wikiLink = "https://www.shellcheck.net/wiki/"
|
||||
|
||||
-- An arbitrary Ord thing to order warnings
|
||||
type Ranking = (Char, Severity, Integer)
|
||||
-- Ansi coloring function
|
||||
type ColorFunc = (String -> String -> String)
|
||||
|
||||
format :: FormatterOptions -> IO Formatter
|
||||
format options = do
|
||||
@@ -51,6 +57,7 @@ colorForLevel level =
|
||||
"warning" -> 33 -- yellow
|
||||
"info" -> 32 -- green
|
||||
"style" -> 32 -- green
|
||||
"verbose" -> 32 -- green
|
||||
"message" -> 1 -- bold
|
||||
"source" -> 0 -- none
|
||||
_ -> 0 -- none
|
||||
@@ -95,7 +102,7 @@ outputWiki errRef = do
|
||||
where
|
||||
showErr (_, code, msg) =
|
||||
putStrLn $ " " ++ wikiLink ++ "SC" ++ show code ++ " -- " ++ shorten msg
|
||||
limit = 40
|
||||
limit = 36
|
||||
shorten msg =
|
||||
if length msg < limit
|
||||
then msg
|
||||
@@ -116,73 +123,54 @@ outputForFile color sys comments = do
|
||||
let fileName = sourceFile (head comments)
|
||||
result <- (siReadFile sys) fileName
|
||||
let contents = either (const "") id result
|
||||
let fileLines = lines contents
|
||||
let lineCount = fromIntegral $ length fileLines
|
||||
let fileLinesList = lines contents
|
||||
let lineCount = length fileLinesList
|
||||
let fileLines = listArray (1, lineCount) fileLinesList
|
||||
let groups = groupWith lineNo comments
|
||||
mapM_ (\commentsForLine -> do
|
||||
let lineNum = lineNo (head commentsForLine)
|
||||
let lineNum = fromIntegral $ lineNo (head commentsForLine)
|
||||
let line = if lineNum < 1 || lineNum > lineCount
|
||||
then ""
|
||||
else fileLines !! fromIntegral (lineNum - 1)
|
||||
else fileLines ! fromIntegral lineNum
|
||||
putStrLn ""
|
||||
putStrLn $ color "message" $
|
||||
"In " ++ fileName ++" line " ++ show lineNum ++ ":"
|
||||
putStrLn (color "source" line)
|
||||
mapM_ (\c -> putStrLn (color (severityText c) $ cuteIndent c)) commentsForLine
|
||||
putStrLn ""
|
||||
-- FIXME: Enable when reasonably stable
|
||||
-- showFixedString color comments lineNum line
|
||||
showFixedString color commentsForLine (fromIntegral lineNum) fileLines
|
||||
) groups
|
||||
|
||||
hasApplicableFix lineNum comment = fromMaybe False $ do
|
||||
replacements <- fixReplacements <$> pcFix comment
|
||||
guard $ all (\c -> onSameLine (repStartPos c) && onSameLine (repEndPos c)) replacements
|
||||
return True
|
||||
-- Pick out only the lines necessary to show a fix in action
|
||||
sliceFile :: Fix -> Array Int String -> (Fix, Array Int String)
|
||||
sliceFile fix lines =
|
||||
(mapPositions adjust fix, sliceLines lines)
|
||||
where
|
||||
onSameLine pos = posLine pos == lineNum
|
||||
(minLine, maxLine) =
|
||||
foldl (\(mm, mx) pos -> ((min mm $ fromIntegral $ posLine pos), (max mx $ fromIntegral $ posLine pos)))
|
||||
(maxBound, minBound) $
|
||||
concatMap (\x -> [repStartPos x, repEndPos x]) $ fixReplacements fix
|
||||
sliceLines :: Array Int String -> Array Int String
|
||||
sliceLines = ixmap (1, maxLine - minLine + 1) (\x -> x + minLine - 1)
|
||||
adjust pos =
|
||||
pos {
|
||||
posLine = posLine pos - (fromIntegral minLine) + 1
|
||||
}
|
||||
|
||||
-- FIXME: Work correctly with multiple replacements
|
||||
showFixedString color comments lineNum line =
|
||||
case filter (hasApplicableFix lineNum) comments of
|
||||
(first:_) -> do
|
||||
showFixedString :: ColorFunc -> [PositionedComment] -> Int -> Array Int String -> IO ()
|
||||
showFixedString color comments lineNum fileLines =
|
||||
let line = fileLines ! fromIntegral lineNum in
|
||||
case mapMaybe pcFix comments of
|
||||
[] -> return ()
|
||||
fixes -> do
|
||||
-- Folding automatically removes overlap
|
||||
let mergedFix = fold fixes
|
||||
-- We show the complete, associated fixes, whether or not it includes this
|
||||
-- and/or other unrelated lines.
|
||||
let (excerptFix, excerpt) = sliceFile mergedFix fileLines
|
||||
-- in the spirit of error prone
|
||||
putStrLn $ color "message" "Did you mean: "
|
||||
putStrLn $ fixedString first line
|
||||
putStrLn ""
|
||||
_ -> return ()
|
||||
|
||||
-- need to do something smart about sorting by end index
|
||||
fixedString :: PositionedComment -> String -> String
|
||||
fixedString comment line =
|
||||
case (pcFix comment) of
|
||||
Nothing -> ""
|
||||
Just rs ->
|
||||
applyReplacement (fixReplacements rs) line 0
|
||||
where
|
||||
applyReplacement [] s _ = s
|
||||
applyReplacement (rep:xs) s offset =
|
||||
let replacementString = repString rep
|
||||
start = (posColumn . repStartPos) rep
|
||||
end = (posColumn . repEndPos) rep
|
||||
z = doReplace start end s replacementString
|
||||
len_r = (fromIntegral . length) replacementString in
|
||||
applyReplacement xs z (offset + (end - start) + len_r)
|
||||
|
||||
-- FIXME: Work correctly with tabs
|
||||
-- start and end comes from pos, which is 1 based
|
||||
-- doReplace 0 0 "1234" "A" -> "A1234" -- technically not valid
|
||||
-- doReplace 1 1 "1234" "A" -> "A1234"
|
||||
-- doReplace 1 2 "1234" "A" -> "A234"
|
||||
-- doReplace 3 3 "1234" "A" -> "12A34"
|
||||
-- doReplace 4 4 "1234" "A" -> "123A4"
|
||||
-- doReplace 5 5 "1234" "A" -> "1234A"
|
||||
doReplace start end o r =
|
||||
let si = fromIntegral (start-1)
|
||||
ei = fromIntegral (end-1)
|
||||
(x, xs) = splitAt si o
|
||||
(y, z) = splitAt (ei - si) xs
|
||||
in
|
||||
x ++ r ++ z
|
||||
putStrLn $ unlines $ applyFix excerptFix excerpt
|
||||
|
||||
cuteIndent :: PositionedComment -> String
|
||||
cuteIndent comment =
|
||||
@@ -198,14 +186,9 @@ cuteIndent comment =
|
||||
|
||||
code num = "SC" ++ show num
|
||||
|
||||
getColorFunc :: ColorOption -> IO ColorFunc
|
||||
getColorFunc colorOption = do
|
||||
term <- hIsTerminalDevice stdout
|
||||
let windows = "mingw" `isPrefixOf` os
|
||||
let isUsableTty = term && not windows
|
||||
let useColor = case colorOption of
|
||||
ColorAlways -> True
|
||||
ColorNever -> False
|
||||
ColorAuto -> isUsableTty
|
||||
useColor <- shouldOutputColor colorOption
|
||||
return $ if useColor then colorComment else const id
|
||||
where
|
||||
colorComment level comment =
|
||||
|
@@ -1,5 +1,5 @@
|
||||
{-
|
||||
Copyright 2012-2015 Vidar Holen
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
@@ -21,11 +21,11 @@
|
||||
module ShellCheck.Interface
|
||||
(
|
||||
SystemInterface(..)
|
||||
, CheckSpec(csFilename, csScript, csCheckSourced, csExcludedWarnings, csShellTypeOverride, csMinSeverity)
|
||||
, CheckSpec(csFilename, csScript, csCheckSourced, csIncludedWarnings, csExcludedWarnings, csShellTypeOverride, csMinSeverity, csIgnoreRC, csOptionalChecks)
|
||||
, CheckResult(crFilename, crComments)
|
||||
, ParseSpec(psFilename, psScript, psCheckSourced, psShellTypeOverride)
|
||||
, ParseSpec(psFilename, psScript, psCheckSourced, psIgnoreRC, psShellTypeOverride)
|
||||
, ParseResult(prComments, prTokenPositions, prRoot)
|
||||
, AnalysisSpec(asScript, asShellType, asExecutionMode, asCheckSourced, asTokenPositions)
|
||||
, AnalysisSpec(asScript, asShellType, asFallbackShell, asExecutionMode, asCheckSourced, asTokenPositions, asOptionalChecks)
|
||||
, AnalysisResult(arComments)
|
||||
, FormatterOptions(foColorOption, foWikiLinkCount)
|
||||
, Shell(Ksh, Sh, Bash, Dash)
|
||||
@@ -46,28 +46,43 @@ module ShellCheck.Interface
|
||||
, newPosition
|
||||
, newTokenComment
|
||||
, mockedSystemInterface
|
||||
, mockRcFile
|
||||
, newParseSpec
|
||||
, emptyCheckSpec
|
||||
, newPositionedComment
|
||||
, newComment
|
||||
, Fix(fixReplacements)
|
||||
, newFix
|
||||
, Replacement(repStartPos, repEndPos, repString)
|
||||
, InsertionPoint(InsertBefore, InsertAfter)
|
||||
, Replacement(repStartPos, repEndPos, repString, repPrecedence, repInsertionPoint)
|
||||
, newReplacement
|
||||
, CheckDescription(cdName, cdDescription, cdPositive, cdNegative)
|
||||
, newCheckDescription
|
||||
) where
|
||||
|
||||
import ShellCheck.AST
|
||||
|
||||
import Control.DeepSeq
|
||||
import Control.Monad.Identity
|
||||
import Data.List
|
||||
import Data.Monoid
|
||||
import Data.Ord
|
||||
import Data.Semigroup
|
||||
import GHC.Generics (Generic)
|
||||
import qualified Data.Map as Map
|
||||
|
||||
|
||||
newtype SystemInterface m = SystemInterface {
|
||||
data SystemInterface m = SystemInterface {
|
||||
-- Read a file by filename, or return an error
|
||||
siReadFile :: String -> m (Either ErrorMessage String)
|
||||
siReadFile :: String -> m (Either ErrorMessage String),
|
||||
-- Given:
|
||||
-- the current script,
|
||||
-- a list of source-path annotations in effect,
|
||||
-- and a sourced file,
|
||||
-- find the sourced file
|
||||
siFindSource :: String -> [String] -> String -> m FilePath,
|
||||
-- Get the configuration file (name, contents) for a filename
|
||||
siGetConfig :: String -> m (Maybe (FilePath, String))
|
||||
}
|
||||
|
||||
-- ShellCheck input and output
|
||||
@@ -75,9 +90,12 @@ data CheckSpec = CheckSpec {
|
||||
csFilename :: String,
|
||||
csScript :: String,
|
||||
csCheckSourced :: Bool,
|
||||
csIgnoreRC :: Bool,
|
||||
csExcludedWarnings :: [Integer],
|
||||
csIncludedWarnings :: Maybe [Integer],
|
||||
csShellTypeOverride :: Maybe Shell,
|
||||
csMinSeverity :: Severity
|
||||
csMinSeverity :: Severity,
|
||||
csOptionalChecks :: [String]
|
||||
} deriving (Show, Eq)
|
||||
|
||||
data CheckResult = CheckResult {
|
||||
@@ -96,9 +114,12 @@ emptyCheckSpec = CheckSpec {
|
||||
csFilename = "",
|
||||
csScript = "",
|
||||
csCheckSourced = False,
|
||||
csIgnoreRC = False,
|
||||
csExcludedWarnings = [],
|
||||
csIncludedWarnings = Nothing,
|
||||
csShellTypeOverride = Nothing,
|
||||
csMinSeverity = StyleC
|
||||
csMinSeverity = StyleC,
|
||||
csOptionalChecks = []
|
||||
}
|
||||
|
||||
newParseSpec :: ParseSpec
|
||||
@@ -106,6 +127,7 @@ newParseSpec = ParseSpec {
|
||||
psFilename = "",
|
||||
psScript = "",
|
||||
psCheckSourced = False,
|
||||
psIgnoreRC = False,
|
||||
psShellTypeOverride = Nothing
|
||||
}
|
||||
|
||||
@@ -114,6 +136,7 @@ data ParseSpec = ParseSpec {
|
||||
psFilename :: String,
|
||||
psScript :: String,
|
||||
psCheckSourced :: Bool,
|
||||
psIgnoreRC :: Bool,
|
||||
psShellTypeOverride :: Maybe Shell
|
||||
} deriving (Show, Eq)
|
||||
|
||||
@@ -134,16 +157,20 @@ newParseResult = ParseResult {
|
||||
data AnalysisSpec = AnalysisSpec {
|
||||
asScript :: Token,
|
||||
asShellType :: Maybe Shell,
|
||||
asFallbackShell :: Maybe Shell,
|
||||
asExecutionMode :: ExecutionMode,
|
||||
asCheckSourced :: Bool,
|
||||
asOptionalChecks :: [String],
|
||||
asTokenPositions :: Map.Map Id (Position, Position)
|
||||
}
|
||||
|
||||
newAnalysisSpec token = AnalysisSpec {
|
||||
asScript = token,
|
||||
asShellType = Nothing,
|
||||
asFallbackShell = Nothing,
|
||||
asExecutionMode = Executed,
|
||||
asCheckSourced = False,
|
||||
asOptionalChecks = [],
|
||||
asTokenPositions = Map.empty
|
||||
}
|
||||
|
||||
@@ -166,6 +193,19 @@ newFormatterOptions = FormatterOptions {
|
||||
foWikiLinkCount = 3
|
||||
}
|
||||
|
||||
data CheckDescription = CheckDescription {
|
||||
cdName :: String,
|
||||
cdDescription :: String,
|
||||
cdPositive :: String,
|
||||
cdNegative :: String
|
||||
}
|
||||
|
||||
newCheckDescription = CheckDescription {
|
||||
cdName = "",
|
||||
cdDescription = "",
|
||||
cdPositive = "",
|
||||
cdNegative = ""
|
||||
}
|
||||
|
||||
-- Supporting data types
|
||||
data Shell = Ksh | Sh | Bash | Dash deriving (Show, Eq)
|
||||
@@ -180,7 +220,7 @@ data Position = Position {
|
||||
posFile :: String, -- Filename
|
||||
posLine :: Integer, -- 1 based source line
|
||||
posColumn :: Integer -- 1 based source column, where tabs are 8
|
||||
} deriving (Show, Eq, Generic, NFData)
|
||||
} deriving (Show, Eq, Generic, NFData, Ord)
|
||||
|
||||
newPosition :: Position
|
||||
newPosition = Position {
|
||||
@@ -206,13 +246,22 @@ newComment = Comment {
|
||||
data Replacement = Replacement {
|
||||
repStartPos :: Position,
|
||||
repEndPos :: Position,
|
||||
repString :: String
|
||||
repString :: String,
|
||||
-- Order in which the replacements should happen: highest precedence first.
|
||||
repPrecedence :: Int,
|
||||
-- Whether to insert immediately before or immediately after the specified region.
|
||||
repInsertionPoint :: InsertionPoint
|
||||
} deriving (Show, Eq, Generic, NFData)
|
||||
|
||||
data InsertionPoint = InsertBefore | InsertAfter
|
||||
deriving (Show, Eq, Generic, NFData)
|
||||
|
||||
newReplacement = Replacement {
|
||||
repStartPos = newPosition,
|
||||
repEndPos = newPosition,
|
||||
repString = ""
|
||||
repString = "",
|
||||
repPrecedence = 1,
|
||||
repInsertionPoint = InsertAfter
|
||||
}
|
||||
|
||||
data Fix = Fix {
|
||||
@@ -259,11 +308,18 @@ data ColorOption =
|
||||
-- For testing
|
||||
mockedSystemInterface :: [(String, String)] -> SystemInterface Identity
|
||||
mockedSystemInterface files = SystemInterface {
|
||||
siReadFile = rf
|
||||
siReadFile = rf,
|
||||
siFindSource = fs,
|
||||
siGetConfig = const $ return Nothing
|
||||
}
|
||||
where
|
||||
rf file =
|
||||
case filter ((== file) . fst) files of
|
||||
[] -> return $ Left "File not included in mock."
|
||||
[(_, contents)] -> return $ Right contents
|
||||
rf file = return $
|
||||
case find ((== file) . fst) files of
|
||||
Nothing -> Left "File not included in mock."
|
||||
Just (_, contents) -> Right contents
|
||||
fs _ _ file = return file
|
||||
|
||||
mockRcFile rcfile mock = mock {
|
||||
siGetConfig = const . return $ Just (".shellcheckrc", rcfile)
|
||||
}
|
||||
|
||||
|
@@ -1,5 +1,5 @@
|
||||
{-
|
||||
Copyright 2012-2015 Vidar Holen
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
@@ -34,7 +34,7 @@ import Control.Monad.Identity
|
||||
import Control.Monad.Trans
|
||||
import Data.Char
|
||||
import Data.Functor
|
||||
import Data.List (isPrefixOf, isInfixOf, isSuffixOf, partition, sortBy, intercalate, nub)
|
||||
import Data.List (isPrefixOf, isInfixOf, isSuffixOf, partition, sortBy, intercalate, nub, find)
|
||||
import Data.Maybe
|
||||
import Data.Monoid
|
||||
import Debug.Trace
|
||||
@@ -69,7 +69,7 @@ variableChars = upper <|> lower <|> digit <|> oneOf "_"
|
||||
functionChars = variableChars <|> oneOf ":+?-./^@"
|
||||
-- Chars to allow in functions using the 'function' keyword
|
||||
extendedFunctionChars = functionChars <|> oneOf "[]*=!"
|
||||
specialVariable = oneOf "@*#?-$!"
|
||||
specialVariable = oneOf (concat specialVariables)
|
||||
paramSubSpecialChars = oneOf "/:+-=%"
|
||||
quotableChars = "|&;<>()\\ '\t\n\r\xA0" ++ doubleQuotableChars
|
||||
quotable = almostSpace <|> oneOf quotableChars
|
||||
@@ -113,6 +113,7 @@ allspacing = do
|
||||
allspacingOrFail = do
|
||||
s <- allspacing
|
||||
when (null s) $ fail "Expected whitespace"
|
||||
return s
|
||||
|
||||
readUnicodeQuote = do
|
||||
start <- startSpan
|
||||
@@ -137,7 +138,6 @@ almostSpace =
|
||||
return ' '
|
||||
|
||||
--------- Message/position annotation on top of user state
|
||||
data Note = Note Id Severity Code String deriving (Show, Eq)
|
||||
data ParseNote = ParseNote SourcePos SourcePos Severity Code String deriving (Show, Eq)
|
||||
data Context =
|
||||
ContextName SourcePos String
|
||||
@@ -165,10 +165,6 @@ initialUserState = UserState {
|
||||
}
|
||||
|
||||
codeForParseNote (ParseNote _ _ _ code _) = code
|
||||
noteToParseNote map (Note id severity code message) =
|
||||
ParseNote pos pos severity code message
|
||||
where
|
||||
pos = fromJust $ Map.lookup id map
|
||||
|
||||
getLastId = lastId <$> getState
|
||||
|
||||
@@ -190,12 +186,12 @@ getNextIdSpanningTokens startTok endTok = do
|
||||
|
||||
-- Get an ID starting from the first token of the list, and ending after the last
|
||||
getNextIdSpanningTokenList list =
|
||||
if null list
|
||||
then do
|
||||
case list of
|
||||
[] -> do
|
||||
pos <- getPosition
|
||||
getNextIdBetween pos pos
|
||||
else
|
||||
getNextIdSpanningTokens (head list) (last list)
|
||||
(h:_) ->
|
||||
getNextIdSpanningTokens h (last list)
|
||||
|
||||
-- Get the span covered by an id
|
||||
getSpanForId :: Monad m => Id -> SCParser m (SourcePos, SourcePos)
|
||||
@@ -250,6 +246,10 @@ addParseNote n = do
|
||||
parseNotes = n : parseNotes state
|
||||
}
|
||||
|
||||
ignoreProblemsOf p = do
|
||||
systemState <- lift . lift $ Ms.get
|
||||
p <* (lift . lift . Ms.put $ systemState)
|
||||
|
||||
shouldIgnoreCode code = do
|
||||
context <- getCurrentContexts
|
||||
checkSourced <- Mr.asks checkSourced
|
||||
@@ -263,6 +263,15 @@ shouldIgnoreCode code = do
|
||||
disabling' (DisableComment n) = code == n
|
||||
disabling' _ = False
|
||||
|
||||
getCurrentAnnotations includeSource =
|
||||
concatMap get . takeWhile (not . isBoundary) <$> getCurrentContexts
|
||||
where
|
||||
get (ContextAnnotation list) = list
|
||||
get _ = []
|
||||
isBoundary (ContextSource _) = not includeSource
|
||||
isBoundary _ = False
|
||||
|
||||
|
||||
shouldFollow file = do
|
||||
context <- getCurrentContexts
|
||||
if any isThisFile context
|
||||
@@ -306,6 +315,8 @@ initialSystemState = SystemState {
|
||||
data Environment m = Environment {
|
||||
systemInterface :: SystemInterface m,
|
||||
checkSourced :: Bool,
|
||||
ignoreRC :: Bool,
|
||||
currentFilename :: String,
|
||||
shellTypeOverride :: Maybe Shell
|
||||
}
|
||||
|
||||
@@ -314,16 +325,15 @@ parseProblem level code msg = do
|
||||
parseProblemAt pos level code msg
|
||||
|
||||
setCurrentContexts c = Ms.modify (\state -> state { contextStack = c })
|
||||
getCurrentContexts = contextStack <$> Ms.get
|
||||
getCurrentContexts = Ms.gets contextStack
|
||||
|
||||
popContext = do
|
||||
v <- getCurrentContexts
|
||||
if not $ null v
|
||||
then do
|
||||
let (a:r) = v
|
||||
case v of
|
||||
(a:r) -> do
|
||||
setCurrentContexts r
|
||||
return $ Just a
|
||||
else
|
||||
[] ->
|
||||
return Nothing
|
||||
|
||||
pushContext c = do
|
||||
@@ -430,6 +440,7 @@ readConditionContents single =
|
||||
readCondContents `attempting` lookAhead (do
|
||||
pos <- getPosition
|
||||
s <- readVariableName
|
||||
spacing1
|
||||
when (s `elem` commonCommands) $
|
||||
parseProblemAt pos WarningC 1014 "Use 'if cmd; then ..' to check exit code, or 'if [[ $(cmd) == .. ]]' to check output.")
|
||||
|
||||
@@ -575,9 +586,9 @@ readConditionContents single =
|
||||
return $ TC_Nullary id typ x
|
||||
)
|
||||
|
||||
checkTrailingOp x = fromMaybe (return ()) $ do
|
||||
checkTrailingOp x = sequence_ $ do
|
||||
(T_Literal id str) <- getTrailingUnquotedLiteral x
|
||||
trailingOp <- listToMaybe (filter (`isSuffixOf` str) binaryTestOps)
|
||||
trailingOp <- find (`isSuffixOf` str) binaryTestOps
|
||||
return $ parseProblemAtId id ErrorC 1108 $
|
||||
"You need a space before and after the " ++ trailingOp ++ " ."
|
||||
|
||||
@@ -903,6 +914,8 @@ prop_readCondition20 = isOk readCondition "[[ echo_rc -eq 0 ]]"
|
||||
prop_readCondition21 = isOk readCondition "[[ $1 =~ ^(a\\ b)$ ]]"
|
||||
prop_readCondition22 = isOk readCondition "[[ $1 =~ \\.a\\.(\\.b\\.)\\.c\\. ]]"
|
||||
prop_readCondition23 = isOk readCondition "[[ -v arr[$var] ]]"
|
||||
prop_readCondition24 = isWarning readCondition "[[ 1 == 2 ]]]"
|
||||
prop_readCondition25 = isOk readCondition "[[ lex.yy.c -ot program.l ]]"
|
||||
readCondition = called "test expression" $ do
|
||||
opos <- getPosition
|
||||
start <- startSpan
|
||||
@@ -931,6 +944,11 @@ readCondition = called "test expression" $ do
|
||||
id <- endSpan start
|
||||
when (open == "[[" && close /= "]]") $ parseProblemAt cpos ErrorC 1033 "Did you mean ]] ?"
|
||||
when (open == "[" && close /= "]" ) $ parseProblemAt opos ErrorC 1034 "Did you mean [[ ?"
|
||||
optional $ lookAhead $ do
|
||||
pos <- getPosition
|
||||
notFollowedBy2 readCmdWord <|>
|
||||
parseProblemAt pos ErrorC 1136
|
||||
("Unexpected characters after terminating " ++ close ++ ". Missing semicolon/linefeed?")
|
||||
spacing
|
||||
many readCmdWord -- Read and throw away remainders to get then/do warnings. Fixme?
|
||||
return $ T_Condition id typ condition
|
||||
@@ -949,9 +967,12 @@ prop_readAnnotation6 = isOk readAnnotation "# shellcheck disable=SC1234 # shellc
|
||||
readAnnotation = called "shellcheck directive" $ do
|
||||
try readAnnotationPrefix
|
||||
many1 linewhitespace
|
||||
readAnnotationWithoutPrefix
|
||||
|
||||
readAnnotationWithoutPrefix = do
|
||||
values <- many1 readKey
|
||||
optional readAnyComment
|
||||
void linefeed <|> do
|
||||
void linefeed <|> eof <|> do
|
||||
parseNote ErrorC 1125 "Invalid key=value pair? Ignoring the rest of this directive starting here."
|
||||
many (noneOf "\n")
|
||||
void linefeed <|> eof
|
||||
@@ -960,7 +981,7 @@ readAnnotation = called "shellcheck directive" $ do
|
||||
where
|
||||
readKey = do
|
||||
keyPos <- getPosition
|
||||
key <- many1 letter
|
||||
key <- many1 (letter <|> char '-')
|
||||
char '=' <|> fail "Expected '=' after directive key"
|
||||
annotations <- case key of
|
||||
"disable" -> readCode `sepBy` char ','
|
||||
@@ -970,10 +991,18 @@ readAnnotation = called "shellcheck directive" $ do
|
||||
int <- many1 digit
|
||||
return $ DisableComment (read int)
|
||||
|
||||
"enable" -> readName `sepBy` char ','
|
||||
where
|
||||
readName = EnableComment <$> many1 (letter <|> char '-')
|
||||
|
||||
"source" -> do
|
||||
filename <- many1 $ noneOf " \n"
|
||||
return [SourceOverride filename]
|
||||
|
||||
"source-path" -> do
|
||||
dirname <- many1 $ noneOf " \n"
|
||||
return [SourcePath dirname]
|
||||
|
||||
"shell" -> do
|
||||
pos <- getPosition
|
||||
shell <- many1 $ noneOf " \n"
|
||||
@@ -1014,14 +1043,16 @@ prop_readNormalWord9 = isOk readSubshell "(foo\\ ;\nbar)"
|
||||
prop_readNormalWord10 = isWarning readNormalWord "\x201Chello\x201D"
|
||||
prop_readNormalWord11 = isWarning readNormalWord "\x2018hello\x2019"
|
||||
prop_readNormalWord12 = isWarning readNormalWord "hello\x2018"
|
||||
readNormalWord = readNormalishWord ""
|
||||
readNormalWord = readNormalishWord "" ["do", "done", "then", "fi", "esac"]
|
||||
|
||||
readNormalishWord end = do
|
||||
readPatternWord = readNormalishWord "" ["esac"]
|
||||
|
||||
readNormalishWord end terms = do
|
||||
start <- startSpan
|
||||
pos <- getPosition
|
||||
x <- many1 (readNormalWordPart end)
|
||||
id <- endSpan start
|
||||
checkPossibleTermination pos x
|
||||
checkPossibleTermination pos x terms
|
||||
return $ T_NormalWord id x
|
||||
|
||||
readIndexSpan = do
|
||||
@@ -1041,10 +1072,10 @@ readIndexSpan = do
|
||||
id <- endSpan start
|
||||
return $ T_Literal id str
|
||||
|
||||
checkPossibleTermination pos [T_Literal _ x] =
|
||||
when (x `elem` ["do", "done", "then", "fi", "esac"]) $
|
||||
checkPossibleTermination pos [T_Literal _ x] terminators =
|
||||
when (x `elem` terminators) $
|
||||
parseProblemAt pos WarningC 1010 $ "Use semicolon or linefeed before '" ++ x ++ "' (or quote to make it literal)."
|
||||
checkPossibleTermination _ _ = return ()
|
||||
checkPossibleTermination _ _ _ = return ()
|
||||
|
||||
readNormalWordPart end = do
|
||||
notFollowedBy2 $ oneOf end
|
||||
@@ -1200,7 +1231,7 @@ readBackTicked quoted = called "backtick expansion" $ do
|
||||
suggestForgotClosingQuote startPos endPos "backtick expansion"
|
||||
|
||||
-- Result positions may be off due to escapes
|
||||
result <- subParse subStart subParser (unEscape subString)
|
||||
result <- subParse subStart (tryWithErrors subParser <|> return []) (unEscape subString)
|
||||
return $ T_Backticked id result
|
||||
where
|
||||
unEscape [] = []
|
||||
@@ -1506,10 +1537,10 @@ ensureDollar =
|
||||
|
||||
readNormalDollar = do
|
||||
ensureDollar
|
||||
readDollarExp <|> readDollarDoubleQuote <|> readDollarSingleQuote <|> readDollarLonely
|
||||
readDollarExp <|> readDollarDoubleQuote <|> readDollarSingleQuote <|> readDollarLonely False
|
||||
readDoubleQuotedDollar = do
|
||||
ensureDollar
|
||||
readDollarExp <|> readDollarLonely
|
||||
readDollarExp <|> readDollarLonely True
|
||||
|
||||
|
||||
prop_readDollarExpression1 = isOk readDollarExpression "$(((1) && 3))"
|
||||
@@ -1523,7 +1554,7 @@ readDollarExpression = do
|
||||
readDollarExp = arithmetic <|> readDollarExpansion <|> readDollarBracket <|> readDollarBraceCommandExpansion <|> readDollarBraced <|> readDollarVariable
|
||||
where
|
||||
arithmetic = readAmbiguous "$((" readDollarArithmetic readDollarExpansion (\pos ->
|
||||
parseNoteAt pos WarningC 1102 "Shells disambiguate $(( differently or not at all. For $(command substition), add space after $( . For $((arithmetics)), fix parsing errors.")
|
||||
parseNoteAt pos ErrorC 1102 "Shells disambiguate $(( differently or not at all. For $(command substition), add space after $( . For $((arithmetics)), fix parsing errors.")
|
||||
|
||||
prop_readDollarSingleQuote = isOk readDollarSingleQuote "$'foo\\\'lol'"
|
||||
readDollarSingleQuote = called "$'..' expression" $ do
|
||||
@@ -1611,7 +1642,7 @@ readDollarBraced = called "parameter expansion" $ do
|
||||
word <- readDollarBracedWord
|
||||
char '}'
|
||||
id <- endSpan start
|
||||
return $ T_DollarBraced id word
|
||||
return $ T_DollarBraced id True word
|
||||
|
||||
prop_readDollarExpansion1= isOk readDollarExpansion "$(echo foo; ls\n)"
|
||||
prop_readDollarExpansion2= isOk readDollarExpansion "$( )"
|
||||
@@ -1638,7 +1669,7 @@ readDollarVariable = do
|
||||
let singleCharred p = do
|
||||
value <- wrapString ((:[]) <$> p)
|
||||
id <- endSpan start
|
||||
return $ (T_DollarBraced id value)
|
||||
return $ (T_DollarBraced id False value)
|
||||
|
||||
let positional = do
|
||||
value <- singleCharred digit
|
||||
@@ -1651,7 +1682,7 @@ readDollarVariable = do
|
||||
let regular = do
|
||||
value <- wrapString readVariableName
|
||||
id <- endSpan start
|
||||
return (T_DollarBraced id value) `attempting` do
|
||||
return (T_DollarBraced id False value) `attempting` do
|
||||
lookAhead $ char '['
|
||||
parseNoteAt pos ErrorC 1087 "Use braces when expanding arrays, e.g. ${array[idx]} (or ${var}[.. to quiet)."
|
||||
|
||||
@@ -1671,12 +1702,32 @@ readVariableName = do
|
||||
rest <- many variableChars
|
||||
return (f:rest)
|
||||
|
||||
readDollarLonely = do
|
||||
|
||||
prop_readDollarLonely1 = isWarning readNormalWord "\"$\"var"
|
||||
prop_readDollarLonely2 = isWarning readNormalWord "\"$\"\"var\""
|
||||
prop_readDollarLonely3 = isOk readNormalWord "\"$\"$var"
|
||||
prop_readDollarLonely4 = isOk readNormalWord "\"$\"*"
|
||||
prop_readDollarLonely5 = isOk readNormalWord "$\"str\""
|
||||
readDollarLonely quoted = do
|
||||
start <- startSpan
|
||||
char '$'
|
||||
id <- endSpan start
|
||||
n <- lookAhead (anyChar <|> (eof >> return '_'))
|
||||
when quoted $ do
|
||||
isHack <- quoteForEscape
|
||||
when isHack $
|
||||
parseProblemAtId id StyleC 1135
|
||||
"Prefer escape over ending quote to make $ literal. Instead of \"It costs $\"5, use \"It costs \\$5\"."
|
||||
return $ T_Literal id "$"
|
||||
where
|
||||
quoteForEscape = option False $ try . lookAhead $ do
|
||||
char '"'
|
||||
-- Check for "foo $""bar"
|
||||
optional $ char '"'
|
||||
c <- anyVar
|
||||
-- Don't trigger on [[ x == "$"* ]] or "$"$pattern
|
||||
return $ c `notElem` "*$"
|
||||
anyVar = variableStart <|> digit <|> specialVariable
|
||||
|
||||
|
||||
prop_readHereDoc = isOk readScript "cat << foo\nlol\ncow\nfoo"
|
||||
prop_readHereDoc2 = isNotOk readScript "cat <<- EOF\n cow\n EOF"
|
||||
@@ -1695,6 +1746,7 @@ prop_readHereDoc14= isWarning readScript "cat << foo\nbar\nfoo \n"
|
||||
prop_readHereDoc15= isWarning readScript "cat <<foo\nbar\nfoo bar\nfoo"
|
||||
prop_readHereDoc16= isOk readScript "cat <<- ' foo'\nbar\n foo\n"
|
||||
prop_readHereDoc17= isWarning readScript "cat <<- ' foo'\nbar\n foo\n foo\n"
|
||||
prop_readHereDoc18= isOk readScript "cat <<'\"foo'\nbar\n\"foo\n"
|
||||
prop_readHereDoc20= isWarning readScript "cat << foo\n foo\n()\nfoo\n"
|
||||
prop_readHereDoc21= isOk readScript "# shellcheck disable=SC1039\ncat << foo\n foo\n()\nfoo\n"
|
||||
readHereDoc = called "here document" $ do
|
||||
@@ -1715,14 +1767,17 @@ readHereDoc = called "here document" $ do
|
||||
addPendingHereDoc doc
|
||||
return doc
|
||||
where
|
||||
quotes = "\"'\\"
|
||||
unquote :: String -> (Quoted, String)
|
||||
unquote "" = (Unquoted, "")
|
||||
unquote [c] = (Unquoted, [c])
|
||||
unquote s@(cl:tl) =
|
||||
case reverse tl of
|
||||
(cr:tr) | cr == cl && cl `elem` "\"'" -> (Quoted, reverse tr)
|
||||
_ -> (if '\\' `elem` s then (Quoted, filter ((/=) '\\') s) else (Unquoted, s))
|
||||
-- Fun fact: bash considers << foo"" quoted, but not << <("foo").
|
||||
-- Instead of replicating this, just read a token and strip quotes.
|
||||
readToken = do
|
||||
str <- readStringForParser readNormalWord
|
||||
return (if any (`elem` quotes) str then Quoted else Unquoted,
|
||||
filter (not . (`elem` quotes)) str)
|
||||
|
||||
return $ unquote str
|
||||
|
||||
readPendingHereDocs = do
|
||||
docs <- popPendingHereDocs
|
||||
@@ -1771,7 +1826,7 @@ readPendingHereDocs = do
|
||||
let thereIsNoTrailer = null trailingSpace && null trailer
|
||||
let leaderIsOk = null leadingSpace
|
||||
|| dashed == Dashed && leadingSpacesAreTabs
|
||||
let trailerStart = if null trailer then '\0' else head trailer
|
||||
let trailerStart = case trailer of [] -> '\0'; (h:_) -> h
|
||||
let hasTrailingSpace = not $ null trailingSpace
|
||||
let hasTrailer = not $ null trailer
|
||||
let ppt = parseProblemAt trailerPos ErrorC
|
||||
@@ -1916,8 +1971,9 @@ readNewlineList =
|
||||
where
|
||||
checkBadBreak = optional $ do
|
||||
pos <- getPosition
|
||||
try $ lookAhead (oneOf "|&") -- |, || or &&
|
||||
parseProblemAt pos ErrorC 1133 "Unexpected start of line. If breaking lines, |/||/&& should be at the end of the previous one."
|
||||
try $ lookAhead (oneOf "|&") -- See if the next thing could be |, || or &&
|
||||
parseProblemAt pos ErrorC 1133
|
||||
"Unexpected start of line. If breaking lines, |/||/&& should be at the end of the previous one."
|
||||
readLineBreak = optional readNewlineList
|
||||
|
||||
prop_readSeparator1 = isWarning readScript "a &; b"
|
||||
@@ -1996,7 +2052,11 @@ readSimpleCommand = called "simple command" $ do
|
||||
|
||||
Just cmd -> do
|
||||
validateCommand cmd
|
||||
suffix <- option [] $ getParser readCmdSuffix cmd [
|
||||
-- We have to ignore possible parsing problems from the lookAhead parser
|
||||
firstArgument <- ignoreProblemsOf . optionMaybe . try . lookAhead $ readCmdWord
|
||||
suffix <- option [] $ getParser readCmdSuffix
|
||||
-- If `export` or other modifier commands are called with `builtin` we have to look at the first argument
|
||||
(if isCommand ["builtin"] cmd then fromMaybe cmd firstArgument else cmd) [
|
||||
(["declare", "export", "local", "readonly", "typeset"], readModifierSuffix),
|
||||
(["time"], readTimeSuffix),
|
||||
(["let"], readLetSuffix),
|
||||
@@ -2055,7 +2115,8 @@ readSimpleCommand = called "simple command" $ do
|
||||
|
||||
|
||||
readSource :: Monad m => Token -> SCParser m Token
|
||||
readSource t@(T_Redirecting _ _ (T_SimpleCommand cmdId _ (cmd:file:_))) = do
|
||||
readSource t@(T_Redirecting _ _ (T_SimpleCommand cmdId _ (cmd:file':rest'))) = do
|
||||
let file = getFile file' rest'
|
||||
override <- getSourceOverride
|
||||
let literalFile = do
|
||||
name <- override `mplus` getLiteralString file
|
||||
@@ -2071,15 +2132,21 @@ readSource t@(T_Redirecting _ _ (T_SimpleCommand cmdId _ (cmd:file:_))) = do
|
||||
proceed <- shouldFollow filename
|
||||
if not proceed
|
||||
then do
|
||||
-- FIXME: This actually gets squashed without -a
|
||||
parseNoteAtId (getId file) InfoC 1093
|
||||
"This file appears to be recursively sourced. Ignoring."
|
||||
return t
|
||||
else do
|
||||
sys <- Mr.asks systemInterface
|
||||
input <-
|
||||
(input, resolvedFile) <-
|
||||
if filename == "/dev/null" -- always allow /dev/null
|
||||
then return (Right "")
|
||||
else system $ siReadFile sys filename
|
||||
then return (Right "", filename)
|
||||
else do
|
||||
currentScript <- Mr.asks currentFilename
|
||||
paths <- mapMaybe getSourcePath <$> getCurrentAnnotations True
|
||||
resolved <- system $ siFindSource sys currentScript paths filename
|
||||
contents <- system $ siReadFile sys resolved
|
||||
return (contents, resolved)
|
||||
case input of
|
||||
Left err -> do
|
||||
parseNoteAtId (getId file) InfoC 1091 $
|
||||
@@ -2090,7 +2157,7 @@ readSource t@(T_Redirecting _ _ (T_SimpleCommand cmdId _ (cmd:file:_))) = do
|
||||
id2 <- getNewIdFor cmdId
|
||||
|
||||
let included = do
|
||||
src <- subRead filename script
|
||||
src <- subRead resolvedFile script
|
||||
return $ T_SourceCommand id1 t (T_Include id2 src)
|
||||
|
||||
let failed = do
|
||||
@@ -2100,10 +2167,22 @@ readSource t@(T_Redirecting _ _ (T_SimpleCommand cmdId _ (cmd:file:_))) = do
|
||||
|
||||
included <|> failed
|
||||
where
|
||||
getFile :: Token -> [Token] -> Token
|
||||
getFile file (next:rest) =
|
||||
case getLiteralString file of
|
||||
Just "--" -> next
|
||||
x -> file
|
||||
getFile file _ = file
|
||||
|
||||
getSourcePath t =
|
||||
case t of
|
||||
SourcePath x -> Just x
|
||||
_ -> Nothing
|
||||
|
||||
subRead name script =
|
||||
withContext (ContextSource name) $
|
||||
inSeparateContext $
|
||||
subParse (initialPos name) readScript script
|
||||
subParse (initialPos name) (readScriptFile True) script
|
||||
readSource t = return t
|
||||
|
||||
|
||||
@@ -2225,6 +2304,7 @@ prop_readIfClause2 = isWarning readIfClause "if false; then; echo oo; fi"
|
||||
prop_readIfClause3 = isWarning readIfClause "if false; then true; else; echo lol; fi"
|
||||
prop_readIfClause4 = isWarning readIfClause "if false; then true; else if true; then echo lol; fi; fi"
|
||||
prop_readIfClause5 = isOk readIfClause "if false; then true; else\nif true; then echo lol; fi; fi"
|
||||
prop_readIfClause6 = isWarning readIfClause "if true\nthen\nDo the thing\nfi"
|
||||
readIfClause = called "if expression" $ do
|
||||
start <- startSpan
|
||||
pos <- getPosition
|
||||
@@ -2330,6 +2410,17 @@ readBraceGroup = called "brace group" $ do
|
||||
id <- endSpan start
|
||||
return $ T_BraceGroup id list
|
||||
|
||||
prop_readBatsTest = isOk readBatsTest "@test 'can parse' {\n true\n}"
|
||||
readBatsTest = called "bats @test" $ do
|
||||
start <- startSpan
|
||||
try $ string "@test"
|
||||
spacing
|
||||
name <- readNormalWord
|
||||
spacing
|
||||
test <- readBraceGroup
|
||||
id <- endSpan start
|
||||
return $ T_BatsTest id name test
|
||||
|
||||
prop_readWhileClause = isOk readWhileClause "while [[ -e foo ]]; do sleep 1; done"
|
||||
readWhileClause = called "while loop" $ do
|
||||
start <- startSpan
|
||||
@@ -2373,6 +2464,7 @@ readDoGroup kwId = do
|
||||
|
||||
|
||||
prop_readForClause = isOk readForClause "for f in *; do rm \"$f\"; done"
|
||||
prop_readForClause1 = isOk readForClause "for f in *; { rm \"$f\"; }"
|
||||
prop_readForClause3 = isOk readForClause "for f; do foo; done"
|
||||
prop_readForClause4 = isOk readForClause "for((i=0; i<10; i++)); do echo $i; done"
|
||||
prop_readForClause5 = isOk readForClause "for ((i=0;i<10 && n>x;i++,--n))\ndo \necho $i\ndone"
|
||||
@@ -2412,7 +2504,7 @@ readForClause = called "for loop" $ do
|
||||
"Don't use $ on the iterator name in for loops."
|
||||
name <- readVariableName `thenSkip` allspacing
|
||||
values <- readInClause <|> (optional readSequentialSep >> return [])
|
||||
group <- readDoGroup id
|
||||
group <- readBraced <|> readDoGroup id
|
||||
return $ T_ForIn id name values group
|
||||
|
||||
prop_readSelectClause1 = isOk readSelectClause "select foo in *; do echo $foo; done"
|
||||
@@ -2450,6 +2542,7 @@ prop_readCaseClause2 = isOk readCaseClause "case foo\n in * ) echo bar;; esac"
|
||||
prop_readCaseClause3 = isOk readCaseClause "case foo\n in * ) echo bar & ;; esac"
|
||||
prop_readCaseClause4 = isOk readCaseClause "case foo\n in *) echo bar ;& bar) foo; esac"
|
||||
prop_readCaseClause5 = isOk readCaseClause "case foo\n in *) echo bar;;& foo) baz;; esac"
|
||||
prop_readCaseClause6 = isOk readCaseClause "case foo\n in if) :;; done) :;; esac"
|
||||
readCaseClause = called "case expression" $ do
|
||||
start <- startSpan
|
||||
g_Case
|
||||
@@ -2573,14 +2666,14 @@ readCoProc = called "coproc" $ do
|
||||
return $ T_CoProcBody id body
|
||||
|
||||
|
||||
readPattern = (readNormalWord `thenSkip` spacing) `sepBy1` (char '|' `thenSkip` spacing)
|
||||
readPattern = (readPatternWord `thenSkip` spacing) `sepBy1` (char '|' `thenSkip` spacing)
|
||||
|
||||
prop_readCompoundCommand = isOk readCompoundCommand "{ echo foo; }>/dev/null"
|
||||
readCompoundCommand = do
|
||||
cmd <- choice [
|
||||
readBraceGroup,
|
||||
readAmbiguous "((" readArithmeticExpression readSubshell (\pos ->
|
||||
parseNoteAt pos WarningC 1105 "Shells disambiguate (( differently or not at all. For subshell, add spaces around ( . For ((, fix parsing errors."),
|
||||
parseNoteAt pos ErrorC 1105 "Shells disambiguate (( differently or not at all. For subshell, add spaces around ( . For ((, fix parsing errors."),
|
||||
readSubshell,
|
||||
readCondition,
|
||||
readWhileClause,
|
||||
@@ -2589,6 +2682,7 @@ readCompoundCommand = do
|
||||
readForClause,
|
||||
readSelectClause,
|
||||
readCaseClause,
|
||||
readBatsTest,
|
||||
readFunctionDefinition
|
||||
]
|
||||
spacing
|
||||
@@ -2695,7 +2789,7 @@ readAssignmentWordExt lenient = try $ do
|
||||
variable <- readVariableName
|
||||
when lenient $
|
||||
optional (readNormalDollar >> parseNoteAt pos ErrorC
|
||||
1067 "For indirection, use (associative) arrays or 'read \"var$n\" <<< \"value\"'")
|
||||
1067 "For indirection, use arrays, declare \"var$n=value\", or (for sh) read/eval.")
|
||||
indices <- many readArrayIndex
|
||||
hasLeftSpace <- fmap (not . null) spacing
|
||||
pos <- getPosition
|
||||
@@ -2714,9 +2808,10 @@ readAssignmentWordExt lenient = try $ do
|
||||
when (hasLeftSpace || hasRightSpace) $
|
||||
parseNoteAt pos ErrorC 1068 $
|
||||
"Don't put spaces around the "
|
||||
++ if op == Append
|
||||
then "+= when appending."
|
||||
else "= in assignments."
|
||||
++ (if op == Append
|
||||
then "+= when appending"
|
||||
else "= in assignments")
|
||||
++ " (or quote to make it literal)."
|
||||
value <- readArray <|> readNormalWord
|
||||
spacing
|
||||
return $ T_Assignment id op variable indices value
|
||||
@@ -2734,10 +2829,11 @@ readAssignmentWordExt lenient = try $ do
|
||||
|
||||
string "=" >> return Assign
|
||||
]
|
||||
readEmptyLiteral = do
|
||||
start <- startSpan
|
||||
id <- endSpan start
|
||||
return $ T_Literal id ""
|
||||
|
||||
readEmptyLiteral = do
|
||||
start <- startSpan
|
||||
id <- endSpan start
|
||||
return $ T_Literal id ""
|
||||
|
||||
readArrayIndex = do
|
||||
start <- startSpan
|
||||
@@ -2795,6 +2891,7 @@ redirToken c t = try $ do
|
||||
|
||||
tryWordToken s t = tryParseWordToken s t `thenSkip` spacing
|
||||
tryParseWordToken keyword t = try $ do
|
||||
pos <- getPosition
|
||||
start <- startSpan
|
||||
str <- anycaseString keyword
|
||||
id <- endSpan start
|
||||
@@ -2810,9 +2907,10 @@ tryParseWordToken keyword t = try $ do
|
||||
_ -> return ()
|
||||
|
||||
lookAhead keywordSeparator
|
||||
when (str /= keyword) $
|
||||
parseProblem ErrorC 1081 $
|
||||
"Scripts are case sensitive. Use '" ++ keyword ++ "', not '" ++ str ++ "'."
|
||||
when (str /= keyword) $ do
|
||||
parseProblemAt pos ErrorC 1081 $
|
||||
"Scripts are case sensitive. Use '" ++ keyword ++ "', not '" ++ str ++ "' (or quote if literal)."
|
||||
fail ""
|
||||
return $ t id
|
||||
|
||||
anycaseString =
|
||||
@@ -2885,12 +2983,14 @@ prop_readShebang5 = isWarning readShebang "\n#!/bin/sh"
|
||||
prop_readShebang6 = isWarning readShebang " # Copyright \n!#/bin/bash"
|
||||
prop_readShebang7 = isNotOk readShebang "# Copyright \nfoo\n#!/bin/bash"
|
||||
readShebang = do
|
||||
start <- startSpan
|
||||
anyShebang <|> try readMissingBang <|> withHeader
|
||||
many linewhitespace
|
||||
str <- many $ noneOf "\r\n"
|
||||
id <- endSpan start
|
||||
optional carriageReturn
|
||||
optional linefeed
|
||||
return str
|
||||
return $ T_Literal id str
|
||||
where
|
||||
anyShebang = choice $ map try [
|
||||
readCorrect,
|
||||
@@ -2966,22 +3066,72 @@ verifyEof = eof <|> choice [
|
||||
try (lookAhead p)
|
||||
action
|
||||
|
||||
prop_readScript1 = isOk readScriptFile "#!/bin/bash\necho hello world\n"
|
||||
prop_readScript2 = isWarning readScriptFile "#!/bin/bash\r\necho hello world\n"
|
||||
prop_readScript3 = isWarning readScriptFile "#!/bin/bash\necho hello\xA0world"
|
||||
prop_readScript4 = isWarning readScriptFile "#!/usr/bin/perl\nfoo=("
|
||||
prop_readScript5 = isOk readScriptFile "#!/bin/bash\n#This is an empty script\n\n"
|
||||
readScriptFile = do
|
||||
|
||||
readConfigFile :: Monad m => FilePath -> SCParser m [Annotation]
|
||||
readConfigFile filename = do
|
||||
shouldIgnore <- Mr.asks ignoreRC
|
||||
if shouldIgnore then return [] else read' filename
|
||||
where
|
||||
read' filename = do
|
||||
sys <- Mr.asks systemInterface
|
||||
contents <- system $ siGetConfig sys filename
|
||||
case contents of
|
||||
Nothing -> return []
|
||||
Just (file, str) -> readConfig file str
|
||||
|
||||
readConfig filename contents = do
|
||||
result <- lift $ runParserT readConfigKVs initialUserState filename contents
|
||||
case result of
|
||||
Right result ->
|
||||
return result
|
||||
|
||||
Left err -> do
|
||||
parseProblem ErrorC 1134 $ errorFor filename err
|
||||
return []
|
||||
|
||||
errorFor filename err =
|
||||
let line = "line " ++ (show . sourceLine $ errorPos err)
|
||||
suggestion = getStringFromParsec $ errorMessages err
|
||||
in
|
||||
"Failed to process " ++ filename ++ ", " ++ line ++ ": "
|
||||
++ suggestion
|
||||
|
||||
prop_readConfigKVs1 = isOk readConfigKVs "disable=1234"
|
||||
prop_readConfigKVs2 = isOk readConfigKVs "# Comment\ndisable=1234 # Comment\n"
|
||||
prop_readConfigKVs3 = isOk readConfigKVs ""
|
||||
prop_readConfigKVs4 = isOk readConfigKVs "\n\n\n\n\t \n"
|
||||
prop_readConfigKVs5 = isOk readConfigKVs "# shellcheck accepts annotation-like comments in rc files\ndisable=1234"
|
||||
readConfigKVs = do
|
||||
anySpacingOrComment
|
||||
annotations <- many (readAnnotationWithoutPrefix <* anySpacingOrComment)
|
||||
eof
|
||||
return $ concat annotations
|
||||
anySpacingOrComment =
|
||||
many (void allspacingOrFail <|> void readAnyComment)
|
||||
|
||||
prop_readScript1 = isOk readScript "#!/bin/bash\necho hello world\n"
|
||||
prop_readScript2 = isWarning readScript "#!/bin/bash\r\necho hello world\n"
|
||||
prop_readScript3 = isWarning readScript "#!/bin/bash\necho hello\xA0world"
|
||||
prop_readScript4 = isWarning readScript "#!/usr/bin/perl\nfoo=("
|
||||
prop_readScript5 = isOk readScript "#!/bin/bash\n#This is an empty script\n\n"
|
||||
readScriptFile sourced = do
|
||||
start <- startSpan
|
||||
pos <- getPosition
|
||||
optional $ do
|
||||
readUtf8Bom
|
||||
parseProblem ErrorC 1082
|
||||
"This file has a UTF-8 BOM. Remove it with: LC_CTYPE=C sed '1s/^...//' < yourscript ."
|
||||
sb <- option "" readShebang
|
||||
shebang <- readShebang <|> readEmptyLiteral
|
||||
let (T_Literal _ shebangString) = shebang
|
||||
allspacing
|
||||
annotationStart <- startSpan
|
||||
annotations <- readAnnotations
|
||||
fileAnnotations <- readAnnotations
|
||||
rcAnnotations <- if sourced
|
||||
then return []
|
||||
else do
|
||||
filename <- Mr.asks currentFilename
|
||||
readConfigFile filename
|
||||
let annotations = fileAnnotations ++ rcAnnotations
|
||||
annotationId <- endSpan annotationStart
|
||||
let shellAnnotationSpecified =
|
||||
any (\x -> case x of ShellOverride {} -> True; _ -> False) annotations
|
||||
@@ -2989,19 +3139,19 @@ readScriptFile = do
|
||||
let ignoreShebang = shellAnnotationSpecified || shellFlagSpecified
|
||||
|
||||
unless ignoreShebang $
|
||||
verifyShebang pos (getShell sb)
|
||||
if ignoreShebang || isValidShell (getShell sb) /= Just False
|
||||
verifyShebang pos (getShell shebangString)
|
||||
if ignoreShebang || isValidShell (getShell shebangString) /= Just False
|
||||
then do
|
||||
commands <- withAnnotations annotations readCompoundListOrEmpty
|
||||
id <- endSpan start
|
||||
verifyEof
|
||||
let script = T_Annotation annotationId annotations $
|
||||
T_Script id sb commands
|
||||
T_Script id shebang commands
|
||||
reparseIndices script
|
||||
else do
|
||||
many anyChar
|
||||
id <- endSpan start
|
||||
return $ T_Script id sb []
|
||||
return $ T_Script id shebang []
|
||||
|
||||
where
|
||||
basename s = reverse . takeWhile (/= '/') . reverse $ s
|
||||
@@ -3018,10 +3168,10 @@ readScriptFile = do
|
||||
case isValidShell s of
|
||||
Just True -> return ()
|
||||
Just False -> parseProblemAt pos ErrorC 1071 "ShellCheck only supports sh/bash/dash/ksh scripts. Sorry!"
|
||||
Nothing -> parseProblemAt pos InfoC 1008 "This shebang was unrecognized. Note that ShellCheck only handles sh/bash/dash/ksh."
|
||||
Nothing -> parseProblemAt pos ErrorC 1008 "This shebang was unrecognized. ShellCheck only supports sh/bash/dash/ksh. Add a 'shell' directive to specify."
|
||||
|
||||
isValidShell s =
|
||||
let good = s == "" || any (`isPrefixOf` s) goodShells
|
||||
let good = null s || any (`isPrefixOf` s) goodShells
|
||||
bad = any (`isPrefixOf` s) badShells
|
||||
in
|
||||
if good
|
||||
@@ -3035,6 +3185,7 @@ readScriptFile = do
|
||||
"ash",
|
||||
"dash",
|
||||
"bash",
|
||||
"bats",
|
||||
"ksh"
|
||||
]
|
||||
badShells = [
|
||||
@@ -3050,7 +3201,7 @@ readScriptFile = do
|
||||
|
||||
readUtf8Bom = called "Byte Order Mark" $ string "\xFEFF"
|
||||
|
||||
readScript = readScriptFile
|
||||
readScript = readScriptFile False
|
||||
|
||||
-- Interactively run a specific parser in ghci:
|
||||
-- debugParse readSimpleCommand "echo 'hello world'"
|
||||
@@ -3085,6 +3236,8 @@ testEnvironment =
|
||||
Environment {
|
||||
systemInterface = (mockedSystemInterface []),
|
||||
checkSourced = False,
|
||||
currentFilename = "myscript",
|
||||
ignoreRC = False,
|
||||
shellTypeOverride = Nothing
|
||||
}
|
||||
|
||||
@@ -3260,6 +3413,8 @@ parseScript sys spec =
|
||||
env = Environment {
|
||||
systemInterface = sys,
|
||||
checkSourced = psCheckSourced spec,
|
||||
currentFilename = psFilename spec,
|
||||
ignoreRC = psIgnoreRC spec,
|
||||
shellTypeOverride = psShellTypeOverride spec
|
||||
}
|
||||
|
||||
@@ -3296,4 +3451,3 @@ tryWithErrors parser = do
|
||||
|
||||
return []
|
||||
runTests = $quickCheckAll
|
||||
|
||||
|
@@ -1,5 +1,5 @@
|
||||
{-
|
||||
Copyright 2012-2015 Vidar Holen
|
||||
Copyright 2012-2019 Vidar Holen
|
||||
|
||||
This file is part of ShellCheck.
|
||||
https://www.shellcheck.net
|
||||
@@ -30,7 +30,7 @@ import Text.Regex.TDFA
|
||||
-- Precompile the regex
|
||||
mkRegex :: String -> Regex
|
||||
mkRegex str =
|
||||
let make :: RegexMaker Regex CompOption ExecOption String => String -> Regex
|
||||
let make :: String -> Regex
|
||||
make = makeRegex
|
||||
in
|
||||
make str
|
||||
|
@@ -2,7 +2,7 @@
|
||||
# For more information, see: https://docs.haskellstack.org/en/stable/yaml_configuration/
|
||||
|
||||
# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
|
||||
resolver: lts-8.5
|
||||
resolver: lts-13.26
|
||||
|
||||
# Local packages, usually specified by relative directory name
|
||||
packages:
|
||||
|
@@ -11,21 +11,34 @@ command -v cabal ||
|
||||
|
||||
cabal update ||
|
||||
die "can't update"
|
||||
cabal install --dependencies-only --enable-tests ||
|
||||
die "can't install dependencies"
|
||||
cabal configure --enable-tests ||
|
||||
|
||||
if [ -e /etc/arch-release ]
|
||||
then
|
||||
# Arch has an unconventional packaging setup
|
||||
flags=(--disable-library-vanilla --enable-shared --enable-executable-dynamic --ghc-options=-dynamic)
|
||||
else
|
||||
flags=()
|
||||
fi
|
||||
|
||||
cabal install --dependencies-only --enable-tests "${flags[@]}" ||
|
||||
cabal install --dependencies-only "${flags[@]}" ||
|
||||
die "can't install dependencies"
|
||||
cabal configure --enable-tests "${flags[@]}" ||
|
||||
die "configure failed"
|
||||
cabal build ||
|
||||
die "build failed"
|
||||
cabal test ||
|
||||
die "test failed"
|
||||
|
||||
dist/build/shellcheck/shellcheck - << 'EOF' || die "execution failed"
|
||||
sc="$(find . -name shellcheck -type f -perm -111)"
|
||||
[ -x "$sc" ] || die "Can't find executable"
|
||||
|
||||
"$sc" - << 'EOF' || die "execution failed"
|
||||
#!/bin/sh
|
||||
echo "Hello World"
|
||||
EOF
|
||||
|
||||
dist/build/shellcheck/shellcheck - << 'EOF' && die "negative execution failed"
|
||||
"$sc" - << 'EOF' && die "negative execution failed"
|
||||
#!/bin/sh
|
||||
echo $1
|
||||
EOF
|
||||
|
76
test/check_release
Executable file
76
test/check_release
Executable file
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env bash
|
||||
# shellcheck disable=SC2257
|
||||
|
||||
failed=0
|
||||
fail() {
|
||||
echo "$(tput setaf 1)$*$(tput sgr0)"
|
||||
failed=1
|
||||
}
|
||||
|
||||
if git diff | grep -q ""
|
||||
then
|
||||
fail "There are uncommited changes"
|
||||
fi
|
||||
|
||||
current=$(git tag --points-at)
|
||||
if [[ -z "$current" ]]
|
||||
then
|
||||
fail "No git tag on the current commit"
|
||||
echo "Create one with: git tag -a v0.0.0"
|
||||
fi
|
||||
|
||||
if [[ "$current" != v* ]]
|
||||
then
|
||||
fail "Bad tag format: expected v0.0.0"
|
||||
fi
|
||||
|
||||
if [[ "$(git cat-file -t "$current")" != "tag" ]]
|
||||
then
|
||||
fail "Current tag is not annotated (required for Snap)."
|
||||
fi
|
||||
|
||||
if [[ "$(git tag --points-at master)" != "$current" ]]
|
||||
then
|
||||
fail "You are not on master"
|
||||
fi
|
||||
|
||||
version=${current#v}
|
||||
if ! grep "Version:" ShellCheck.cabal | grep -qFw "$version"
|
||||
then
|
||||
fail "The cabal file does not match tag version $version"
|
||||
fi
|
||||
|
||||
if ! grep -qF "## $current" CHANGELOG.md
|
||||
then
|
||||
fail "CHANGELOG.md does not contain '## $current'"
|
||||
fi
|
||||
|
||||
if [[ $(git log -1 --pretty=%B) != "Stable version "* ]]
|
||||
then
|
||||
fail "Expected git log message to be 'Stable version ...'"
|
||||
fi
|
||||
|
||||
i=1 j=1
|
||||
cat << EOF
|
||||
|
||||
Manual Checklist
|
||||
|
||||
$((i++)). Make sure none of the automated checks above failed
|
||||
$((i++)). Make sure Travis build currently passes: https://travis-ci.org/koalaman/shellcheck
|
||||
$((i++)). Make sure SnapCraft build currently works: https://build.snapcraft.io/user/koalaman
|
||||
$((i++)). Run test/distrotest to ensure that most distros can build OOTB.
|
||||
$((i++)). Format and read over the manual for bad formatting and outdated info.
|
||||
$((i++)). Make sure the Hackage package builds, so that all files are
|
||||
|
||||
Release Steps
|
||||
|
||||
$((j++)). \`cabal sdist\` to generate a Hackage package
|
||||
$((j++)). \`git push --follow-tags\` to push commit
|
||||
$((j++)). Wait for Travis to build
|
||||
$((j++)). Verify release:
|
||||
a. Check that the new versions are uploaded: https://shellcheck.storage.googleapis.com/index.html
|
||||
b. Check that the docker images have version tags: https://hub.docker.com/u/koalaman
|
||||
$((j++)). If no disaster, upload to Hackage: http://hackage.haskell.org/upload
|
||||
$((j++)). Push a new commit that updates CHANGELOG.md
|
||||
EOF
|
||||
exit "$failed"
|
@@ -16,10 +16,14 @@ and is still highly experimental.
|
||||
|
||||
Make sure you're plugged in and have screen/tmux in place,
|
||||
then re-run with $0 --run to continue.
|
||||
|
||||
Also note that dist* will be deleted.
|
||||
EOF
|
||||
exit 0
|
||||
}
|
||||
|
||||
echo "Deleting 'dist' and 'dist-newstyle'..."
|
||||
rm -rf dist dist-newstyle
|
||||
|
||||
log=$(mktemp) || die "Can't create temp file"
|
||||
date >> "$log" || die "Can't write to log"
|
||||
@@ -57,19 +61,16 @@ done << EOF
|
||||
debian:stable apt-get update && apt-get install -y cabal-install
|
||||
debian:testing apt-get update && apt-get install -y cabal-install
|
||||
ubuntu:latest apt-get update && apt-get install -y cabal-install
|
||||
opensuse:latest zypper install -y cabal-install ghc
|
||||
|
||||
# Older Ubuntu versions we want to support
|
||||
ubuntu:18.04 apt-get update && apt-get install -y cabal-install
|
||||
ubuntu:17.10 apt-get update && apt-get install -y cabal-install
|
||||
|
||||
# Misc
|
||||
haskell:latest true
|
||||
opensuse/leap:latest zypper install -y cabal-install ghc
|
||||
fedora:latest dnf install -y cabal-install ghc-template-haskell-devel findutils
|
||||
archlinux/base:latest pacman -S -y --noconfirm cabal-install ghc-static base-devel
|
||||
|
||||
# Known to currently fail
|
||||
centos:latest yum install -y epel-release && yum install -y cabal-install
|
||||
fedora:latest dnf install -y cabal-install
|
||||
base/archlinux:latest pacman -S -y --noconfirm cabal-install ghc-static base-devel
|
||||
# Other versions we want to support
|
||||
ubuntu:18.04 apt-get update && apt-get install -y cabal-install
|
||||
|
||||
# Misc Haskell including current and latest Stack build
|
||||
ubuntu:18.04 set -e; apt-get update && apt-get install -y curl && curl -sSL https://get.haskellstack.org/ | sh -s - -f && cd /mnt && exec test/stacktest
|
||||
EOF
|
||||
|
||||
exit "$final"
|
||||
|
@@ -2,22 +2,28 @@ module Main where
|
||||
|
||||
import Control.Monad
|
||||
import System.Exit
|
||||
import qualified ShellCheck.Checker
|
||||
import qualified ShellCheck.Analytics
|
||||
import qualified ShellCheck.AnalyzerLib
|
||||
import qualified ShellCheck.Parser
|
||||
import qualified ShellCheck.Checker
|
||||
import qualified ShellCheck.Checks.Commands
|
||||
import qualified ShellCheck.Checks.Custom
|
||||
import qualified ShellCheck.Checks.ShellSupport
|
||||
import qualified ShellCheck.Fixer
|
||||
import qualified ShellCheck.Formatter.Diff
|
||||
import qualified ShellCheck.Parser
|
||||
|
||||
main = do
|
||||
putStrLn "Running ShellCheck tests..."
|
||||
results <- sequence [
|
||||
ShellCheck.Checker.runTests,
|
||||
ShellCheck.Checks.Commands.runTests,
|
||||
ShellCheck.Checks.ShellSupport.runTests,
|
||||
ShellCheck.Analytics.runTests,
|
||||
ShellCheck.AnalyzerLib.runTests,
|
||||
ShellCheck.Parser.runTests
|
||||
ShellCheck.Analytics.runTests
|
||||
,ShellCheck.AnalyzerLib.runTests
|
||||
,ShellCheck.Checker.runTests
|
||||
,ShellCheck.Checks.Commands.runTests
|
||||
,ShellCheck.Checks.Custom.runTests
|
||||
,ShellCheck.Checks.ShellSupport.runTests
|
||||
,ShellCheck.Fixer.runTests
|
||||
,ShellCheck.Formatter.Diff.runTests
|
||||
,ShellCheck.Parser.runTests
|
||||
]
|
||||
if and results
|
||||
then exitSuccess
|
||||
|
27
test/stacktest
Executable file
27
test/stacktest
Executable file
@@ -0,0 +1,27 @@
|
||||
#!/bin/bash
|
||||
# This script builds ShellCheck through `stack` using
|
||||
# various resolvers. It's run via distrotest.
|
||||
|
||||
resolvers=(
|
||||
nightly-"$(date -d "3 days ago" +"%Y-%m-%d")"
|
||||
)
|
||||
|
||||
die() { echo "$*" >&2; exit 1; }
|
||||
|
||||
[ -e "ShellCheck.cabal" ] ||
|
||||
die "ShellCheck.cabal not in current dir"
|
||||
[ -e "stack.yaml" ] ||
|
||||
die "stack.yaml not in current dir"
|
||||
command -v stack ||
|
||||
die "stack is missing"
|
||||
|
||||
stack setup || die "Failed to setup with default resolver"
|
||||
stack build --test || die "Failed to build/test with default resolver"
|
||||
|
||||
for resolver in "${resolvers[@]}"
|
||||
do
|
||||
stack --resolver="$resolver" setup || die "Failed to setup $resolver"
|
||||
stack --resolver="$resolver" build --test || die "Failed build/test with $resolver!"
|
||||
done
|
||||
|
||||
echo "Success"
|
Reference in New Issue
Block a user