Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jun 29, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
adds --max-warnings flag to next lint
housseindjirdeh committed Jun 28, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit ce9b474898d9509f40a773004362f3785a9cd0bb
14 changes: 12 additions & 2 deletions packages/next/cli/next-lint.ts
Original file line number Diff line number Diff line change
@@ -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
19 changes: 14 additions & 5 deletions packages/next/lib/eslint/runLintCheck.ts
Original file line number Diff line number Diff line change
@@ -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
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
@@ -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'
)
})
})
})