Skip to content

Commit

Permalink
style: additional JS-W1043 anti-pattern fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmeese7 committed Sep 26, 2023
1 parent 4897034 commit cb622b4
Show file tree
Hide file tree
Showing 36 changed files with 184 additions and 125 deletions.
2 changes: 1 addition & 1 deletion apps/filemanager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const getDirectoryCount = (files) =>
const getFileCount = (files) =>
files.filter((file) => !file.isDirectory).length;
const getTotalSize = (files) =>
files.reduce((total, file) => total + (file.size || 0), 0);
files.reduce((total, file) => total + (file.size ?? 0), 0);

/**
* Formats the file selection status message
Expand Down
2 changes: 1 addition & 1 deletion apps/filemanager/src/factories.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export const vfsActionFactory = (core, proc, win, dialog, state) => {
refresh(refreshValue);
return result;
} catch (error) {
dialog("error", error, defaultError || "An error occurred");
dialog("error", error, defaultError ?? "An error occurred");
} finally {
win.setState("loading", false);
}
Expand Down
2 changes: 1 addition & 1 deletion apps/settings/src/cursorEffects.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const getCursorChoices = () =>
const properties = cursorEffects[key];

return {
label: properties.label || "Mystery",
label: properties.label ?? "Mystery",
value: properties.effect.name,
};
});
Expand Down
2 changes: 1 addition & 1 deletion apps/settings/src/dynamicBackgrounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const getDynamicWallpaperChoices = (core) => {
const properties = effects[key];

return {
label: properties.label || "Mystery",
label: properties.label ?? "Mystery",
value: properties.effect.name,
};
});
Expand Down
4 changes: 2 additions & 2 deletions apps/terminal/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ let terminals = [];
*/
const createTerminal = (core, ws, options = {}, args = []) => {
const hostname = core.config("xterm.hostname", "localhost");
const username = process.env.USERNAME || options.username || "root";
const password = process.env.PASSWORD || options.password || "toor";
const username = process.env.USERNAME ?? options.username ?? "root";
const password = process.env.PASSWORD ?? options.password ?? "toor";
const sshCommand = `ssh -o StrictHostKeyChecking=no ${username}@${hostname}`;
let sshPassCommand = `sshpass -p ${password} ${sshCommand}`;

Expand Down
2 changes: 1 addition & 1 deletion backend/server/src/adapters/vfs/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ module.exports = (core) => {
(file) => getRealPath(core, options.session, vfs.mount, file)
);

const action = options.action || "compress";
const action = options.action ?? "compress";
switch (action) {
case "compress": {
// Define the archive instance
Expand Down
2 changes: 1 addition & 1 deletion backend/server/src/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class Filesystem {

return filenames[path.basename(filename)]
? filenames[path.basename(filename)]
: mime.getType(filename) || "application/octet-stream";
: mime.getType(filename) ?? "application/octet-stream";
}

/**
Expand Down
2 changes: 1 addition & 1 deletion frontend/client/__mocks__/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ document.createElement = (type) => {
const el = originalCreateElement.call(document, type);
if (type === "script" || type === "link") {
setTimeout(() => {
const src = (el.src || el.href || "").replace(/\.(css|js)$/, "");
const src = (el.src ?? el.href ?? "").replace(/\.(css|js)$/, "");
if (src === "http://localhost/onreadystatechange") {
el.readyState = "loaded";
el.onreadystatechange();
Expand Down
14 changes: 7 additions & 7 deletions frontend/client/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,32 +560,32 @@ declare class Window extends EventEmitter {
_updateDOM(): void;

/**
* Updates the window buttons in DOM.
* Updates the window buttons in the DOM.
*/
private _updateButtons;

/**
* Updates window title in DOM.
* Updates window title in the DOM.
*/
private _updateTitle;

/**
* Updates window icon decoration in DOM.
* Updates window icon decoration in the DOM.
*/
private _updateIconStyles;

/**
* Updates window header decoration in DOM.
* Updates window header decoration in the DOM.
*/
private _updateHeaderStyles;

/**
* Updates window data in DOM.
* Updates window data in the DOM.
*/
private _updateAttributes;

/**
* Updates window style in DOM.
* Updates window style in the DOM.
*/
private _updateStyles;
}
Expand Down Expand Up @@ -1063,7 +1063,7 @@ declare class Core extends CoreBase {
readonly splash: Splash;

/**
* Main DOM element.
* Main the DOM element.
*/
readonly $root: Element;

Expand Down
4 changes: 2 additions & 2 deletions frontend/client/src/adapters/ui/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ const createAttributes = (props, field, disabled) => {
disabled = disabled ? "disabled" : undefined;
if (field.tagName === "input") {
if (field.attributes.type !== "submit") {
const autocomplete = field.attributes.autocomplete || true;
const autocomplete = field.attributes.autocomplete ?? true;
return {
autocapitalize: "off",
autocomplete: autocomplete ? "new-" + field.attributes.name : "off",
disabled,
oncreate: (el) =>
(el.value = props[field.attributes.name] || field.value || ""),
(el.value = props[field.attributes.name] ?? field.value ?? ""),
...field.attributes,
};
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/client/src/adapters/vfs/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const requester = (core) =>
}

const contentType = response.headers.get("content-type")
|| "application/octet-stream";
?? "application/octet-stream";

return response.arrayBuffer().then((result) => ({
mime: contentType,
Expand Down
4 changes: 2 additions & 2 deletions frontend/client/src/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ const createAdapter = (core, options) => {
? localStorageAuth
: typeof options.adapter === "function"
? options.adapter
: defaultAdapters[options.adapter || "server"];
: defaultAdapters[options.adapter ?? "server"];

return {
login: () => Promise.reject(new Error("Not implemented")),
logout: () => Promise.reject(new Error("Not implemented")),
register: () => Promise.reject(new Error("Not implemented")),
init: () => Promise.resolve(true),
destroy: () => {},
...adapter(core, options.config || {}),
...adapter(core, options.config ?? {}),
};
};

Expand Down
2 changes: 1 addition & 1 deletion frontend/client/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const createUri = (str) =>

const pathname = createUri(window.location.pathname);
const href = createUri(window.location.href);
const development = !/^prod/i.test(process.env.NODE_ENV || "");
const development = !/^prod/i.test(process.env.NODE_ENV ?? "");

export const defaultConfiguration = {
development: development,
Expand Down
2 changes: 1 addition & 1 deletion frontend/client/src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default class Core extends CoreBase {
this.ping = null;

/**
* Main DOM element (typically `<body>`)
* Main the DOM element (typically `<body>`)
* @type {Element}
* @readonly
*/
Expand Down
4 changes: 2 additions & 2 deletions frontend/client/src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ const createAdapter = (core, options) => {
? localStorageSettings
: typeof options.adapter === "function"
? options.adapter
: defaultAdapters[options.adapter || "localStorage"];
: defaultAdapters[options.adapter ?? "localStorage"];

return {
load: () => Promise.reject(new Error("Not implemented")),
save: () => Promise.reject(new Error("Not implemented")),
init: () => Promise.resolve(true),
clear: () => Promise.resolve(true),
destroy: () => {},
...adapter(core, options.config),
...adapter(core, options.config ?? {}),
};
};

Expand Down
2 changes: 1 addition & 1 deletion frontend/client/src/utils/desktop.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export const applyCursorEffect = (cursor) => {
}

// Calls the cursor effect function by name
const effectName = cursor.effect || "none";
const effectName = cursor.effect ?? "none";
const options = cursor.options || {};
cursorEffects[effectName].effect(options);
};
Expand Down
2 changes: 1 addition & 1 deletion frontend/client/src/utils/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const fetchXhr = (target, fetchOptions) =>
Object.entries(fetchOptions.headers).forEach(([key, val]) =>
req.setRequestHeader(key, val)
);
req.responseType = fetchOptions.responseType || "";
req.responseType = fetchOptions.responseType ?? "";
req.send(fetchOptions.body);
});

Expand Down
7 changes: 3 additions & 4 deletions frontend/client/src/utils/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ const sortMap = {
*/
const createSpecials = (path) => {
const specials = [];

const stripped = path.replace(/\/+/g, "/").replace(/^(\w+):/, "") || "/";
const stripped = path.replace(/\/+/g, "/").replace(/^(\w+):/, "") ?? "/";

if (stripped !== "/") {
specials.push({
Expand All @@ -119,15 +118,15 @@ const createSpecials = (path) => {
size: 0,
stat: {},
filename: "..",
path: parentDirectory(path) || "/",
path: parentDirectory(path) ?? "/",
});
}

return specials;
};

/**
* Creates a FileReader (promisified)
* Creates a FileReader (promisified).
*
* @param {String} method The method to call
* @param {ArrayBuffer} ab The ArrayBuffer
Expand Down
2 changes: 1 addition & 1 deletion frontend/client/src/utils/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ export const loadOptionsFromConfig = (config, appName, windowId) => {
return false;
} else if (application && !matchStringOrRegex(appName, application)) {
return false;
} else if (window && !matchStringOrRegex(windowId || "", window)) {
} else if (window && !matchStringOrRegex(windowId ?? "", window)) {
return false;
}

Expand Down
35 changes: 16 additions & 19 deletions frontend/client/src/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,21 +735,21 @@ export default class Window extends EventEmitter {

/**
* Set the Window title.
* @param {String} title Title
* @param {String} [title=""] Title
*/
setTitle(title) {
this.state.title = title || "";
setTitle(title = "") {
this.state.title = title;
this._updateTitle();

this.core.emit("meeseOS/window:change", this, "title", title);
}

/**
* Set the Window dimension.
* @param {WindowDimension} dimension The dimension
* @param {WindowDimension} [dimension={}] The dimension
*/
setDimension(dimension) {
const { width, height } = { ...this.state.dimension, ...(dimension || {}) };
setDimension(dimension = {}) {
const { width, height } = { ...this.state.dimension, ...dimension };

this.state.dimension.width = width;
this.state.dimension.height = height;
Expand All @@ -759,11 +759,11 @@ export default class Window extends EventEmitter {

/**
* Set the Window position.
* @param {WindowPosition} position The position
* @param {WindowPosition} [position={}] The position
* @param {Boolean} [preventDefault=false] Prevents any future position setting in init procedure
*/
setPosition(position, preventDefault = false) {
const { left, top } = { ...this.state.position, ...(position || {}) };
setPosition(position = {}, preventDefault = false) {
const { left, top } = { ...this.state.position, ...position };

this.state.position.top = top;
this.state.position.left = left;
Expand Down Expand Up @@ -888,7 +888,6 @@ export default class Window extends EventEmitter {
* @param {String} name State name
* @param {*} value State value
* @param {Boolean} [update=true] Update the DOM
* @param {Boolean} [updateAll=true] Update the entire DOM
*/
_setState(name, value, update = true) {
const oldValue = this.state[name];
Expand All @@ -913,9 +912,7 @@ export default class Window extends EventEmitter {
* @param {Boolean} [update=true] Update the DOM
*/
_toggleState(name, value, eventName, update = true) {
if (this.state[name] === value) {
return false;
}
if (this.state[name] === value) return false;

logger.debug("Window::_toggleState()", name, value, eventName, update);

Expand Down Expand Up @@ -958,7 +955,7 @@ export default class Window extends EventEmitter {
}

/**
* Updates the window buttons in DOM.
* Updates the window buttons in the DOM.
* @private
*/
_updateButtons() {
Expand Down Expand Up @@ -987,7 +984,7 @@ export default class Window extends EventEmitter {
}

/**
* Updates window title in DOM.
* Updates window title in the DOM.
* @private
*/
_updateTitle() {
Expand All @@ -1000,7 +997,7 @@ export default class Window extends EventEmitter {
}

/**
* Updates window icon decoration in DOM.
* Updates window icon decoration in the DOM.
* @private
*/
_updateIconStyles() {
Expand All @@ -1013,7 +1010,7 @@ export default class Window extends EventEmitter {
}

/**
* Updates window header decoration in DOM.
* Updates window header decoration in the DOM.
* @private
*/
_updateHeaderStyles() {
Expand All @@ -1026,7 +1023,7 @@ export default class Window extends EventEmitter {
}

/**
* Updates window data in DOM.
* Updates window data in the DOM.
* @private
*/
_updateAttributes() {
Expand All @@ -1046,7 +1043,7 @@ export default class Window extends EventEmitter {
}

/**
* Updates window style in DOM.
* Updates window style in the DOM.
* @private
*/
_updateStyles() {
Expand Down
Loading

0 comments on commit cb622b4

Please sign in to comment.