From e35061d833b18ce3845f11575bdf8a3f47a35354 Mon Sep 17 00:00:00 2001 From: Craig Beck Date: Mon, 3 Oct 2022 15:57:30 -0700 Subject: [PATCH 01/10] Add eslint; remove jshint --- .eslintrc | 36 ++++++++++++++++++++++++++++++++++++ .jshintrc | 18 ------------------ package.json | 2 +- 3 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 .eslintrc delete mode 100644 .jshintrc diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 000000000..4548f101d --- /dev/null +++ b/.eslintrc @@ -0,0 +1,36 @@ +{ + "env": { + "node": true + }, + "globals": { + "window": false, + "document": false + }, + "rules": { + "comma-style": [ + 2, + "last" + ], + "no-eq-null": 2, + "eqeqeq": 2, + "indent": [ + 2, + 2, + { + "SwitchCase": 1 + } + ], + "new-cap": 2, + "quotes": [ + 2, + "single" + ], + "no-undef": 2, + "no-shadow": 0, + "no-unused-expressions": 2, + "no-cond-assign": [ + 2, + "except-parens" + ] + } +} diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index cf514a151..000000000 --- a/.jshintrc +++ /dev/null @@ -1,18 +0,0 @@ -{ - "node": true, - "laxcomma": true, - "eqnull": true, - "eqeqeq": true, - "indent": 2, - "newcap": true, - "quotmark": "single", - "undef": true, - "trailing": true, - "shadow": true, - "expr": true, - "boss": true, - "globals": { - "window": false, - "document": false - } -} diff --git a/package.json b/package.json index 4c6179a01..b4c90693e 100644 --- a/package.json +++ b/package.json @@ -27,9 +27,9 @@ "async": "^1.5.2", "browserify": "^16.2.2", "chai": "^4.2.0", + "eslint": "^8.24.0", "express": "^4.13.3", "jsdom": "^15.2.0", - "jshint": "^2.9.5", "mocha": "^6.2.0" }, "optionalDependencies": {}, From 433309579c09fc97d44a6a9c97ce5f8cfec46224 Mon Sep 17 00:00:00 2001 From: Craig Beck Date: Mon, 3 Oct 2022 16:04:44 -0700 Subject: [PATCH 02/10] Automated eslint --fix --- lib/AppForServer.js | 14 +++++++------- lib/Dom.js | 2 +- lib/PageForServer.js | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/AppForServer.js b/lib/AppForServer.js index 2910bd4ac..b942faf5e 100644 --- a/lib/AppForServer.js +++ b/lib/AppForServer.js @@ -26,8 +26,8 @@ if (module.require) { var STYLE_EXTENSIONS = ['.css']; var VIEW_EXTENSIONS = ['.html']; var COMPILERS = { - '.css': cssCompiler -, '.html': htmlCompiler + '.css': cssCompiler, + '.html': htmlCompiler }; function cssCompiler(file, filename, options) { return {css: file, files: [filename]}; @@ -136,7 +136,7 @@ AppForServer.prototype.bundle = function(backend, options, cb) { if (filename !== viewsFilename) return through(); return through( function write() {} - , function end() { + , function end() { this.queue(viewsSource); this.queue(null); } @@ -229,10 +229,10 @@ AppForServer.prototype.serialize = function() { this.scriptMapUrl.slice(this.scriptMapBaseUrl.length) : this.scriptMapUrl; var serialized = JSON.stringify({ - scriptBaseUrl: this.scriptBaseUrl - , scriptMapBaseUrl: this.scriptMapBaseUrl - , scriptUrl: scriptUrl - , scriptMapUrl: scriptMapUrl + scriptBaseUrl: this.scriptBaseUrl, + scriptMapBaseUrl: this.scriptMapBaseUrl, + scriptUrl: scriptUrl, + scriptMapUrl: scriptMapUrl }); fs.writeFileSync(this.serializedBase + '.json', serialized, 'utf8'); }; diff --git a/lib/Dom.js b/lib/Dom.js index 0462e2e23..ed582d6ca 100644 --- a/lib/Dom.js +++ b/lib/Dom.js @@ -35,7 +35,7 @@ Dom.prototype.addListener = function(type, target, listener, useCapture) { } var domListener = (type === 'destroy') ? new DestroyListener(target, listener) : - new DomListener(type, target, listener, useCapture); + new DomListener(type, target, listener, useCapture); if (-1 === this._listenerIndex(domListener)) { var listeners = this._listeners || this._initListeners(); listeners.push(domListener); diff --git a/lib/PageForServer.js b/lib/PageForServer.js index c05f5987a..24b545850 100644 --- a/lib/PageForServer.js +++ b/lib/PageForServer.js @@ -96,9 +96,9 @@ function stringifyBundle(bundle) { // TODO: Cleanup; copied from tracks function pageParams(req) { var params = { - url: req.url - , body: req.body - , query: req.query + url: req.url, + body: req.body, + query: req.query, }; for (var key in req.params) { params[key] = req.params[key]; From 828b486c0df45ba69be960564e0c23567a3a6ce4 Mon Sep 17 00:00:00 2001 From: Craig Beck Date: Mon, 3 Oct 2022 17:25:55 -0700 Subject: [PATCH 03/10] Use conditional calls --- lib/AppForServer.js | 4 +++- lib/Page.js | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/AppForServer.js b/lib/AppForServer.js index b942faf5e..c3c9f4229 100644 --- a/lib/AppForServer.js +++ b/lib/AppForServer.js @@ -204,7 +204,9 @@ AppForServer.prototype.writeScripts = function(backend, dir, options, cb) { fs.unlinkSync(oldFilename); } } - cb && cb(); + if (cb) { + cb(); + } }); }; diff --git a/lib/Page.js b/lib/Page.js index e20536a8d..0d36ea406 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -126,7 +126,9 @@ Page.prototype.destroy = function() { silentModel.destroy('_page'); silentModel.destroy('$components'); // Unfetch and unsubscribe from all queries and documents - silentModel.unloadAll && silentModel.unloadAll(); + if (silentModel.unloadAll) { + silentModel.unloadAll(); + } }; Page.prototype._addModelListeners = function(eventModel) { From 068fe0291161514ccc6267baf3bccb55ce69a536 Mon Sep 17 00:00:00 2001 From: Craig Beck Date: Mon, 3 Oct 2022 17:26:16 -0700 Subject: [PATCH 04/10] Use default parameters --- .eslintrc | 3 +++ lib/textDiff.js | 6 ++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.eslintrc b/.eslintrc index 4548f101d..49e67f7f7 100644 --- a/.eslintrc +++ b/.eslintrc @@ -2,6 +2,9 @@ "env": { "node": true }, + "parserOptions": { + "ecmaVersion": 8 + }, "globals": { "window": false, "document": false diff --git a/lib/textDiff.js b/lib/textDiff.js index 335b1b38a..51d7a7754 100644 --- a/lib/textDiff.js +++ b/lib/textDiff.js @@ -2,20 +2,18 @@ exports.onStringInsert = onStringInsert; exports.onStringRemove = onStringRemove; exports.onTextInput = onTextInput; -function onStringInsert(el, previous, index, text) { +function onStringInsert(el, previous = '', index, text) { function transformCursor(cursor) { return (index < cursor) ? cursor + text.length : cursor; } - previous || (previous = ''); var newText = previous.slice(0, index) + text + previous.slice(index); replaceText(el, newText, transformCursor); } -function onStringRemove(el, previous, index, howMany) { +function onStringRemove(el, previous = '', index, howMany) { function transformCursor(cursor) { return (index < cursor) ? cursor - Math.min(howMany, cursor - index) : cursor; } - previous || (previous = ''); var newText = previous.slice(0, index) + previous.slice(index + howMany); replaceText(el, newText, transformCursor); } From 5ef8a78c3ba1c81c19b55a388f098e8f9e6c2366 Mon Sep 17 00:00:00 2001 From: Craig Beck Date: Mon, 3 Oct 2022 17:32:05 -0700 Subject: [PATCH 05/10] Allow double-eq comparison with `null` --- .eslintrc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.eslintrc b/.eslintrc index 49e67f7f7..9a64a0f6a 100644 --- a/.eslintrc +++ b/.eslintrc @@ -14,8 +14,7 @@ 2, "last" ], - "no-eq-null": 2, - "eqeqeq": 2, + "eqeqeq": ["error", "always", {"null": "ignore"}], "indent": [ 2, 2, From 3714c7babdb2f8b19c05abf060ddbf0d680f0360 Mon Sep 17 00:00:00 2001 From: Craig Beck Date: Tue, 4 Oct 2022 10:46:26 -0700 Subject: [PATCH 06/10] Conditional assignments --- lib/AppForServer.js | 4 +++- lib/eventmodel.js | 4 +++- lib/files.js | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/AppForServer.js b/lib/AppForServer.js index c3c9f4229..0f3d44d20 100644 --- a/lib/AppForServer.js +++ b/lib/AppForServer.js @@ -121,7 +121,9 @@ AppForServer.prototype.bundle = function(backend, options, cb) { cb = options; options = null; } - options || (options = {}); + if (options == null) { + options = {}; + } if (options.minify == null) options.minify = util.isProduction; // Turn all of the app's currently registered views into a javascript // function that can recreate them in the client diff --git a/lib/eventmodel.js b/lib/eventmodel.js index 62f82b1e3..9fefb862d 100644 --- a/lib/eventmodel.js +++ b/lib/eventmodel.js @@ -257,7 +257,9 @@ EventModel.prototype._removeItemContext = function(context) { EventModel.prototype._addBinding = function(binding) { var bindings = this.bindings || (this.bindings = new BindingsMap()); - binding.eventModels || (binding.eventModels = new EventModelsMap()); + if (bindings.eventModel == null) { + binding.eventModels = new EventModelsMap() + } bindings[binding.id] = binding; binding.eventModels[this.id] = this; }; diff --git a/lib/files.js b/lib/files.js index 0da39fd97..681688a58 100644 --- a/lib/files.js +++ b/lib/files.js @@ -54,7 +54,9 @@ function loadViewsSync(app, sourceFilename, namespace) { } function loadStylesSync(app, sourceFilename, options) { - options || (options = {compress: util.isProduction}); + if (options == null) { + options = { compress: util.isProduction } + } var resolved = resolve.sync(sourceFilename, { extensions: app.styleExtensions, packageFilter: deleteMain} From aec9f7bfa0ae35e3bbb374684f725ad88d80d0f4 Mon Sep 17 00:00:00 2001 From: Craig Beck Date: Tue, 4 Oct 2022 13:22:09 -0700 Subject: [PATCH 07/10] Include tests in lint; add script for checks --- .eslintrc | 8 +++++++- package.json | 4 +++- test-utils/assertions.js | 2 +- test/dom/bindings.mocha.js | 2 +- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.eslintrc b/.eslintrc index 9a64a0f6a..db482617c 100644 --- a/.eslintrc +++ b/.eslintrc @@ -34,5 +34,11 @@ 2, "except-parens" ] - } + }, + "overrides": [ + { + "files": ["test/**/*.mocha.js", "test/browser/*.js"], + "env": {"mocha": true, "node": true} + } + ] } diff --git a/package.json b/package.json index b4c90693e..2cf39ceb0 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,9 @@ }, "main": "index.js", "scripts": { - "test": "./node_modules/.bin/mocha test/all/*.mocha.js test/dom/*.mocha.js test/server/*.mocha.js; ./node_modules/.bin/jshint lib/*.js test/*.js test-utils/*.js", + "checks": "npm run lint && npm test", + "lint": "npx eslint *.js lib/**/*.js test/**/*.js test-utils/**/*.js", + "test": "npx mocha test/all/*.mocha.js test/dom/*.mocha.js test/server/*.mocha.js", "test-browser": "node test/server.js" }, "dependencies": { diff --git a/test-utils/assertions.js b/test-utils/assertions.js index d5d20ec3f..6437e0cdf 100644 --- a/test-utils/assertions.js +++ b/test-utils/assertions.js @@ -18,7 +18,7 @@ module.exports = function(dom, Assertion) { // NodeFilter.SHOW_COMMENT === 128 var treeWalker = domDocument.createTreeWalker(clone, 128, null, false); var toRemove = []; - for (var item; item = treeWalker.nextNode();) { + for (var item = treeWalker.nextNode(); item != null; item = treeWalker.nextNode()) { toRemove.push(item); } for (var i = toRemove.length; i--;) { diff --git a/test/dom/bindings.mocha.js b/test/dom/bindings.mocha.js index 0b7d8a29d..fe74f0393 100644 --- a/test/dom/bindings.mocha.js +++ b/test/dom/bindings.mocha.js @@ -417,7 +417,7 @@ describe('bindings', function() { var page = app.createPage(); page.model.on('insert', '_data.items', function(index, values) { - if (values[0] == 'B') { + if (values[0] === 'B') { page.model.insert('_data.items', 0, 'C'); } }); From 779642d36ddfbe87a48ab2d043261bfdde9b8753 Mon Sep 17 00:00:00 2001 From: Craig Beck Date: Tue, 4 Oct 2022 13:22:59 -0700 Subject: [PATCH 08/10] Update github workflow to use checks script --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 545f33a9c..2b1d541b4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -28,4 +28,4 @@ jobs: - name: Install run: npm install - name: Test - run: npm run test + run: npm run checks From 1266cdaa90603ba58eb9811dc6936b36bdc36395 Mon Sep 17 00:00:00 2001 From: Craig Beck Date: Thu, 6 Oct 2022 14:21:12 -0700 Subject: [PATCH 09/10] Use conditional to ensure previous has value --- lib/textDiff.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/textDiff.js b/lib/textDiff.js index 51d7a7754..99a8c50ff 100644 --- a/lib/textDiff.js +++ b/lib/textDiff.js @@ -2,18 +2,24 @@ exports.onStringInsert = onStringInsert; exports.onStringRemove = onStringRemove; exports.onTextInput = onTextInput; -function onStringInsert(el, previous = '', index, text) { +function onStringInsert(el, previous, index, text) { function transformCursor(cursor) { return (index < cursor) ? cursor + text.length : cursor; } + if (!previous) { + previous = ''; + } var newText = previous.slice(0, index) + text + previous.slice(index); replaceText(el, newText, transformCursor); } -function onStringRemove(el, previous = '', index, howMany) { +function onStringRemove(el, previous, index, howMany) { function transformCursor(cursor) { return (index < cursor) ? cursor - Math.min(howMany, cursor - index) : cursor; } + if (!previous) { + previous = ''; + } var newText = previous.slice(0, index) + previous.slice(index + howMany); replaceText(el, newText, transformCursor); } From 3bc388d9f6f0a3f273cadf68c2bf493e2623d72c Mon Sep 17 00:00:00 2001 From: Craig Beck Date: Thu, 6 Oct 2022 14:24:08 -0700 Subject: [PATCH 10/10] Add semicolons --- lib/eventmodel.js | 2 +- lib/files.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/eventmodel.js b/lib/eventmodel.js index 9fefb862d..3864e88b2 100644 --- a/lib/eventmodel.js +++ b/lib/eventmodel.js @@ -258,7 +258,7 @@ EventModel.prototype._removeItemContext = function(context) { EventModel.prototype._addBinding = function(binding) { var bindings = this.bindings || (this.bindings = new BindingsMap()); if (bindings.eventModel == null) { - binding.eventModels = new EventModelsMap() + binding.eventModels = new EventModelsMap(); } bindings[binding.id] = binding; binding.eventModels[this.id] = this; diff --git a/lib/files.js b/lib/files.js index 681688a58..8cc132c97 100644 --- a/lib/files.js +++ b/lib/files.js @@ -55,7 +55,7 @@ function loadViewsSync(app, sourceFilename, namespace) { function loadStylesSync(app, sourceFilename, options) { if (options == null) { - options = { compress: util.isProduction } + options = { compress: util.isProduction }; } var resolved = resolve.sync(sourceFilename, { extensions: app.styleExtensions,