-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
195 lines (162 loc) · 4.51 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* @typedef {import('mdast').Root} Root
*/
import assert from 'node:assert/strict'
import test from 'node:test'
import {VFileMessage} from 'vfile-message'
/* eslint-disable no-undef */
/** @type {Error} */
let exception
/** @type {Error} */
let changedMessage
/** @type {Error} */
let multilineException
try {
// @ts-expect-error: we want to capture this error.
variable = 1
} catch (error_) {
const error = /** @type {Error} */ (error_)
exception = error
}
try {
// @ts-expect-error: we want to capture this error.
variable = 1
} catch (error_) {
const error = /** @type {Error} */ (error_)
error.message = 'foo'
changedMessage = error
}
try {
// @ts-expect-error: we want to capture this error.
variable = 1
} catch (error_) {
const error = /** @type {Error} */ (error_)
error.message = 'foo\nbar\nbaz'
multilineException = error
}
/* eslint-enable no-undef */
test('VFileMessage', async function () {
assert.deepEqual(
Object.keys(await import('vfile-message')).sort(),
['VFileMessage'],
'should expose the public api'
)
assert.ok(new VFileMessage('') instanceof Error, 'should return an Error')
const m1 = new VFileMessage('Foo')
assert.equal(m1.name, '1:1')
assert.equal(m1.file, '')
assert.equal(m1.reason, 'Foo')
assert.equal(m1.ruleId, undefined)
assert.equal(m1.source, undefined)
assert.equal(m1.stack, '')
assert.equal(m1.fatal, undefined)
assert.equal(m1.line, undefined)
assert.equal(m1.column, undefined)
assert.deepEqual(m1.place, undefined)
assert.equal(
String(m1),
'1:1: Foo',
'should have a pretty `toString()` message'
)
const m2 = new VFileMessage(exception)
assert.equal(
m2.message,
'variable is not defined',
'should accept an error (1)'
)
assert.equal(
String(m2.stack || '').split('\n')[0],
'ReferenceError: variable is not defined',
'should accept an error (2)'
)
const m3 = new VFileMessage(changedMessage)
assert.equal(m3.message, 'foo', 'should accept a changed error (1)')
assert.equal(
String(m3.stack || '').split('\n')[0],
'ReferenceError: foo',
'should accept a changed error (2)'
)
const m4 = new VFileMessage(multilineException)
assert.equal(
m4.message,
'foo\nbar\nbaz',
'should accept a multiline error (1)'
)
assert.equal(
String(m4.stack || '')
.split('\n')
.slice(0, 3)
.join('\n'),
'ReferenceError: foo\nbar\nbaz',
'should accept a multiline error (2)'
)
const literalNode = {
type: 'x',
position: {
start: {line: 2, column: 3},
end: {line: 2, column: 5}
}
}
const m5 = new VFileMessage('test', literalNode)
assert.deepEqual(m5.place, literalNode.position, 'should accept a node (1)')
assert.equal(String(m5), '2:3-2:5: test', 'should accept a node (2)')
assert.equal(
String(new VFileMessage('test', {type: 'x'})),
'1:1: test',
'should accept a node (3)'
)
assert.equal(
String(
new VFileMessage(
'xxx',
/** @type {Root} */ ({
type: 'root',
children: [],
position: {
start: {line: 1, column: 1},
end: {line: 2, column: 1}
}
})
)
),
'1:1-2:1: xxx',
'should accept a node (4, typed)'
)
const position = literalNode.position
const m6 = new VFileMessage('test', position)
assert.deepEqual(m6.place, position, 'should accept a position (1)')
assert.equal(String(m6), '2:3-2:5: test', 'should accept a position (2)')
const point = position.start
const m7 = new VFileMessage('test', point)
assert.deepEqual(m7.place, point, 'should accept a position (3)')
assert.equal(String(m7), '2:3: test', 'should accept a position (4)')
assert.equal(
new VFileMessage('test', 'charlie').ruleId,
'charlie',
'should accept a `ruleId` as `origin`'
)
const m8 = new VFileMessage('test', 'delta:echo')
assert.deepEqual(
[m8.source, m8.ruleId],
['delta', 'echo'],
'should accept a `source` and `ruleId` in `origin`'
)
const m9 = new VFileMessage('Something went wrong', {
ancestors: [literalNode],
cause: exception,
place: literalNode.position,
ruleId: 'my-rule',
source: 'my-package'
})
assert.equal(m9.reason, 'Something went wrong', 'should support options')
assert.deepEqual(
[m9.line, m9.column],
[2, 3],
'should support `options.place` for `line`/`column`'
)
assert.deepEqual(
[m9.source, m9.ruleId],
['my-package', 'my-rule'],
'should support `options.source`, `options.ruleId`'
)
})