From aa158dcdf6afab21a5d9ffa1d30a4dd0376739f4 Mon Sep 17 00:00:00 2001 From: allohamora Date: Sat, 16 Mar 2024 10:48:50 +0200 Subject: [PATCH] refactor: fix eslint warnings/errors --- .../categories/js/docker/docker.config.test.ts | 4 ++-- __tests__/test-utils/cjs-module.ts | 4 ++-- __tests__/utils/run-command.test.ts | 2 +- __tests__/utils/state.test.ts | 4 ++-- src/categories/js/docker/config/default.config.ts | 4 ++-- src/categories/js/eslint/config/config.interface.ts | 8 ++++---- src/categories/js/eslint/eslint.utils.ts | 12 ++++++------ src/categories/js/jest/config/config.interface.ts | 4 ++-- .../js/lint-staged/config/config.interface.ts | 4 ++-- src/categories/js/lint-staged/lint-staged.utils.ts | 4 ++-- .../js/stylelint/config/config.interface.ts | 8 ++++---- src/index.ts | 2 +- src/states/context.ts | 4 ++-- src/utils/npm.ts | 13 ++++++++----- 14 files changed, 40 insertions(+), 37 deletions(-) diff --git a/__tests__/categories/js/docker/docker.config.test.ts b/__tests__/categories/js/docker/docker.config.test.ts index 012128e..bf22a8f 100644 --- a/__tests__/categories/js/docker/docker.config.test.ts +++ b/__tests__/categories/js/docker/docker.config.test.ts @@ -1,10 +1,10 @@ import { getConfig } from 'src/categories/js/docker/docker.config'; import { expectJsConfig } from '__tests__/test-utils/js-config'; -interface ExpectDockerConfigArgs { +type ExpectDockerConfigArgs = { getDockerFile: ({ version }: { version: string }) => string; dockerIgnore: string; -} +}; const expectDockerConfig = ({ getDockerFile, dockerIgnore }: ExpectDockerConfigArgs) => { const version = '16.14.2'; diff --git a/__tests__/test-utils/cjs-module.ts b/__tests__/test-utils/cjs-module.ts index 5781e59..d1cef03 100644 --- a/__tests__/test-utils/cjs-module.ts +++ b/__tests__/test-utils/cjs-module.ts @@ -1,11 +1,11 @@ import vm from 'node:vm'; -interface Context { +type Context = { module: { exports?: Record; }; [key: string]: unknown; -} +}; export const parse = (script: string) => { const context: Context = { module: { exports: {} } }; diff --git a/__tests__/utils/run-command.test.ts b/__tests__/utils/run-command.test.ts index fb787d3..35d6f9f 100644 --- a/__tests__/utils/run-command.test.ts +++ b/__tests__/utils/run-command.test.ts @@ -19,7 +19,7 @@ const spawnMock = (event: 'exit' | 'error' = 'exit', emitValue?: EV, delayMs const child = new ChildMock(); const exitHandler = () => child.emit(event, emitValue); - delay(delayMs).then(exitHandler); + void delay(delayMs).then(exitHandler); childProcessMocked.spawn.mockReturnValueOnce(child as ReturnType); diff --git a/__tests__/utils/state.test.ts b/__tests__/utils/state.test.ts index bc3fe5e..56eaa43 100644 --- a/__tests__/utils/state.test.ts +++ b/__tests__/utils/state.test.ts @@ -31,7 +31,7 @@ describe('createConfigState', () => { const types = ['1', '2', '3']; const createNewTypeState = () => createTypeState(types); let state = createNewTypeState(); - let [getType, setType] = state; + let [, setType] = state; const configValues = { default: 'default', '1': '1' }; const createNewConfigState = () => createConfigState(state, configValues); @@ -39,7 +39,7 @@ describe('createConfigState', () => { afterEach(() => { state = createNewTypeState(); - [getType, setType] = state; + [, setType] = state; [getConfig] = createNewConfigState(); }); diff --git a/src/categories/js/docker/config/default.config.ts b/src/categories/js/docker/config/default.config.ts index 5964958..90d88d3 100644 --- a/src/categories/js/docker/config/default.config.ts +++ b/src/categories/js/docker/config/default.config.ts @@ -1,9 +1,9 @@ import { readableMultilineString } from 'src/utils/string'; import { FILE_NAME, IGNORE_NAME } from '../docker.const'; -interface GetDockerFileArgs { +type GetDockerFileArgs = { version: string; -} +}; export const getDockerFile = ({ version }: GetDockerFileArgs) => readableMultilineString` FROM node:${version} diff --git a/src/categories/js/eslint/config/config.interface.ts b/src/categories/js/eslint/config/config.interface.ts index a21cae8..85b0a6e 100644 --- a/src/categories/js/eslint/config/config.interface.ts +++ b/src/categories/js/eslint/config/config.interface.ts @@ -1,6 +1,6 @@ import { Mutation } from 'src/utils/mutation'; -export interface EslintConfig { +export type EslintConfig = { parser?: string; parserOptions?: { project?: string; @@ -19,12 +19,12 @@ export interface EslintConfig { plugins?: string[]; extends?: string[]; rules?: Record; -} +}; -export interface Config { +export type Config = { dependencies: string[]; eslintConfig: EslintConfig; ignore: string[]; scripts: { name: string; script: string }[]; mutations: Mutation[]; -} +}; diff --git a/src/categories/js/eslint/eslint.utils.ts b/src/categories/js/eslint/eslint.utils.ts index c3de46a..0e76521 100644 --- a/src/categories/js/eslint/eslint.utils.ts +++ b/src/categories/js/eslint/eslint.utils.ts @@ -4,12 +4,6 @@ import { isPrettierInstalled } from '../prettier/prettier.utils'; import { Config } from './config/config.interface'; import { CONFIG_FILE_NAME, SCRIPT_NAME } from './eslint.const'; -export const prettierMutation = async (config: Config) => { - if (await isPrettierInstalled()) { - addPrettierToConfig(config); - } -}; - export const jestMutation = async (config: Config) => { if (await isJestInstalled()) { config.eslintConfig.env = { ...config.eslintConfig.env, jest: true }; @@ -27,4 +21,10 @@ const addPrettierToConfig = (config: Config) => { config.eslintConfig.extends = eslintExtends; }; +export const prettierMutation = async (config: Config) => { + if (await isPrettierInstalled()) { + addPrettierToConfig(config); + } +}; + export const isEslintInstalled = isInstalledAndInRootCheck(SCRIPT_NAME, CONFIG_FILE_NAME); diff --git a/src/categories/js/jest/config/config.interface.ts b/src/categories/js/jest/config/config.interface.ts index 82a40da..1a06c65 100644 --- a/src/categories/js/jest/config/config.interface.ts +++ b/src/categories/js/jest/config/config.interface.ts @@ -1,7 +1,7 @@ import { NpmScript } from 'src/utils/npm'; -export interface Config { +export type Config = { devDependencies: string[]; scripts: NpmScript[]; configFileContent: string; -} +}; diff --git a/src/categories/js/lint-staged/config/config.interface.ts b/src/categories/js/lint-staged/config/config.interface.ts index 71a3595..9bcc9df 100644 --- a/src/categories/js/lint-staged/config/config.interface.ts +++ b/src/categories/js/lint-staged/config/config.interface.ts @@ -2,7 +2,7 @@ import { Mutation } from 'src/utils/mutation'; export type LintStagedConfig = Record; -export interface Config { +export type Config = { config: LintStagedConfig; mutations: Mutation[]; -} +}; diff --git a/src/categories/js/lint-staged/lint-staged.utils.ts b/src/categories/js/lint-staged/lint-staged.utils.ts index 56c9bd2..b7a9cc1 100644 --- a/src/categories/js/lint-staged/lint-staged.utils.ts +++ b/src/categories/js/lint-staged/lint-staged.utils.ts @@ -12,10 +12,10 @@ import { CLI_NAME as STYLELINT_CLI_NAME } from '../stylelint/stylelint.const'; type ScriptFileExtension = '*.js' | '*.ts' | '*.css' | '*.{ts,tsx}' | '*.{css,ts,tsx}'; -interface OptionMutation { +type OptionMutation = { check: (config: LintStagedConfig, key: string, value: string) => boolean; mutate: (config: LintStagedConfig, key: string, value: string) => void; -} +}; const arrayOptionMutation: OptionMutation = { check: (config, key) => Array.isArray(config[key]), diff --git a/src/categories/js/stylelint/config/config.interface.ts b/src/categories/js/stylelint/config/config.interface.ts index e1dc365..1461ea3 100644 --- a/src/categories/js/stylelint/config/config.interface.ts +++ b/src/categories/js/stylelint/config/config.interface.ts @@ -1,15 +1,15 @@ import { Mutation } from 'src/utils/mutation'; import { NpmScript } from 'src/utils/npm'; -interface StylelintConfig { +type StylelintConfig = { extends: string[]; overrides?: { files: string[]; customSyntax?: string }[]; -} +}; -export interface Config { +export type Config = { devDependencies: string[]; scripts: NpmScript[]; stylelintConfig: StylelintConfig; stylelintIgnore: string; mutations: Mutation[]; -} +}; diff --git a/src/index.ts b/src/index.ts index 3b1b744..7f72776 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,5 +14,5 @@ export const main = async () => { }; if (require.main) { - main(); + void main(); } diff --git a/src/states/context.ts b/src/states/context.ts index 9973591..9114059 100644 --- a/src/states/context.ts +++ b/src/states/context.ts @@ -1,6 +1,6 @@ -export interface Context { +export type Context = { installing: string[]; -} +}; export const context: Context = { installing: [] }; diff --git a/src/utils/npm.ts b/src/utils/npm.ts index 2f7909e..f02c0fc 100644 --- a/src/utils/npm.ts +++ b/src/utils/npm.ts @@ -4,9 +4,9 @@ import { stringify } from './json'; import { PACKAGE_JSON_PATH } from './path'; import { runCommand } from './run-command'; -interface PackageJson extends BasePackageJson { +type PackageJson = { [key: string]: unknown; -} +} & BasePackageJson; export const getPackageJson = async () => { const json = await fsp.readFile(PACKAGE_JSON_PATH, { encoding: 'utf-8' }); @@ -28,10 +28,10 @@ export const addToPackageJson = async (name: keyof PackageJson, value: V) => await setPackageJson(packageJson); }; -export interface NpmScript { +export type NpmScript = { name: string; script: string; -} +}; export const addScripts = async (...scripts: NpmScript[]) => { const packageJson = await getPackageJson(); @@ -39,7 +39,10 @@ export const addScripts = async (...scripts: NpmScript[]) => { packageJson.scripts ??= {}; scripts.forEach(({ name, script }) => { - packageJson.scripts![name] = script; + // type-guard + if (!packageJson.scripts) throw new Error('packageJson.scripts is undefined'); + + packageJson.scripts[name] = script; }); await setPackageJson(packageJson);