Unverified Commit 66cb786d authored by Vitaly Baev's avatar Vitaly Baev Committed by GitHub

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

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

* tests: added integration tests for next lint
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 775c7be5
...@@ -49,25 +49,25 @@ export async function writeDefaultConfig( ...@@ -49,25 +49,25 @@ export async function writeDefaultConfig(
) )
) )
} }
} else if ( } else if (packageJsonConfig?.eslintConfig) {
packageJsonConfig?.eslintConfig && // Creates .eslintrc only if package.json's eslintConfig field is empty
Object.entries(packageJsonConfig?.eslintConfig).length === 0 if (Object.entries(packageJsonConfig?.eslintConfig).length === 0) {
) { packageJsonConfig.eslintConfig = defaultConfig
packageJsonConfig.eslintConfig = defaultConfig
if (pkgJsonPath) if (pkgJsonPath)
await fs.writeFile( await fs.writeFile(
pkgJsonPath, pkgJsonPath,
CommentJson.stringify(packageJsonConfig, null, 2) + os.EOL CommentJson.stringify(packageJsonConfig, null, 2) + os.EOL
) )
console.log( console.log(
chalk.green( chalk.green(
`We detected an empty ${chalk.bold( `We detected an empty ${chalk.bold(
'eslintConfig' 'eslintConfig'
)} field in package.json and updated it for you to include the base Next.js ESLint configuration.` )} field in package.json and updated it for you to include the base Next.js ESLint configuration.`
)
) )
) }
} else { } else {
await fs.writeFile( await fs.writeFile(
'.eslintrc', '.eslintrc',
......
{
"eslintConfig": {
"extends": "next",
"root": true
}
}
export default class Test {
render() {
return (
<div>
<h1>Hello title</h1>
</div>
)
}
}
import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import findUp from 'next/dist/compiled/find-up'
import { nextBuild, nextLint } from 'next-test-utils' import { nextBuild, nextLint } from 'next-test-utils'
import { writeFile, readFile } from 'fs-extra' import { writeFile, readFile } from 'fs-extra'
...@@ -8,6 +10,7 @@ const dirFirstTimeSetup = join(__dirname, '../first-time-setup') ...@@ -8,6 +10,7 @@ const dirFirstTimeSetup = join(__dirname, '../first-time-setup')
const dirCustomConfig = join(__dirname, '../custom-config') const dirCustomConfig = join(__dirname, '../custom-config')
const dirIgnoreDuringBuilds = join(__dirname, '../ignore-during-builds') const dirIgnoreDuringBuilds = join(__dirname, '../ignore-during-builds')
const dirCustomDirectories = join(__dirname, '../custom-directories') const dirCustomDirectories = join(__dirname, '../custom-directories')
const dirConfigInPackageJson = join(__dirname, '../config-in-package-json')
describe('ESLint', () => { describe('ESLint', () => {
describe('Next Build', () => { describe('Next Build', () => {
...@@ -113,5 +116,43 @@ describe('ESLint', () => { ...@@ -113,5 +116,43 @@ describe('ESLint', () => {
const output = stdout + stderr const output = stdout + stderr
expect(output).toContain('No ESLint warnings or errors') 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)
}
}
})
}) })
}) })
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