@murky-web/oxlint-plugin-solid 0.1.1 → 0.1.3
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/package.json
CHANGED
|
@@ -15,18 +15,44 @@ function isAstNode(value) {
|
|
|
15
15
|
);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
);
|
|
18
|
+
function hasJsxElementReturnType(node) {
|
|
19
|
+
const rt = node.returnType?.typeAnnotation;
|
|
20
|
+
if (!rt) return false;
|
|
21
|
+
// JSX.Element
|
|
22
|
+
if (
|
|
23
|
+
rt.type === "TSTypeReference" &&
|
|
24
|
+
rt.typeName?.type === "TSQualifiedName" &&
|
|
25
|
+
rt.typeName.left?.name === "JSX" &&
|
|
26
|
+
rt.typeName.right?.name === "Element"
|
|
27
|
+
) return true;
|
|
28
|
+
// JSX.Element via plain identifier (rare but possible)
|
|
29
|
+
if (rt.type === "TSTypeReference" && rt.typeName?.name === "JSXElement") return true;
|
|
30
|
+
return false;
|
|
28
31
|
}
|
|
29
32
|
|
|
33
|
+
function isFunctionComponent(node) {
|
|
34
|
+
if (
|
|
35
|
+
node.type !== "FunctionDeclaration" ||
|
|
36
|
+
node.async === true ||
|
|
37
|
+
node.generator === true ||
|
|
38
|
+
node.typeParameters
|
|
39
|
+
) return false;
|
|
40
|
+
|
|
41
|
+
if (node.id?.type !== "Identifier") return false;
|
|
42
|
+
|
|
43
|
+
// exported PascalCase — classic component
|
|
44
|
+
const isExported = node.parent?.type === "ExportNamedDeclaration";
|
|
45
|
+
if (isExported && isPascalCaseComponentName(node.id.name)) return true;
|
|
46
|
+
|
|
47
|
+
// has JSX.Element return type annotation — intent is clear regardless of export/casing
|
|
48
|
+
if (hasJsxElementReturnType(node)) return true;
|
|
49
|
+
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Keep old name as alias so call sites don't need changing
|
|
54
|
+
const isExportedFunctionComponent = isFunctionComponent;
|
|
55
|
+
|
|
30
56
|
function isJsxLikeNode(node) {
|
|
31
57
|
return node?.type === "JSXElement" || node?.type === "JSXFragment";
|
|
32
58
|
}
|
|
@@ -312,14 +338,14 @@ function buildReplacementText({
|
|
|
312
338
|
typeReference,
|
|
313
339
|
usesChildren,
|
|
314
340
|
functionNode,
|
|
341
|
+
isExported,
|
|
315
342
|
}) {
|
|
316
|
-
const
|
|
317
|
-
const childrenComment = usesChildren ? "" : "";
|
|
343
|
+
const exportPrefix = isExported ? "export " : "";
|
|
318
344
|
const propsSuffix = getPropsSuffix(typeReference);
|
|
319
345
|
const paramsText = propsParam.text;
|
|
320
346
|
const bodyText = sourceCode.getText(functionNode.body);
|
|
321
347
|
|
|
322
|
-
return
|
|
348
|
+
return `${exportPrefix}const ${componentName}: ${localTypeName}${propsSuffix} = (${paramsText}) => ${bodyText}`;
|
|
323
349
|
}
|
|
324
350
|
|
|
325
351
|
function createRuleFix({
|
|
@@ -331,10 +357,12 @@ function createRuleFix({
|
|
|
331
357
|
typeReference,
|
|
332
358
|
usesChildren,
|
|
333
359
|
}) {
|
|
360
|
+
const isExported = functionNode.parent?.type === "ExportNamedDeclaration";
|
|
334
361
|
const componentName = functionNode.id.name;
|
|
335
362
|
const replacementText = buildReplacementText({
|
|
336
363
|
componentName,
|
|
337
364
|
functionNode,
|
|
365
|
+
isExported,
|
|
338
366
|
localTypeName,
|
|
339
367
|
propsParam,
|
|
340
368
|
sourceCode,
|
|
@@ -342,7 +370,10 @@ function createRuleFix({
|
|
|
342
370
|
usesChildren,
|
|
343
371
|
});
|
|
344
372
|
|
|
345
|
-
|
|
373
|
+
// For exported functions, replace the ExportNamedDeclaration (the parent).
|
|
374
|
+
// For non-exported functions, replace only the FunctionDeclaration itself.
|
|
375
|
+
const nodeToReplace = isExported ? functionNode.parent : functionNode;
|
|
376
|
+
return fixer.replaceText(nodeToReplace, replacementText);
|
|
346
377
|
}
|
|
347
378
|
|
|
348
379
|
export const preferArrowComponentsRule = {
|
|
@@ -386,30 +417,53 @@ export const preferArrowComponentsRule = {
|
|
|
386
417
|
usesPropsChildren(node, propsParam.name);
|
|
387
418
|
const desiredTypeName = getDesiredSolidTypeName(usesChildren);
|
|
388
419
|
|
|
420
|
+
const { importDeclaration, localTypeName } = getSolidTypeImportInfo(
|
|
421
|
+
programNode,
|
|
422
|
+
desiredTypeName,
|
|
423
|
+
);
|
|
424
|
+
const resolvedTypeName = localTypeName !== "" ? localTypeName : desiredTypeName;
|
|
425
|
+
const needsNewImport = localTypeName === "" && !importDeclaration;
|
|
426
|
+
|
|
389
427
|
context.report({
|
|
390
428
|
fix(fixer) {
|
|
391
|
-
const
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
})
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
429
|
+
const isExported = node.parent?.type === "ExportNamedDeclaration";
|
|
430
|
+
const componentName = node.id.name;
|
|
431
|
+
const propsSuffix = getPropsSuffix(typeReference);
|
|
432
|
+
const paramsText = propsParam.text;
|
|
433
|
+
const bodyText = sourceCode.getText(node.body);
|
|
434
|
+
const exportPrefix = isExported ? "export " : "";
|
|
435
|
+
|
|
436
|
+
// If we need a new import AND the function is not exported,
|
|
437
|
+
// prepend the import declaration directly into the replacement text
|
|
438
|
+
// to avoid multi-range fix conflicts.
|
|
439
|
+
const importPrefix = needsNewImport
|
|
440
|
+
? `import type { ${desiredTypeName} } from "solid-js";\n`
|
|
441
|
+
: "";
|
|
442
|
+
|
|
443
|
+
const replacementText = `${importPrefix}${exportPrefix}const ${componentName}: ${resolvedTypeName}${propsSuffix} = (${paramsText}) => ${bodyText}`;
|
|
444
|
+
const nodeToReplace = isExported ? node.parent : node;
|
|
445
|
+
|
|
446
|
+
if (!needsNewImport && localTypeName === "" && importDeclaration) {
|
|
447
|
+
// Need to add to existing import — use two fixes only when
|
|
448
|
+
// the import is AFTER the function (safe, no overlap).
|
|
449
|
+
// Otherwise embed the import in the replacement.
|
|
450
|
+
const importEnd = importDeclaration.range?.[1] ?? 0;
|
|
451
|
+
const funcStart = nodeToReplace.range?.[0] ?? 0;
|
|
452
|
+
if (importEnd < funcStart) {
|
|
453
|
+
const typeKeyword = getTypeKeyword(importDeclaration, sourceCode);
|
|
454
|
+
const namedSpecifiers = getNamedImportSpecifiers(importDeclaration);
|
|
455
|
+
const lastSpec = namedSpecifiers.at(LAST_INDEX);
|
|
456
|
+
const importOp = lastSpec
|
|
457
|
+
? fixer.insertTextAfter(lastSpec, `, ${typeKeyword}${desiredTypeName}`)
|
|
458
|
+
: fixer.insertTextBefore(importDeclaration, `import type { ${desiredTypeName} } from "solid-js";\n`);
|
|
459
|
+
return [
|
|
460
|
+
fixer.replaceText(nodeToReplace, replacementText),
|
|
461
|
+
importOp,
|
|
462
|
+
];
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
return fixer.replaceText(nodeToReplace, replacementText);
|
|
413
467
|
},
|
|
414
468
|
message:
|
|
415
469
|
"Prefer export const components typed as Component<Props> or ParentComponent<Props>.",
|