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 ...@@ -54,22 +54,24 @@ If you do not want ESLint to run as a build step, refer to the documentation for
## Linting Custom Directories ## 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 ```js
module.exports = { module.exports = {
eslint: { 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 ```bash
yarn lint --dir pages --dir utils yarn lint --dir pages --dir utils
``` ```
This will take precedence over the configuration from `next.config.js`.
## ESLint Plugin ## 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: 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) => ({ ...@@ -33,7 +33,7 @@ const eslintOptions = (args: arg.Spec) => ({
: false, : false,
}) })
const nextLint: cliCommand = (argv) => { const nextLint: cliCommand = async (argv) => {
const validArgs: arg.Spec = { const validArgs: arg.Spec = {
// Types // Types
'--help': Boolean, '--help': Boolean,
...@@ -140,7 +140,9 @@ const nextLint: cliCommand = (argv) => { ...@@ -140,7 +140,9 @@ const nextLint: cliCommand = (argv) => {
printAndExit(`> No such directory exists as the project root: ${baseDir}`) 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( const lintDirs = (dirs ?? ESLINT_DEFAULT_DIRS).reduce(
(res: string[], d: string) => { (res: string[], d: string) => {
const currDir = join(baseDir, d) const currDir = join(baseDir, d)
...@@ -169,7 +171,6 @@ const nextLint: cliCommand = (argv) => { ...@@ -169,7 +171,6 @@ const nextLint: cliCommand = (argv) => {
typeof lintResults === 'string' ? lintResults : lintResults?.output typeof lintResults === 'string' ? lintResults : lintResults?.output
if (typeof lintResults !== 'string' && lintResults?.eventInfo) { if (typeof lintResults !== 'string' && lintResults?.eventInfo) {
const conf = await loadConfig(PHASE_PRODUCTION_BUILD, baseDir)
const telemetry = new Telemetry({ const telemetry = new Telemetry({
distDir: join(baseDir, conf.distDir), distDir: join(baseDir, conf.distDir),
}) })
......
...@@ -268,6 +268,21 @@ describe('ESLint', () => { ...@@ -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 () => { test('max warnings flag errors when warnings exceed threshold', async () => {
const { stdout, stderr } = await nextLint( const { stdout, stderr } = await nextLint(
dirMaxWarnings, 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