From 257c7c61cfac81c46c533408f49ae02caa1515f2 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Tue, 28 Jan 2020 15:02:26 +0100
Subject: [PATCH] Add eslint support and fix various linting problems

---
 packages/common/.eslintignore  |  4 ++++
 packages/common/.eslintrc.json | 26 ++++++++++++++++++++++++++
 packages/common/error.js       |  8 ++++----
 packages/common/errorreport.js | 11 ++++++-----
 packages/common/i18next.js     | 14 +++++++-------
 packages/common/jsonld.js      |  4 ++--
 packages/common/package.json   |  5 ++++-
 packages/common/styles.js      |  2 +-
 packages/common/utils.js       | 12 ++++++++++--
 packages/common/vpu-button.js  |  8 +++-----
 10 files changed, 67 insertions(+), 27 deletions(-)
 create mode 100644 packages/common/.eslintignore
 create mode 100644 packages/common/.eslintrc.json

diff --git a/packages/common/.eslintignore b/packages/common/.eslintignore
new file mode 100644
index 00000000..c139ed4c
--- /dev/null
+++ b/packages/common/.eslintignore
@@ -0,0 +1,4 @@
+/vendor/**
+/dist/**
+*.conf.js
+*.config.js
\ No newline at end of file
diff --git a/packages/common/.eslintrc.json b/packages/common/.eslintrc.json
new file mode 100644
index 00000000..fb4d4d31
--- /dev/null
+++ b/packages/common/.eslintrc.json
@@ -0,0 +1,26 @@
+{
+    "env": {
+        "browser": true,
+        "es6": true,
+        "mocha": true
+    },
+    "extends": ["eslint:recommended", "plugin:jsdoc/recommended"],
+    "globals": {
+        "Atomics": "readonly",
+        "SharedArrayBuffer": "readonly"
+    },
+    "parser": "babel-eslint",
+    "parserOptions": {
+        "ecmaVersion": 2018,
+        "sourceType": "module"
+    },
+    "rules": {
+        "no-unused-vars": ["error", { "args": "none" }],
+        "semi": [2, "always"],
+        "jsdoc/require-jsdoc": 0,
+        "jsdoc/require-param-description": 0,
+        "jsdoc/require-returns": 0,
+        "jsdoc/require-returns-description": 0,
+        "jsdoc/require-param-type": 0
+    }
+}
\ No newline at end of file
diff --git a/packages/common/error.js b/packages/common/error.js
index 34a192f0..3aa831fb 100644
--- a/packages/common/error.js
+++ b/packages/common/error.js
@@ -40,8 +40,8 @@ export const handleXhrError = (jqXHR, textStatus, errorThrown, icon = "sad") =>
         "type": "danger",
     });
 
-    if (_paq !== undefined) {
-        _paq.push(['trackEvent', 'XhrError', body]);
+    if (window._paq !== undefined) {
+        window._paq.push(['trackEvent', 'XhrError', body]);
     }
 };
 
@@ -89,7 +89,7 @@ export const handleFetchError = async (error, summary = "", icon = "sad") => {
         "type": "danger",
     });
 
-    if (_paq !== undefined) {
-        _paq.push(['trackEvent', 'FetchError', summary === "" ? body : summary + ": " + body]);
+    if (window._paq !== undefined) {
+        window._paq.push(['trackEvent', 'FetchError', summary === "" ? body : summary + ": " + body]);
     }
 };
diff --git a/packages/common/errorreport.js b/packages/common/errorreport.js
index c350db7d..31276934 100644
--- a/packages/common/errorreport.js
+++ b/packages/common/errorreport.js
@@ -2,7 +2,7 @@ import * as Sentry from '@sentry/browser';
 import env from './env.js';
 import environment from 'consts:environment';
 
-let _isInitialized = false
+let _isInitialized = false;
 let _canReportEvent = false;
 
 /**
@@ -10,8 +10,9 @@ let _canReportEvent = false;
  * 
  * If a sentry DSN is set we will use sentry, if not we will log to the console.
  * 
- * @param {Boolean} [options.debug=false] Enable debug output
- * @param {String} [options.release] The project release
+ * @param {object} [options]
+ * @param {boolean} [options.debug=false] Enable debug output
+ * @param {string} [options.release] The project release
  */
 export function init(options) {
   let defaults = {
@@ -84,8 +85,8 @@ export function captureException(exception) {
 /**
  * Log a message, returns an internal ID
  *
- * @param {String} message The message to log
- * @param {String} [level=error] The loglevel (error, warning, info, debug)
+ * @param {string} message The message to log
+ * @param {string} [level=error] The loglevel (error, warning, info, debug)
  */
 export function captureMessage(message, level) {
   if (!_isInitialized)
diff --git a/packages/common/i18next.js b/packages/common/i18next.js
index 7dc135f0..03882eec 100644
--- a/packages/common/i18next.js
+++ b/packages/common/i18next.js
@@ -7,8 +7,8 @@ import i18next from 'i18next';
  *
  * @param {i18next.i18n} i18n - The i18next instance
  * @param {Date} date - The date to format
- * @param options - Options passed to Intl.DateTimeFormat
- * @returns {string}
+ * @param {object} options - Options passed to Intl.DateTimeFormat
+ * @returns {string} The formated datetime
  */
 export function dateTimeFormat(i18n, date, options) {
     return new Intl.DateTimeFormat(i18n.languages, options).format(date);
@@ -20,9 +20,9 @@ export function dateTimeFormat(i18n, date, options) {
  * A i18next instance can be created with createInstance()
  *
  * @param {i18next.i18n} i18n - The i18next instance
- * @param {Number} number - The number to format
- * @param {Object} options - Options passed to Intl.NumberFormat
- * @returns {string}
+ * @param {number} number - The number to format
+ * @param {object} options - Options passed to Intl.NumberFormat
+ * @returns {string} The formated number
  */
 export function numberFormat(i18n, number, options) {
     return new Intl.NumberFormat(i18n.languages, options).format(number);
@@ -33,10 +33,10 @@ export function numberFormat(i18n, number, options) {
  *
  * Call changeLanguage() on the returned object to change the language.
  *
- * @param {Object} languages - Mapping from languages to translation objects
+ * @param {object} languages - Mapping from languages to translation objects
  * @param {string} lng - The default language
  * @param {string} fallback - The fallback language to use for unknown languages or untranslated keys
- * @returns {i18next.i18n}
+ * @returns {i18next.i18n} A new independent i18next instance
  */
 export function createInstance(languages, lng, fallback) {
     var options = {
diff --git a/packages/common/jsonld.js b/packages/common/jsonld.js
index 0d34d188..e2d00282 100644
--- a/packages/common/jsonld.js
+++ b/packages/common/jsonld.js
@@ -258,7 +258,7 @@ export default class JSONLD {
      *
      * @param data
      * @param localContext
-     * @returns {Array}
+     * @returns {Array} An array of transformed objects
      */
     transformMembers(data, localContext) {
         const members = data['hydra:member'];
@@ -277,4 +277,4 @@ export default class JSONLD {
 
         return results;
     }
-};
+}
diff --git a/packages/common/package.json b/packages/common/package.json
index 8ecde8d6..8541b42e 100644
--- a/packages/common/package.json
+++ b/packages/common/package.json
@@ -4,6 +4,8 @@
   "module": "index.js",
   "devDependencies": {
     "chai": "^4.2.0",
+    "eslint": "^6.8.0",
+    "eslint-plugin-jsdoc": "^21.0.0",
     "karma": "^4.2.0",
     "karma-chrome-launcher": "^3.0.0",
     "karma-mocha": "^1.3.0",
@@ -25,7 +27,8 @@
     "build": "rollup -c",
     "build-test": "rollup -c --environment BUILD:test",
     "test": "npm run build-test && karma start --singleRun",
-    "watch": "rollup -c --watch"
+    "watch": "rollup -c --watch",
+    "lint": "eslint ."
   },
   "dependencies": {
     "@sentry/browser": "^5.6.3",
diff --git a/packages/common/styles.js b/packages/common/styles.js
index 34f331c2..4ecc1994 100644
--- a/packages/common/styles.js
+++ b/packages/common/styles.js
@@ -1,4 +1,4 @@
-import {css, unsafeCSS} from 'lit-element';
+import {css, unsafeCSS, CSSResult} from 'lit-element';
 import {getIconSVGURL} from './vpu-icon.js';
 
 /**
diff --git a/packages/common/utils.js b/packages/common/utils.js
index 5a357954..56c870c0 100644
--- a/packages/common/utils.js
+++ b/packages/common/utils.js
@@ -65,12 +65,13 @@ export const parseBaseUrl = (url) => {
  */
 export const stringListToSelect2DataArray = (list) => {
     let data = [];
-    list.forEach((item) => {data.push({id: item, text: item})});
+    list.forEach((item) => {data.push({id: item, text: item});});
     return data;
 };
 
 /**
  * Does generic Base64 Encoding with support for 16-bit encoded strings
+ *
  * @see https://www.base64encoder.io/javascript/
  *
  * @param str
@@ -94,6 +95,13 @@ export const base64EncodeUnicode = (str) => {
  *
  * @returns {boolean}
  */
+
+/**
+ * 
+ * @param {string} name 
+ * @param {Function} constructor 
+ * @param {object} options 
+ */
 export const defineCustomElement = (name, constructor, options) => {
     // Checks taken from https://github.com/webcomponents/webcomponentsjs/blob/master/webcomponents-loader.js
     if (!('attachShadow' in Element.prototype && 'getRootNode' in Element.prototype && window.customElements)) {
@@ -133,7 +141,7 @@ export const makeId = (length) => {
  * @param n
  * @returns {string}
  */
-export const pad10 = (n) => { return n < 10 ? '0' + n : n };
+export const pad10 = (n) => { return n < 10 ? '0' + n : n; };
 
 /**
  * Converts a date object or string to a local iso datetime with stripped seconds and timezone for the datetime-local input
diff --git a/packages/common/vpu-button.js b/packages/common/vpu-button.js
index 89f3f771..f5fff6cc 100644
--- a/packages/common/vpu-button.js
+++ b/packages/common/vpu-button.js
@@ -35,10 +35,6 @@ class Button extends LitElement {
         };
     }
 
-    static get styles() {
-        return css`vpu-mini-spinner { margin-left: 0.5em; }`;
-    }
-
     clickHandler() {
         if (!this.noSpinnerOnClick) {
             this.start();
@@ -56,7 +52,7 @@ class Button extends LitElement {
     }
 
     isDisabled() {
-        return this.disabled
+        return this.disabled;
     }
 
     static get styles() {
@@ -64,6 +60,8 @@ class Button extends LitElement {
         return css`
             ${commonStyles.getThemeCSS()}
             ${commonStyles.getButtonCSS()}
+
+            vpu-mini-spinner { margin-left: 0.5em; }
         `;
     }
 
-- 
GitLab