-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (37 loc) · 1.46 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
46
47
const parser = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const { types: c } = require('@babel/core');
const generator = require('@babel/generator').default;
module.exports = function (source) {
if (!source.includes('__cursorPointer')) return source
const ast = parser.parse(source, {
sourceType: 'module',
plugins: ['jsx', 'typescript', 'decorators-legacy'],
parserOpts: {
allowReturnOutsideFunction: true
}
});
traverse(ast, {
JSXElement(path) {
const has = !!path.node.openingElement?.attributes?.find(v => v.name.name == '__cursorPointer')
if (has) {
const styleObject = c.objectExpression([
c.objectProperty(c.identifier('cursor'), c.stringLiteral('pointer')),
]);
const styleTPL = c.jsxAttribute(
c.jsxIdentifier('style'),
c.jSXExpressionContainer(styleObject)
)
const oldStyle = path.node.openingElement.attributes.find(v => v.name.name == 'style')
if (!!oldStyle) {
oldStyle.value.expression.properties.push(styleObject.properties[0])
} else {
path.node.openingElement.attributes.push(styleTPL)
}
path.node.openingElement.attributes = path.node.openingElement.attributes.filter(v => v?.name.name != '__cursorPointer')
}
},
});
const transformedCode = generator(ast, {}, source).code;
return transformedCode
};