@@ -14,6 +14,14 @@ const tracingChannelPredicate = (node) => (
1414 node . declarations ?. [ 0 ] ?. id ?. properties ?. [ 0 ] ?. value ?. name === 'tr_ch_apm_tracingChannel'
1515)
1616
17+ const CHANNEL_REGEX = / [ ^ \w ] / g
18+ /**
19+ * Formats the channel variable name by replacing any non-whitespace characters with `_`
20+ *
21+ * @param {string } channelName
22+ */
23+ const formatChannelVariable = ( channelName ) => `tr_ch_apm$${ channelName . replace ( CHANNEL_REGEX , '_' ) } `
24+
1725const transforms = module . exports = {
1826 /**
1927 * Injects a `tracingChannel` import/require into the program body if one is not
@@ -55,7 +63,7 @@ const transforms = module.exports = {
5563 */
5664 tracingChannelDeclaration ( state , node ) {
5765 const { channelName, module : { name } } = state
58- const channelVariable = 'tr_ch_apm$' + channelName . replaceAll ( ':' , '_' )
66+ const channelVariable = formatChannelVariable ( channelName )
5967
6068 if ( node . body . some ( child => child . declarations ?. [ 0 ] ?. id ?. name === channelVariable ) ) return
6169
@@ -313,7 +321,7 @@ function wrapSuper (_state, node) {
313321 */
314322function wrapCallback ( state , node ) {
315323 const { channelName, functionQuery : { callbackIndex = - 1 } } = state
316- const channelVariable = 'tr_ch_apm$' + channelName . replaceAll ( ':' , '_' )
324+ const channelVariable = formatChannelVariable ( channelName )
317325
318326 return parse ( `
319327 function wrapper () {
@@ -373,7 +381,7 @@ function wrapCallback (state, node) {
373381 */
374382function wrapPromise ( state , node ) {
375383 const { channelName } = state
376- const channelVariable = 'tr_ch_apm$' + channelName . replaceAll ( ':' , '_' )
384+ const channelVariable = formatChannelVariable ( channelName )
377385
378386 return parse ( `
379387 function wrapper () {
@@ -426,7 +434,7 @@ function wrapPromise (state, node) {
426434 */
427435function wrapSync ( state , node ) {
428436 const { channelName } = state
429- const channelVariable = 'tr_ch_apm$' + channelName . replaceAll ( ':' , '_' )
437+ const channelVariable = formatChannelVariable ( channelName )
430438
431439 return parse ( `
432440 function wrapper () {
0 commit comments