From 94f7e6d0976abaf7ed02c0a1d202982b193e3485 Mon Sep 17 00:00:00 2001 From: Alejandro Santander Date: Fri, 20 Nov 2020 11:53:21 -0300 Subject: [PATCH] Improved lint pre-commit hook (#918) --- hooks/lint | 34 ++++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100755 hooks/lint diff --git a/hooks/lint b/hooks/lint new file mode 100755 index 0000000000..6879f51703 --- /dev/null +++ b/hooks/lint @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +# Detect changes in js and sol files +jsChangedFiles=$(git diff --name-only --exit-code --staged -- '***.js') +solChangedFiles=$(git diff --name-only --exit-code --staged -- '***.sol' ':!contracts/interfaces/*.sol') +isolChangedFiles=$(git diff --name-only --exit-code --staged -- 'contracts/interfaces/*.sol') + +# Lint Javascript, only on changed files +if [[ ! -z $jsChangedFiles ]]; then + echo "Javascript changed files [$jsChangedFiles]" + stderrOutput=$(npx eslint $jsChangedFiles 2>&1) + if [[ ! -z $stderrOutput ]]; then + echo "Javascript lint failed with: $stderrOutput" + exit 1 + fi +fi + +# Lint Solidity, only on changed files +if [[ ! -z $solChangedFiles ]]; then + echo "Solidity changed files [$solChangedFiles]" + stderrOutput=$(npx solhint $solChangedFiles 2>&1) + if [[ ! -z $stderrOutput ]]; then + echo "Solidity lint failed with: $stderrOutput" + exit 1 + fi +fi +if [[ ! -z $isolChangedFiles ]]; then + echo "Solidity interface files changed [$isolChangedFiles]" + stderrOutput=$(npx solhint $isolChangedFiles --config contracts/interfaces/.solhint.json 2>&1) # Capture stderr + if [[ ! -z $stderrOutput ]]; then + echo "Solidity interface lint failed with: $stderrOutput" + exit 1 + fi +fi diff --git a/package.json b/package.json index 2c2c62b004..354176090c 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ }, "husky": { "hooks": { - "pre-commit": "./hooks/circleci && npm run lint && pretty-quick --staged" + "pre-commit": "./hooks/circleci && pretty-quick --staged && ./hooks/lint" } }, "repository": {