Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: avoid unnecessary async scopes in eslint #52418

Merged
merged 3 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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