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(
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
}
......@@ -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(
......
......@@ -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<boolean> {
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
......
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