forked from babel/babel
/
index.ts
48 lines (42 loc) · 1.23 KB
/
index.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
import { declare } from "@babel/helper-plugin-utils";
import { type MutatorMap, pushAccessor, toDefineObject } from "./define-map";
import { types as t } from "@babel/core";
export default declare(api => {
api.assertVersion(7);
return {
name: "transform-property-mutators",
visitor: {
ObjectExpression(path) {
const { node } = path;
let mutatorMap: MutatorMap | undefined;
const newProperties = node.properties.filter(function (prop) {
if (
t.isObjectMethod(prop) &&
!prop.computed &&
(prop.kind === "get" || prop.kind === "set")
) {
pushAccessor(
(mutatorMap ??= {}),
prop as t.ObjectMethod & { kind: "get" | "set"; computed: false },
);
return false;
}
return true;
});
if (mutatorMap === undefined) {
return;
}
node.properties = newProperties;
path.replaceWith(
t.callExpression(
t.memberExpression(
t.identifier("Object"),
t.identifier("defineProperties"),
),
[node, toDefineObject(mutatorMap)],
),
);
},
},
};
});