Skip to content

Commit

Permalink
chore: fix all fixable eslint errors in src
Browse files Browse the repository at this point in the history
  • Loading branch information
18alantom committed Jun 22, 2023
1 parent 4415c04 commit e67e6ae
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 119 deletions.
6 changes: 3 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module.exports = {
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-floating-promises': 'off',
'@typescript-eslint/no-misused-promises': 'off',
'@typescript-eslint/no-floating-promises': 'warn',
'@typescript-eslint/no-misused-promises': 'warn',
},
parser: '@typescript-eslint/parser',
parserOptions: { project: true, tsconfigRootDir: __dirname },
Expand All @@ -28,5 +28,5 @@ module.exports = {
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
ignorePatterns: ['.eslintrc.js'],
ignorePatterns: ['.eslintrc.js', 'tailwind.config.js'],
};
31 changes: 19 additions & 12 deletions src/errorHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { fyo } from './initFyo';
import router from './router';
import { getErrorMessage, stringifyCircular } from './utils';
import { DialogOptions, ToastOptions } from './utils/types';
import { UnknownFunction } from 'utils/types';
const { ipcRenderer } = require('electron');

function shouldNotStore(error: Error) {
Expand Down Expand Up @@ -40,6 +39,7 @@ export async function sendError(errorLogObj: ErrorLog) {
};

if (fyo.store.isDevelopment) {
// eslint-disable-next-line no-console
console.log('sendError', body);
}

Expand Down Expand Up @@ -80,6 +80,7 @@ export async function handleError(
notifyUser = true
) {
if (logToConsole) {
// eslint-disable-next-line no-console
console.error(error);
}

Expand All @@ -93,7 +94,7 @@ export async function handleError(
if (notifyUser) {
const toastProps = getToastProps(errorLogObj);
const { showToast } = await import('src/utils/interactive');
await showToast(toastProps);
showToast(toastProps);
}
}

Expand Down Expand Up @@ -140,6 +141,7 @@ export async function handleErrorWithDialog(
await showDialog(options);
if (dontThrow) {
if (fyo.store.isDevelopment) {
// eslint-disable-next-line no-console
console.error(error);
}
return;
Expand All @@ -156,12 +158,14 @@ export async function showErrorDialog(title?: string, content?: string) {
await ipcRenderer.invoke(IPC_ACTIONS.SHOW_ERROR, { title, content });
}

// Wrapper Functions

export function getErrorHandled(func: UnknownFunction) {
return async function errorHandled(...args: unknown[]) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getErrorHandled<T extends (...args: any[]) => Promise<any>>(
func: T
) {
type Return = ReturnType<T> extends Promise<infer P> ? P : true;
return async function errorHandled(...args: Parameters<T>): Promise<Return> {
try {
return await func(...args);
return (await func(...args)) as Return;
} catch (error) {
await handleError(false, error as Error, {
functionName: func.name,
Expand All @@ -173,16 +177,19 @@ export function getErrorHandled(func: UnknownFunction) {
};
}

export function getErrorHandledSync(func: UnknownFunction) {
return function errorHandledSync(...args: unknown[]) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getErrorHandledSync<T extends (...args: any[]) => any>(
func: T
) {
type Return = ReturnType<T> extends Promise<infer P> ? P : ReturnType<T>;
return function errorHandledSync(...args: Parameters<T>) {
try {
return func(...args);
return func(...args) as Return;
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
handleError(false, error as Error, {
functionName: func.name,
functionArgs: args,
}).then(() => {
throw error;
});
}
};
Expand Down
10 changes: 5 additions & 5 deletions src/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export class Importer {
}[];

const cellErrors = [];
for (const i in this.valueMatrix) {
for (let i = 0; i < this.valueMatrix.length; i++) {
const row = this.valueMatrix[i];
for (const { tf, index } of assigned) {
if (!row[index]?.error) {
Expand Down Expand Up @@ -278,14 +278,14 @@ export class Importer {
return { dataMap, childTableMap };
}

for (const i in this.valueMatrix) {
for (let i = 0; i < this.valueMatrix.length; i++) {
const row = this.valueMatrix[i];
const name = row[nameIndex]?.value;
if (typeof name !== 'string') {
continue;
}

for (const j in row) {
for (let j = 0; j < row.length; j++) {
const key = this.assignedTemplateFields[j];
const tf = this.templateFieldsMap.get(key ?? '');
if (!tf || !key) {
Expand Down Expand Up @@ -387,7 +387,7 @@ export class Importer {

pushToValueMatrixFromParsedRow(row: string[]) {
const vmRow: ValueMatrix[number] = [];
for (const i in row) {
for (let i = 0; i < row.length; i++) {
const rawValue = row[i];
const index = Number(i);

Expand Down Expand Up @@ -492,7 +492,7 @@ export class Importer {
return false;
}

for (const i in row) {
for (let i = 0; i < row.length; i++) {
const value = row[i];
const tf = this.templateFieldsMap.get(value);
let key: string | null = value;
Expand Down
17 changes: 14 additions & 3 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@ import { App as VueApp, createApp } from 'vue';
import App from './App.vue';
import Badge from './components/Badge.vue';
import FeatherIcon from './components/FeatherIcon.vue';
import { getErrorHandled, handleError, sendError } from './errorHandling';
import {
getErrorHandled,
getErrorHandledSync,
handleError,
sendError,
} from './errorHandling';
import { fyo } from './initFyo';
import { outsideClickDirective } from './renderer/helpers';
import registerIpcRendererListeners from './renderer/registerIpcRendererListeners';
import router from './router';
import { stringifyCircular } from './utils';
import { setLanguageMap } from './utils/language';

// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async () => {
const language = fyo.config.get('language') as string;
if (language) {
await setLanguageMap(language);
}
fyo.store.language = language || 'English';

ipcRenderer.send = getErrorHandled(ipcRenderer.send);
ipcRenderer.invoke = getErrorHandled(ipcRenderer.invoke);
ipcRenderer.send = getErrorHandledSync(ipcRenderer.send.bind(ipcRenderer));
ipcRenderer.invoke = getErrorHandled(ipcRenderer.invoke.bind(ipcRenderer));

registerIpcRendererListeners();
const { isDevelopment, platform, version } = (await ipcRenderer.invoke(
Expand Down Expand Up @@ -69,6 +75,7 @@ import { setLanguageMap } from './utils/language';
function setErrorHandlers(app: VueApp) {
window.onerror = (message, source, lineno, colno, error) => {
error = error ?? new Error('triggered in window.onerror');
// eslint-disable-next-line @typescript-eslint/no-floating-promises
handleError(true, error, { message, source, lineno, colno });
};

Expand All @@ -80,11 +87,13 @@ function setErrorHandlers(app: VueApp) {
error = new Error(String(event.reason));
}

// eslint-disable-next-line no-console
handleError(true, error).catch((err) => console.error(err));
};

window.addEventListener(CUSTOM_EVENTS.LOG_UNEXPECTED, (event) => {
const details = (event as CustomEvent)?.detail as UnexpectedLogObject;
// eslint-disable-next-line @typescript-eslint/no-floating-promises
sendError(details);
});

Expand All @@ -100,7 +109,9 @@ function setErrorHandlers(app: VueApp) {
more.props = stringifyCircular(vm.$props ?? {}, true, true);
}

// eslint-disable-next-line @typescript-eslint/no-floating-promises
handleError(false, err as Error, more);
// eslint-disable-next-line no-console
console.error(err, vm, info);
};
}
Expand Down
7 changes: 5 additions & 2 deletions src/renderer/registerIpcRendererListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IPC_CHANNELS } from 'utils/messages';
export default function registerIpcRendererListeners() {
ipcRenderer.on(
IPC_CHANNELS.LOG_MAIN_PROCESS_ERROR,
async (_, error, more) => {
(_, error: unknown, more?: Record<string, unknown>) => {
if (!(error instanceof Error)) {
throw error;
}
Expand All @@ -22,7 +22,8 @@ export default function registerIpcRendererListeners() {
more.isMainProcess = true;
more.notifyUser ??= true;

await handleError(true, error, more, more.notifyUser);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
handleError(true, error, more, !!more.notifyUser);
}
);

Expand All @@ -32,13 +33,15 @@ export default function registerIpcRendererListeners() {
}

if (fyo.store.isDevelopment) {
// eslint-disable-next-line no-console
console.log(...stuff);
}
});

document.addEventListener('visibilitychange', () => {
const { visibilityState } = document;
if (visibilityState === 'visible' && !fyo.telemetry.started) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
fyo.telemetry.start();
}

Expand Down
7 changes: 4 additions & 3 deletions src/setup/createCOA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class CreateCOA {
const child = children[rootName];

if (rootAccount) {
rootType = (child as COARootAccount).rootType as AccountRootType;
rootType = (child as COARootAccount).rootType;
}

const accountType = (child as COAChildAccount).accountType ?? '';
Expand Down Expand Up @@ -83,7 +83,7 @@ function identifyIsGroup(child: COARootAccount | COAChildAccount): boolean {
return false;
}

async function getCOA(chartOfAccounts: string) {
async function getCOA(chartOfAccounts: string): Promise<COATree> {
const coaList = getCOAList();
const coa = coaList.find(({ name }) => name === chartOfAccounts);

Expand All @@ -93,8 +93,9 @@ async function getCOA(chartOfAccounts: string) {
}

try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const countryCoa = (await import(`../../fixtures/verified/${conCode}.json`))
.default;
.default as { tree: COATree };
return countryCoa.tree;
} catch (e) {
return getStandardCOA();
Expand Down
14 changes: 6 additions & 8 deletions src/setup/setupInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ async function updateSystemSettings(
const systemSettings = await fyo.doc.getDoc('SystemSettings');
const instanceId = getRandomString();

systemSettings.setAndSync({
await systemSettings.setAndSync({
locale,
currency,
instanceId,
Expand Down Expand Up @@ -168,10 +168,8 @@ async function createCurrencyRecords(fyo: Fyo) {
};

const doc = checkAndCreateDoc('Currency', docObject, fyo);
if (doc) {
promises.push(doc);
queue.push(currency);
}
promises.push(doc);
queue.push(currency);
}
return Promise.all(promises);
}
Expand Down Expand Up @@ -256,7 +254,7 @@ async function checkAndCreateDoc(
schemaName: string,
docObject: DocValueMap,
fyo: Fyo
) {
): Promise<Doc | undefined> {
const canCreate = await checkIfExactRecordAbsent(schemaName, docObject, fyo);
if (!canCreate) {
return;
Expand Down Expand Up @@ -373,12 +371,12 @@ async function updateInventorySettings(fyo: Fyo) {
}

const settingName = accountTypeDefaultMap[accountType]!;
inventorySettings.set(settingName, accounts[0].name);
await inventorySettings.set(settingName, accounts[0].name);
}

const location = fyo.t`Stores`;
if (await fyo.db.exists(ModelNameEnum.Location, location)) {
inventorySettings.set('defaultLocation', location);
await inventorySettings.set('defaultLocation', location);
}

await inventorySettings.sync();
Expand Down
11 changes: 5 additions & 6 deletions src/shims-tsx.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import Vue, { VNode } from 'vue'
import Vue, { VNode } from 'vue';

declare global {
namespace JSX {
// tslint:disable no-empty-interface
type Element = VNode
// tslint:disable no-empty-interface
type ElementClass = Vue
type Element = VNode;
type ElementClass = Vue;
interface IntrinsicElements {
[elem: string]: any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[elem: string]: any;
}
}
}
2 changes: 1 addition & 1 deletion src/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async function handleDirectoryDoesNotExist(dbPath: string) {

async function showDbErrorDialog(detail: string) {
const { showDialog } = await import('src/utils/interactive');
return await showDialog({
return showDialog({
type: 'error',
title: t`Cannot Open File`,
detail,
Expand Down
16 changes: 4 additions & 12 deletions src/utils/getStartedConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,23 @@ export function getGetStartedConfig() {
icon: 'general',
description: t`Set up your company information, email, country and fiscal year`,
fieldname: 'companySetup',
action() {
openSettings(ModelNameEnum.AccountingSettings);
},
action: () => openSettings(ModelNameEnum.AccountingSettings),
},
{
key: 'Print',
label: t`Print`,
icon: 'invoice',
description: t`Customize your invoices by adding a logo and address details`,
fieldname: 'printSetup',
action() {
openSettings(ModelNameEnum.PrintSettings);
},
action: () => openSettings(ModelNameEnum.PrintSettings),
},
{
key: 'System',
label: t`System`,
icon: 'system',
description: t`Setup system defaults like date format and display precision`,
fieldname: 'systemSetup',
action() {
openSettings(ModelNameEnum.SystemSettings);
},
action: () => openSettings(ModelNameEnum.SystemSettings),
},
],
},
Expand All @@ -49,9 +43,7 @@ export function getGetStartedConfig() {
label: t`Review Accounts`,
icon: 'review-ac',
description: t`Review your chart of accounts, add any account or tax heads as needed`,
action: () => {
routeTo('/chart-of-accounts');
},
action: () => routeTo('/chart-of-accounts'),
fieldname: 'chartOfAccountsReviewed',
documentation:
'https://docs.frappebooks.com/setting-up/initial-entries.html#add-additional-bank-accounts',
Expand Down
Loading

0 comments on commit e67e6ae

Please sign in to comment.