This repository was archived by the owner on Dec 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (41 loc) · 1.44 KB
/
index.js
File metadata and controls
46 lines (41 loc) · 1.44 KB
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
import template from "babel-template";
const buildProperty = template(`
$0[$1] = $2;
`);
export default function ({ types: t }) {
return {
visitor: {
CallExpression: {
exit(path) {
// Only Object.defineProperty for processing
if (
!(path.get('callee').isMemberExpression()
&& t.isIdentifier(path.node.callee.object, { name: 'Object' })
&& t.isIdentifier(path.node.callee.property, { name: 'defineProperty' }))) return;
if (
path.node.arguments.length === 3
&& t.isObjectExpression(path.node.arguments[2])
&& Array.isArray(path.node.arguments[2].properties)
&& !path.node.arguments[2].properties.some((n) => {
return t.isIdentifier(n.key, { name: 'set' });
})
) {
const FunctionDecl = path.node.arguments[2].properties.find((n) => {
return t.isIdentifier(n.key, { name: 'get' });
});
if (FunctionDecl && FunctionDecl.value) {
if (path.node.arguments[1].value != null) {
const node = buildProperty(
path.node.arguments[0],
t.stringLiteral(path.node.arguments[1].value),
t.callExpression(t.parenthesizedExpression(FunctionDecl.value), [])
);
path.replaceWith(node);
}
}
}
}
}
}
};
}