Skip to content

Commit 3f0e14d

Browse files
authored
feat(transformer): add objectName+propertyName selector to #fromFunctionQuery (#58)
1 parent b494f56 commit 3f0e14d

6 files changed

Lines changed: 92 additions & 1 deletion

File tree

lib/transformer.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class Transformer {
245245
* @returns {string} esquery selector.
246246
*/
247247
#fromFunctionQuery (functionQuery) {
248-
const { functionName, expressionName, className } = functionQuery
248+
const { functionName, expressionName, className, objectName, propertyName } = functionQuery
249249
const type = functionQuery.privateMethodName ? 'PrivateIdentifier' : 'Identifier'
250250
const queries = []
251251

@@ -281,6 +281,19 @@ class Transformer {
281281
)
282282
}
283283

284+
if (objectName || propertyName) {
285+
if (!objectName || !propertyName) {
286+
throw new Error(
287+
`functionQuery: 'objectName' and 'propertyName' must be used together (got objectName=${objectName}, propertyName=${propertyName})`
288+
)
289+
}
290+
const objectSelector = objectName === 'this'
291+
? 'left.object.type=ThisExpression'
292+
: `left.object.name="${objectName}"`
293+
queries.push(
294+
`AssignmentExpression[${objectSelector}][left.property.name="${propertyName}"] > [async]`
295+
)
296+
}
284297
return queries.join(', ')
285298
}
286299
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
// Named object pattern: async arrow function assigned to a property
4+
// on a named identifier (not `this`).
5+
const conn = {}
6+
conn.query = async () => {
7+
return 42
8+
}
9+
10+
module.exports = { conn }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
const { conn } = require('./instrumented.js')
4+
const { assert, getContext } = require('../common/preamble.js')
5+
const context = getContext('orchestrion:undici:conn_query')
6+
7+
;(async () => {
8+
const result = await conn.query()
9+
assert.strictEqual(result, 42)
10+
assert.deepStrictEqual(context, {
11+
start: true,
12+
end: true,
13+
asyncStart: 42,
14+
asyncEnd: 42
15+
})
16+
})()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
// Mimics the mariadb v2 pattern: query methods are arrow functions
4+
// assigned to `this` inside a function constructor.
5+
function Connection (opts) {
6+
this._query = async () => {
7+
return 42
8+
}
9+
}
10+
11+
module.exports = { Connection }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
const { Connection } = require('./instrumented.js')
4+
const { assert, getContext } = require('../common/preamble.js')
5+
const context = getContext('orchestrion:undici:Connection_query')
6+
7+
;(async () => {
8+
const conn = new Connection({ host: 'localhost' })
9+
const result = await conn._query()
10+
assert.strictEqual(result, 42)
11+
assert.deepStrictEqual(context, {
12+
start: true,
13+
end: true,
14+
asyncStart: 42,
15+
asyncEnd: 42
16+
})
17+
})()

tests/tests.test.mjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,27 @@ describe('IIFE with class', () => {
529529
])
530530
})
531531
})
532+
533+
describe('object_property_this_cjs', () => {
534+
test('instruments async arrow function assigned to this inside a function constructor', () => {
535+
runTest('object_property_this_cjs', [
536+
{
537+
channelName: 'Connection_query',
538+
module: { name: TEST_MODULE_NAME, versionRange: '>=0.0.1', filePath: TEST_MODULE_PATH },
539+
functionQuery: { objectName: 'this', propertyName: '_query', kind: 'Async' },
540+
},
541+
])
542+
})
543+
})
544+
545+
describe('object_property_named_cjs', () => {
546+
test('instruments async arrow function assigned to a named identifier property', () => {
547+
runTest('object_property_named_cjs', [
548+
{
549+
channelName: 'conn_query',
550+
module: { name: TEST_MODULE_NAME, versionRange: '>=0.0.1', filePath: TEST_MODULE_PATH },
551+
functionQuery: { objectName: 'conn', propertyName: 'query', kind: 'Async' },
552+
},
553+
])
554+
})
555+
})

0 commit comments

Comments
 (0)