[Project][code_formatter] Added u options and changed JS formatting and validating... 70/202370/5
authorArkadiusz Pietraszek <a.pietraszek@partner.samsung.com>
Wed, 27 Mar 2019 14:29:37 +0000 (15:29 +0100)
committerArkadiusz Pietraszek <a.pietraszek@partner.samsung.com>
Fri, 5 Apr 2019 10:02:31 +0000 (12:02 +0200)
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/README
tools/codestyle/c++_clang_formatter.sh
tools/codestyle/code_formatter.sh
tools/codestyle/code_validation.sh
tools/codestyle/eslint_config_mandatory_length90.js [deleted file]
tools/codestyle/eslintrc_mandatory_4spaces.js [new file with mode: 0644]
tools/codestyle/js_clang_formatter.sh [deleted file]
tools/codestyle/js_eslint_formatter.sh [new file with mode: 0755]

index 88746fb23ef21198c2a81bc09a5260d53ab103e2..574e7e672315242a9567ca70ec68488eee63855f 100644 (file)
@@ -6,10 +6,10 @@
       - 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
 
index 2d0f0702bc01a7029a955c33661de456d7e77f3a..863e1609854acc5a1b598420d4767a2fd0e23110 100755 (executable)
@@ -1,8 +1,5 @@
 #!/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";
@@ -45,10 +42,23 @@ checkDirectoriesRecursively() {
   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"
index 2607d1a2d212ee897531e15b2227b7f121d35dfc..4ba525e6b86b3afaf08f763903849ab92c3f4f3c 100755 (executable)
@@ -4,6 +4,7 @@ dir="$(dirname "$(readlink -f "$0")")"
 dir2analyze="$(pwd)/src"
 analyzeJS="false";
 analyzeC="false";
+files_to_update="";
 
 printHelp() {
   echo "Script for automatic fixing some coding style rules issues."
@@ -13,10 +14,14 @@ printHelp() {
   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++() {
@@ -30,17 +35,69 @@ 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";
@@ -63,8 +120,4 @@ do
   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
index ab89d67eaaf042afde0f1a21bf2f3616a9c3aa2f..9e9b51994b0d863e40d9385e8e7043c77b394af5 100755 (executable)
@@ -8,12 +8,15 @@ analyzeC="false";
 
 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"
 }
@@ -69,12 +72,57 @@ checkJsAndC++() {
   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";
diff --git a/tools/codestyle/eslint_config_mandatory_length90.js b/tools/codestyle/eslint_config_mandatory_length90.js
deleted file mode 100644 (file)
index a09f75b..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-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']
-  }
-};
diff --git a/tools/codestyle/eslintrc_mandatory_4spaces.js b/tools/codestyle/eslintrc_mandatory_4spaces.js
new file mode 100644 (file)
index 0000000..2db494a
--- /dev/null
@@ -0,0 +1,54 @@
+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
diff --git a/tools/codestyle/js_clang_formatter.sh b/tools/codestyle/js_clang_formatter.sh
deleted file mode 100755 (executable)
index 1b55c3c..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-#!/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"
-
-
-
diff --git a/tools/codestyle/js_eslint_formatter.sh b/tools/codestyle/js_eslint_formatter.sh
new file mode 100755 (executable)
index 0000000..ad78a43
--- /dev/null
@@ -0,0 +1,61 @@
+#!/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"
+