Unverified Commit 15afd977 authored by Houssein Djirdeh's avatar Houssein Djirdeh Committed by GitHub

[ESLint] Adds --max-warnings flag to `next lint` (#26697)

Adds `--max-warnings` flag to `next lint` to enable setting of a maximum warning threshold.

Fixes #26671
parent 956bc2f3
......@@ -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,
......@@ -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
......@@ -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
......
......@@ -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 {
......@@ -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
......@@ -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,
......@@ -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
......@@ -205,7 +213,8 @@ export async function runLintCheck(
eslintrcFile,
pkgJsonPath,
eslintOptions,
reportErrorsOnly
reportErrorsOnly,
maxWarnings
)
} catch (err) {
throw err
......
{
"extends": "next",
"root": true,
"rules": {
"@next/next/no-html-link-for-pages": 0,
"@next/next/no-sync-scripts": 1
}
}
const About = () => (
<div>
<p>About</p>
<script src="https://example.com" />
</div>
)
export default About
const Home = () => (
<div>
<p>Home</p>
<script src="https://example.com" />
</div>
)
export default Home
......@@ -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', () => {
......@@ -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'
)
})
})
})
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