-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.ts
29 lines (27 loc) · 1.08 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
import babel from '@babel/core';
import {isComponentDeclaration} from '@reskript/babel-utils';
const {types} = babel;
export default function addReactDisplayName(): babel.PluginObj {
return {
visitor: {
FunctionDeclaration(declaration) {
if (isComponentDeclaration(declaration)) {
const componentName = declaration.node.id?.name ?? '';
if (componentName) {
const assignDisplayName = types.expressionStatement(
types.assignmentExpression(
'=',
types.memberExpression(
types.identifier(componentName),
types.identifier('displayName')
),
types.stringLiteral(componentName)
)
);
declaration.insertAfter(assignDisplayName);
}
}
},
},
};
}