forked from babel/babel
/
index.js
45 lines (40 loc) 路 1.24 KB
/
index.js
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
import { declare } from "@babel/helper-plugin-utils";
import { types as t } from "@babel/core";
export default declare(api => {
api.assertVersion(7);
return {
name: "transform-shorthand-properties",
visitor: {
ObjectMethod(path) {
const { node } = path;
if (node.kind === "method") {
const func = t.functionExpression(
null,
node.params,
node.body,
node.generator,
node.async,
);
func.returnType = node.returnType;
const computedKey = t.toComputedKey(node);
if (t.isStringLiteral(computedKey, { value: "__proto__" })) {
path.replaceWith(t.objectProperty(computedKey, func, true));
} else {
path.replaceWith(t.objectProperty(node.key, func, node.computed));
}
}
},
ObjectProperty(path) {
const { node } = path;
if (node.shorthand) {
const computedKey = t.toComputedKey(node);
if (t.isStringLiteral(computedKey, { value: "__proto__" })) {
path.replaceWith(t.objectProperty(computedKey, node.value, true));
} else {
node.shorthand = false;
}
}
},
},
};
});