Skip to content

Commit af40a87

Browse files
authored
[DI] Condtions: Allow using len to count keys in objects (#5542)
1 parent ded5a77 commit af40a87

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

packages/dd-trace/src/debugger/devtools_client/condition.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,12 @@ function getSize (variable) {
184184
return ${guardAgainstPropertyAccessSideEffects('val', '"length"')}
185185
} else if (${isInstanceOfCoreType('Set', 'val')} || ${isInstanceOfCoreType('Map', 'val')}) {
186186
return ${guardAgainstPropertyAccessSideEffects('val', '"size"')}
187+
} else if (${isInstanceOfCoreType('WeakSet', 'val')} || ${isInstanceOfCoreType('WeakMap', 'val')}) {
188+
throw new TypeError('Cannot get size of WeakSet or WeakMap')
189+
} else if (typeof val === 'object' && val !== null) {
190+
return Object.keys(val).length
187191
} else {
188-
throw new TypeError('Cannot get length or size of string/collection')
192+
throw new TypeError('Cannot get length of variable')
189193
}
190194
})(${variable})`
191195
}

packages/dd-trace/test/debugger/devtools_client/condition-test-cases.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,12 @@ const sizes = [
195195
[
196196
{ len: { ref: 'wset' } },
197197
{ wset: new WeakSet([weakKey]) },
198-
new TypeError('Cannot get length or size of string/collection')
198+
new TypeError('Cannot get size of WeakSet or WeakMap')
199199
],
200200
[
201201
{ len: { ref: 'wmap' } },
202202
{ wmap: new WeakMap([[weakKey, 2]]) },
203-
new TypeError('Cannot get length or size of string/collection')
203+
new TypeError('Cannot get size of WeakSet or WeakMap')
204204
],
205205
[{ len: { getmember: [{ ref: 'obj' }, 'arr'] } }, { obj: { arr: Array(10).fill(0) } }, 10],
206206
[{ len: { getmember: [{ ref: 'obj' }, 'tarr'] } }, { obj: { tarr: new Int16Array([10, 20, 30]) } }, 3],
@@ -209,10 +209,11 @@ const sizes = [
209209
{ obj: { tarr: overloadPropertyWithGetter(new Int16Array([10, 20, 30]), 'length') } },
210210
new Error('Possibility of side effect')
211211
],
212+
[{ len: { ref: 'pojo' } }, { pojo: { a: 1, b: 2, c: 3 } }, 3],
212213
[
213214
{ len: { getmember: [{ ref: 'obj' }, 'unknownProp'] } },
214215
{ obj: {} },
215-
new TypeError('Cannot get length or size of string/collection')
216+
new TypeError('Cannot get length of variable')
216217
],
217218
[{ len: { ref: 'invalid' } }, {}, new ReferenceError('invalid is not defined')],
218219

@@ -237,7 +238,7 @@ const sizes = [
237238
[
238239
{ isEmpty: { ref: 'obj' } },
239240
{ obj: new WeakSet() },
240-
new TypeError('Cannot get length or size of string/collection')
241+
new TypeError('Cannot get size of WeakSet or WeakMap')
241242
]
242243
]
243244

0 commit comments

Comments
 (0)