Add a shared pre-commit to run eslintfix for frontend code changes

This pre-commit hook will fail the commit when you have linter errors.
It will also only run on changed staged files.

Also cleaned up linter config in package and lint_test.

Change-Id: I312b16d8ad2245b2cd848cb03b2a50e56372aa72
This commit is contained in:
Tao Zhou 2019-12-11 15:25:26 +01:00
parent 6b92574bf2
commit 22302f03ed
3 changed files with 49 additions and 2 deletions

View File

@ -15,7 +15,7 @@
"scripts": {
"start": "polygerrit-ui/run-server.sh",
"test": "WCT_HEADLESS_MODE=1 WCT_ARGS='--verbose -l chrome' ./polygerrit-ui/app/run_test.sh",
"eslint": "./node_modules/eslint/bin/eslint.js --ignore-pattern 'bower_components/' --ignore-pattern 'gr-linked-text' --ignore-pattern 'scripts/vendor' --ext .html,.js polygerrit-ui/app",
"eslint": "./node_modules/eslint/bin/eslint.js --ext .html,.js polygerrit-ui/app",
"eslintfix": "npm run eslint -- --fix",
"test-template": "./polygerrit-ui/app/run_template_test.sh",
"polylint": "bazel test polygerrit-ui/app:polylint_test"

View File

@ -19,4 +19,4 @@ if [ -z "$eslint_bin" ] || [ "$eslint_config" -eq "0" ] || [ "$eslint_plugin" -e
exit 1
fi
${eslint_bin} --ignore-pattern 'bower_components/' --ignore-pattern 'gr-linked-text' --ignore-pattern 'scripts/vendor' --ext .html,.js .
${eslint_bin} --ext .html,.js .

47
tools/dev-hooks/pre-commit Executable file
View File

@ -0,0 +1,47 @@
#!/bin/sh
#
# Part of Gerrit Code Review (https://www.gerritcodereview.com/)
#
# Copyright (C) 2019 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# To enable this hook:
# - copy this file or content to ".git/hooks/pre-commit"
# - (optional if you copied this file) make it executable: `chmod +x .git/hooks/pre-commit`
set -ue
# gitroot, default to .
gitroot=$(git rev-parse --show-cdup)
gitroot=${gitroot:-.};
# eslint
eslint=${gitroot}/node_modules/eslint/bin/eslint.js
# Run eslint over changed frontend code
CHANGED_UI_FILES=$(git diff --cached --name-only -- '*.js' '*.html' | grep 'polygerrit-ui') && true
if [ "${CHANGED_UI_FILES}" ]; then
if $eslint --fix ${CHANGED_UI_FILES}; then
# Add again in case lint fix modified some files
git add ${CHANGED_UI_FILES}
exit 0
else
echo "Failed to fix all linter issues.";
exit 1
fi
else
echo "No UI files changed"
exit 0
fi