/
errors.js
441 lines (393 loc) · 15.3 KB
/
errors.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import * as YAML from 'yaml'
import { source } from '../_utils'
test('fail on map value indented with tab', () => {
const src = 'a:\n\t1\nb:\n\t2\n'
const doc = YAML.parseDocument(src)
expect(doc.errors).not.toHaveLength(0)
expect(() => String(doc)).toThrow(
'Document with errors cannot be stringified'
)
})
test('eemeli/yaml#6', () => {
const src = 'abc: 123\ndef'
const doc = YAML.parseDocument(src)
expect(doc.errors).toMatchObject([{ pos: [9, 12] }])
})
describe('eemeli/yaml#7', () => {
test('map', () => {
const src = '{ , }\n---\n{ 123,,, }\n'
const docs = YAML.parseAllDocuments(src)
expect(docs[0].errors).toMatchObject([{ pos: [2, 3] }])
expect(docs[1].errors).toMatchObject([{ pos: [16, 17] }, { pos: [17, 18] }])
})
test('seq', () => {
const src = '[ , ]\n---\n[ 123,,, ]\n'
const docs = YAML.parseAllDocuments(src)
expect(docs[0].errors).toMatchObject([{ pos: [2, 3] }])
expect(docs[1].errors).toMatchObject([{ pos: [16, 17] }, { pos: [17, 18] }])
})
})
describe('block scalars', () => {
test('invalid header', () => {
const doc = YAML.parseDocument('>99\n foo\n')
expect(doc.errors).toMatchObject(
[
'Block scalar header includes extra characters: >99',
'Unexpected scalar at node end'
].map(msg => ({ message: expect.stringContaining(msg) }))
)
})
test('missing newline at header end', () => {
const doc = YAML.parseDocument('> foo\n')
expect(doc.errors).toHaveLength(1)
expect(doc.errors[0].message).toMatch('Not a YAML token: foo')
})
})
describe('block collections', () => {
test('mapping with bad indentation', () => {
const src = 'foo: "1"\n bar: 2\n'
const doc = YAML.parseDocument(src)
expect(doc.errors).toHaveLength(1)
expect(doc.errors[0].message).toMatch(
'All mapping items must start at the same column'
)
expect(doc.contents).toMatchObject({
items: [
{ key: { value: 'foo' }, value: { value: '1' } },
{ key: { value: 'bar' }, value: { value: 2 } }
]
})
})
test('sequence with bad indentation', () => {
const src = '- "foo"\n - bar\n'
const doc = YAML.parseDocument(src)
expect(doc.errors).toHaveLength(1)
expect(doc.errors[0].message).toMatch(
'All sequence items must start at the same column'
)
expect(doc.contents).toMatchObject({
items: [{ value: 'foo' }, { items: [{ value: 'bar' }] }]
})
})
test('seq item in mapping', () => {
const src = 'foo: "1"\n- bar\n'
const doc = YAML.parseDocument(src)
expect(doc.errors).toMatchObject(
[
'A block sequence may not be used as an implicit map key',
'Implicit keys need to be on a single line',
'Implicit map keys need to be followed by map values'
].map(msg => ({ message: expect.stringContaining(msg) }))
)
expect(doc.contents).toMatchObject({
items: [
{ key: { value: 'foo' }, value: { value: '1' } },
{ key: { items: [{ value: 'bar' }] }, value: null }
]
})
})
test('doubled value indicator', () => {
const doc = YAML.parseDocument('foo : : bar\n')
expect(doc.errors).toMatchObject([
{
message: source`
Nested mappings are not allowed in compact mappings at line 1, column 7:
foo : : bar
^
`
}
])
})
test('excessively long key', () => {
const doc = YAML.parseDocument(`foo ${'x'.repeat(1024)} : bar\n`)
expect(doc.errors).toMatchObject([
{
message: source`
The : indicator must be at most 1024 chars after the start of an implicit block mapping key at line 1, column 1:
foo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx…
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`
}
])
})
test('key after long comment on empty value (eemeli/yaml#413)', () => {
const doc = YAML.parseDocument(source`
one:
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
two: b
`)
expect(doc.errors).toMatchObject([])
})
test('key after long comment on block map (eemeli/yaml#413)', () => {
const doc = YAML.parseDocument(source`
one:
sub: a
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
two: b
`)
expect(doc.errors).toMatchObject([])
})
test('key after long comment on block seq (eemeli/yaml#413)', () => {
const doc = YAML.parseDocument(source`
one:
- a
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
two: b
`)
expect(doc.errors).toMatchObject([])
})
})
describe('flow collections', () => {
test('start only of flow map (eemeli/yaml#8)', () => {
const doc = YAML.parseDocument('{')
expect(doc.errors).toMatchObject([{ code: 'MISSING_CHAR', pos: [1, 2] }])
})
test('start only of flow sequence (eemeli/yaml#8)', () => {
const doc = YAML.parseDocument('[')
expect(doc.errors).toMatchObject([{ code: 'MISSING_CHAR', pos: [1, 2] }])
})
test('flow sequence without end', () => {
const doc = YAML.parseDocument('[ foo, bar,')
expect(doc.errors).toMatchObject([{ code: 'MISSING_CHAR', pos: [11, 12] }])
})
test('doc-end within flow sequence', () => {
const doc = YAML.parseDocument('[ foo, bar,\n...\n]', {
prettyErrors: false
})
expect(doc.errors).toMatchObject([
{ code: 'MISSING_CHAR' },
{ message: 'Unexpected flow-seq-end token in YAML document: "]"' },
{
message:
'Source contains multiple documents; please use YAML.parseAllDocuments()'
}
])
})
test('block scalar in flow collection', () => {
const doc = YAML.parseDocument('{ |\n foo\n}')
expect(doc.errors).toHaveLength(1)
expect(doc.errors[0].message).toMatch(
'Plain value cannot start with block scalar indicator |'
)
})
test('block seq in flow collection', () => {
const doc = YAML.parseDocument('{\n- foo\n}')
expect(doc.errors).toMatchObject([{ code: 'BLOCK_IN_FLOW' }])
})
test('anchor before explicit key indicator in block map', () => {
const doc = YAML.parseDocument('&a ? A')
expect(doc.errors).toMatchObject([{ code: 'BAD_PROP_ORDER' }])
})
test('anchor before explicit key indicator in flow map', () => {
const doc = YAML.parseDocument('{ &a ? A }')
expect(doc.errors).toMatchObject([{ code: 'BAD_PROP_ORDER' }])
})
test('flow map with doubled indicator', () => {
const doc = YAML.parseDocument('{ foo: : bar }')
expect(doc.errors).toMatchObject([{ code: 'UNEXPECTED_TOKEN' }])
})
})
describe('comments', () => {
test('comment without whitespace after tag', () => {
const doc = YAML.parseDocument('!<a>#cc\nA')
expect(doc.errors).toHaveLength(2)
expect(doc.errors[0].message).toMatch(
'Tags and anchors must be separated from the next token by white space'
)
expect(doc.errors[1].message).toMatch(
'Comments must be separated from other tokens by white space characters'
)
})
test('comment without whitespace after value', () => {
const doc = YAML.parseDocument('foo: "bar"#cc')
expect(doc.errors).toHaveLength(1)
expect(doc.errors[0].message).toMatch(
'Comments must be separated from other tokens by white space characters'
)
})
})
describe('pretty errors', () => {
test('eemeli/yaml#6', () => {
const src = 'abc: 123\ndef'
const doc = YAML.parseDocument(src, { prettyErrors: true })
expect(doc.errors).toMatchObject([
{
message: source`
Implicit map keys need to be followed by map values at line 2, column 1:
abc: 123
def
^^^
`,
pos: [9, 12],
linePos: [
{ line: 2, col: 1 },
{ line: 2, col: 4 }
]
}
])
expect(doc.errors[0]).not.toHaveProperty('source')
})
test('eemeli/yaml#7 maps', () => {
const src = '{ , }\n---\n{ 123,,, }\n'
const docs = YAML.parseAllDocuments(src, { prettyErrors: true })
expect(docs[0].errors).toMatchObject([
{
message: source`
Unexpected , in flow map at line 1, column 3:
{ , }
^
`,
pos: [2, 3],
linePos: [
{ line: 1, col: 3 },
{ line: 1, col: 4 }
]
}
])
expect(docs[0].errors[0]).not.toHaveProperty('source')
expect(docs[1].errors).toMatchObject([
{
code: 'UNEXPECTED_TOKEN',
message: source`
Unexpected , in flow map at line 3, column 7:
{ 123,,, }
^
`,
pos: [16, 17],
linePos: [
{ line: 3, col: 7 },
{ line: 3, col: 8 }
]
},
{
code: 'UNEXPECTED_TOKEN',
message: source`
Unexpected , in flow map at line 3, column 8:
{ 123,,, }
^
`,
pos: [17, 18],
linePos: [
{ line: 3, col: 8 },
{ line: 3, col: 9 }
]
}
])
expect(docs[1].errors[0]).not.toHaveProperty('source')
expect(docs[1].errors[1]).not.toHaveProperty('source')
})
test('pretty warnings', () => {
const src = '%FOO\n---bar\n'
const doc = YAML.parseDocument(src, { prettyErrors: true })
expect(doc.warnings).toMatchObject([{ name: 'YAMLWarning' }])
})
test('repeated CR', () => {
const src = '[' + '\r'.repeat(80)
const doc = YAML.parseDocument(src, { prettyErrors: true })
expect(doc.errors[0]).not.toHaveProperty('source')
})
})
describe('tags on invalid nodes', () => {
test('!!map on scalar', () => {
const doc = YAML.parseDocument('!!map foo')
expect(doc.warnings).toHaveLength(1)
expect(doc.warnings[0].message).toMatch(
'Unresolved tag: tag:yaml.org,2002:map'
)
expect(doc.toJS()).toBe('foo')
})
test('!!str on map', () => {
const doc = YAML.parseDocument('!!str { answer: 42 }')
expect(doc.warnings).toHaveLength(1)
expect(doc.warnings[0].message).toMatch(
'Unresolved tag: tag:yaml.org,2002:str'
)
expect(doc.toJS()).toMatchObject({ answer: 42 })
})
})
describe('invalid options', () => {
test('unknown schema', () => {
expect(() => new YAML.Document(undefined, { schema: 'foo' })).toThrow(
/Unknown schema/
)
})
test('unknown custom tag', () => {
expect(() => new YAML.Document(undefined, { customTags: ['foo'] })).toThrow(
/Unknown custom tag/
)
})
})
test('broken document with comment before first node', () => {
const doc = YAML.parseDocument('#c\n*x\nfoo\n', { prettyErrors: false })
expect(doc.errors).toMatchObject([
{ message: 'Unexpected scalar at node end' }
])
})
test('multiple tags on one node', () => {
const doc = YAML.parseDocument('!foo !bar baz\n')
expect(doc.contents).toMatchObject({ value: 'baz', type: 'PLAIN' })
expect(doc.errors).toHaveLength(1)
expect(doc.warnings).toHaveLength(1)
})
describe('logLevel', () => {
// process.emitWarning will throw in Jest if `warning` is an Error instance
// due to https://github.com/facebook/jest/issues/2549
const mock = jest.spyOn(global.process, 'emitWarning').mockImplementation()
beforeEach(() => mock.mockClear())
afterEach(() => mock.mockRestore())
test('by default, warn for tag fallback', () => {
YAML.parse('!foo bar')
const message = source`
Unresolved tag: !foo at line 1, column 1:
!foo bar
^^^^
`
expect(mock.mock.calls).toMatchObject([[{ message }]])
})
test("silence warnings with logLevel: 'error'", () => {
YAML.parse('!foo bar', { logLevel: 'error' })
expect(mock).toHaveBeenCalledTimes(0)
})
test("silence warnings with logLevel: 'silent'", () => {
YAML.parse('!foo bar', { logLevel: 'silent' })
expect(mock).toHaveBeenCalledTimes(0)
})
test("silence errors with logLevel: 'silent'", () => {
const res = YAML.parse('foo: bar: baz\n---\ndoc2\n', { logLevel: 'silent' })
expect(res).toMatchObject({ foo: { bar: 'baz' } })
})
})
describe('Invalid plain first characters', () => {
for (const ch of [',', '%', '@', '`'])
test(ch, () => {
const doc = YAML.parseDocument(`- ${ch}foo`)
expect(doc.errors).toMatchObject([{ code: 'BAD_SCALAR_START' }])
})
})