Skip to content

chore(lint): use console[xyz] over process #274

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

Merged
merged 2 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/linter/reporters/console.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export default issue => {
? ` (${issue.location.position.start.line}:${issue.location.position.end.line})`
: '';

process.stdout.write(
console[issue.level](
styleText(
levelToColorMap[issue.level],
`${issue.message} at ${issue.location.path}${position}`
) + '\n'
)
);
};
44 changes: 27 additions & 17 deletions src/linter/tests/reporters/console.test.mjs
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
import { describe, it } from 'node:test';
import console from '../../reporters/console.mjs';
import assert from 'node:assert';
import reporter from '../../reporters/console.mjs';
import { errorIssue, infoIssue, warnIssue } from '../fixtures/issues.mjs';

describe('console', () => {
it('should write to stdout with the correct colors based on the issue level', t => {
t.mock.method(process.stdout, 'write');

console(infoIssue);
console(warnIssue);
console(errorIssue);
const testCases = [
{
issue: infoIssue,
method: 'info',
expected: '\x1B[90mThis is a INFO issue at doc/api/test.md\x1B[39m',
},
{
issue: warnIssue,
method: 'warn',
expected: '\x1B[33mThis is a WARN issue at doc/api/test.md (1:1)\x1B[39m',
},
{
issue: errorIssue,
method: 'error',
expected: '\x1B[31mThis is a ERROR issue at doc/api/test.md (1:1)\x1B[39m',
},
];

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

const callsArgs = process.stdout.write.mock.calls.map(call =>
call.arguments[0].trim()
);
reporter(issue);

assert.deepStrictEqual(callsArgs, [
'\x1B[90mThis is a INFO issue at doc/api/test.md\x1B[39m',
'\x1B[33mThis is a WARN issue at doc/api/test.md (1:1)\x1B[39m',
'\x1B[31mThis is a ERROR issue at doc/api/test.md (1:1)\x1B[39m',
]);
assert.strictEqual(mock.callCount(), 1);
assert.deepStrictEqual(mock.calls[0].arguments, [expected]);
});
});
});
Loading