Unverified Commit 7fe8a001 authored by Michał Bundyra's avatar Michał Bundyra Committed by GitHub

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

Attempt to fix #26393

~~Sorry, I wasn't able to run tests on local, but I hope at least test should be valid.~~
Edit: Tests are working correctly on my local :) 

fixes #26393



## Bug

- [x] Related issues linked using `fixes #26393`
- [x] Integration tests added
parent 3b2732bc
......@@ -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:
......
......@@ -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,
......@@ -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
const lintDirs = (dirs ?? ESLINT_DEFAULT_DIRS).reduce(
(res: string[], d: string) => {
const currDir = join(baseDir, d)
......@@ -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),
})
......
......@@ -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,
......
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