Skip to content

Commit 16a36c6

Browse files
authored
chore(lint): use console[xyz] over process (#274)
1 parent 5d0e043 commit 16a36c6

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/linter/reporters/console.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ export default issue => {
2121
? ` (${issue.location.position.start.line}:${issue.location.position.end.line})`
2222
: '';
2323

24-
process.stdout.write(
24+
console[issue.level](
2525
styleText(
2626
levelToColorMap[issue.level],
2727
`${issue.message} at ${issue.location.path}${position}`
28-
) + '\n'
28+
)
2929
);
3030
};
Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
import { describe, it } from 'node:test';
2-
import console from '../../reporters/console.mjs';
32
import assert from 'node:assert';
3+
import reporter from '../../reporters/console.mjs';
44
import { errorIssue, infoIssue, warnIssue } from '../fixtures/issues.mjs';
55

6-
describe('console', () => {
7-
it('should write to stdout with the correct colors based on the issue level', t => {
8-
t.mock.method(process.stdout, 'write');
9-
10-
console(infoIssue);
11-
console(warnIssue);
12-
console(errorIssue);
6+
const testCases = [
7+
{
8+
issue: infoIssue,
9+
method: 'info',
10+
expected: '\x1B[90mThis is a INFO issue at doc/api/test.md\x1B[39m',
11+
},
12+
{
13+
issue: warnIssue,
14+
method: 'warn',
15+
expected: '\x1B[33mThis is a WARN issue at doc/api/test.md (1:1)\x1B[39m',
16+
},
17+
{
18+
issue: errorIssue,
19+
method: 'error',
20+
expected: '\x1B[31mThis is a ERROR issue at doc/api/test.md (1:1)\x1B[39m',
21+
},
22+
];
1323

14-
assert.strictEqual(process.stdout.write.mock.callCount(), 3);
24+
describe('console', () => {
25+
testCases.forEach(({ issue, method, expected }) => {
26+
it(`should use correct colors and output on ${method} issues`, t => {
27+
t.mock.method(console, method);
28+
const mock = console[method].mock;
1529

16-
const callsArgs = process.stdout.write.mock.calls.map(call =>
17-
call.arguments[0].trim()
18-
);
30+
reporter(issue);
1931

20-
assert.deepStrictEqual(callsArgs, [
21-
'\x1B[90mThis is a INFO issue at doc/api/test.md\x1B[39m',
22-
'\x1B[33mThis is a WARN issue at doc/api/test.md (1:1)\x1B[39m',
23-
'\x1B[31mThis is a ERROR issue at doc/api/test.md (1:1)\x1B[39m',
24-
]);
32+
assert.strictEqual(mock.callCount(), 1);
33+
assert.deepStrictEqual(mock.calls[0].arguments, [expected]);
34+
});
2535
});
2636
});

0 commit comments

Comments
 (0)