/
jsx.ts
154 lines (143 loc) · 4.87 KB
/
jsx.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as t from '@babel/types';
import * as m from '@codemod/matchers';
import { Transform } from '.';
import { codePreview } from '../utils/ast';
import {
constMemberExpression,
deepIdentifierMemberExpression,
} from '../utils/matcher';
import { renameFast } from '../utils/rename';
export default {
name: 'jsx',
tags: ['unsafe'],
visitor: () => {
const type = m.capture(
m.or(
m.identifier(), // React.createElement(Component, ...)
m.stringLiteral(), // React.createElement('div', ...)
deepIdentifierMemberExpression // React.createElement(Component.SubComponent, ...)
)
);
const props = m.capture(m.or(m.objectExpression(), m.nullLiteral()));
// React.createElement(type, props, ...children)
const elementMatcher = m.callExpression(
constMemberExpression(m.identifier('React'), 'createElement'),
m.anyList<t.Expression>(type, props, m.zeroOrMore(m.anyExpression()))
);
// React.createElement(React.Fragment, null, ...children)
const fragmentMatcher = m.callExpression(
constMemberExpression(m.identifier('React'), 'createElement'),
m.anyList<t.Expression>(
constMemberExpression(m.identifier('React'), 'Fragment'),
m.nullLiteral(),
m.zeroOrMore(m.anyExpression())
)
);
return {
CallExpression: {
exit(path) {
if (fragmentMatcher.match(path.node)) {
const children = convertChildren(
path.node.arguments.slice(2) as t.Expression[]
);
const opening = t.jSXOpeningFragment();
const closing = t.jsxClosingFragment();
const fragment = t.jsxFragment(opening, closing, children);
path.replaceWith(fragment);
this.changes++;
}
if (elementMatcher.match(path.node)) {
let name = convertType(type.current!);
// rename component to avoid conflict with built-in html tags
// https://react.dev/reference/react/createElement#caveats
if (
t.isIdentifier(type.current!) &&
/^[a-z]/.test(type.current.name)
) {
const binding = path.scope.getBinding(type.current.name);
if (!binding) return;
name = t.jsxIdentifier(path.scope.generateUid('Component'));
renameFast(binding, name.name);
}
const attributes = t.isObjectExpression(props.current)
? convertAttributes(props.current)
: [];
const children = convertChildren(
path.node.arguments.slice(2) as t.Expression[]
);
const opening = t.jsxOpeningElement(name, attributes);
const closing = t.jsxClosingElement(name);
const element = t.jsxElement(opening, closing, children);
path.replaceWith(element);
this.changes++;
}
},
},
noScope: true,
};
},
} satisfies Transform;
/**
* - `Component` -> `Component`
* - `Component.SubComponent` -> `Component.SubComponent`
* - `'div'` -> `div`
*/
function convertType(
type: t.Identifier | t.MemberExpression | t.StringLiteral
): t.JSXIdentifier | t.JSXMemberExpression {
if (t.isIdentifier(type)) {
return t.jsxIdentifier(type.name);
} else if (t.isStringLiteral(type)) {
return t.jsxIdentifier(type.value);
} else {
const object = convertType(
type.object as t.Identifier | t.MemberExpression
);
const property = t.jsxIdentifier((type.property as t.Identifier).name);
return t.jsxMemberExpression(object, property);
}
}
/**
* `{ className: 'foo', style: { display: 'block' } }`
* ->
* `className='foo' style={{ display: 'block' }}`
*/
function convertAttributes(
object: t.ObjectExpression
): (t.JSXAttribute | t.JSXSpreadAttribute)[] {
const name = m.capture(m.anyString());
const value = m.capture(m.anyExpression());
const matcher = m.objectProperty(
m.or(m.identifier(name), m.stringLiteral(name)),
value
);
return object.properties.map(property => {
if (matcher.match(property)) {
const jsxName = t.jsxIdentifier(name.current!);
const jsxValue =
value.current!.type === 'StringLiteral'
? value.current!
: t.jsxExpressionContainer(value.current!);
return t.jsxAttribute(jsxName, jsxValue);
} else if (t.isSpreadElement(property)) {
return t.jsxSpreadAttribute(property.argument);
} else {
throw new Error(
`jsx: property type not implemented ${codePreview(object)}`
);
}
});
}
function convertChildren(
children: t.Expression[]
): (t.JSXText | t.JSXElement | t.JSXExpressionContainer)[] {
return children.map(child => {
if (t.isJSXElement(child)) {
return child;
} else if (t.isStringLiteral(child)) {
return t.jsxText(child.value);
} else {
return t.jsxExpressionContainer(child);
}
});
}