Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
[ESLint] Adds --max-warnings flag to next lint (vercel#26697)
Browse files Browse the repository at this point in the history
Adds `--max-warnings` flag to `next lint` to enable setting of a maximum warning threshold.

Fixes vercel#26671
  • Loading branch information
housseindjirdeh authored Jun 29, 2021
1 parent 18dfc4c commit 254b985
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 7 deletions.
14 changes: 12 additions & 2 deletions packages/next/cli/next-lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const nextLint: cliCommand = (argv) => {
'--ignore-path': String,
'--no-ignore': Boolean,
'--quiet': Boolean,
'--max-warnings': Number,
'--no-inline-config': Boolean,
'--report-unused-disable-directives': String,
'--cache': Boolean,
Expand Down Expand Up @@ -108,6 +109,7 @@ const nextLint: cliCommand = (argv) => {
Handling warnings:
--quiet Report errors only - default: false
--max-warnings Int Number of warnings to trigger nonzero exit code - default: -1
Inline configuration comments:
--no-inline-config Prevent comments from changing config or rules
Expand Down Expand Up @@ -143,8 +145,16 @@ const nextLint: cliCommand = (argv) => {
)

const reportErrorsOnly = Boolean(args['--quiet'])

runLintCheck(baseDir, lintDirs, false, eslintOptions(args), reportErrorsOnly)
const maxWarnings = args['--max-warnings'] ?? -1

runLintCheck(
baseDir,
lintDirs,
false,
eslintOptions(args),
reportErrorsOnly,
maxWarnings
)
.then(async (lintResults) => {
const lintOutput =
typeof lintResults === 'string' ? lintResults : lintResults?.output
Expand Down
19 changes: 14 additions & 5 deletions packages/next/lib/eslint/runLintCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import findUp from 'next/dist/compiled/find-up'
import semver from 'next/dist/compiled/semver'
import * as CommentJson from 'next/dist/compiled/comment-json'

import { formatResults } from './customFormatter'
import { LintResult, formatResults } from './customFormatter'
import { writeDefaultConfig } from './writeDefaultConfig'
import { existsSync, findPagesDir } from '../find-pages-dir'
import {
Expand All @@ -29,7 +29,8 @@ async function lint(
eslintrcFile: string | null,
pkgJsonPath: string | null,
eslintOptions: any = null,
reportErrorsOnly: boolean = false
reportErrorsOnly: boolean = false,
maxWarnings: number = -1
): Promise<
| string
| null
Expand Down Expand Up @@ -116,10 +117,16 @@ async function lint(

const formattedResult = formatResults(baseDir, results)
const lintEnd = process.hrtime(lintStart)
const totalWarnings = results.reduce(
(sum: number, file: LintResult) => sum + file.warningCount,
0
)

return {
output: formattedResult.output,
isError: ESLint.getErrorResults(results)?.length > 0,
isError:
ESLint.getErrorResults(results)?.length > 0 ||
(maxWarnings >= 0 && totalWarnings > maxWarnings),
eventInfo: {
durationInSeconds: lintEnd[0],
eslintVersion: eslintVersion,
Expand All @@ -143,7 +150,8 @@ export async function runLintCheck(
lintDirs: string[],
lintDuringBuild: boolean = false,
eslintOptions: any = null,
reportErrorsOnly: boolean = false
reportErrorsOnly: boolean = false,
maxWarnings: number = -1
): ReturnType<typeof lint> {
try {
// Find user's .eslintrc file
Expand Down Expand Up @@ -205,7 +213,8 @@ export async function runLintCheck(
eslintrcFile,
pkgJsonPath,
eslintOptions,
reportErrorsOnly
reportErrorsOnly,
maxWarnings
)
} catch (err) {
throw err
Expand Down
8 changes: 8 additions & 0 deletions test/integration/eslint/max-warnings/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "next",
"root": true,
"rules": {
"@next/next/no-html-link-for-pages": 0,
"@next/next/no-sync-scripts": 1
}
}
8 changes: 8 additions & 0 deletions test/integration/eslint/max-warnings/pages/about.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const About = () => (
<div>
<p>About</p>
<script src="https://example.com" />
</div>
)

export default About
8 changes: 8 additions & 0 deletions test/integration/eslint/max-warnings/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Home = () => (
<div>
<p>Home</p>
<script src="https://example.com" />
</div>
)

export default Home
39 changes: 39 additions & 0 deletions test/integration/eslint/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const dirIgnoreDuringBuilds = join(__dirname, '../ignore-during-builds')
const dirCustomDirectories = join(__dirname, '../custom-directories')
const dirConfigInPackageJson = join(__dirname, '../config-in-package-json')
const dirInvalidEslintVersion = join(__dirname, '../invalid-eslint-version')
const dirMaxWarnings = join(__dirname, '../max-warnings')

describe('ESLint', () => {
describe('Next Build', () => {
Expand Down Expand Up @@ -188,5 +189,43 @@ describe('ESLint', () => {
'Warning: External synchronous scripts are forbidden'
)
})

test('max warnings flag errors when warnings exceed threshold', async () => {
const { stdout, stderr } = await nextLint(
dirMaxWarnings,
['--max-warnings', 1],
{
stdout: true,
stderr: true,
}
)

expect(stderr).not.toEqual('')
expect(stderr).toContain(
'Warning: External synchronous scripts are forbidden'
)
expect(stdout).not.toContain(
'Warning: External synchronous scripts are forbidden'
)
})

test('max warnings flag does not error when warnings do not exceed threshold', async () => {
const { stdout, stderr } = await nextLint(
dirMaxWarnings,
['--max-warnings', 2],
{
stdout: true,
stderr: true,
}
)

expect(stderr).toEqual('')
expect(stderr).not.toContain(
'Warning: External synchronous scripts are forbidden'
)
expect(stdout).toContain(
'Warning: External synchronous scripts are forbidden'
)
})
})
})

0 comments on commit 254b985

Please sign in to comment.