From 95df3522a15631abc51a4295cae0ea401a8d4e1e Mon Sep 17 00:00:00 2001 From: Markus Hatvan Date: Tue, 14 Sep 2021 19:58:52 +0200 Subject: feat: add eslint-plugin-unicorn (#1936) --- src/features/appearance/index.js | 26 ++++++++++---------- src/features/communityRecipes/store.js | 8 ++++--- src/features/quickSwitch/Component.js | 6 ++--- src/features/serviceProxy/index.js | 4 ++-- src/features/settingsWS/store.js | 20 +++++++++------- src/features/todos/preload.js | 12 ++++++---- src/features/utils/FeatureStore.js | 8 +++---- .../webControls/containers/WebControlsScreen.js | 28 ++++++++++++---------- .../workspaces/components/EditWorkspaceForm.js | 2 +- .../workspaces/components/WorkspaceDrawerItem.js | 2 +- src/features/workspaces/models/Workspace.js | 2 +- src/features/workspaces/store.js | 8 +++---- 12 files changed, 69 insertions(+), 57 deletions(-) (limited to 'src/features') diff --git a/src/features/appearance/index.js b/src/features/appearance/index.js index d1db68ac6..0c935be32 100644 --- a/src/features/appearance/index.js +++ b/src/features/appearance/index.js @@ -14,7 +14,7 @@ function createStyleElement() { } function setAppearance(style) { - const styleElement = document.getElementById(STYLE_ELEMENT_ID); + const styleElement = document.querySelector(`#${STYLE_ELEMENT_ID}`); if (styleElement) { styleElement.innerHTML = style; @@ -30,18 +30,18 @@ function darkenAbsolute(originalColor, absoluteChange) { function generateAccentStyle(accentColorStr) { let style = ''; - Object.keys(themeInfo).forEach(property => { + for (const property of Object.keys(themeInfo)) { style += ` ${themeInfo[property]} { ${property}: ${accentColorStr}; } `; - }); + } let accentColor = color(DEFAULT_APP_SETTINGS.accentColor); try { accentColor = color(accentColorStr); - } catch (e) { + } catch { // Ignore invalid accent color. } const darkerColorStr = darkenAbsolute(accentColor, 5).hex(); @@ -133,14 +133,14 @@ function generateShowDragAreaStyle(accentColor) { } function generateVerticalStyle(widthStr, alwaysShowWorkspaces) { - if (!document.getElementById('vertical-style')) { + if (!document.querySelector('#vertical-style')) { const link = document.createElement('link'); link.id = 'vertical-style'; link.rel = 'stylesheet'; link.type = 'text/css'; link.href = './styles/vertical.css'; - document.head.appendChild(link); + document.head.append(link); } const width = Number(widthStr); const sidebarWidth = width - 4; @@ -150,12 +150,12 @@ function generateVerticalStyle(widthStr, alwaysShowWorkspaces) { .sidebar { height: ${sidebarWidth + verticalStyleOffset + 1}px !important; ${ - alwaysShowWorkspaces - ? ` + alwaysShowWorkspaces + ? ` width: calc(100% - 300px) !important; ` - : '' -} + : '' + } } .sidebar .sidebar__button { @@ -220,10 +220,10 @@ function generateStyle(settings) { } if (useVerticalStyle) { style += generateVerticalStyle(serviceRibbonWidth, alwaysShowWorkspaces); - } else if (document.getElementById('vertical-style')) { - const link = document.getElementById('vertical-style'); + } else if (document.querySelector('#vertical-style')) { + const link = document.querySelector('#vertical-style'); if (link) { - document.head.removeChild(link); + link.remove(); } } if (alwaysShowWorkspaces) { diff --git a/src/features/communityRecipes/store.js b/src/features/communityRecipes/store.js index a3614dd11..05e18e2f7 100644 --- a/src/features/communityRecipes/store.js +++ b/src/features/communityRecipes/store.js @@ -18,11 +18,13 @@ export class CommunityRecipesStore extends FeatureStore { @computed get communityRecipes() { if (!this.stores) return []; - return this.stores.recipePreviews.dev.map((r) => { + return this.stores.recipePreviews.dev.map(recipePreview => { // TODO: Need to figure out if this is even necessary/used - r.isDevRecipe = !!r.author.find((a) => a.email === this.stores.user.data.email); + recipePreview.isDevRecipe = !!recipePreview.author.some( + author => author.email === this.stores.user.data.email, + ); - return r; + return recipePreview; }); } } diff --git a/src/features/quickSwitch/Component.js b/src/features/quickSwitch/Component.js index df2bf968d..f21db0ebd 100644 --- a/src/features/quickSwitch/Component.js +++ b/src/features/quickSwitch/Component.js @@ -140,7 +140,7 @@ class QuickSwitchModal extends Component { let services = []; if ( this.state.search && - compact(invoke(this.state.search, 'match', /^[a-z0-9]/i)).length > 0 + compact(invoke(this.state.search, 'match', /^[\da-z]/i)).length > 0 ) { // Apply simple search algorythm to list of all services services = this.props.stores.services.allDisplayed; @@ -261,7 +261,7 @@ class QuickSwitchModal extends Component { // Wrapped inside timeout to let the modal render first setTimeout(() => { if (this.inputRef.current) { - this.inputRef.current.getElementsByTagName('input')[0].focus(); + this.inputRef.current.querySelectorAll('input')[0].focus(); } }, 10); @@ -273,7 +273,7 @@ class QuickSwitchModal extends Component { // search query change when modal not visible setTimeout(() => { if (this.inputRef.current) { - this.inputRef.current.getElementsByTagName('input')[0].blur(); + this.inputRef.current.querySelectorAll('input')[0].blur(); } }, 100); diff --git a/src/features/serviceProxy/index.js b/src/features/serviceProxy/index.js index eb7116651..b9320cda9 100644 --- a/src/features/serviceProxy/index.js +++ b/src/features/serviceProxy/index.js @@ -18,7 +18,7 @@ export default function init(stores) { debug('Service Proxy autorun'); - services.forEach((service) => { + for (const service of services) { const s = session.fromPartition(`persist:service-${service.id}`); if (config.isEnabled) { @@ -33,6 +33,6 @@ export default function init(stores) { }); } } - }); + } }); } diff --git a/src/features/settingsWS/store.js b/src/features/settingsWS/store.js index 9100f33d1..e36ccee72 100755 --- a/src/features/settingsWS/store.js +++ b/src/features/settingsWS/store.js @@ -25,11 +25,13 @@ export class SettingsWSStore extends FeatureStore { this.stores = stores; this.actions = actions; - this._registerReactions(createReactions([ - this._initialize.bind(this), - this._reconnect.bind(this), - this._close.bind(this), - ])); + this._registerReactions( + createReactions([ + this._initialize.bind(this), + this._reconnect.bind(this), + this._close.bind(this), + ]), + ); } connect() { @@ -51,12 +53,12 @@ export class SettingsWSStore extends FeatureStore { this.heartbeat(); }); - this.ws.on('message', (data) => { + this.ws.on('message', data => { const resp = JSON.parse(data); debug('Received message', resp); if (resp.id) { - this.stores.user.getUserInfoRequest.patch((result) => { + this.stores.user.getUserInfoRequest.patch(result => { if (!result) return; debug('Patching user object with new values'); @@ -66,8 +68,8 @@ export class SettingsWSStore extends FeatureStore { }); this.ws.on('ping', this.heartbeat.bind(this)); - } catch (err) { - console.err(err); + } catch (error) { + console.error(error); } } diff --git a/src/features/todos/preload.js b/src/features/todos/preload.js index 9bd76a704..3b86ddbc5 100644 --- a/src/features/todos/preload.js +++ b/src/features/todos/preload.js @@ -7,7 +7,9 @@ debug('Preloading Todos Webview'); let hostMessageListener = ({ action }) => { switch (action) { - case 'todos:initialize-as-service': ipcRenderer.sendToHost('hello'); break; + case 'todos:initialize-as-service': + ipcRenderer.sendToHost('hello'); + break; default: } }; @@ -15,7 +17,9 @@ let hostMessageListener = ({ action }) => { window.ferdi = { onInitialize(ipcHostMessageListener) { hostMessageListener = ipcHostMessageListener; - ipcRenderer.sendToHost(IPC.TODOS_CLIENT_CHANNEL, { action: 'todos:initialized' }); + ipcRenderer.sendToHost(IPC.TODOS_CLIENT_CHANNEL, { + action: 'todos:initialized', + }); }, sendToHost(message) { ipcRenderer.sendToHost(IPC.TODOS_CLIENT_CHANNEL, message); @@ -30,7 +34,7 @@ ipcRenderer.on(IPC.TODOS_HOST_CHANNEL, (event, message) => { if (window.location.href === 'https://app.franztodos.com/login/') { // Insert info element informing about Franz accounts const infoElement = document.createElement('p'); - infoElement.innerText = `You are using Franz's official Todo Service. + infoElement.textContent = `You are using Franz's official Todo Service. This service will only work with accounts registered with Franz - no Ferdi accounts will work here! If you do not have a Franz account you can change the Todo service by going into Ferdi's settings and changing the "Todo server". You can choose any service as this Todo server, e.g. Todoist or Apple Notes.`; @@ -42,7 +46,7 @@ You can choose any service as this Todo server, e.g. Todoist or Apple Notes.`; const textElement = document.querySelector('p'); if (textElement) { clearInterval(waitForReact); - textElement.parentElement.insertBefore(infoElement, textElement); + textElement.parentElement?.insertBefore(infoElement, textElement); } else { numChecks += 1; diff --git a/src/features/utils/FeatureStore.js b/src/features/utils/FeatureStore.js index 4d4e217a9..afe726294 100644 --- a/src/features/utils/FeatureStore.js +++ b/src/features/utils/FeatureStore.js @@ -16,11 +16,11 @@ export class FeatureStore { } _startActions(actions = this._actions) { - actions.forEach((a) => a.start()); + for (const a of actions) a.start(); } _stopActions(actions = this._actions) { - actions.forEach((a) => a.stop()); + for (const a of actions) a.stop(); } // REACTIONS @@ -31,10 +31,10 @@ export class FeatureStore { } _startReactions(reactions = this._reactions) { - reactions.forEach((r) => r.start()); + for (const r of reactions) r.start(); } _stopReactions(reactions = this._reactions) { - reactions.forEach((r) => r.stop()); + for (const r of reactions) r.stop(); } } diff --git a/src/features/webControls/containers/WebControlsScreen.js b/src/features/webControls/containers/WebControlsScreen.js index e1e1b9991..0273bb13e 100644 --- a/src/features/webControls/containers/WebControlsScreen.js +++ b/src/features/webControls/containers/WebControlsScreen.js @@ -16,7 +16,8 @@ const URL_EVENTS = [ 'did-navigate-in-page', ]; -@inject('stores', 'actions') @observer +@inject('stores', 'actions') +@observer class WebControlsScreen extends Component { @observable url = ''; @@ -36,15 +37,15 @@ class WebControlsScreen extends Component { this.webview = service.webview; this.url = this.webview.getURL(); - URL_EVENTS.forEach((event) => { - this.webview.addEventListener(event, (e) => { + for (const event of URL_EVENTS) { + this.webview.addEventListener(event, e => { if (!e.isMainFrame) return; this.url = e.url; this.canGoBack = this.webview.canGoBack(); this.canGoForward = this.webview.canGoForward(); }); - }); + } } }); } @@ -83,13 +84,16 @@ class WebControlsScreen extends Component { try { url = new URL(url).toString(); - } catch (err) { - // eslint-disable-next-line no-useless-escape - if (url.match(/^((?!-))(xn--)?[a-z0-9][a-z0-9-_]{0,61}[a-z0-9]{0,1}\.(xn--)?([a-z0-9\-]{1,61}|[a-z0-9-]{1,30}\.[a-z]{2,})$/)) { - url = `http://${url}`; - } else { - url = SEARCH_ENGINE_URLS[this.settings.app.searchEngine]({ searchTerm: url }); - } + } catch { + url = + // eslint-disable-next-line no-useless-escape + /^((?!-))(xn--)?[\da-z][\d_a-z-]{0,61}[\da-z]{0,1}\.(xn--)?([\da-z\-]{1,61}|[\da-z-]{1,30}\.[a-z]{2,})$/.test( + url, + ) + ? `http://${url}` + : SEARCH_ENGINE_URLS[this.settings.app.searchEngine]({ + searchTerm: url, + }); } this.webview.loadURL(url); @@ -114,7 +118,7 @@ class WebControlsScreen extends Component { goBack={() => this.goBack()} canGoForward={this.canGoForward} goForward={() => this.goForward()} - navigate={(url) => this.navigate(url)} + navigate={url => this.navigate(url)} url={this.url} /> ); diff --git a/src/features/workspaces/components/EditWorkspaceForm.js b/src/features/workspaces/components/EditWorkspaceForm.js index cae95e9ed..f562733dd 100644 --- a/src/features/workspaces/components/EditWorkspaceForm.js +++ b/src/features/workspaces/components/EditWorkspaceForm.js @@ -108,7 +108,7 @@ class EditWorkspaceForm extends Component { default: false, }, services: { - value: workspace.services.slice(), + value: [...workspace.services], }, }, }); diff --git a/src/features/workspaces/components/WorkspaceDrawerItem.js b/src/features/workspaces/components/WorkspaceDrawerItem.js index 82e1b81a4..7df2b60be 100644 --- a/src/features/workspaces/components/WorkspaceDrawerItem.js +++ b/src/features/workspaces/components/WorkspaceDrawerItem.js @@ -143,7 +143,7 @@ class WorkspaceDrawerItem extends Component { isActive ? classes.activeServices : null, ])} > - {services.length + {services.length > 0 ? services.join(', ') : intl.formatMessage(messages.noServicesAddedYet)} diff --git a/src/features/workspaces/models/Workspace.js b/src/features/workspaces/models/Workspace.js index d9488e991..14add9437 100644 --- a/src/features/workspaces/models/Workspace.js +++ b/src/features/workspaces/models/Workspace.js @@ -15,7 +15,7 @@ export default class Workspace { constructor(data) { if (!data.id) { - throw Error('Workspace requires Id'); + throw new Error('Workspace requires Id'); } this.id = data.id; diff --git a/src/features/workspaces/store.js b/src/features/workspaces/store.js index ec9d7ee7f..73e882990 100644 --- a/src/features/workspaces/store.js +++ b/src/features/workspaces/store.js @@ -302,8 +302,8 @@ export default class WorkspacesStore extends FeatureStore { const { allServicesRequest } = services; const servicesHaveBeenLoaded = allServicesRequest.wasExecuted && !allServicesRequest.isError; // Loop through all workspaces and remove invalid service ids (locally) - this.workspaces.forEach((workspace) => { - workspace.services.forEach((serviceId) => { + for (const workspace of this.workspaces) { + for (const serviceId of workspace.services) { if ( servicesHaveBeenLoaded && !services.one(serviceId) @@ -311,7 +311,7 @@ export default class WorkspacesStore extends FeatureStore { ) { workspace.services.remove(serviceId); } - }); - }); + } + } }; } -- cgit v1.2.3-70-g09d2