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(next-eslint): .eslintrc.json not being created by next lint on App Router #55104

Merged
merged 6 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/app-dir/no-config/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/app-dir/no-config/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>
)
}
45 changes: 39 additions & 6 deletions test/integration/eslint/test/next-lint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const dirMaxWarnings = join(__dirname, '../max-warnings')
const dirEmptyDirectory = join(__dirname, '../empty-directory')
const dirEslintIgnore = join(__dirname, '../eslint-ignore')
const dirNoEslintPlugin = join(__dirname, '../no-eslint-plugin')
const dirNoConfig = join(__dirname, '../no-config')
const dirPageDirNoConfig = join(__dirname, '../no-config')
const dirAppDirNoConfig = join(__dirname, '../app-dir/no-config')
const dirEslintCache = join(__dirname, '../eslint-cache')
const dirEslintCacheCustomDir = join(__dirname, '../eslint-cache-custom-dir')
const dirFileLinting = join(__dirname, '../file-linting')
Expand All @@ -38,10 +39,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(isApp ? dirAppDirNoConfig : dirPageDirNoConfig, folder)
await setupCallback?.(folder)

try {
Expand Down Expand Up @@ -88,7 +89,7 @@ describe('Next Lint', () => {
{ packageManger: 'pnpm', lockFile: 'pnpm-lock.yaml' },
{ packageManger: 'npm', lockFile: 'package-lock.json' },
]) {
test(`installs eslint and eslint-config-next as devDependencies if missing with ${packageManger}`, async () => {
test(`Page Router - installs eslint and eslint-config-next as devDependencies if missing with ${packageManger}`, async () => {
const { stdout, pkgJson } = await nextLintTemp(async (folder) => {
await fs.writeFile(join(folder, lockFile), '')
})
Expand All @@ -102,9 +103,24 @@ describe('Next Lint', () => {
expect(pkgJson.devDependencies).toHaveProperty('eslint')
expect(pkgJson.devDependencies).toHaveProperty('eslint-config-next')
})

test(`App Router - installs eslint and eslint-config-next as devDependencies if missing with ${packageManger}`, async () => {
const { stdout, pkgJson } = await nextLintTemp(async (folder) => {
await fs.writeFile(join(folder, lockFile), '')
}, true)

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

test('creates .eslintrc.json file with a default configuration', async () => {
test('Page Router - creates .eslintrc.json file with a default configuration', async () => {
const { stdout, eslintrcJson } = await nextLintTemp()

expect(stdout).toContain(
Expand All @@ -113,13 +129,30 @@ describe('Next Lint', () => {
expect(eslintrcJson).toMatchObject({ extends: 'next/core-web-vitals' })
})

test('shows a successful message when completed', async () => {
test('App Router - creates .eslintrc.json file with a default configuration', async () => {
const { stdout, eslintrcJson } = await nextLintTemp(undefined, true)

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

test('Page Router - shows a successful message when completed', async () => {
const { stdout } = await nextLintTemp()

expect(stdout).toContain(
'ESLint has successfully been configured. Run next lint again to view warnings and errors'
)
})

test('App Router - shows a successful message when completed', async () => {
const { stdout } = await nextLintTemp(undefined, true)

expect(stdout).toContain(
'ESLint has successfully been configured. Run next lint again to view warnings and errors'
)
})
})

test('should generate next-env.d.ts before lint command', async () => {
Expand Down
Loading