blob: 920921d2e4ebb74f2237a61a03f295a6bcd31234 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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();
};
|