From 571fde1b0be51f00eead68a6bf66757564591afc Mon Sep 17 00:00:00 2001 From: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> Date: Sat, 1 May 2021 18:51:50 +0900 Subject: [PATCH] Fix invalid json for max warnings option (#5267) * Output max warnings message to STDERR I think stylelint should output any error or warning messages to STDERR instead of STDOUT. Because users should expect that *piping* is available. For example, here is an example of using the `jq` command: ```console $ stylelint --formatter=json --max-warnings=0 a.css | jq . Max warnings exceeded: 1 found. 0 allowed [ { "source": "/tmp/a.css", "deprecations": [], "invalidOptionWarnings": [], "parseErrors": [], "errored": false, "warnings": [ { "line": 1, "column": 2, "rule": "block-no-empty", "severity": "warning", "text": "Unexpected empty block (block-no-empty)" } ] } ] ``` * Use `fixturesPath()` --- lib/__tests__/cli.test.js | 24 +++++++++++++++++++ .../fixtures/default-severity-warning.json | 6 +++++ lib/cli.js | 4 ++-- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 lib/__tests__/fixtures/default-severity-warning.json diff --git a/lib/__tests__/cli.test.js b/lib/__tests__/cli.test.js index 3aa5070084..7ba07ac993 100644 --- a/lib/__tests__/cli.test.js +++ b/lib/__tests__/cli.test.js @@ -3,6 +3,7 @@ 'use strict'; const path = require('path'); +const stripAnsi = require('strip-ansi'); const cli = require('../cli'); const pkg = require('../../package.json'); @@ -296,4 +297,27 @@ describe('CLI', () => { expect.stringContaining('Cannot find module'), ); }); + + it('outputs a valid JSON even if max warnings exceeded', async () => { + await cli([ + '--formatter=json', + '--max-warnings=0', + '--config', + fixturesPath('default-severity-warning.json'), + fixturesPath('empty-block.css'), + ]); + + expect(process.exitCode).toEqual(2); + + expect(process.stdout.write).toHaveBeenCalledTimes(1); + const output = JSON.parse(process.stdout.write.mock.calls[0][0]); + + expect(output).toBeInstanceOf(Array); + expect(output[0]).toHaveProperty('source', expect.stringContaining('empty-block.css')); + + expect(process.stderr.write).toHaveBeenCalledTimes(1); + expect(stripAnsi(process.stderr.write.mock.calls[0][0])).toContain( + 'Max warnings exceeded: 1 found. 0 allowed', + ); + }); }); diff --git a/lib/__tests__/fixtures/default-severity-warning.json b/lib/__tests__/fixtures/default-severity-warning.json new file mode 100644 index 0000000000..9ec0f60359 --- /dev/null +++ b/lib/__tests__/fixtures/default-severity-warning.json @@ -0,0 +1,6 @@ +{ + "defaultSeverity": "warning", + "rules": { + "block-no-empty": true + } +} diff --git a/lib/cli.js b/lib/cli.js index 958162c603..efaaea2aba 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -509,8 +509,8 @@ module.exports = (argv) => { } else if (maxWarnings !== undefined && linted.maxWarningsExceeded) { const foundWarnings = linted.maxWarningsExceeded.foundWarnings; - process.stdout.write( - `${chalk.red(`Max warnings exceeded: `)}${foundWarnings} found. ${chalk.dim( + process.stderr.write( + `${EOL}${chalk.red(`Max warnings exceeded: `)}${foundWarnings} found. ${chalk.dim( `${maxWarnings} allowed${EOL}${EOL}`, )}`, );