From c9acf5e7e9e16fdd34cb2de882d627f97364a952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Sat, 20 May 2017 20:28:13 +0200 Subject: [PATCH] Add eslint to project Closes #524 --- .eslintrc | 15 +++++++++++++++ .gitignore | 1 + jquery.pjax.js | 32 ++++++++++++++++++-------------- package.json | 5 ++++- script/bootstrap | 1 + script/test | 4 ++-- 6 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 .eslintrc create mode 100644 .gitignore diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 00000000..c27520f3 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,15 @@ +{ "extends": "eslint:recommended", + "env": { + "browser": true, + "jquery": true + }, + "rules": { + "eqeqeq": "warn", + "no-eval": "error", + "no-extra-parens": "error", + "no-implicit-globals": "error", + "no-trailing-spaces": "error", + "no-unused-expressions": "error", + "semi": ["error", "never"] + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..c2658d7d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/jquery.pjax.js b/jquery.pjax.js index f14d6e01..d57269d4 100644 --- a/jquery.pjax.js +++ b/jquery.pjax.js @@ -253,7 +253,7 @@ function pjax(options) { // If $.pjax.defaults.version is a function, invoke it first. // Otherwise it can be a static string. - var currentVersion = (typeof $.pjax.defaults.version === 'function') ? + var currentVersion = typeof $.pjax.defaults.version === 'function' ? $.pjax.defaults.version() : $.pjax.defaults.version @@ -299,7 +299,7 @@ function pjax(options) { if (blurFocus) { try { document.activeElement.blur() - } catch (e) { } + } catch (e) { /* ignore */ } } if (container.title) document.title = container.title @@ -492,7 +492,7 @@ function onPjaxPopstate(event) { // Force reflow/relayout before the browser tries to restore the // scroll position. - container[0].offsetHeight + container[0].offsetHeight // eslint-disable-line no-unused-expressions } else { locationReplace(location.href) } @@ -566,7 +566,7 @@ function cloneContents(container) { // Unmark script tags as already being eval'd so they can get executed again // when restored from cache. HAXX: Uses jQuery internal method. cloned.find('script').each(function(){ - if (!this.src) jQuery._data(this, 'globalEval', false) + if (!this.src) $._data(this, 'globalEval', false) }) return cloned.contents() } @@ -665,13 +665,14 @@ function extractContainer(data, xhr, options) { var serverUrl = xhr.getResponseHeader('X-PJAX-URL') obj.url = serverUrl ? stripInternalParams(parseURL(serverUrl)) : options.requestUrl + var $head, $body // Attempt to parse response html into elements if (fullDocument) { - var $body = $(parseHTML(data.match(/]*>([\s\S.]*)<\/body>/i)[0])) + $body = $(parseHTML(data.match(/]*>([\s\S.]*)<\/body>/i)[0])) var head = data.match(/]*>([\s\S.]*)<\/head>/i) - var $head = head != null ? $(parseHTML(head[0])) : $body + $head = head != null ? $(parseHTML(head[0])) : $body } else { - var $head = $body = $(parseHTML(data)) + $head = $body = $(parseHTML(data)) } // If response data is empty, return fast @@ -683,12 +684,11 @@ function extractContainer(data, xhr, options) { obj.title = findAll($head, 'title').last().text() if (options.fragment) { + var $fragment = $body // If they specified a fragment, look for it in the response // and pull it out. - if (options.fragment === 'body') { - var $fragment = $body - } else { - var $fragment = findAll($body, options.fragment).first() + if (options.fragment !== 'body') { + $fragment = findAll($fragment, options.fragment).first() } if ($fragment.length) { @@ -797,8 +797,8 @@ function cachePop(direction, id, value) { } pushStack.push(id) - if (id = popStack.pop()) - delete cacheMapping[id] + id = popStack.pop() + if (id) delete cacheMapping[id] // Trim whichever stack we just pushed to to max cache length. trimCacheStack(pushStack, pjax.defaults.maxCacheLength) @@ -894,6 +894,10 @@ $.support.pjax = // pushState isn't reliable on iOS until 5. !navigator.userAgent.match(/((iPod|iPhone|iPad).+\bOS\s+[1-4]\D|WebApps\/.+CFNetwork)/) -$.support.pjax ? enable() : disable() +if ($.support.pjax) { + enable() +} else { + disable() +} })(jQuery) diff --git a/package.json b/package.json index 9af6b447..7cf767b0 100644 --- a/package.json +++ b/package.json @@ -12,5 +12,8 @@ "files" : [ "LICENSE", "jquery.pjax.js" - ] + ], + "devDependencies": { + "eslint": "^3.19.0" + } } diff --git a/script/bootstrap b/script/bootstrap index be9eaf0c..7f219766 100755 --- a/script/bootstrap +++ b/script/bootstrap @@ -1,6 +1,7 @@ #!/usr/bin/env bash set -e +npm install bundle install if phantom_version="$(phantomjs --version)"; then diff --git a/script/test b/script/test index 70f6e458..581f9a86 100755 --- a/script/test +++ b/script/test @@ -1,6 +1,8 @@ #!/usr/bin/env bash set -e +./node_modules/.bin/eslint *.js + port=3999 script/server -p "$port" &>/dev/null & pid=$! @@ -11,8 +13,6 @@ while ! lsof -i :$port >/dev/null; do sleep .05 done -[ -z "$CI" ] || echo "PhantomJS $(phantomjs --version)" - phantomjs ./test/run-qunit.js \ "http://localhost:$port/?jquery=3.2" \ "http://localhost:$port/?jquery=2.2" \