Skip to content

Commit d408938

Browse files
committed
✨ feat: init
1 parent e819569 commit d408938

File tree

4 files changed

+640
-0
lines changed

4 files changed

+640
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
3+
const parser = require('@babel/parser');
4+
const traverse = require('@babel/traverse').default;
5+
const { types: c } = require('@babel/core');
6+
const generator = require('@babel/generator').default;
7+
8+
9+
module.exports = function (source) {
10+
if (!source.includes('__cursorPointer')) return source
11+
const ast = parser.parse(source, {
12+
sourceType: 'module',
13+
plugins: ['jsx', 'typescript', 'decorators-legacy'],
14+
parserOpts: {
15+
allowReturnOutsideFunction: true
16+
}
17+
});
18+
traverse(ast, {
19+
JSXElement(path) {
20+
const has = !!path.node.openingElement?.attributes?.find(v => v.name.name == '__cursorPointer')
21+
if (has) {
22+
const styleObject = c.objectExpression([
23+
c.objectProperty(c.identifier('cursor'), c.stringLiteral('pointer')),
24+
]);
25+
const styleTPL = c.jsxAttribute(
26+
c.jsxIdentifier('style'),
27+
c.jSXExpressionContainer(styleObject)
28+
)
29+
30+
const oldStyle = path.node.openingElement.attributes.find(v => v.name.name == 'style')
31+
if (!!oldStyle) {
32+
oldStyle.value.expression.properties.push(styleObject.properties[0])
33+
} else {
34+
path.node.openingElement.attributes.push(styleTPL)
35+
}
36+
path.node.openingElement.attributes = path.node.openingElement.attributes.filter(v => v?.name.name != '__cursorPointer')
37+
}
38+
},
39+
});
40+
41+
42+
const transformedCode = generator(ast, {}, source).code;
43+
return transformedCode
44+
45+
46+
};
47+

0 commit comments

Comments
 (0)