[assets] Add rules to ESLint:

- Fix long line.
- Enforce a few stylistic habits:

    - Avoid some potential dangerous constructs.
    - `arrow-spacing`: Use at least one space around arrows.
    - `keyword-spacing`: Use at least one space around keywords (if, else, for…).
    - `no-multiple-empty-lines`: Only use one empty line between code.
	- `no-var`: Use `let` or `const` instead of `var`:
    - `padded-blocks`: Do not pad blocks.
    - `padding-line-between-statements`: Use empty lines between some statements.
    - `space-before-blocks`: Use at least one space before the opening brace of a block.
This commit is contained in:
Fabien Basmaison 2021-04-06 10:42:52 +02:00
parent 991d897ac7
commit 70c652d565
3 changed files with 105 additions and 23 deletions

View file

@ -9,6 +9,64 @@ module.exports = {
"extends": "eslint:recommended", "extends": "eslint:recommended",
"rules": { "rules": {
"strict": "error" // Possible Errors
"no-async-promise-executor": "error",
"no-await-in-loop": "error",
"no-class-assign": "error",
"no-confusing-arrow": "error",
"no-const-assign": "error",
"no-dupe-class-members": "error",
"no-duplicate-imports": "error",
"no-template-curly-in-string": "error",
"no-useless-computed-key": "error",
"no-useless-constructor": "error",
"no-useless-rename": "error",
"require-atomic-updates": "error",
// Best practices
"strict": "error",
"no-var": "error",
// Stylistic Issues
"arrow-spacing": "error",
"keyword-spacing": "error",
"no-multiple-empty-lines": [
"error",
{
"max": 1,
},
],
"padded-blocks": [
"error",
"never",
],
"padding-line-between-statements": [
"error",
{
// always before return
"blankLine": "always",
"prev": "*",
"next": "return",
},
{
// always before block-like expressions
"blankLine": "always",
"prev": "*",
"next": "block-like",
},
{
// always after variable declaration
"blankLine": "always",
"prev": [ "const", "let", "var" ],
"next": "*",
},
{
// not necessary between variable declaration
"blankLine": "any",
"prev": [ "const", "let", "var" ],
"next": [ "const", "let", "var" ],
},
],
"space-before-blocks": "error",
} }
}; };

View file

@ -56,8 +56,10 @@ let BookWyrm = new class {
polling(el, delay) { polling(el, delay) {
let poller = this; let poller = this;
delay = delay || 10000; delay = delay || 10000;
delay += (Math.random() * 1000); delay += (Math.random() * 1000);
setTimeout(function() { setTimeout(function() {
fetch('/api/updates/' + el.getAttribute('data-poll')) fetch('/api/updates/' + el.getAttribute('data-poll'))
.then(response => response.json()) .then(response => response.json())
@ -69,6 +71,7 @@ let BookWyrm = new class {
updateCountElement(el, data) { updateCountElement(el, data) {
const currentCount = el.innerText; const currentCount = el.innerText;
const count = data.count; const count = data.count;
if (count != currentCount) { if (count != currentCount) {
this.addRemoveClass(el.closest('[data-poll-wrapper]'), 'hidden', count < 1); this.addRemoveClass(el.closest('[data-poll-wrapper]'), 'hidden', count < 1);
el.innerText = count; el.innerText = count;
@ -76,52 +79,62 @@ let BookWyrm = new class {
} }
revealForm(e) { revealForm(e) {
var hidden = e.currentTarget.closest('.hidden-form').getElementsByClassName('hidden')[0]; let hidden = e.currentTarget.closest('.hidden-form').getElementsByClassName('hidden')[0];
if (hidden) { if (hidden) {
this.removeClass(hidden, 'hidden'); this.removeClass(hidden, 'hidden');
} }
} }
toggleAction(e) { toggleAction(e) {
var el = e.currentTarget; let el = e.currentTarget;
var pressed = el.getAttribute('aria-pressed') == 'false'; let pressed = el.getAttribute('aria-pressed') == 'false';
let targetId = el.getAttribute('data-controls');
var targetId = el.getAttribute('data-controls');
document.querySelectorAll('[data-controls="' + targetId + '"]') document.querySelectorAll('[data-controls="' + targetId + '"]')
.forEach(t => t.setAttribute('aria-pressed', (t.getAttribute('aria-pressed') == 'false'))); .forEach(t => {
t.setAttribute('aria-pressed', (t.getAttribute('aria-pressed') == 'false'))
});
if (targetId) { if (targetId) {
var target = document.getElementById(targetId); let target = document.getElementById(targetId);
this.addRemoveClass(target, 'hidden', !pressed); this.addRemoveClass(target, 'hidden', !pressed);
this.addRemoveClass(target, 'is-active', pressed); this.addRemoveClass(target, 'is-active', pressed);
} }
// show/hide container // show/hide container
var container = document.getElementById('hide-' + targetId); let container = document.getElementById('hide-' + targetId);
if (container) { if (container) {
this.addRemoveClass(container, 'hidden', pressed); this.addRemoveClass(container, 'hidden', pressed);
} }
// set checkbox, if appropriate // set checkbox, if appropriate
var checkbox = el.getAttribute('data-controls-checkbox'); let checkbox = el.getAttribute('data-controls-checkbox');
if (checkbox) { if (checkbox) {
document.getElementById(checkbox).checked = !!pressed; document.getElementById(checkbox).checked = !!pressed;
} }
// set focus, if appropriate // set focus, if appropriate
var focus = el.getAttribute('data-focus-target'); let focus = el.getAttribute('data-focus-target');
if (focus) { if (focus) {
var focusEl = document.getElementById(focus); let focusEl = document.getElementById(focus);
focusEl.focus(); focusEl.focus();
setTimeout(function(){ focusEl.selectionStart = focusEl.selectionEnd = 10000; }, 0); setTimeout(function() { focusEl.selectionStart = focusEl.selectionEnd = 10000; }, 0);
} }
} }
// @todo Only update status if the promise is successful. // @todo Only update status if the promise is successful.
interact(e) { interact(e) {
e.preventDefault(); e.preventDefault();
let identifier = e.target.getAttribute('data-id');
this.ajaxPost(e.target); this.ajaxPost(e.target);
var identifier = e.target.getAttribute('data-id');
// @todo This probably should be done with IDs. // @todo This probably should be done with IDs.
document.querySelectorAll(`.${identifier}`) document.querySelectorAll(`.${identifier}`)
@ -129,12 +142,15 @@ let BookWyrm = new class {
} }
toggleMenu(e) { toggleMenu(e) {
var el = e.currentTarget; let el = e.currentTarget;
var expanded = el.getAttribute('aria-expanded') == 'false'; let expanded = el.getAttribute('aria-expanded') == 'false';
let targetId = el.getAttribute('data-controls');
el.setAttribute('aria-expanded', expanded); el.setAttribute('aria-expanded', expanded);
var targetId = el.getAttribute('data-controls');
if (targetId) { if (targetId) {
var target = document.getElementById(targetId); let target = document.getElementById(targetId);
this.addRemoveClass(target, 'is-active', expanded); this.addRemoveClass(target, 'is-active', expanded);
} }
} }
@ -155,22 +171,28 @@ let BookWyrm = new class {
} }
addClass(el, classname) { addClass(el, classname) {
var classes = el.className.split(' '); let classes = el.className.split(' ');
if (classes.indexOf(classname) > -1) { if (classes.indexOf(classname) > -1) {
return; return;
} }
el.className = classes.concat(classname).join(' '); el.className = classes.concat(classname).join(' ');
} }
removeClass(el, className) { removeClass(el, className) {
var classes = []; let classes = [];
if (el.className) { if (el.className) {
classes = el.className.split(' '); classes = el.className.split(' ');
} }
const idx = classes.indexOf(className); const idx = classes.indexOf(className);
if (idx > -1) { if (idx > -1) {
classes.splice(idx, 1); classes.splice(idx, 1);
} }
el.className = classes.join(' '); el.className = classes.join(' ');
} }
} }

View file

@ -15,8 +15,9 @@ let LocalStorageTools = new class {
// set javascript listeners // set javascript listeners
updateDisplay(e) { updateDisplay(e) {
// used in set reading goal // used in set reading goal
var key = e.target.getAttribute('data-id'); let key = e.target.getAttribute('data-id');
var value = e.target.getAttribute('data-value'); let value = e.target.getAttribute('data-value');
window.localStorage.setItem(key, value); window.localStorage.setItem(key, value);
document.querySelectorAll('[data-hide="' + key + '"]') document.querySelectorAll('[data-hide="' + key + '"]')
@ -25,8 +26,9 @@ let LocalStorageTools = new class {
setDisplay(el) { setDisplay(el) {
// used in set reading goal // used in set reading goal
var key = el.getAttribute('data-hide'); let key = el.getAttribute('data-hide');
var value = window.localStorage.getItem(key); let value = window.localStorage.getItem(key);
BookWyrm.addRemoveClass(el, 'hidden', value); BookWyrm.addRemoveClass(el, 'hidden', value);
} }
} }