Skip to content

Commit

Permalink
Update vfile
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 10, 2023
1 parent d42d584 commit 5481a46
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 39 deletions.
20 changes: 8 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

import stringWidth from 'string-width'
import {stringifyPosition} from 'unist-util-stringify-position'
import {sort} from 'vfile-sort'
import {compareFile, compareMessage} from 'vfile-sort'
import {statistics} from 'vfile-statistics'
import {color} from './color.js'

Expand Down Expand Up @@ -118,14 +118,16 @@ export function reporter(files, options) {
/**
* Parse a list of messages.
*
* @param {Array<VFile>} files
* @param {Array<VFile>} rawFiles
* List of files.
* @param {Options} options
* Options.
* @returns {Info}
* Rows.
*/
function transform(files, options) {
function transform(rawFiles, options) {
// To do: when Node 18 is EOL, use `toSorted`.
const files = rawFiles.sort(compareFile)
/** @type {Array<FileRow | MessageRow>} */
const rows = []
/** @type {Array<VFileMessage>} */
Expand All @@ -135,8 +137,8 @@ function transform(files, options) {
let index = -1

while (++index < files.length) {
// @ts-expect-error it works fine.
const messages = sort({messages: [...files[index].messages]}).messages
// To do: when Node 18 is EOL, use `toSorted`.
const messages = [...files[index].messages].sort(compareMessage)
/** @type {Array<MessageRow>} */
const messageRows = []
let offset = -1
Expand All @@ -148,13 +150,7 @@ function transform(files, options) {
all.push(message)

const row = {
place: stringifyPosition(
message.position
? message.position.end.line && message.position.end.column
? message.position
: message.position.start
: undefined
),
place: stringifyPosition(message.place),
label: labels[/** @type {keyof labels} */ (String(message.fatal))],
reason:
(message.stack || message.message) +
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
"string-width": "^6.0.0",
"supports-color": "^9.0.0",
"unist-util-stringify-position": "^3.0.0",
"vfile": "^5.0.0",
"vfile-message": "^3.0.0",
"vfile-sort": "^3.0.0",
"vfile-statistics": "^2.0.0"
"vfile": "^6.0.0",
"vfile-message": "^4.0.0",
"vfile-sort": "^4.0.0",
"vfile-statistics": "^3.0.0"
},
"devDependencies": {
"@types/node": "^20.0.0",
Expand Down
46 changes: 23 additions & 23 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ test('reporter', async function () {
strip(reporter([file, new VFile({path: 'b.js'})])),
[
'a.js',
' 1:1 warning Warning!',
' warning Warning!',
'',
'b.js: no issues found',
'',
Expand All @@ -121,7 +121,7 @@ test('reporter', async function () {
strip(reporter([file, new VFile({path: 'b.js'})])),
[
'a.js',
' 1:1 error Error!',
' error Error!',
'',
'b.js: no issues found',
'',
Expand Down Expand Up @@ -149,12 +149,12 @@ test('reporter', async function () {
strip(reporter(file)),
[
'a.js',
' 1:1 error Another error!',
' 1:1 error Error!',
' 1:1 warning Another warning!',
' 1:1 warning Note!',
' 1:1 warning Warning!',
' 1:1 info Another note!',
' error Another error!',
' error Error!',
' warning Another warning!',
' warning Note!',
' warning Warning!',
' info Another note!',
'',
'6 messages (✖ 2 errors, ⚠ 3 warnings)'
].join('\n'),
Expand All @@ -165,12 +165,12 @@ test('reporter', async function () {
reporter(file, {color: false}),
[
'a.js',
' 1:1 error Another error!',
' 1:1 error Error!',
' 1:1 warning Another warning!',
' 1:1 warning Note!',
' 1:1 warning Warning!',
' 1:1 info Another note!',
' error Another error!',
' error Error!',
' warning Another warning!',
' warning Note!',
' warning Warning!',
' info Another note!',
'',
'6 messages (✖ 2 errors, ⚠ 3 warnings)'
].join('\n'),
Expand Down Expand Up @@ -230,7 +230,7 @@ test('reporter', async function () {
cleanStack(strip(reporter(file)), 3),
[
'test.js',
' 1:1 error ReferenceError: variable is not defined',
' error ReferenceError: variable is not defined',
' at test.js:1:1'
].join('\n'),
'should support a “real” error (show a stack)'
Expand All @@ -246,7 +246,7 @@ test('reporter', async function () {
cleanStack(strip(reporter(file)), 3),
[
'test.js',
' 1:1 error ReferenceError: variable is not defined bar foo',
' error ReferenceError: variable is not defined bar foo',
' at test.js:1:1'
].join('\n'),
'should properly align a real error with a source'
Expand All @@ -260,7 +260,7 @@ test('reporter', async function () {

assert.equal(
cleanStack(strip(reporter(file)), 3),
'test.js\n 1:1 error ReferenceError: foo\n at test.js:1:1',
'test.js\n error ReferenceError: foo\n at test.js:1:1',
'should support a “real” error with a changed message'
)

Expand All @@ -274,7 +274,7 @@ test('reporter', async function () {
cleanStack(strip(reporter(file)), 5),
[
'test.js',
' 1:1 error ReferenceError: foo',
' error ReferenceError: foo',
'bar',
'baz',
' at test.js:1:1'
Expand All @@ -292,7 +292,7 @@ test('reporter', async function () {
cleanStack(strip(reporter(file)), 5),
[
'test.js',
' 1:1 error ReferenceError: foo bravo alpha',
' error ReferenceError: foo bravo alpha',
'bar',
'baz',
' at test.js:1:1'
Expand All @@ -309,8 +309,8 @@ test('reporter', async function () {
strip(reporter(file, {verbose: true})),
[
'a.js',
' 1:1 warning ...and some more warnings',
' 1:1 warning Whoops',
' warning ...and some more warnings',
' warning Whoops',
'Lorem ipsum dolor sit amet.',
'',
'⚠ 2 warnings'
Expand All @@ -323,7 +323,7 @@ test('reporter', async function () {

assert.equal(
strip(reporter([file, new VFile({path: 'b.js'})], {quiet: true})),
['a.js', ' 1:1 warning Warning!', '', '⚠ 1 warning'].join('\n'),
['a.js', ' warning Warning!', '', '⚠ 1 warning'].join('\n'),
'should ignore successful files in `quiet` mode'
)

Expand All @@ -338,7 +338,7 @@ test('reporter', async function () {

assert.equal(
strip(reporter([file, fileB], {silent: true})),
['a.js', ' 1:1 error Error!', '', '✖ 1 error'].join('\n'),
['a.js', ' error Error!', '', '✖ 1 error'].join('\n'),
'should ignore non-failures in `silent` mode'
)

Expand Down

0 comments on commit 5481a46

Please sign in to comment.