forked from raycast/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
78 lines (74 loc) · 1.89 KB
/
utils.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { JSCodeshift, Collection } from "jscodeshift";
export function renameJSXProp(
j: JSCodeshift,
root: Collection<any>,
component: string | string[],
from: string,
to: string
) {
root
.find(j.JSXOpeningElement, {
name: Array.isArray(component)
? {
type: "JSXMemberExpression",
object: { type: "JSXIdentifier", name: component[0] },
property: { name: component[1] },
}
: { name: component },
})
.find(j.JSXAttribute, { name: { type: "JSXIdentifier", name: from } })
.forEach((p) => {
if (p.node.type !== "JSXAttribute") {
return;
}
p.node.name.name = to;
});
}
export function isVariableDeclared(
j: JSCodeshift,
root: Collection<any>,
variable: string
) {
return (
root
.find(j.Identifier, {
name: variable,
})
.filter(
(path) =>
path.parent.value.type !== "MemberExpression" &&
path.parent.value.type !== "QualifiedTypeIdentifier" &&
path.parent.value.type !== "JSXMemberExpression"
)
.size() > 0
);
}
export function removeImport(
j: JSCodeshift,
root: Collection<any>,
name: string
) {
root
.find(j.ImportDeclaration, { source: { value: "@raycast/api" } })
.replaceWith((p) =>
j.importDeclaration(
p.node.specifiers?.filter((x) => x.local?.name !== name),
p.node.source,
p.node.importKind
)
);
}
export function addImport(j: JSCodeshift, root: Collection<any>, name: string) {
root
.find(j.ImportDeclaration, { source: { value: "@raycast/api" } })
.replaceWith((p) => {
if (p.node.specifiers?.some((x) => x.local?.name === name)) {
return p.node;
}
return j.importDeclaration(
p.node.specifiers?.concat(j.importSpecifier(j.identifier(name))),
p.node.source,
p.node.importKind
);
});
}