From 9900b741552b744fad790d6bb3464b925c40d73a Mon Sep 17 00:00:00 2001 From: pubkey Date: Wed, 11 Jul 2018 12:02:14 +0200 Subject: [PATCH] UPDATE eslit --- .eslintignore | 5 + .eslintrc.json | 301 ++++++++++++++++++++++++++++++++-- src/index.js | 2 +- src/leader-election/index.js | 16 +- src/method-chooser.js | 4 +- src/methods/indexed-db.js | 10 +- src/methods/localstorage.js | 4 +- src/methods/native.js | 6 +- src/methods/node.js | 8 +- src/util.js | 3 +- test/.eslintrc | 3 + test/integration.test.js | 9 +- test/scripts/util.js | 2 + test/typings.test.js | 2 +- test/unit/node.method.test.js | 2 - 15 files changed, 334 insertions(+), 43 deletions(-) create mode 100644 .eslintignore create mode 100644 test/.eslintrc diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..de0eea0c --- /dev/null +++ b/.eslintignore @@ -0,0 +1,5 @@ +dist/ +node_modules/ +tmp/ +test/scripts/worker.js +test/e2e.test.js \ No newline at end of file diff --git a/.eslintrc.json b/.eslintrc.json index c512d62d..d5e1d95d 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,36 +1,317 @@ { - "parser": "babel-eslint", "env": { "browser": true, "es6": true, "node": true }, + "extends": "eslint:recommended", "parserOptions": { + "ecmaVersion": 2018, "sourceType": "module" }, "rules": { + "accessor-pairs": "error", + "array-bracket-newline": "off", + "array-bracket-spacing": [ + "error", + "never" + ], + "array-callback-return": "off", + "array-element-newline": "off", + "arrow-body-style": "off", + "arrow-parens": "off", + "arrow-spacing": [ + "error", + { + "after": true, + "before": true + } + ], + "block-scoped-var": "error", + "block-spacing": "error", + "brace-style": [ + "error", + "1tbs" + ], + "callback-return": "error", + "capitalized-comments": "off", + "class-methods-use-this": "off", + "comma-dangle": "off", + "comma-spacing": [ + "error", + { + "after": true, + "before": false + } + ], + "comma-style": [ + "error", + "last" + ], + "complexity": "off", + "computed-property-spacing": [ + "error", + "never" + ], + "consistent-return": "off", + "consistent-this": "off", + "curly": "off", + "default-case": "off", + "dot-location": [ + "error", + "property" + ], + "dot-notation": "off", + "eol-last": "off", + "eqeqeq": "off", + "func-call-spacing": "error", + "func-name-matching": "off", + "func-names": "off", + "func-style": "off", + "function-paren-newline": "off", + "generator-star-spacing": "error", + "global-require": "off", + "guard-for-in": "off", + "handle-callback-err": "error", + "id-blacklist": "error", + "id-length": "off", + "id-match": "error", + "implicit-arrow-linebreak": "off", "indent": [ "error", - 4, { - "MemberExpression": "off", + 4, + { + "MemberExpression": "off", "SwitchCase": 1 } ], + "indent-legacy": "off", + "init-declarations": "off", + "jsx-quotes": "error", + "key-spacing": "error", + "keyword-spacing": "off", + "line-comment-position": "off", "linebreak-style": [ "error", "unix" ], - "quotes": [ + "lines-around-comment": "off", + "lines-around-directive": "error", + "lines-between-class-members": "off", + "max-classes-per-file": "off", + "max-depth": "error", + "max-len": "off", + "max-lines": "off", + "max-lines-per-function": "off", + "max-nested-callbacks": "error", + "max-params": "off", + "max-statements": "off", + "max-statements-per-line": "error", + "multiline-comment-style": "off", + "multiline-ternary": [ "error", - "single" + "always-multiline" ], - "semi": [ + "new-parens": "off", + "newline-after-var": "off", + "newline-before-return": "off", + "newline-per-chained-call": "off", + "no-alert": "error", + "no-array-constructor": "error", + "no-await-in-loop": "off", + "no-bitwise": "off", + "no-buffer-constructor": "off", + "no-caller": "error", + "no-catch-shadow": "error", + "no-case-declarations": "off", + "no-confusing-arrow": "off", + "no-constant-condition": [ "error", - "always" + { + "checkLoops": false + } ], "no-console": "off", - "no-unused-vars": "error", + "no-continue": "off", + "no-div-regex": "error", + "no-duplicate-imports": "off", + "no-else-return": "off", + "no-empty": [ + "error", + { + "allowEmptyCatch": true + } + ], + "no-empty-function": "off", + "no-eq-null": "error", + "no-eval": "error", + "no-extend-native": "error", + "no-extra-bind": "error", + "no-extra-label": "error", + "no-extra-parens": "off", + "no-floating-decimal": "error", + "no-implicit-globals": "error", + "no-implied-eval": "error", + "no-inline-comments": "off", + "no-invalid-this": "off", + "no-iterator": "error", + "no-label-var": "error", + "no-labels": "error", + "no-lone-blocks": "error", + "no-lonely-if": "off", + "no-loop-func": "off", + "no-magic-numbers": "off", + "no-mixed-operators": "off", + "no-mixed-requires": "error", + "no-multi-assign": "error", + "no-multi-spaces": "error", + "no-multi-str": "error", + "no-multiple-empty-lines": "off", + "no-native-reassign": "error", + "no-negated-condition": "off", + "no-negated-in-lhs": "error", + "no-nested-ternary": "error", + "no-new": "error", + "no-new-func": "error", + "no-new-object": "error", + "no-new-require": "error", + "no-new-wrappers": "error", + "no-octal-escape": "error", + "no-param-reassign": "off", + "no-path-concat": "error", + "no-plusplus": "off", + "no-process-env": "off", + "no-process-exit": "off", + "no-proto": "off", + "no-prototype-builtins": "off", + "no-restricted-globals": "error", + "no-restricted-imports": "error", + "no-restricted-modules": "error", + "no-restricted-properties": "error", + "no-restricted-syntax": "error", + "no-return-assign": "off", + "no-return-await": "off", + "no-script-url": "error", + "no-self-compare": "error", + "no-sequences": "error", + "no-shadow": "off", + "no-shadow-restricted-names": "error", + "no-spaced-func": "error", + "no-sync": "off", + "no-tabs": "error", + "no-template-curly-in-string": "error", + "no-ternary": "off", + "no-throw-literal": "error", + "no-trailing-spaces": [ + "error", + { + "ignoreComments": true, + "skipBlankLines": true + } + ], + "no-undef-init": "error", + "no-undefined": "off", + "no-underscore-dangle": "off", + "no-unmodified-loop-condition": "error", + "no-unneeded-ternary": "off", + "no-unused-expressions": "off", + "no-use-before-define": "off", + "no-useless-call": "error", + "no-useless-computed-key": "error", + "no-useless-concat": "off", + "no-useless-constructor": "error", + "no-useless-return": "off", "no-var": "error", - "no-throw-literal": "error" + "no-void": "error", + "no-warning-comments": "off", + "no-whitespace-before-property": "error", + "no-with": "error", + "nonblock-statement-body-position": [ + "error", + "any" + ], + "object-curly-newline": "error", + "object-curly-spacing": [ + "error", + "always" + ], + "object-property-newline": "error", + "object-shorthand": "off", + "one-var": "off", + "one-var-declaration-per-line": [ + "error", + "initializations" + ], + "operator-assignment": "off", + "operator-linebreak": [ + "error", + "after" + ], + "padded-blocks": "off", + "padding-line-between-statements": "error", + "prefer-arrow-callback": "off", + "prefer-const": "error", + "prefer-destructuring": "off", + "prefer-numeric-literals": "error", + "prefer-object-spread": "off", + "prefer-promise-reject-errors": "error", + "prefer-reflect": "off", + "prefer-rest-params": "off", + "prefer-spread": "off", + "prefer-template": "off", + "quote-props": "off", + "quotes": [ + "error", + "single" + ], + "radix": "off", + "require-await": "off", + "require-jsdoc": "off", + "rest-spread-spacing": [ + "error", + "never" + ], + "semi": "error", + "semi-spacing": [ + "error", + { + "after": true, + "before": false + } + ], + "semi-style": [ + "error", + "last" + ], + "sort-imports": "off", + "sort-keys": "off", + "sort-vars": "off", + "space-before-blocks": "error", + "space-before-function-paren": "off", + "space-in-parens": [ + "error", + "never" + ], + "space-infix-ops": "error", + "space-unary-ops": "error", + "spaced-comment": "off", + "strict": "error", + "switch-colon-spacing": "error", + "symbol-description": "error", + "template-curly-spacing": [ + "error", + "never" + ], + "template-tag-spacing": "error", + "unicode-bom": [ + "error", + "never" + ], + "valid-jsdoc": "off", + "vars-on-top": "error", + "wrap-iife": "error", + "wrap-regex": "off", + "yield-star-spacing": "error", + "yoda": "off" } -} +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 63859b43..a1218d9a 100644 --- a/src/index.js +++ b/src/index.js @@ -157,7 +157,7 @@ function _startListening(channel) { channel._addEventListeners[msgObj.type].forEach(obj => { if (msgObj.time >= obj.time) { obj.fn(msgObj.data); - }; + } }); }; diff --git a/src/leader-election/index.js b/src/leader-election/index.js index 2cdeadcf..2a396784 100644 --- a/src/leader-election/index.js +++ b/src/leader-election/index.js @@ -55,7 +55,7 @@ LeaderElection.prototype = { stopCriteria = true; } } - } + }; this._channel.addEventListener('internal', handleMessage); @@ -63,12 +63,12 @@ LeaderElection.prototype = { const ret = this._sendMessage('apply') // send out that this one is applying .then(() => sleep(this._options.responseTime)) // let others time to respond .then(() => { - if (stopCriteria) return Promise.reject(); + if (stopCriteria) return Promise.reject(new Error()); else return this._sendMessage('apply'); }) .then(() => sleep(this._options.responseTime)) // let others time to respond .then(() => { - if (stopCriteria) return Promise.reject(); + if (stopCriteria) return Promise.reject(new Error()); else return this._sendMessage(); }) .then(() => this._beLeader()) // no one disagreed -> this one is now leader @@ -81,7 +81,7 @@ LeaderElection.prototype = { this._reApply = false; return this.applyOnce(); } else return success; - }) + }); return ret; }, @@ -118,8 +118,8 @@ LeaderElection.prototype = { this.applyOnce().then(() => { if (this.isLeader) finish(); }); - }; - } + } + }; this._channel.addEventListener('internal', whenDeathListener); this._listeners.push(whenDeathListener); }); @@ -164,8 +164,8 @@ LeaderElection.prototype = { const isLeaderListener = msg => { if (msg.context === 'leader' && msg.action === 'apply') { this._sendMessage('tell'); - }; - } + } + }; this._channel.addEventListener('internal', isLeaderListener); this._listeners.push(isLeaderListener); return this._sendMessage('tell'); diff --git a/src/method-chooser.js b/src/method-chooser.js index 7416969b..7221e631 100644 --- a/src/method-chooser.js +++ b/src/method-chooser.js @@ -5,7 +5,7 @@ const IndexeDbMethod = require('./methods/indexed-db.js'); const LocalstorageMethod = require('./methods/localstorage.js'); // order is important -let METHODS = [ +const METHODS = [ NativeMethod, // fastest IndexeDbMethod, LocalstorageMethod @@ -37,7 +37,7 @@ export function chooseMethod(options) { chooseMethods = METHODS.filter(m => m.type !== 'idb'); } - const useMethod = chooseMethods.find(method => method.canBeUsed()); + const useMethod = chooseMethods.find(method => typeof method === 'function' && method.canBeUsed()); if (!useMethod) throw new Error('No useable methode found:' + JSON.stringify(METHODS.map(m => m.type))); else diff --git a/src/methods/indexed-db.js b/src/methods/indexed-db.js index 6ffccc88..23a5fbea 100644 --- a/src/methods/indexed-db.js +++ b/src/methods/indexed-db.js @@ -23,9 +23,9 @@ export const type = 'idb'; export function getIdb() { if (typeof indexedDB !== 'undefined') return indexedDB; - if (typeof mozIndexedDB !== 'undefined') return mozIndexedDB; - if (typeof webkitIndexedDB !== 'undefined') return webkitIndexedDB; - if (typeof msIndexedDB !== 'undefined') return msIndexedDB; + if (typeof window.mozIndexedDB !== 'undefined') return window.mozIndexedDB; + if (typeof window.webkitIndexedDB !== 'undefined') return window.webkitIndexedDB; + if (typeof window.msIndexedDB !== 'undefined') return window.msIndexedDB; return false; } @@ -260,8 +260,8 @@ export function canBeUsed() { if (!idb) return false; return true; -}; +} -export function averageResponseTime(options){ +export function averageResponseTime(options) { return options.idb.fallbackInterval * 1.5; } diff --git a/src/methods/localstorage.js b/src/methods/localstorage.js index 35e6593d..d3a65420 100644 --- a/src/methods/localstorage.js +++ b/src/methods/localstorage.js @@ -147,9 +147,9 @@ export function canBeUsed() { if (!ls) return false; return true; -}; +} -export function averageResponseTime(){ +export function averageResponseTime() { return 120; } \ No newline at end of file diff --git a/src/methods/native.js b/src/methods/native.js index b8a5636c..000e6670 100644 --- a/src/methods/native.js +++ b/src/methods/native.js @@ -19,7 +19,7 @@ export function create(channelName, options) { }; return state; -}; +} export function close(channelState) { channelState.bc.close(); @@ -39,9 +39,9 @@ export function canBeUsed() { if (isNode) return false; if (typeof BroadcastChannel === 'function') return true; -}; +} -export function averageResponseTime(){ +export function averageResponseTime() { return 100; } \ No newline at end of file diff --git a/src/methods/node.js b/src/methods/node.js index 09cfefca..9a5e1f94 100644 --- a/src/methods/node.js +++ b/src/methods/node.js @@ -41,7 +41,7 @@ export function cleanPipeName(str) { } else { return str; } -}; +} const mkdir = util.promisify(fs.mkdir); const writeFile = util.promisify(fs.writeFile); @@ -90,7 +90,7 @@ export async function ensureFoldersExist(channelName) { await mkdir(paths.readers).catch(() => null), await mkdir(paths.messages).catch(() => null) ]); -}; +} export function socketPath(channelName, readerUuid) { @@ -491,9 +491,9 @@ export async function close(channelState) { export function canBeUsed() { return isNode; -}; +} -export function averageResponseTime(){ +export function averageResponseTime() { return 50; } \ No newline at end of file diff --git a/src/util.js b/src/util.js index 0f92f55a..e8e6c485 100644 --- a/src/util.js +++ b/src/util.js @@ -23,10 +23,11 @@ export function randomInt(min, max) { * https://stackoverflow.com/a/1349426/3443137 */ export function randomToken(length) { + if (!length) length = 5; let text = ''; const possible = 'abcdefghijklmnopqrstuvwxzy0123456789'; - for (let i = 0; i < 5; i++) + for (let i = 0; i < length; i++) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; diff --git a/test/.eslintrc b/test/.eslintrc new file mode 100644 index 00000000..cc36e1ec --- /dev/null +++ b/test/.eslintrc @@ -0,0 +1,3 @@ +env: + node: true + mocha: true \ No newline at end of file diff --git a/test/integration.test.js b/test/integration.test.js index ccc6c88a..6e216b6a 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -369,7 +369,7 @@ function runTest(channelType) { it('should create an elector', () => { const channelName = AsyncTestUtil.randomString(12); const channel = new BroadcastChannel(channelName, channelOptions); - const elector = LeaderElection.create(channel); + LeaderElection.create(channel); channel.close(); }); }); @@ -461,8 +461,9 @@ function runTest(channelType) { channel2.close(); }); - it('should clean up all unloaded when dead', async()=> { + it('should clean up all unloaded when dead', async() => { return; // TODO run this once unload-module has been fixed + /* console.log('======'); const cacheLengthBefore = Object.keys(unload._getCache()).length; @@ -479,7 +480,7 @@ function runTest(channelType) { assert.equal(cacheLengthBefore, cacheLengthAfter); - process.exit(); + process.exit();*/ }); }); describe('.awaitLeadership()', () => { @@ -556,7 +557,7 @@ function runTest(channelType) { }); }); }); -}; +} if (isNode) { runTest('node'); diff --git a/test/scripts/util.js b/test/scripts/util.js index 560d69b1..26ae51e1 100644 --- a/test/scripts/util.js +++ b/test/scripts/util.js @@ -1,3 +1,5 @@ +/* eslint no-useless-escape: "off" */ + // https://stackoverflow.com/a/901144/3443137 export function getParameterByName(name, url) { if (!url) url = window.location.href; diff --git a/test/typings.test.js b/test/typings.test.js index 9f27d398..edbab005 100644 --- a/test/typings.test.js +++ b/test/typings.test.js @@ -119,7 +119,7 @@ describe('typings.test.ts', () => { ); }); }); - describe('LeaderElection', ()=> { + describe('LeaderElection', () => { it('call all methods', async () => { const code = ` (async()=>{ diff --git a/test/unit/node.method.test.js b/test/unit/node.method.test.js index 15ec4e83..296b7c41 100644 --- a/test/unit/node.method.test.js +++ b/test/unit/node.method.test.js @@ -2,8 +2,6 @@ const AsyncTestUtil = require('async-test-util'); const assert = require('assert'); const isNode = require('detect-node'); -const util = require('../../dist/lib/util.js'); - describe('unit/node.method.test.js', () => { /** * do not run in browser-tests