@agent-scope/babel-plugin 1.0.1
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/dist/index.cjs +553 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +43 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.js +548 -0
- package/dist/index.js.map +1 -0
- package/package.json +46 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
// src/utils/ast-helpers.ts
|
|
6
|
+
function isComponentName(name) {
|
|
7
|
+
return /^[A-Z]/.test(name);
|
|
8
|
+
}
|
|
9
|
+
function isJSXNode(node) {
|
|
10
|
+
return node.type === "JSXElement" || node.type === "JSXFragment";
|
|
11
|
+
}
|
|
12
|
+
function bodyContainsJSX(node) {
|
|
13
|
+
for (const stmt of node.body) {
|
|
14
|
+
if (stmt.type === "ReturnStatement" && stmt.argument && isJSXNode(stmt.argument)) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
if (stmt.type === "ReturnStatement" && stmt.argument) {
|
|
18
|
+
if (containsJSXExpression(stmt.argument)) return true;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
function containsJSXExpression(node) {
|
|
24
|
+
if (isJSXNode(node)) return true;
|
|
25
|
+
if (node.type === "ConditionalExpression") {
|
|
26
|
+
return containsJSXExpression(node.consequent) || containsJSXExpression(node.alternate);
|
|
27
|
+
}
|
|
28
|
+
if (node.type === "LogicalExpression") {
|
|
29
|
+
return containsJSXExpression(node.left) || containsJSXExpression(node.right);
|
|
30
|
+
}
|
|
31
|
+
if (node.type === "ParenthesizedExpression") {
|
|
32
|
+
return containsJSXExpression(node.expression);
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
function isReactMemoCall(node) {
|
|
37
|
+
const callee = node.callee;
|
|
38
|
+
if (callee.type === "Identifier" && callee.name === "memo") return true;
|
|
39
|
+
if (callee.type === "MemberExpression" && callee.object.type === "Identifier" && callee.object.name === "React" && callee.property.type === "Identifier" && callee.property.name === "memo") {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
function isReactForwardRefCall(node) {
|
|
45
|
+
const callee = node.callee;
|
|
46
|
+
if (callee.type === "Identifier" && callee.name === "forwardRef") return true;
|
|
47
|
+
if (callee.type === "MemberExpression" && callee.object.type === "Identifier" && callee.object.name === "React" && callee.property.type === "Identifier" && callee.property.name === "forwardRef") {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/visitors/component-detector.ts
|
|
54
|
+
function detectFunctionDeclaration(node) {
|
|
55
|
+
if (!node.id || !isComponentName(node.id.name)) return null;
|
|
56
|
+
if (!node.body || !bodyContainsJSX(node.body)) return null;
|
|
57
|
+
const loc = node.loc?.start ?? { line: 0, column: 0 };
|
|
58
|
+
return {
|
|
59
|
+
name: node.id.name,
|
|
60
|
+
line: loc.line,
|
|
61
|
+
column: loc.column,
|
|
62
|
+
bindingIdentifierName: node.id.name
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function detectVariableDeclaration(node) {
|
|
66
|
+
for (const declarator of node.declarations) {
|
|
67
|
+
if (declarator.id.type !== "Identifier") continue;
|
|
68
|
+
const name = declarator.id.name;
|
|
69
|
+
if (!isComponentName(name)) continue;
|
|
70
|
+
if (!declarator.init) continue;
|
|
71
|
+
if (isFunctionReturningJSX(declarator.init)) {
|
|
72
|
+
const loc = node.loc?.start ?? { line: 0, column: 0 };
|
|
73
|
+
return { name, line: loc.line, column: loc.column, bindingIdentifierName: name };
|
|
74
|
+
}
|
|
75
|
+
if (declarator.init.type === "CallExpression" && (isReactMemoCall(declarator.init) || isReactForwardRefCall(declarator.init))) {
|
|
76
|
+
const loc = node.loc?.start ?? { line: 0, column: 0 };
|
|
77
|
+
return { name, line: loc.line, column: loc.column, bindingIdentifierName: name };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
function isFunctionReturningJSX(node) {
|
|
83
|
+
if (node.type === "ArrowFunctionExpression") {
|
|
84
|
+
if (node.body.type !== "BlockStatement") {
|
|
85
|
+
return containsJSXExpression(node.body);
|
|
86
|
+
}
|
|
87
|
+
return bodyContainsJSX(node.body);
|
|
88
|
+
}
|
|
89
|
+
if (node.type === "FunctionExpression") {
|
|
90
|
+
if (!node.body) return false;
|
|
91
|
+
return bodyContainsJSX(node.body);
|
|
92
|
+
}
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/visitors/prop-type-extractor.ts
|
|
97
|
+
function resolvePropType(node) {
|
|
98
|
+
switch (node.type) {
|
|
99
|
+
case "TSStringKeyword":
|
|
100
|
+
return "string";
|
|
101
|
+
case "TSNumberKeyword":
|
|
102
|
+
return "number";
|
|
103
|
+
case "TSBooleanKeyword":
|
|
104
|
+
return "boolean";
|
|
105
|
+
case "TSVoidKeyword":
|
|
106
|
+
case "TSUndefinedKeyword":
|
|
107
|
+
case "TSNullKeyword":
|
|
108
|
+
case "TSNeverKeyword":
|
|
109
|
+
case "TSAnyKeyword":
|
|
110
|
+
case "TSUnknownKeyword":
|
|
111
|
+
return "unknown";
|
|
112
|
+
case "TSArrayType":
|
|
113
|
+
case "TSTupleType":
|
|
114
|
+
return "array";
|
|
115
|
+
case "TSTypeLiteral":
|
|
116
|
+
case "TSMappedType":
|
|
117
|
+
return "object";
|
|
118
|
+
case "TSFunctionType":
|
|
119
|
+
case "TSConstructorType":
|
|
120
|
+
return "function";
|
|
121
|
+
case "TSTypeReference": {
|
|
122
|
+
const typeName = resolveTypeReferenceName(node);
|
|
123
|
+
if (typeName === "ReactNode" || typeName === "React.ReactNode" || typeName === "ReactElement" || typeName === "React.ReactElement") {
|
|
124
|
+
return "ReactNode";
|
|
125
|
+
}
|
|
126
|
+
if (typeName === "MouseEvent" || typeName === "KeyboardEvent" || typeName === "ChangeEvent" || typeName === "FocusEvent" || typeName === "SyntheticEvent" || typeName === "EventHandler" || typeName?.startsWith("React.") && typeName.endsWith("Handler")) {
|
|
127
|
+
return "function";
|
|
128
|
+
}
|
|
129
|
+
return "unknown";
|
|
130
|
+
}
|
|
131
|
+
case "TSUnionType": {
|
|
132
|
+
const members = node.types;
|
|
133
|
+
if (members.length > 0 && members.every((m) => m.type === "TSLiteralType")) {
|
|
134
|
+
return "union";
|
|
135
|
+
}
|
|
136
|
+
return "unknown";
|
|
137
|
+
}
|
|
138
|
+
case "TSIntersectionType":
|
|
139
|
+
case "TSConditionalType":
|
|
140
|
+
case "TSIndexedAccessType":
|
|
141
|
+
case "TSTypeQuery":
|
|
142
|
+
case "TSTemplateLiteralType":
|
|
143
|
+
return "unknown";
|
|
144
|
+
default:
|
|
145
|
+
return "unknown";
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function resolveTypeReferenceName(node) {
|
|
149
|
+
const { typeName } = node;
|
|
150
|
+
if (typeName.type === "Identifier") return typeName.name;
|
|
151
|
+
if (typeName.type === "TSQualifiedName") {
|
|
152
|
+
const left = typeName.left.type === "Identifier" ? typeName.left.name : "";
|
|
153
|
+
return `${left}.${typeName.right.name}`;
|
|
154
|
+
}
|
|
155
|
+
return void 0;
|
|
156
|
+
}
|
|
157
|
+
function extractLiterals(unionNode) {
|
|
158
|
+
const values = [];
|
|
159
|
+
for (const member of unionNode.types) {
|
|
160
|
+
if (member.type !== "TSLiteralType") return void 0;
|
|
161
|
+
const lit = member.literal;
|
|
162
|
+
if (lit.type === "StringLiteral") {
|
|
163
|
+
values.push(lit.value);
|
|
164
|
+
} else if (lit.type === "NumericLiteral") {
|
|
165
|
+
values.push(String(lit.value));
|
|
166
|
+
} else {
|
|
167
|
+
return void 0;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return values.length > 0 ? values : void 0;
|
|
171
|
+
}
|
|
172
|
+
function extractDefaultValues(params) {
|
|
173
|
+
const defaults = {};
|
|
174
|
+
if (params.length === 0) return defaults;
|
|
175
|
+
const firstParam = params[0];
|
|
176
|
+
if (!firstParam) return defaults;
|
|
177
|
+
let pattern = null;
|
|
178
|
+
if (firstParam.type === "ObjectPattern") {
|
|
179
|
+
pattern = firstParam;
|
|
180
|
+
} else if (firstParam.type === "AssignmentPattern" && firstParam.left.type === "ObjectPattern") {
|
|
181
|
+
pattern = firstParam.left;
|
|
182
|
+
}
|
|
183
|
+
if (pattern?.type === "ObjectPattern") {
|
|
184
|
+
for (const prop of pattern.properties) {
|
|
185
|
+
if (prop.type !== "ObjectProperty") continue;
|
|
186
|
+
if (prop.key.type !== "Identifier") continue;
|
|
187
|
+
const propName = prop.key.name;
|
|
188
|
+
if (prop.value.type === "AssignmentPattern") {
|
|
189
|
+
const defaultNode = prop.value.right;
|
|
190
|
+
const extracted = extractLiteralValue(defaultNode);
|
|
191
|
+
if (extracted !== void 0) {
|
|
192
|
+
defaults[propName] = extracted;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return defaults;
|
|
198
|
+
}
|
|
199
|
+
function extractLiteralValue(node) {
|
|
200
|
+
if (node.type === "StringLiteral") return node.value;
|
|
201
|
+
if (node.type === "NumericLiteral") return node.value;
|
|
202
|
+
if (node.type === "BooleanLiteral") return node.value;
|
|
203
|
+
if (node.type === "NullLiteral") return null;
|
|
204
|
+
if (node.type === "UnaryExpression" && node.operator === "-" && node.argument.type === "NumericLiteral") {
|
|
205
|
+
return -node.argument.value;
|
|
206
|
+
}
|
|
207
|
+
return void 0;
|
|
208
|
+
}
|
|
209
|
+
function extractPropsFromTypeMembers(members, defaults) {
|
|
210
|
+
const props = {};
|
|
211
|
+
for (const member of members) {
|
|
212
|
+
if (member.type !== "TSPropertySignature") continue;
|
|
213
|
+
if (member.key.type !== "Identifier") continue;
|
|
214
|
+
const propName = member.key.name;
|
|
215
|
+
const required = !member.optional;
|
|
216
|
+
if (!member.typeAnnotation) {
|
|
217
|
+
props[propName] = { type: "unknown", required };
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
const tsType = member.typeAnnotation.typeAnnotation;
|
|
221
|
+
let propType = resolvePropType(tsType);
|
|
222
|
+
let values;
|
|
223
|
+
if (tsType.type === "TSUnionType") {
|
|
224
|
+
const literals = extractLiterals(tsType);
|
|
225
|
+
if (literals) {
|
|
226
|
+
propType = "union";
|
|
227
|
+
values = literals;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
const meta = { type: propType, required };
|
|
231
|
+
if (values) meta.values = values;
|
|
232
|
+
const defaultVal = defaults[propName];
|
|
233
|
+
if (defaultVal !== void 0) meta.defaultValue = defaultVal;
|
|
234
|
+
props[propName] = meta;
|
|
235
|
+
}
|
|
236
|
+
return props;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// src/visitors/props-injector.ts
|
|
240
|
+
function buildPrimitiveLiteral(t, value) {
|
|
241
|
+
if (typeof value === "string") return t.stringLiteral(value);
|
|
242
|
+
if (typeof value === "number") return t.numericLiteral(value);
|
|
243
|
+
if (typeof value === "boolean") return t.booleanLiteral(value);
|
|
244
|
+
if (value === null) return t.nullLiteral();
|
|
245
|
+
return t.stringLiteral(String(value));
|
|
246
|
+
}
|
|
247
|
+
function buildPropMetaObject(t, meta) {
|
|
248
|
+
const props = [
|
|
249
|
+
t.objectProperty(t.identifier("type"), t.stringLiteral(meta.type)),
|
|
250
|
+
t.objectProperty(t.identifier("required"), t.booleanLiteral(meta.required))
|
|
251
|
+
];
|
|
252
|
+
if (meta.values !== void 0) {
|
|
253
|
+
props.push(
|
|
254
|
+
t.objectProperty(
|
|
255
|
+
t.identifier("values"),
|
|
256
|
+
t.arrayExpression(meta.values.map((v) => t.stringLiteral(v)))
|
|
257
|
+
)
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
if (meta.defaultValue !== void 0) {
|
|
261
|
+
props.push(
|
|
262
|
+
t.objectProperty(t.identifier("defaultValue"), buildPrimitiveLiteral(t, meta.defaultValue))
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
return t.objectExpression(props);
|
|
266
|
+
}
|
|
267
|
+
function buildScopePropsStatement(t, name, props) {
|
|
268
|
+
const entries = Object.entries(props).map(
|
|
269
|
+
([key, meta]) => t.objectProperty(t.identifier(key), buildPropMetaObject(t, meta))
|
|
270
|
+
);
|
|
271
|
+
return t.expressionStatement(
|
|
272
|
+
t.assignmentExpression(
|
|
273
|
+
"=",
|
|
274
|
+
t.memberExpression(t.identifier(name), t.identifier("__scopeProps")),
|
|
275
|
+
t.objectExpression(entries)
|
|
276
|
+
)
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// src/visitors/source-injector.ts
|
|
281
|
+
function buildScopeSourceStatement(t, name, loc) {
|
|
282
|
+
return t.expressionStatement(
|
|
283
|
+
t.assignmentExpression(
|
|
284
|
+
"=",
|
|
285
|
+
t.memberExpression(t.identifier(name), t.identifier("__scopeSource")),
|
|
286
|
+
t.objectExpression([
|
|
287
|
+
t.objectProperty(t.identifier("filePath"), t.stringLiteral(loc.filePath)),
|
|
288
|
+
t.objectProperty(t.identifier("line"), t.numericLiteral(loc.line)),
|
|
289
|
+
t.objectProperty(t.identifier("column"), t.numericLiteral(loc.column))
|
|
290
|
+
])
|
|
291
|
+
)
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// src/index.ts
|
|
296
|
+
var typeMapsByProgram = /* @__PURE__ */ new WeakMap();
|
|
297
|
+
function scopeBabelPlugin(babel) {
|
|
298
|
+
const t = babel.types;
|
|
299
|
+
return {
|
|
300
|
+
name: "@agent-scope/babel-plugin",
|
|
301
|
+
visitor: {
|
|
302
|
+
/**
|
|
303
|
+
* Pre-collect all top-level type declarations at program entry, before
|
|
304
|
+
* @babel/plugin-transform-typescript removes them during traversal.
|
|
305
|
+
*
|
|
306
|
+
* This fires for the Program node before ANY child visitors, so all
|
|
307
|
+
* TSInterfaceDeclaration and TSTypeAliasDeclaration nodes are still present.
|
|
308
|
+
*/
|
|
309
|
+
Program: {
|
|
310
|
+
enter(path) {
|
|
311
|
+
const typeMap = /* @__PURE__ */ new Map();
|
|
312
|
+
for (const stmt of path.node.body) {
|
|
313
|
+
if (stmt.type === "TSInterfaceDeclaration") {
|
|
314
|
+
typeMap.set(stmt.id.name, stmt.body.body);
|
|
315
|
+
}
|
|
316
|
+
if (stmt.type === "ExportNamedDeclaration" && stmt.declaration?.type === "TSInterfaceDeclaration") {
|
|
317
|
+
typeMap.set(stmt.declaration.id.name, stmt.declaration.body.body);
|
|
318
|
+
}
|
|
319
|
+
if (stmt.type === "TSTypeAliasDeclaration" && stmt.typeAnnotation.type === "TSTypeLiteral") {
|
|
320
|
+
typeMap.set(stmt.id.name, stmt.typeAnnotation.members);
|
|
321
|
+
}
|
|
322
|
+
if (stmt.type === "ExportNamedDeclaration" && stmt.declaration?.type === "TSTypeAliasDeclaration" && stmt.declaration.typeAnnotation.type === "TSTypeLiteral") {
|
|
323
|
+
typeMap.set(stmt.declaration.id.name, stmt.declaration.typeAnnotation.members);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
typeMapsByProgram.set(path.node, typeMap);
|
|
327
|
+
}
|
|
328
|
+
},
|
|
329
|
+
FunctionDeclaration(path, state) {
|
|
330
|
+
if (path.parentPath?.isExportDefaultDeclaration()) return;
|
|
331
|
+
const component = detectFunctionDeclaration(path.node);
|
|
332
|
+
if (!component) return;
|
|
333
|
+
const filePath = resolveFilePath(state);
|
|
334
|
+
const typeMap = getTypeMap(path);
|
|
335
|
+
const sourceStmt = buildScopeSourceStatement(t, component.name, {
|
|
336
|
+
filePath,
|
|
337
|
+
line: component.line,
|
|
338
|
+
column: component.column
|
|
339
|
+
});
|
|
340
|
+
const scopeProps = safeExtractScopeProps(path.node, typeMap);
|
|
341
|
+
if (scopeProps) {
|
|
342
|
+
const propsStmt = buildScopePropsStatement(t, component.name, scopeProps);
|
|
343
|
+
path.insertAfter(propsStmt);
|
|
344
|
+
path.insertAfter(sourceStmt);
|
|
345
|
+
} else {
|
|
346
|
+
path.insertAfter(sourceStmt);
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
VariableDeclaration(path, state) {
|
|
350
|
+
const component = detectVariableDeclaration(path.node);
|
|
351
|
+
if (!component) return;
|
|
352
|
+
const filePath = resolveFilePath(state);
|
|
353
|
+
const typeMap = getTypeMap(path);
|
|
354
|
+
const sourceStmt = buildScopeSourceStatement(t, component.name, {
|
|
355
|
+
filePath,
|
|
356
|
+
line: component.line,
|
|
357
|
+
column: component.column
|
|
358
|
+
});
|
|
359
|
+
const scopeProps = safeExtractPropsFromVariableDecl(path.node, typeMap);
|
|
360
|
+
if (scopeProps) {
|
|
361
|
+
const propsStmt = buildScopePropsStatement(t, component.name, scopeProps);
|
|
362
|
+
path.insertAfter(propsStmt);
|
|
363
|
+
path.insertAfter(sourceStmt);
|
|
364
|
+
} else {
|
|
365
|
+
path.insertAfter(sourceStmt);
|
|
366
|
+
}
|
|
367
|
+
},
|
|
368
|
+
ExportDefaultDeclaration(path, state) {
|
|
369
|
+
const decl = path.node.declaration;
|
|
370
|
+
if (decl.type === "FunctionDeclaration" && decl.id && decl.body) {
|
|
371
|
+
const component = detectFunctionDeclaration(decl);
|
|
372
|
+
if (!component) return;
|
|
373
|
+
const filePath = resolveFilePath(state);
|
|
374
|
+
const typeMap = getTypeMap(path);
|
|
375
|
+
const sourceStmt = buildScopeSourceStatement(t, component.name, {
|
|
376
|
+
filePath,
|
|
377
|
+
line: component.line,
|
|
378
|
+
column: component.column
|
|
379
|
+
});
|
|
380
|
+
const scopeProps = safeExtractScopeProps(decl, typeMap);
|
|
381
|
+
if (scopeProps) {
|
|
382
|
+
const propsStmt = buildScopePropsStatement(t, component.name, scopeProps);
|
|
383
|
+
path.insertAfter(propsStmt);
|
|
384
|
+
path.insertAfter(sourceStmt);
|
|
385
|
+
} else {
|
|
386
|
+
path.insertAfter(sourceStmt);
|
|
387
|
+
}
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
if (decl.type === "ArrowFunctionExpression" || decl.type === "FunctionDeclaration" && !decl.id) {
|
|
391
|
+
if (!checkAnonymousReturnsJSX(decl)) return;
|
|
392
|
+
const tempName = "_ScopeDefaultExport";
|
|
393
|
+
const filePath = resolveFilePath(state);
|
|
394
|
+
const loc = decl.loc?.start ?? { line: 0, column: 0 };
|
|
395
|
+
const typeMap = getTypeMap(path);
|
|
396
|
+
const sourceStmt = buildScopeSourceStatement(t, tempName, {
|
|
397
|
+
filePath,
|
|
398
|
+
line: loc.line,
|
|
399
|
+
column: loc.column
|
|
400
|
+
});
|
|
401
|
+
const scopeProps = safeExtractScopeProps(
|
|
402
|
+
decl,
|
|
403
|
+
typeMap
|
|
404
|
+
);
|
|
405
|
+
const replacements = [
|
|
406
|
+
t.variableDeclaration("const", [
|
|
407
|
+
t.variableDeclarator(t.identifier(tempName), decl)
|
|
408
|
+
]),
|
|
409
|
+
sourceStmt
|
|
410
|
+
];
|
|
411
|
+
if (scopeProps) {
|
|
412
|
+
replacements.push(buildScopePropsStatement(t, tempName, scopeProps));
|
|
413
|
+
}
|
|
414
|
+
replacements.push(t.exportDefaultDeclaration(t.identifier(tempName)));
|
|
415
|
+
path.replaceWithMultiple(replacements);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
function resolveFilePath(state) {
|
|
422
|
+
return state.opts.filePath ?? state.filename ?? "<unknown>";
|
|
423
|
+
}
|
|
424
|
+
function getTypeMap(path) {
|
|
425
|
+
const program = path.findParent((p) => p.isProgram());
|
|
426
|
+
if (!program || !program.isProgram()) return /* @__PURE__ */ new Map();
|
|
427
|
+
return typeMapsByProgram.get(program.node) ?? /* @__PURE__ */ new Map();
|
|
428
|
+
}
|
|
429
|
+
function safeExtractScopeProps(node, typeMap) {
|
|
430
|
+
try {
|
|
431
|
+
return extractScopePropsWithMap(node, typeMap);
|
|
432
|
+
} catch {
|
|
433
|
+
return null;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
function extractScopePropsWithMap(funcNode, typeMap) {
|
|
437
|
+
const params = funcNode.params;
|
|
438
|
+
if (params.length === 0) return null;
|
|
439
|
+
const firstParam = params[0];
|
|
440
|
+
if (!firstParam) return null;
|
|
441
|
+
const defaults = extractDefaultValues(params);
|
|
442
|
+
let typeAnnotation = null;
|
|
443
|
+
if (firstParam.type === "Identifier" || firstParam.type === "ObjectPattern") {
|
|
444
|
+
if (firstParam.typeAnnotation?.type === "TSTypeAnnotation") {
|
|
445
|
+
typeAnnotation = firstParam.typeAnnotation.typeAnnotation;
|
|
446
|
+
}
|
|
447
|
+
} else if (firstParam.type === "AssignmentPattern") {
|
|
448
|
+
const left = firstParam.left;
|
|
449
|
+
if (left.type === "Identifier" || left.type === "ObjectPattern") {
|
|
450
|
+
if (left.typeAnnotation?.type === "TSTypeAnnotation") {
|
|
451
|
+
typeAnnotation = left.typeAnnotation.typeAnnotation;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
if (!typeAnnotation) return null;
|
|
456
|
+
if (typeAnnotation.type === "TSTypeLiteral") {
|
|
457
|
+
const props = extractPropsFromTypeMembers(typeAnnotation.members, defaults);
|
|
458
|
+
return Object.keys(props).length > 0 ? props : null;
|
|
459
|
+
}
|
|
460
|
+
if (typeAnnotation.type === "TSTypeReference") {
|
|
461
|
+
const typeName = resolveTypeReferenceName(typeAnnotation);
|
|
462
|
+
if (!typeName) return null;
|
|
463
|
+
const members = typeMap.get(typeName);
|
|
464
|
+
if (!members) return null;
|
|
465
|
+
const props = extractPropsFromTypeMembers(members, defaults);
|
|
466
|
+
return Object.keys(props).length > 0 ? props : null;
|
|
467
|
+
}
|
|
468
|
+
return null;
|
|
469
|
+
}
|
|
470
|
+
function safeExtractPropsFromVariableDecl(node, typeMap) {
|
|
471
|
+
try {
|
|
472
|
+
for (const declarator of node.declarations) {
|
|
473
|
+
if (declarator.id.type !== "Identifier") continue;
|
|
474
|
+
if (!declarator.init) continue;
|
|
475
|
+
const init = declarator.init;
|
|
476
|
+
if (declarator.id.typeAnnotation?.type === "TSTypeAnnotation") {
|
|
477
|
+
const tsType = declarator.id.typeAnnotation.typeAnnotation;
|
|
478
|
+
if (tsType.type === "TSTypeReference") {
|
|
479
|
+
const typeName = resolveTypeReferenceName(tsType);
|
|
480
|
+
if (typeName === "React.FC" || typeName === "FC" || typeName === "React.FunctionComponent" || typeName === "FunctionComponent") {
|
|
481
|
+
const typeParams = tsType.typeParameters;
|
|
482
|
+
const funcNode2 = extractFuncNode(init);
|
|
483
|
+
const defaults = funcNode2 ? extractDefaultValues(funcNode2.params) : {};
|
|
484
|
+
if (typeParams && typeParams.params.length > 0) {
|
|
485
|
+
const firstTypeArg = typeParams.params[0];
|
|
486
|
+
if (!firstTypeArg) continue;
|
|
487
|
+
if (firstTypeArg.type === "TSTypeLiteral") {
|
|
488
|
+
const props = extractPropsFromTypeMembers(firstTypeArg.members, defaults);
|
|
489
|
+
if (Object.keys(props).length > 0) return props;
|
|
490
|
+
} else if (firstTypeArg.type === "TSTypeReference") {
|
|
491
|
+
const refName = resolveTypeReferenceName(firstTypeArg);
|
|
492
|
+
if (refName) {
|
|
493
|
+
const members = typeMap.get(refName);
|
|
494
|
+
if (members) {
|
|
495
|
+
const props = extractPropsFromTypeMembers(members, defaults);
|
|
496
|
+
if (Object.keys(props).length > 0) return props;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
const funcNode = extractFuncNode(init);
|
|
505
|
+
if (funcNode) {
|
|
506
|
+
const props = extractScopePropsWithMap(funcNode, typeMap);
|
|
507
|
+
if (props) return props;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return null;
|
|
511
|
+
} catch {
|
|
512
|
+
return null;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
function extractFuncNode(node) {
|
|
516
|
+
if (node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression") {
|
|
517
|
+
return node;
|
|
518
|
+
}
|
|
519
|
+
if (node.type === "CallExpression" && node.arguments.length > 0) {
|
|
520
|
+
const first = node.arguments[0];
|
|
521
|
+
if (first !== void 0 && (first.type === "ArrowFunctionExpression" || first.type === "FunctionExpression")) {
|
|
522
|
+
return first;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
return null;
|
|
526
|
+
}
|
|
527
|
+
function checkAnonymousReturnsJSX(decl) {
|
|
528
|
+
if (decl.type === "ArrowFunctionExpression") {
|
|
529
|
+
const body = decl.body;
|
|
530
|
+
if (body.type !== "BlockStatement") {
|
|
531
|
+
return body.type === "JSXElement" || body.type === "JSXFragment";
|
|
532
|
+
}
|
|
533
|
+
return bodyHasJSXReturn(body);
|
|
534
|
+
}
|
|
535
|
+
if (decl.type === "FunctionDeclaration" && decl.body) {
|
|
536
|
+
return bodyHasJSXReturn(decl.body);
|
|
537
|
+
}
|
|
538
|
+
return false;
|
|
539
|
+
}
|
|
540
|
+
function bodyHasJSXReturn(block) {
|
|
541
|
+
for (const stmt of block.body) {
|
|
542
|
+
if (stmt.type === "ReturnStatement" && stmt.argument && (stmt.argument.type === "JSXElement" || stmt.argument.type === "JSXFragment")) {
|
|
543
|
+
return true;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
return false;
|
|
547
|
+
}
|
|
548
|
+
var index_default = scopeBabelPlugin;
|
|
549
|
+
|
|
550
|
+
exports.default = index_default;
|
|
551
|
+
exports.scopeBabelPlugin = scopeBabelPlugin;
|
|
552
|
+
//# sourceMappingURL=index.cjs.map
|
|
553
|
+
//# sourceMappingURL=index.cjs.map
|