Skip to content

Commit

Permalink
fix(next-eslint): .eslintrc.json not being created by next lint o…
Browse files Browse the repository at this point in the history
…n App Router (#55104)

This PR fixes `.eslintrc.json` not being created after the user runs
`next lint` and selects an option of `Strict` or `Base`.
The reason is that the lint check was looking for `pages` and
`src/pages` only. Added `app` and `src/app` to be checked also.

Fixes: #55094
Fixes: #55102

---------

Co-authored-by: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
devjiwonchoi and ijjk authored Feb 5, 2024
1 parent 4ba1f06 commit 4c53843
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/next/src/lib/eslint/runLintCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,9 @@ export async function runLintCheck(
// Write default ESLint config.
// Check for /pages and src/pages is to make sure this happens in Next.js folder
if (
existsSync(path.join(baseDir, 'pages')) ||
existsSync(path.join(baseDir, 'src/pages'))
['app', 'src/app', 'pages', 'src/pages'].some((dir) =>
existsSync(path.join(baseDir, dir))
)
) {
await writeDefaultConfig(
baseDir,
Expand Down
7 changes: 7 additions & 0 deletions test/integration/eslint/no-config/app/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
7 changes: 7 additions & 0 deletions test/integration/eslint/no-config/app/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Home() {
return (
<div>
<h1>Hello World!</h1>
</div>
)
}
36 changes: 34 additions & 2 deletions test/integration/eslint/test/next-lint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ const dirTypescript = join(__dirname, '../with-typescript')

describe('Next Lint', () => {
describe('First Time Setup ', () => {
async function nextLintTemp(setupCallback) {
async function nextLintTemp(setupCallback, isApp = false) {
const folder = join(os.tmpdir(), Math.random().toString(36).substring(2))
await fs.mkdirp(folder)
await fs.copy(dirNoConfig, folder)
await fs.copy(join(dirNoConfig, isApp ? 'app' : ''), folder)
await setupCallback?.(folder)

try {
Expand Down Expand Up @@ -101,6 +101,23 @@ describe('Next Lint', () => {
expect(stdout).toContain(packageManger)
expect(pkgJson.devDependencies).toHaveProperty('eslint')
expect(pkgJson.devDependencies).toHaveProperty('eslint-config-next')

// App Router
const { stdout: appStdout, pkgJson: appPkgJson } = await nextLintTemp(
async (folder) => {
await fs.writeFile(join(folder, lockFile), '')
},
true
)

expect(appStdout).toContain(
`Installing devDependencies (${packageManger}):`
)
expect(appStdout).toContain('eslint')
expect(appStdout).toContain('eslint-config-next')
expect(appStdout).toContain(packageManger)
expect(appPkgJson.devDependencies).toHaveProperty('eslint')
expect(appPkgJson.devDependencies).toHaveProperty('eslint-config-next')
})
}

Expand All @@ -111,6 +128,15 @@ describe('Next Lint', () => {
'We created the .eslintrc.json file for you and included your selected configuration'
)
expect(eslintrcJson).toMatchObject({ extends: 'next/core-web-vitals' })

// App Router
const { stdout: appStdout, eslintrcJson: appEslintrcJson } =
await nextLintTemp(null, true)

expect(appStdout).toContain(
'We created the .eslintrc.json file for you and included your selected configuration'
)
expect(appEslintrcJson).toMatchObject({ extends: 'next/core-web-vitals' })
})

test('shows a successful message when completed', async () => {
Expand All @@ -119,6 +145,12 @@ describe('Next Lint', () => {
expect(stdout).toContain(
'ESLint has successfully been configured. Run next lint again to view warnings and errors'
)

// App Router
const { stdout: appStdout } = await nextLintTemp(null, true)
expect(appStdout).toContain(
'ESLint has successfully been configured. Run next lint again to view warnings and errors'
)
})
})

Expand Down

0 comments on commit 4c53843

Please sign in to comment.