Unverified Commit 078cfb59 authored by Houssein Djirdeh's avatar Houssein Djirdeh Committed by GitHub

[ESLint] Adds `--format` flag to `next lint` (#27052)

Adds `--format` support to `next lint` to allow defining of additional formatters. Fixes #26387.
parent fac083bc
...@@ -63,9 +63,11 @@ const nextLint: cliCommand = (argv) => { ...@@ -63,9 +63,11 @@ const nextLint: cliCommand = (argv) => {
'--cache': Boolean, '--cache': Boolean,
'--cache-location': String, '--cache-location': String,
'--error-on-unmatched-pattern': Boolean, '--error-on-unmatched-pattern': Boolean,
'--format': String,
// Aliases // Aliases
'-c': '--config', '-c': '--config',
'-f': '--format',
} }
let args: arg.Result<arg.Spec> let args: arg.Result<arg.Spec>
...@@ -112,6 +114,9 @@ const nextLint: cliCommand = (argv) => { ...@@ -112,6 +114,9 @@ const nextLint: cliCommand = (argv) => {
Handling warnings: Handling warnings:
--quiet Report errors only - default: false --quiet Report errors only - default: false
--max-warnings Int Number of warnings to trigger nonzero exit code - default: -1 --max-warnings Int Number of warnings to trigger nonzero exit code - default: -1
Output:
-f, --format String Use a specific output format - default: Next.js custom formatter
Inline configuration comments: Inline configuration comments:
--no-inline-config Prevent comments from changing config or rules --no-inline-config Prevent comments from changing config or rules
...@@ -148,6 +153,7 @@ const nextLint: cliCommand = (argv) => { ...@@ -148,6 +153,7 @@ const nextLint: cliCommand = (argv) => {
const reportErrorsOnly = Boolean(args['--quiet']) const reportErrorsOnly = Boolean(args['--quiet'])
const maxWarnings = args['--max-warnings'] ?? -1 const maxWarnings = args['--max-warnings'] ?? -1
const formatter = args['--format'] || null
runLintCheck( runLintCheck(
baseDir, baseDir,
...@@ -155,7 +161,8 @@ const nextLint: cliCommand = (argv) => { ...@@ -155,7 +161,8 @@ const nextLint: cliCommand = (argv) => {
false, false,
eslintOptions(args), eslintOptions(args),
reportErrorsOnly, reportErrorsOnly,
maxWarnings maxWarnings,
formatter
) )
.then(async (lintResults) => { .then(async (lintResults) => {
const lintOutput = const lintOutput =
......
...@@ -24,15 +24,35 @@ export interface LintResult { ...@@ -24,15 +24,35 @@ export interface LintResult {
source?: string source?: string
} }
function pluginCount(
messages: LintMessage[]
): { nextPluginErrorCount: number; nextPluginWarningCount: number } {
let nextPluginWarningCount = 0
let nextPluginErrorCount = 0
for (let i = 0; i < messages.length; i++) {
const { severity, ruleId } = messages[i]
if (ruleId?.includes('@next/next')) {
if (severity === MessageSeverity.Warning) {
nextPluginWarningCount += 1
} else {
nextPluginErrorCount += 1
}
}
}
return {
nextPluginErrorCount,
nextPluginWarningCount,
}
}
function formatMessage( function formatMessage(
dir: string, dir: string,
messages: LintMessage[], messages: LintMessage[],
filePath: string filePath: string
): { ): string {
output: string
nextPluginErrorCount: number
nextPluginWarningCount: number
} {
let fileName = path.posix.normalize( let fileName = path.posix.normalize(
path.relative(dir, filePath).replace(/\\/g, '/') path.relative(dir, filePath).replace(/\\/g, '/')
) )
...@@ -42,8 +62,6 @@ function formatMessage( ...@@ -42,8 +62,6 @@ function formatMessage(
} }
let output = '\n' + chalk.cyan(fileName) let output = '\n' + chalk.cyan(fileName)
let nextPluginWarningCount = 0
let nextPluginErrorCount = 0
for (let i = 0; i < messages.length; i++) { for (let i = 0; i < messages.length; i++) {
const { message, severity, line, column, ruleId } = messages[i] const { message, severity, line, column, ruleId } = messages[i]
...@@ -59,14 +77,6 @@ function formatMessage( ...@@ -59,14 +77,6 @@ function formatMessage(
' ' ' '
} }
if (ruleId?.includes('@next/next')) {
if (severity === MessageSeverity.Warning) {
nextPluginWarningCount += 1
} else {
nextPluginErrorCount += 1
}
}
if (severity === MessageSeverity.Warning) { if (severity === MessageSeverity.Warning) {
output += chalk.yellow.bold('Warning') + ': ' output += chalk.yellow.bold('Warning') + ': '
} else { } else {
...@@ -80,16 +90,13 @@ function formatMessage( ...@@ -80,16 +90,13 @@ function formatMessage(
} }
} }
return { return output
output,
nextPluginErrorCount,
nextPluginWarningCount,
}
} }
export function formatResults( export function formatResults(
baseDir: string, baseDir: string,
results: LintResult[] results: LintResult[],
format: (r: LintResult[]) => string
): { ): {
output: string output: string
totalNextPluginErrorCount: number totalNextPluginErrorCount: number
...@@ -97,21 +104,28 @@ export function formatResults( ...@@ -97,21 +104,28 @@ export function formatResults(
} { } {
let totalNextPluginErrorCount = 0 let totalNextPluginErrorCount = 0
let totalNextPluginWarningCount = 0 let totalNextPluginWarningCount = 0
let resultsWithMessages = results.filter(({ messages }) => messages?.length)
const formattedResults = results
.filter(({ messages }) => messages?.length) // Track number of Next.js plugin errors and warnings
.map(({ messages, filePath }) => { resultsWithMessages.forEach(({ messages }) => {
const res = formatMessage(baseDir, messages, filePath) const res = pluginCount(messages)
totalNextPluginErrorCount += res.nextPluginErrorCount totalNextPluginErrorCount += res.nextPluginErrorCount
totalNextPluginWarningCount += res.nextPluginWarningCount totalNextPluginWarningCount += res.nextPluginWarningCount
return res.output })
})
.join('\n') // Use user defined formatter or Next.js's built-in custom formatter
const output = format
? format(resultsWithMessages)
: resultsWithMessages
.map(({ messages, filePath }) =>
formatMessage(baseDir, messages, filePath)
)
.join('\n')
return { return {
output: output:
formattedResults.length > 0 resultsWithMessages.length > 0
? formattedResults + ? output +
`\n\n${chalk.bold( `\n\n${chalk.bold(
'Need to disable some ESLint rules? Learn more here:' 'Need to disable some ESLint rules? Learn more here:'
)} https://nextjs.org/docs/basic-features/eslint#disabling-rules\n` )} https://nextjs.org/docs/basic-features/eslint#disabling-rules\n`
......
...@@ -30,7 +30,8 @@ async function lint( ...@@ -30,7 +30,8 @@ async function lint(
pkgJsonPath: string | null, pkgJsonPath: string | null,
eslintOptions: any = null, eslintOptions: any = null,
reportErrorsOnly: boolean = false, reportErrorsOnly: boolean = false,
maxWarnings: number = -1 maxWarnings: number = -1,
formatter: string | null = null
): Promise< ): Promise<
| string | string
| null | null
...@@ -111,12 +112,18 @@ async function lint( ...@@ -111,12 +112,18 @@ async function lint(
} }
} }
const lintStart = process.hrtime() const lintStart = process.hrtime()
let results = await eslint.lintFiles(lintDirs) let results = await eslint.lintFiles(lintDirs)
let selectedFormatter = null
if (options.fix) await ESLint.outputFixes(results) if (options.fix) await ESLint.outputFixes(results)
if (reportErrorsOnly) results = await ESLint.getErrorResults(results) // Only return errors if --quiet flag is used if (reportErrorsOnly) results = await ESLint.getErrorResults(results) // Only return errors if --quiet flag is used
const formattedResult = formatResults(baseDir, results) if (formatter) selectedFormatter = await eslint.loadFormatter(formatter)
const formattedResult = formatResults(
baseDir,
results,
selectedFormatter?.format
)
const lintEnd = process.hrtime(lintStart) const lintEnd = process.hrtime(lintStart)
const totalWarnings = results.reduce( const totalWarnings = results.reduce(
(sum: number, file: LintResult) => sum + file.warningCount, (sum: number, file: LintResult) => sum + file.warningCount,
...@@ -152,7 +159,8 @@ export async function runLintCheck( ...@@ -152,7 +159,8 @@ export async function runLintCheck(
lintDuringBuild: boolean = false, lintDuringBuild: boolean = false,
eslintOptions: any = null, eslintOptions: any = null,
reportErrorsOnly: boolean = false, reportErrorsOnly: boolean = false,
maxWarnings: number = -1 maxWarnings: number = -1,
formatter: string | null = null
): ReturnType<typeof lint> { ): ReturnType<typeof lint> {
try { try {
// Find user's .eslintrc file // Find user's .eslintrc file
...@@ -215,7 +223,8 @@ export async function runLintCheck( ...@@ -215,7 +223,8 @@ export async function runLintCheck(
pkgJsonPath, pkgJsonPath,
eslintOptions, eslintOptions,
reportErrorsOnly, reportErrorsOnly,
maxWarnings maxWarnings,
formatter
) )
} catch (err) { } catch (err) {
throw err throw err
......
...@@ -305,5 +305,23 @@ describe('ESLint', () => { ...@@ -305,5 +305,23 @@ describe('ESLint', () => {
'Warning: External synchronous scripts are forbidden' 'Warning: External synchronous scripts are forbidden'
) )
}) })
test('format flag supports additional user-defined formats', async () => {
const { stdout, stderr } = await nextLint(
dirMaxWarnings,
['-f', 'codeframe'],
{
stdout: true,
stderr: true,
}
)
const output = stdout + stderr
expect(output).toContain(
'warning: External synchronous scripts are forbidden'
)
expect(stdout).toContain('<script src="https://example.com" />')
expect(stdout).toContain('2 warnings found')
})
}) })
}) })
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