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(
)
)
}
} 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',
......
{
"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 findUp from 'next/dist/compiled/find-up'
import { nextBuild, nextLint } from 'next-test-utils'
import { writeFile, readFile } from 'fs-extra'
......@@ -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', () => {
......@@ -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)
}
}
})
})
})
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