summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webrtc/tools/codemod-peerconnection-addcleanup
diff options
context:
space:
mode:
Diffstat (limited to 'testing/web-platform/tests/webrtc/tools/codemod-peerconnection-addcleanup')
-rw-r--r--testing/web-platform/tests/webrtc/tools/codemod-peerconnection-addcleanup58
1 files changed, 58 insertions, 0 deletions
diff --git a/testing/web-platform/tests/webrtc/tools/codemod-peerconnection-addcleanup b/testing/web-platform/tests/webrtc/tools/codemod-peerconnection-addcleanup
new file mode 100644
index 0000000000..920921d2e4
--- /dev/null
+++ b/testing/web-platform/tests/webrtc/tools/codemod-peerconnection-addcleanup
@@ -0,0 +1,58 @@
+/* a codemod for ensuring RTCPeerConnection is cleaned up in tests.
+ * For each `new RTCPeerConnection` add a
+ * `test.add_cleanup(() => pc.close())`
+ * Only applies in promise_tests if there is no add_cleanup in the
+ * test function body.
+ */
+export default function transformer(file, api) {
+ const j = api.jscodeshift;
+ return j(file.source)
+ // find each RTCPeerConnection constructor
+ .find(j.NewExpression, {callee: {type: 'Identifier', name: 'RTCPeerConnection'}})
+
+ // check it is inside a promise_test
+ .filter(path => {
+ // iterate parentPath until you find a CallExpression
+ let nextPath = path.parentPath;
+ while (nextPath && nextPath.value.type !== 'CallExpression') {
+ nextPath = nextPath.parentPath;
+ }
+ return nextPath && nextPath.value.callee.name === 'promise_test';
+ })
+ // check there is no add_cleanup in the function body
+ .filter(path => {
+ let nextPath = path.parentPath;
+ while (nextPath && nextPath.value.type !== 'CallExpression') {
+ nextPath = nextPath.parentPath;
+ }
+ const body = nextPath.value.arguments[0].body;
+ return j(body).find(j.Identifier, {name: 'add_cleanup'}).length === 0;
+ })
+ .forEach(path => {
+ // iterate parentPath until you find a CallExpression
+ let nextPath = path.parentPath;
+ while (nextPath && nextPath.value.type !== 'CallExpression') {
+ nextPath = nextPath.parentPath;
+ }
+ const declaration = path.parentPath.parentPath.parentPath;
+ const pc = path.parentPath.value.id;
+
+ declaration.insertAfter(
+ j.expressionStatement(
+ j.callExpression(
+ j.memberExpression(
+ nextPath.node.arguments[0].params[0],
+ j.identifier('add_cleanup')
+ ),
+ [j.arrowFunctionExpression([],
+ j.callExpression(
+ j.memberExpression(pc, j.identifier('close'), false),
+ []
+ )
+ )]
+ )
+ )
+ );
+ })
+ .toSource();
+};