Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/canary' into my-errorMsg-contr…
Browse files Browse the repository at this point in the history
…ibution
  • Loading branch information
ijjk committed Jul 25, 2021
2 parents 7cbde57 + 7fe8a00 commit f3fe1d6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
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
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

0 comments on commit f3fe1d6

Please sign in to comment.