Improve pre-commit hook

Runs a robust TypeScript check before running ESLint.

Test plan:
  - Make a change to a .ts/.tsx file that will cause a lint error
    - Try to commit a change
    - Verify that the precommit hook fails

flag=none

Change-Id: I6772041b2ae670ae9b9d654b07cbd97a80ad6896
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/310371
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Ed Schiebel <eschiebel@instructure.com>
Reviewed-by: Christopher Soto <christopher.soto@instructure.com>
QA-Review: Aaron Shafovaloff <ashafovaloff@instructure.com>
Product-Review: Aaron Shafovaloff <ashafovaloff@instructure.com>
This commit is contained in:
Aaron Shafovaloff 2023-02-03 12:28:07 -07:00
parent fe7a1fa858
commit f9eca2680a
4 changed files with 30 additions and 12 deletions

View File

@ -1,4 +1,3 @@
module.exports = { module.exports = {
'*.{js,ts,tsx}': ['eslint --fix', 'git add'], '*.{js,ts,tsx}': ['eslint --fix', 'git add'],
'*.{ts,tsx}': ['tsc-files -p tsconfig.json --noEmit']
} }

View File

@ -4,16 +4,41 @@ if [[ "$SKIP_CANVAS_PRECOMMIT_HOOK" == "1" ]]; then
exit 0 exit 0
fi fi
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" SCRIPTPATH="$(
cd -- "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
CANVAS=$(dirname $SCRIPTPATH) CANVAS=$(dirname $SCRIPTPATH)
if [ `pwd -P` = $CANVAS ]; then if [ $(pwd -P) = $CANVAS ]; then
# if .ts or .tsx files have changed, run tsc
if git diff --cached --name-only | grep -qE '\.(ts|tsx)$'; then
if [ -f node_modules/.bin/tsc ]; then
echo 'Checking TypeScript...'
node_modules/.bin/tsc -p tsconfig.json --noEmit
if [ $? -ne 0 ]; then
echo 'TypeScript errors found, aborting commit'
exit 1
fi
else
echo 'Trying to run tsc inside Docker. If you want things quicker yarn install locally.'
docker-compose exec -T web node_modules/.bin/tsc -p tsconfig.json --noEmit ||
echo "You should run yarn locally or check to make sure docker is running."
fi
fi
# if .js, .ts, or .tsx files have changed, run eslint
if [ -f node_modules/.bin/lint-staged ]; then if [ -f node_modules/.bin/lint-staged ]; then
echo 'Running ESLint...'
yarn run --silent lint:staged yarn run --silent lint:staged
if [ $? -ne 0 ]; then
echo 'ESLint errors found, aborting commit'
exit 1
fi
else else
echo 'Trying to run lint:staged inside Docker, if you want things quicker yarn install locally.' echo 'Trying to run lint:staged inside Docker. If you want things quicker yarn install locally.'
docker-compose run --rm --no-deps web yarn run lint:staged || docker-compose exec -T web yarn run lint:staged ||
echo 'You should run `yarn` locally or check to make sure docker is running.' echo "You should run yarn locally or check to make sure docker is running."
fi fi
fi fi

View File

@ -327,7 +327,6 @@
"through2": "^3", "through2": "^3",
"timezone-mock": "^1.3.1", "timezone-mock": "^1.3.1",
"tinymce": "^5", "tinymce": "^5",
"tsc-files": "^1.1.3",
"typescript": "^4.3.5", "typescript": "^4.3.5",
"waait": "^1", "waait": "^1",
"webpack": "^5", "webpack": "^5",

View File

@ -24701,11 +24701,6 @@ ts-pnp@^1.1.6:
resolved "https://registry.yarnpkg.com/ts-pnp/-/ts-pnp-1.2.0.tgz#a500ad084b0798f1c3071af391e65912c86bca92" resolved "https://registry.yarnpkg.com/ts-pnp/-/ts-pnp-1.2.0.tgz#a500ad084b0798f1c3071af391e65912c86bca92"
integrity sha512-csd+vJOb/gkzvcCHgTGSChYpy5f1/XKNsmvBGO4JXS+z1v2HobugDz4s1IeFXM3wZB44uczs+eazB5Q/ccdhQw== integrity sha512-csd+vJOb/gkzvcCHgTGSChYpy5f1/XKNsmvBGO4JXS+z1v2HobugDz4s1IeFXM3wZB44uczs+eazB5Q/ccdhQw==
tsc-files@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/tsc-files/-/tsc-files-1.1.3.tgz#ef4cfcb7affc9b90577d707a879dc53bb105be83"
integrity sha512-G6uXkTNofGU9EE1fYBaCpR72x/aqXW4PDAuznWj4JYlDwhcaKnUn4CiCHBMc89lDxLmikK+hhaEWLoTPEKKvXg==
tsconfig-paths@^3.12.0: tsconfig-paths@^3.12.0:
version "3.14.1" version "3.14.1"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.14.1.tgz#ba0734599e8ea36c862798e920bcf163277b137a" resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.14.1.tgz#ba0734599e8ea36c862798e920bcf163277b137a"