Skip to content

Commit 66cb786

Browse files
vitalybaevijjk
andauthored
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: JJ Kasper <[email protected]>
1 parent 775c7be commit 66cb786

File tree

4 files changed

+72
-16
lines changed

4 files changed

+72
-16
lines changed

packages/next/lib/eslint/writeDefaultConfig.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,25 @@ export async function writeDefaultConfig(
4949
)
5050
)
5151
}
52-
} else if (
53-
packageJsonConfig?.eslintConfig &&
54-
Object.entries(packageJsonConfig?.eslintConfig).length === 0
55-
) {
56-
packageJsonConfig.eslintConfig = defaultConfig
52+
} else if (packageJsonConfig?.eslintConfig) {
53+
// Creates .eslintrc only if package.json's eslintConfig field is empty
54+
if (Object.entries(packageJsonConfig?.eslintConfig).length === 0) {
55+
packageJsonConfig.eslintConfig = defaultConfig
5756

58-
if (pkgJsonPath)
59-
await fs.writeFile(
60-
pkgJsonPath,
61-
CommentJson.stringify(packageJsonConfig, null, 2) + os.EOL
62-
)
57+
if (pkgJsonPath)
58+
await fs.writeFile(
59+
pkgJsonPath,
60+
CommentJson.stringify(packageJsonConfig, null, 2) + os.EOL
61+
)
6362

64-
console.log(
65-
chalk.green(
66-
`We detected an empty ${chalk.bold(
67-
'eslintConfig'
68-
)} field in package.json and updated it for you to include the base Next.js ESLint configuration.`
63+
console.log(
64+
chalk.green(
65+
`We detected an empty ${chalk.bold(
66+
'eslintConfig'
67+
)} field in package.json and updated it for you to include the base Next.js ESLint configuration.`
68+
)
6969
)
70-
)
70+
}
7171
} else {
7272
await fs.writeFile(
7373
'.eslintrc',
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"eslintConfig": {
3+
"extends": "next",
4+
"root": true
5+
}
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default class Test {
2+
render() {
3+
return (
4+
<div>
5+
<h1>Hello title</h1>
6+
</div>
7+
)
8+
}
9+
}

test/integration/eslint/test/index.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import fs from 'fs-extra'
12
import { join } from 'path'
3+
import findUp from 'next/dist/compiled/find-up'
24
import { nextBuild, nextLint } from 'next-test-utils'
35
import { writeFile, readFile } from 'fs-extra'
46

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

1215
describe('ESLint', () => {
1316
describe('Next Build', () => {
@@ -113,5 +116,43 @@ describe('ESLint', () => {
113116
const output = stdout + stderr
114117
expect(output).toContain('No ESLint warnings or errors')
115118
})
119+
120+
test("don't create .eslintrc file if package.json has eslintConfig field", async () => {
121+
const eslintrcFile =
122+
(await findUp(
123+
[
124+
'.eslintrc.js',
125+
'.eslintrc.yaml',
126+
'.eslintrc.yml',
127+
'.eslintrc.json',
128+
'.eslintrc',
129+
],
130+
{
131+
cwd: '.',
132+
}
133+
)) ?? null
134+
135+
try {
136+
// If we found a .eslintrc file, it's probably config from root Next.js directory. Rename it during the test
137+
if (eslintrcFile) {
138+
await fs.move(eslintrcFile, `${eslintrcFile}.original`)
139+
}
140+
141+
const { stdout, stderr } = await nextLint(dirConfigInPackageJson, [], {
142+
stdout: true,
143+
stderr: true,
144+
})
145+
146+
const output = stdout + stderr
147+
expect(output).not.toContain(
148+
'We created the .eslintrc file for you and included the base Next.js ESLint configuration'
149+
)
150+
} finally {
151+
// Restore original .eslintrc file
152+
if (eslintrcFile) {
153+
await fs.move(`${eslintrcFile}.original`, eslintrcFile)
154+
}
155+
}
156+
})
116157
})
117158
})

0 commit comments

Comments
 (0)