Unverified Commit 85bca2e7 authored by Yagiz Nizipli's avatar Yagiz Nizipli Committed by GitHub

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`
parent a06775cd
...@@ -29,14 +29,16 @@ export async function hasEslintConfiguration( ...@@ -29,14 +29,16 @@ export async function hasEslintConfiguration(
content === '---' || content === '---' ||
content === 'module.exports = {}' content === 'module.exports = {}'
) { ) {
return { ...configObject, emptyEslintrc: true } configObject.emptyEslintrc = true
} else {
configObject.exists = true
} }
return { ...configObject, exists: true }
} else if (packageJsonConfig?.eslintConfig) { } else if (packageJsonConfig?.eslintConfig) {
if (Object.keys(packageJsonConfig?.eslintConfig).length) { if (Object.keys(packageJsonConfig.eslintConfig).length) {
return { ...configObject, exists: true } configObject.exists = true
} else {
configObject.emptyPkgJsonConfig = true
} }
return { ...configObject, emptyPkgJsonConfig: true }
} }
return configObject return configObject
} }
...@@ -43,7 +43,7 @@ const requiredPackages = [ ...@@ -43,7 +43,7 @@ const requiredPackages = [
}, },
] ]
async function cliPrompt() { async function cliPrompt(): Promise<{ config?: any }> {
console.log( console.log(
chalk.bold( chalk.bold(
`${chalk.cyan( `${chalk.cyan(
...@@ -72,7 +72,7 @@ async function cliPrompt() { ...@@ -72,7 +72,7 @@ async function cliPrompt() {
unselected: ' ', unselected: ' ',
}) })
return { config: value?.config } return { config: value?.config ?? null }
} catch { } catch {
return { config: null } return { config: null }
} }
...@@ -131,7 +131,7 @@ async function lint( ...@@ -131,7 +131,7 @@ async function lint(
const mod = await Promise.resolve(require(deps.resolved.get('eslint')!)) const mod = await Promise.resolve(require(deps.resolved.get('eslint')!))
const { ESLint } = mod 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')) { if (!eslintVersion || semver.lt(eslintVersion, '7.0.0')) {
return `${chalk.red( return `${chalk.red(
......
...@@ -7,13 +7,14 @@ import isError from '../../lib/is-error' ...@@ -7,13 +7,14 @@ import isError from '../../lib/is-error'
* Check if a given file path is a directory or not. * Check if a given file path is a directory or not.
* Returns `true` if the path is a directory. * Returns `true` if the path is a directory.
*/ */
async function isDirectory( function isDirectory(
/** The path to a file to check. */ /** The path to a file to check. */
filePath: string filePath: string
): Promise<boolean> { ): Promise<boolean> {
try { return fs
return (await fs.stat(filePath)).isDirectory() .stat(filePath)
} catch (error) { .then((stat) => stat.isDirectory())
.catch((error) => {
if ( if (
isError(error) && isError(error) &&
(error.code === 'ENOENT' || error.code === 'ENOTDIR') (error.code === 'ENOENT' || error.code === 'ENOTDIR')
...@@ -21,7 +22,7 @@ async function isDirectory( ...@@ -21,7 +22,7 @@ async function isDirectory(
return false return false
} }
throw error throw error
} })
} }
/** /**
* Create a file with eslint output data * Create a file with eslint output data
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment