Skip to content

Commit

Permalink
add index to sequence converter errors (#3178)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Apr 29, 2024
1 parent 41dc36c commit e009b0c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/web/fetch/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ webidl.sequenceConverter = function (converter) {
/** @type {Generator} */
const method = typeof Iterable === 'function' ? Iterable() : V?.[Symbol.iterator]?.()
const seq = []
let index = 0

// 3. If method is undefined, throw a TypeError.
if (
Expand All @@ -270,7 +271,7 @@ webidl.sequenceConverter = function (converter) {
break
}

seq.push(converter(value, prefix, argument))
seq.push(converter(value, prefix, `${argument}[${index++}]`))
}

return seq
Expand Down
4 changes: 2 additions & 2 deletions test/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test('Headers initialization', async (t) => {
throws(() => new Headers(['undici', 'fetch', 'fetch']), TypeError)
throws(
() => new Headers([0, 1, 2]),
TypeError('Headers contructor: init (0) is not iterable.')
TypeError('Headers contructor: init[0] (0) is not iterable.')
)
})

Expand All @@ -42,7 +42,7 @@ test('Headers initialization', async (t) => {
const init = ['undici', 'fetch', 'fetch', 'undici']
throws(
() => new Headers(init),
TypeError('Headers contructor: init ("undici") is not iterable.')
TypeError('Headers contructor: init[0] ("undici") is not iterable.')
)
})
})
Expand Down
16 changes: 16 additions & 0 deletions test/webidl/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,19 @@ describe('dictionary converters', () => {
)
})
})

describe('sequence converters', () => {
test('retains index', () => {
const { port1 } = new MessageChannel()

assert.throws(
() => new MessageEvent('type', { ports: [{}] }),
new TypeError('MessageEvent constructor: Expected eventInitDict.ports[0] ("{}") to be an instance of MessagePort.')
)

assert.throws(
() => new MessageEvent('type', { ports: [port1, {}] }),
new TypeError('MessageEvent constructor: Expected eventInitDict.ports[1] ("{}") to be an instance of MessagePort.')
)
})
})
4 changes: 2 additions & 2 deletions test/websocket/messageevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ test('test/parallel/test-worker-message-port.js', () => {
})
assert.throws(() => new MessageEvent('message', { ports: [null] }), {
constructor: TypeError,
message: 'MessageEvent constructor: Expected eventInitDict.ports ("null") to be an instance of MessagePort.'
message: 'MessageEvent constructor: Expected eventInitDict.ports[0] ("null") to be an instance of MessagePort.'
})
assert.throws(() =>
new MessageEvent('message', { ports: [{}] })
, {
constructor: TypeError,
message: 'MessageEvent constructor: Expected eventInitDict.ports ("{}") to be an instance of MessagePort.'
message: 'MessageEvent constructor: Expected eventInitDict.ports[0] ("{}") to be an instance of MessagePort.'
})

assert(new MessageEvent('message') instanceof Event)
Expand Down

0 comments on commit e009b0c

Please sign in to comment.