-
Notifications
You must be signed in to change notification settings - Fork 1
/
transform.ts
40 lines (32 loc) · 1.01 KB
/
transform.ts
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
import { Transform } from "jscodeshift";
const transform: Transform = (file, api) => {
// Alias the jscodeshift API for ease of use.
const j = api.jscodeshift;
// Convert the entire file source into a collection of nodes paths.
const root = j(file.source);
root
.find(j.CallExpression)
.filter((path) => {
const { node } = path;
if (
node.callee.type === "MemberExpression" &&
node.callee.object.type === "Identifier" &&
node.callee.object.name === "coffee" &&
node.callee.property.type === "Identifier" &&
node.callee.property.name === "brew"
) {
const [waterArg] = node.arguments;
return waterArg.type === "StringLiteral" && waterArg.value !== "💧";
}
})
.replaceWith((path) => {
const { node } = path;
const [waterArg] = node.arguments;
if (waterArg.type === "StringLiteral") {
waterArg.value = "💧";
}
return node;
});
return root.toSource();
};
export default transform;