Skip to content

Commit

Permalink
refactor: avoid unnecessary async scopes in eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jul 7, 2023
1 parent d660bc9 commit b4101d2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
12 changes: 7 additions & 5 deletions packages/next/src/lib/eslint/hasEslintConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions packages/next/src/lib/eslint/runLintCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const requiredPackages = [
},
]

async function cliPrompt() {
async function cliPrompt(): Promise<{ config?: any }> {
console.log(
chalk.bold(
`${chalk.cyan(
Expand Down Expand Up @@ -72,7 +72,7 @@ async function cliPrompt() {
unselected: ' ',
})

return { config: value?.config }
return { config: value?.config ?? null }
} catch {
return { config: null }
}
Expand Down Expand Up @@ -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(
Expand Down
25 changes: 13 additions & 12 deletions packages/next/src/lib/eslint/writeOutputFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b4101d2

Please sign in to comment.