forked from babel/babel
/
index.ts
34 lines (30 loc) 路 926 Bytes
/
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
import { isRequired } from "@babel/helper-compilation-targets";
import { declare } from "@babel/helper-plugin-utils";
import nameFunction from "@babel/helper-function-name";
export default declare(api => {
api.assertVersion(7);
const supportUnicodeId = !isRequired(
"transform-unicode-escapes",
api.targets(),
);
return {
name: "transform-function-name",
visitor: {
FunctionExpression: {
exit(path) {
if (path.key !== "value" && !path.parentPath.isObjectProperty()) {
const replacement = nameFunction(path);
if (replacement) path.replaceWith(replacement);
}
},
},
ObjectProperty(path) {
const value = path.get("value");
if (value.isFunction()) {
const newNode = nameFunction(value, false, supportUnicodeId);
if (newNode) value.replaceWith(newNode);
}
},
},
};
});