From 5906b5ca5d95a244b1f9fd1099926bfdf4894c6f Mon Sep 17 00:00:00 2001 From: Rajiv Shah Date: Sat, 8 Feb 2020 10:17:27 -0500 Subject: [PATCH] refactor(tauri.js): Fix ESLint errors (#398) * refactor(tauri.js): Fix ESLint errors * fix(tauri.js): Disable space-before-function-paren This conflicts with @typescript-eslint/space-before-function-paren. See https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/space-before-function-paren.md#how-to-use * refactor(tauri.js): Change requires to imports * fix(tauri.js): Suppress security/detect-non-literal-fs-filename in src/api/info.ts * fix(tauri.js): Suppress @typescript-eslint/restrict-template-expressions in some cases * fix(tauri.js): Suppress ESLint warnings in src/template/index.ts - Suppress security/detect-object-injection (false positives) - Suppress @typescript-eslint/no-dynamic-delete --- cli/tauri.js/.eslintrc.js | 3 ++- cli/tauri.js/src/api/build.ts | 2 +- cli/tauri.js/src/api/dev.ts | 2 +- cli/tauri.js/src/api/info.ts | 6 +++++- cli/tauri.js/src/api/tauricon.ts | 6 ++++-- cli/tauri.js/src/helpers/logger.ts | 1 + cli/tauri.js/src/helpers/spawn.ts | 7 +++++-- cli/tauri.js/src/helpers/tauri-config.ts | 4 +++- cli/tauri.js/src/template/index.ts | 3 +++ cli/tauri.js/src/types/config.ts | 2 +- 10 files changed, 26 insertions(+), 10 deletions(-) diff --git a/cli/tauri.js/.eslintrc.js b/cli/tauri.js/.eslintrc.js index 97fb2f112..054563fd5 100644 --- a/cli/tauri.js/.eslintrc.js +++ b/cli/tauri.js/.eslintrc.js @@ -47,8 +47,9 @@ module.exports = { 'security/detect-object-injection': 'warn', 'security/detect-possible-timing-attacks': 'error', 'security/detect-pseudoRandomBytes': 'error', + 'space-before-function-paren': 'off', '@typescript-eslint/strict-boolean-expressions': 0, - 'space-before-function-paren': [ + '@typescript-eslint/space-before-function-paren': [ 'error', { asyncArrow: 'always', diff --git a/cli/tauri.js/src/api/build.ts b/cli/tauri.js/src/api/build.ts index 40e692304..93f52f628 100644 --- a/cli/tauri.js/src/api/build.ts +++ b/cli/tauri.js/src/api/build.ts @@ -1,7 +1,7 @@ import { TauriConfig } from 'types' import merge from 'webpack-merge' import Runner from '../runner' -const getTauriConfig = require('../helpers/tauri-config') +import getTauriConfig from '../helpers/tauri-config' module.exports = async (config: TauriConfig): Promise => { const tauri = new Runner() diff --git a/cli/tauri.js/src/api/dev.ts b/cli/tauri.js/src/api/dev.ts index 0cc2fe196..6394bf283 100644 --- a/cli/tauri.js/src/api/dev.ts +++ b/cli/tauri.js/src/api/dev.ts @@ -1,7 +1,7 @@ import { TauriConfig } from 'types' import merge from 'webpack-merge' import Runner from '../runner' -const getTauriConfig = require('../helpers/tauri-config') +import getTauriConfig from '../helpers/tauri-config' module.exports = async (config: TauriConfig): Promise => { const tauri = new Runner() diff --git a/cli/tauri.js/src/api/info.ts b/cli/tauri.js/src/api/info.ts index f6204ad4e..96199beeb 100644 --- a/cli/tauri.js/src/api/info.ts +++ b/cli/tauri.js/src/api/info.ts @@ -14,6 +14,7 @@ interface DirInfo { children?: DirInfo[] } +/* eslint-disable security/detect-non-literal-fs-filename */ function dirTree(filename: string): DirInfo { const stats = fs.lstatSync(filename) const info: DirInfo = { @@ -92,6 +93,7 @@ function printAppInfo(tauriDir: string): void { // @ts-ignore const tauriTomlContents = toml.parse(tauriTomlFile) return chalk.green( + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `${tauriTomlContents.package.version} (from source)` ) } catch (_) {} @@ -173,10 +175,12 @@ module.exports = () => { printInfo({ key: 'App directory structure', section: true }) const tree = dirTree(appDir) - for (const artifact of tree.children || []) { + for (const artifact of tree.children ?? []) { if (artifact.type === 'folder') { console.log(`/${artifact.name}`) } } printAppInfo(tauriDir) } + +/* eslint-enable security/detect-non-literal-fs-filename */ diff --git a/cli/tauri.js/src/api/tauricon.ts b/cli/tauri.js/src/api/tauricon.ts index f57ddf205..f492c8a7e 100644 --- a/cli/tauri.js/src/api/tauricon.ts +++ b/cli/tauri.js/src/api/tauricon.ts @@ -172,6 +172,7 @@ const spinner = (): NodeJS.Timeout => { }, 500) } +/* eslint-disable @typescript-eslint/restrict-template-expressions */ const tauricon = (exports.tauricon = { validate: async function(src: string, target: string) { await validate(src, target) @@ -226,7 +227,7 @@ const tauricon = (exports.tauricon = { try { const pngImage = sharpSrc.resize(pvar[1], pvar[1]) if (pvar[2]) { - const rgb = hexToRgb(options.background_color) || { + const rgb = hexToRgb(options.background_color) ?? { r: undefined, g: undefined, b: undefined @@ -291,7 +292,7 @@ const tauricon = (exports.tauricon = { ) { let output let block = false - const rgb = hexToRgb(options.background_color) || { + const rgb = hexToRgb(options.background_color) ?? { r: undefined, g: undefined, b: undefined @@ -480,6 +481,7 @@ const tauricon = (exports.tauricon = { } } }) +/* eslint-enable @typescript-eslint/restrict-template-expressions */ if (typeof exports !== 'undefined') { if (typeof module !== 'undefined' && module.exports) { diff --git a/cli/tauri.js/src/helpers/logger.ts b/cli/tauri.js/src/helpers/logger.ts index e2bde995f..c3ad83c17 100644 --- a/cli/tauri.js/src/helpers/logger.ts +++ b/cli/tauri.js/src/helpers/logger.ts @@ -14,6 +14,7 @@ export default (banner: string, color: string = 'green') => { console.log( // TODO: proper typings for color and banner // @ts-ignore + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions ` ${chalk[String(color)](String(banner))} ${msg} ${chalk.green(`+${ms(diff)}`)}` ) } else { diff --git a/cli/tauri.js/src/helpers/spawn.ts b/cli/tauri.js/src/helpers/spawn.ts index 625f2c446..ec0c444b7 100644 --- a/cli/tauri.js/src/helpers/spawn.ts +++ b/cli/tauri.js/src/helpers/spawn.ts @@ -25,10 +25,11 @@ export const spawn = ( runner.on('close', code => { log() if (code) { + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions log(`Command "${cmd}" failed with exit code: ${code}`) } - onClose && onClose(code) + onClose?.(code) }) return runner.pid @@ -51,13 +52,15 @@ export const spawnSync = ( cwd }) + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing if (runner.status || runner.error) { warn() + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions warn(`⚠️ Command "${cmd}" failed with exit code: ${runner.status}`) if (runner.status === null) { warn(`⚠️ Please globally install "${cmd}"`) } - onFail && onFail() + onFail?.() process.exit(1) } } diff --git a/cli/tauri.js/src/helpers/tauri-config.ts b/cli/tauri.js/src/helpers/tauri-config.ts index e539a0cc4..232b1a488 100644 --- a/cli/tauri.js/src/helpers/tauri-config.ts +++ b/cli/tauri.js/src/helpers/tauri-config.ts @@ -7,7 +7,7 @@ import * as appPaths from './app-paths' const error = logger('ERROR:', 'red') -module.exports = (cfg: Partial): TauriConfig => { +const getTauriConfig = (cfg: Partial): TauriConfig => { const pkgPath = appPaths.resolve.app('package.json') const tauriConfPath = appPaths.resolve.tauri('tauri.conf.json') if (!existsSync(pkgPath)) { @@ -69,3 +69,5 @@ module.exports = (cfg: Partial): TauriConfig => { return config } + +export default getTauriConfig diff --git a/cli/tauri.js/src/template/index.ts b/cli/tauri.js/src/template/index.ts index 8b16f212e..a1ea6fe05 100644 --- a/cli/tauri.js/src/template/index.ts +++ b/cli/tauri.js/src/template/index.ts @@ -34,9 +34,12 @@ const injectConfFile = ( } Object.keys(finalConf).forEach(key => { // Options marked `null` should be removed + /* eslint-disable security/detect-object-injection */ if (finalConf[key] === null) { + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete finalConf[key] } + /* eslint-enable security/detect-object-injection */ }) writeFileSync(path, JSON.stringify(finalConf, undefined, 2)) } catch (e) { diff --git a/cli/tauri.js/src/types/config.ts b/cli/tauri.js/src/types/config.ts index abbc50057..80a774052 100644 --- a/cli/tauri.js/src/types/config.ts +++ b/cli/tauri.js/src/types/config.ts @@ -38,7 +38,7 @@ export interface TauriConfig { } edge: { active: boolean - }, + } inliner: { active: boolean }