@litsx/babel-preset-litsx 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -0
- package/dist/index.cjs +30 -0
- package/dist/index.cjs.map +1 -0
- package/dist/internal/transform-litsx-components.cjs +2518 -0
- package/dist/internal/transform-litsx-components.cjs.map +1 -0
- package/dist/internal/transform-litsx-dom-refs.cjs +16 -0
- package/dist/internal/transform-litsx-dom-refs.cjs.map +1 -0
- package/dist/internal/transform-litsx-hooks.cjs +45 -0
- package/dist/internal/transform-litsx-hooks.cjs.map +1 -0
- package/dist/internal/transform-litsx-properties.cjs +1254 -0
- package/dist/internal/transform-litsx-properties.cjs.map +1 -0
- package/dist/pipeline.cjs +97 -0
- package/dist/pipeline.cjs.map +1 -0
- package/package.json +80 -0
- package/src/index.js +15 -0
- package/src/internal/transform-litsx-class-generation.js +109 -0
- package/src/internal/transform-litsx-components.js +549 -0
- package/src/internal/transform-litsx-dom-refs.js +9 -0
- package/src/internal/transform-litsx-handlers.js +220 -0
- package/src/internal/transform-litsx-hooks.js +38 -0
- package/src/internal/transform-litsx-param-rewrites.js +227 -0
- package/src/internal/transform-litsx-program.js +229 -0
- package/src/internal/transform-litsx-properties.js +1246 -0
- package/src/internal/transform-litsx-refs.js +231 -0
- package/src/internal/transform-litsx-static-hoists.js +754 -0
- package/src/internal/transform-litsx-wrapper-utils.js +243 -0
- package/src/pipeline.js +90 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
let t;
|
|
2
|
+
|
|
3
|
+
export function setRefsBabelTypes(nextTypes) {
|
|
4
|
+
t = nextTypes;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function createThisMemberExpression(propName) {
|
|
8
|
+
return t.memberExpression(t.thisExpression(), t.identifier(propName));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function createManagedRefLookupExpression(refName) {
|
|
12
|
+
const selectorLiteral = t.stringLiteral(`[data-ref="${refName}"]`);
|
|
13
|
+
|
|
14
|
+
const renderRootQuery = t.optionalCallExpression(
|
|
15
|
+
t.optionalMemberExpression(
|
|
16
|
+
t.memberExpression(t.thisExpression(), t.identifier("renderRoot")),
|
|
17
|
+
t.identifier("querySelector"),
|
|
18
|
+
false,
|
|
19
|
+
true
|
|
20
|
+
),
|
|
21
|
+
[selectorLiteral],
|
|
22
|
+
false
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const hostQuery = t.callExpression(
|
|
26
|
+
t.memberExpression(t.thisExpression(), t.identifier("querySelector")),
|
|
27
|
+
[t.cloneNode(selectorLiteral)]
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
return t.logicalExpression("??", renderRootQuery, hostQuery);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function createForwardedTargetRefSyncStatement(propName, refName) {
|
|
34
|
+
return t.expressionStatement(
|
|
35
|
+
t.callExpression(t.identifier("useCallbackRef"), [
|
|
36
|
+
t.thisExpression(),
|
|
37
|
+
t.arrowFunctionExpression([], createManagedRefLookupExpression(refName)),
|
|
38
|
+
t.arrowFunctionExpression(
|
|
39
|
+
[t.identifier("node")],
|
|
40
|
+
t.blockStatement([
|
|
41
|
+
t.variableDeclaration("const", [
|
|
42
|
+
t.variableDeclarator(
|
|
43
|
+
t.identifier("componentRef"),
|
|
44
|
+
createThisMemberExpression(propName)
|
|
45
|
+
),
|
|
46
|
+
]),
|
|
47
|
+
t.ifStatement(
|
|
48
|
+
t.binaryExpression(
|
|
49
|
+
"===",
|
|
50
|
+
t.unaryExpression("typeof", t.identifier("componentRef")),
|
|
51
|
+
t.stringLiteral("function")
|
|
52
|
+
),
|
|
53
|
+
t.blockStatement([
|
|
54
|
+
t.expressionStatement(
|
|
55
|
+
t.callExpression(t.identifier("componentRef"), [t.identifier("node")])
|
|
56
|
+
),
|
|
57
|
+
]),
|
|
58
|
+
t.ifStatement(
|
|
59
|
+
t.logicalExpression(
|
|
60
|
+
"&&",
|
|
61
|
+
t.identifier("componentRef"),
|
|
62
|
+
t.binaryExpression(
|
|
63
|
+
"===",
|
|
64
|
+
t.unaryExpression("typeof", t.identifier("componentRef")),
|
|
65
|
+
t.stringLiteral("object")
|
|
66
|
+
)
|
|
67
|
+
),
|
|
68
|
+
t.blockStatement([
|
|
69
|
+
t.expressionStatement(
|
|
70
|
+
t.assignmentExpression(
|
|
71
|
+
"=",
|
|
72
|
+
t.memberExpression(t.identifier("componentRef"), t.identifier("current")),
|
|
73
|
+
t.identifier("node")
|
|
74
|
+
)
|
|
75
|
+
),
|
|
76
|
+
])
|
|
77
|
+
)
|
|
78
|
+
),
|
|
79
|
+
])
|
|
80
|
+
),
|
|
81
|
+
t.arrayExpression([createThisMemberExpression(propName)]),
|
|
82
|
+
])
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function isStandardElementJsxName(nameNode) {
|
|
87
|
+
if (!t.isJSXIdentifier(nameNode)) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const name = nameNode.name || "";
|
|
92
|
+
return Boolean(name) && name[0] === name[0].toLowerCase() && !name.includes("-");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function createComponentInstanceRefSyncStatement() {
|
|
96
|
+
return t.expressionStatement(
|
|
97
|
+
t.callExpression(t.identifier("useCallbackRef"), [
|
|
98
|
+
t.thisExpression(),
|
|
99
|
+
t.arrowFunctionExpression([], t.thisExpression()),
|
|
100
|
+
t.arrowFunctionExpression(
|
|
101
|
+
[t.identifier("node")],
|
|
102
|
+
t.blockStatement([
|
|
103
|
+
t.variableDeclaration("const", [
|
|
104
|
+
t.variableDeclarator(
|
|
105
|
+
t.identifier("componentRef"),
|
|
106
|
+
createThisMemberExpression("ref")
|
|
107
|
+
),
|
|
108
|
+
]),
|
|
109
|
+
t.ifStatement(
|
|
110
|
+
t.binaryExpression(
|
|
111
|
+
"===",
|
|
112
|
+
t.unaryExpression("typeof", t.identifier("componentRef")),
|
|
113
|
+
t.stringLiteral("function")
|
|
114
|
+
),
|
|
115
|
+
t.blockStatement([
|
|
116
|
+
t.expressionStatement(
|
|
117
|
+
t.callExpression(t.identifier("componentRef"), [t.identifier("node")])
|
|
118
|
+
),
|
|
119
|
+
]),
|
|
120
|
+
t.ifStatement(
|
|
121
|
+
t.logicalExpression(
|
|
122
|
+
"&&",
|
|
123
|
+
t.identifier("componentRef"),
|
|
124
|
+
t.binaryExpression(
|
|
125
|
+
"===",
|
|
126
|
+
t.unaryExpression("typeof", t.identifier("componentRef")),
|
|
127
|
+
t.stringLiteral("object")
|
|
128
|
+
)
|
|
129
|
+
),
|
|
130
|
+
t.blockStatement([
|
|
131
|
+
t.expressionStatement(
|
|
132
|
+
t.assignmentExpression(
|
|
133
|
+
"=",
|
|
134
|
+
t.memberExpression(t.identifier("componentRef"), t.identifier("current")),
|
|
135
|
+
t.identifier("node")
|
|
136
|
+
)
|
|
137
|
+
),
|
|
138
|
+
])
|
|
139
|
+
)
|
|
140
|
+
),
|
|
141
|
+
])
|
|
142
|
+
),
|
|
143
|
+
t.arrayExpression([createThisMemberExpression("ref")]),
|
|
144
|
+
])
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function hasRefProp(functionPath) {
|
|
149
|
+
const [firstParam] = functionPath.node.params || [];
|
|
150
|
+
if (!firstParam) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (t.isObjectPattern(firstParam)) {
|
|
155
|
+
return firstParam.properties.some((property) => {
|
|
156
|
+
if (!t.isObjectProperty(property)) return false;
|
|
157
|
+
return (
|
|
158
|
+
t.isIdentifier(property.key, { name: "ref" }) ||
|
|
159
|
+
t.isStringLiteral(property.key, { value: "ref" })
|
|
160
|
+
);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (t.isAssignmentPattern(firstParam) && t.isObjectPattern(firstParam.left)) {
|
|
165
|
+
return firstParam.left.properties.some((property) => {
|
|
166
|
+
if (!t.isObjectProperty(property)) return false;
|
|
167
|
+
return (
|
|
168
|
+
t.isIdentifier(property.key, { name: "ref" }) ||
|
|
169
|
+
t.isStringLiteral(property.key, { value: "ref" })
|
|
170
|
+
);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (t.isIdentifier(firstParam)) {
|
|
175
|
+
const binding = functionPath.scope.getBinding(firstParam.name);
|
|
176
|
+
if (!binding) return false;
|
|
177
|
+
return binding.referencePaths.some((refPath) => {
|
|
178
|
+
const parentPath = refPath.parentPath;
|
|
179
|
+
return Boolean(
|
|
180
|
+
parentPath &&
|
|
181
|
+
parentPath.isMemberExpression() &&
|
|
182
|
+
parentPath.node.object === refPath.node &&
|
|
183
|
+
t.isIdentifier(parentPath.node.property, { name: "ref" }) &&
|
|
184
|
+
!parentPath.node.computed
|
|
185
|
+
);
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function lowerForwardedElementRefs(functionPath, propName) {
|
|
193
|
+
if (!propName) {
|
|
194
|
+
return [];
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const callbackStatements = [];
|
|
198
|
+
const seenRefNames = new Set();
|
|
199
|
+
|
|
200
|
+
functionPath.traverse({
|
|
201
|
+
JSXAttribute(attrPath) {
|
|
202
|
+
if (!t.isJSXIdentifier(attrPath.node.name, { name: "ref" })) return;
|
|
203
|
+
|
|
204
|
+
const value = attrPath.node.value;
|
|
205
|
+
if (!t.isJSXExpressionContainer(value)) return;
|
|
206
|
+
if (
|
|
207
|
+
!t.isMemberExpression(value.expression) ||
|
|
208
|
+
!t.isThisExpression(value.expression.object) ||
|
|
209
|
+
!t.isIdentifier(value.expression.property, { name: propName }) ||
|
|
210
|
+
value.expression.computed
|
|
211
|
+
) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const openingElement = attrPath.parentPath;
|
|
216
|
+
if (!openingElement?.isJSXOpeningElement()) return;
|
|
217
|
+
if (!isStandardElementJsxName(openingElement.node.name)) return;
|
|
218
|
+
|
|
219
|
+
const managedRefName = functionPath.scope.generateUidIdentifier(`${propName}Element`).name;
|
|
220
|
+
attrPath.replaceWith(
|
|
221
|
+
t.jsxAttribute(t.jsxIdentifier("data-ref"), t.stringLiteral(managedRefName))
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
if (seenRefNames.has(managedRefName)) return;
|
|
225
|
+
seenRefNames.add(managedRefName);
|
|
226
|
+
callbackStatements.push(createForwardedTargetRefSyncStatement(propName, managedRefName));
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
return callbackStatements;
|
|
231
|
+
}
|