Forráskód Böngészése

JavaScript: avoid assignment in if

Simon Legner 1 hónapja
szülő
commit
1af057324c

+ 5 - 5
assets/javascripts/app/db.js

@@ -150,8 +150,8 @@ app.DB = class DB {
   }
 
   onUpgradeNeeded(event) {
-    let db;
-    if (!(db = event.target.result)) {
+    const db = event.target.result;
+    if (!db) {
       return;
     }
 
@@ -351,7 +351,7 @@ app.DB = class DB {
   load(entry, onSuccess, onError) {
     if (this.shouldLoadWithIDB(entry)) {
       return this.loadWithIDB(entry, onSuccess, () =>
-        this.loadWithXHR(entry, onSuccess, onError),
+        this.loadWithXHR(entry, onSuccess, onError)
       );
     } else {
       return this.loadWithXHR(entry, onSuccess, onError);
@@ -418,8 +418,8 @@ app.DB = class DB {
 
     const req = txn.objectStore("docs").openCursor();
     req.onsuccess = (event) => {
-      let cursor;
-      if (!(cursor = event.target.result)) {
+      const cursor = event.target.result;
+      if (!cursor) {
         return;
       }
       this.cachedDocs[cursor.key] = cursor.value;

+ 13 - 13
assets/javascripts/app/router.js

@@ -35,12 +35,12 @@ app.Router = class Router extends Events {
   }
 
   before(context, next) {
-    let res;
     const previousContext = this.context;
     this.context = context;
     this.trigger("before", context);
 
-    if ((res = next())) {
+    const res = next();
+    if (res) {
       this.context = previousContext;
       return res;
     } else {
@@ -79,7 +79,6 @@ app.Router = class Router extends Events {
   }
 
   entry(context, next) {
-    let entry;
     const doc = app.docs.findBySlug(context.params.doc);
     if (!doc) {
       return next();
@@ -87,19 +86,22 @@ app.Router = class Router extends Events {
     let { path } = context.params;
     const { hash } = context;
 
-    if ((entry = doc.findEntryByPathAndHash(path, hash))) {
+    let entry = doc.findEntryByPathAndHash(path, hash);
+    if (entry) {
       context.doc = doc;
       context.entry = entry;
       this.triggerRoute("entry");
       return;
     } else if (path.slice(-6) === "/index") {
       path = path.substr(0, path.length - 6);
-      if ((entry = doc.findEntryByPathAndHash(path, hash))) {
+      entry = doc.findEntryByPathAndHash(path, hash);
+      if (entry) {
         return entry.fullPath();
       }
     } else {
       path = `${path}/index`;
-      if ((entry = doc.findEntryByPathAndHash(path, hash))) {
+      entry = doc.findEntryByPathAndHash(path, hash);
+      if (entry) {
         return entry.fullPath();
       }
     }
@@ -169,10 +171,8 @@ app.Router = class Router extends Events {
 
   setInitialPath() {
     // Remove superfluous forward slashes at the beginning of the path
-    let path;
-    if (
-      (path = location.pathname.replace(/^\/{2,}/g, "/")) !== location.pathname
-    ) {
+    let path = location.pathname.replace(/^\/{2,}/g, "/");
+    if (path !== location.pathname) {
       page.replace(path + location.search + location.hash, null, true);
     }
 
@@ -192,8 +192,8 @@ app.Router = class Router extends Events {
   }
 
   getInitialPathFromCookie() {
-    let path;
-    if ((path = Cookies.get("initial_path"))) {
+    const path = Cookies.get("initial_path");
+    if (path) {
       Cookies.expire("initial_path");
       return path;
     }
@@ -203,7 +203,7 @@ app.Router = class Router extends Events {
     page.replace(
       location.pathname + location.search + (hash || ""),
       null,
-      true,
+      true
     );
   }
 };

+ 2 - 2
assets/javascripts/lib/ajax.js

@@ -101,8 +101,8 @@ function ajax(options) {
 
   function onComplete(xhr, options) {
     if (200 <= xhr.status && xhr.status < 300) {
-      let response;
-      if ((response = parseResponse(xhr, options)) != null) {
+      const response = parseResponse(xhr, options);
+      if (response != null) {
         onSuccess(response, xhr, options);
       } else {
         onError("invalid", xhr, options);

+ 11 - 14
assets/javascripts/lib/page.js

@@ -42,14 +42,14 @@ page.stop = function () {
 };
 
 page.show = function (path, state) {
-  let res;
   if (path === currentState?.path) {
     return;
   }
   const context = new Context(path, state);
   const previousState = currentState;
   currentState = context.state;
-  if ((res = page.dispatch(context))) {
+  const res = page.dispatch(context);
+  if (res) {
     currentState = previousState;
     location.assign(res);
   } else {
@@ -84,12 +84,9 @@ page.replace = function (path, state, skipDispatch, init) {
 
 page.dispatch = function (context) {
   let i = 0;
-  var next = function () {
-    let fn, res;
-    if ((fn = callbacks[i++])) {
-      res = fn(context, next);
-    }
-    return res;
+  const next = function () {
+    let fn = callbacks[i++];
+    return fn?.(context, next);
   };
   return next();
 };
@@ -170,8 +167,8 @@ class Route {
 
   middleware(fn) {
     return (context, next) => {
-      let params;
-      if (this.match(context.pathname, (params = []))) {
+      let params = [];
+      if (this.match(context.pathname, params)) {
         context.params = params;
         return fn(context, next);
       } else {
@@ -181,19 +178,19 @@ class Route {
   }
 
   match(path, params) {
-    let matchData;
-    if (!(matchData = this.regexp.exec(path))) {
+    const matchData = this.regexp.exec(path);
+    if (!matchData) {
       return;
     }
 
     const iterable = matchData.slice(1);
     for (let i = 0; i < iterable.length; i++) {
-      var key;
+      var key = this.keys[i];
       var value = iterable[i];
       if (typeof value === "string") {
         value = decodeURIComponent(value);
       }
-      if ((key = this.keys[i])) {
+      if (key) {
         params[key.name] = value;
       } else {
         params.push(value);

+ 2 - 2
assets/javascripts/lib/util.js

@@ -340,8 +340,8 @@ $.scrollToWithImageLock = function (el, parent, ...args) {
 
 // Calls the function while locking the element's position relative to the window.
 $.lockScroll = function (el, fn) {
-  let parent;
-  if ((parent = $.scrollParent(el))) {
+  const parent = $.scrollParent(el);
+  if (parent) {
     let { top } = $.rect(el);
     if (![document.body, document.documentElement].includes(parent)) {
       top -= $.rect(parent).top;

+ 6 - 6
assets/javascripts/models/doc.js

@@ -72,8 +72,8 @@ app.models.Doc = class Doc extends app.Model {
   }
 
   findEntryByPathAndHash(path, hash) {
-    let entry;
-    if (hash && (entry = this.entries.findBy("path", `${path}#${hash}`))) {
+    const entry = hash && this.entries.findBy("path", `${path}#${hash}`);
+    if (entry) {
       return entry;
     } else if (path === "index") {
       return this.toEntry();
@@ -110,8 +110,8 @@ app.models.Doc = class Doc extends app.Model {
   }
 
   _loadFromCache(onSuccess) {
-    let data;
-    if (!(data = this._getCache())) {
+    const data = this._getCache();
+    if (!data) {
       return;
     }
 
@@ -125,8 +125,8 @@ app.models.Doc = class Doc extends app.Model {
   }
 
   _getCache() {
-    let data;
-    if (!(data = app.localStorage.get(this.slug))) {
+    const data = app.localStorage.get(this.slug);
+    if (!data) {
       return;
     }
 

+ 10 - 10
assets/javascripts/views/content/offline_page.js

@@ -68,9 +68,9 @@ app.views.OfflinePage = class OfflinePage extends app.View {
   }
 
   onClick(event) {
-    let action;
     let el = $.eventTarget(event);
-    if ((action = el.getAttribute("data-action"))) {
+    let action = el.getAttribute("data-action");
+    if (action) {
       const doc = this.docByEl(el);
       if (action === "update") {
         action = "install";
@@ -78,7 +78,7 @@ app.views.OfflinePage = class OfflinePage extends app.View {
       doc[action](
         this.onInstallSuccess.bind(this, doc),
         this.onInstallError.bind(this, doc),
-        this.onInstallProgress.bind(this, doc),
+        this.onInstallProgress.bind(this, doc)
       );
       el.parentNode.innerHTML = `${el.textContent.replace(/e$/, "")}ing…`;
     } else if (
@@ -101,11 +101,11 @@ app.views.OfflinePage = class OfflinePage extends app.View {
       return;
     }
     doc.getInstallStatus((status) => {
-      let el;
       if (!this.activated) {
         return;
       }
-      if ((el = this.docEl(doc))) {
+      const el = this.docEl(doc);
+      if (el) {
         el.outerHTML = this.renderDoc(doc, status);
         $.highlight(el, { className: "_highlight" });
         this.refreshLinks();
@@ -114,25 +114,25 @@ app.views.OfflinePage = class OfflinePage extends app.View {
   }
 
   onInstallError(doc) {
-    let el;
     if (!this.activated) {
       return;
     }
-    if ((el = this.docEl(doc))) {
+    const el = this.docEl(doc);
+    if (el) {
       el.lastElementChild.textContent = "Error";
     }
   }
 
   onInstallProgress(doc, event) {
-    let el;
     if (!this.activated || !event.lengthComputable) {
       return;
     }
-    if ((el = this.docEl(doc))) {
+    const el = this.docEl(doc);
+    if (el) {
       const percentage = Math.round((event.loaded * 100) / event.total);
       el.lastElementChild.textContent = el.lastElementChild.textContent.replace(
         /(\s.+)?$/,
-        ` (${percentage}%)`,
+        ` (${percentage}%)`
       );
     }
   }

+ 2 - 2
assets/javascripts/views/layout/mobile.js

@@ -76,7 +76,6 @@ app.views.Mobile = class Mobile extends app.View {
   }
 
   showSidebar() {
-    let selection;
     if (this.isSidebarShown()) {
       window.scrollTo(0, 0);
       return;
@@ -86,7 +85,8 @@ app.views.Mobile = class Mobile extends app.View {
     this.content.style.display = "none";
     this.sidebar.style.display = "block";
 
-    if ((selection = this.findByClass(app.views.ListSelect.activeClass))) {
+    const selection = this.findByClass(app.views.ListSelect.activeClass);
+    if (selection) {
       const scrollContainer =
         window.scrollY === this.body.scrollTop
           ? this.body

+ 2 - 2
assets/javascripts/views/layout/path.js

@@ -24,8 +24,8 @@ app.views.Path = class Path extends app.View {
   }
 
   onClick(event) {
-    let link;
-    if ((link = $.closestLink(event.target, this.el))) {
+    const link = $.closestLink(event.target, this.el);
+    if (link) {
       this.clicked = true;
     }
   }

+ 18 - 18
assets/javascripts/views/list/list_focus.js

@@ -31,8 +31,8 @@ app.views.ListFocus = class ListFocus extends app.View {
   }
 
   blur() {
-    let cursor;
-    if ((cursor = this.getCursor())) {
+    const cursor = this.getCursor();
+    if (cursor) {
       cursor.classList.remove(this.constructor.activeClass);
       $.trigger(cursor, "blur");
     }
@@ -46,8 +46,8 @@ app.views.ListFocus = class ListFocus extends app.View {
   }
 
   findNext(cursor) {
-    let next;
-    if ((next = cursor.nextSibling)) {
+    const next = cursor.nextSibling;
+    if (next) {
       if (next.tagName === "A") {
         return next;
       } else if (next.tagName === "SPAN") {
@@ -71,8 +71,8 @@ app.views.ListFocus = class ListFocus extends app.View {
   }
 
   findFirst(cursor) {
-    let first;
-    if (!(first = cursor.firstChild)) {
+    const first = cursor.firstChild;
+    if (!first) {
       return;
     }
 
@@ -86,8 +86,8 @@ app.views.ListFocus = class ListFocus extends app.View {
   }
 
   findPrev(cursor) {
-    let prev;
-    if ((prev = cursor.previousSibling)) {
+    const prev = cursor.previousSibling;
+    if (prev) {
       if (prev.tagName === "A") {
         return prev;
       } else if (prev.tagName === "SPAN") {
@@ -111,8 +111,8 @@ app.views.ListFocus = class ListFocus extends app.View {
   }
 
   findLast(cursor) {
-    let last;
-    if (!(last = cursor.lastChild)) {
+    const last = cursor.lastChild;
+    if (!last) {
       return;
     }
 
@@ -128,8 +128,8 @@ app.views.ListFocus = class ListFocus extends app.View {
   }
 
   onDown() {
-    let cursor;
-    if ((cursor = this.getCursor())) {
+    const cursor = this.getCursor();
+    if (cursor) {
       this.focusOnNextFrame(this.findNext(cursor));
     } else {
       this.focusOnNextFrame(this.findByTag("a"));
@@ -137,8 +137,8 @@ app.views.ListFocus = class ListFocus extends app.View {
   }
 
   onUp() {
-    let cursor;
-    if ((cursor = this.getCursor())) {
+    const cursor = this.getCursor();
+    if (cursor) {
       this.focusOnNextFrame(this.findPrev(cursor));
     } else {
       this.focusOnNextFrame(this.findLastByTag("a"));
@@ -160,15 +160,15 @@ app.views.ListFocus = class ListFocus extends app.View {
   }
 
   onEnter() {
-    let cursor;
-    if ((cursor = this.getCursor())) {
+    const cursor = this.getCursor();
+    if (cursor) {
       $.click(cursor);
     }
   }
 
   onSuperEnter() {
-    let cursor;
-    if ((cursor = this.getCursor())) {
+    const cursor = this.getCursor();
+    if (cursor) {
       $.popup(cursor);
     }
   }

+ 2 - 2
assets/javascripts/views/list/list_select.js

@@ -18,8 +18,8 @@ app.views.ListSelect = class ListSelect extends app.View {
   }
 
   deselect() {
-    let selection;
-    if ((selection = this.getSelection())) {
+    const selection = this.getSelection();
+    if (selection) {
       selection.classList.remove(this.constructor.activeClass);
       $.trigger(selection, "deselect");
     }

+ 2 - 2
assets/javascripts/views/pages/base.js

@@ -55,8 +55,8 @@ app.views.BasePage = class BasePage extends app.View {
     }
 
     for (var el of this.highlightNodes.splice(0, this.nodesPerFrame)) {
-      var clipEl;
-      if ((clipEl = el.lastElementChild)) {
+      const clipEl = el.lastElementChild;
+      if (clipEl) {
         $.remove(clipEl);
       }
       Prism.highlightElement(el);

+ 2 - 2
assets/javascripts/views/pages/hidden.js

@@ -13,8 +13,8 @@ app.views.HiddenPage = class HiddenPage extends app.View {
   }
 
   onClick(event) {
-    let link;
-    if ((link = $.closestLink(event.target, this.el))) {
+    const link = $.closestLink(event.target, this.el);
+    if (link) {
       $.stopEvent(event);
       $.popup(link);
     }

+ 2 - 2
assets/javascripts/views/pages/jquery.js

@@ -28,13 +28,13 @@ app.views.JqueryPage = class JqueryPage extends app.views.BasePage {
   }
 
   runExample(el) {
-    let iframe;
     const source = el.getElementsByClassName("syntaxhighlighter")[0];
     if (!source || source.innerHTML.indexOf("!doctype") === -1) {
       return;
     }
 
-    if (!(iframe = el.getElementsByClassName(JqueryPage.demoClassName)[0])) {
+    let iframe = el.getElementsByClassName(JqueryPage.demoClassName)[0];
+    if (!iframe) {
       iframe = document.createElement("iframe");
       iframe.className = JqueryPage.demoClassName;
       iframe.width = "100%";

+ 4 - 3
assets/javascripts/views/pages/sqlite.js

@@ -4,11 +4,12 @@ app.views.SqlitePage = class SqlitePage extends app.views.BasePage {
   static events = { click: "onClick" };
 
   onClick(event) {
-    let el, id;
-    if (!(id = event.target.getAttribute("data-toggle"))) {
+    const id = event.target.getAttribute("data-toggle");
+    if (!id) {
       return;
     }
-    if (!(el = this.find(`#${id}`))) {
+    const el = this.find(`#${id}`);
+    if (!el) {
       return;
     }
     $.stopEvent(event);

+ 4 - 4
assets/javascripts/views/search/search.js

@@ -119,14 +119,14 @@ app.views.Search = class Search extends app.View {
   }
 
   searchUrl() {
-    let value;
     if (location.pathname === "/") {
       this.scope.searchUrl();
     } else if (!app.router.isIndex()) {
       return;
     }
 
-    if (!(value = this.extractHashValue())) {
+    const value = this.extractHashValue();
+    if (!value) {
       return;
     }
     this.input.value = this.value = value;
@@ -141,8 +141,8 @@ app.views.Search = class Search extends app.View {
   }
 
   externalSearch(url) {
-    let value;
-    if ((value = this.value)) {
+    const value = this.value;
+    if (value) {
       if (this.scope.name()) {
         value = `${this.scope.name()} ${value}`;
       }

+ 7 - 7
assets/javascripts/views/search/search_scope.js

@@ -52,15 +52,15 @@ app.views.SearchScope = class SearchScope extends app.View {
   }
 
   searchUrl() {
-    let value;
-    if ((value = this.extractHashValue())) {
+    const value = this.extractHashValue();
+    if (value) {
       this.search(value, true);
     }
   }
 
   onResults(results) {
-    let doc;
-    if (!(doc = results[0])) {
+    const doc = results[0];
+    if (!doc) {
       return;
     }
     if (app.docs.contains(doc)) {
@@ -155,11 +155,11 @@ app.views.SearchScope = class SearchScope extends app.View {
   }
 
   extractHashValue() {
-    let value;
-    if ((value = this.getHashValue())) {
+    const value = this.getHashValue();
+    if (value) {
       const newHash = $.urlDecode(location.hash).replace(
         `#${SearchScope.SEARCH_PARAM}=${value} `,
-        `#${SearchScope.SEARCH_PARAM}=`,
+        `#${SearchScope.SEARCH_PARAM}=`
       );
       app.router.replaceHash(newHash);
       return value;

+ 7 - 5
assets/javascripts/views/sidebar/doc_list.js

@@ -59,7 +59,7 @@ app.views.DocList = class DocList extends app.View {
 
   renderDisabled() {
     this.append(
-      this.tmpl("sidebarDisabled", { count: app.disabledDocs.size() }),
+      this.tmpl("sidebarDisabled", { count: app.disabledDocs.size() })
     );
     this.refreshElements();
     this.renderDisabledList();
@@ -164,8 +164,8 @@ app.views.DocList = class DocList extends app.View {
   }
 
   revealCurrent() {
-    let model;
-    if ((model = app.router.context.type || app.router.context.entry)) {
+    const model = app.router.context.type || app.router.context.entry;
+    if (model) {
       this.reveal(model);
       this.select(model);
     }
@@ -213,7 +213,6 @@ app.views.DocList = class DocList extends app.View {
   }
 
   onClick(event) {
-    let slug;
     const target = $.eventTarget(event);
     if (
       this.disabledTitle &&
@@ -222,7 +221,10 @@ app.views.DocList = class DocList extends app.View {
     ) {
       $.stopEvent(event);
       this.toggleDisabled();
-    } else if ((slug = target.getAttribute("data-enable"))) {
+      return;
+    }
+    const slug = target.getAttribute("data-enable");
+    if (slug) {
       $.stopEvent(event);
       const doc = app.disabledDocs.findBy("slug", slug);
       if (doc) {

+ 2 - 2
assets/javascripts/views/sidebar/results.js

@@ -79,11 +79,11 @@ app.views.Results = class Results extends app.View {
   }
 
   onClick(event) {
-    let slug;
     if (event.which !== 1) {
       return;
     }
-    if ((slug = $.eventTarget(event).getAttribute("data-enable"))) {
+    const slug = $.eventTarget(event).getAttribute("data-enable");
+    if (slug) {
       $.stopEvent(event);
       const doc = app.disabledDocs.findBy("slug", slug);
       if (doc) {

+ 2 - 2
assets/javascripts/views/sidebar/sidebar.js

@@ -180,10 +180,10 @@ app.views.Sidebar = class Sidebar extends app.View {
   }
 
   onEscape() {
-    let doc;
+    const doc = this.search.getScopeDoc();
     this.reset();
     this.resetDisplay();
-    if ((doc = this.search.getScopeDoc())) {
+    if (doc) {
       this.docList.reveal(doc.toEntry());
     } else {
       this.scrollToTop();