From 85bca2e7785dcb9bd5aab6325a73b198cec89973 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 10 Jul 2023 06:52:20 -0400 Subject: [PATCH] refactor: avoid unnecessary async scopes in eslint (#52418) This pull request: - removes unnecessary object copying in `hasEslintConfiguration` - returns always `null` on `runLintCheck`, and make sure it does not return undefined. - removes unnecessary async scope creation on `isDirectory` --- .../src/lib/eslint/hasEslintConfiguration.ts | 12 +++++---- packages/next/src/lib/eslint/runLintCheck.ts | 6 ++--- .../next/src/lib/eslint/writeOutputFile.ts | 25 ++++++++++--------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/packages/next/src/lib/eslint/hasEslintConfiguration.ts b/packages/next/src/lib/eslint/hasEslintConfiguration.ts index 6c600f545478c..4d77920813e80 100644 --- a/packages/next/src/lib/eslint/hasEslintConfiguration.ts +++ b/packages/next/src/lib/eslint/hasEslintConfiguration.ts @@ -29,14 +29,16 @@ export async function hasEslintConfiguration( content === '---' || content === 'module.exports = {}' ) { - return { ...configObject, emptyEslintrc: true } + configObject.emptyEslintrc = true + } else { + configObject.exists = true } - return { ...configObject, exists: true } } else if (packageJsonConfig?.eslintConfig) { - if (Object.keys(packageJsonConfig?.eslintConfig).length) { - return { ...configObject, exists: true } + if (Object.keys(packageJsonConfig.eslintConfig).length) { + configObject.exists = true + } else { + configObject.emptyPkgJsonConfig = true } - return { ...configObject, emptyPkgJsonConfig: true } } return configObject } diff --git a/packages/next/src/lib/eslint/runLintCheck.ts b/packages/next/src/lib/eslint/runLintCheck.ts index 528aecd570f4d..077a26cd0f1c6 100644 --- a/packages/next/src/lib/eslint/runLintCheck.ts +++ b/packages/next/src/lib/eslint/runLintCheck.ts @@ -43,7 +43,7 @@ const requiredPackages = [ }, ] -async function cliPrompt() { +async function cliPrompt(): Promise<{ config?: any }> { console.log( chalk.bold( `${chalk.cyan( @@ -72,7 +72,7 @@ async function cliPrompt() { unselected: ' ', }) - return { config: value?.config } + return { config: value?.config ?? null } } catch { return { config: null } } @@ -131,7 +131,7 @@ async function lint( const mod = await Promise.resolve(require(deps.resolved.get('eslint')!)) const { ESLint } = mod - let eslintVersion = ESLint?.version ?? mod?.CLIEngine?.version + let eslintVersion = ESLint?.version ?? mod.CLIEngine?.version if (!eslintVersion || semver.lt(eslintVersion, '7.0.0')) { return `${chalk.red( diff --git a/packages/next/src/lib/eslint/writeOutputFile.ts b/packages/next/src/lib/eslint/writeOutputFile.ts index cef79393b0378..2666b8d1b9442 100644 --- a/packages/next/src/lib/eslint/writeOutputFile.ts +++ b/packages/next/src/lib/eslint/writeOutputFile.ts @@ -7,21 +7,22 @@ import isError from '../../lib/is-error' * Check if a given file path is a directory or not. * Returns `true` if the path is a directory. */ -async function isDirectory( +function isDirectory( /** The path to a file to check. */ filePath: string ): Promise { - try { - return (await fs.stat(filePath)).isDirectory() - } catch (error) { - if ( - isError(error) && - (error.code === 'ENOENT' || error.code === 'ENOTDIR') - ) { - return false - } - throw error - } + return fs + .stat(filePath) + .then((stat) => stat.isDirectory()) + .catch((error) => { + if ( + isError(error) && + (error.code === 'ENOENT' || error.code === 'ENOTDIR') + ) { + return false + } + throw error + }) } /** * Create a file with eslint output data