Skip to content

Commit 1af0573

Browse files
committed
JavaScript: avoid assignment in if
1 parent 045c719 commit 1af0573

File tree

20 files changed

+105
-105
lines changed

20 files changed

+105
-105
lines changed

assets/javascripts/app/db.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ app.DB = class DB {
150150
}
151151

152152
onUpgradeNeeded(event) {
153-
let db;
154-
if (!(db = event.target.result)) {
153+
const db = event.target.result;
154+
if (!db) {
155155
return;
156156
}
157157

@@ -351,7 +351,7 @@ app.DB = class DB {
351351
load(entry, onSuccess, onError) {
352352
if (this.shouldLoadWithIDB(entry)) {
353353
return this.loadWithIDB(entry, onSuccess, () =>
354-
this.loadWithXHR(entry, onSuccess, onError),
354+
this.loadWithXHR(entry, onSuccess, onError)
355355
);
356356
} else {
357357
return this.loadWithXHR(entry, onSuccess, onError);
@@ -418,8 +418,8 @@ app.DB = class DB {
418418

419419
const req = txn.objectStore("docs").openCursor();
420420
req.onsuccess = (event) => {
421-
let cursor;
422-
if (!(cursor = event.target.result)) {
421+
const cursor = event.target.result;
422+
if (!cursor) {
423423
return;
424424
}
425425
this.cachedDocs[cursor.key] = cursor.value;

assets/javascripts/app/router.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ app.Router = class Router extends Events {
3535
}
3636

3737
before(context, next) {
38-
let res;
3938
const previousContext = this.context;
4039
this.context = context;
4140
this.trigger("before", context);
4241

43-
if ((res = next())) {
42+
const res = next();
43+
if (res) {
4444
this.context = previousContext;
4545
return res;
4646
} else {
@@ -79,27 +79,29 @@ app.Router = class Router extends Events {
7979
}
8080

8181
entry(context, next) {
82-
let entry;
8382
const doc = app.docs.findBySlug(context.params.doc);
8483
if (!doc) {
8584
return next();
8685
}
8786
let { path } = context.params;
8887
const { hash } = context;
8988

90-
if ((entry = doc.findEntryByPathAndHash(path, hash))) {
89+
let entry = doc.findEntryByPathAndHash(path, hash);
90+
if (entry) {
9191
context.doc = doc;
9292
context.entry = entry;
9393
this.triggerRoute("entry");
9494
return;
9595
} else if (path.slice(-6) === "/index") {
9696
path = path.substr(0, path.length - 6);
97-
if ((entry = doc.findEntryByPathAndHash(path, hash))) {
97+
entry = doc.findEntryByPathAndHash(path, hash);
98+
if (entry) {
9899
return entry.fullPath();
99100
}
100101
} else {
101102
path = `${path}/index`;
102-
if ((entry = doc.findEntryByPathAndHash(path, hash))) {
103+
entry = doc.findEntryByPathAndHash(path, hash);
104+
if (entry) {
103105
return entry.fullPath();
104106
}
105107
}
@@ -169,10 +171,8 @@ app.Router = class Router extends Events {
169171

170172
setInitialPath() {
171173
// Remove superfluous forward slashes at the beginning of the path
172-
let path;
173-
if (
174-
(path = location.pathname.replace(/^\/{2,}/g, "/")) !== location.pathname
175-
) {
174+
let path = location.pathname.replace(/^\/{2,}/g, "/");
175+
if (path !== location.pathname) {
176176
page.replace(path + location.search + location.hash, null, true);
177177
}
178178

@@ -192,8 +192,8 @@ app.Router = class Router extends Events {
192192
}
193193

194194
getInitialPathFromCookie() {
195-
let path;
196-
if ((path = Cookies.get("initial_path"))) {
195+
const path = Cookies.get("initial_path");
196+
if (path) {
197197
Cookies.expire("initial_path");
198198
return path;
199199
}
@@ -203,7 +203,7 @@ app.Router = class Router extends Events {
203203
page.replace(
204204
location.pathname + location.search + (hash || ""),
205205
null,
206-
true,
206+
true
207207
);
208208
}
209209
};

assets/javascripts/lib/ajax.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ function ajax(options) {
101101

102102
function onComplete(xhr, options) {
103103
if (200 <= xhr.status && xhr.status < 300) {
104-
let response;
105-
if ((response = parseResponse(xhr, options)) != null) {
104+
const response = parseResponse(xhr, options);
105+
if (response != null) {
106106
onSuccess(response, xhr, options);
107107
} else {
108108
onError("invalid", xhr, options);

assets/javascripts/lib/page.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ page.stop = function () {
4242
};
4343

4444
page.show = function (path, state) {
45-
let res;
4645
if (path === currentState?.path) {
4746
return;
4847
}
4948
const context = new Context(path, state);
5049
const previousState = currentState;
5150
currentState = context.state;
52-
if ((res = page.dispatch(context))) {
51+
const res = page.dispatch(context);
52+
if (res) {
5353
currentState = previousState;
5454
location.assign(res);
5555
} else {
@@ -84,12 +84,9 @@ page.replace = function (path, state, skipDispatch, init) {
8484

8585
page.dispatch = function (context) {
8686
let i = 0;
87-
var next = function () {
88-
let fn, res;
89-
if ((fn = callbacks[i++])) {
90-
res = fn(context, next);
91-
}
92-
return res;
87+
const next = function () {
88+
let fn = callbacks[i++];
89+
return fn?.(context, next);
9390
};
9491
return next();
9592
};
@@ -170,8 +167,8 @@ class Route {
170167

171168
middleware(fn) {
172169
return (context, next) => {
173-
let params;
174-
if (this.match(context.pathname, (params = []))) {
170+
let params = [];
171+
if (this.match(context.pathname, params)) {
175172
context.params = params;
176173
return fn(context, next);
177174
} else {
@@ -181,19 +178,19 @@ class Route {
181178
}
182179

183180
match(path, params) {
184-
let matchData;
185-
if (!(matchData = this.regexp.exec(path))) {
181+
const matchData = this.regexp.exec(path);
182+
if (!matchData) {
186183
return;
187184
}
188185

189186
const iterable = matchData.slice(1);
190187
for (let i = 0; i < iterable.length; i++) {
191-
var key;
188+
var key = this.keys[i];
192189
var value = iterable[i];
193190
if (typeof value === "string") {
194191
value = decodeURIComponent(value);
195192
}
196-
if ((key = this.keys[i])) {
193+
if (key) {
197194
params[key.name] = value;
198195
} else {
199196
params.push(value);

assets/javascripts/lib/util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@ $.scrollToWithImageLock = function (el, parent, ...args) {
340340

341341
// Calls the function while locking the element's position relative to the window.
342342
$.lockScroll = function (el, fn) {
343-
let parent;
344-
if ((parent = $.scrollParent(el))) {
343+
const parent = $.scrollParent(el);
344+
if (parent) {
345345
let { top } = $.rect(el);
346346
if (![document.body, document.documentElement].includes(parent)) {
347347
top -= $.rect(parent).top;

assets/javascripts/models/doc.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ app.models.Doc = class Doc extends app.Model {
7272
}
7373

7474
findEntryByPathAndHash(path, hash) {
75-
let entry;
76-
if (hash && (entry = this.entries.findBy("path", `${path}#${hash}`))) {
75+
const entry = hash && this.entries.findBy("path", `${path}#${hash}`);
76+
if (entry) {
7777
return entry;
7878
} else if (path === "index") {
7979
return this.toEntry();
@@ -110,8 +110,8 @@ app.models.Doc = class Doc extends app.Model {
110110
}
111111

112112
_loadFromCache(onSuccess) {
113-
let data;
114-
if (!(data = this._getCache())) {
113+
const data = this._getCache();
114+
if (!data) {
115115
return;
116116
}
117117

@@ -125,8 +125,8 @@ app.models.Doc = class Doc extends app.Model {
125125
}
126126

127127
_getCache() {
128-
let data;
129-
if (!(data = app.localStorage.get(this.slug))) {
128+
const data = app.localStorage.get(this.slug);
129+
if (!data) {
130130
return;
131131
}
132132

assets/javascripts/views/content/offline_page.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ app.views.OfflinePage = class OfflinePage extends app.View {
6868
}
6969

7070
onClick(event) {
71-
let action;
7271
let el = $.eventTarget(event);
73-
if ((action = el.getAttribute("data-action"))) {
72+
let action = el.getAttribute("data-action");
73+
if (action) {
7474
const doc = this.docByEl(el);
7575
if (action === "update") {
7676
action = "install";
7777
}
7878
doc[action](
7979
this.onInstallSuccess.bind(this, doc),
8080
this.onInstallError.bind(this, doc),
81-
this.onInstallProgress.bind(this, doc),
81+
this.onInstallProgress.bind(this, doc)
8282
);
8383
el.parentNode.innerHTML = `${el.textContent.replace(/e$/, "")}ing…`;
8484
} else if (
@@ -101,11 +101,11 @@ app.views.OfflinePage = class OfflinePage extends app.View {
101101
return;
102102
}
103103
doc.getInstallStatus((status) => {
104-
let el;
105104
if (!this.activated) {
106105
return;
107106
}
108-
if ((el = this.docEl(doc))) {
107+
const el = this.docEl(doc);
108+
if (el) {
109109
el.outerHTML = this.renderDoc(doc, status);
110110
$.highlight(el, { className: "_highlight" });
111111
this.refreshLinks();
@@ -114,25 +114,25 @@ app.views.OfflinePage = class OfflinePage extends app.View {
114114
}
115115

116116
onInstallError(doc) {
117-
let el;
118117
if (!this.activated) {
119118
return;
120119
}
121-
if ((el = this.docEl(doc))) {
120+
const el = this.docEl(doc);
121+
if (el) {
122122
el.lastElementChild.textContent = "Error";
123123
}
124124
}
125125

126126
onInstallProgress(doc, event) {
127-
let el;
128127
if (!this.activated || !event.lengthComputable) {
129128
return;
130129
}
131-
if ((el = this.docEl(doc))) {
130+
const el = this.docEl(doc);
131+
if (el) {
132132
const percentage = Math.round((event.loaded * 100) / event.total);
133133
el.lastElementChild.textContent = el.lastElementChild.textContent.replace(
134134
/(\s.+)?$/,
135-
` (${percentage}%)`,
135+
` (${percentage}%)`
136136
);
137137
}
138138
}

assets/javascripts/views/layout/mobile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ app.views.Mobile = class Mobile extends app.View {
7676
}
7777

7878
showSidebar() {
79-
let selection;
8079
if (this.isSidebarShown()) {
8180
window.scrollTo(0, 0);
8281
return;
@@ -86,7 +85,8 @@ app.views.Mobile = class Mobile extends app.View {
8685
this.content.style.display = "none";
8786
this.sidebar.style.display = "block";
8887

89-
if ((selection = this.findByClass(app.views.ListSelect.activeClass))) {
88+
const selection = this.findByClass(app.views.ListSelect.activeClass);
89+
if (selection) {
9090
const scrollContainer =
9191
window.scrollY === this.body.scrollTop
9292
? this.body

assets/javascripts/views/layout/path.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ app.views.Path = class Path extends app.View {
2424
}
2525

2626
onClick(event) {
27-
let link;
28-
if ((link = $.closestLink(event.target, this.el))) {
27+
const link = $.closestLink(event.target, this.el);
28+
if (link) {
2929
this.clicked = true;
3030
}
3131
}

0 commit comments

Comments
 (0)