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

ESLint: custom directories from next.config.js #26401

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions docs/basic-features/eslint.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,24 @@ If you do not want ESLint to run as a build step, refer to the documentation for

## Linting Custom Directories

By default, Next.js will run ESLint for all files in the `pages/`, `components/`, and `lib/` directories. However, you can specify which directories using the `dirs` option in the `eslint` config in `next.config.js` for production builds:
By default, Next.js will run ESLint for all files in the `pages/`, `components/`, and `lib/` directories. However, you can specify which directories using the `dirs` option in the `eslint` config in `next.config.js`:

```js
module.exports = {
eslint: {
dirs: ['pages', 'utils'], // Only run ESLint on the 'pages' and 'utils' directories during production builds (next build)
dirs: ['pages', 'utils'], // Only run ESLint on the 'pages' and 'utils' directories (next build and next lint)
},
}
```

Similarly, the `--dir` flag can be used for `next lint`:
Also, the `--dir` flag can be used for `next lint`:

```bash
yarn lint --dir pages --dir utils
```

This will take precedence over the configuration from `next.config.js`.

## ESLint Plugin

Next.js provides an ESLint plugin, [`eslint-plugin-next`](https://www.npmjs.com/package/@next/eslint-plugin-next), making it easier to catch common issues and problems in a Next.js application. The full set of rules is as follows:
Expand Down
7 changes: 4 additions & 3 deletions packages/next/cli/next-lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const eslintOptions = (args: arg.Spec) => ({
: false,
})

const nextLint: cliCommand = (argv) => {
const nextLint: cliCommand = async (argv) => {
const validArgs: arg.Spec = {
// Types
'--help': Boolean,
Expand Down Expand Up @@ -140,7 +140,9 @@ const nextLint: cliCommand = (argv) => {
printAndExit(`> No such directory exists as the project root: ${baseDir}`)
}

const dirs: string[] = args['--dir']
const conf = await loadConfig(PHASE_PRODUCTION_BUILD, baseDir)

const dirs: string[] = args['--dir'] ?? conf.eslint?.dirs
michalbundyra marked this conversation as resolved.
Show resolved Hide resolved
const lintDirs = (dirs ?? ESLINT_DEFAULT_DIRS).reduce(
(res: string[], d: string) => {
const currDir = join(baseDir, d)
Expand Down Expand Up @@ -169,7 +171,6 @@ const nextLint: cliCommand = (argv) => {
typeof lintResults === 'string' ? lintResults : lintResults?.output

if (typeof lintResults !== 'string' && lintResults?.eventInfo) {
const conf = await loadConfig(PHASE_PRODUCTION_BUILD, baseDir)
const telemetry = new Telemetry({
distDir: join(baseDir, conf.distDir),
})
Expand Down
15 changes: 15 additions & 0 deletions test/integration/eslint/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,21 @@ describe('ESLint', () => {
)
})

test('custom directories', async () => {
const { stdout, stderr } = await nextLint(dirCustomDirectories, [], {
stdout: true,
stderr: true,
})

const output = stdout + stderr
expect(output).toContain(
'Error: Comments inside children section of tag should be placed inside braces'
)
expect(output).toContain(
'Warning: External synchronous scripts are forbidden'
)
})

test('max warnings flag errors when warnings exceed threshold', async () => {
const { stdout, stderr } = await nextLint(
dirMaxWarnings,
Expand Down