Skip to content

Commit 7e328c9

Browse files
authored
Merge pull request #1169 from DataDog/carlosnogueira/issue-1168/fix-babel-memoized-wrapping
[FIX] Babel plugin's incorrect wrapping logic on memoized functions
2 parents 4291b20 + 6e814a0 commit 7e328c9

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

packages/react-native-babel-plugin/src/actions/rum/tap.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,19 @@ function handleMemoization(
298298
? callback
299299
: t.callExpression(callback, callArgs);
300300

301+
// Update handlerArgs in argsObject to match the actual callback params
302+
// instead of the default [...args] computed from the outer identifier
303+
const handlerArgsProp = argsObject.properties.find(
304+
prop =>
305+
t.isObjectProperty(prop) &&
306+
t.isStringLiteral(prop.key) &&
307+
prop.key.value === 'handlerArgs'
308+
);
309+
310+
if (handlerArgsProp && t.isObjectProperty(handlerArgsProp)) {
311+
handlerArgsProp.value = t.arrayExpression(callArgs);
312+
}
313+
301314
const actionWrapper = getActionWrapperFunction(
302315
t,
303316
state,

packages/react-native-babel-plugin/test/plugin.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ describe('Babel plugin: wrap interaction handlers for RUM ( with memoization )',
12011201
"getContent": () => {
12021202
return __ddExtractText(<></>, []);
12031203
},
1204-
"handlerArgs": [...args],
1204+
"handlerArgs": [event],
12051205
"componentName": "Pressable"
12061206
})(event);else return (event => {
12071207
console.log('Testing ', a, b, event);
@@ -1299,7 +1299,7 @@ describe('Babel plugin: wrap interaction handlers for RUM ( with memoization )',
12991299
"getContent": () => {
13001300
return __ddExtractText(<></>, []);
13011301
},
1302-
"handlerArgs": [...args],
1302+
"handlerArgs": [],
13031303
"componentName": "Pressable"
13041304
})();else return (() => {
13051305
console.log('Testing ');
@@ -1348,7 +1348,7 @@ describe('Babel plugin: wrap interaction handlers for RUM ( with memoization )',
13481348
"getContent": () => {
13491349
return __ddExtractText(<></>, []);
13501350
},
1351-
"handlerArgs": [...args],
1351+
"handlerArgs": [],
13521352
"componentName": "Pressable"
13531353
})();else return (() => {
13541354
console.log('Testing ');
@@ -1407,7 +1407,7 @@ describe('Babel plugin: wrap interaction handlers for RUM ( with memoization )',
14071407
"getContent": () => {
14081408
return __ddExtractText(<></>, []);
14091409
},
1410-
"handlerArgs": [...args],
1410+
"handlerArgs": [test],
14111411
"componentName": "Pressable"
14121412
})(test);else return funcN(test);
14131413
}, [a, b]);
@@ -1461,7 +1461,7 @@ describe('Babel plugin: wrap interaction handlers for RUM ( with memoization )',
14611461
"getContent": () => {
14621462
return __ddExtractText(<></>, []);
14631463
},
1464-
"handlerArgs": [...args],
1464+
"handlerArgs": [test],
14651465
"componentName": "Pressable"
14661466
})(test);else return funcN(test);
14671467
}, [a, b]);
@@ -1511,7 +1511,7 @@ describe('Babel plugin: wrap interaction handlers for RUM ( with memoization )',
15111511
"getContent": () => {
15121512
return __ddExtractText(<></>, []);
15131513
},
1514-
"handlerArgs": [...args],
1514+
"handlerArgs": [test],
15151515
"componentName": "Pressable"
15161516
})(test);else return funcN(test);
15171517
}, [a, b]);
@@ -1594,7 +1594,7 @@ describe('Babel plugin: wrap interaction handlers for RUM ( with memoization )',
15941594
"getContent": () => {
15951595
return __ddExtractText(<></>, []);
15961596
},
1597-
"handlerArgs": [...args],
1597+
"handlerArgs": [],
15981598
"componentName": "Pressable"
15991599
})();else return funcN();
16001600
}, []);

0 commit comments

Comments
 (0)