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

fix: don't create .eslintrc if package.json contains eslintConfig #26025

32 changes: 16 additions & 16 deletions packages/next/lib/eslint/writeDefaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,25 @@ export async function writeDefaultConfig(
)
)
}
} else if (
packageJsonConfig?.eslintConfig &&
Object.entries(packageJsonConfig?.eslintConfig).length === 0
) {
packageJsonConfig.eslintConfig = defaultConfig
} else if (packageJsonConfig?.eslintConfig) {
// Creates .eslintrc only if package.json's eslintConfig field is empty
if (Object.entries(packageJsonConfig?.eslintConfig).length === 0) {
packageJsonConfig.eslintConfig = defaultConfig

if (pkgJsonPath)
await fs.writeFile(
pkgJsonPath,
CommentJson.stringify(packageJsonConfig, null, 2) + os.EOL
)
if (pkgJsonPath)
await fs.writeFile(
pkgJsonPath,
CommentJson.stringify(packageJsonConfig, null, 2) + os.EOL
)

console.log(
chalk.green(
`We detected an empty ${chalk.bold(
'eslintConfig'
)} field in package.json and updated it for you to include the base Next.js ESLint configuration.`
console.log(
chalk.green(
`We detected an empty ${chalk.bold(
'eslintConfig'
)} field in package.json and updated it for you to include the base Next.js ESLint configuration.`
)
)
)
}
} else {
await fs.writeFile(
'.eslintrc',
Expand Down
6 changes: 6 additions & 0 deletions test/integration/eslint/config-in-package-json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"eslintConfig": {
"extends": "next",
"root": true
}
}
9 changes: 9 additions & 0 deletions test/integration/eslint/config-in-package-json/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default class Test {
render() {
return (
<div>
<h1>Hello title</h1>
</div>
)
}
}
41 changes: 41 additions & 0 deletions test/integration/eslint/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import fs from 'fs-extra'
import { join } from 'path'
import findUp from 'next/dist/compiled/find-up'
import { nextBuild, nextLint } from 'next-test-utils'
import { writeFile, readFile } from 'fs-extra'

Expand All @@ -8,6 +10,7 @@ const dirFirstTimeSetup = join(__dirname, '../first-time-setup')
const dirCustomConfig = join(__dirname, '../custom-config')
const dirIgnoreDuringBuilds = join(__dirname, '../ignore-during-builds')
const dirCustomDirectories = join(__dirname, '../custom-directories')
const dirConfigInPackageJson = join(__dirname, '../config-in-package-json')

describe('ESLint', () => {
describe('Next Build', () => {
Expand Down Expand Up @@ -113,5 +116,43 @@ describe('ESLint', () => {
const output = stdout + stderr
expect(output).toContain('No ESLint warnings or errors')
})

test("don't create .eslintrc file if package.json has eslintConfig field", async () => {
const eslintrcFile =
(await findUp(
[
'.eslintrc.js',
'.eslintrc.yaml',
'.eslintrc.yml',
'.eslintrc.json',
'.eslintrc',
],
{
cwd: '.',
}
)) ?? null

try {
// If we found a .eslintrc file, it's probably config from root Next.js directory. Rename it during the test
if (eslintrcFile) {
await fs.move(eslintrcFile, `${eslintrcFile}.original`)
}

const { stdout, stderr } = await nextLint(dirConfigInPackageJson, [], {
stdout: true,
stderr: true,
})

const output = stdout + stderr
expect(output).not.toContain(
'We created the .eslintrc file for you and included the base Next.js ESLint configuration'
)
} finally {
// Restore original .eslintrc file
if (eslintrcFile) {
await fs.move(`${eslintrcFile}.original`, eslintrcFile)
}
}
})
})
})