Now eslint is used to format js files.
Not all issues can be fixed by eslint, so it prints list of issues for user to fix.
All 'u' options in formatter format appropriate files, that was changed from last commit.
Since files to format are not commited, this script backs up those files.
[Verification] Script executes without errors and proper files are chosen to format. Formated .js files have 4 spaces of indentation.
Change-Id: I10509ce3f639b25cca7e68e95e7c9419d9d82304
Signed-off-by: Arkadiusz Pietraszek <a.pietraszek@partner.samsung.com>
- tools/codestyle/code_validation.sh - common validator for both C/C++ and JS
(does not change files)
- tools/codestyle/c++_clang_formatter.sh - for auto formating C++ files
- - tools/codestyle/js_clang_formatter.sh - for auto formating JS files
+ - tools/codestyle/js_eslint_formatter.sh - for auto formating JS files
- tools/codestyle/cpplint_tizen_160919.py and tools/codestyle/cpplint_tizen_dir.sh
scripts for C++ code style validation, provided by HQ
- - tools/codestyle/eslint_config_mandatory_length90.js - coding rules for
+ - tools/codestyle/eslintrc_mandatory_4spaces.js - coding rules for
JS sources validation - provided by HQ
- code_format and code_validate - links to scripts for easier access
#!/bin/bash
-#set -x
-script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
-
# https://clangformat.com/
command -v clang-format >/dev/null 2>&1 || {
echo >&2 "clang-format is required, but it's not installed";
done
}
+checkUpdatedFiles() {
+ files=$(git diff-index --name-only --diff-filter=AM HEAD --)
+ for f in $files;
+ do
+ printf "."
+ if [ ${f: -3} == ".cc" ] || [ ${f: -4} == ".cpp" ] || [ ${f: -2} == ".h" ] || [ ${f: -4} == ".hpp" ]; then
+ clang-format -i -style="$config" "$f";
+ fi
+ done
+}
+
printf "C/C++ reformatting: ";
if [[ $# -eq 0 ]]; then
checkDirectoriesRecursively "src/"
+elif [[ "$1" == "-u" ]]; then
+ checkUpdatedFiles
else
checkDirectoriesRecursively "$1"
fi
-printf "\nDONE C/C++ reformatting\n"
+printf " DONE C/C++ reformatting\n"
dir2analyze="$(pwd)/src"
analyzeJS="false";
analyzeC="false";
+files_to_update="";
printHelp() {
echo "Script for automatic fixing some coding style rules issues."
echo "Using it without specifying a path would analyse \"src\" from current directory."
printf -- "\nUsage: $(basename $0) [directory] [options ...]\n"
printf -- "Options:\n"
+ printf -- "-u\t\tcheck all JS/C/C++ files that has been modified since last commit and fix issues\n"
+ printf -- "-uc\t\tcheck all C/C++ files that has been modified since last commit and fix issues\n"
+ printf -- "-ujs\t\tcheck all JS files that has been modified since last commit and fix issues\n"
printf -- "-c\t\tcheck C/C++ files and fix issues\n"
printf -- "-js\t\tcheck JS files and fix issues\n"
- printf -- "-a [--all]\tcheck all files JS and C/C++\n"
+ printf -- "-a [--all]\tcheck all files JS and C/C++ and fix issues\n"
printf -- "-h [--help]\tshow this help\n"
+ echo "When using any of 'u' options, all not commited files will be backed up."
}
formatJsAndC++() {
if [[ "$analyzeJS" == "true" ]]; then
echo "Reformatting JS sources recursively for directory $path";
- $dir/js_clang_formatter.sh "$path";
+ $dir/js_eslint_formatter.sh "$path";
else
echo "no JS sources reformatting - please use '-js' option to enable it";
fi
}
+backupUpdatedFiles() {
+ files_to_update=$(git diff-index --name-only --diff-filter=AM HEAD --);
+ if [[ "$files_to_update" == "" ]]; then
+ echo "There are no updated files to dormat";
+ exit;
+ fi
+ time_stamp=$(date +%Y-%m-%d-%T);
+ dir_name="backup_${time_stamp}";
+ echo "Creating backup of not commited changes in directory: $dir_name";
+ mkdir -p "${dir_name}";
+ for f in "$files_to_update"
+ do
+ cp $f "$dir_name";
+ done
+}
+
+formatUpdatedJsAndC++() {
+ if [[ "$files_to_update" == "" ]]; then
+ exit
+ fi
+ echo "Reformatting C++ and JS sources from list:";
+ git diff-index --name-only --diff-filter=AM HEAD --;
+ $dir/c++_clang_formatter.sh -u;
+ $dir/js_eslint_formatter.sh -u;
+}
+
+formatUpdatedJs() {
+ if [[ "$files_to_update" == "" ]]; then
+ exit
+ fi
+ echo "Reformatting JS sources from list:";
+ git diff-index --name-only --diff-filter=AM HEAD --;
+ $dir/js_eslint_formatter.sh -u;
+}
+
+formatUpdatedC++() {
+ if [["$files_to_update" == "" ]]; then
+ exit
+ fi
+ echo "Reformatting C++ sources from list:";
+ git diff-index --name-only --diff-filter=AM HEAD --;
+ $dir/c++_clang_formatter.sh -u;
+}
+
for arg in "$@";
do
if [[ "$arg" == "-u" ]]; then
- echo "check updated files"
- echo "TODO - Not supported yet"
+ backupUpdatedFiles
+ formatUpdatedJsAndC++
+ exit
+ elif [[ "$arg" == "-uc" ]]; then
+ backupUpdatedFiles
+ formatUpdatedC++
+ exit
+ elif [[ "$arg" == "-ujs" ]]; then
+ backupUpdatedFiles
+ formatUpdatedJs
exit
elif [[ "$arg" == "-c" ]]; then
analyzeC="true";
fi
done
-formatJsAndC++ "$dir2analyze"
-
-# some comments that probably could be useful while resolving TODO in future
-# (git diff --name-only; git diff --name-only --cached) | grep "\.js" - modified JS files
-# (git diff --name-only; git diff --name-only --cached) | grep "\.cc\|\.h\|\.cpp\|\.hpp" - modified C/C++ files
+formatJsAndC++ "$dir2analyze"
\ No newline at end of file
printHelp() {
echo "Script for validation source code about coding style rules violations."
- echo "The script would no change any files, only print report about found issues."
+ echo "The script would not change any files, only print report about found issues."
echo "Using it without specifying a path would analyse \"src\" from current directory."
printf -- "\nUsage: $(basename $0) [directory] [options ...]\n"
printf -- "Options:\n"
- printf -- "-c\t\tcheck C/C++ files and fix issues\n"
- printf -- "-js\t\tcheck JS files and fix issues\n"
+ printf -- "-u\t\tcheck all JS/C/C++ files that has been modified since last commit\n"
+ printf -- "-uc\t\tcheck all C/C++ files that has been modified since last commit\n"
+ printf -- "-ujs\t\tcheck all JS files that has been modified since last commit\n"
+ printf -- "-c\t\tcheck C/C++ files\n"
+ printf -- "-js\t\tcheck JS files\n"
printf -- "-a [--all]\tcheck all files JS and C/C++\n"
printf -- "-h [--help]\tshow this help\n"
}
fi
}
+checkUpdatedJs() {
+ echo "Validating updated JS files:";
+ files=$(git diff-index --name-only --diff-filter=AM HEAD --)
+ config_file="$script_path/eslintrc_mandatory_4spaces.js"
+ counter=0
+ for f in $files;
+ do
+ if [[ ${f: -3} == ".js" ]]; then
+ echo "Checking: $f"
+ counter=$((counter+1))
+ eslint --no-eslintrc -c "$config_file" $f
+ fi
+ done
+ if [[ "$counter" -eq 0 ]]; then
+ echo "No updated JS files to validate."
+ else
+ echo "JS validation done."
+ fi
+}
+
+checkUpdatedC++() {
+ echo "Validating updated C++ files:";
+ patch_cpp=`dirname $0`/cpplint_tizen_160919.py
+ files=$(git diff-index --name-only --diff-filter=AM HEAD --)
+ counter=0
+ for f in $files;
+ do
+ if [ ${f: -3} == ".cc" ] || [ ${f: -4} == ".cpp" ] || [ ${f: -2} == ".h" ] || [ ${f: -4} == ".hpp" ]; then
+ echo "Checking: $f\n"
+ counter=$((counter+1))
+ python $patch_cpp $f;
+ fi
+ done
+ if [[ "$counter" -eq 0 ]]; then
+ echo "No updated C++ files to validate."
+ else
+ echo "C++ validation done."
+ fi
+}
for arg in "$@";
do
if [[ "$arg" == "-u" ]]; then
- echo "check updated files"
- echo "TODO - Not supported yet"
+ checkUpdatedJs
+ checkUpdatedC++
+ exit
+ elif [[ "$arg" == "-uc" ]]; then
+ checkUpdatedC++
+ exit
+ elif [[ "$arg" == "-ujs" ]]; then
+ checkUpdatedJs
exit
elif [[ "$arg" == "-c" ]]; then
analyzeC="true";
+++ /dev/null
-module.exports = {
- 'env': {'browser': true, 'es6': true},
- 'parserOptions': {'sourceType': 'module'},
- 'rules': {
- // mandatory
- 'comma-dangle': [
- 'error', {
- 'arrays': 'never',
- 'objects': 'never',
- 'imports': 'never',
- 'exports': 'never',
- 'functions': 'ignore',
- }
- ],
- // mandatory
- 'indent': ['error', 2],
- // mandatory
- 'max-len': ['error', {'code': 90}],
- // mandatory
- 'no-extra-semi': ['error'],
- // mandatory
- 'operator-linebreak':
- ['error', 'after', {'overrides': {'?': 'before', ':': 'before'}}],
- // mandatory
- 'quotes': ['error', 'single'],
- // mandatory
- 'semi': ['error', 'always']
- }
-};
--- /dev/null
+module.exports = {
+ 'env': {
+ 'browser': true,
+ 'es6': true,
+ 'amd':true,
+ 'jquery': true
+ },
+ 'parserOptions': {
+ 'sourceType': 'script',
+ 'ecmaFeatures': {
+ 'impliedStrict': false
+ }
+ },
+ 'rules': {
+ // mandatory
+ 'comma-dangle': [
+ 'error', {
+ 'arrays': 'never',
+ 'objects': 'never',
+ 'imports': 'never',
+ 'exports': 'never',
+ 'functions': 'ignore'
+ }],
+ // mandatory
+ 'indent': [
+ 'error',
+ 4
+ ],
+ // mandatory
+ 'max-len': [
+ 'error',
+ { 'code': 90 }
+ ],
+ // mandatory
+ 'no-extra-semi': [
+ 'error'
+ ],
+ // mandatory
+ 'operator-linebreak': [
+ 'error',
+ 'after', { 'overrides': { '?': 'before', ':': 'before' } }
+ ],
+ // mandatory
+ 'quotes': [
+ 'error',
+ 'single'
+ ],
+ // mandatory
+ 'semi': [
+ 'error',
+ 'always'
+ ]
+ }
+};
\ No newline at end of file
+++ /dev/null
-#!/bin/bash
-
-command -v fixjsstyle >/dev/null 2>&1 || {
- echo >&2 "fixjsstyle is required, but it's not installed.";
- echo "-------------------------------------------------------------------------------------";
- echo "Related webpage: https://developers.google.com/closure/utilities/docs/linter_howto";
- echo "To install Closure Linter on Debian/Ubuntu, execute the following commands:";
- echo " sudo apt-get install python-pip";
- echo " sudo pip install https://github.com/google/closure-linter/zipball/master";
- echo "-------------------------------------------------------------------------------------";
- exit 1;
- }
-
-# https://clangformat.com/
-command -v clang-format >/dev/null 2>&1 || {
- echo >&2 "clang-format is required, but it's not installed";
- echo "-------------------------------------------------------------------------------------";
- echo "To install clang-format on Debian/Ubuntu, execute the following commands."
- echo " sudo apt-get install clang-format-3.9"
- echo " sudo ln -s /usr/bin/clang-format-3.9 /usr/bin/clang-format"
- echo "-------------------------------------------------------------------------------------";
- exit 1;
- }
-
-
-CONFIG='{
-BasedOnStyle: Google,
-Language: JavaScript,
-UseTab: Never,
-IndentWidth: 2,
-ColumnLimit: 90,
-AllowShortFunctionsOnASingleLine: false,
-PenaltyExcessCharacter: 1000,
-}'
-
-formatJS() {
- ls $1/*.js &>/dev/null
- if [[ $? -eq 0 ]]; then
- printf "."
- # using fixjsstyle for fixing some js issues
- fixjsstyle --nojsdoc --strict $1/*.js &> /dev/null
- # using clang-format for adjusting code style
- clang-format -i -style="$CONFIG" $1/*.js &> /dev/null
-
- # some issues could still appear after first style fixes,
- # repeating the same operations to fix them also
- fixjsstyle --nojsdoc --strict $1/*.js &> /dev/null
- clang-format -i -style="$CONFIG" $1/*.js &> /dev/null
- fi
-}
-
-checkDirectoriesRecursive() {
- dirs=$(find $1 -type d)
- for d in $dirs;
- do
- if [[ -d $d ]]; then
- formatJS $d;
- fi
- done
-}
-
-printf "JS reformatting: ";
-if [[ $# -eq 0 ]]; then
- checkDirectoriesRecursive src/
-else
- checkDirectoriesRecursive $1
-fi
-echo " DONE JS reformatting"
-
-
-
--- /dev/null
+#!/bin/bash
+
+# https://github.sec.samsung.net/RS-SA/coding-style-guides/tree/master/javascript
+# https://eslint.org/
+command -v eslint >/dev/null 2>&1 || {
+
+ echo >&2 "eslint is required, but it's not installed";
+ echo "-------------------------------------------------------------------------------------";
+ echo "To install eslint on Debian/Ubuntu, execute the following commands."
+ echo "sudo npm install -g eslint"
+ echo "-------------------------------------------------------------------------------------";
+ exit 1;
+}
+
+script_path="$( cd "$(dirname "$0")" ; pwd -P )";
+config_file="$script_path/eslintrc_mandatory_4spaces.js"
+
+formatJSDirectory() {
+ ls $1/*.js &>/dev/null
+ if [[ $? -eq 0 ]]; then
+ formatJSFile $1;
+ fi
+}
+
+formatJSFile() {
+ printf "."
+ # using eslint for fixing some js issues
+ eslint --no-eslintrc -c "$config_file" $1 --fix
+ # issues that are not fixed, will be printed fur the user to take care of them
+}
+
+checkDirectoriesRecursive() {
+ dirs=$(find $1 -type d)
+ for d in $dirs;
+ do
+ if [[ -d $d ]]; then
+ formatJSDirectory $d;
+ fi
+ done
+}
+
+checkUpdatedFiles() {
+ files=$(git diff-index --name-only --diff-filter=AM HEAD --)
+ for f in $files;
+ do
+ if [[ ${f: -3} == ".js" ]]; then
+ formatJSFile $f;
+ fi
+ done
+}
+
+printf "JS reformatting: ";
+if [[ $# -eq 0 ]]; then
+ checkDirectoriesRecursive src/
+elif [[ "$1" == "-u" ]]; then
+ checkUpdatedFiles
+else
+ checkDirectoriesRecursive $1
+fi
+echo " DONE JS reformatting"
+